2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.systemui.doze;
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.ArgumentMatchers.anyInt;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.never;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
26 import android.app.AlarmManager;
27 import android.app.Instrumentation;
28 import android.os.Handler;
29 import android.os.Looper;
30 import android.support.test.InstrumentationRegistry;
31 import android.support.test.filters.SmallTest;
32 import android.support.test.runner.AndroidJUnit4;
34 import com.android.internal.hardware.AmbientDisplayConfiguration;
35 import com.android.systemui.SysuiTestCase;
36 import com.android.systemui.statusbar.phone.DozeParameters;
37 import com.android.systemui.util.wakelock.WakeLock;
38 import com.android.systemui.util.wakelock.WakeLockFake;
39 import com.android.systemui.utils.hardware.FakeSensorManager;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Ignore;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
49 @RunWith(AndroidJUnit4.class)
50 public class DozeTriggersTest extends SysuiTestCase {
51 private DozeTriggers mTriggers;
52 private DozeMachine mMachine;
53 private DozeHostFake mHost;
54 private AmbientDisplayConfiguration mConfig;
55 private DozeParameters mParameters;
56 private FakeSensorManager mSensors;
57 private Handler mHandler;
58 private WakeLock mWakeLock;
59 private Instrumentation mInstrumentation;
60 private AlarmManager mAlarmManager;
63 public static void setupSuite() {
64 // We can't use KeyguardUpdateMonitor from tests.
65 DozeLog.setRegisterKeyguardCallback(false);
69 public void setUp() throws Exception {
70 mInstrumentation = InstrumentationRegistry.getInstrumentation();
71 mMachine = mock(DozeMachine.class);
72 mAlarmManager = mock(AlarmManager.class);
73 mHost = new DozeHostFake();
74 mConfig = DozeConfigurationUtil.createMockConfig();
75 mParameters = DozeConfigurationUtil.createMockParameters();
76 mSensors = new FakeSensorManager(mContext);
77 mHandler = new Handler(Looper.getMainLooper());
78 mWakeLock = new WakeLockFake();
80 mInstrumentation.runOnMainSync(() -> {
81 mTriggers = new DozeTriggers(mContext, mMachine, mHost, mAlarmManager,
82 mConfig, mParameters, mSensors, mHandler, mWakeLock, true);
87 public void testOnNotification_stillWorksAfterOneFailedProxCheck() throws Exception {
88 when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE);
90 mInstrumentation.runOnMainSync(()->{
91 mTriggers.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
92 mTriggers.transitionTo(DozeMachine.State.INITIALIZED, DozeMachine.State.DOZE);
94 mHost.callback.onNotificationHeadsUp();
97 mInstrumentation.runOnMainSync(() -> {
98 mSensors.getMockProximitySensor().sendProximityResult(false); /* Near */
101 verify(mMachine, never()).requestState(any());
102 verify(mMachine, never()).requestPulse(anyInt());
104 mInstrumentation.runOnMainSync(()->{
105 mHost.callback.onNotificationHeadsUp();
108 mInstrumentation.runOnMainSync(() -> {
109 mSensors.getMockProximitySensor().sendProximityResult(true); /* Far */
112 verify(mMachine).requestPulse(anyInt());