blob: a2f1f01b689fb8bf6c120a60289cfdc10d47d46c [file] [log] [blame]
Lucas Dupin0a5d7972019-01-16 18:52:30 -08001/*
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
17package com.android.server.power;
18
19import static android.os.BatteryStats.Uid.NUM_USER_ACTIVITY_TYPES;
20
21import static com.google.common.truth.Truth.assertThat;
22
23import static org.mockito.ArgumentMatchers.any;
24import static org.mockito.ArgumentMatchers.anyInt;
25import static org.mockito.ArgumentMatchers.anyLong;
26import static org.mockito.Mockito.never;
27import static org.mockito.Mockito.reset;
28import static org.mockito.Mockito.verify;
29import static org.mockito.Mockito.when;
30
31import android.attention.AttentionManagerInternal;
32import android.os.PowerManager;
33import android.os.PowerManagerInternal;
34import android.os.SystemClock;
Alex Salo9af68f12019-02-15 13:50:11 -080035import android.os.UserHandle;
36import android.provider.Settings;
Lucas Dupin0a5d7972019-01-16 18:52:30 -080037import android.service.attention.AttentionService;
38import android.test.AndroidTestCase;
39import android.test.suitebuilder.annotation.SmallTest;
40
Alex Salo9af68f12019-02-15 13:50:11 -080041import org.junit.After;
Lucas Dupin0a5d7972019-01-16 18:52:30 -080042import org.junit.Before;
43import org.junit.Test;
44import org.mockito.Mock;
45import org.mockito.MockitoAnnotations;
46
47@SmallTest
48public class AttentionDetectorTest extends AndroidTestCase {
49
50 private @Mock AttentionManagerInternal mAttentionManagerInternal;
51 private @Mock Runnable mOnUserAttention;
52 private TestableAttentionDetector mAttentionDetector;
53 private long mAttentionTimeout;
54 private long mNextDimming;
Alex Salo9af68f12019-02-15 13:50:11 -080055 private int mIsSettingEnabled;
Lucas Dupin0a5d7972019-01-16 18:52:30 -080056
57 @Before
58 public void setUp() {
59 MockitoAnnotations.initMocks(this);
60 when(mAttentionManagerInternal.checkAttention(anyInt(), anyLong(), any()))
61 .thenReturn(true);
62 mAttentionDetector = new TestableAttentionDetector();
63 mAttentionDetector.onWakefulnessChangeStarted(PowerManagerInternal.WAKEFULNESS_AWAKE);
64 mAttentionDetector.setAttentionServiceSupported(true);
65 mNextDimming = SystemClock.uptimeMillis() + 3000L;
Alex Salo9af68f12019-02-15 13:50:11 -080066
67 // Save the existing state.
68 mIsSettingEnabled = Settings.System.getIntForUser(getContext().getContentResolver(),
69 Settings.System.ADAPTIVE_SLEEP, 0, UserHandle.USER_CURRENT);
70
71 Settings.System.putIntForUser(getContext().getContentResolver(),
72 Settings.System.ADAPTIVE_SLEEP, 1, UserHandle.USER_CURRENT);
73 mAttentionDetector.updateEnabledFromSettings(getContext());
74 }
75
76 @After
77 public void tearDown() {
78 Settings.System.putIntForUser(getContext().getContentResolver(),
79 Settings.System.ADAPTIVE_SLEEP, mIsSettingEnabled, UserHandle.USER_CURRENT);
Lucas Dupin0a5d7972019-01-16 18:52:30 -080080 }
81
82 @Test
83 public void testOnUserActivity_checksAttention() {
84 long when = registerAttention();
85 verify(mAttentionManagerInternal).checkAttention(anyInt(), anyLong(), any());
86 assertThat(when).isLessThan(mNextDimming);
87 }
88
89 @Test
Alex Salo9af68f12019-02-15 13:50:11 -080090 public void testOnUserActivity_doesntCheckIfNotEnabled() {
91 Settings.System.putIntForUser(getContext().getContentResolver(),
92 Settings.System.ADAPTIVE_SLEEP, 0, UserHandle.USER_CURRENT);
93 mAttentionDetector.updateEnabledFromSettings(getContext());
94 long when = registerAttention();
95 verify(mAttentionManagerInternal, never()).checkAttention(anyInt(), anyLong(), any());
96 assertThat(mNextDimming).isEqualTo(when);
97 }
98
99 @Test
Lucas Dupin0a5d7972019-01-16 18:52:30 -0800100 public void testOnUserActivity_doesntCheckIfNotSupported() {
101 mAttentionDetector.setAttentionServiceSupported(false);
102 long when = registerAttention();
103 verify(mAttentionManagerInternal, never()).checkAttention(anyInt(), anyLong(), any());
104 assertThat(mNextDimming).isEqualTo(when);
105 }
106
107 @Test
108 public void onUserActivity_ignoresWhiteListedActivityTypes() {
109 for (int i = 0; i < NUM_USER_ACTIVITY_TYPES; i++) {
110 int result = mAttentionDetector.onUserActivity(SystemClock.uptimeMillis(), i);
111 if (result == -1) {
112 throw new AssertionError("User activity " + i + " isn't listed in"
113 + " AttentionDetector#onUserActivity. Please consider how this new activity"
114 + " type affects the attention service.");
115 }
116 }
117 }
118
119 @Test
120 public void testUpdateUserActivity_ignoresWhenItsNotTimeYet() {
121 long now = SystemClock.uptimeMillis();
122 mNextDimming = now;
123 mAttentionDetector.onUserActivity(now, PowerManager.USER_ACTIVITY_EVENT_TOUCH);
124 mAttentionDetector.updateUserActivity(mNextDimming + 5000L);
125 verify(mAttentionManagerInternal, never()).checkAttention(anyInt(), anyLong(), any());
126 }
127
128 @Test
Alex Salo1761fce2019-02-13 15:54:50 -0800129 public void testUpdateUserActivity_schedulesTheNextCheck() {
130 long now = SystemClock.uptimeMillis();
131 mNextDimming = now;
132 mAttentionDetector.onUserActivity(now, PowerManager.USER_ACTIVITY_EVENT_TOUCH);
133 long nextTimeout = mAttentionDetector.updateUserActivity(mNextDimming + 5000L);
134 assertThat(nextTimeout).isEqualTo(mNextDimming + 5000L);
135 }
136
137 @Test
Lucas Dupin0a5d7972019-01-16 18:52:30 -0800138 public void testOnUserActivity_ignoresAfterMaximumExtension() {
139 long now = SystemClock.uptimeMillis();
140 mAttentionDetector.onUserActivity(now - 15000L, PowerManager.USER_ACTIVITY_EVENT_TOUCH);
141 mAttentionDetector.updateUserActivity(now + 2000L);
142 verify(mAttentionManagerInternal, never()).checkAttention(anyInt(), anyLong(), any());
143 }
144
145 @Test
146 public void testOnUserActivity_skipsIfAlreadyScheduled() {
147 registerAttention();
148 reset(mAttentionManagerInternal);
149 long when = mAttentionDetector.updateUserActivity(mNextDimming);
150 verify(mAttentionManagerInternal, never()).checkAttention(anyInt(), anyLong(), any());
151 assertThat(when).isLessThan(mNextDimming);
152 }
153
154 @Test
155 public void testOnWakefulnessChangeStarted_cancelsRequestWhenNotAwake() {
156 registerAttention();
157 mAttentionDetector.onWakefulnessChangeStarted(PowerManagerInternal.WAKEFULNESS_ASLEEP);
158 verify(mAttentionManagerInternal).cancelAttentionCheck(anyInt());
159 }
160
161 @Test
162 public void testCallbackOnSuccess_ignoresIfNoAttention() {
163 registerAttention();
164 mAttentionDetector.mCallback.onSuccess(mAttentionDetector.getRequestCode(),
165 AttentionService.ATTENTION_SUCCESS_ABSENT, SystemClock.uptimeMillis());
166 verify(mOnUserAttention, never()).run();
167 }
168
169 @Test
170 public void testCallbackOnSuccess_callsCallback() {
171 registerAttention();
172 mAttentionDetector.mCallback.onSuccess(mAttentionDetector.getRequestCode(),
173 AttentionService.ATTENTION_SUCCESS_PRESENT, SystemClock.uptimeMillis());
174 verify(mOnUserAttention).run();
175 }
176
177 @Test
178 public void testCallbackOnFailure_unregistersCurrentRequestCode() {
179 registerAttention();
180 mAttentionDetector.mCallback.onFailure(mAttentionDetector.getRequestCode(),
181 AttentionService.ATTENTION_FAILURE_UNKNOWN);
182 mAttentionDetector.mCallback.onSuccess(mAttentionDetector.getRequestCode(),
183 AttentionService.ATTENTION_SUCCESS_PRESENT, SystemClock.uptimeMillis());
184 verify(mOnUserAttention, never()).run();
185 }
186
187 private long registerAttention() {
188 mAttentionTimeout = 4000L;
189 mAttentionDetector.onUserActivity(SystemClock.uptimeMillis(),
190 PowerManager.USER_ACTIVITY_EVENT_TOUCH);
191 return mAttentionDetector.updateUserActivity(mNextDimming);
192 }
193
194 private class TestableAttentionDetector extends AttentionDetector {
195
196 private boolean mAttentionServiceSupported;
197
198 TestableAttentionDetector() {
199 super(AttentionDetectorTest.this.mOnUserAttention, new Object());
200 mAttentionManager = mAttentionManagerInternal;
201 mMaximumExtensionMillis = 10000L;
202 }
203
204 void setAttentionServiceSupported(boolean supported) {
205 mAttentionServiceSupported = supported;
206 }
207
208 @Override
209 public boolean isAttentionServiceSupported() {
210 return mAttentionServiceSupported;
211 }
212
213 @Override
214 public long getAttentionTimeout() {
215 return mAttentionTimeout;
216 }
217 }
218}