OSDN Git Service

Merge "docs: Add documentation for equals() method" into qt-dev am: 732a127636
[android-x86/frameworks-base.git] / services / tests / servicestests / src / com / android / server / power / AttentionDetectorTest.java
1 /*
2  * Copyright (C) 2019 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.server.power;
18
19 import static android.os.BatteryStats.Uid.NUM_USER_ACTIVITY_TYPES;
20
21 import static com.google.common.truth.Truth.assertThat;
22
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.anyLong;
25 import static org.mockito.Mockito.atMost;
26 import static org.mockito.Mockito.clearInvocations;
27 import static org.mockito.Mockito.doAnswer;
28 import static org.mockito.Mockito.never;
29 import static org.mockito.Mockito.reset;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32
33 import android.attention.AttentionManagerInternal;
34 import android.attention.AttentionManagerInternal.AttentionCallbackInternal;
35 import android.content.pm.PackageManager;
36 import android.os.PowerManager;
37 import android.os.PowerManagerInternal;
38 import android.os.SystemClock;
39 import android.os.UserHandle;
40 import android.provider.Settings;
41 import android.service.attention.AttentionService;
42 import android.test.AndroidTestCase;
43 import android.test.suitebuilder.annotation.SmallTest;
44
45 import com.android.server.wm.WindowManagerInternal;
46
47 import org.junit.After;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.mockito.ArgumentCaptor;
51 import org.mockito.Mock;
52 import org.mockito.MockitoAnnotations;
53
54 @SmallTest
55 public class AttentionDetectorTest extends AndroidTestCase {
56
57     @Mock
58     private PackageManager mPackageManager;
59     @Mock
60     private AttentionManagerInternal mAttentionManagerInternal;
61     @Mock
62     private WindowManagerInternal mWindowManagerInternal;
63     @Mock
64     private Runnable mOnUserAttention;
65     private TestableAttentionDetector mAttentionDetector;
66     private long mAttentionTimeout;
67     private long mNextDimming;
68     private int mIsSettingEnabled;
69
70     @Before
71     public void setUp() {
72         MockitoAnnotations.initMocks(this);
73         when(mPackageManager.getAttentionServicePackageName()).thenReturn("com.google.android.as");
74         when(mPackageManager.checkPermission(any(), any())).thenReturn(
75                 PackageManager.PERMISSION_GRANTED);
76         when(mAttentionManagerInternal.checkAttention(anyLong(), any()))
77                 .thenReturn(true);
78         when(mWindowManagerInternal.isKeyguardShowingAndNotOccluded()).thenReturn(false);
79         mAttentionDetector = new TestableAttentionDetector();
80         mAttentionDetector.onWakefulnessChangeStarted(PowerManagerInternal.WAKEFULNESS_AWAKE);
81         mAttentionDetector.setAttentionServiceSupported(true);
82         mNextDimming = SystemClock.uptimeMillis() + 3000L;
83
84         // Save the existing state.
85         mIsSettingEnabled = Settings.System.getIntForUser(getContext().getContentResolver(),
86                 Settings.System.ADAPTIVE_SLEEP, 0, UserHandle.USER_CURRENT);
87
88         Settings.System.putIntForUser(getContext().getContentResolver(),
89                 Settings.System.ADAPTIVE_SLEEP, 1, UserHandle.USER_CURRENT);
90         mAttentionDetector.updateEnabledFromSettings(getContext());
91     }
92
93     @After
94     public void tearDown() {
95         Settings.System.putIntForUser(getContext().getContentResolver(),
96                 Settings.System.ADAPTIVE_SLEEP, mIsSettingEnabled, UserHandle.USER_CURRENT);
97     }
98
99     @Test
100     public void testOnUserActivity_checksAttention() {
101         long when = registerAttention();
102         verify(mAttentionManagerInternal).checkAttention(anyLong(), any());
103         assertThat(when).isLessThan(mNextDimming);
104     }
105
106     @Test
107     public void testOnUserActivity_doesntCheckIfNotEnabled() {
108         Settings.System.putIntForUser(getContext().getContentResolver(),
109                 Settings.System.ADAPTIVE_SLEEP, 0, UserHandle.USER_CURRENT);
110         mAttentionDetector.updateEnabledFromSettings(getContext());
111         long when = registerAttention();
112         verify(mAttentionManagerInternal, never()).checkAttention(anyLong(), any());
113         assertThat(mNextDimming).isEqualTo(when);
114     }
115
116     @Test
117     public void testOnUserActivity_doesntCheckIfNotSupported() {
118         mAttentionDetector.setAttentionServiceSupported(false);
119         long when = registerAttention();
120         verify(mAttentionManagerInternal, never()).checkAttention(anyLong(), any());
121         assertThat(mNextDimming).isEqualTo(when);
122     }
123
124     @Test
125     public void testOnUserActivity_doesntCheckIfInLockscreen() {
126         when(mWindowManagerInternal.isKeyguardShowingAndNotOccluded()).thenReturn(true);
127
128         long when = registerAttention();
129         verify(mAttentionManagerInternal, never()).checkAttention(anyLong(), any());
130         assertThat(mNextDimming).isEqualTo(when);
131     }
132
133     @Test
134     public void testOnUserActivity_doesntCheckIfNotSufficientPermissions() {
135         when(mPackageManager.checkPermission(any(), any())).thenReturn(
136                 PackageManager.PERMISSION_DENIED);
137
138         long when = registerAttention();
139         verify(mAttentionManagerInternal, never()).checkAttention(anyLong(), any());
140         assertThat(mNextDimming).isEqualTo(when);
141     }
142
143     @Test
144     public void testOnUserActivity_disablesSettingIfNotSufficientPermissions() {
145         when(mPackageManager.checkPermission(any(), any())).thenReturn(
146                 PackageManager.PERMISSION_DENIED);
147
148         registerAttention();
149         boolean enabled = Settings.System.getIntForUser(getContext().getContentResolver(),
150                 Settings.System.ADAPTIVE_SLEEP, 0, UserHandle.USER_CURRENT) == 1;
151         assertFalse(enabled);
152     }
153
154     @Test
155     public void testOnUserActivity_doesntCrashIfNoAttentionService() {
156         mAttentionManagerInternal = null;
157         registerAttention();
158         // Does not crash.
159     }
160
161     @Test
162     public void onUserActivity_ignoresWhiteListedActivityTypes() {
163         for (int i = 0; i < NUM_USER_ACTIVITY_TYPES; i++) {
164             int result = mAttentionDetector.onUserActivity(SystemClock.uptimeMillis(), i);
165             if (result == -1) {
166                 throw new AssertionError("User activity " + i + " isn't listed in"
167                         + " AttentionDetector#onUserActivity. Please consider how this new activity"
168                         + " type affects the attention service.");
169             }
170         }
171     }
172
173     @Test
174     public void testUpdateUserActivity_ignoresWhenItsNotTimeYet() {
175         long now = SystemClock.uptimeMillis();
176         mNextDimming = now;
177         mAttentionDetector.onUserActivity(now, PowerManager.USER_ACTIVITY_EVENT_TOUCH);
178         mAttentionDetector.updateUserActivity(mNextDimming + 5000L);
179         verify(mAttentionManagerInternal, never()).checkAttention(anyLong(), any());
180     }
181
182     @Test
183     public void testUpdateUserActivity_schedulesTheNextCheck() {
184         long now = SystemClock.uptimeMillis();
185         mNextDimming = now;
186         mAttentionDetector.onUserActivity(now, PowerManager.USER_ACTIVITY_EVENT_TOUCH);
187         long nextTimeout = mAttentionDetector.updateUserActivity(mNextDimming + 5000L);
188         assertThat(nextTimeout).isEqualTo(mNextDimming + 5000L);
189     }
190
191     @Test
192     public void testOnUserActivity_ignoresAfterMaximumExtension() {
193         long now = SystemClock.uptimeMillis();
194         mAttentionDetector.onUserActivity(now - 15000L, PowerManager.USER_ACTIVITY_EVENT_TOUCH);
195         mAttentionDetector.updateUserActivity(now + 2000L);
196         verify(mAttentionManagerInternal, never()).checkAttention(anyLong(), any());
197     }
198
199     @Test
200     public void testOnUserActivity_ignoresIfAlreadyDoneForThatNextScreenDimming() {
201         long when = registerAttention();
202         verify(mAttentionManagerInternal).checkAttention(anyLong(), any());
203         assertThat(when).isLessThan(mNextDimming);
204         clearInvocations(mAttentionManagerInternal);
205
206         long redundantWhen = mAttentionDetector.updateUserActivity(mNextDimming);
207         assertThat(redundantWhen).isEqualTo(mNextDimming);
208         verify(mAttentionManagerInternal, never()).checkAttention(anyLong(), any());
209     }
210
211     @Test
212     public void testOnUserActivity_skipsIfAlreadyScheduled() {
213         registerAttention();
214         reset(mAttentionManagerInternal);
215         long when = mAttentionDetector.updateUserActivity(mNextDimming + 1);
216         verify(mAttentionManagerInternal, never()).checkAttention(anyLong(), any());
217         assertThat(when).isLessThan(mNextDimming);
218     }
219
220     @Test
221     public void testOnWakefulnessChangeStarted_cancelsRequestWhenNotAwake() {
222         registerAttention();
223         mAttentionDetector.onWakefulnessChangeStarted(PowerManagerInternal.WAKEFULNESS_ASLEEP);
224
225         ArgumentCaptor<AttentionCallbackInternal> callbackCaptor = ArgumentCaptor.forClass(
226                 AttentionCallbackInternal.class);
227         verify(mAttentionManagerInternal).cancelAttentionCheck(callbackCaptor.capture());
228         assertEquals(callbackCaptor.getValue(), mAttentionDetector.mCallback);
229     }
230
231     @Test
232     public void testCallbackOnSuccess_ignoresIfNoAttention() {
233         registerAttention();
234         mAttentionDetector.mCallback.onSuccess(AttentionService.ATTENTION_SUCCESS_ABSENT,
235                 SystemClock.uptimeMillis());
236         verify(mOnUserAttention, never()).run();
237     }
238
239     @Test
240     public void testCallbackOnSuccess_callsCallback() {
241         registerAttention();
242         mAttentionDetector.mCallback.onSuccess(AttentionService.ATTENTION_SUCCESS_PRESENT,
243                 SystemClock.uptimeMillis());
244         verify(mOnUserAttention).run();
245     }
246
247     @Test
248     public void testCallbackOnSuccess_doesNotCallNonCurrentCallback() {
249         mAttentionDetector.mRequestId = 5;
250         registerAttention(); // mRequestId = 6;
251         mAttentionDetector.mRequestId = 55;
252
253         mAttentionDetector.mCallback.onSuccess(AttentionService.ATTENTION_SUCCESS_PRESENT,
254                 SystemClock.uptimeMillis());
255         verify(mOnUserAttention, never()).run();
256     }
257
258     @Test
259     public void testCallbackOnSuccess_callsCallbackAfterOldCallbackCame() {
260         mAttentionDetector.mRequestId = 5;
261         registerAttention(); // mRequestId = 6;
262         mAttentionDetector.mRequestId = 55;
263
264         mAttentionDetector.mCallback.onSuccess(AttentionService.ATTENTION_SUCCESS_PRESENT,
265                 SystemClock.uptimeMillis()); // old callback came
266         mAttentionDetector.mRequestId = 6; // now back to current
267         mAttentionDetector.mCallback.onSuccess(AttentionService.ATTENTION_SUCCESS_PRESENT,
268                 SystemClock.uptimeMillis());
269         verify(mOnUserAttention).run();
270     }
271
272     @Test
273     public void testCallbackOnSuccess_DoesNotGoIntoInfiniteLoop() {
274         // Mimic real behavior
275         doAnswer((invocation) -> {
276             // Mimic a cache hit: calling onSuccess() immediately
277             registerAttention();
278             mAttentionDetector.mRequestId++;
279             mAttentionDetector.mCallback.onSuccess(AttentionService.ATTENTION_SUCCESS_PRESENT,
280                     SystemClock.uptimeMillis());
281             return null;
282         }).when(mOnUserAttention).run();
283
284         registerAttention();
285         // This test fails with literal stack overflow:
286         // e.g. java.lang.StackOverflowError: stack size 1039KB
287         mAttentionDetector.mCallback.onSuccess(AttentionService.ATTENTION_SUCCESS_PRESENT,
288                 SystemClock.uptimeMillis());
289
290         // We don't actually get here when the test fails
291         verify(mOnUserAttention, atMost(1)).run();
292     }
293
294     @Test
295     public void testCallbackOnFailure_unregistersCurrentRequestCode() {
296         registerAttention();
297         mAttentionDetector.mCallback.onFailure(AttentionService.ATTENTION_FAILURE_UNKNOWN);
298         mAttentionDetector.mCallback.onSuccess(AttentionService.ATTENTION_SUCCESS_PRESENT,
299                 SystemClock.uptimeMillis());
300         verify(mOnUserAttention, never()).run();
301     }
302
303     private long registerAttention() {
304         mAttentionTimeout = 4000L;
305         mAttentionDetector.onUserActivity(SystemClock.uptimeMillis(),
306                 PowerManager.USER_ACTIVITY_EVENT_TOUCH);
307         return mAttentionDetector.updateUserActivity(mNextDimming);
308     }
309
310     private class TestableAttentionDetector extends AttentionDetector {
311         private boolean mAttentionServiceSupported;
312
313         TestableAttentionDetector() {
314             super(AttentionDetectorTest.this.mOnUserAttention, new Object());
315             mAttentionManager = mAttentionManagerInternal;
316             mWindowManager = mWindowManagerInternal;
317             mPackageManager = AttentionDetectorTest.this.mPackageManager;
318             mContentResolver = getContext().getContentResolver();
319             mMaximumExtensionMillis = 10000L;
320         }
321
322         void setAttentionServiceSupported(boolean supported) {
323             mAttentionServiceSupported = supported;
324         }
325
326         @Override
327         public boolean isAttentionServiceSupported() {
328             return mAttentionServiceSupported;
329         }
330
331         @Override
332         public long getAttentionTimeout() {
333             return mAttentionTimeout;
334         }
335     }
336 }