OSDN Git Service

4f59fc4c6589dd28b4ee991ec1f4f5fb47c6a256
[android-x86/frameworks-base.git] / packages / SystemUI / tests / src / com / android / systemui / doze / DozeScreenBrightnessTest.java
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_AOD_PAUSED;
22 import static com.android.systemui.doze.DozeMachine.State.DOZE_AOD_PAUSING;
23 import static com.android.systemui.doze.DozeMachine.State.DOZE_PULSE_DONE;
24 import static com.android.systemui.doze.DozeMachine.State.DOZE_PULSING;
25 import static com.android.systemui.doze.DozeMachine.State.DOZE_REQUEST_PULSE;
26 import static com.android.systemui.doze.DozeMachine.State.FINISH;
27 import static com.android.systemui.doze.DozeMachine.State.INITIALIZED;
28 import static com.android.systemui.doze.DozeMachine.State.UNINITIALIZED;
29
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertFalse;
32 import static org.junit.Assert.assertNotEquals;
33 import static org.junit.Assert.assertTrue;
34
35 import android.os.PowerManager;
36 import android.support.test.filters.SmallTest;
37 import android.support.test.runner.AndroidJUnit4;
38
39 import com.android.systemui.SysuiTestCase;
40 import com.android.systemui.utils.hardware.FakeSensorManager;
41
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45
46 @RunWith(AndroidJUnit4.class)
47 @SmallTest
48 public class DozeScreenBrightnessTest extends SysuiTestCase {
49
50     static final int DEFAULT_BRIGHTNESS = 10;
51     static final int[] SENSOR_TO_BRIGHTNESS = new int[]{-1, 1, 2, 3, 4};
52     static final int[] SENSOR_TO_OPACITY = new int[]{-1, 10, 0, 0, 0};
53
54     DozeServiceFake mServiceFake;
55     DozeScreenBrightness mScreen;
56     FakeSensorManager.FakeGenericSensor mSensor;
57     FakeSensorManager mSensorManager;
58     DozeHostFake mHostFake;
59
60     @Before
61     public void setUp() throws Exception {
62         mServiceFake = new DozeServiceFake();
63         mHostFake = new DozeHostFake();
64         mSensorManager = new FakeSensorManager(mContext);
65         mSensor = mSensorManager.getFakeLightSensor();
66         mScreen = new DozeScreenBrightness(mContext, mServiceFake, mSensorManager,
67                 mSensor.getSensor(), mHostFake, null /* handler */,
68                 DEFAULT_BRIGHTNESS, SENSOR_TO_BRIGHTNESS, SENSOR_TO_OPACITY);
69     }
70
71     @Test
72     public void testInitialize_setsScreenBrightnessToValidValue() throws Exception {
73         mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
74
75         assertEquals(DEFAULT_BRIGHTNESS, mServiceFake.screenBrightness);
76         assertTrue(mServiceFake.screenBrightness <= PowerManager.BRIGHTNESS_ON);
77     }
78
79     @Test
80     public void testAod_usesLightSensor() throws Exception {
81         mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
82         mScreen.transitionTo(INITIALIZED, DOZE_AOD);
83
84         mSensor.sendSensorEvent(3);
85
86         assertEquals(3, mServiceFake.screenBrightness);
87     }
88
89     @Test
90     public void testPausingAod_doesntPauseLightSensor() throws Exception {
91         mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
92         mScreen.transitionTo(INITIALIZED, DOZE_AOD);
93
94         mSensor.sendSensorEvent(1);
95
96         mScreen.transitionTo(DOZE_AOD, DOZE_AOD_PAUSING);
97         mScreen.transitionTo(DOZE_AOD_PAUSING, DOZE_AOD_PAUSED);
98
99         mSensor.sendSensorEvent(2);
100
101         assertEquals(2, mServiceFake.screenBrightness);
102     }
103
104     @Test
105     public void testPausingAod_doesNotResetBrightness() throws Exception {
106         mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
107         mScreen.transitionTo(INITIALIZED, DOZE_AOD);
108
109         mSensor.sendSensorEvent(1);
110
111         mScreen.transitionTo(DOZE_AOD, DOZE_AOD_PAUSING);
112         mScreen.transitionTo(DOZE_AOD_PAUSING, DOZE_AOD_PAUSED);
113
114         assertEquals(1, mServiceFake.screenBrightness);
115     }
116
117     @Test
118     public void testPulsing_usesLightSensor() throws Exception {
119         mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
120         mScreen.transitionTo(INITIALIZED, DOZE);
121         mScreen.transitionTo(DOZE, DOZE_REQUEST_PULSE);
122
123         mSensor.sendSensorEvent(1);
124
125         assertEquals(1, mServiceFake.screenBrightness);
126     }
127
128     @Test
129     public void testDozingAfterPulsing_pausesLightSensor() throws Exception {
130         mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
131         mScreen.transitionTo(INITIALIZED, DOZE);
132         mScreen.transitionTo(DOZE, DOZE_REQUEST_PULSE);
133         mScreen.transitionTo(DOZE_REQUEST_PULSE, DOZE_PULSING);
134         mScreen.transitionTo(DOZE_PULSING, DOZE_PULSE_DONE);
135         mScreen.transitionTo(DOZE_PULSE_DONE, DOZE);
136
137         mSensor.sendSensorEvent(1);
138
139         assertEquals(DEFAULT_BRIGHTNESS, mServiceFake.screenBrightness);
140     }
141
142     @Test
143     public void testNullSensor() throws Exception {
144         mScreen = new DozeScreenBrightness(mContext, mServiceFake, mSensorManager,
145                 null /* sensor */, mHostFake, null /* handler */,
146                 DEFAULT_BRIGHTNESS, SENSOR_TO_BRIGHTNESS, SENSOR_TO_OPACITY);
147
148         mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
149         mScreen.transitionTo(INITIALIZED, DOZE_AOD);
150         mScreen.transitionTo(DOZE_AOD, DOZE_AOD_PAUSING);
151         mScreen.transitionTo(DOZE_AOD_PAUSING, DOZE_AOD_PAUSED);
152
153         assertTrue(mScreen.isReady());
154     }
155
156     @Test
157     public void testNoBrightnessDeliveredAfterFinish() throws Exception {
158         mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
159         mScreen.transitionTo(INITIALIZED, DOZE_AOD);
160         mScreen.transitionTo(DOZE_AOD, FINISH);
161
162         mSensor.sendSensorEvent(1);
163
164         assertNotEquals(1, mServiceFake.screenBrightness);
165     }
166
167     @Test
168     public void testNonPositiveBrightness_keepsPreviousBrightness() throws Exception {
169         mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
170         mScreen.transitionTo(INITIALIZED, DOZE_AOD);
171
172         mSensor.sendSensorEvent(2);
173         mSensor.sendSensorEvent(0);
174
175         assertEquals(2, mServiceFake.screenBrightness);
176     }
177
178     @Test
179     public void readyWhenNotInitialized() {
180         assertTrue(mScreen.isReady());
181     }
182
183     @Test
184     public void readyWhenNotRegistered() {
185         mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
186         mScreen.transitionTo(INITIALIZED, DOZE);
187
188         assertTrue(mScreen.isReady());
189     }
190
191     @Test
192     public void notReadyWhenRegistered_butNoEventYet() {
193         mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
194         mScreen.transitionTo(INITIALIZED, DOZE_AOD);
195
196         assertFalse(mScreen.isReady());
197     }
198
199     @Test
200     public void notReady_afterZeroBrightness() {
201         mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
202         mScreen.transitionTo(INITIALIZED, DOZE_AOD);
203
204         mSensor.sendSensorEvent(0);
205
206         assertFalse(mScreen.isReady());
207     }
208
209     @Test
210     public void ready_afterNonZeroBrightness() {
211         mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
212         mScreen.transitionTo(INITIALIZED, DOZE_AOD);
213
214         mSensor.sendSensorEvent(1);
215
216         assertTrue(mScreen.isReady());
217     }
218
219     @Test
220     public void notReady_nonZeroThenZeroBrightness() {
221         mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
222         mScreen.transitionTo(INITIALIZED, DOZE_AOD);
223
224         mSensor.sendSensorEvent(1);
225         mSensor.sendSensorEvent(0);
226
227         assertFalse(mScreen.isReady());
228     }
229
230     @Test
231     public void readyListener_getsCalled_whenRegistering() throws Exception {
232         Boolean[] ready = new Boolean[1];
233
234         mScreen.setBrightnessReadyListener((x) -> ready[0] = true);
235
236         assertTrue(ready[0]);
237     }
238
239     @Test
240     public void readyListener_getsCalled_whenReadyChanges() throws Exception {
241         Boolean[] ready = new Boolean[1];
242         mScreen.setBrightnessReadyListener((x) -> ready[0] = true);
243
244         mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
245         mScreen.transitionTo(INITIALIZED, DOZE_AOD);
246
247         ready[0] = null;
248         mSensor.sendSensorEvent(1);
249         assertTrue(ready[0]);
250     }
251 }