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.keyguard; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.mockito.Mockito.mock; 21 import static org.mockito.Mockito.times; 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.verifyNoMoreInteractions; 24 25 import android.app.IWallpaperManager; 26 import android.os.PowerManager; 27 import android.testing.AndroidTestingRunner; 28 29 import androidx.test.filters.SmallTest; 30 31 import com.android.systemui.SysuiTestCase; 32 import com.android.systemui.dump.DumpManager; 33 import com.android.systemui.util.time.FakeSystemClock; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 39 import java.io.ByteArrayOutputStream; 40 import java.io.PrintWriter; 41 42 @RunWith(AndroidTestingRunner.class) 43 @SmallTest 44 public class WakefulnessLifecycleTest extends SysuiTestCase { 45 46 private WakefulnessLifecycle mWakefulness; 47 private WakefulnessLifecycle.Observer mWakefulnessObserver; 48 49 private IWallpaperManager mWallpaperManager; 50 51 @Before setUp()52 public void setUp() throws Exception { 53 mWallpaperManager = mock(IWallpaperManager.class); 54 mWakefulness = 55 new WakefulnessLifecycle( 56 mContext, 57 mWallpaperManager, 58 new FakeSystemClock(), 59 mock(DumpManager.class) 60 ); 61 mWakefulnessObserver = mock(WakefulnessLifecycle.Observer.class); 62 mWakefulness.addObserver(mWakefulnessObserver); 63 } 64 65 @Test baseState()66 public void baseState() throws Exception { 67 assertEquals(WakefulnessLifecycle.WAKEFULNESS_AWAKE, mWakefulness.getWakefulness()); 68 69 verifyNoMoreInteractions(mWakefulnessObserver); 70 } 71 72 @Test dispatchStartedWakingUp()73 public void dispatchStartedWakingUp() throws Exception { 74 mWakefulness.dispatchStartedWakingUp(PowerManager.WAKE_REASON_UNKNOWN); 75 76 assertEquals(WakefulnessLifecycle.WAKEFULNESS_WAKING, mWakefulness.getWakefulness()); 77 78 verify(mWakefulnessObserver).onStartedWakingUp(); 79 } 80 81 @Test dispatchFinishedWakingUp()82 public void dispatchFinishedWakingUp() throws Exception { 83 mWakefulness.dispatchStartedWakingUp(PowerManager.WAKE_REASON_UNKNOWN); 84 mWakefulness.dispatchFinishedWakingUp(); 85 86 assertEquals(WakefulnessLifecycle.WAKEFULNESS_AWAKE, mWakefulness.getWakefulness()); 87 88 verify(mWakefulnessObserver).onFinishedWakingUp(); 89 verify(mWakefulnessObserver).onPostFinishedWakingUp(); 90 } 91 92 @Test dispatchStartedGoingToSleep()93 public void dispatchStartedGoingToSleep() throws Exception { 94 mWakefulness.dispatchStartedWakingUp(PowerManager.WAKE_REASON_UNKNOWN); 95 mWakefulness.dispatchFinishedWakingUp(); 96 mWakefulness.dispatchStartedGoingToSleep(PowerManager.GO_TO_SLEEP_REASON_MIN); 97 98 assertEquals(WakefulnessLifecycle.WAKEFULNESS_GOING_TO_SLEEP, 99 mWakefulness.getWakefulness()); 100 101 verify(mWakefulnessObserver).onStartedGoingToSleep(); 102 } 103 104 @Test dispatchFinishedGoingToSleep()105 public void dispatchFinishedGoingToSleep() throws Exception { 106 mWakefulness.dispatchStartedWakingUp(PowerManager.WAKE_REASON_UNKNOWN); 107 mWakefulness.dispatchFinishedWakingUp(); 108 mWakefulness.dispatchStartedGoingToSleep(PowerManager.GO_TO_SLEEP_REASON_MIN); 109 mWakefulness.dispatchFinishedGoingToSleep(); 110 111 assertEquals(WakefulnessLifecycle.WAKEFULNESS_ASLEEP, 112 mWakefulness.getWakefulness()); 113 114 verify(mWakefulnessObserver).onFinishedGoingToSleep(); 115 } 116 117 @Test doesNotDispatchTwice()118 public void doesNotDispatchTwice() throws Exception { 119 mWakefulness.dispatchStartedWakingUp(PowerManager.WAKE_REASON_UNKNOWN); 120 mWakefulness.dispatchStartedWakingUp(PowerManager.WAKE_REASON_UNKNOWN); 121 mWakefulness.dispatchFinishedWakingUp(); 122 mWakefulness.dispatchFinishedWakingUp(); 123 mWakefulness.dispatchStartedGoingToSleep(PowerManager.GO_TO_SLEEP_REASON_MIN); 124 mWakefulness.dispatchStartedGoingToSleep(PowerManager.GO_TO_SLEEP_REASON_MIN); 125 mWakefulness.dispatchFinishedGoingToSleep(); 126 mWakefulness.dispatchFinishedGoingToSleep(); 127 128 verify(mWakefulnessObserver, times(1)).onStartedGoingToSleep(); 129 verify(mWakefulnessObserver, times(1)).onFinishedGoingToSleep(); 130 verify(mWakefulnessObserver, times(1)).onStartedWakingUp(); 131 verify(mWakefulnessObserver, times(1)).onFinishedWakingUp(); 132 } 133 134 @Test dump()135 public void dump() throws Exception { 136 mWakefulness.dump(new PrintWriter(new ByteArrayOutputStream()), new String[0]); 137 } 138 139 @Test(expected = NullPointerException.class) throwNPEOnNullObserver()140 public void throwNPEOnNullObserver() { 141 mWakefulness.addObserver(null); 142 } 143 } 144