Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.usage; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Assert.assertFalse; |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 21 | import static org.junit.Assert.assertNotNull; |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 22 | import static org.junit.Assert.assertTrue; |
Michael Wachenschwanz | ae9811d | 2018-11-12 14:45:25 -0800 | [diff] [blame] | 23 | import static org.junit.Assert.fail; |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 24 | |
| 25 | import android.app.PendingIntent; |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 26 | import android.app.usage.UsageStatsManagerInternal; |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 27 | import android.os.HandlerThread; |
| 28 | import android.os.Looper; |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 29 | import android.os.UserHandle; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 30 | |
Michael Wachenschwanz | ae9811d | 2018-11-12 14:45:25 -0800 | [diff] [blame] | 31 | import androidx.test.filters.LargeTest; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 32 | import androidx.test.runner.AndroidJUnit4; |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 33 | |
| 34 | import org.junit.After; |
| 35 | import org.junit.Before; |
| 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
| 38 | |
Michael Wachenschwanz | ae9811d | 2018-11-12 14:45:25 -0800 | [diff] [blame] | 39 | import java.io.PrintWriter; |
| 40 | import java.io.StringWriter; |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 41 | import java.util.concurrent.CountDownLatch; |
| 42 | import java.util.concurrent.TimeUnit; |
| 43 | |
| 44 | @RunWith(AndroidJUnit4.class) |
Michael Wachenschwanz | ae9811d | 2018-11-12 14:45:25 -0800 | [diff] [blame] | 45 | @LargeTest |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 46 | public 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 Wachenschwanz | c870309 | 2018-05-01 16:02:45 -0700 | [diff] [blame] | 59 | 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 Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 67 | |
Michael Wachenschwanz | c870309 | 2018-05-01 16:02:45 -0700 | [diff] [blame] | 68 | private static final long TIME_30_MIN = 30 * 60_000L; |
| 69 | private static final long TIME_10_MIN = 10 * 60_000L; |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 70 | private static final long TIME_1_MIN = 1 * 60_000L; |
Michael Wachenschwanz | c870309 | 2018-05-01 16:02:45 -0700 | [diff] [blame] | 71 | |
| 72 | private static final long MAX_OBSERVER_PER_UID = 10; |
| 73 | private static final long MIN_TIME_LIMIT = 4_000L; |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 74 | |
| 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 Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 87 | private CountDownLatch mLimitReachedLatch = new CountDownLatch(1); |
| 88 | private CountDownLatch mSessionEndLatch = new CountDownLatch(1); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 89 | |
| 90 | private AppTimeLimitController mController; |
| 91 | |
| 92 | private HandlerThread mThread; |
| 93 | |
| 94 | private long mUptimeMillis; |
| 95 | |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 96 | 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 Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 104 | |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 105 | @Override |
| 106 | public void onSessionEnd(int observerId, int userId, long timeElapsed, |
| 107 | PendingIntent callbackIntent) { |
| 108 | mSessionEndLatch.countDown(); |
| 109 | } |
| 110 | }; |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 111 | |
| 112 | class MyAppTimeLimitController extends AppTimeLimitController { |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 113 | MyAppTimeLimitController(AppTimeLimitController.TimeLimitCallbackListener listener, |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 114 | Looper looper) { |
| 115 | super(listener, looper); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | protected long getUptimeMillis() { |
| 120 | return mUptimeMillis; |
| 121 | } |
Michael Wachenschwanz | c870309 | 2018-05-01 16:02:45 -0700 | [diff] [blame] | 122 | |
| 123 | @Override |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 124 | protected long getAppUsageObserverPerUidLimit() { |
| 125 | return MAX_OBSERVER_PER_UID; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | protected long getUsageSessionObserverPerUidLimit() { |
Michael Wachenschwanz | c870309 | 2018-05-01 16:02:45 -0700 | [diff] [blame] | 130 | return MAX_OBSERVER_PER_UID; |
| 131 | } |
| 132 | |
| 133 | @Override |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 134 | protected long getAppUsageLimitObserverPerUidLimit() { |
| 135 | return MAX_OBSERVER_PER_UID; |
| 136 | } |
| 137 | |
| 138 | @Override |
Michael Wachenschwanz | c870309 | 2018-05-01 16:02:45 -0700 | [diff] [blame] | 139 | protected long getMinTimeLimit() { |
| 140 | return MIN_TIME_LIMIT; |
| 141 | } |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 142 | } |
| 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 Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 156 | /** Verify app usage observer is added */ |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 157 | @Test |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 158 | 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 Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 166 | /** Verify usage session observer is added */ |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 167 | @Test |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 168 | 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 Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 175 | /** Verify app usage limit observer is added */ |
| 176 | @Test |
| 177 | public void testAppUsageLimitObserver_AddObserver() { |
| 178 | addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN); |
| 179 | assertTrue("Observer wasn't added", hasAppUsageLimitObserver(UID, OBS_ID1)); |
| 180 | addAppUsageLimitObserver(OBS_ID2, GROUP_GAME, TIME_30_MIN); |
| 181 | assertTrue("Observer wasn't added", hasAppUsageLimitObserver(UID, OBS_ID2)); |
| 182 | assertTrue("Observer wasn't added", hasAppUsageLimitObserver(UID, OBS_ID1)); |
| 183 | } |
| 184 | |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 185 | /** 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 Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 203 | /** Verify app usage limit observer is removed */ |
| 204 | @Test |
| 205 | public void testAppUsageLimitObserver_RemoveObserver() { |
| 206 | addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN); |
| 207 | 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 Wachenschwanz | ae9811d | 2018-11-12 14:45:25 -0800 | [diff] [blame] | 212 | /** 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 Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 248 | /** 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 Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 266 | /** Re-adding an observer should result in only one copy */ |
| 267 | @Test |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 268 | 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 Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 272 | assertTrue("Observer wasn't added", |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 273 | 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 Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 290 | /** Re-adding an observer should result in only one copy */ |
| 291 | @Test |
| 292 | public void testAppUsageLimitObserver_ObserverReAdd() { |
| 293 | addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN); |
| 294 | assertTrue("Observer wasn't added", hasAppUsageLimitObserver(UID, OBS_ID1)); |
| 295 | addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_10_MIN); |
| 296 | 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 Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 302 | /** 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 Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 307 | addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_10_MIN); |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 308 | assertTrue("Observer wasn't added", hasAppUsageObserver(UID, OBS_ID1)); |
| 309 | assertTrue("Observer wasn't added", hasUsageSessionObserver(UID, OBS_ID1)); |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 310 | assertTrue("Observer wasn't added", hasAppUsageLimitObserver(UID, OBS_ID1)); |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 311 | |
| 312 | AppTimeLimitController.UsageGroup appUsageGroup = mController.getAppUsageGroup(UID, |
| 313 | OBS_ID1); |
| 314 | AppTimeLimitController.UsageGroup sessionUsageGroup = mController.getSessionUsageGroup(UID, |
| 315 | OBS_ID1); |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 316 | AppTimeLimitController.UsageGroup appUsageLimitGroup = getAppUsageLimitObserver( |
| 317 | UID, OBS_ID1); |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 318 | |
| 319 | // Verify data still intact |
| 320 | assertEquals(TIME_10_MIN, appUsageGroup.getTimeLimitMs()); |
| 321 | assertEquals(TIME_30_MIN, sessionUsageGroup.getTimeLimitMs()); |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 322 | assertEquals(TIME_10_MIN, appUsageLimitGroup.getTimeLimitMs()); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /** Verify that usage across different apps within a group are added up */ |
| 326 | @Test |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 327 | public void testAppUsageObserver_Accumulation() throws Exception { |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 328 | setTime(0L); |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 329 | addAppUsageObserver(OBS_ID1, GROUP1, TIME_30_MIN); |
| 330 | startUsage(PKG_SOC1); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 331 | // Add 10 mins |
| 332 | setTime(TIME_10_MIN); |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 333 | stopUsage(PKG_SOC1); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 334 | |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 335 | AppTimeLimitController.UsageGroup group = mController.getAppUsageGroup(UID, OBS_ID1); |
| 336 | |
| 337 | long timeRemaining = group.getTimeLimitMs() - group.getUsageTimeMs(); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 338 | assertEquals(TIME_10_MIN * 2, timeRemaining); |
| 339 | |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 340 | startUsage(PKG_SOC1); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 341 | setTime(TIME_10_MIN * 2); |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 342 | stopUsage(PKG_SOC1); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 343 | |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 344 | timeRemaining = group.getTimeLimitMs() - group.getUsageTimeMs(); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 345 | assertEquals(TIME_10_MIN, timeRemaining); |
| 346 | |
| 347 | setTime(TIME_30_MIN); |
| 348 | |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 349 | assertFalse(mLimitReachedLatch.await(100L, TimeUnit.MILLISECONDS)); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 350 | |
| 351 | // Add a different package in the group |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 352 | startUsage(PKG_GAME1); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 353 | setTime(TIME_30_MIN + TIME_10_MIN); |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 354 | stopUsage(PKG_GAME1); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 355 | |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 356 | 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 Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 364 | addUsageSessionObserver(OBS_ID1, GROUP1, TIME_30_MIN, TIME_10_MIN); |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 365 | 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 Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 395 | /** Verify that usage across different apps within a group are added up */ |
| 396 | @Test |
| 397 | public void testAppUsageLimitObserver_Accumulation() throws Exception { |
| 398 | setTime(0L); |
| 399 | addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN); |
| 400 | 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 Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 430 | /** Verify that time limit does not get triggered due to a different app */ |
| 431 | @Test |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 432 | public void testAppUsageObserver_TimeoutOtherApp() throws Exception { |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 433 | setTime(0L); |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 434 | addAppUsageObserver(OBS_ID1, GROUP1, 4_000L); |
| 435 | startUsage(PKG_SOC2); |
| 436 | assertFalse(mLimitReachedLatch.await(6_000L, TimeUnit.MILLISECONDS)); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 437 | setTime(6_000L); |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 438 | 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 Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 455 | /** 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); |
| 459 | addAppUsageLimitObserver(OBS_ID1, GROUP1, 4_000L); |
| 460 | 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 Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 467 | /** Verify the timeout message is delivered at the right time */ |
| 468 | @Test |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 469 | public void testAppUsageObserver_Timeout() throws Exception { |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 470 | setTime(0L); |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 471 | addAppUsageObserver(OBS_ID1, GROUP1, 4_000L); |
| 472 | startUsage(PKG_SOC1); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 473 | setTime(6_000L); |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 474 | assertTrue(mLimitReachedLatch.await(6_000L, TimeUnit.MILLISECONDS)); |
| 475 | stopUsage(PKG_SOC1); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 476 | // Verify that the observer was removed |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 477 | 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 Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 497 | /** Verify the timeout message is delivered at the right time */ |
| 498 | @Test |
| 499 | public void testAppUsageLimitObserver_Timeout() throws Exception { |
| 500 | setTime(0L); |
| 501 | addAppUsageLimitObserver(OBS_ID1, GROUP1, 4_000L); |
| 502 | 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 Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 510 | /** If an app was already running, make sure it is partially counted towards the time limit */ |
| 511 | @Test |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 512 | public void testAppUsageObserver_AlreadyRunning() throws Exception { |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 513 | setTime(TIME_10_MIN); |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 514 | startUsage(PKG_GAME1); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 515 | setTime(TIME_30_MIN); |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 516 | addAppUsageObserver(OBS_ID2, GROUP_GAME, TIME_30_MIN); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 517 | setTime(TIME_30_MIN + TIME_10_MIN); |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 518 | stopUsage(PKG_GAME1); |
| 519 | assertFalse(mLimitReachedLatch.await(1_000L, TimeUnit.MILLISECONDS)); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 520 | |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 521 | startUsage(PKG_GAME2); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 522 | setTime(TIME_30_MIN + TIME_30_MIN); |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 523 | stopUsage(PKG_GAME2); |
| 524 | assertTrue(mLimitReachedLatch.await(1_000L, TimeUnit.MILLISECONDS)); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 525 | // Verify that the observer was removed |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 526 | 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 Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 548 | /** 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); |
| 554 | addAppUsageLimitObserver(OBS_ID2, GROUP_GAME, TIME_30_MIN); |
| 555 | 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 Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 567 | /** If watched app is already running, verify the timeout callback happens at the right time */ |
| 568 | @Test |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 569 | public void testAppUsageObserver_AlreadyRunningTimeout() throws Exception { |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 570 | setTime(0); |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 571 | startUsage(PKG_SOC1); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 572 | setTime(TIME_10_MIN); |
| 573 | // 10 second time limit |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 574 | addAppUsageObserver(OBS_ID1, GROUP_SOC, 10_000L); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 575 | setTime(TIME_10_MIN + 5_000L); |
| 576 | // Shouldn't call back in 6 seconds |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 577 | assertFalse(mLimitReachedLatch.await(6_000L, TimeUnit.MILLISECONDS)); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 578 | setTime(TIME_10_MIN + 10_000L); |
| 579 | // Should call back by 11 seconds (6 earlier + 5 now) |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 580 | assertTrue(mLimitReachedLatch.await(5_000L, TimeUnit.MILLISECONDS)); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 581 | // Verify that the observer was removed |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 582 | assertFalse(hasAppUsageObserver(UID, OBS_ID1)); |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 585 | /** If watched app is already running, verify the timeout callback happens at the right time */ |
Michael Wachenschwanz | c870309 | 2018-05-01 16:02:45 -0700 | [diff] [blame] | 586 | @Test |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 587 | 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 Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 608 | /** 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 |
| 615 | addAppUsageLimitObserver(OBS_ID1, GROUP_SOC, 10_000L); |
| 616 | 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 Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 626 | /** |
| 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 Wachenschwanz | c870309 | 2018-05-01 16:02:45 -0700 | [diff] [blame] | 632 | boolean receivedException = false; |
| 633 | int ANOTHER_UID = UID + 1; |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 634 | 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 Wachenschwanz | c870309 | 2018-05-01 16:02:45 -0700 | [diff] [blame] | 644 | // Readding an observer should not cause an IllegalStateException |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 645 | addAppUsageObserver(OBS_ID5, GROUP1, TIME_30_MIN); |
Michael Wachenschwanz | c870309 | 2018-05-01 16:02:45 -0700 | [diff] [blame] | 646 | // Adding an observer for a different uid shouldn't cause an IllegalStateException |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 647 | mController.addAppUsageObserver(ANOTHER_UID, OBS_ID11, GROUP1, TIME_30_MIN, null, USER_ID); |
Michael Wachenschwanz | c870309 | 2018-05-01 16:02:45 -0700 | [diff] [blame] | 648 | try { |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 649 | addAppUsageObserver(OBS_ID11, GROUP1, TIME_30_MIN); |
Michael Wachenschwanz | c870309 | 2018-05-01 16:02:45 -0700 | [diff] [blame] | 650 | } catch (IllegalStateException ise) { |
| 651 | receivedException = true; |
| 652 | } |
| 653 | assertTrue("Should have caused an IllegalStateException", receivedException); |
| 654 | } |
| 655 | |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 656 | /** |
| 657 | * Verify that App Time Limit Controller will limit the number of observerIds for usage session |
| 658 | * observers |
| 659 | */ |
Michael Wachenschwanz | c870309 | 2018-05-01 16:02:45 -0700 | [diff] [blame] | 660 | @Test |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 661 | 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 Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 687 | /** |
| 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; |
| 695 | addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN); |
| 696 | addAppUsageLimitObserver(OBS_ID2, GROUP1, TIME_30_MIN); |
| 697 | addAppUsageLimitObserver(OBS_ID3, GROUP1, TIME_30_MIN); |
| 698 | addAppUsageLimitObserver(OBS_ID4, GROUP1, TIME_30_MIN); |
| 699 | addAppUsageLimitObserver(OBS_ID5, GROUP1, TIME_30_MIN); |
| 700 | addAppUsageLimitObserver(OBS_ID6, GROUP1, TIME_30_MIN); |
| 701 | addAppUsageLimitObserver(OBS_ID7, GROUP1, TIME_30_MIN); |
| 702 | addAppUsageLimitObserver(OBS_ID8, GROUP1, TIME_30_MIN); |
| 703 | addAppUsageLimitObserver(OBS_ID9, GROUP1, TIME_30_MIN); |
| 704 | addAppUsageLimitObserver(OBS_ID10, GROUP1, TIME_30_MIN); |
| 705 | // Readding an observer should not cause an IllegalStateException |
| 706 | addAppUsageLimitObserver(OBS_ID5, GROUP1, TIME_30_MIN); |
| 707 | // Adding an observer for a different uid shouldn't cause an IllegalStateException |
| 708 | mController.addAppUsageLimitObserver( |
| 709 | ANOTHER_UID, OBS_ID11, GROUP1, TIME_30_MIN, null, USER_ID); |
| 710 | try { |
| 711 | addAppUsageLimitObserver(OBS_ID11, GROUP1, TIME_30_MIN); |
| 712 | } catch (IllegalStateException ise) { |
| 713 | receivedException = true; |
| 714 | } |
| 715 | assertTrue("Should have caused an IllegalStateException", receivedException); |
| 716 | } |
| 717 | |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 718 | /** Verify that addAppUsageObserver minimum time limit is one minute */ |
| 719 | @Test |
| 720 | public void testAppUsageObserver_MinimumTimeLimit() throws Exception { |
Michael Wachenschwanz | c870309 | 2018-05-01 16:02:45 -0700 | [diff] [blame] | 721 | boolean receivedException = false; |
| 722 | // adding an observer with a one minute time limit should not cause an exception |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 723 | addAppUsageObserver(OBS_ID1, GROUP1, MIN_TIME_LIMIT); |
Michael Wachenschwanz | c870309 | 2018-05-01 16:02:45 -0700 | [diff] [blame] | 724 | try { |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 725 | addAppUsageObserver(OBS_ID1, GROUP1, MIN_TIME_LIMIT - 1); |
Michael Wachenschwanz | c870309 | 2018-05-01 16:02:45 -0700 | [diff] [blame] | 726 | } catch (IllegalArgumentException iae) { |
| 727 | receivedException = true; |
| 728 | } |
| 729 | assertTrue("Should have caused an IllegalArgumentException", receivedException); |
| 730 | } |
| 731 | |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 732 | /** 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 Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 746 | /** 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 |
| 751 | addAppUsageLimitObserver(OBS_ID1, GROUP1, MIN_TIME_LIMIT); |
| 752 | try { |
| 753 | addAppUsageLimitObserver(OBS_ID1, GROUP1, MIN_TIME_LIMIT - 1); |
| 754 | } catch (IllegalArgumentException iae) { |
| 755 | receivedException = true; |
| 756 | } |
| 757 | assertTrue("Should have caused an IllegalArgumentException", receivedException); |
| 758 | } |
| 759 | |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 760 | /** 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 Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 783 | /** 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 Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 806 | /** 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); |
| 810 | addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN); |
| 811 | 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 Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 829 | /** 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 Wachenschwanz | 3677852 | 2018-11-12 11:06:19 -0800 | [diff] [blame] | 929 | /** 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 Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 967 | /** Verify app usage limit observer added correctly reports its total usage limit */ |
| 968 | @Test |
| 969 | public void testAppUsageLimitObserver_GetTotalUsageLimit() { |
| 970 | addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN); |
| 971 | 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); |
| 981 | addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN); |
| 982 | 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() { |
| 996 | addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN); |
| 997 | addAppUsageLimitObserver(OBS_ID2, GROUP_SOC, TIME_10_MIN); |
| 998 | 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); |
| 1009 | addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN); |
| 1010 | addAppUsageLimitObserver(OBS_ID2, GROUP_SOC, TIME_10_MIN); |
| 1011 | 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); |
| 1027 | addAppUsageLimitObserver(OBS_ID1, GROUP1, TIME_30_MIN); |
| 1028 | addAppUsageLimitObserver(OBS_ID2, GROUP_SOC, TIME_10_MIN); |
| 1029 | 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 Shah | 54f7f7f | 2019-02-07 10:21:17 -0800 | [diff] [blame] | 1038 | /** Verify that a limit of 0 is allowed for the special case of re-registering an observer. */ |
| 1039 | @Test |
| 1040 | public void testAppUsageLimitObserver_ZeroTimeLimitIsAllowed() { |
| 1041 | addAppUsageLimitObserver(OBS_ID1, GROUP1, 0); |
| 1042 | AppTimeLimitController.AppUsageLimitGroup group = getAppUsageLimitObserver(UID, OBS_ID1); |
| 1043 | assertNotNull("Observer wasn't added", group); |
| 1044 | assertEquals("Usage remaining was not 0.", 0, group.getUsageRemaining()); |
| 1045 | } |
| 1046 | |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 1047 | private void startUsage(String packageName) { |
| 1048 | mController.noteUsageStart(packageName, USER_ID); |
| 1049 | } |
| 1050 | |
Michael Wachenschwanz | 3677852 | 2018-11-12 11:06:19 -0800 | [diff] [blame] | 1051 | private void startPastUsage(String packageName, int timeAgo) { |
| 1052 | mController.noteUsageStart(packageName, USER_ID, timeAgo); |
| 1053 | } |
| 1054 | |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 1055 | private void stopUsage(String packageName) { |
| 1056 | mController.noteUsageStop(packageName, USER_ID); |
| 1057 | } |
| 1058 | |
| 1059 | private void addAppUsageObserver(int observerId, String[] packages, long timeLimit) { |
| 1060 | mController.addAppUsageObserver(UID, observerId, packages, timeLimit, null, USER_ID); |
| 1061 | } |
| 1062 | |
| 1063 | private void addUsageSessionObserver(int observerId, String[] packages, long timeLimit, |
| 1064 | long sessionThreshold) { |
| 1065 | mController.addUsageSessionObserver(UID, observerId, packages, timeLimit, sessionThreshold, |
| 1066 | null, null, USER_ID); |
| 1067 | } |
| 1068 | |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 1069 | private void addAppUsageLimitObserver(int observerId, String[] packages, long timeLimit) { |
| 1070 | mController.addAppUsageLimitObserver(UID, observerId, packages, timeLimit, null, USER_ID); |
| 1071 | } |
| 1072 | |
Michael Wachenschwanz | 0f47284 | 2018-10-23 23:02:48 -0700 | [diff] [blame] | 1073 | /** Is there still an app usage observer by that id */ |
| 1074 | private boolean hasAppUsageObserver(int uid, int observerId) { |
| 1075 | return mController.getAppUsageGroup(uid, observerId) != null; |
| 1076 | } |
| 1077 | |
| 1078 | /** Is there still an usage session observer by that id */ |
| 1079 | private boolean hasUsageSessionObserver(int uid, int observerId) { |
| 1080 | return mController.getSessionUsageGroup(uid, observerId) != null; |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 1083 | /** Is there still an app usage limit observer by that id */ |
| 1084 | private boolean hasAppUsageLimitObserver(int uid, int observerId) { |
| 1085 | return mController.getAppUsageLimitGroup(uid, observerId) != null; |
| 1086 | } |
| 1087 | |
| 1088 | private AppTimeLimitController.AppUsageLimitGroup getAppUsageLimitObserver( |
| 1089 | int uid, int observerId) { |
| 1090 | return mController.getAppUsageLimitGroup(uid, observerId); |
| 1091 | } |
| 1092 | |
| 1093 | private UsageStatsManagerInternal.AppUsageLimitData getAppUsageLimit(String packageName) { |
| 1094 | return mController.getAppUsageLimit(packageName, UserHandle.of(USER_ID)); |
| 1095 | } |
| 1096 | |
Amith Yamasani | 62ec27e9 | 2018-03-11 14:42:06 -0700 | [diff] [blame] | 1097 | private void setTime(long time) { |
| 1098 | mUptimeMillis = time; |
| 1099 | } |
| 1100 | } |