Adrian Roos | 2981eb0 | 2017-05-26 18:40:09 -0700 | [diff] [blame] | 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.doze; |
| 18 | |
| 19 | import static com.android.systemui.doze.DozeMachine.State.DOZE; |
| 20 | import static com.android.systemui.doze.DozeMachine.State.DOZE_AOD; |
| 21 | import static com.android.systemui.doze.DozeMachine.State.DOZE_PULSING; |
| 22 | import static com.android.systemui.doze.DozeMachine.State.DOZE_REQUEST_PULSE; |
| 23 | import static com.android.systemui.doze.DozeMachine.State.INITIALIZED; |
| 24 | import static com.android.systemui.doze.DozeMachine.State.UNINITIALIZED; |
| 25 | |
| 26 | import static org.junit.Assert.assertEquals; |
Adrian Roos | 8db9311 | 2017-08-11 19:43:17 +0200 | [diff] [blame] | 27 | import static org.mockito.ArgumentMatchers.any; |
| 28 | import static org.mockito.ArgumentMatchers.anyLong; |
| 29 | import static org.mockito.Mockito.spy; |
| 30 | import static org.mockito.Mockito.verify; |
Adrian Roos | 2981eb0 | 2017-05-26 18:40:09 -0700 | [diff] [blame] | 31 | |
Adrian Roos | 8db9311 | 2017-08-11 19:43:17 +0200 | [diff] [blame] | 32 | import android.os.Handler; |
| 33 | import android.os.Looper; |
| 34 | import android.os.Message; |
Adrian Roos | 2981eb0 | 2017-05-26 18:40:09 -0700 | [diff] [blame] | 35 | import android.support.test.filters.SmallTest; |
| 36 | import android.support.test.runner.AndroidJUnit4; |
| 37 | import android.view.Display; |
| 38 | |
| 39 | import com.android.systemui.SysuiTestCase; |
| 40 | |
| 41 | import org.junit.Before; |
| 42 | import org.junit.Test; |
| 43 | import org.junit.runner.RunWith; |
| 44 | |
| 45 | @RunWith(AndroidJUnit4.class) |
| 46 | @SmallTest |
| 47 | public class DozeScreenStateTest extends SysuiTestCase { |
| 48 | |
| 49 | DozeServiceFake mServiceFake; |
| 50 | DozeScreenState mScreen; |
Adrian Roos | 8db9311 | 2017-08-11 19:43:17 +0200 | [diff] [blame] | 51 | private ImmediateHandler mHandler; |
Adrian Roos | 2981eb0 | 2017-05-26 18:40:09 -0700 | [diff] [blame] | 52 | |
| 53 | @Before |
| 54 | public void setUp() throws Exception { |
| 55 | mServiceFake = new DozeServiceFake(); |
Adrian Roos | 8db9311 | 2017-08-11 19:43:17 +0200 | [diff] [blame] | 56 | mHandler = spy(new ImmediateHandler(Looper.getMainLooper())); |
| 57 | mScreen = new DozeScreenState(mServiceFake, mHandler); |
Adrian Roos | 2981eb0 | 2017-05-26 18:40:09 -0700 | [diff] [blame] | 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 |
Adrian Roos | e4cb6c8a | 2017-07-19 18:08:07 +0200 | [diff] [blame] | 98 | public void testScreen_offInRequestPulseWithAoD() { |
Adrian Roos | 2981eb0 | 2017-05-26 18:40:09 -0700 | [diff] [blame] | 99 | mScreen.transitionTo(UNINITIALIZED, INITIALIZED); |
| 100 | mScreen.transitionTo(INITIALIZED, DOZE_AOD); |
| 101 | |
| 102 | mScreen.transitionTo(DOZE, DOZE_REQUEST_PULSE); |
| 103 | |
Adrian Roos | e4cb6c8a | 2017-07-19 18:08:07 +0200 | [diff] [blame] | 104 | assertEquals(Display.STATE_OFF, mServiceFake.screenState); |
Adrian Roos | 2981eb0 | 2017-05-26 18:40:09 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Adrian Roos | 8db9311 | 2017-08-11 19:43:17 +0200 | [diff] [blame] | 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 | } |
Adrian Roos | 2981eb0 | 2017-05-26 18:40:09 -0700 | [diff] [blame] | 131 | } |