DozeTriggersTest.java revision 6023ccbea1568d8b22db82cac9bf7a03367d0a61
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
266023ccbea1568d8b22db82cac9bf7a03367d0a61Adrian Roosimport android.app.AlarmManager;
27d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport android.app.Instrumentation;
28d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport android.os.Handler;
29d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport android.os.Looper;
30d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport android.support.test.InstrumentationRegistry;
3184a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roosimport android.support.test.filters.SmallTest;
3284a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roosimport android.support.test.runner.AndroidJUnit4;
33d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
34d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport com.android.internal.hardware.AmbientDisplayConfiguration;
3525a52b65b2cac1f49f37f4532cfa62282432957aJason Monkimport com.android.systemui.SysuiTestCase;
36d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport com.android.systemui.statusbar.phone.DozeParameters;
37d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport com.android.systemui.util.wakelock.WakeLock;
38d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport com.android.systemui.util.wakelock.WakeLockFake;
3984a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roosimport com.android.systemui.utils.hardware.FakeSensorManager;
40d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
41d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport org.junit.Before;
42d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport org.junit.BeforeClass;
43eefa3be55d941bc0193edae515b9e1b01b50e908Geoffrey Pitschimport org.junit.Ignore;
44d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport org.junit.Test;
4584a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roosimport org.junit.runner.RunWith;
46d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
4784a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roos@SmallTest
48eefa3be55d941bc0193edae515b9e1b01b50e908Geoffrey Pitsch@Ignore("failing")
4984a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roos@RunWith(AndroidJUnit4.class)
5025a52b65b2cac1f49f37f4532cfa62282432957aJason Monkpublic class DozeTriggersTest extends SysuiTestCase {
51d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    private DozeTriggers mTriggers;
52d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    private DozeMachine mMachine;
53d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    private DozeHostFake mHost;
54d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    private AmbientDisplayConfiguration mConfig;
55d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    private DozeParameters mParameters;
5684a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roos    private FakeSensorManager mSensors;
57d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    private Handler mHandler;
58d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    private WakeLock mWakeLock;
59d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    private Instrumentation mInstrumentation;
606023ccbea1568d8b22db82cac9bf7a03367d0a61Adrian Roos    private AlarmManager mAlarmManager;
61d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
62d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    @BeforeClass
63d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    public static void setupSuite() {
64d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        // We can't use KeyguardUpdateMonitor from tests.
65d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        DozeLog.setRegisterKeyguardCallback(false);
66d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    }
67d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
68d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    @Before
69d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    public void setUp() throws Exception {
70d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mInstrumentation = InstrumentationRegistry.getInstrumentation();
71d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mMachine = mock(DozeMachine.class);
726023ccbea1568d8b22db82cac9bf7a03367d0a61Adrian Roos        mAlarmManager = mock(AlarmManager.class);
73d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mHost = new DozeHostFake();
74d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mConfig = DozeConfigurationUtil.createMockConfig();
75d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mParameters = DozeConfigurationUtil.createMockParameters();
7684a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roos        mSensors = new FakeSensorManager(mContext);
77d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mHandler = new Handler(Looper.getMainLooper());
78d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mWakeLock = new WakeLockFake();
79d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
80d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mInstrumentation.runOnMainSync(() -> {
816023ccbea1568d8b22db82cac9bf7a03367d0a61Adrian Roos            mTriggers = new DozeTriggers(mContext, mMachine, mHost, mAlarmManager,
82d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos                    mConfig, mParameters, mSensors, mHandler, mWakeLock, true);
83d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        });
84d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    }
85d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
86d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    @Test
87d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    public void testOnNotification_stillWorksAfterOneFailedProxCheck() throws Exception {
88d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE);
89d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
90d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mInstrumentation.runOnMainSync(()->{
91d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos            mTriggers.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
9284a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roos            mTriggers.transitionTo(DozeMachine.State.INITIALIZED, DozeMachine.State.DOZE);
93d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
94d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos            mHost.callback.onNotificationHeadsUp();
95d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        });
96d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
97d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mInstrumentation.runOnMainSync(() -> {
9884a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roos            mSensors.getMockProximitySensor().sendProximityResult(false); /* Near */
99d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        });
100d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
101d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        verify(mMachine, never()).requestState(any());
10284a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roos        verify(mMachine, never()).requestPulse(anyInt());
103d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
104d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mInstrumentation.runOnMainSync(()->{
105d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos            mHost.callback.onNotificationHeadsUp();
106d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        });
107d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
108d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        mInstrumentation.runOnMainSync(() -> {
10984a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roos            mSensors.getMockProximitySensor().sendProximityResult(true); /* Far */
110d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos        });
111d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
112d7b9d10d569438968f75d2453c4293caf89774f2Adrian Roos        verify(mMachine).requestPulse(anyInt());
113d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos    }
114d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos
115eefa3be55d941bc0193edae515b9e1b01b50e908Geoffrey Pitsch}
116