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.os.Handler; 28d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport android.os.Looper; 29d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport android.support.test.InstrumentationRegistry; 30f26517d46617cb8b8d3ea5a7559bbcb7df31a526Lucas Dupinimport android.support.test.filters.FlakyTest; 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; 43d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roosimport org.junit.Test; 4484a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roosimport org.junit.runner.RunWith; 45d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos 4684a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roos@SmallTest 4784a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roos@RunWith(AndroidJUnit4.class) 4825a52b65b2cac1f49f37f4532cfa62282432957aJason Monkpublic class DozeTriggersTest extends SysuiTestCase { 49d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos private DozeTriggers mTriggers; 50d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos private DozeMachine mMachine; 51d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos private DozeHostFake mHost; 52d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos private AmbientDisplayConfiguration mConfig; 53d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos private DozeParameters mParameters; 5484a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roos private FakeSensorManager mSensors; 55d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos private Handler mHandler; 56d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos private WakeLock mWakeLock; 57d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos private Instrumentation mInstrumentation; 58d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos 59d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos @BeforeClass 60d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos public static void setupSuite() { 61d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos // We can't use KeyguardUpdateMonitor from tests. 62d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos DozeLog.setRegisterKeyguardCallback(false); 63d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos } 64d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos 65d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos @Before 66d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos public void setUp() throws Exception { 67d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos mInstrumentation = InstrumentationRegistry.getInstrumentation(); 68d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos mMachine = mock(DozeMachine.class); 69d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos mHost = new DozeHostFake(); 70d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos mConfig = DozeConfigurationUtil.createMockConfig(); 71d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos mParameters = DozeConfigurationUtil.createMockParameters(); 7284a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roos mSensors = new FakeSensorManager(mContext); 73d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos mHandler = new Handler(Looper.getMainLooper()); 74d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos mWakeLock = new WakeLockFake(); 75d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos 76d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos mInstrumentation.runOnMainSync(() -> { 77d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos mTriggers = new DozeTriggers(mContext, mMachine, mHost, 78d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos mConfig, mParameters, mSensors, mHandler, mWakeLock, true); 79d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos }); 80d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos } 81d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos 82f26517d46617cb8b8d3ea5a7559bbcb7df31a526Lucas Dupin @FlakyTest 83d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos @Test 84d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos public void testOnNotification_stillWorksAfterOneFailedProxCheck() throws Exception { 85d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE); 86d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos 87d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos mInstrumentation.runOnMainSync(()->{ 88d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos mTriggers.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); 8984a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roos mTriggers.transitionTo(DozeMachine.State.INITIALIZED, DozeMachine.State.DOZE); 90d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos 91d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos mHost.callback.onNotificationHeadsUp(); 92d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos }); 93d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos 94d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos mInstrumentation.runOnMainSync(() -> { 9584a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roos mSensors.getMockProximitySensor().sendProximityResult(false); /* Near */ 96d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos }); 97d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos 98d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos verify(mMachine, never()).requestState(any()); 9984a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roos verify(mMachine, never()).requestPulse(anyInt()); 100d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos 101d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos mInstrumentation.runOnMainSync(()->{ 102d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos mHost.callback.onNotificationHeadsUp(); 103d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos }); 104d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos 105d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos mInstrumentation.runOnMainSync(() -> { 10684a187f0dd9b480a058e787f85e766a4a66121a9Adrian Roos mSensors.getMockProximitySensor().sendProximityResult(true); /* Far */ 107d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos }); 108d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos 109d7b9d10d569438968f75d2453c4293caf89774f2Adrian Roos verify(mMachine).requestPulse(anyInt()); 110d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos } 111d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos 112d35d4ca69f06cc53c02036f418275c255dc6ed68Adrian Roos}