DozeScreenStateTest.java revision 8db9311cb683c58b7d61f812ae4975feca926aa4
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.doze;
18
19import static com.android.systemui.doze.DozeMachine.State.DOZE;
20import static com.android.systemui.doze.DozeMachine.State.DOZE_AOD;
21import static com.android.systemui.doze.DozeMachine.State.DOZE_PULSING;
22import static com.android.systemui.doze.DozeMachine.State.DOZE_REQUEST_PULSE;
23import static com.android.systemui.doze.DozeMachine.State.INITIALIZED;
24import static com.android.systemui.doze.DozeMachine.State.UNINITIALIZED;
25
26import static org.junit.Assert.assertEquals;
27import static org.mockito.ArgumentMatchers.any;
28import static org.mockito.ArgumentMatchers.anyLong;
29import static org.mockito.Mockito.spy;
30import static org.mockito.Mockito.verify;
31
32import android.os.Handler;
33import android.os.Looper;
34import android.os.Message;
35import android.support.test.filters.SmallTest;
36import android.support.test.runner.AndroidJUnit4;
37import android.view.Display;
38
39import com.android.systemui.SysuiTestCase;
40
41import org.junit.Before;
42import org.junit.Test;
43import org.junit.runner.RunWith;
44
45@RunWith(AndroidJUnit4.class)
46@SmallTest
47public class DozeScreenStateTest extends SysuiTestCase {
48
49    DozeServiceFake mServiceFake;
50    DozeScreenState mScreen;
51    private ImmediateHandler mHandler;
52
53    @Before
54    public void setUp() throws Exception {
55        mServiceFake = new DozeServiceFake();
56        mHandler = spy(new ImmediateHandler(Looper.getMainLooper()));
57        mScreen = new DozeScreenState(mServiceFake, mHandler);
58    }
59
60    @Test
61    public void testScreen_offInDoze() {
62        mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
63        mScreen.transitionTo(INITIALIZED, DOZE);
64
65        assertEquals(Display.STATE_OFF, mServiceFake.screenState);
66    }
67
68    @Test
69    public void testScreen_onInAod() {
70        mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
71        mScreen.transitionTo(INITIALIZED, DOZE_AOD);
72
73        assertEquals(Display.STATE_DOZE_SUSPEND, mServiceFake.screenState);
74    }
75
76    @Test
77    public void testScreen_onInPulse() {
78        mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
79        mScreen.transitionTo(INITIALIZED, DOZE);
80
81        mScreen.transitionTo(DOZE, DOZE_REQUEST_PULSE);
82        mScreen.transitionTo(DOZE_REQUEST_PULSE, DOZE_PULSING);
83
84        assertEquals(Display.STATE_ON, mServiceFake.screenState);
85    }
86
87    @Test
88    public void testScreen_offInRequestPulseWithoutAoD() {
89        mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
90        mScreen.transitionTo(INITIALIZED, DOZE);
91
92        mScreen.transitionTo(DOZE, DOZE_REQUEST_PULSE);
93
94        assertEquals(Display.STATE_OFF, mServiceFake.screenState);
95    }
96
97    @Test
98    public void testScreen_offInRequestPulseWithAoD() {
99        mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
100        mScreen.transitionTo(INITIALIZED, DOZE_AOD);
101
102        mScreen.transitionTo(DOZE, DOZE_REQUEST_PULSE);
103
104        assertEquals(Display.STATE_OFF, mServiceFake.screenState);
105    }
106
107    @Test
108    public void test_postedToHandler() {
109        mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
110        mScreen.transitionTo(INITIALIZED, DOZE_AOD);
111
112        verify(mHandler).sendMessageAtTime(any(), anyLong());
113    }
114
115    private static class ImmediateHandler extends Handler {
116
117        public ImmediateHandler(Looper looper) {
118            super(looper);
119        }
120
121        @Override
122        public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
123            Runnable callback = msg.getCallback();
124            if (callback != null) {
125                callback.run();
126                return false;
127            }
128            return super.sendMessageAtTime(msg, uptimeMillis);
129        }
130    }
131}