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