TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.eq; |
| 21 | import static org.mockito.Mockito.mock; |
| 22 | import static org.mockito.Mockito.never; |
| 23 | import static org.mockito.Mockito.spy; |
| 24 | import static org.mockito.Mockito.verify; |
| 25 | import static org.mockito.Mockito.when; |
| 26 | |
| 27 | import android.app.Instrumentation; |
| 28 | import android.os.Handler; |
| 29 | import android.os.Looper; |
| 30 | import android.support.test.InstrumentationRegistry; |
| 31 | import android.support.test.filters.SmallTest; |
| 32 | import android.testing.AndroidTestingRunner; |
| 33 | import android.testing.TestableLooper; |
| 34 | import android.testing.TestableLooper.RunWithLooper; |
| 35 | |
| 36 | import com.android.internal.hardware.AmbientDisplayConfiguration; |
| 37 | import com.android.systemui.SysuiTestCase; |
| 38 | import com.android.systemui.dock.DockManager; |
| 39 | import com.android.systemui.dock.DockManagerFake; |
| 40 | |
| 41 | import org.junit.Before; |
| 42 | import org.junit.BeforeClass; |
| 43 | import org.junit.Test; |
| 44 | import org.junit.runner.RunWith; |
| 45 | |
| 46 | @SmallTest |
| 47 | @RunWith(AndroidTestingRunner.class) |
| 48 | @RunWithLooper |
| 49 | public class DozeDockHandlerTest extends SysuiTestCase { |
| 50 | private DozeDockHandler mDockHandler; |
| 51 | private DozeMachine mMachine; |
| 52 | private DozeHostFake mHost; |
| 53 | private AmbientDisplayConfiguration mConfig; |
| 54 | private Instrumentation mInstrumentation; |
| 55 | private DockManagerFake mDockManagerFake; |
| 56 | |
| 57 | @BeforeClass |
| 58 | public static void setupSuite() { |
| 59 | // We can't use KeyguardUpdateMonitor from tests. |
| 60 | DozeLog.setRegisterKeyguardCallback(false); |
| 61 | } |
| 62 | |
| 63 | @Before |
| 64 | public void setUp() throws Exception { |
| 65 | mInstrumentation = InstrumentationRegistry.getInstrumentation(); |
| 66 | mMachine = mock(DozeMachine.class); |
| 67 | mHost = spy(new DozeHostFake()); |
| 68 | mConfig = DozeConfigurationUtil.createMockConfig(); |
| 69 | |
| 70 | mDockManagerFake = spy(new DockManagerFake()); |
| 71 | mContext.putComponent(DockManager.class, mDockManagerFake); |
| 72 | |
| 73 | mDockHandler = new DozeDockHandler(mContext, mMachine, mHost, mConfig, |
| 74 | Handler.createAsync(Looper.myLooper())); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void testDockEventListener_registerAndUnregister() throws Exception { |
| 79 | mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); |
| 80 | |
| 81 | verify(mDockManagerFake).addListener(any()); |
| 82 | |
| 83 | mDockHandler.transitionTo(DozeMachine.State.DOZE, DozeMachine.State.FINISH); |
| 84 | |
| 85 | verify(mDockManagerFake).removeListener(any()); |
| 86 | } |
| 87 | |
| 88 | @Test |
| 89 | public void testOnEvent_dockingWhenDoze_requestPulse() throws Exception { |
| 90 | mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); |
| 91 | when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE); |
| 92 | |
| 93 | mDockManagerFake.setDockEvent(DockManager.STATE_DOCKING); |
| 94 | |
| 95 | verify(mMachine).requestPulse(eq(DozeLog.PULSE_REASON_DOCKING)); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testOnEvent_dockingWhenPausing_neverRequestPulse() throws Exception { |
| 100 | mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); |
| 101 | when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE_AOD_PAUSING); |
| 102 | |
| 103 | mDockManagerFake.setDockEvent(DockManager.STATE_DOCKING); |
| 104 | |
| 105 | verify(mMachine, never()).requestPulse(eq(DozeLog.PULSE_REASON_DOCKING)); |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void testOnEvent_undockedWhenPulsing_requestPulseOut() throws Exception { |
| 110 | mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); |
| 111 | when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE_PULSING); |
| 112 | when(mMachine.getPulseReason()).thenReturn(DozeLog.PULSE_REASON_DOCKING); |
| 113 | |
| 114 | mDockManagerFake.setDockEvent(DockManager.STATE_UNDOCKING); |
| 115 | |
| 116 | verify(mHost).stopPulsing(); |
| 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void testOnEvent_undockedWhenDoze_neverRequestPulseOut() throws Exception { |
| 121 | mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); |
TYM Tsai | 2d23690 | 2018-12-27 16:46:06 +0800 | [diff] [blame] | 122 | when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE); |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 123 | |
| 124 | mDockManagerFake.setDockEvent(DockManager.STATE_UNDOCKING); |
| 125 | |
| 126 | verify(mHost, never()).stopPulsing(); |
| 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void testTransitionToDozeWhenDocking_RequestPulse() throws Exception { |
| 131 | mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); |
| 132 | mDockManagerFake.setDockEvent(DockManager.STATE_DOCKING); |
| 133 | mDockHandler.transitionTo(DozeMachine.State.DOZE_AOD_PAUSING, DozeMachine.State.DOZE); |
| 134 | when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE); |
| 135 | |
| 136 | TestableLooper.get(this).processAllMessages(); |
| 137 | |
| 138 | verify(mMachine).requestPulse(eq(DozeLog.PULSE_REASON_DOCKING)); |
| 139 | } |
| 140 | } |