blob: 1dc415048f74b559744ddf6257f1d035701f95d6 [file] [log] [blame]
Adrian Roos369907f2017-07-14 14:53:39 +02001/*
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
17package com.android.systemui.keyguard;
18
19import static org.junit.Assert.assertEquals;
20import static org.mockito.Mockito.mock;
Lucas Dupin16cfe452018-02-08 13:14:50 -080021import static org.mockito.Mockito.times;
Adrian Roos369907f2017-07-14 14:53:39 +020022import static org.mockito.Mockito.verify;
23import static org.mockito.Mockito.verifyNoMoreInteractions;
24
Adrian Roos369907f2017-07-14 14:53:39 +020025import android.testing.AndroidTestingRunner;
26
Brett Chabot84151d92019-02-27 15:37:59 -080027import androidx.test.filters.SmallTest;
28
Adrian Roos369907f2017-07-14 14:53:39 +020029import com.android.systemui.SysuiTestCase;
30
31import org.junit.Before;
32import org.junit.Test;
33import org.junit.runner.RunWith;
34
35import java.io.ByteArrayOutputStream;
36import java.io.PrintWriter;
37
38@RunWith(AndroidTestingRunner.class)
39@SmallTest
40public class WakefulnessLifecycleTest extends SysuiTestCase {
41
42 private WakefulnessLifecycle mWakefulness;
43 private WakefulnessLifecycle.Observer mWakefulnessObserver;
44
45 @Before
46 public void setUp() throws Exception {
47 mWakefulness = new WakefulnessLifecycle();
48 mWakefulnessObserver = mock(WakefulnessLifecycle.Observer.class);
49 mWakefulness.addObserver(mWakefulnessObserver);
50 }
51
52 @Test
53 public void baseState() throws Exception {
54 assertEquals(WakefulnessLifecycle.WAKEFULNESS_ASLEEP, mWakefulness.getWakefulness());
55
56 verifyNoMoreInteractions(mWakefulnessObserver);
57 }
58
59 @Test
60 public void dispatchStartedWakingUp() throws Exception {
61 mWakefulness.dispatchStartedWakingUp();
62
63 assertEquals(WakefulnessLifecycle.WAKEFULNESS_WAKING, mWakefulness.getWakefulness());
64
65 verify(mWakefulnessObserver).onStartedWakingUp();
66 }
67
68 @Test
69 public void dispatchFinishedWakingUp() throws Exception {
70 mWakefulness.dispatchStartedWakingUp();
71 mWakefulness.dispatchFinishedWakingUp();
72
73 assertEquals(WakefulnessLifecycle.WAKEFULNESS_AWAKE, mWakefulness.getWakefulness());
74
75 verify(mWakefulnessObserver).onFinishedWakingUp();
76 }
77
78 @Test
79 public void dispatchStartedGoingToSleep() throws Exception {
80 mWakefulness.dispatchStartedWakingUp();
81 mWakefulness.dispatchFinishedWakingUp();
82 mWakefulness.dispatchStartedGoingToSleep();
83
84 assertEquals(WakefulnessLifecycle.WAKEFULNESS_GOING_TO_SLEEP,
85 mWakefulness.getWakefulness());
86
87 verify(mWakefulnessObserver).onStartedGoingToSleep();
88 }
89
90 @Test
91 public void dispatchFinishedGoingToSleep() throws Exception {
92 mWakefulness.dispatchStartedWakingUp();
93 mWakefulness.dispatchFinishedWakingUp();
94 mWakefulness.dispatchStartedGoingToSleep();
95 mWakefulness.dispatchFinishedGoingToSleep();
96
97 assertEquals(WakefulnessLifecycle.WAKEFULNESS_ASLEEP,
98 mWakefulness.getWakefulness());
99
100 verify(mWakefulnessObserver).onFinishedGoingToSleep();
101 }
102
103 @Test
Lucas Dupin16cfe452018-02-08 13:14:50 -0800104 public void doesNotDispatchTwice() throws Exception {
105 mWakefulness.dispatchStartedWakingUp();
106 mWakefulness.dispatchStartedWakingUp();
107 mWakefulness.dispatchFinishedWakingUp();
108 mWakefulness.dispatchFinishedWakingUp();
109 mWakefulness.dispatchStartedGoingToSleep();
110 mWakefulness.dispatchStartedGoingToSleep();
111 mWakefulness.dispatchFinishedGoingToSleep();
112 mWakefulness.dispatchFinishedGoingToSleep();
113
114 verify(mWakefulnessObserver, times(1)).onStartedGoingToSleep();
115 verify(mWakefulnessObserver, times(1)).onFinishedGoingToSleep();
116 verify(mWakefulnessObserver, times(1)).onStartedWakingUp();
117 verify(mWakefulnessObserver, times(1)).onFinishedWakingUp();
118 }
119
120 @Test
Adrian Roos369907f2017-07-14 14:53:39 +0200121 public void dump() throws Exception {
122 mWakefulness.dump(null, new PrintWriter(new ByteArrayOutputStream()), new String[0]);
123 }
124
125}