Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.systemui.doze; |
| 18 | |
| 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; |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 25 | |
| 26 | import android.app.Instrumentation; |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 27 | import android.os.Handler; |
| 28 | import android.os.Looper; |
| 29 | import android.support.test.InstrumentationRegistry; |
Lucas Dupin | f26517d | 2017-06-09 14:10:33 -0700 | [diff] [blame] | 30 | import android.support.test.filters.FlakyTest; |
Adrian Roos | 84a187f | 2017-04-28 14:10:51 -0700 | [diff] [blame] | 31 | import android.support.test.filters.SmallTest; |
| 32 | import android.support.test.runner.AndroidJUnit4; |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 33 | |
| 34 | import com.android.internal.hardware.AmbientDisplayConfiguration; |
Jason Monk | 25a52b6 | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 35 | import com.android.systemui.SysuiTestCase; |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 36 | import com.android.systemui.statusbar.phone.DozeParameters; |
| 37 | import com.android.systemui.util.wakelock.WakeLock; |
| 38 | import com.android.systemui.util.wakelock.WakeLockFake; |
Adrian Roos | 84a187f | 2017-04-28 14:10:51 -0700 | [diff] [blame] | 39 | import com.android.systemui.utils.hardware.FakeSensorManager; |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 40 | |
| 41 | import org.junit.Before; |
| 42 | import org.junit.BeforeClass; |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 43 | import org.junit.Test; |
Adrian Roos | 84a187f | 2017-04-28 14:10:51 -0700 | [diff] [blame] | 44 | import org.junit.runner.RunWith; |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 45 | |
Adrian Roos | 84a187f | 2017-04-28 14:10:51 -0700 | [diff] [blame] | 46 | @SmallTest |
| 47 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 25a52b6 | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 48 | public class DozeTriggersTest extends SysuiTestCase { |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 49 | private DozeTriggers mTriggers; |
| 50 | private DozeMachine mMachine; |
| 51 | private DozeHostFake mHost; |
| 52 | private AmbientDisplayConfiguration mConfig; |
| 53 | private DozeParameters mParameters; |
Adrian Roos | 84a187f | 2017-04-28 14:10:51 -0700 | [diff] [blame] | 54 | private FakeSensorManager mSensors; |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 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(); |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 68 | mMachine = mock(DozeMachine.class); |
| 69 | mHost = new DozeHostFake(); |
| 70 | mConfig = DozeConfigurationUtil.createMockConfig(); |
| 71 | mParameters = DozeConfigurationUtil.createMockParameters(); |
Adrian Roos | 84a187f | 2017-04-28 14:10:51 -0700 | [diff] [blame] | 72 | mSensors = new FakeSensorManager(mContext); |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 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 | |
Lucas Dupin | f26517d | 2017-06-09 14:10:33 -0700 | [diff] [blame] | 82 | @FlakyTest |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 83 | @Test |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 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); |
Adrian Roos | 84a187f | 2017-04-28 14:10:51 -0700 | [diff] [blame] | 89 | mTriggers.transitionTo(DozeMachine.State.INITIALIZED, DozeMachine.State.DOZE); |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 90 | |
| 91 | mHost.callback.onNotificationHeadsUp(); |
| 92 | }); |
| 93 | |
| 94 | mInstrumentation.runOnMainSync(() -> { |
Adrian Roos | 84a187f | 2017-04-28 14:10:51 -0700 | [diff] [blame] | 95 | mSensors.getMockProximitySensor().sendProximityResult(false); /* Near */ |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 96 | }); |
| 97 | |
| 98 | verify(mMachine, never()).requestState(any()); |
Adrian Roos | 84a187f | 2017-04-28 14:10:51 -0700 | [diff] [blame] | 99 | verify(mMachine, never()).requestPulse(anyInt()); |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 100 | |
| 101 | mInstrumentation.runOnMainSync(()->{ |
| 102 | mHost.callback.onNotificationHeadsUp(); |
| 103 | }); |
| 104 | |
| 105 | mInstrumentation.runOnMainSync(() -> { |
Adrian Roos | 84a187f | 2017-04-28 14:10:51 -0700 | [diff] [blame] | 106 | mSensors.getMockProximitySensor().sendProximityResult(true); /* Far */ |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 107 | }); |
| 108 | |
Adrian Roos | d7b9d10 | 2017-04-28 15:42:58 -0700 | [diff] [blame] | 109 | verify(mMachine).requestPulse(anyInt()); |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | } |