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; |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 20 | import static org.mockito.ArgumentMatchers.anyInt; |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 21 | import static org.mockito.ArgumentMatchers.eq; |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 22 | import static org.mockito.Mockito.doReturn; |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 23 | import static org.mockito.Mockito.mock; |
| 24 | import static org.mockito.Mockito.never; |
| 25 | import static org.mockito.Mockito.spy; |
| 26 | import static org.mockito.Mockito.verify; |
| 27 | import static org.mockito.Mockito.when; |
| 28 | |
| 29 | import android.app.Instrumentation; |
Issei Suzuki | ca19e6e | 2019-02-26 12:39:11 +0100 | [diff] [blame] | 30 | import android.hardware.display.AmbientDisplayConfiguration; |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 31 | import android.os.Handler; |
| 32 | import android.os.Looper; |
| 33 | import android.support.test.InstrumentationRegistry; |
| 34 | import android.support.test.filters.SmallTest; |
| 35 | import android.testing.AndroidTestingRunner; |
| 36 | import android.testing.TestableLooper; |
| 37 | import android.testing.TestableLooper.RunWithLooper; |
| 38 | |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 39 | import com.android.systemui.SysuiTestCase; |
| 40 | import com.android.systemui.dock.DockManager; |
| 41 | import com.android.systemui.dock.DockManagerFake; |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 42 | import com.android.systemui.doze.DozeMachine.State; |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 43 | |
| 44 | import org.junit.Before; |
| 45 | import org.junit.BeforeClass; |
| 46 | import org.junit.Test; |
| 47 | import org.junit.runner.RunWith; |
| 48 | |
| 49 | @SmallTest |
| 50 | @RunWith(AndroidTestingRunner.class) |
| 51 | @RunWithLooper |
| 52 | public class DozeDockHandlerTest extends SysuiTestCase { |
| 53 | private DozeDockHandler mDockHandler; |
| 54 | private DozeMachine mMachine; |
| 55 | private DozeHostFake mHost; |
| 56 | private AmbientDisplayConfiguration mConfig; |
| 57 | private Instrumentation mInstrumentation; |
| 58 | private DockManagerFake mDockManagerFake; |
| 59 | |
| 60 | @BeforeClass |
| 61 | public static void setupSuite() { |
| 62 | // We can't use KeyguardUpdateMonitor from tests. |
| 63 | DozeLog.setRegisterKeyguardCallback(false); |
| 64 | } |
| 65 | |
| 66 | @Before |
| 67 | public void setUp() throws Exception { |
| 68 | mInstrumentation = InstrumentationRegistry.getInstrumentation(); |
| 69 | mMachine = mock(DozeMachine.class); |
| 70 | mHost = spy(new DozeHostFake()); |
| 71 | mConfig = DozeConfigurationUtil.createMockConfig(); |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 72 | doReturn(false).when(mConfig).alwaysOnEnabled(anyInt()); |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 73 | |
| 74 | mDockManagerFake = spy(new DockManagerFake()); |
| 75 | mContext.putComponent(DockManager.class, mDockManagerFake); |
| 76 | |
| 77 | mDockHandler = new DozeDockHandler(mContext, mMachine, mHost, mConfig, |
lpeter | 8a5f470 | 2019-01-18 16:53:07 +0800 | [diff] [blame] | 78 | Handler.createAsync(Looper.myLooper()), mDockManagerFake); |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Test |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 82 | public void testDockEventListener_registerAndUnregister() { |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 83 | mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); |
| 84 | |
| 85 | verify(mDockManagerFake).addListener(any()); |
| 86 | |
| 87 | mDockHandler.transitionTo(DozeMachine.State.DOZE, DozeMachine.State.FINISH); |
| 88 | |
| 89 | verify(mDockManagerFake).removeListener(any()); |
| 90 | } |
| 91 | |
| 92 | @Test |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 93 | public void testOnEvent_dockedWhenDoze_requestPulse() { |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 94 | mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); |
| 95 | when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE); |
| 96 | |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 97 | mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED); |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 98 | |
| 99 | verify(mMachine).requestPulse(eq(DozeLog.PULSE_REASON_DOCKING)); |
| 100 | } |
| 101 | |
| 102 | @Test |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 103 | public void testOnEvent_dockedWhenDozeAoD_requestPulse() { |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 104 | mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 105 | when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE_AOD); |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 106 | |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 107 | mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED); |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 108 | |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 109 | verify(mMachine).requestPulse(eq(DozeLog.PULSE_REASON_DOCKING)); |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Test |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 113 | public void testOnEvent_dockedHideWhenPulsing_requestPulseOut() { |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 114 | mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 115 | when(mMachine.getState()).thenReturn(State.DOZE_PULSING); |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 116 | when(mMachine.getPulseReason()).thenReturn(DozeLog.PULSE_REASON_DOCKING); |
| 117 | |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 118 | mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED_HIDE); |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 119 | |
| 120 | verify(mHost).stopPulsing(); |
| 121 | } |
| 122 | |
| 123 | @Test |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 124 | public void testOnEvent_undockedWhenPulsing_requestPulseOut() { |
| 125 | mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); |
| 126 | when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE_PULSING); |
| 127 | when(mMachine.getPulseReason()).thenReturn(DozeLog.PULSE_REASON_DOCKING); |
| 128 | |
| 129 | mDockManagerFake.setDockEvent(DockManager.STATE_NONE); |
| 130 | |
| 131 | verify(mHost).stopPulsing(); |
| 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void testOnEvent_undockedWhenDoze_neverRequestPulseOut() { |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 136 | mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); |
TYM Tsai | 2d23690 | 2018-12-27 16:46:06 +0800 | [diff] [blame] | 137 | when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE); |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 138 | |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 139 | mDockManagerFake.setDockEvent(DockManager.STATE_NONE); |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 140 | |
| 141 | verify(mHost, never()).stopPulsing(); |
| 142 | } |
| 143 | |
| 144 | @Test |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 145 | public void testOnEvent_undockedWhenDozeAndEnabledAoD_requestDozeAoD() { |
| 146 | doReturn(true).when(mConfig).alwaysOnEnabled(anyInt()); |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 147 | mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 148 | when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE); |
| 149 | |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 150 | mDockManagerFake.setDockEvent(DockManager.STATE_NONE); |
| 151 | |
| 152 | verify(mMachine).requestState(eq(State.DOZE_AOD)); |
| 153 | } |
| 154 | |
| 155 | @Test |
| 156 | public void testTransitionToDoze_whenDocked_requestPulse() { |
| 157 | mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); |
| 158 | when(mMachine.getState()).thenReturn(DozeMachine.State.INITIALIZED); |
| 159 | mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED); |
| 160 | when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE); |
| 161 | mDockHandler.transitionTo(State.INITIALIZED, DozeMachine.State.DOZE); |
| 162 | |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 163 | TestableLooper.get(this).processAllMessages(); |
| 164 | |
| 165 | verify(mMachine).requestPulse(eq(DozeLog.PULSE_REASON_DOCKING)); |
| 166 | } |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 167 | |
| 168 | @Test |
| 169 | public void testTransitionToDozeAoD_whenDocked_requestPulse() { |
| 170 | mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); |
| 171 | when(mMachine.getState()).thenReturn(DozeMachine.State.INITIALIZED); |
| 172 | mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED); |
| 173 | when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE_AOD); |
| 174 | mDockHandler.transitionTo(State.INITIALIZED, DozeMachine.State.DOZE_AOD); |
| 175 | |
| 176 | TestableLooper.get(this).processAllMessages(); |
| 177 | |
| 178 | verify(mMachine).requestPulse(eq(DozeLog.PULSE_REASON_DOCKING)); |
| 179 | } |
| 180 | |
| 181 | @Test |
| 182 | public void testTransitionToDoze_whenDockedHide_neverRequestPulse() { |
| 183 | mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); |
| 184 | when(mMachine.getState()).thenReturn(DozeMachine.State.INITIALIZED); |
| 185 | mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED_HIDE); |
| 186 | when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE); |
| 187 | |
| 188 | mDockHandler.transitionTo(DozeMachine.State.INITIALIZED, DozeMachine.State.DOZE); |
| 189 | |
| 190 | verify(mMachine, never()).requestPulse(eq(DozeLog.PULSE_REASON_DOCKING)); |
| 191 | } |
| 192 | |
| 193 | @Test |
| 194 | public void testTransitionToDozeAoD_whenDockedHide_requestDoze() { |
| 195 | mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); |
| 196 | when(mMachine.getState()).thenReturn(DozeMachine.State.INITIALIZED); |
| 197 | mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED_HIDE); |
| 198 | when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE_AOD); |
| 199 | |
| 200 | mDockHandler.transitionTo(DozeMachine.State.INITIALIZED, State.DOZE_AOD); |
| 201 | |
| 202 | verify(mMachine).requestState(eq(State.DOZE)); |
| 203 | } |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 204 | } |