Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 com.android.systemui.plugins.SensorManagerPlugin.Sensor.TYPE_WAKE_LOCK_SCREEN; |
| 20 | |
| 21 | import static org.mockito.ArgumentMatchers.any; |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 22 | import static org.mockito.ArgumentMatchers.anyFloat; |
| 23 | import static org.mockito.ArgumentMatchers.anyInt; |
| 24 | import static org.mockito.ArgumentMatchers.eq; |
Lucas Dupin | fac2e8e | 2019-06-27 16:10:19 -0700 | [diff] [blame] | 25 | import static org.mockito.Mockito.clearInvocations; |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 26 | import static org.mockito.Mockito.doAnswer; |
| 27 | import static org.mockito.Mockito.mock; |
| 28 | import static org.mockito.Mockito.never; |
| 29 | import static org.mockito.Mockito.reset; |
lpeter | aca9763 | 2019-06-11 11:13:54 +0800 | [diff] [blame] | 30 | import static org.mockito.Mockito.times; |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 31 | import static org.mockito.Mockito.verify; |
| 32 | import static org.mockito.Mockito.when; |
| 33 | |
| 34 | import android.app.AlarmManager; |
lpeter | aca9763 | 2019-06-11 11:13:54 +0800 | [diff] [blame] | 35 | import android.database.ContentObserver; |
Dave Mankoff | 86c7344 | 2019-12-17 12:15:16 -0500 | [diff] [blame] | 36 | import android.hardware.Sensor; |
Issei Suzuki | ca19e6e | 2019-02-26 12:39:11 +0100 | [diff] [blame] | 37 | import android.hardware.display.AmbientDisplayConfiguration; |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 38 | import android.testing.AndroidTestingRunner; |
| 39 | import android.testing.TestableLooper; |
| 40 | import android.testing.TestableLooper.RunWithLooper; |
| 41 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 42 | import androidx.test.filters.SmallTest; |
| 43 | |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 44 | import com.android.systemui.SysuiTestCase; |
lpeter | aca9763 | 2019-06-11 11:13:54 +0800 | [diff] [blame] | 45 | import com.android.systemui.doze.DozeSensors.TriggerSensor; |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 46 | import com.android.systemui.plugins.SensorManagerPlugin; |
| 47 | import com.android.systemui.statusbar.phone.DozeParameters; |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 48 | import com.android.systemui.util.sensors.AsyncSensorManager; |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 49 | import com.android.systemui.util.wakelock.WakeLock; |
| 50 | |
| 51 | import org.junit.Before; |
| 52 | import org.junit.Test; |
| 53 | import org.junit.runner.RunWith; |
| 54 | import org.mockito.Mock; |
| 55 | import org.mockito.MockitoAnnotations; |
| 56 | |
| 57 | import java.util.function.Consumer; |
| 58 | |
| 59 | @RunWith(AndroidTestingRunner.class) |
| 60 | @RunWithLooper |
| 61 | @SmallTest |
| 62 | public class DozeSensorsTest extends SysuiTestCase { |
| 63 | |
| 64 | @Mock |
| 65 | private AlarmManager mAlarmManager; |
| 66 | @Mock |
| 67 | private AsyncSensorManager mSensorManager; |
| 68 | @Mock |
| 69 | private DozeParameters mDozeParameters; |
| 70 | @Mock |
| 71 | private AmbientDisplayConfiguration mAmbientDisplayConfiguration; |
| 72 | @Mock |
| 73 | private WakeLock mWakeLock; |
| 74 | @Mock |
| 75 | private DozeSensors.Callback mCallback; |
| 76 | @Mock |
| 77 | private Consumer<Boolean> mProxCallback; |
| 78 | @Mock |
| 79 | private AlwaysOnDisplayPolicy mAlwaysOnDisplayPolicy; |
lpeter | aca9763 | 2019-06-11 11:13:54 +0800 | [diff] [blame] | 80 | @Mock |
Lucas Dupin | fac2e8e | 2019-06-27 16:10:19 -0700 | [diff] [blame] | 81 | private TriggerSensor mTriggerSensor; |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 82 | @Mock |
| 83 | private DozeLog mDozeLog; |
Dave Mankoff | 86c7344 | 2019-12-17 12:15:16 -0500 | [diff] [blame] | 84 | @Mock |
| 85 | private Sensor mProximitySensor; |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 86 | private SensorManagerPlugin.SensorEventListener mWakeLockScreenListener; |
| 87 | private TestableLooper mTestableLooper; |
| 88 | private DozeSensors mDozeSensors; |
| 89 | |
| 90 | @Before |
| 91 | public void setUp() { |
| 92 | MockitoAnnotations.initMocks(this); |
| 93 | mTestableLooper = TestableLooper.get(this); |
| 94 | when(mAmbientDisplayConfiguration.getWakeLockScreenDebounce()).thenReturn(5000L); |
| 95 | when(mAmbientDisplayConfiguration.alwaysOnEnabled(anyInt())).thenReturn(true); |
Dave Mankoff | 86c7344 | 2019-12-17 12:15:16 -0500 | [diff] [blame] | 96 | when(mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY)).thenReturn(mProximitySensor); |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 97 | doAnswer(invocation -> { |
| 98 | ((Runnable) invocation.getArgument(0)).run(); |
| 99 | return null; |
| 100 | }).when(mWakeLock).wrap(any(Runnable.class)); |
| 101 | mDozeSensors = new TestableDozeSensors(); |
| 102 | } |
| 103 | |
| 104 | @Test |
Dave Mankoff | 86c7344 | 2019-12-17 12:15:16 -0500 | [diff] [blame] | 105 | public void testRegisterProx() { |
| 106 | // We should not register with the sensor manager initially. |
| 107 | verify(mSensorManager, never()).registerListener(any(), any(Sensor.class), anyInt()); |
| 108 | mDozeSensors.setProxListening(true); |
| 109 | verify(mSensorManager).registerListener(any(), any(Sensor.class), anyInt()); |
| 110 | } |
| 111 | |
| 112 | @Test |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 113 | public void testSensorDebounce() { |
| 114 | mDozeSensors.setListening(true); |
| 115 | |
| 116 | mWakeLockScreenListener.onSensorChanged(mock(SensorManagerPlugin.SensorEvent.class)); |
| 117 | mTestableLooper.processAllMessages(); |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 118 | verify(mCallback).onSensorPulse(eq(DozeEvent.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN), |
Lucas Dupin | f40bd8f | 2019-08-07 15:55:00 -0700 | [diff] [blame] | 119 | anyFloat(), anyFloat(), eq(null)); |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 120 | |
| 121 | mDozeSensors.requestTemporaryDisable(); |
| 122 | reset(mCallback); |
| 123 | mWakeLockScreenListener.onSensorChanged(mock(SensorManagerPlugin.SensorEvent.class)); |
| 124 | mTestableLooper.processAllMessages(); |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 125 | verify(mCallback, never()).onSensorPulse(eq(DozeEvent.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN), |
Lucas Dupin | f40bd8f | 2019-08-07 15:55:00 -0700 | [diff] [blame] | 126 | anyFloat(), anyFloat(), eq(null)); |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 127 | } |
| 128 | |
lpeter | aca9763 | 2019-06-11 11:13:54 +0800 | [diff] [blame] | 129 | @Test |
| 130 | public void testSetListening_firstTrue_registerSettingsObserver() { |
Dave Mankoff | 86c7344 | 2019-12-17 12:15:16 -0500 | [diff] [blame] | 131 | verify(mSensorManager, never()).registerListener(any(), any(Sensor.class), anyInt()); |
lpeter | aca9763 | 2019-06-11 11:13:54 +0800 | [diff] [blame] | 132 | mDozeSensors.setListening(true); |
| 133 | |
Lucas Dupin | fac2e8e | 2019-06-27 16:10:19 -0700 | [diff] [blame] | 134 | verify(mTriggerSensor).registerSettingsObserver(any(ContentObserver.class)); |
lpeter | aca9763 | 2019-06-11 11:13:54 +0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Test |
| 138 | public void testSetListening_twiceTrue_onlyRegisterSettingsObserverOnce() { |
Dave Mankoff | 86c7344 | 2019-12-17 12:15:16 -0500 | [diff] [blame] | 139 | verify(mSensorManager, never()).registerListener(any(), any(Sensor.class), anyInt()); |
Lucas Dupin | fac2e8e | 2019-06-27 16:10:19 -0700 | [diff] [blame] | 140 | mDozeSensors.setListening(true); |
lpeter | aca9763 | 2019-06-11 11:13:54 +0800 | [diff] [blame] | 141 | mDozeSensors.setListening(true); |
| 142 | |
Lucas Dupin | fac2e8e | 2019-06-27 16:10:19 -0700 | [diff] [blame] | 143 | verify(mTriggerSensor, times(1)).registerSettingsObserver(any(ContentObserver.class)); |
| 144 | } |
lpeter | aca9763 | 2019-06-11 11:13:54 +0800 | [diff] [blame] | 145 | |
Lucas Dupin | fac2e8e | 2019-06-27 16:10:19 -0700 | [diff] [blame] | 146 | @Test |
Lucas Dupin | f40bd8f | 2019-08-07 15:55:00 -0700 | [diff] [blame] | 147 | public void testSetPaused_doesntPause_sensors() { |
Dave Mankoff | 86c7344 | 2019-12-17 12:15:16 -0500 | [diff] [blame] | 148 | verify(mSensorManager, never()).registerListener(any(), any(Sensor.class), anyInt()); |
Lucas Dupin | fac2e8e | 2019-06-27 16:10:19 -0700 | [diff] [blame] | 149 | mDozeSensors.setListening(true); |
| 150 | verify(mTriggerSensor).setListening(eq(true)); |
Lucas Dupin | fac2e8e | 2019-06-27 16:10:19 -0700 | [diff] [blame] | 151 | |
Lucas Dupin | f40bd8f | 2019-08-07 15:55:00 -0700 | [diff] [blame] | 152 | clearInvocations(mTriggerSensor); |
Lucas Dupin | fac2e8e | 2019-06-27 16:10:19 -0700 | [diff] [blame] | 153 | mDozeSensors.setPaused(true); |
Lucas Dupin | fac2e8e | 2019-06-27 16:10:19 -0700 | [diff] [blame] | 154 | verify(mTriggerSensor).setListening(eq(true)); |
Lucas Dupin | f40bd8f | 2019-08-07 15:55:00 -0700 | [diff] [blame] | 155 | |
| 156 | clearInvocations(mTriggerSensor); |
| 157 | mDozeSensors.setListening(false); |
| 158 | verify(mTriggerSensor).setListening(eq(false)); |
lpeter | aca9763 | 2019-06-11 11:13:54 +0800 | [diff] [blame] | 159 | } |
| 160 | |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 161 | private class TestableDozeSensors extends DozeSensors { |
| 162 | |
| 163 | TestableDozeSensors() { |
| 164 | super(getContext(), mAlarmManager, mSensorManager, mDozeParameters, |
Dave Mankoff | 86c7344 | 2019-12-17 12:15:16 -0500 | [diff] [blame] | 165 | mAmbientDisplayConfiguration, mWakeLock, mCallback, mProxCallback, mDozeLog); |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 166 | for (TriggerSensor sensor : mSensors) { |
| 167 | if (sensor instanceof PluginSensor |
| 168 | && ((PluginSensor) sensor).mPluginSensor.getType() |
| 169 | == TYPE_WAKE_LOCK_SCREEN) { |
| 170 | mWakeLockScreenListener = (PluginSensor) sensor; |
| 171 | } |
| 172 | } |
Lucas Dupin | f40bd8f | 2019-08-07 15:55:00 -0700 | [diff] [blame] | 173 | mSensors = new TriggerSensor[] {mTriggerSensor}; |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |