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; |
| 22 | import static org.mockito.ArgumentMatchers.anyBoolean; |
| 23 | import static org.mockito.ArgumentMatchers.anyFloat; |
| 24 | import static org.mockito.ArgumentMatchers.anyInt; |
| 25 | import static org.mockito.ArgumentMatchers.eq; |
| 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; |
| 30 | import static org.mockito.Mockito.verify; |
| 31 | import static org.mockito.Mockito.when; |
| 32 | |
| 33 | import android.app.AlarmManager; |
Issei Suzuki | ca19e6e | 2019-02-26 12:39:11 +0100 | [diff] [blame] | 34 | import android.hardware.display.AmbientDisplayConfiguration; |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 35 | import android.support.test.filters.SmallTest; |
| 36 | import android.testing.AndroidTestingRunner; |
| 37 | import android.testing.TestableLooper; |
| 38 | import android.testing.TestableLooper.RunWithLooper; |
| 39 | |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 40 | import com.android.systemui.SysuiTestCase; |
| 41 | import com.android.systemui.plugins.SensorManagerPlugin; |
| 42 | import com.android.systemui.statusbar.phone.DozeParameters; |
| 43 | import com.android.systemui.util.AsyncSensorManager; |
| 44 | import com.android.systemui.util.wakelock.WakeLock; |
| 45 | |
| 46 | import org.junit.Before; |
| 47 | import org.junit.Test; |
| 48 | import org.junit.runner.RunWith; |
| 49 | import org.mockito.Mock; |
| 50 | import org.mockito.MockitoAnnotations; |
| 51 | |
| 52 | import java.util.function.Consumer; |
| 53 | |
| 54 | @RunWith(AndroidTestingRunner.class) |
| 55 | @RunWithLooper |
| 56 | @SmallTest |
| 57 | public class DozeSensorsTest extends SysuiTestCase { |
| 58 | |
| 59 | @Mock |
| 60 | private AlarmManager mAlarmManager; |
| 61 | @Mock |
| 62 | private AsyncSensorManager mSensorManager; |
| 63 | @Mock |
| 64 | private DozeParameters mDozeParameters; |
| 65 | @Mock |
| 66 | private AmbientDisplayConfiguration mAmbientDisplayConfiguration; |
| 67 | @Mock |
| 68 | private WakeLock mWakeLock; |
| 69 | @Mock |
| 70 | private DozeSensors.Callback mCallback; |
| 71 | @Mock |
| 72 | private Consumer<Boolean> mProxCallback; |
| 73 | @Mock |
| 74 | private AlwaysOnDisplayPolicy mAlwaysOnDisplayPolicy; |
| 75 | private SensorManagerPlugin.SensorEventListener mWakeLockScreenListener; |
| 76 | private TestableLooper mTestableLooper; |
| 77 | private DozeSensors mDozeSensors; |
| 78 | |
| 79 | @Before |
| 80 | public void setUp() { |
| 81 | MockitoAnnotations.initMocks(this); |
| 82 | mTestableLooper = TestableLooper.get(this); |
| 83 | when(mAmbientDisplayConfiguration.getWakeLockScreenDebounce()).thenReturn(5000L); |
| 84 | when(mAmbientDisplayConfiguration.alwaysOnEnabled(anyInt())).thenReturn(true); |
| 85 | doAnswer(invocation -> { |
| 86 | ((Runnable) invocation.getArgument(0)).run(); |
| 87 | return null; |
| 88 | }).when(mWakeLock).wrap(any(Runnable.class)); |
| 89 | mDozeSensors = new TestableDozeSensors(); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void testSensorDebounce() { |
| 94 | mDozeSensors.setListening(true); |
| 95 | |
| 96 | mWakeLockScreenListener.onSensorChanged(mock(SensorManagerPlugin.SensorEvent.class)); |
| 97 | mTestableLooper.processAllMessages(); |
| 98 | verify(mCallback).onSensorPulse(eq(DozeLog.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN), |
| 99 | anyBoolean(), anyFloat(), anyFloat(), eq(null)); |
| 100 | |
| 101 | mDozeSensors.requestTemporaryDisable(); |
| 102 | reset(mCallback); |
| 103 | mWakeLockScreenListener.onSensorChanged(mock(SensorManagerPlugin.SensorEvent.class)); |
| 104 | mTestableLooper.processAllMessages(); |
| 105 | verify(mCallback, never()).onSensorPulse(eq(DozeLog.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN), |
| 106 | anyBoolean(), anyFloat(), anyFloat(), eq(null)); |
| 107 | } |
| 108 | |
| 109 | private class TestableDozeSensors extends DozeSensors { |
| 110 | |
| 111 | TestableDozeSensors() { |
| 112 | super(getContext(), mAlarmManager, mSensorManager, mDozeParameters, |
| 113 | mAmbientDisplayConfiguration, mWakeLock, mCallback, mProxCallback, |
| 114 | mAlwaysOnDisplayPolicy); |
| 115 | for (TriggerSensor sensor : mSensors) { |
| 116 | if (sensor instanceof PluginSensor |
| 117 | && ((PluginSensor) sensor).mPluginSensor.getType() |
| 118 | == TYPE_WAKE_LOCK_SCREEN) { |
| 119 | mWakeLockScreenListener = (PluginSensor) sensor; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |