DozeTriggersTest.java revision f26517d46617cb8b8d3ea5a7559bbcb7df31a526
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
17package com.android.systemui.doze;
18
19import static org.mockito.ArgumentMatchers.any;
20import static org.mockito.ArgumentMatchers.anyInt;
21import static org.mockito.Mockito.mock;
22import static org.mockito.Mockito.never;
23import static org.mockito.Mockito.verify;
24import static org.mockito.Mockito.when;
25
26import android.app.Instrumentation;
27import android.os.Handler;
28import android.os.Looper;
29import android.support.test.InstrumentationRegistry;
30import android.support.test.filters.FlakyTest;
31import android.support.test.filters.SmallTest;
32import android.support.test.runner.AndroidJUnit4;
33
34import com.android.internal.hardware.AmbientDisplayConfiguration;
35import com.android.systemui.SysuiTestCase;
36import com.android.systemui.statusbar.phone.DozeParameters;
37import com.android.systemui.util.wakelock.WakeLock;
38import com.android.systemui.util.wakelock.WakeLockFake;
39import com.android.systemui.utils.hardware.FakeSensorManager;
40
41import org.junit.Before;
42import org.junit.BeforeClass;
43import org.junit.Test;
44import org.junit.runner.RunWith;
45
46@SmallTest
47@RunWith(AndroidJUnit4.class)
48public class DozeTriggersTest extends SysuiTestCase {
49    private DozeTriggers mTriggers;
50    private DozeMachine mMachine;
51    private DozeHostFake mHost;
52    private AmbientDisplayConfiguration mConfig;
53    private DozeParameters mParameters;
54    private FakeSensorManager mSensors;
55    private Handler mHandler;
56    private WakeLock mWakeLock;
57    private Instrumentation mInstrumentation;
58
59    @BeforeClass
60    public static void setupSuite() {
61        // We can't use KeyguardUpdateMonitor from tests.
62        DozeLog.setRegisterKeyguardCallback(false);
63    }
64
65    @Before
66    public void setUp() throws Exception {
67        mInstrumentation = InstrumentationRegistry.getInstrumentation();
68        mMachine = mock(DozeMachine.class);
69        mHost = new DozeHostFake();
70        mConfig = DozeConfigurationUtil.createMockConfig();
71        mParameters = DozeConfigurationUtil.createMockParameters();
72        mSensors = new FakeSensorManager(mContext);
73        mHandler = new Handler(Looper.getMainLooper());
74        mWakeLock = new WakeLockFake();
75
76        mInstrumentation.runOnMainSync(() -> {
77            mTriggers = new DozeTriggers(mContext, mMachine, mHost,
78                    mConfig, mParameters, mSensors, mHandler, mWakeLock, true);
79        });
80    }
81
82    @FlakyTest
83    @Test
84    public void testOnNotification_stillWorksAfterOneFailedProxCheck() throws Exception {
85        when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE);
86
87        mInstrumentation.runOnMainSync(()->{
88            mTriggers.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
89            mTriggers.transitionTo(DozeMachine.State.INITIALIZED, DozeMachine.State.DOZE);
90
91            mHost.callback.onNotificationHeadsUp();
92        });
93
94        mInstrumentation.runOnMainSync(() -> {
95            mSensors.getMockProximitySensor().sendProximityResult(false); /* Near */
96        });
97
98        verify(mMachine, never()).requestState(any());
99        verify(mMachine, never()).requestPulse(anyInt());
100
101        mInstrumentation.runOnMainSync(()->{
102            mHost.callback.onNotificationHeadsUp();
103        });
104
105        mInstrumentation.runOnMainSync(() -> {
106            mSensors.getMockProximitySensor().sendProximityResult(true); /* Far */
107        });
108
109        verify(mMachine).requestPulse(anyInt());
110    }
111
112}