DozeTriggersTest.java revision d7b9d10d569438968f75d2453c4293caf89774f2
1d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos/*
2d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos * Copyright (C) 2017 The Android Open Source Project
3d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos *
4d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos * Licensed under the Apache License, Version 2.0 (the "License");
5d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos * you may not use this file except in compliance with the License.
6d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos * You may obtain a copy of the License at
7d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos *
8d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos *      http://www.apache.org/licenses/LICENSE-2.0
9d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos *
10d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos * Unless required by applicable law or agreed to in writing, software
11d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos * distributed under the License is distributed on an "AS IS" BASIS,
12d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos * See the License for the specific language governing permissions and
14d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos * limitations under the License.
15d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos */
16d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
17d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roospackage com.android.systemui.doze;
18d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
19d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport static org.mockito.ArgumentMatchers.any;
20d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport static org.mockito.ArgumentMatchers.anyInt;
21d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport static org.mockito.Mockito.mock;
22d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport static org.mockito.Mockito.never;
23d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport static org.mockito.Mockito.verify;
24d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport static org.mockito.Mockito.when;
25d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
26d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport android.app.Instrumentation;
27d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport android.content.Context;
28d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport android.os.Handler;
29d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport android.os.Looper;
30d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport android.support.test.InstrumentationRegistry;
31d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
32d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport com.android.internal.hardware.AmbientDisplayConfiguration;
33d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport com.android.systemui.statusbar.phone.DozeParameters;
34d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport com.android.systemui.util.wakelock.WakeLock;
35d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport com.android.systemui.util.wakelock.WakeLockFake;
36d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
37d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport org.junit.Before;
38d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport org.junit.BeforeClass;
39d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport org.junit.Ignore;
40d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport org.junit.Test;
41d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
42d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roospublic class DozeTriggersTest {
43d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    private Context mContext;
44d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    private DozeTriggers mTriggers;
45d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    private DozeMachine mMachine;
46d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    private DozeHostFake mHost;
47d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    private AmbientDisplayConfiguration mConfig;
48d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    private DozeParameters mParameters;
49d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    private SensorManagerFake mSensors;
50d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    private Handler mHandler;
51d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    private WakeLock mWakeLock;
52d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    private Instrumentation mInstrumentation;
53d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
54d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    @BeforeClass
55d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    public static void setupSuite() {
56d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        // We can't use KeyguardUpdateMonitor from tests.
57d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        DozeLog.setRegisterKeyguardCallback(false);
58d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    }
59d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
60d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    @Before
61d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    public void setUp() throws Exception {
62d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mInstrumentation = InstrumentationRegistry.getInstrumentation();
63d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mContext = InstrumentationRegistry.getContext();
64d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mMachine = mock(DozeMachine.class);
65d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mHost = new DozeHostFake();
66d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mConfig = DozeConfigurationUtil.createMockConfig();
67d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mParameters = DozeConfigurationUtil.createMockParameters();
68d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mSensors = new SensorManagerFake(mContext);
69d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mHandler = new Handler(Looper.getMainLooper());
70d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mWakeLock = new WakeLockFake();
71d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
72d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mInstrumentation.runOnMainSync(() -> {
73d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos            mTriggers = new DozeTriggers(mContext, mMachine, mHost,
74d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos                    mConfig, mParameters, mSensors, mHandler, mWakeLock, true);
75d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        });
76d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    }
77d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
78d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    @Test
79d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    @Ignore("setup crashes on virtual devices")
80d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    public void testOnNotification_stillWorksAfterOneFailedProxCheck() throws Exception {
81d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE);
82d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
83d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mInstrumentation.runOnMainSync(()->{
84d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos            mTriggers.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
85d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos            mTriggers.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.DOZE);
86d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
87d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos            mHost.callback.onNotificationHeadsUp();
88d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        });
89d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
90d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mInstrumentation.runOnMainSync(() -> {
91d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos            mSensors.PROXIMITY.sendProximityResult(false); /* Near */
92d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        });
93d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
94d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        verify(mMachine, never()).requestState(any());
95d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
96d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mInstrumentation.runOnMainSync(()->{
97d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos            mHost.callback.onNotificationHeadsUp();
98d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        });
99d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
100d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mInstrumentation.runOnMainSync(() -> {
101d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos            mSensors.PROXIMITY.sendProximityResult(true); /* Far */
102d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        });
103d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
104d7b9d10d569438968f75d2453c4293caf89774f2Adrian Roos        verify(mMachine).requestPulse(anyInt());
105d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    }
106d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
107d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos}