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