Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.doze; |
| 18 | |
| 19 | import static org.mockito.ArgumentMatchers.any; |
| 20 | import static org.mockito.ArgumentMatchers.anyInt; |
Lucas Dupin | ed5b7a9 | 2019-03-20 11:00:27 -0700 | [diff] [blame] | 21 | import static org.mockito.ArgumentMatchers.eq; |
Lucas Dupin | 3174c66 | 2019-07-15 15:49:54 -0700 | [diff] [blame] | 22 | import static org.mockito.Mockito.clearInvocations; |
Lucas Dupin | edbe45b | 2019-10-14 15:43:03 -0700 | [diff] [blame] | 23 | import static org.mockito.Mockito.doAnswer; |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 24 | import static org.mockito.Mockito.mock; |
| 25 | import static org.mockito.Mockito.never; |
lpeter | 8a5f470 | 2019-01-18 16:53:07 +0800 | [diff] [blame] | 26 | import static org.mockito.Mockito.spy; |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 27 | import static org.mockito.Mockito.verify; |
| 28 | import static org.mockito.Mockito.when; |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 29 | |
Adrian Roos | 6023ccb | 2017-06-28 16:22:02 +0200 | [diff] [blame] | 30 | import android.app.AlarmManager; |
Lucas Dupin | 3174c66 | 2019-07-15 15:49:54 -0700 | [diff] [blame] | 31 | import android.hardware.Sensor; |
Issei Suzuki | ca19e6e | 2019-02-26 12:39:11 +0100 | [diff] [blame] | 32 | import android.hardware.display.AmbientDisplayConfiguration; |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 33 | import android.os.Handler; |
| 34 | import android.os.Looper; |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 35 | import android.testing.AndroidTestingRunner; |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 36 | import android.testing.TestableLooper; |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 37 | import android.testing.TestableLooper.RunWithLooper; |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 38 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 39 | import androidx.test.filters.SmallTest; |
| 40 | |
Jason Monk | 25a52b6 | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 41 | import com.android.systemui.SysuiTestCase; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 42 | import com.android.systemui.broadcast.BroadcastDispatcher; |
Lucas Dupin | 3174c66 | 2019-07-15 15:49:54 -0700 | [diff] [blame] | 43 | import com.android.systemui.dock.DockManager; |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 44 | import com.android.systemui.statusbar.phone.DozeParameters; |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 45 | import com.android.systemui.util.sensors.AsyncSensorManager; |
Dave Mankoff | bf52f4b | 2019-09-20 14:34:28 -0400 | [diff] [blame] | 46 | import com.android.systemui.util.sensors.FakeProximitySensor; |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 47 | import com.android.systemui.util.sensors.FakeSensorManager; |
Dave Mankoff | bf52f4b | 2019-09-20 14:34:28 -0400 | [diff] [blame] | 48 | import com.android.systemui.util.sensors.ProximitySensor; |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 49 | import com.android.systemui.util.wakelock.WakeLock; |
| 50 | import com.android.systemui.util.wakelock.WakeLockFake; |
| 51 | |
| 52 | import org.junit.Before; |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 53 | import org.junit.Test; |
Adrian Roos | 84a187f | 2017-04-28 14:10:51 -0700 | [diff] [blame] | 54 | import org.junit.runner.RunWith; |
Lucas Dupin | edbe45b | 2019-10-14 15:43:03 -0700 | [diff] [blame] | 55 | import org.mockito.ArgumentCaptor; |
| 56 | import org.mockito.Mock; |
| 57 | import org.mockito.MockitoAnnotations; |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 58 | |
Adrian Roos | 84a187f | 2017-04-28 14:10:51 -0700 | [diff] [blame] | 59 | @SmallTest |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 60 | @RunWith(AndroidTestingRunner.class) |
Lucas Dupin | 3174c66 | 2019-07-15 15:49:54 -0700 | [diff] [blame] | 61 | @RunWithLooper(setAsMainLooper = true) |
Jason Monk | 25a52b6 | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 62 | public class DozeTriggersTest extends SysuiTestCase { |
Lucas Dupin | edbe45b | 2019-10-14 15:43:03 -0700 | [diff] [blame] | 63 | |
| 64 | @Mock |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 65 | private DozeMachine mMachine; |
Lucas Dupin | edbe45b | 2019-10-14 15:43:03 -0700 | [diff] [blame] | 66 | @Mock |
| 67 | private DozeHost mHost; |
| 68 | @Mock |
| 69 | private AlarmManager mAlarmManager; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 70 | @Mock |
| 71 | private BroadcastDispatcher mBroadcastDispatcher; |
Jerry Chang | ae4dc4b | 2019-10-16 18:45:03 +0800 | [diff] [blame] | 72 | @Mock |
| 73 | private DockManager mDockManager; |
Lucas Dupin | edbe45b | 2019-10-14 15:43:03 -0700 | [diff] [blame] | 74 | private DozeTriggers mTriggers; |
Adrian Roos | 84a187f | 2017-04-28 14:10:51 -0700 | [diff] [blame] | 75 | private FakeSensorManager mSensors; |
Lucas Dupin | 3174c66 | 2019-07-15 15:49:54 -0700 | [diff] [blame] | 76 | private Sensor mTapSensor; |
Dave Mankoff | bf52f4b | 2019-09-20 14:34:28 -0400 | [diff] [blame] | 77 | private FakeProximitySensor mProximitySensor; |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 78 | |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 79 | @Before |
| 80 | public void setUp() throws Exception { |
Lucas Dupin | edbe45b | 2019-10-14 15:43:03 -0700 | [diff] [blame] | 81 | MockitoAnnotations.initMocks(this); |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 82 | AmbientDisplayConfiguration config = DozeConfigurationUtil.createMockConfig(); |
| 83 | DozeParameters parameters = DozeConfigurationUtil.createMockParameters(); |
Lucas Dupin | 3174c66 | 2019-07-15 15:49:54 -0700 | [diff] [blame] | 84 | mSensors = spy(new FakeSensorManager(mContext)); |
| 85 | mTapSensor = mSensors.getFakeTapSensor().getSensor(); |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 86 | WakeLock wakeLock = new WakeLockFake(); |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 87 | AsyncSensorManager asyncSensorManager = |
| 88 | new AsyncSensorManager(mSensors, null, new Handler()); |
Dave Mankoff | d94c969 | 2019-10-18 12:38:21 -0400 | [diff] [blame] | 89 | mProximitySensor = new FakeProximitySensor(getContext().getResources(), asyncSensorManager); |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 90 | |
Lucas Dupin | edbe45b | 2019-10-14 15:43:03 -0700 | [diff] [blame] | 91 | mTriggers = new DozeTriggers(mContext, mMachine, mHost, mAlarmManager, config, parameters, |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 92 | asyncSensorManager, Handler.createAsync(Looper.myLooper()), wakeLock, true, |
Jerry Chang | ae4dc4b | 2019-10-16 18:45:03 +0800 | [diff] [blame] | 93 | mDockManager, mProximitySensor, mock(DozeLog.class), mBroadcastDispatcher); |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 94 | waitForSensorManager(); |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Test |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 98 | public void testOnNotification_stillWorksAfterOneFailedProxCheck() throws Exception { |
| 99 | when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE); |
Lucas Dupin | edbe45b | 2019-10-14 15:43:03 -0700 | [diff] [blame] | 100 | ArgumentCaptor<DozeHost.Callback> captor = ArgumentCaptor.forClass(DozeHost.Callback.class); |
| 101 | doAnswer(invocation -> null).when(mHost).addCallback(captor.capture()); |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 102 | |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 103 | mTriggers.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); |
| 104 | mTriggers.transitionTo(DozeMachine.State.INITIALIZED, DozeMachine.State.DOZE); |
Lucas Dupin | 3174c66 | 2019-07-15 15:49:54 -0700 | [diff] [blame] | 105 | clearInvocations(mMachine); |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 106 | |
Dave Mankoff | bf52f4b | 2019-09-20 14:34:28 -0400 | [diff] [blame] | 107 | mProximitySensor.setLastEvent(new ProximitySensor.ProximityEvent(true, 1)); |
Lucas Dupin | edbe45b | 2019-10-14 15:43:03 -0700 | [diff] [blame] | 108 | captor.getValue().onNotificationAlerted(null /* pulseSuppressedListener */); |
Dave Mankoff | bf52f4b | 2019-09-20 14:34:28 -0400 | [diff] [blame] | 109 | mProximitySensor.alertListeners(); |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 110 | |
| 111 | verify(mMachine, never()).requestState(any()); |
Adrian Roos | 84a187f | 2017-04-28 14:10:51 -0700 | [diff] [blame] | 112 | verify(mMachine, never()).requestPulse(anyInt()); |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 113 | |
Lucas Dupin | edbe45b | 2019-10-14 15:43:03 -0700 | [diff] [blame] | 114 | captor.getValue().onNotificationAlerted(null /* pulseSuppressedListener */); |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 115 | waitForSensorManager(); |
Dave Mankoff | bf52f4b | 2019-09-20 14:34:28 -0400 | [diff] [blame] | 116 | mProximitySensor.setLastEvent(new ProximitySensor.ProximityEvent(false, 2)); |
| 117 | mProximitySensor.alertListeners(); |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 118 | |
Adrian Roos | d7b9d10 | 2017-04-28 15:42:58 -0700 | [diff] [blame] | 119 | verify(mMachine).requestPulse(anyInt()); |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 120 | } |
| 121 | |
lpeter | 8a5f470 | 2019-01-18 16:53:07 +0800 | [diff] [blame] | 122 | @Test |
Lucas Dupin | 3174c66 | 2019-07-15 15:49:54 -0700 | [diff] [blame] | 123 | public void testTransitionTo_disablesAndEnablesTouchSensors() { |
| 124 | when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE); |
| 125 | |
| 126 | mTriggers.transitionTo(DozeMachine.State.INITIALIZED, DozeMachine.State.DOZE); |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 127 | waitForSensorManager(); |
Lucas Dupin | 3174c66 | 2019-07-15 15:49:54 -0700 | [diff] [blame] | 128 | verify(mSensors).requestTriggerSensor(any(), eq(mTapSensor)); |
| 129 | |
| 130 | clearInvocations(mSensors); |
| 131 | mTriggers.transitionTo(DozeMachine.State.DOZE, |
| 132 | DozeMachine.State.DOZE_REQUEST_PULSE); |
| 133 | mTriggers.transitionTo(DozeMachine.State.DOZE_REQUEST_PULSE, |
| 134 | DozeMachine.State.DOZE_PULSING); |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 135 | waitForSensorManager(); |
Lucas Dupin | 3174c66 | 2019-07-15 15:49:54 -0700 | [diff] [blame] | 136 | verify(mSensors).cancelTriggerSensor(any(), eq(mTapSensor)); |
| 137 | |
| 138 | clearInvocations(mSensors); |
| 139 | mTriggers.transitionTo(DozeMachine.State.DOZE_PULSING, DozeMachine.State.DOZE_PULSE_DONE); |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 140 | waitForSensorManager(); |
Lucas Dupin | 3174c66 | 2019-07-15 15:49:54 -0700 | [diff] [blame] | 141 | verify(mSensors).requestTriggerSensor(any(), eq(mTapSensor)); |
| 142 | } |
| 143 | |
| 144 | @Test |
Jerry Chang | ae4dc4b | 2019-10-16 18:45:03 +0800 | [diff] [blame] | 145 | public void transitionToDockedAod_disablesTouchSensors() { |
| 146 | mTriggers.transitionTo(DozeMachine.State.INITIALIZED, DozeMachine.State.DOZE); |
| 147 | waitForSensorManager(); |
| 148 | verify(mSensors).requestTriggerSensor(any(), eq(mTapSensor)); |
| 149 | |
| 150 | mTriggers.transitionTo(DozeMachine.State.DOZE, DozeMachine.State.DOZE_AOD_DOCKED); |
| 151 | waitForSensorManager(); |
| 152 | |
| 153 | verify(mSensors).cancelTriggerSensor(any(), eq(mTapSensor)); |
| 154 | } |
| 155 | |
| 156 | @Test |
lpeter | 8a5f470 | 2019-01-18 16:53:07 +0800 | [diff] [blame] | 157 | public void testDockEventListener_registerAndUnregister() { |
| 158 | mTriggers.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); |
Jerry Chang | ae4dc4b | 2019-10-16 18:45:03 +0800 | [diff] [blame] | 159 | verify(mDockManager).addListener(any()); |
lpeter | 8a5f470 | 2019-01-18 16:53:07 +0800 | [diff] [blame] | 160 | |
| 161 | mTriggers.transitionTo(DozeMachine.State.DOZE, DozeMachine.State.FINISH); |
Jerry Chang | ae4dc4b | 2019-10-16 18:45:03 +0800 | [diff] [blame] | 162 | verify(mDockManager).removeListener(any()); |
lpeter | 8a5f470 | 2019-01-18 16:53:07 +0800 | [diff] [blame] | 163 | } |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 164 | |
Dave Mankoff | bf52f4b | 2019-09-20 14:34:28 -0400 | [diff] [blame] | 165 | @Test |
| 166 | public void testProximitySensorNotAvailablel() { |
| 167 | mProximitySensor.setSensorAvailable(false); |
Ned Burns | 30d6770 | 2020-01-28 12:58:45 -0500 | [diff] [blame] | 168 | mTriggers.onSensor(DozeLog.PULSE_REASON_SENSOR_LONG_PRESS, 100, 100, null); |
| 169 | mTriggers.onSensor(DozeLog.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN, 100, 100, |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 170 | new float[]{1}); |
Ned Burns | 30d6770 | 2020-01-28 12:58:45 -0500 | [diff] [blame] | 171 | mTriggers.onSensor(DozeLog.REASON_SENSOR_TAP, 100, 100, null); |
Dave Mankoff | bf52f4b | 2019-09-20 14:34:28 -0400 | [diff] [blame] | 172 | } |
| 173 | |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 174 | private void waitForSensorManager() { |
| 175 | TestableLooper.get(this).processAllMessages(); |
| 176 | } |
Geoffrey Pitsch | eefa3be | 2017-06-12 10:55:28 -0400 | [diff] [blame] | 177 | } |