blob: 1f861716d380c05f7a912907883fe362215e8768 [file] [log] [blame]
Amith Yamasani62ec27e92018-03-11 14:42:06 -07001/*
2 * Copyright (C) 2018 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.usage;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
Varun Shah2546cef2019-01-11 15:50:54 -080021import static org.junit.Assert.assertNotNull;
Amith Yamasani62ec27e92018-03-11 14:42:06 -070022import static org.junit.Assert.assertTrue;
Michael Wachenschwanzae9811d2018-11-12 14:45:25 -080023import static org.junit.Assert.fail;
Amith Yamasani62ec27e92018-03-11 14:42:06 -070024
25import android.app.PendingIntent;
Varun Shah2546cef2019-01-11 15:50:54 -080026import android.app.usage.UsageStatsManagerInternal;
Amith Yamasani62ec27e92018-03-11 14:42:06 -070027import android.os.HandlerThread;
28import android.os.Looper;
Varun Shah2546cef2019-01-11 15:50:54 -080029import android.os.UserHandle;
Brett Chabota26eda92018-07-23 13:08:30 -070030
Michael Wachenschwanzae9811d2018-11-12 14:45:25 -080031import androidx.test.filters.LargeTest;
Brett Chabota26eda92018-07-23 13:08:30 -070032import androidx.test.runner.AndroidJUnit4;
Amith Yamasani62ec27e92018-03-11 14:42:06 -070033
34import org.junit.After;
35import org.junit.Before;
36import org.junit.Test;
37import org.junit.runner.RunWith;
38
Michael Wachenschwanzae9811d2018-11-12 14:45:25 -080039import java.io.PrintWriter;
40import java.io.StringWriter;
Amith Yamasani62ec27e92018-03-11 14:42:06 -070041import java.util.concurrent.CountDownLatch;
42import java.util.concurrent.TimeUnit;
43
44@RunWith(AndroidJUnit4.class)
Michael Wachenschwanzae9811d2018-11-12 14:45:25 -080045@LargeTest
Amith Yamasani62ec27e92018-03-11 14:42:06 -070046public class AppTimeLimitControllerTests {
47
48 private static final String PKG_SOC1 = "package.soc1";
49 private static final String PKG_SOC2 = "package.soc2";
50 private static final String PKG_GAME1 = "package.game1";
51 private static final String PKG_GAME2 = "package.game2";
52 private static final String PKG_PROD = "package.prod";
53
54 private static final int UID = 10100;
55 private static final int USER_ID = 10;
56 private static final int OBS_ID1 = 1;
57 private static final int OBS_ID2 = 2;
58 private static final int OBS_ID3 = 3;
Michael Wachenschwanzc8703092018-05-01 16:02:45 -070059 private static final int OBS_ID4 = 4;
60 private static final int OBS_ID5 = 5;
61 private static final int OBS_ID6 = 6;
62 private static final int OBS_ID7 = 7;
63 private static final int OBS_ID8 = 8;
64 private static final int OBS_ID9 = 9;
65 private static final int OBS_ID10 = 10;
66 private static final int OBS_ID11 = 11;
Amith Yamasani62ec27e92018-03-11 14:42:06 -070067
Michael Wachenschwanzc8703092018-05-01 16:02:45 -070068 private static final long TIME_30_MIN = 30 * 60_000L;
69 private static final long TIME_10_MIN = 10 * 60_000L;
Varun Shah2546cef2019-01-11 15:50:54 -080070 private static final long TIME_1_MIN = 1 * 60_000L;
Michael Wachenschwanzc8703092018-05-01 16:02:45 -070071
72 private static final long MAX_OBSERVER_PER_UID = 10;
73 private static final long MIN_TIME_LIMIT = 4_000L;
Amith Yamasani62ec27e92018-03-11 14:42:06 -070074
75 private static final String[] GROUP1 = {
76 PKG_SOC1, PKG_GAME1, PKG_PROD
77 };
78
79 private static final String[] GROUP_SOC = {
80 PKG_SOC1, PKG_SOC2
81 };
82
83 private static final String[] GROUP_GAME = {
84 PKG_GAME1, PKG_GAME2
85 };
86
Michael Wachenschwanz0f472842018-10-23 23:02:48 -070087 private CountDownLatch mLimitReachedLatch = new CountDownLatch(1);
88 private CountDownLatch mSessionEndLatch = new CountDownLatch(1);
Amith Yamasani62ec27e92018-03-11 14:42:06 -070089
90 private AppTimeLimitController mController;
91
92 private HandlerThread mThread;
93
94 private long mUptimeMillis;
95
Michael Wachenschwanz0f472842018-10-23 23:02:48 -070096 AppTimeLimitController.TimeLimitCallbackListener mListener =
97 new AppTimeLimitController.TimeLimitCallbackListener() {
98 @Override
99 public void onLimitReached(int observerId, int userId, long timeLimit,
100 long timeElapsed,
101 PendingIntent callbackIntent) {
102 mLimitReachedLatch.countDown();
103 }
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700104
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700105 @Override
106 public void onSessionEnd(int observerId, int userId, long timeElapsed,
107 PendingIntent callbackIntent) {
108 mSessionEndLatch.countDown();
109 }
110 };
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700111
112 class MyAppTimeLimitController extends AppTimeLimitController {
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700113 MyAppTimeLimitController(AppTimeLimitController.TimeLimitCallbackListener listener,
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700114 Looper looper) {
115 super(listener, looper);
116 }
117
118 @Override
119 protected long getUptimeMillis() {
120 return mUptimeMillis;
121 }
Michael Wachenschwanzc8703092018-05-01 16:02:45 -0700122
123 @Override
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700124 protected long getAppUsageObserverPerUidLimit() {
125 return MAX_OBSERVER_PER_UID;
126 }
127
128 @Override
129 protected long getUsageSessionObserverPerUidLimit() {
Michael Wachenschwanzc8703092018-05-01 16:02:45 -0700130 return MAX_OBSERVER_PER_UID;
131 }
132
133 @Override
Varun Shah2546cef2019-01-11 15:50:54 -0800134 protected long getAppUsageLimitObserverPerUidLimit() {
135 return MAX_OBSERVER_PER_UID;
136 }
137
138 @Override
Michael Wachenschwanzc8703092018-05-01 16:02:45 -0700139 protected long getMinTimeLimit() {
140 return MIN_TIME_LIMIT;
141 }
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700142 }
143
144 @Before
145 public void setUp() {
146 mThread = new HandlerThread("Test");
147 mThread.start();
148 mController = new MyAppTimeLimitController(mListener, mThread.getLooper());
149 }
150
151 @After
152 public void tearDown() {
153 mThread.quit();
154 }
155
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700156 /** Verify app usage observer is added */
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700157 @Test
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700158 public void testAppUsageObserver_AddObserver() {
159 addAppUsageObserver(OBS_ID1, GROUP1, TIME_30_MIN);
160 assertTrue("Observer wasn't added", hasAppUsageObserver(UID, OBS_ID1));
161 addAppUsageObserver(OBS_ID2, GROUP_GAME, TIME_30_MIN);
162 assertTrue("Observer wasn't added", hasAppUsageObserver(UID, OBS_ID2));
163 assertTrue("Observer wasn't added", hasAppUsageObserver(UID, OBS_ID1));
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700164 }
165
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700166 /** Verify usage session observer is added */
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700167 @Test
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700168 public void testUsageSessionObserver_AddObserver() {
169 addUsageSessionObserver(OBS_ID1, GROUP1, TIME_30_MIN, TIME_1_MIN);
170 assertTrue("Observer wasn't added", hasUsageSessionObserver(UID, OBS_ID1));
171 addUsageSessionObserver(OBS_ID2, GROUP_GAME, TIME_30_MIN, TIME_1_MIN);
172 assertTrue("Observer wasn't added", hasUsageSessionObserver(UID, OBS_ID2));
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700173 }
174
Varun Shah2546cef2019-01-11 15:50:54 -0800175 /** Verify app usage limit observer is added */
176 @Test
177 public void testAppUsageLimitObserver_AddObserver() {
Varun Shah9f58b7c2019-03-01 10:36:21 -0800178 addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN, TIME_30_MIN);
Varun Shah2546cef2019-01-11 15:50:54 -0800179 assertTrue("Observer wasn't added", hasAppUsageLimitObserver(UID, OBS_ID1));
Varun Shah9f58b7c2019-03-01 10:36:21 -0800180 addAppUsageLimitObserver(OBS_ID2, GROUP_GAME, TIME_30_MIN, TIME_30_MIN);
Varun Shah2546cef2019-01-11 15:50:54 -0800181 assertTrue("Observer wasn't added", hasAppUsageLimitObserver(UID, OBS_ID2));
182 assertTrue("Observer wasn't added", hasAppUsageLimitObserver(UID, OBS_ID1));
183 }
184
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700185 /** Verify app usage observer is removed */
186 @Test
187 public void testAppUsageObserver_RemoveObserver() {
188 addAppUsageObserver(OBS_ID1, GROUP1, TIME_30_MIN);
189 assertTrue("Observer wasn't added", hasAppUsageObserver(UID, OBS_ID1));
190 mController.removeAppUsageObserver(UID, OBS_ID1, USER_ID);
191 assertFalse("Observer wasn't removed", hasAppUsageObserver(UID, OBS_ID1));
192 }
193
194 /** Verify usage session observer is removed */
195 @Test
196 public void testUsageSessionObserver_RemoveObserver() {
197 addUsageSessionObserver(OBS_ID1, GROUP1, TIME_30_MIN, TIME_1_MIN);
198 assertTrue("Observer wasn't added", hasUsageSessionObserver(UID, OBS_ID1));
199 mController.removeUsageSessionObserver(UID, OBS_ID1, USER_ID);
200 assertFalse("Observer wasn't removed", hasUsageSessionObserver(UID, OBS_ID1));
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700201 }
202
Varun Shah2546cef2019-01-11 15:50:54 -0800203 /** Verify app usage limit observer is removed */
204 @Test
205 public void testAppUsageLimitObserver_RemoveObserver() {
Varun Shah9f58b7c2019-03-01 10:36:21 -0800206 addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN, TIME_30_MIN);
Varun Shah2546cef2019-01-11 15:50:54 -0800207 assertTrue("Observer wasn't added", hasAppUsageLimitObserver(UID, OBS_ID1));
208 mController.removeAppUsageLimitObserver(UID, OBS_ID1, USER_ID);
209 assertFalse("Observer wasn't removed", hasAppUsageLimitObserver(UID, OBS_ID1));
210 }
211
Michael Wachenschwanzae9811d2018-11-12 14:45:25 -0800212 /** Verify nothing happens when a nonexistent app usage observer is removed */
213 @Test
214 public void testAppUsageObserver_RemoveMissingObserver() {
215 assertFalse("Observer should not exist", hasAppUsageObserver(UID, OBS_ID1));
216 try {
217 mController.removeAppUsageObserver(UID, OBS_ID1, USER_ID);
218 } catch (Exception e) {
219 StringWriter sw = new StringWriter();
220 sw.write("Hit exception trying to remove nonexistent observer:\n");
221 sw.write(e.toString());
222 PrintWriter pw = new PrintWriter(sw);
223 e.printStackTrace(pw);
224 sw.write("\nTest Failed!");
225 fail(sw.toString());
226 }
227 assertFalse("Observer should not exist", hasAppUsageObserver(UID, OBS_ID1));
228 }
229
230 /** Verify nothing happens when a nonexistent usage session observer is removed */
231 @Test
232 public void testUsageSessionObserver_RemoveMissingObserver() {
233 assertFalse("Observer should not exist", hasUsageSessionObserver(UID, OBS_ID1));
234 try {
235 mController.removeUsageSessionObserver(UID, OBS_ID1, USER_ID);
236 } catch (Exception e) {
237 StringWriter sw = new StringWriter();
238 sw.write("Hit exception trying to remove nonexistent observer:");
239 sw.write(e.toString());
240 PrintWriter pw = new PrintWriter(sw);
241 e.printStackTrace(pw);
242 sw.write("\nTest Failed!");
243 fail(sw.toString());
244 }
245 assertFalse("Observer should not exist", hasUsageSessionObserver(UID, OBS_ID1));
246 }
247
Varun Shah2546cef2019-01-11 15:50:54 -0800248 /** Verify nothing happens when a nonexistent app usage limit observer is removed */
249 @Test
250 public void testAppUsageLimitObserver_RemoveMissingObserver() {
251 assertFalse("Observer should not exist", hasAppUsageLimitObserver(UID, OBS_ID1));
252 try {
253 mController.removeAppUsageLimitObserver(UID, OBS_ID1, USER_ID);
254 } catch (Exception e) {
255 StringWriter sw = new StringWriter();
256 sw.write("Hit exception trying to remove nonexistent observer:\n");
257 sw.write(e.toString());
258 PrintWriter pw = new PrintWriter(sw);
259 e.printStackTrace(pw);
260 sw.write("\nTest Failed!");
261 fail(sw.toString());
262 }
263 assertFalse("Observer should not exist", hasAppUsageLimitObserver(UID, OBS_ID1));
264 }
265
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700266 /** Re-adding an observer should result in only one copy */
267 @Test
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700268 public void testAppUsageObserver_ObserverReAdd() {
269 addAppUsageObserver(OBS_ID1, GROUP1, TIME_30_MIN);
270 assertTrue("Observer wasn't added", hasAppUsageObserver(UID, OBS_ID1));
271 addAppUsageObserver(OBS_ID1, GROUP1, TIME_10_MIN);
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700272 assertTrue("Observer wasn't added",
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700273 mController.getAppUsageGroup(UID, OBS_ID1).getTimeLimitMs() == TIME_10_MIN);
274 mController.removeAppUsageObserver(UID, OBS_ID1, USER_ID);
275 assertFalse("Observer wasn't removed", hasAppUsageObserver(UID, OBS_ID1));
276 }
277
278 /** Re-adding an observer should result in only one copy */
279 @Test
280 public void testUsageSessionObserver_ObserverReAdd() {
281 addUsageSessionObserver(OBS_ID1, GROUP1, TIME_30_MIN, TIME_1_MIN);
282 assertTrue("Observer wasn't added", hasUsageSessionObserver(UID, OBS_ID1));
283 addUsageSessionObserver(OBS_ID1, GROUP1, TIME_10_MIN, TIME_1_MIN);
284 assertTrue("Observer wasn't added",
285 mController.getSessionUsageGroup(UID, OBS_ID1).getTimeLimitMs() == TIME_10_MIN);
286 mController.removeUsageSessionObserver(UID, OBS_ID1, USER_ID);
287 assertFalse("Observer wasn't removed", hasUsageSessionObserver(UID, OBS_ID1));
288 }
289
Varun Shah2546cef2019-01-11 15:50:54 -0800290 /** Re-adding an observer should result in only one copy */
291 @Test
292 public void testAppUsageLimitObserver_ObserverReAdd() {
Varun Shah9f58b7c2019-03-01 10:36:21 -0800293 addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN, TIME_30_MIN);
Varun Shah2546cef2019-01-11 15:50:54 -0800294 assertTrue("Observer wasn't added", hasAppUsageLimitObserver(UID, OBS_ID1));
Varun Shah9f58b7c2019-03-01 10:36:21 -0800295 addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_10_MIN, TIME_10_MIN);
Varun Shah2546cef2019-01-11 15:50:54 -0800296 assertTrue("Observer wasn't added",
297 getAppUsageLimitObserver(UID, OBS_ID1).getTimeLimitMs() == TIME_10_MIN);
298 mController.removeAppUsageLimitObserver(UID, OBS_ID1, USER_ID);
299 assertFalse("Observer wasn't removed", hasAppUsageLimitObserver(UID, OBS_ID1));
300 }
301
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700302 /** Different type observers can be registered to the same observerId value */
303 @Test
304 public void testAllObservers_ExclusiveObserverIds() {
305 addAppUsageObserver(OBS_ID1, GROUP1, TIME_10_MIN);
306 addUsageSessionObserver(OBS_ID1, GROUP1, TIME_30_MIN, TIME_1_MIN);
Varun Shah9f58b7c2019-03-01 10:36:21 -0800307 addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_10_MIN, TIME_10_MIN);
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700308 assertTrue("Observer wasn't added", hasAppUsageObserver(UID, OBS_ID1));
309 assertTrue("Observer wasn't added", hasUsageSessionObserver(UID, OBS_ID1));
Varun Shah2546cef2019-01-11 15:50:54 -0800310 assertTrue("Observer wasn't added", hasAppUsageLimitObserver(UID, OBS_ID1));
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700311
312 AppTimeLimitController.UsageGroup appUsageGroup = mController.getAppUsageGroup(UID,
313 OBS_ID1);
314 AppTimeLimitController.UsageGroup sessionUsageGroup = mController.getSessionUsageGroup(UID,
315 OBS_ID1);
Varun Shah2546cef2019-01-11 15:50:54 -0800316 AppTimeLimitController.UsageGroup appUsageLimitGroup = getAppUsageLimitObserver(
317 UID, OBS_ID1);
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700318
319 // Verify data still intact
320 assertEquals(TIME_10_MIN, appUsageGroup.getTimeLimitMs());
321 assertEquals(TIME_30_MIN, sessionUsageGroup.getTimeLimitMs());
Varun Shah2546cef2019-01-11 15:50:54 -0800322 assertEquals(TIME_10_MIN, appUsageLimitGroup.getTimeLimitMs());
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700323 }
324
325 /** Verify that usage across different apps within a group are added up */
326 @Test
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700327 public void testAppUsageObserver_Accumulation() throws Exception {
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700328 setTime(0L);
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700329 addAppUsageObserver(OBS_ID1, GROUP1, TIME_30_MIN);
330 startUsage(PKG_SOC1);
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700331 // Add 10 mins
332 setTime(TIME_10_MIN);
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700333 stopUsage(PKG_SOC1);
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700334
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700335 AppTimeLimitController.UsageGroup group = mController.getAppUsageGroup(UID, OBS_ID1);
336
337 long timeRemaining = group.getTimeLimitMs() - group.getUsageTimeMs();
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700338 assertEquals(TIME_10_MIN * 2, timeRemaining);
339
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700340 startUsage(PKG_SOC1);
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700341 setTime(TIME_10_MIN * 2);
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700342 stopUsage(PKG_SOC1);
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700343
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700344 timeRemaining = group.getTimeLimitMs() - group.getUsageTimeMs();
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700345 assertEquals(TIME_10_MIN, timeRemaining);
346
347 setTime(TIME_30_MIN);
348
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700349 assertFalse(mLimitReachedLatch.await(100L, TimeUnit.MILLISECONDS));
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700350
351 // Add a different package in the group
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700352 startUsage(PKG_GAME1);
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700353 setTime(TIME_30_MIN + TIME_10_MIN);
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700354 stopUsage(PKG_GAME1);
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700355
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700356 assertEquals(0, group.getTimeLimitMs() - group.getUsageTimeMs());
357 assertTrue(mLimitReachedLatch.await(100L, TimeUnit.MILLISECONDS));
358 }
359
360 /** Verify that usage across different apps within a group are added up */
361 @Test
362 public void testUsageSessionObserver_Accumulation() throws Exception {
363 setTime(0L);
Varun Shah2546cef2019-01-11 15:50:54 -0800364 addUsageSessionObserver(OBS_ID1, GROUP1, TIME_30_MIN, TIME_10_MIN);
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700365 startUsage(PKG_SOC1);
366 // Add 10 mins
367 setTime(TIME_10_MIN);
368 stopUsage(PKG_SOC1);
369
370 AppTimeLimitController.UsageGroup group = mController.getSessionUsageGroup(UID, OBS_ID1);
371
372 long timeRemaining = group.getTimeLimitMs() - group.getUsageTimeMs();
373 assertEquals(TIME_10_MIN * 2, timeRemaining);
374
375 startUsage(PKG_SOC1);
376 setTime(TIME_10_MIN * 2);
377 stopUsage(PKG_SOC1);
378
379 timeRemaining = group.getTimeLimitMs() - group.getUsageTimeMs();
380 assertEquals(TIME_10_MIN, timeRemaining);
381
382 setTime(TIME_30_MIN);
383
384 assertFalse(mLimitReachedLatch.await(100L, TimeUnit.MILLISECONDS));
385
386 // Add a different package in the group
387 startUsage(PKG_GAME1);
388 setTime(TIME_30_MIN + TIME_10_MIN);
389 stopUsage(PKG_GAME1);
390
391 assertEquals(0, group.getTimeLimitMs() - group.getUsageTimeMs());
392 assertTrue(mLimitReachedLatch.await(100L, TimeUnit.MILLISECONDS));
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700393 }
394
Varun Shah2546cef2019-01-11 15:50:54 -0800395 /** Verify that usage across different apps within a group are added up */
396 @Test
397 public void testAppUsageLimitObserver_Accumulation() throws Exception {
398 setTime(0L);
Varun Shah9f58b7c2019-03-01 10:36:21 -0800399 addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN, TIME_30_MIN);
Varun Shah2546cef2019-01-11 15:50:54 -0800400 startUsage(PKG_SOC1);
401 // Add 10 mins
402 setTime(TIME_10_MIN);
403 stopUsage(PKG_SOC1);
404
405 AppTimeLimitController.UsageGroup group = getAppUsageLimitObserver(UID, OBS_ID1);
406
407 long timeRemaining = group.getTimeLimitMs() - group.getUsageTimeMs();
408 assertEquals(TIME_10_MIN * 2, timeRemaining);
409
410 startUsage(PKG_SOC1);
411 setTime(TIME_10_MIN * 2);
412 stopUsage(PKG_SOC1);
413
414 timeRemaining = group.getTimeLimitMs() - group.getUsageTimeMs();
415 assertEquals(TIME_10_MIN, timeRemaining);
416
417 setTime(TIME_30_MIN);
418
419 assertFalse(mLimitReachedLatch.await(100L, TimeUnit.MILLISECONDS));
420
421 // Add a different package in the group
422 startUsage(PKG_GAME1);
423 setTime(TIME_30_MIN + TIME_10_MIN);
424 stopUsage(PKG_GAME1);
425
426 assertEquals(0, group.getTimeLimitMs() - group.getUsageTimeMs());
427 assertTrue(mLimitReachedLatch.await(100L, TimeUnit.MILLISECONDS));
428 }
429
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700430 /** Verify that time limit does not get triggered due to a different app */
431 @Test
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700432 public void testAppUsageObserver_TimeoutOtherApp() throws Exception {
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700433 setTime(0L);
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700434 addAppUsageObserver(OBS_ID1, GROUP1, 4_000L);
435 startUsage(PKG_SOC2);
436 assertFalse(mLimitReachedLatch.await(6_000L, TimeUnit.MILLISECONDS));
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700437 setTime(6_000L);
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700438 stopUsage(PKG_SOC2);
439 assertFalse(mLimitReachedLatch.await(100L, TimeUnit.MILLISECONDS));
440 }
441
442 /** Verify that time limit does not get triggered due to a different app */
443 @Test
444 public void testUsageSessionObserver_TimeoutOtherApp() throws Exception {
445 setTime(0L);
446 addUsageSessionObserver(OBS_ID1, GROUP1, 4_000L, 1_000L);
447 startUsage(PKG_SOC2);
448 assertFalse(mLimitReachedLatch.await(6_000L, TimeUnit.MILLISECONDS));
449 setTime(6_000L);
450 stopUsage(PKG_SOC2);
451 assertFalse(mLimitReachedLatch.await(100L, TimeUnit.MILLISECONDS));
452
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700453 }
454
Varun Shah2546cef2019-01-11 15:50:54 -0800455 /** Verify that time limit does not get triggered due to a different app */
456 @Test
457 public void testAppUsageLimitObserver_TimeoutOtherApp() throws Exception {
458 setTime(0L);
Varun Shah9f58b7c2019-03-01 10:36:21 -0800459 addAppUsageLimitObserver(OBS_ID1, GROUP1, 4_000L, 4_000L);
Varun Shah2546cef2019-01-11 15:50:54 -0800460 startUsage(PKG_SOC2);
461 assertFalse(mLimitReachedLatch.await(6_000L, TimeUnit.MILLISECONDS));
462 setTime(6_000L);
463 stopUsage(PKG_SOC2);
464 assertFalse(mLimitReachedLatch.await(100L, TimeUnit.MILLISECONDS));
465 }
466
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700467 /** Verify the timeout message is delivered at the right time */
468 @Test
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700469 public void testAppUsageObserver_Timeout() throws Exception {
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700470 setTime(0L);
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700471 addAppUsageObserver(OBS_ID1, GROUP1, 4_000L);
472 startUsage(PKG_SOC1);
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700473 setTime(6_000L);
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700474 assertTrue(mLimitReachedLatch.await(6_000L, TimeUnit.MILLISECONDS));
475 stopUsage(PKG_SOC1);
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700476 // Verify that the observer was removed
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700477 assertFalse(hasAppUsageObserver(UID, OBS_ID1));
478 }
479
480 /** Verify the timeout message is delivered at the right time */
481 @Test
482 public void testUsageSessionObserver_Timeout() throws Exception {
483 setTime(0L);
484 addUsageSessionObserver(OBS_ID1, GROUP1, 4_000L, 1_000L);
485 startUsage(PKG_SOC1);
486 setTime(6_000L);
487 assertTrue(mLimitReachedLatch.await(6_000L, TimeUnit.MILLISECONDS));
488 stopUsage(PKG_SOC1);
489 // Usage has stopped, Session should end in a second. Verify session end occurs in a second
490 // (+/- 100ms, which is hopefully not too slim a margin)
491 assertFalse(mSessionEndLatch.await(900L, TimeUnit.MILLISECONDS));
492 assertTrue(mSessionEndLatch.await(200L, TimeUnit.MILLISECONDS));
493 // Verify that the observer was not removed
494 assertTrue(hasUsageSessionObserver(UID, OBS_ID1));
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700495 }
496
Varun Shah2546cef2019-01-11 15:50:54 -0800497 /** Verify the timeout message is delivered at the right time */
498 @Test
499 public void testAppUsageLimitObserver_Timeout() throws Exception {
500 setTime(0L);
Varun Shah9f58b7c2019-03-01 10:36:21 -0800501 addAppUsageLimitObserver(OBS_ID1, GROUP1, 4_000L, 4_000L);
Varun Shah2546cef2019-01-11 15:50:54 -0800502 startUsage(PKG_SOC1);
503 setTime(6_000L);
504 assertTrue(mLimitReachedLatch.await(6_000L, TimeUnit.MILLISECONDS));
505 stopUsage(PKG_SOC1);
506 // Verify that the observer was not removed
507 assertTrue(hasAppUsageLimitObserver(UID, OBS_ID1));
508 }
509
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700510 /** If an app was already running, make sure it is partially counted towards the time limit */
511 @Test
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700512 public void testAppUsageObserver_AlreadyRunning() throws Exception {
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700513 setTime(TIME_10_MIN);
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700514 startUsage(PKG_GAME1);
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700515 setTime(TIME_30_MIN);
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700516 addAppUsageObserver(OBS_ID2, GROUP_GAME, TIME_30_MIN);
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700517 setTime(TIME_30_MIN + TIME_10_MIN);
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700518 stopUsage(PKG_GAME1);
519 assertFalse(mLimitReachedLatch.await(1_000L, TimeUnit.MILLISECONDS));
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700520
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700521 startUsage(PKG_GAME2);
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700522 setTime(TIME_30_MIN + TIME_30_MIN);
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700523 stopUsage(PKG_GAME2);
524 assertTrue(mLimitReachedLatch.await(1_000L, TimeUnit.MILLISECONDS));
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700525 // Verify that the observer was removed
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700526 assertFalse(hasAppUsageObserver(UID, OBS_ID2));
527 }
528
529 /** If an app was already running, make sure it is partially counted towards the time limit */
530 @Test
531 public void testUsageSessionObserver_AlreadyRunning() throws Exception {
532 setTime(TIME_10_MIN);
533 startUsage(PKG_GAME1);
534 setTime(TIME_30_MIN);
535 addUsageSessionObserver(OBS_ID2, GROUP_GAME, TIME_30_MIN, TIME_1_MIN);
536 setTime(TIME_30_MIN + TIME_10_MIN);
537 stopUsage(PKG_GAME1);
538 assertFalse(mLimitReachedLatch.await(1_000L, TimeUnit.MILLISECONDS));
539
540 startUsage(PKG_GAME2);
541 setTime(TIME_30_MIN + TIME_30_MIN);
542 stopUsage(PKG_GAME2);
543 assertTrue(mLimitReachedLatch.await(1_000L, TimeUnit.MILLISECONDS));
544 // Verify that the observer was removed
545 assertTrue(hasUsageSessionObserver(UID, OBS_ID2));
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700546 }
547
Varun Shah2546cef2019-01-11 15:50:54 -0800548 /** If an app was already running, make sure it is partially counted towards the time limit */
549 @Test
550 public void testAppUsageLimitObserver_AlreadyRunning() throws Exception {
551 setTime(TIME_10_MIN);
552 startUsage(PKG_GAME1);
553 setTime(TIME_30_MIN);
Varun Shah9f58b7c2019-03-01 10:36:21 -0800554 addAppUsageLimitObserver(OBS_ID2, GROUP_GAME, TIME_30_MIN, TIME_30_MIN);
Varun Shah2546cef2019-01-11 15:50:54 -0800555 setTime(TIME_30_MIN + TIME_10_MIN);
556 stopUsage(PKG_GAME1);
557 assertFalse(mLimitReachedLatch.await(1_000L, TimeUnit.MILLISECONDS));
558
559 startUsage(PKG_GAME2);
560 setTime(TIME_30_MIN + TIME_30_MIN);
561 stopUsage(PKG_GAME2);
562 assertTrue(mLimitReachedLatch.await(1_000L, TimeUnit.MILLISECONDS));
563 // Verify that the observer was not removed
564 assertTrue(hasAppUsageLimitObserver(UID, OBS_ID2));
565 }
566
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700567 /** If watched app is already running, verify the timeout callback happens at the right time */
568 @Test
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700569 public void testAppUsageObserver_AlreadyRunningTimeout() throws Exception {
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700570 setTime(0);
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700571 startUsage(PKG_SOC1);
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700572 setTime(TIME_10_MIN);
573 // 10 second time limit
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700574 addAppUsageObserver(OBS_ID1, GROUP_SOC, 10_000L);
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700575 setTime(TIME_10_MIN + 5_000L);
576 // Shouldn't call back in 6 seconds
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700577 assertFalse(mLimitReachedLatch.await(6_000L, TimeUnit.MILLISECONDS));
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700578 setTime(TIME_10_MIN + 10_000L);
579 // Should call back by 11 seconds (6 earlier + 5 now)
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700580 assertTrue(mLimitReachedLatch.await(5_000L, TimeUnit.MILLISECONDS));
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700581 // Verify that the observer was removed
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700582 assertFalse(hasAppUsageObserver(UID, OBS_ID1));
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700583 }
584
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700585 /** If watched app is already running, verify the timeout callback happens at the right time */
Michael Wachenschwanzc8703092018-05-01 16:02:45 -0700586 @Test
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700587 public void testUsageSessionObserver_AlreadyRunningTimeout() throws Exception {
588 setTime(0);
589 startUsage(PKG_SOC1);
590 setTime(TIME_10_MIN);
591 // 10 second time limit
592 addUsageSessionObserver(OBS_ID1, GROUP_SOC, 10_000L, 1_000L);
593 setTime(TIME_10_MIN + 5_000L);
594 // Shouldn't call back in 6 seconds
595 assertFalse(mLimitReachedLatch.await(6_000L, TimeUnit.MILLISECONDS));
596 setTime(TIME_10_MIN + 10_000L);
597 // Should call back by 11 seconds (6 earlier + 5 now)
598 assertTrue(mLimitReachedLatch.await(5_000L, TimeUnit.MILLISECONDS));
599 stopUsage(PKG_SOC1);
600 // Usage has stopped, Session should end in a second. Verify session end occurs in a second
601 // (+/- 100ms, which is hopefully not too slim a margin)
602 assertFalse(mSessionEndLatch.await(900L, TimeUnit.MILLISECONDS));
603 assertTrue(mSessionEndLatch.await(200L, TimeUnit.MILLISECONDS));
604 // Verify that the observer was removed
605 assertTrue(hasUsageSessionObserver(UID, OBS_ID1));
606 }
607
Varun Shah2546cef2019-01-11 15:50:54 -0800608 /** If watched app is already running, verify the timeout callback happens at the right time */
609 @Test
610 public void testAppUsageLimitObserver_AlreadyRunningTimeout() throws Exception {
611 setTime(0);
612 startUsage(PKG_SOC1);
613 setTime(TIME_10_MIN);
614 // 10 second time limit
Varun Shah9f58b7c2019-03-01 10:36:21 -0800615 addAppUsageLimitObserver(OBS_ID1, GROUP_SOC, 10_000L, 10_000L);
Varun Shah2546cef2019-01-11 15:50:54 -0800616 setTime(TIME_10_MIN + 5_000L);
617 // Shouldn't call back in 6 seconds
618 assertFalse(mLimitReachedLatch.await(6_000L, TimeUnit.MILLISECONDS));
619 setTime(TIME_10_MIN + 10_000L);
620 // Should call back by 11 seconds (6 earlier + 5 now)
621 assertTrue(mLimitReachedLatch.await(5_000L, TimeUnit.MILLISECONDS));
622 // Verify that the observer was not removed
623 assertTrue(hasAppUsageLimitObserver(UID, OBS_ID1));
624 }
625
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700626 /**
627 * Verify that App Time Limit Controller will limit the number of observerIds for app usage
628 * observers
629 */
630 @Test
631 public void testAppUsageObserver_MaxObserverLimit() throws Exception {
Michael Wachenschwanzc8703092018-05-01 16:02:45 -0700632 boolean receivedException = false;
633 int ANOTHER_UID = UID + 1;
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700634 addAppUsageObserver(OBS_ID1, GROUP1, TIME_30_MIN);
635 addAppUsageObserver(OBS_ID2, GROUP1, TIME_30_MIN);
636 addAppUsageObserver(OBS_ID3, GROUP1, TIME_30_MIN);
637 addAppUsageObserver(OBS_ID4, GROUP1, TIME_30_MIN);
638 addAppUsageObserver(OBS_ID5, GROUP1, TIME_30_MIN);
639 addAppUsageObserver(OBS_ID6, GROUP1, TIME_30_MIN);
640 addAppUsageObserver(OBS_ID7, GROUP1, TIME_30_MIN);
641 addAppUsageObserver(OBS_ID8, GROUP1, TIME_30_MIN);
642 addAppUsageObserver(OBS_ID9, GROUP1, TIME_30_MIN);
643 addAppUsageObserver(OBS_ID10, GROUP1, TIME_30_MIN);
Michael Wachenschwanzc8703092018-05-01 16:02:45 -0700644 // Readding an observer should not cause an IllegalStateException
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700645 addAppUsageObserver(OBS_ID5, GROUP1, TIME_30_MIN);
Michael Wachenschwanzc8703092018-05-01 16:02:45 -0700646 // Adding an observer for a different uid shouldn't cause an IllegalStateException
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700647 mController.addAppUsageObserver(ANOTHER_UID, OBS_ID11, GROUP1, TIME_30_MIN, null, USER_ID);
Michael Wachenschwanzc8703092018-05-01 16:02:45 -0700648 try {
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700649 addAppUsageObserver(OBS_ID11, GROUP1, TIME_30_MIN);
Michael Wachenschwanzc8703092018-05-01 16:02:45 -0700650 } catch (IllegalStateException ise) {
651 receivedException = true;
652 }
653 assertTrue("Should have caused an IllegalStateException", receivedException);
654 }
655
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700656 /**
657 * Verify that App Time Limit Controller will limit the number of observerIds for usage session
658 * observers
659 */
Michael Wachenschwanzc8703092018-05-01 16:02:45 -0700660 @Test
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700661 public void testUsageSessionObserver_MaxObserverLimit() throws Exception {
662 addUsageSessionObserver(OBS_ID1, GROUP1, TIME_30_MIN, TIME_1_MIN);
663 boolean receivedException = false;
664 int ANOTHER_UID = UID + 1;
665 addUsageSessionObserver(OBS_ID2, GROUP1, TIME_30_MIN, TIME_1_MIN);
666 addUsageSessionObserver(OBS_ID3, GROUP1, TIME_30_MIN, TIME_1_MIN);
667 addUsageSessionObserver(OBS_ID4, GROUP1, TIME_30_MIN, TIME_1_MIN);
668 addUsageSessionObserver(OBS_ID5, GROUP1, TIME_30_MIN, TIME_1_MIN);
669 addUsageSessionObserver(OBS_ID6, GROUP1, TIME_30_MIN, TIME_1_MIN);
670 addUsageSessionObserver(OBS_ID7, GROUP1, TIME_30_MIN, TIME_1_MIN);
671 addUsageSessionObserver(OBS_ID8, GROUP1, TIME_30_MIN, TIME_1_MIN);
672 addUsageSessionObserver(OBS_ID9, GROUP1, TIME_30_MIN, TIME_1_MIN);
673 addUsageSessionObserver(OBS_ID10, GROUP1, TIME_30_MIN, TIME_1_MIN);
674 // Readding an observer should not cause an IllegalStateException
675 addUsageSessionObserver(OBS_ID5, GROUP1, TIME_30_MIN, TIME_1_MIN);
676 // Adding an observer for a different uid shouldn't cause an IllegalStateException
677 mController.addUsageSessionObserver(ANOTHER_UID, OBS_ID11, GROUP1, TIME_30_MIN, TIME_1_MIN,
678 null, null, USER_ID);
679 try {
680 addUsageSessionObserver(OBS_ID11, GROUP1, TIME_30_MIN, TIME_1_MIN);
681 } catch (IllegalStateException ise) {
682 receivedException = true;
683 }
684 assertTrue("Should have caused an IllegalStateException", receivedException);
685 }
686
Varun Shah2546cef2019-01-11 15:50:54 -0800687 /**
688 * Verify that App Time Limit Controller will limit the number of observerIds for app usage
689 * limit observers
690 */
691 @Test
692 public void testAppUsageLimitObserver_MaxObserverLimit() throws Exception {
693 boolean receivedException = false;
694 int ANOTHER_UID = UID + 1;
Varun Shah9f58b7c2019-03-01 10:36:21 -0800695 addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN, TIME_30_MIN);
696 addAppUsageLimitObserver(OBS_ID2, GROUP1, TIME_30_MIN, TIME_30_MIN);
697 addAppUsageLimitObserver(OBS_ID3, GROUP1, TIME_30_MIN, TIME_30_MIN);
698 addAppUsageLimitObserver(OBS_ID4, GROUP1, TIME_30_MIN, TIME_30_MIN);
699 addAppUsageLimitObserver(OBS_ID5, GROUP1, TIME_30_MIN, TIME_30_MIN);
700 addAppUsageLimitObserver(OBS_ID6, GROUP1, TIME_30_MIN, TIME_30_MIN);
701 addAppUsageLimitObserver(OBS_ID7, GROUP1, TIME_30_MIN, TIME_30_MIN);
702 addAppUsageLimitObserver(OBS_ID8, GROUP1, TIME_30_MIN, TIME_30_MIN);
703 addAppUsageLimitObserver(OBS_ID9, GROUP1, TIME_30_MIN, TIME_30_MIN);
704 addAppUsageLimitObserver(OBS_ID10, GROUP1, TIME_30_MIN, TIME_30_MIN);
Varun Shah2546cef2019-01-11 15:50:54 -0800705 // Readding an observer should not cause an IllegalStateException
Varun Shah9f58b7c2019-03-01 10:36:21 -0800706 addAppUsageLimitObserver(OBS_ID5, GROUP1, TIME_30_MIN, TIME_30_MIN);
Varun Shah2546cef2019-01-11 15:50:54 -0800707 // Adding an observer for a different uid shouldn't cause an IllegalStateException
708 mController.addAppUsageLimitObserver(
Varun Shah9f58b7c2019-03-01 10:36:21 -0800709 ANOTHER_UID, OBS_ID11, GROUP1, TIME_30_MIN, TIME_30_MIN, null, USER_ID);
Varun Shah2546cef2019-01-11 15:50:54 -0800710 try {
Varun Shah9f58b7c2019-03-01 10:36:21 -0800711 addAppUsageLimitObserver(OBS_ID11, GROUP1, TIME_30_MIN, TIME_30_MIN);
Varun Shah2546cef2019-01-11 15:50:54 -0800712 } catch (IllegalStateException ise) {
713 receivedException = true;
714 }
715 assertTrue("Should have caused an IllegalStateException", receivedException);
716 }
717
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700718 /** Verify that addAppUsageObserver minimum time limit is one minute */
719 @Test
720 public void testAppUsageObserver_MinimumTimeLimit() throws Exception {
Michael Wachenschwanzc8703092018-05-01 16:02:45 -0700721 boolean receivedException = false;
722 // adding an observer with a one minute time limit should not cause an exception
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700723 addAppUsageObserver(OBS_ID1, GROUP1, MIN_TIME_LIMIT);
Michael Wachenschwanzc8703092018-05-01 16:02:45 -0700724 try {
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700725 addAppUsageObserver(OBS_ID1, GROUP1, MIN_TIME_LIMIT - 1);
Michael Wachenschwanzc8703092018-05-01 16:02:45 -0700726 } catch (IllegalArgumentException iae) {
727 receivedException = true;
728 }
729 assertTrue("Should have caused an IllegalArgumentException", receivedException);
730 }
731
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700732 /** Verify that addUsageSessionObserver minimum time limit is one minute */
733 @Test
734 public void testUsageSessionObserver_MinimumTimeLimit() throws Exception {
735 boolean receivedException = false;
736 // test also for session observers
737 addUsageSessionObserver(OBS_ID10, GROUP1, MIN_TIME_LIMIT, TIME_1_MIN);
738 try {
739 addUsageSessionObserver(OBS_ID10, GROUP1, MIN_TIME_LIMIT - 1, TIME_1_MIN);
740 } catch (IllegalArgumentException iae) {
741 receivedException = true;
742 }
743 assertTrue("Should have caused an IllegalArgumentException", receivedException);
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700744 }
745
Varun Shah2546cef2019-01-11 15:50:54 -0800746 /** Verify that addAppUsageLimitObserver minimum time limit is one minute */
747 @Test
748 public void testAppUsageLimitObserver_MinimumTimeLimit() throws Exception {
749 boolean receivedException = false;
750 // adding an observer with a one minute time limit should not cause an exception
Varun Shah9f58b7c2019-03-01 10:36:21 -0800751 addAppUsageLimitObserver(OBS_ID1, GROUP1, MIN_TIME_LIMIT, MIN_TIME_LIMIT);
Varun Shah2546cef2019-01-11 15:50:54 -0800752 try {
Varun Shah9f58b7c2019-03-01 10:36:21 -0800753 addAppUsageLimitObserver(OBS_ID1, GROUP1, MIN_TIME_LIMIT - 1, MIN_TIME_LIMIT - 1);
Varun Shah2546cef2019-01-11 15:50:54 -0800754 } catch (IllegalArgumentException iae) {
755 receivedException = true;
756 }
757 assertTrue("Should have caused an IllegalArgumentException", receivedException);
758 }
759
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700760 /** Verify that concurrent usage from multiple apps in the same group will counted correctly */
761 @Test
762 public void testAppUsageObserver_ConcurrentUsage() throws Exception {
763 setTime(0L);
764 addAppUsageObserver(OBS_ID1, GROUP1, TIME_30_MIN);
765 AppTimeLimitController.UsageGroup group = mController.getAppUsageGroup(UID, OBS_ID1);
766 startUsage(PKG_SOC1);
767 // Add 10 mins
768 setTime(TIME_10_MIN);
769
770 // Add a different package in the group will first package is still in use
771 startUsage(PKG_GAME1);
772 setTime(TIME_10_MIN * 2);
773 // Stop first package usage
774 stopUsage(PKG_SOC1);
775
776 setTime(TIME_30_MIN);
777 stopUsage(PKG_GAME1);
778
779 assertEquals(TIME_30_MIN, group.getUsageTimeMs());
780 assertTrue(mLimitReachedLatch.await(100L, TimeUnit.MILLISECONDS));
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700781 }
782
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700783 /** Verify that concurrent usage from multiple apps in the same group will counted correctly */
784 @Test
785 public void testUsageSessionObserver_ConcurrentUsage() throws Exception {
786 setTime(0L);
787 addUsageSessionObserver(OBS_ID1, GROUP1, TIME_30_MIN, TIME_1_MIN);
788 AppTimeLimitController.UsageGroup group = mController.getSessionUsageGroup(UID, OBS_ID1);
789 startUsage(PKG_SOC1);
790 // Add 10 mins
791 setTime(TIME_10_MIN);
792
793 // Add a different package in the group will first package is still in use
794 startUsage(PKG_GAME1);
795 setTime(TIME_10_MIN * 2);
796 // Stop first package usage
797 stopUsage(PKG_SOC1);
798
799 setTime(TIME_30_MIN);
800 stopUsage(PKG_GAME1);
801
802 assertEquals(TIME_30_MIN, group.getUsageTimeMs());
803 assertTrue(mLimitReachedLatch.await(100L, TimeUnit.MILLISECONDS));
Amith Yamasani62ec27e92018-03-11 14:42:06 -0700804 }
805
Varun Shah2546cef2019-01-11 15:50:54 -0800806 /** Verify that concurrent usage from multiple apps in the same group will counted correctly */
807 @Test
808 public void testAppUsageLimitObserver_ConcurrentUsage() throws Exception {
809 setTime(0L);
Varun Shah9f58b7c2019-03-01 10:36:21 -0800810 addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN, TIME_30_MIN);
Varun Shah2546cef2019-01-11 15:50:54 -0800811 AppTimeLimitController.UsageGroup group = getAppUsageLimitObserver(UID, OBS_ID1);
812 startUsage(PKG_SOC1);
813 // Add 10 mins
814 setTime(TIME_10_MIN);
815
816 // Add a different package in the group will first package is still in use
817 startUsage(PKG_GAME1);
818 setTime(TIME_10_MIN * 2);
819 // Stop first package usage
820 stopUsage(PKG_SOC1);
821
822 setTime(TIME_30_MIN);
823 stopUsage(PKG_GAME1);
824
825 assertEquals(TIME_30_MIN, group.getUsageTimeMs());
826 assertTrue(mLimitReachedLatch.await(100L, TimeUnit.MILLISECONDS));
827 }
828
Michael Wachenschwanz0f472842018-10-23 23:02:48 -0700829 /** Verify that a session will continue if usage starts again within the session threshold */
830 @Test
831 public void testUsageSessionObserver_ContinueSession() throws Exception {
832 setTime(0L);
833 addUsageSessionObserver(OBS_ID1, GROUP1, 10_000L, 2_000L);
834 startUsage(PKG_SOC1);
835 setTime(6_000L);
836 stopUsage(PKG_SOC1);
837 // Wait momentarily, Session should not end
838 assertFalse(mSessionEndLatch.await(1_000L, TimeUnit.MILLISECONDS));
839
840 setTime(7_000L);
841 startUsage(PKG_SOC1);
842 setTime(10_500L);
843 stopUsage(PKG_SOC1);
844 // Total usage time has not reached the limit. Time limit callback should not fire yet
845 assertFalse(mLimitReachedLatch.await(100L, TimeUnit.MILLISECONDS));
846
847 setTime(10_600L);
848 startUsage(PKG_SOC1);
849 setTime(12_000L);
850 assertTrue(mLimitReachedLatch.await(1_000L, TimeUnit.MILLISECONDS));
851 stopUsage(PKG_SOC1);
852 // Usage has stopped, Session should end in 2 seconds. Verify session end occurs
853 // (+/- 100ms, which is hopefully not too slim a margin)
854 assertFalse(mSessionEndLatch.await(1_900L, TimeUnit.MILLISECONDS));
855 assertTrue(mSessionEndLatch.await(200L, TimeUnit.MILLISECONDS));
856 // Verify that the observer was not removed
857 assertTrue(hasUsageSessionObserver(UID, OBS_ID1));
858 }
859
860 /** Verify that a new session will start if next usage starts after the session threshold */
861 @Test
862 public void testUsageSessionObserver_NewSession() throws Exception {
863 setTime(0L);
864 addUsageSessionObserver(OBS_ID1, GROUP1, 10_000L, 1_000L);
865 startUsage(PKG_SOC1);
866 setTime(6_000L);
867 stopUsage(PKG_SOC1);
868 // Wait for longer than the session threshold. Session end callback should not be triggered
869 // because the usage timelimit hasn't been triggered.
870 assertFalse(mSessionEndLatch.await(1_500L, TimeUnit.MILLISECONDS));
871
872 setTime(7_500L);
873 // This should be the start of a new session
874 startUsage(PKG_SOC1);
875 setTime(16_000L);
876 stopUsage(PKG_SOC1);
877 // Total usage has exceed the timelimit, but current session time has not
878 assertFalse(mLimitReachedLatch.await(100L, TimeUnit.MILLISECONDS));
879
880 setTime(16_100L);
881 startUsage(PKG_SOC1);
882 setTime(18_000L);
883 assertTrue(mLimitReachedLatch.await(2000L, TimeUnit.MILLISECONDS));
884 stopUsage(PKG_SOC1);
885 // Usage has stopped, Session should end in 2 seconds. Verify session end occurs
886 // (+/- 100ms, which is hopefully not too slim a margin)
887 assertFalse(mSessionEndLatch.await(900L, TimeUnit.MILLISECONDS));
888 assertTrue(mSessionEndLatch.await(200L, TimeUnit.MILLISECONDS));
889 // Verify that the observer was not removed
890 assertTrue(hasUsageSessionObserver(UID, OBS_ID1));
891 }
892
893 /** Verify that the callbacks will be triggered for multiple sessions */
894 @Test
895 public void testUsageSessionObserver_RepeatSessions() throws Exception {
896 setTime(0L);
897 addUsageSessionObserver(OBS_ID1, GROUP1, 10_000L, 1_000L);
898 startUsage(PKG_SOC1);
899 setTime(9_000L);
900 stopUsage(PKG_SOC1);
901 // Stutter usage here, to reduce real world time needed trigger limit reached callback
902 startUsage(PKG_SOC1);
903 setTime(11_000L);
904 assertTrue(mLimitReachedLatch.await(2_000L, TimeUnit.MILLISECONDS));
905 stopUsage(PKG_SOC1);
906 // Usage has stopped, Session should end in 1 seconds. Verify session end occurs
907 // (+/- 100ms, which is hopefully not too slim a margin)
908 assertFalse(mSessionEndLatch.await(900L, TimeUnit.MILLISECONDS));
909 assertTrue(mSessionEndLatch.await(200L, TimeUnit.MILLISECONDS));
910
911 // Rearm the countdown latches
912 mLimitReachedLatch = new CountDownLatch(1);
913 mSessionEndLatch = new CountDownLatch(1);
914
915 // New session start
916 setTime(20_000L);
917 startUsage(PKG_SOC1);
918 setTime(29_000L);
919 stopUsage(PKG_SOC1);
920 startUsage(PKG_SOC1);
921 setTime(31_000L);
922 assertTrue(mLimitReachedLatch.await(2_000L, TimeUnit.MILLISECONDS));
923 stopUsage(PKG_SOC1);
924 assertFalse(mSessionEndLatch.await(900L, TimeUnit.MILLISECONDS));
925 assertTrue(mSessionEndLatch.await(200L, TimeUnit.MILLISECONDS));
926 assertTrue(hasUsageSessionObserver(UID, OBS_ID1));
927 }
928
Michael Wachenschwanz36778522018-11-12 11:06:19 -0800929 /** Verify the timeout message is delivered at the right time after past usage was reported */
930 @Test
931 public void testAppUsageObserver_PastUsage() throws Exception {
932 setTime(10_000L);
933 addAppUsageObserver(OBS_ID1, GROUP1, 6_000L);
934 setTime(20_000L);
935 startPastUsage(PKG_SOC1, 5_000);
936 setTime(21_000L);
937 assertTrue(mLimitReachedLatch.await(2_000L, TimeUnit.MILLISECONDS));
938 stopUsage(PKG_SOC1);
939 // Verify that the observer was removed
940 assertFalse(hasAppUsageObserver(UID, OBS_ID1));
941 }
942
943 /**
944 * Verify the timeout message is delivered at the right time after past usage was reported
945 * that overlaps with already known usage
946 */
947 @Test
948 public void testAppUsageObserver_PastUsageOverlap() throws Exception {
949 setTime(0L);
950 addAppUsageObserver(OBS_ID1, GROUP1, 20_000L);
951 setTime(10_000L);
952 startUsage(PKG_SOC1);
953 setTime(20_000L);
954 stopUsage(PKG_SOC1);
955 setTime(25_000L);
956 startPastUsage(PKG_SOC1, 9_000);
957 setTime(26_000L);
958 // the 4 seconds of overlapped usage should not be counted
959 assertFalse(mLimitReachedLatch.await(2_000L, TimeUnit.MILLISECONDS));
960 setTime(30_000L);
961 assertTrue(mLimitReachedLatch.await(4_000L, TimeUnit.MILLISECONDS));
962 stopUsage(PKG_SOC1);
963 // Verify that the observer was removed
964 assertFalse(hasAppUsageObserver(UID, OBS_ID1));
965 }
966
Varun Shah2546cef2019-01-11 15:50:54 -0800967 /** Verify app usage limit observer added correctly reports its total usage limit */
968 @Test
969 public void testAppUsageLimitObserver_GetTotalUsageLimit() {
Varun Shah9f58b7c2019-03-01 10:36:21 -0800970 addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN, TIME_30_MIN);
Varun Shah2546cef2019-01-11 15:50:54 -0800971 AppTimeLimitController.AppUsageLimitGroup group = getAppUsageLimitObserver(UID, OBS_ID1);
972 assertNotNull("Observer wasn't added", group);
973 assertEquals("Observer didn't correctly report total usage limit",
974 TIME_30_MIN, group.getTotaUsageLimit());
975 }
976
977 /** Verify app usage limit observer added correctly reports its total usage limit */
978 @Test
979 public void testAppUsageLimitObserver_GetUsageRemaining() {
980 setTime(0L);
Varun Shah9f58b7c2019-03-01 10:36:21 -0800981 addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN, TIME_30_MIN);
Varun Shah2546cef2019-01-11 15:50:54 -0800982 startUsage(PKG_SOC1);
983 setTime(TIME_10_MIN);
984 stopUsage(PKG_SOC1);
985 AppTimeLimitController.AppUsageLimitGroup group = getAppUsageLimitObserver(UID, OBS_ID1);
986 assertNotNull("Observer wasn't added", group);
987 assertEquals("Observer didn't correctly report total usage limit",
988 TIME_10_MIN * 2, group.getUsageRemaining());
989 }
990
991 /** Verify the app usage limit observer with the smallest usage limit remaining is returned
992 * when querying the getAppUsageLimit API.
993 */
994 @Test
995 public void testAppUsageLimitObserver_GetAppUsageLimit() {
Varun Shah9f58b7c2019-03-01 10:36:21 -0800996 addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN, TIME_30_MIN);
997 addAppUsageLimitObserver(OBS_ID2, GROUP_SOC, TIME_10_MIN, TIME_10_MIN);
Varun Shah2546cef2019-01-11 15:50:54 -0800998 UsageStatsManagerInternal.AppUsageLimitData group = getAppUsageLimit(PKG_SOC1);
999 assertEquals("Observer with the smallest usage limit remaining wasn't returned",
1000 TIME_10_MIN, group.getTotalUsageLimit());
1001 }
1002
1003 /** Verify the app usage limit observer with the smallest usage limit remaining is returned
1004 * when querying the getAppUsageLimit API.
1005 */
1006 @Test
1007 public void testAppUsageLimitObserver_GetAppUsageLimitUsed() {
1008 setTime(0L);
Varun Shah9f58b7c2019-03-01 10:36:21 -08001009 addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN, TIME_30_MIN);
1010 addAppUsageLimitObserver(OBS_ID2, GROUP_SOC, TIME_10_MIN, TIME_10_MIN);
Varun Shah2546cef2019-01-11 15:50:54 -08001011 startUsage(PKG_GAME1);
1012 setTime(TIME_10_MIN * 2 + TIME_1_MIN);
1013 stopUsage(PKG_GAME1);
1014 // PKG_GAME1 is only in GROUP1 but since we're querying for PCK_SOC1 which is
1015 // in both groups, GROUP1 should be returned since it has a smaller time remaining
1016 UsageStatsManagerInternal.AppUsageLimitData group = getAppUsageLimit(PKG_SOC1);
1017 assertEquals("Observer with the smallest usage limit remaining wasn't returned",
1018 TIME_1_MIN * 9, group.getUsageRemaining());
1019 }
1020
1021 /** Verify the app usage limit observer with the smallest usage limit remaining is returned
1022 * when querying the getAppUsageLimit API.
1023 */
1024 @Test
1025 public void testAppUsageLimitObserver_GetAppUsageLimitAllUsed() {
1026 setTime(0L);
Varun Shah9f58b7c2019-03-01 10:36:21 -08001027 addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN, TIME_30_MIN);
1028 addAppUsageLimitObserver(OBS_ID2, GROUP_SOC, TIME_10_MIN, TIME_10_MIN);
Varun Shah2546cef2019-01-11 15:50:54 -08001029 startUsage(PKG_SOC1);
1030 setTime(TIME_10_MIN);
1031 stopUsage(PKG_SOC1);
1032 // GROUP_SOC should be returned since it should be completely used up (0ms remaining)
1033 UsageStatsManagerInternal.AppUsageLimitData group = getAppUsageLimit(PKG_SOC1);
1034 assertEquals("Observer with the smallest usage limit remaining wasn't returned",
1035 0L, group.getUsageRemaining());
1036 }
1037
Varun Shah9f58b7c2019-03-01 10:36:21 -08001038 /** Verify that a limit of 0 is not allowed. */
1039 @Test
1040 public void testAppUsageLimitObserver_ZeroTimeLimitIsNotAllowed() {
1041 try {
1042 addAppUsageLimitObserver(OBS_ID1, GROUP1, 0, 0);
1043 fail("timeLimit of 0 should not be allowed.");
1044 } catch (IllegalArgumentException expected) {
1045 // Exception expected.
1046 }
1047 }
1048
Varun Shah54f7f7f2019-02-07 10:21:17 -08001049 /** Verify that a limit of 0 is allowed for the special case of re-registering an observer. */
1050 @Test
Varun Shah9f58b7c2019-03-01 10:36:21 -08001051 public void testAppUsageLimitObserver_ZeroTimeRemainingIsAllowed() {
1052 addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_1_MIN, 0);
Varun Shah54f7f7f2019-02-07 10:21:17 -08001053 AppTimeLimitController.AppUsageLimitGroup group = getAppUsageLimitObserver(UID, OBS_ID1);
1054 assertNotNull("Observer wasn't added", group);
1055 assertEquals("Usage remaining was not 0.", 0, group.getUsageRemaining());
1056 }
1057
Michael Wachenschwanz0f472842018-10-23 23:02:48 -07001058 private void startUsage(String packageName) {
1059 mController.noteUsageStart(packageName, USER_ID);
1060 }
1061
Michael Wachenschwanz36778522018-11-12 11:06:19 -08001062 private void startPastUsage(String packageName, int timeAgo) {
1063 mController.noteUsageStart(packageName, USER_ID, timeAgo);
1064 }
1065
Michael Wachenschwanz0f472842018-10-23 23:02:48 -07001066 private void stopUsage(String packageName) {
1067 mController.noteUsageStop(packageName, USER_ID);
1068 }
1069
1070 private void addAppUsageObserver(int observerId, String[] packages, long timeLimit) {
1071 mController.addAppUsageObserver(UID, observerId, packages, timeLimit, null, USER_ID);
1072 }
1073
1074 private void addUsageSessionObserver(int observerId, String[] packages, long timeLimit,
1075 long sessionThreshold) {
1076 mController.addUsageSessionObserver(UID, observerId, packages, timeLimit, sessionThreshold,
1077 null, null, USER_ID);
1078 }
1079
Varun Shah9f58b7c2019-03-01 10:36:21 -08001080 private void addAppUsageLimitObserver(int observerId, String[] packages, long timeLimit,
1081 long timeRemaining) {
1082 mController.addAppUsageLimitObserver(UID, observerId, packages, timeLimit, timeRemaining,
1083 null, USER_ID);
Varun Shah2546cef2019-01-11 15:50:54 -08001084 }
1085
Michael Wachenschwanz0f472842018-10-23 23:02:48 -07001086 /** Is there still an app usage observer by that id */
1087 private boolean hasAppUsageObserver(int uid, int observerId) {
1088 return mController.getAppUsageGroup(uid, observerId) != null;
1089 }
1090
1091 /** Is there still an usage session observer by that id */
1092 private boolean hasUsageSessionObserver(int uid, int observerId) {
1093 return mController.getSessionUsageGroup(uid, observerId) != null;
Amith Yamasani62ec27e92018-03-11 14:42:06 -07001094 }
1095
Varun Shah2546cef2019-01-11 15:50:54 -08001096 /** Is there still an app usage limit observer by that id */
1097 private boolean hasAppUsageLimitObserver(int uid, int observerId) {
1098 return mController.getAppUsageLimitGroup(uid, observerId) != null;
1099 }
1100
1101 private AppTimeLimitController.AppUsageLimitGroup getAppUsageLimitObserver(
1102 int uid, int observerId) {
1103 return mController.getAppUsageLimitGroup(uid, observerId);
1104 }
1105
1106 private UsageStatsManagerInternal.AppUsageLimitData getAppUsageLimit(String packageName) {
1107 return mController.getAppUsageLimit(packageName, UserHandle.of(USER_ID));
1108 }
1109
Amith Yamasani62ec27e92018-03-11 14:42:06 -07001110 private void setTime(long time) {
1111 mUptimeMillis = time;
1112 }
1113}