1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.server; 17 18 import static android.app.usage.UsageStatsManager.REASON_MAIN_DEFAULT; 19 import static android.app.usage.UsageStatsManager.REASON_MAIN_USAGE; 20 21 import static com.android.server.AppStateTrackerImpl.TARGET_OP; 22 23 import static org.junit.Assert.assertEquals; 24 import static org.junit.Assert.assertFalse; 25 import static org.junit.Assert.assertNotNull; 26 import static org.junit.Assert.assertTrue; 27 import static org.mockito.ArgumentMatchers.any; 28 import static org.mockito.ArgumentMatchers.anyBoolean; 29 import static org.mockito.ArgumentMatchers.anyInt; 30 import static org.mockito.ArgumentMatchers.anyString; 31 import static org.mockito.ArgumentMatchers.eq; 32 import static org.mockito.ArgumentMatchers.isNull; 33 import static org.mockito.Mockito.mock; 34 import static org.mockito.Mockito.reset; 35 import static org.mockito.Mockito.times; 36 import static org.mockito.Mockito.verify; 37 import static org.mockito.Mockito.when; 38 39 import android.app.ActivityManager; 40 import android.app.ActivityManagerInternal; 41 import android.app.AppOpsManager; 42 import android.app.AppOpsManager.OpEntry; 43 import android.app.AppOpsManager.PackageOps; 44 import android.app.IActivityManager; 45 import android.app.IUidObserver; 46 import android.app.usage.UsageStatsManager; 47 import android.content.BroadcastReceiver; 48 import android.content.Context; 49 import android.content.Intent; 50 import android.content.IntentFilter; 51 import android.net.Uri; 52 import android.os.BatteryManager; 53 import android.os.Handler; 54 import android.os.Looper; 55 import android.os.PowerManager.ServiceType; 56 import android.os.PowerManagerInternal; 57 import android.os.PowerSaveState; 58 import android.os.Process; 59 import android.os.RemoteException; 60 import android.os.UserHandle; 61 import android.platform.test.annotations.Presubmit; 62 import android.provider.Settings.Global; 63 import android.test.mock.MockContentResolver; 64 import android.util.ArraySet; 65 import android.util.Pair; 66 67 import androidx.test.filters.SmallTest; 68 import androidx.test.runner.AndroidJUnit4; 69 70 import com.android.internal.app.IAppOpsCallback; 71 import com.android.internal.app.IAppOpsService; 72 import com.android.server.AppStateTrackerImpl.Listener; 73 import com.android.server.usage.AppStandbyInternal; 74 import com.android.server.usage.AppStandbyInternal.AppIdleStateChangeListener; 75 76 import org.junit.Before; 77 import org.junit.Test; 78 import org.junit.runner.RunWith; 79 import org.mockito.ArgumentCaptor; 80 import org.mockito.Mock; 81 import org.mockito.MockitoAnnotations; 82 import org.mockito.stubbing.Answer; 83 84 import java.util.ArrayList; 85 import java.util.Arrays; 86 import java.util.Collections; 87 import java.util.HashMap; 88 import java.util.List; 89 import java.util.Random; 90 import java.util.concurrent.CountDownLatch; 91 import java.util.concurrent.TimeUnit; 92 import java.util.function.Consumer; 93 94 /** 95 * Tests for {@link AppStateTrackerImpl} 96 * 97 * Run with: atest com.android.server.AppStateTrackerTest 98 */ 99 @Presubmit 100 @SmallTest 101 @RunWith(AndroidJUnit4.class) 102 public class AppStateTrackerTest { 103 104 private class AppStateTrackerTestable extends AppStateTrackerImpl { AppStateTrackerTestable()105 AppStateTrackerTestable() { 106 super(mMockContext, Looper.getMainLooper()); 107 } 108 109 @Override injectAppOpsManager()110 AppOpsManager injectAppOpsManager() { 111 return mMockAppOpsManager; 112 } 113 114 @Override injectIAppOpsService()115 IAppOpsService injectIAppOpsService() { 116 return mMockIAppOpsService; 117 } 118 119 @Override injectIActivityManager()120 IActivityManager injectIActivityManager() { 121 return mMockIActivityManager; 122 } 123 124 @Override injectActivityManagerInternal()125 ActivityManagerInternal injectActivityManagerInternal() { 126 return mMockIActivityManagerInternal; 127 } 128 129 @Override injectPowerManagerInternal()130 PowerManagerInternal injectPowerManagerInternal() { 131 return mMockPowerManagerInternal; 132 } 133 134 @Override injectAppStandbyInternal()135 AppStandbyInternal injectAppStandbyInternal() { 136 when(mMockAppStandbyInternal.isAppIdleEnabled()).thenReturn(true); 137 return mMockAppStandbyInternal; 138 } 139 140 @Override injectGetGlobalSettingInt(String key, int def)141 int injectGetGlobalSettingInt(String key, int def) { 142 Integer val = mGlobalSettings.get(key); 143 144 return (val == null) ? def : val; 145 } 146 147 @Override isSmallBatteryDevice()148 boolean isSmallBatteryDevice() { 149 return mIsSmallBatteryDevice; 150 } 151 } 152 153 private static final int UID_1 = Process.FIRST_APPLICATION_UID + 1; 154 private static final int UID_2 = Process.FIRST_APPLICATION_UID + 2; 155 private static final int UID_3 = Process.FIRST_APPLICATION_UID + 3; 156 private static final int UID_10_1 = UserHandle.getUid(10, UID_1); 157 private static final int UID_10_2 = UserHandle.getUid(10, UID_2); 158 private static final int UID_10_3 = UserHandle.getUid(10, UID_3); 159 private static final String PACKAGE_1 = "package1"; 160 private static final String PACKAGE_2 = "package2"; 161 private static final String PACKAGE_3 = "package3"; 162 private static final String PACKAGE_SYSTEM = "android"; 163 164 private Handler mMainHandler; 165 166 @Mock 167 private Context mMockContext; 168 169 @Mock 170 private IActivityManager mMockIActivityManager; 171 172 @Mock 173 private ActivityManagerInternal mMockIActivityManagerInternal; 174 175 @Mock 176 private AppOpsManager mMockAppOpsManager; 177 178 @Mock 179 private IAppOpsService mMockIAppOpsService; 180 181 @Mock 182 private PowerManagerInternal mMockPowerManagerInternal; 183 184 @Mock 185 private AppStandbyInternal mMockAppStandbyInternal; 186 187 private MockContentResolver mMockContentResolver; 188 189 private IUidObserver mIUidObserver; 190 private IAppOpsCallback.Stub mAppOpsCallback; 191 private Consumer<PowerSaveState> mPowerSaveObserver; 192 private BroadcastReceiver mReceiver; 193 private AppIdleStateChangeListener mAppIdleStateChangeListener; 194 195 private boolean mPowerSaveMode; 196 private boolean mIsSmallBatteryDevice; 197 198 private final ArraySet<Pair<Integer, String>> mRestrictedPackages = new ArraySet(); 199 200 private final HashMap<String, Integer> mGlobalSettings = new HashMap<>(); 201 202 private Answer<List<PackageOps>> mGetPackagesForOps = 203 inv -> new ArrayList<PackageOps>(); 204 205 @Before setUp()206 public void setUp() { 207 mMainHandler = new Handler(Looper.getMainLooper()); 208 } 209 210 /** 211 * Enqueues a message and waits for it to complete. This ensures that any messages posted until 212 * now have been executed. 213 * 214 * Note that these messages may have enqueued more messages, which may or may not have executed 215 * when this method returns. 216 */ waitUntilMainHandlerDrain()217 private void waitUntilMainHandlerDrain() throws Exception { 218 final CountDownLatch l = new CountDownLatch(1); 219 mMainHandler.post(() -> { 220 l.countDown(); 221 }); 222 assertTrue(l.await(5, TimeUnit.SECONDS)); 223 } 224 getPowerSaveState()225 private PowerSaveState getPowerSaveState() { 226 return new PowerSaveState.Builder().setBatterySaverEnabled(mPowerSaveMode).build(); 227 } 228 newInstance()229 private AppStateTrackerTestable newInstance() throws Exception { 230 MockitoAnnotations.initMocks(this); 231 232 when(mMockIAppOpsService.checkOperation(eq(TARGET_OP), anyInt(), anyString())) 233 .thenAnswer(inv -> { 234 return mRestrictedPackages.indexOf( 235 Pair.create(inv.getArgument(1), inv.getArgument(2))) >= 0 ? 236 AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED; 237 }); 238 239 final AppStateTrackerTestable instance = new AppStateTrackerTestable(); 240 241 return instance; 242 } 243 callStart(AppStateTrackerTestable instance)244 private void callStart(AppStateTrackerTestable instance) throws RemoteException { 245 246 // Set up functions that start() calls. 247 when(mMockPowerManagerInternal.getLowPowerState(eq(ServiceType.FORCE_ALL_APPS_STANDBY))) 248 .thenAnswer(inv -> getPowerSaveState()); 249 when(mMockAppOpsManager.getPackagesForOps( 250 any(int[].class) 251 )).thenAnswer(mGetPackagesForOps); 252 253 mMockContentResolver = new MockContentResolver(); 254 when(mMockContext.getContentResolver()).thenReturn(mMockContentResolver); 255 256 // Call start. 257 instance.onSystemServicesReady(); 258 259 // Capture the listeners. 260 ArgumentCaptor<IUidObserver> uidObserverArgumentCaptor = 261 ArgumentCaptor.forClass(IUidObserver.class); 262 ArgumentCaptor<IAppOpsCallback.Stub> appOpsCallbackCaptor = 263 ArgumentCaptor.forClass(IAppOpsCallback.Stub.class); 264 ArgumentCaptor<Consumer<PowerSaveState>> powerSaveObserverCaptor = 265 ArgumentCaptor.forClass(Consumer.class); 266 ArgumentCaptor<BroadcastReceiver> receiverCaptor = 267 ArgumentCaptor.forClass(BroadcastReceiver.class); 268 ArgumentCaptor<AppIdleStateChangeListener> appIdleStateChangeListenerCaptor = 269 ArgumentCaptor.forClass(AppIdleStateChangeListener.class); 270 271 verify(mMockIActivityManager).registerUidObserver( 272 uidObserverArgumentCaptor.capture(), 273 eq(ActivityManager.UID_OBSERVER_GONE | ActivityManager.UID_OBSERVER_IDLE 274 | ActivityManager.UID_OBSERVER_ACTIVE 275 | ActivityManager.UID_OBSERVER_CACHED), 276 eq(ActivityManager.PROCESS_STATE_UNKNOWN), 277 isNull()); 278 verify(mMockIAppOpsService).startWatchingMode( 279 eq(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND), 280 isNull(), 281 appOpsCallbackCaptor.capture()); 282 verify(mMockPowerManagerInternal).registerLowPowerModeObserver( 283 eq(ServiceType.FORCE_ALL_APPS_STANDBY), 284 powerSaveObserverCaptor.capture()); 285 286 verify(mMockContext, times(2)).registerReceiver( 287 receiverCaptor.capture(), any(IntentFilter.class)); 288 verify(mMockAppStandbyInternal).addListener( 289 appIdleStateChangeListenerCaptor.capture()); 290 291 mIUidObserver = uidObserverArgumentCaptor.getValue(); 292 mAppOpsCallback = appOpsCallbackCaptor.getValue(); 293 mPowerSaveObserver = powerSaveObserverCaptor.getValue(); 294 mReceiver = receiverCaptor.getValue(); 295 mAppIdleStateChangeListener = appIdleStateChangeListenerCaptor.getValue(); 296 297 assertNotNull(mIUidObserver); 298 assertNotNull(mAppOpsCallback); 299 assertNotNull(mPowerSaveObserver); 300 assertNotNull(mReceiver); 301 assertNotNull(instance.mFlagsObserver); 302 } 303 setAppOps(int uid, String packageName, boolean restrict)304 private void setAppOps(int uid, String packageName, boolean restrict) throws RemoteException { 305 final Pair p = Pair.create(uid, packageName); 306 if (restrict) { 307 mRestrictedPackages.add(p); 308 } else { 309 mRestrictedPackages.remove(p); 310 } 311 if (mAppOpsCallback != null) { 312 mAppOpsCallback.opChanged(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, uid, packageName); 313 } 314 } 315 areJobsRestricted(AppStateTrackerTestable instance, int[] uids, String[] packages, boolean[] restricted, boolean exemption)316 private void areJobsRestricted(AppStateTrackerTestable instance, int[] uids, String[] packages, 317 boolean[] restricted, boolean exemption) { 318 assertTrue(uids.length == packages.length && uids.length == restricted.length); 319 for (int i = 0; i < uids.length; i++) { 320 assertEquals(restricted[i], 321 instance.areJobsRestricted(uids[i], packages[i], exemption)); 322 } 323 } 324 areAlarmsRestrictedByFAS(AppStateTrackerTestable instance, int[] uids, String[] packages, boolean[] restricted)325 private void areAlarmsRestrictedByFAS(AppStateTrackerTestable instance, int[] uids, 326 String[] packages, boolean[] restricted) { 327 assertTrue(uids.length == packages.length && uids.length == restricted.length); 328 for (int i = 0; i < uids.length; i++) { 329 assertEquals(restricted[i], instance.areAlarmsRestricted(uids[i], packages[i])); 330 } 331 } 332 areAlarmsRestrictedByBatterySaver(AppStateTrackerTestable instance, int[] uids, String[] packages, boolean[] restricted)333 private void areAlarmsRestrictedByBatterySaver(AppStateTrackerTestable instance, int[] uids, 334 String[] packages, boolean[] restricted) { 335 assertTrue(uids.length == packages.length && uids.length == restricted.length); 336 for (int i = 0; i < uids.length; i++) { 337 assertEquals(restricted[i], 338 instance.areAlarmsRestrictedByBatterySaver(uids[i], packages[i])); 339 } 340 } 341 342 @Test testAll()343 public void testAll() throws Exception { 344 final AppStateTrackerTestable instance = newInstance(); 345 callStart(instance); 346 347 assertFalse(instance.isForceAllAppsStandbyEnabled()); 348 349 when(mMockIActivityManagerInternal.isBgAutoRestrictedBucketFeatureFlagEnabled()) 350 .thenReturn(false); 351 352 areJobsRestricted(instance, 353 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 354 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 355 new boolean[] {false, false, false, false}, 356 false); 357 areJobsRestricted(instance, 358 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 359 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 360 new boolean[] {false, false, false, false}, 361 true); 362 areAlarmsRestrictedByBatterySaver(instance, 363 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 364 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 365 new boolean[] {false, false, false, false}); 366 367 // Toggle the auto restricted bucket feature flag on bg restriction, shouldn't make a 368 // difference. 369 when(mMockIActivityManagerInternal.isBgAutoRestrictedBucketFeatureFlagEnabled()) 370 .thenReturn(true); 371 372 areJobsRestricted(instance, 373 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 374 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 375 new boolean[] {false, false, false, false}, 376 false); 377 areJobsRestricted(instance, 378 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 379 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 380 new boolean[] {false, false, false, false}, 381 true); 382 areAlarmsRestrictedByBatterySaver(instance, 383 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 384 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 385 new boolean[] {false, false, false, false}); 386 387 mPowerSaveMode = true; 388 mPowerSaveObserver.accept(getPowerSaveState()); 389 390 assertTrue(instance.isForceAllAppsStandbyEnabled()); 391 392 when(mMockIActivityManagerInternal.isBgAutoRestrictedBucketFeatureFlagEnabled()) 393 .thenReturn(false); 394 395 areJobsRestricted(instance, 396 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 397 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 398 new boolean[] {true, true, true, false}, 399 false); 400 areJobsRestricted(instance, 401 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 402 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 403 new boolean[] {false, false, false, false}, 404 true); 405 areAlarmsRestrictedByBatterySaver(instance, 406 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 407 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 408 new boolean[] {true, true, true, false}); 409 410 // Toggle the auto restricted bucket feature flag on bg restriction, shouldn't make a 411 // difference. 412 when(mMockIActivityManagerInternal.isBgAutoRestrictedBucketFeatureFlagEnabled()) 413 .thenReturn(true); 414 415 areJobsRestricted(instance, 416 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 417 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 418 new boolean[] {true, true, true, false}, 419 false); 420 areJobsRestricted(instance, 421 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 422 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 423 new boolean[] {false, false, false, false}, 424 true); 425 areAlarmsRestrictedByBatterySaver(instance, 426 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 427 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 428 new boolean[] {true, true, true, false}); 429 430 when(mMockIActivityManagerInternal.isBgAutoRestrictedBucketFeatureFlagEnabled()) 431 .thenReturn(false); 432 433 // Toggle the foreground state. 434 435 assertFalse(instance.isUidActive(UID_1)); 436 assertFalse(instance.isUidActive(UID_2)); 437 assertTrue(instance.isUidActive(Process.SYSTEM_UID)); 438 439 mIUidObserver.onUidActive(UID_1); 440 waitUntilMainHandlerDrain(); 441 waitUntilMainHandlerDrain(); 442 443 areJobsRestricted(instance, 444 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 445 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 446 new boolean[] {false, true, true, false}, 447 false); 448 areAlarmsRestrictedByBatterySaver(instance, 449 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 450 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 451 new boolean[] {false, true, true, false}); 452 453 assertTrue(instance.isUidActive(UID_1)); 454 assertFalse(instance.isUidActive(UID_2)); 455 456 mIUidObserver.onUidGone(UID_1, /*disable=*/ false); 457 waitUntilMainHandlerDrain(); 458 waitUntilMainHandlerDrain(); 459 460 areJobsRestricted(instance, 461 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 462 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 463 new boolean[] {true, true, true, false}, 464 false); 465 areAlarmsRestrictedByBatterySaver(instance, 466 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 467 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 468 new boolean[] {true, true, true, false}); 469 470 assertFalse(instance.isUidActive(UID_1)); 471 assertFalse(instance.isUidActive(UID_2)); 472 473 mIUidObserver.onUidActive(UID_1); 474 waitUntilMainHandlerDrain(); 475 waitUntilMainHandlerDrain(); 476 477 areJobsRestricted(instance, 478 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 479 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 480 new boolean[] {false, true, true, false}, 481 false); 482 areAlarmsRestrictedByBatterySaver(instance, 483 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 484 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 485 new boolean[] {false, true, true, false}); 486 487 mIUidObserver.onUidIdle(UID_1, /*disable=*/ false); 488 waitUntilMainHandlerDrain(); 489 waitUntilMainHandlerDrain(); 490 491 areJobsRestricted(instance, 492 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 493 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 494 new boolean[] {true, true, true, false}, 495 false); 496 areAlarmsRestrictedByBatterySaver(instance, 497 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 498 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 499 new boolean[] {true, true, true, false}); 500 501 assertFalse(instance.isUidActive(UID_1)); 502 assertFalse(instance.isUidActive(UID_2)); 503 504 // Toggle the app ops. 505 mPowerSaveMode = false; 506 mPowerSaveObserver.accept(getPowerSaveState()); 507 508 assertTrue(instance.isRunAnyInBackgroundAppOpsAllowed(UID_1, PACKAGE_1)); 509 assertTrue(instance.isRunAnyInBackgroundAppOpsAllowed(UID_10_1, PACKAGE_1)); 510 assertTrue(instance.isRunAnyInBackgroundAppOpsAllowed(UID_2, PACKAGE_2)); 511 assertTrue(instance.isRunAnyInBackgroundAppOpsAllowed(UID_10_2, PACKAGE_2)); 512 513 areJobsRestricted(instance, 514 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 515 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 516 new boolean[] {false, false, false, false, false}, 517 false); 518 areAlarmsRestrictedByBatterySaver(instance, 519 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 520 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 521 new boolean[] {false, false, false, false, false}); 522 areAlarmsRestrictedByFAS(instance, 523 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 524 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 525 new boolean[] {false, false, false, false, false}); 526 527 setAppOps(UID_1, PACKAGE_1, true); 528 setAppOps(UID_10_2, PACKAGE_2, true); 529 assertFalse(instance.isRunAnyInBackgroundAppOpsAllowed(UID_1, PACKAGE_1)); 530 assertTrue(instance.isRunAnyInBackgroundAppOpsAllowed(UID_10_1, PACKAGE_1)); 531 assertTrue(instance.isRunAnyInBackgroundAppOpsAllowed(UID_2, PACKAGE_2)); 532 assertFalse(instance.isRunAnyInBackgroundAppOpsAllowed(UID_10_2, PACKAGE_2)); 533 534 areJobsRestricted(instance, 535 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 536 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 537 new boolean[] {true, false, false, true, false}, 538 false); 539 areJobsRestricted(instance, 540 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 541 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 542 new boolean[] {true, false, false, true, false}, 543 true); 544 545 areAlarmsRestrictedByBatterySaver(instance, 546 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 547 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 548 new boolean[] {false, false, false, false, false}); 549 areAlarmsRestrictedByFAS(instance, 550 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 551 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 552 new boolean[] {true, false, false, true, false}); 553 554 // Toggle the auto restricted bucket feature flag on bg restriction. 555 when(mMockIActivityManagerInternal.isBgAutoRestrictedBucketFeatureFlagEnabled()) 556 .thenReturn(true); 557 558 areJobsRestricted(instance, 559 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 560 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 561 new boolean[] {false, false, false, false, false}, 562 false); 563 areJobsRestricted(instance, 564 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 565 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 566 new boolean[] {false, false, false, false, false}, 567 true); 568 569 areAlarmsRestrictedByBatterySaver(instance, 570 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 571 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 572 new boolean[] {false, false, false, false, false}); 573 areAlarmsRestrictedByFAS(instance, 574 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 575 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 576 new boolean[] {false, false, false, false, false}); 577 578 // Toggle power saver, should still be the same. 579 mPowerSaveMode = true; 580 mPowerSaveObserver.accept(getPowerSaveState()); 581 when(mMockIActivityManagerInternal.isBgAutoRestrictedBucketFeatureFlagEnabled()) 582 .thenReturn(false); 583 584 areJobsRestricted(instance, 585 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 586 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 587 new boolean[] {true, true, true, true, false}, 588 false); 589 areJobsRestricted(instance, 590 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 591 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 592 new boolean[] {true, false, false, true, false}, 593 true); 594 595 areAlarmsRestrictedByBatterySaver(instance, 596 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 597 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 598 new boolean[] {true, true, true, true, false}); 599 areAlarmsRestrictedByFAS(instance, 600 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 601 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 602 new boolean[] {true, false, false, true, false}); 603 604 // Toggle the auto restricted bucket feature flag on bg restriction. 605 when(mMockIActivityManagerInternal.isBgAutoRestrictedBucketFeatureFlagEnabled()) 606 .thenReturn(true); 607 608 areJobsRestricted(instance, 609 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 610 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 611 new boolean[] {true, true, true, true, false}, 612 false); 613 areJobsRestricted(instance, 614 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 615 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 616 new boolean[] {false, false, false, false, false}, 617 true); 618 619 areAlarmsRestrictedByBatterySaver(instance, 620 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 621 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 622 new boolean[] {true, true, true, true, false}); 623 areAlarmsRestrictedByFAS(instance, 624 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 625 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 626 new boolean[] {false, false, false, false, false}); 627 628 mPowerSaveMode = false; 629 mPowerSaveObserver.accept(getPowerSaveState()); 630 631 when(mMockIActivityManagerInternal.isBgAutoRestrictedBucketFeatureFlagEnabled()) 632 .thenReturn(false); 633 634 areJobsRestricted(instance, 635 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 636 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 637 new boolean[] {true, false, false, true, false}, 638 false); 639 areJobsRestricted(instance, 640 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 641 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 642 new boolean[] {true, false, false, true, false}, 643 true); 644 645 areAlarmsRestrictedByBatterySaver(instance, 646 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 647 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 648 new boolean[] {false, false, false, false, false}); 649 areAlarmsRestrictedByFAS(instance, 650 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 651 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 652 new boolean[] {true, false, false, true, false}); 653 654 // Clear the app ops and update the exemption list. 655 setAppOps(UID_1, PACKAGE_1, false); 656 setAppOps(UID_10_2, PACKAGE_2, false); 657 658 mPowerSaveMode = true; 659 mPowerSaveObserver.accept(getPowerSaveState()); 660 661 areJobsRestricted(instance, 662 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 663 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 664 new boolean[] {true, true, true, true, false}, 665 false); 666 areJobsRestricted(instance, 667 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 668 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 669 new boolean[] {false, false, false, false, false}, 670 true); 671 672 areAlarmsRestrictedByBatterySaver(instance, 673 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 674 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 675 new boolean[] {true, true, true, true, false}); 676 areAlarmsRestrictedByFAS(instance, 677 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 678 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 679 new boolean[] {false, false, false, false, false}); 680 681 instance.setPowerSaveExemptionListAppIds(new int[] {UID_1}, new int[] {}, 682 new int[] {UID_2}); 683 684 areJobsRestricted(instance, 685 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, UID_3, UID_10_3, Process.SYSTEM_UID}, 686 new String[]{PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_3, PACKAGE_3, 687 PACKAGE_SYSTEM}, 688 new boolean[] {false, false, false, false, true, true, false}, 689 false); 690 691 areAlarmsRestrictedByBatterySaver(instance, 692 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, UID_3, UID_10_3, Process.SYSTEM_UID}, 693 new String[]{PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_3, PACKAGE_3, 694 PACKAGE_SYSTEM}, 695 new boolean[] {false, false, true, true, true, true, false}); 696 697 // Again, make sure toggling the global state doesn't change it. 698 mPowerSaveMode = false; 699 mPowerSaveObserver.accept(getPowerSaveState()); 700 701 mPowerSaveMode = true; 702 mPowerSaveObserver.accept(getPowerSaveState()); 703 704 areJobsRestricted(instance, 705 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, UID_3, UID_10_3, Process.SYSTEM_UID}, 706 new String[]{PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_3, PACKAGE_3, 707 PACKAGE_SYSTEM}, 708 new boolean[] {false, false, false, false, true, true, false}, 709 false); 710 711 areAlarmsRestrictedByBatterySaver(instance, 712 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, UID_3, UID_10_3, Process.SYSTEM_UID}, 713 new String[]{PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_3, PACKAGE_3, 714 PACKAGE_SYSTEM}, 715 new boolean[] {false, false, true, true, true, true, false}); 716 717 assertTrue(instance.isUidPowerSaveExempt(UID_1)); 718 assertTrue(instance.isUidPowerSaveExempt(UID_10_1)); 719 assertFalse(instance.isUidPowerSaveExempt(UID_2)); 720 assertFalse(instance.isUidPowerSaveExempt(UID_10_2)); 721 722 assertFalse(instance.isUidTempPowerSaveExempt(UID_1)); 723 assertFalse(instance.isUidTempPowerSaveExempt(UID_10_1)); 724 assertTrue(instance.isUidTempPowerSaveExempt(UID_2)); 725 assertTrue(instance.isUidTempPowerSaveExempt(UID_10_2)); 726 } 727 728 @Test testPowerSaveUserExemptionList()729 public void testPowerSaveUserExemptionList() throws Exception { 730 final AppStateTrackerTestable instance = newInstance(); 731 instance.setPowerSaveExemptionListAppIds(new int[] {}, new int[] {UID_1, UID_2}, 732 new int[] {}); 733 assertTrue(instance.isUidPowerSaveUserExempt(UID_1)); 734 assertTrue(instance.isUidPowerSaveUserExempt(UID_2)); 735 assertFalse(instance.isUidPowerSaveUserExempt(UID_3)); 736 } 737 738 @Test testUidStateForeground()739 public void testUidStateForeground() throws Exception { 740 final AppStateTrackerTestable instance = newInstance(); 741 callStart(instance); 742 743 mIUidObserver.onUidActive(UID_1); 744 745 waitUntilMainHandlerDrain(); 746 waitUntilMainHandlerDrain(); 747 748 assertTrue(instance.isUidActive(UID_1)); 749 assertFalse(instance.isUidActive(UID_2)); 750 assertTrue(instance.isUidActive(Process.SYSTEM_UID)); 751 752 assertTrue(instance.isUidActiveSynced(UID_1)); 753 assertFalse(instance.isUidActiveSynced(UID_2)); 754 assertTrue(instance.isUidActiveSynced(Process.SYSTEM_UID)); 755 756 mIUidObserver.onUidStateChanged(UID_2, 757 ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE, 0, 758 ActivityManager.PROCESS_CAPABILITY_NONE); 759 760 waitUntilMainHandlerDrain(); 761 waitUntilMainHandlerDrain(); 762 763 assertTrue(instance.isUidActive(UID_1)); 764 assertFalse(instance.isUidActive(UID_2)); 765 assertTrue(instance.isUidActive(Process.SYSTEM_UID)); 766 767 assertTrue(instance.isUidActiveSynced(UID_1)); 768 assertFalse(instance.isUidActiveSynced(UID_2)); 769 assertTrue(instance.isUidActiveSynced(Process.SYSTEM_UID)); 770 771 mIUidObserver.onUidStateChanged(UID_1, 772 ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE, 0, 773 ActivityManager.PROCESS_CAPABILITY_NONE); 774 775 waitUntilMainHandlerDrain(); 776 waitUntilMainHandlerDrain(); 777 778 assertTrue(instance.isUidActive(UID_1)); 779 assertFalse(instance.isUidActive(UID_2)); 780 assertTrue(instance.isUidActive(Process.SYSTEM_UID)); 781 782 mIUidObserver.onUidGone(UID_1, true); 783 784 waitUntilMainHandlerDrain(); 785 waitUntilMainHandlerDrain(); 786 787 assertFalse(instance.isUidActive(UID_1)); 788 assertFalse(instance.isUidActive(UID_2)); 789 assertTrue(instance.isUidActive(Process.SYSTEM_UID)); 790 791 mIUidObserver.onUidIdle(UID_2, true); 792 793 waitUntilMainHandlerDrain(); 794 waitUntilMainHandlerDrain(); 795 796 assertFalse(instance.isUidActive(UID_1)); 797 assertFalse(instance.isUidActive(UID_2)); 798 assertTrue(instance.isUidActive(Process.SYSTEM_UID)); 799 800 mIUidObserver.onUidStateChanged(UID_1, 801 ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND, 0, 802 ActivityManager.PROCESS_CAPABILITY_NONE); 803 804 waitUntilMainHandlerDrain(); 805 waitUntilMainHandlerDrain(); 806 807 assertFalse(instance.isUidActive(UID_1)); 808 assertFalse(instance.isUidActive(UID_2)); 809 assertTrue(instance.isUidActive(Process.SYSTEM_UID)); 810 811 mIUidObserver.onUidStateChanged(UID_1, 812 ActivityManager.PROCESS_STATE_TRANSIENT_BACKGROUND, 0, 813 ActivityManager.PROCESS_CAPABILITY_NONE); 814 815 waitUntilMainHandlerDrain(); 816 waitUntilMainHandlerDrain(); 817 818 assertFalse(instance.isUidActive(UID_1)); 819 assertFalse(instance.isUidActive(UID_2)); 820 assertTrue(instance.isUidActive(Process.SYSTEM_UID)); 821 822 assertFalse(instance.isUidActiveSynced(UID_1)); 823 assertFalse(instance.isUidActiveSynced(UID_2)); 824 assertTrue(instance.isUidActiveSynced(Process.SYSTEM_UID)); 825 826 // The result from AMI.isUidActive() only affects isUidActiveSynced(). 827 when(mMockIActivityManagerInternal.isUidActive(anyInt())).thenReturn(true); 828 829 assertFalse(instance.isUidActive(UID_1)); 830 assertFalse(instance.isUidActive(UID_2)); 831 assertTrue(instance.isUidActive(Process.SYSTEM_UID)); 832 833 assertTrue(instance.isUidActiveSynced(UID_1)); 834 assertTrue(instance.isUidActiveSynced(UID_2)); 835 assertTrue(instance.isUidActiveSynced(Process.SYSTEM_UID)); 836 } 837 838 @Test testExemptedBucket()839 public void testExemptedBucket() throws Exception { 840 final AppStateTrackerTestable instance = newInstance(); 841 callStart(instance); 842 843 assertFalse(instance.isForceAllAppsStandbyEnabled()); 844 845 areJobsRestricted(instance, 846 new int[] {UID_1, UID_2, Process.SYSTEM_UID}, 847 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_SYSTEM}, 848 new boolean[] {false, false, false}, 849 false); 850 areAlarmsRestrictedByBatterySaver(instance, 851 new int[] {UID_1, UID_2, Process.SYSTEM_UID}, 852 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_SYSTEM}, 853 new boolean[] {false, false, false}); 854 855 mPowerSaveMode = true; 856 mPowerSaveObserver.accept(getPowerSaveState()); 857 858 assertTrue(instance.isForceAllAppsStandbyEnabled()); 859 860 areJobsRestricted(instance, 861 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 862 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 863 new boolean[] {true, true, true, false}, 864 false); 865 areJobsRestricted(instance, 866 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 867 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 868 new boolean[] {false, false, false, false}, 869 true); 870 areAlarmsRestrictedByBatterySaver(instance, 871 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 872 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 873 new boolean[] {true, true, true, false}); 874 875 // Exempt package 2 on user-10. 876 mAppIdleStateChangeListener.onAppIdleStateChanged(PACKAGE_2, /*user=*/ 10, false, 877 UsageStatsManager.STANDBY_BUCKET_EXEMPTED, REASON_MAIN_DEFAULT); 878 879 areJobsRestricted(instance, 880 new int[] {UID_1, UID_2, UID_10_2}, 881 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 882 new boolean[] {true, true, false}, 883 false); 884 areJobsRestricted(instance, 885 new int[] {UID_1, UID_2, UID_10_2}, 886 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 887 new boolean[] {false, false, false}, 888 true); 889 areAlarmsRestrictedByBatterySaver(instance, 890 new int[] {UID_1, UID_2, UID_10_2}, 891 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 892 new boolean[] {true, true, false}); 893 894 // Exempt package 1 on user-0. 895 mAppIdleStateChangeListener.onAppIdleStateChanged(PACKAGE_1, /*user=*/ 0, false, 896 UsageStatsManager.STANDBY_BUCKET_EXEMPTED, REASON_MAIN_DEFAULT); 897 898 areJobsRestricted(instance, 899 new int[] {UID_1, UID_2, UID_10_2}, 900 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 901 new boolean[] {false, true, false}, 902 false); 903 areJobsRestricted(instance, 904 new int[] {UID_1, UID_2, UID_10_2}, 905 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 906 new boolean[] {false, false, false}, 907 true); 908 areAlarmsRestrictedByBatterySaver(instance, 909 new int[] {UID_1, UID_2, UID_10_2}, 910 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 911 new boolean[] {false, true, false}); 912 913 // Unexempt package 2 on user-10. 914 mAppIdleStateChangeListener.onAppIdleStateChanged(PACKAGE_2, /*user=*/ 10, false, 915 UsageStatsManager.STANDBY_BUCKET_ACTIVE, REASON_MAIN_USAGE); 916 917 areJobsRestricted(instance, 918 new int[] {UID_1, UID_2, UID_10_2}, 919 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 920 new boolean[] {false, true, true}, 921 false); 922 areJobsRestricted(instance, 923 new int[] {UID_1, UID_2, UID_10_2}, 924 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 925 new boolean[] {false, false, false}, 926 true); 927 areAlarmsRestrictedByBatterySaver(instance, 928 new int[] {UID_1, UID_2, UID_10_2}, 929 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 930 new boolean[] {false, true, true}); 931 932 // Check force-app-standby. 933 // EXEMPT doesn't exempt from force-app-standby. 934 mPowerSaveMode = false; 935 mPowerSaveObserver.accept(getPowerSaveState()); 936 937 mAppIdleStateChangeListener.onAppIdleStateChanged(PACKAGE_1, /*user=*/ 0, false, 938 UsageStatsManager.STANDBY_BUCKET_EXEMPTED, REASON_MAIN_DEFAULT); 939 mAppIdleStateChangeListener.onAppIdleStateChanged(PACKAGE_2, /*user=*/ 0, false, 940 UsageStatsManager.STANDBY_BUCKET_EXEMPTED, REASON_MAIN_DEFAULT); 941 942 // All 3 packages (u0:p1, u0:p2, u10:p2) are now in the exempted bucket. 943 setAppOps(UID_1, PACKAGE_1, true); 944 945 areJobsRestricted(instance, 946 new int[] {UID_1, UID_2, UID_10_2}, 947 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 948 new boolean[] {true, false, false}, 949 false); 950 areJobsRestricted(instance, 951 new int[] {UID_1, UID_2, UID_10_2}, 952 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 953 new boolean[] {true, false, false}, 954 true); 955 956 areAlarmsRestrictedByBatterySaver(instance, 957 new int[] {UID_1, UID_2, UID_10_2}, 958 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 959 new boolean[] {false, false, false}); 960 areAlarmsRestrictedByFAS(instance, 961 new int[] {UID_1, UID_2, UID_10_2}, 962 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 963 new boolean[] {true, false, false}); 964 } 965 966 @Test loadPersistedAppOps()967 public void loadPersistedAppOps() throws Exception { 968 final AppStateTrackerTestable instance = newInstance(); 969 970 final List<PackageOps> ops = new ArrayList<>(); 971 972 //-------------------------------------------------- 973 List<OpEntry> entries = new ArrayList<>(); 974 entries.add(new OpEntry( 975 AppOpsManager.OP_ACCESS_NOTIFICATIONS, 976 AppOpsManager.MODE_IGNORED, 977 Collections.emptyMap())); 978 entries.add(new OpEntry( 979 AppStateTrackerImpl.TARGET_OP, 980 AppOpsManager.MODE_IGNORED, 981 Collections.emptyMap())); 982 983 ops.add(new PackageOps(PACKAGE_1, UID_1, entries)); 984 985 //-------------------------------------------------- 986 entries = new ArrayList<>(); 987 entries.add(new OpEntry( 988 AppStateTrackerImpl.TARGET_OP, 989 AppOpsManager.MODE_IGNORED, 990 Collections.emptyMap())); 991 992 ops.add(new PackageOps(PACKAGE_2, UID_2, entries)); 993 994 //-------------------------------------------------- 995 entries = new ArrayList<>(); 996 entries.add(new OpEntry( 997 AppStateTrackerImpl.TARGET_OP, 998 AppOpsManager.MODE_ALLOWED, 999 Collections.emptyMap())); 1000 1001 ops.add(new PackageOps(PACKAGE_1, UID_10_1, entries)); 1002 1003 //-------------------------------------------------- 1004 entries = new ArrayList<>(); 1005 entries.add(new OpEntry( 1006 AppStateTrackerImpl.TARGET_OP, 1007 AppOpsManager.MODE_IGNORED, 1008 Collections.emptyMap())); 1009 entries.add(new OpEntry( 1010 AppOpsManager.OP_ACCESS_NOTIFICATIONS, 1011 AppOpsManager.MODE_IGNORED, 1012 Collections.emptyMap())); 1013 1014 ops.add(new PackageOps(PACKAGE_3, UID_10_3, entries)); 1015 1016 mGetPackagesForOps = inv -> { 1017 final int[] arg = (int[]) inv.getArgument(0); 1018 assertEquals(1, arg.length); 1019 assertEquals(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, arg[0]); 1020 return ops; 1021 }; 1022 1023 callStart(instance); 1024 1025 assertFalse(instance.isRunAnyInBackgroundAppOpsAllowed(UID_1, PACKAGE_1)); 1026 assertFalse(instance.isRunAnyInBackgroundAppOpsAllowed(UID_2, PACKAGE_2)); 1027 assertTrue(instance.isRunAnyInBackgroundAppOpsAllowed(UID_3, PACKAGE_3)); 1028 1029 assertTrue(instance.isRunAnyInBackgroundAppOpsAllowed(UID_10_1, PACKAGE_1)); 1030 assertTrue(instance.isRunAnyInBackgroundAppOpsAllowed(UID_10_2, PACKAGE_2)); 1031 assertFalse(instance.isRunAnyInBackgroundAppOpsAllowed(UID_10_3, PACKAGE_3)); 1032 } 1033 assertNoCallbacks(Listener l)1034 private void assertNoCallbacks(Listener l) throws Exception { 1035 waitUntilMainHandlerDrain(); 1036 verify(l, times(0)).updateAllJobs(); 1037 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1038 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1039 1040 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1041 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1042 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1043 reset(l); 1044 } 1045 1046 @Test testPowerSaveListener()1047 public void testPowerSaveListener() throws Exception { 1048 final AppStateTrackerTestable instance = newInstance(); 1049 callStart(instance); 1050 1051 Listener l = mock(Listener.class); 1052 instance.addListener(l); 1053 1054 // Power save on. 1055 mPowerSaveMode = true; 1056 mPowerSaveObserver.accept(getPowerSaveState()); 1057 1058 waitUntilMainHandlerDrain(); 1059 verify(l, times(1)).updateAllJobs(); 1060 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1061 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1062 1063 verify(l, times(1)).updateAllAlarms(); 1064 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1065 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1066 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1067 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1068 reset(l); 1069 1070 // Power save off. 1071 mPowerSaveMode = false; 1072 mPowerSaveObserver.accept(getPowerSaveState()); 1073 1074 waitUntilMainHandlerDrain(); 1075 verify(l, times(1)).updateAllJobs(); 1076 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1077 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1078 1079 verify(l, times(1)).updateAllAlarms(); 1080 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1081 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1082 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1083 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1084 reset(l); 1085 1086 // Updating to the same state should not fire listener 1087 mPowerSaveMode = false; 1088 mPowerSaveObserver.accept(getPowerSaveState()); 1089 1090 assertNoCallbacks(l); 1091 } 1092 1093 @Test testAllListeners()1094 public void testAllListeners() throws Exception { 1095 final AppStateTrackerTestable instance = newInstance(); 1096 callStart(instance); 1097 1098 Listener l = mock(Listener.class); 1099 instance.addListener(l); 1100 1101 // ------------------------------------------------------------------------- 1102 // Test with apppops. 1103 1104 setAppOps(UID_10_2, PACKAGE_2, true); 1105 1106 waitUntilMainHandlerDrain(); 1107 verify(l, times(0)).updateAllJobs(); 1108 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1109 verify(l, times(1)).updateJobsForUidPackage(eq(UID_10_2), eq(PACKAGE_2), anyBoolean()); 1110 verify(l, times(1)).updateBackgroundRestrictedForUidPackage(eq(UID_10_2), eq(PACKAGE_2), 1111 eq(true)); 1112 1113 verify(l, times(0)).updateAllAlarms(); 1114 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1115 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1116 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1117 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1118 reset(l); 1119 1120 setAppOps(UID_10_2, PACKAGE_2, false); 1121 1122 waitUntilMainHandlerDrain(); 1123 verify(l, times(0)).updateAllJobs(); 1124 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1125 verify(l, times(1)).updateJobsForUidPackage(eq(UID_10_2), eq(PACKAGE_2), anyBoolean()); 1126 verify(l, times(1)).updateBackgroundRestrictedForUidPackage(eq(UID_10_2), eq(PACKAGE_2), 1127 eq(false)); 1128 1129 verify(l, times(0)).updateAllAlarms(); 1130 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1131 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1132 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1133 verify(l, times(1)).unblockAlarmsForUidPackage(eq(UID_10_2), eq(PACKAGE_2)); 1134 reset(l); 1135 1136 setAppOps(UID_10_2, PACKAGE_2, false); 1137 1138 verify(l, times(0)).updateAllJobs(); 1139 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1140 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1141 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1142 anyBoolean()); 1143 1144 verify(l, times(0)).updateAllAlarms(); 1145 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1146 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1147 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1148 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1149 1150 // Test overlap with battery saver 1151 mPowerSaveMode = true; 1152 mPowerSaveObserver.accept(getPowerSaveState()); 1153 1154 setAppOps(UID_10_2, PACKAGE_2, true); 1155 1156 waitUntilMainHandlerDrain(); 1157 verify(l, times(1)).updateAllJobs(); 1158 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1159 verify(l, times(1)).updateJobsForUidPackage(eq(UID_10_2), eq(PACKAGE_2), anyBoolean()); 1160 verify(l, times(1)).updateBackgroundRestrictedForUidPackage(eq(UID_10_2), eq(PACKAGE_2), 1161 eq(true)); 1162 1163 verify(l, times(1)).updateAllAlarms(); 1164 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1165 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1166 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1167 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1168 reset(l); 1169 1170 // Battery saver off. 1171 mPowerSaveMode = false; 1172 mPowerSaveObserver.accept(getPowerSaveState()); 1173 1174 waitUntilMainHandlerDrain(); 1175 verify(l, times(1)).updateAllJobs(); 1176 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1177 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1178 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1179 anyBoolean()); 1180 1181 verify(l, times(1)).updateAllAlarms(); 1182 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1183 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1184 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1185 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1186 reset(l); 1187 1188 // ------------------------------------------------------------------------- 1189 // Tests with system/user/temp exemption list. 1190 1191 instance.setPowerSaveExemptionListAppIds(new int[] {UID_1, UID_2}, new int[] {}, 1192 new int[] {}); 1193 1194 waitUntilMainHandlerDrain(); 1195 verify(l, times(1)).updateAllJobs(); 1196 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1197 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1198 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1199 anyBoolean()); 1200 1201 verify(l, times(1)).updateAllAlarms(); 1202 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1203 verify(l, times(1)).unblockAllUnrestrictedAlarms(); 1204 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1205 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1206 reset(l); 1207 1208 instance.setPowerSaveExemptionListAppIds(new int[] {UID_2}, new int[] {}, new int[] {}); 1209 1210 waitUntilMainHandlerDrain(); 1211 verify(l, times(1)).updateAllJobs(); 1212 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1213 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1214 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1215 anyBoolean()); 1216 1217 verify(l, times(1)).updateAllAlarms(); 1218 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1219 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1220 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1221 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1222 reset(l); 1223 1224 // Update temp exemption list. 1225 instance.setPowerSaveExemptionListAppIds(new int[] {UID_2}, new int[] {}, 1226 new int[] {UID_1, UID_3}); 1227 1228 waitUntilMainHandlerDrain(); 1229 verify(l, times(1)).updateAllJobs(); 1230 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1231 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1232 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1233 anyBoolean()); 1234 1235 verify(l, times(0)).updateAllAlarms(); 1236 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1237 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1238 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1239 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1240 reset(l); 1241 1242 instance.setPowerSaveExemptionListAppIds(new int[] {UID_2}, new int[] {}, 1243 new int[] {UID_3}); 1244 1245 waitUntilMainHandlerDrain(); 1246 verify(l, times(1)).updateAllJobs(); 1247 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1248 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1249 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1250 anyBoolean()); 1251 1252 verify(l, times(0)).updateAllAlarms(); 1253 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1254 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1255 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1256 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1257 reset(l); 1258 1259 // Do the same thing with battery saver on. 1260 mPowerSaveMode = true; 1261 mPowerSaveObserver.accept(getPowerSaveState()); 1262 1263 waitUntilMainHandlerDrain(); 1264 verify(l, times(1)).updateAllJobs(); 1265 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1266 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1267 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1268 anyBoolean()); 1269 1270 verify(l, times(1)).updateAllAlarms(); 1271 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1272 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1273 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1274 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1275 reset(l); 1276 1277 instance.setPowerSaveExemptionListAppIds(new int[] {UID_1, UID_2}, new int[] {}, 1278 new int[] {}); 1279 1280 waitUntilMainHandlerDrain(); 1281 // Called once for updating all exemption list and once for updating temp exemption list 1282 verify(l, times(2)).updateAllJobs(); 1283 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1284 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1285 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1286 anyBoolean()); 1287 1288 verify(l, times(1)).updateAllAlarms(); 1289 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1290 verify(l, times(1)).unblockAllUnrestrictedAlarms(); 1291 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1292 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1293 reset(l); 1294 1295 instance.setPowerSaveExemptionListAppIds(new int[] {UID_2}, new int[] {}, new int[] {}); 1296 1297 waitUntilMainHandlerDrain(); 1298 verify(l, times(1)).updateAllJobs(); 1299 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1300 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1301 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1302 anyBoolean()); 1303 1304 verify(l, times(1)).updateAllAlarms(); 1305 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1306 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1307 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1308 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1309 reset(l); 1310 1311 // Update temp exemption list. 1312 instance.setPowerSaveExemptionListAppIds(new int[] {UID_2}, new int[] {}, 1313 new int[] {UID_1, UID_3}); 1314 1315 waitUntilMainHandlerDrain(); 1316 verify(l, times(1)).updateAllJobs(); 1317 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1318 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1319 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1320 anyBoolean()); 1321 1322 verify(l, times(0)).updateAllAlarms(); 1323 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1324 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1325 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1326 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1327 reset(l); 1328 1329 instance.setPowerSaveExemptionListAppIds(new int[] {UID_2}, new int[] {}, 1330 new int[] {UID_3}); 1331 1332 waitUntilMainHandlerDrain(); 1333 verify(l, times(1)).updateAllJobs(); 1334 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1335 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1336 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1337 anyBoolean()); 1338 1339 verify(l, times(0)).updateAllAlarms(); 1340 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1341 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1342 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1343 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1344 reset(l); 1345 1346 1347 // ------------------------------------------------------------------------- 1348 // Tests with proc state changes. 1349 1350 // With battery saver. 1351 // Battery saver is already on. 1352 1353 mIUidObserver.onUidActive(UID_10_1); 1354 1355 waitUntilMainHandlerDrain(); 1356 waitUntilMainHandlerDrain(); 1357 verify(l, times(0)).updateAllJobs(); 1358 verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1359 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1360 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1361 anyBoolean()); 1362 1363 verify(l, times(0)).updateAllAlarms(); 1364 verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1)); 1365 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1366 verify(l, times(1)).unblockAlarmsForUid(eq(UID_10_1)); 1367 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1368 verify(l, times(0)).removeAlarmsForUid(anyInt()); 1369 verify(l, times(0)).handleUidCachedChanged(anyInt(), anyBoolean()); 1370 reset(l); 1371 1372 mIUidObserver.onUidGone(UID_10_1, true); 1373 1374 waitUntilMainHandlerDrain(); 1375 waitUntilMainHandlerDrain(); 1376 verify(l, times(0)).updateAllJobs(); 1377 verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1378 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1379 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1380 anyBoolean()); 1381 1382 verify(l, times(0)).updateAllAlarms(); 1383 verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1)); 1384 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1385 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1386 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1387 verify(l, times(1)).removeAlarmsForUid(UID_10_1); 1388 verify(l, times(0)).handleUidCachedChanged(anyInt(), anyBoolean()); 1389 reset(l); 1390 1391 mIUidObserver.onUidActive(UID_10_1); 1392 1393 waitUntilMainHandlerDrain(); 1394 waitUntilMainHandlerDrain(); 1395 verify(l, times(0)).updateAllJobs(); 1396 verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1397 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1398 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1399 anyBoolean()); 1400 1401 verify(l, times(0)).updateAllAlarms(); 1402 verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1)); 1403 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1404 verify(l, times(1)).unblockAlarmsForUid(eq(UID_10_1)); 1405 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1406 verify(l, times(0)).removeAlarmsForUid(anyInt()); 1407 verify(l, times(0)).handleUidCachedChanged(anyInt(), anyBoolean()); 1408 reset(l); 1409 1410 mIUidObserver.onUidIdle(UID_10_1, true); 1411 1412 waitUntilMainHandlerDrain(); 1413 waitUntilMainHandlerDrain(); 1414 verify(l, times(0)).updateAllJobs(); 1415 verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1416 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1417 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1418 anyBoolean()); 1419 1420 verify(l, times(0)).updateAllAlarms(); 1421 verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1)); 1422 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1423 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1424 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1425 verify(l, times(1)).removeAlarmsForUid(UID_10_1); 1426 verify(l, times(0)).handleUidCachedChanged(anyInt(), anyBoolean()); 1427 reset(l); 1428 1429 mIUidObserver.onUidCachedChanged(UID_10_1, true); 1430 1431 waitUntilMainHandlerDrain(); 1432 waitUntilMainHandlerDrain(); 1433 verify(l, times(0)).updateAllJobs(); 1434 verify(l, times(0)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1435 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1436 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1437 anyBoolean()); 1438 1439 verify(l, times(0)).updateAllAlarms(); 1440 verify(l, times(0)).updateAlarmsForUid(eq(UID_10_1)); 1441 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1442 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1443 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1444 verify(l, times(0)).removeAlarmsForUid(anyInt()); 1445 verify(l, times(1)).handleUidCachedChanged(UID_10_1, true); 1446 reset(l); 1447 1448 mIUidObserver.onUidCachedChanged(UID_10_1, false); 1449 1450 waitUntilMainHandlerDrain(); 1451 waitUntilMainHandlerDrain(); 1452 verify(l, times(0)).updateAllJobs(); 1453 verify(l, times(0)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1454 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1455 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1456 anyBoolean()); 1457 1458 verify(l, times(0)).updateAllAlarms(); 1459 verify(l, times(0)).updateAlarmsForUid(eq(UID_10_1)); 1460 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1461 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1462 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1463 verify(l, times(0)).removeAlarmsForUid(anyInt()); 1464 verify(l, times(1)).handleUidCachedChanged(UID_10_1, false); 1465 reset(l); 1466 1467 1468 // Without battery saver. 1469 mPowerSaveMode = false; 1470 mPowerSaveObserver.accept(getPowerSaveState()); 1471 1472 waitUntilMainHandlerDrain(); 1473 verify(l, times(1)).updateAllJobs(); 1474 verify(l, times(0)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1475 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1476 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1477 anyBoolean()); 1478 1479 verify(l, times(1)).updateAllAlarms(); 1480 verify(l, times(0)).updateAlarmsForUid(eq(UID_10_1)); 1481 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1482 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1483 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1484 verify(l, times(0)).removeAlarmsForUid(anyInt()); 1485 verify(l, times(0)).handleUidCachedChanged(anyInt(), anyBoolean()); 1486 reset(l); 1487 1488 mIUidObserver.onUidActive(UID_10_1); 1489 1490 waitUntilMainHandlerDrain(); 1491 waitUntilMainHandlerDrain(); 1492 verify(l, times(0)).updateAllJobs(); 1493 verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1494 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1495 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1496 anyBoolean()); 1497 1498 verify(l, times(0)).updateAllAlarms(); 1499 verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1)); 1500 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1501 verify(l, times(1)).unblockAlarmsForUid(eq(UID_10_1)); 1502 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1503 verify(l, times(0)).removeAlarmsForUid(anyInt()); 1504 verify(l, times(0)).handleUidCachedChanged(anyInt(), anyBoolean()); 1505 reset(l); 1506 1507 mIUidObserver.onUidGone(UID_10_1, true); 1508 1509 waitUntilMainHandlerDrain(); 1510 waitUntilMainHandlerDrain(); 1511 verify(l, times(0)).updateAllJobs(); 1512 verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1513 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1514 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1515 anyBoolean()); 1516 1517 verify(l, times(0)).updateAllAlarms(); 1518 verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1)); 1519 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1520 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1521 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1522 verify(l, times(1)).removeAlarmsForUid(UID_10_1); 1523 verify(l, times(0)).handleUidCachedChanged(anyInt(), anyBoolean()); 1524 reset(l); 1525 1526 mIUidObserver.onUidActive(UID_10_1); 1527 1528 waitUntilMainHandlerDrain(); 1529 waitUntilMainHandlerDrain(); 1530 verify(l, times(0)).updateAllJobs(); 1531 verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1532 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1533 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1534 anyBoolean()); 1535 1536 verify(l, times(0)).updateAllAlarms(); 1537 verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1)); 1538 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1539 verify(l, times(1)).unblockAlarmsForUid(eq(UID_10_1)); 1540 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1541 verify(l, times(0)).removeAlarmsForUid(anyInt()); 1542 verify(l, times(0)).handleUidCachedChanged(anyInt(), anyBoolean()); 1543 reset(l); 1544 1545 mIUidObserver.onUidIdle(UID_10_1, true); 1546 1547 waitUntilMainHandlerDrain(); 1548 waitUntilMainHandlerDrain(); 1549 verify(l, times(0)).updateAllJobs(); 1550 verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1551 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1552 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1553 anyBoolean()); 1554 1555 verify(l, times(0)).updateAllAlarms(); 1556 verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1)); 1557 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1558 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1559 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1560 verify(l, times(1)).removeAlarmsForUid(UID_10_1); 1561 verify(l, times(0)).handleUidCachedChanged(anyInt(), anyBoolean()); 1562 reset(l); 1563 1564 mIUidObserver.onUidCachedChanged(UID_10_1, true); 1565 1566 waitUntilMainHandlerDrain(); 1567 waitUntilMainHandlerDrain(); 1568 verify(l, times(0)).updateAllJobs(); 1569 verify(l, times(0)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1570 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1571 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1572 anyBoolean()); 1573 1574 verify(l, times(0)).updateAllAlarms(); 1575 verify(l, times(0)).updateAlarmsForUid(eq(UID_10_1)); 1576 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1577 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1578 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1579 verify(l, times(0)).removeAlarmsForUid(anyInt()); 1580 verify(l, times(1)).handleUidCachedChanged(UID_10_1, true); 1581 reset(l); 1582 1583 mIUidObserver.onUidCachedChanged(UID_10_1, false); 1584 1585 waitUntilMainHandlerDrain(); 1586 waitUntilMainHandlerDrain(); 1587 verify(l, times(0)).updateAllJobs(); 1588 verify(l, times(0)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1589 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1590 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1591 anyBoolean()); 1592 1593 verify(l, times(0)).updateAllAlarms(); 1594 verify(l, times(0)).updateAlarmsForUid(eq(UID_10_1)); 1595 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1596 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1597 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1598 verify(l, times(0)).removeAlarmsForUid(anyInt()); 1599 verify(l, times(1)).handleUidCachedChanged(UID_10_1, false); 1600 reset(l); 1601 } 1602 1603 @Test testUserRemoved()1604 public void testUserRemoved() throws Exception { 1605 final AppStateTrackerTestable instance = newInstance(); 1606 callStart(instance); 1607 1608 mIUidObserver.onUidActive(UID_1); 1609 mIUidObserver.onUidActive(UID_10_1); 1610 1611 waitUntilMainHandlerDrain(); 1612 waitUntilMainHandlerDrain(); 1613 1614 setAppOps(UID_2, PACKAGE_2, true); 1615 setAppOps(UID_10_2, PACKAGE_2, true); 1616 1617 assertTrue(instance.isUidActive(UID_1)); 1618 assertTrue(instance.isUidActive(UID_10_1)); 1619 1620 assertFalse(instance.isRunAnyInBackgroundAppOpsAllowed(UID_2, PACKAGE_2)); 1621 assertFalse(instance.isRunAnyInBackgroundAppOpsAllowed(UID_10_2, PACKAGE_2)); 1622 1623 final Intent intent = new Intent(Intent.ACTION_USER_REMOVED); 1624 intent.putExtra(Intent.EXTRA_USER_HANDLE, 10); 1625 mReceiver.onReceive(mMockContext, intent); 1626 1627 waitUntilMainHandlerDrain(); 1628 1629 assertTrue(instance.isUidActive(UID_1)); 1630 assertFalse(instance.isUidActive(UID_10_1)); 1631 1632 assertFalse(instance.isRunAnyInBackgroundAppOpsAllowed(UID_2, PACKAGE_2)); 1633 assertTrue(instance.isRunAnyInBackgroundAppOpsAllowed(UID_10_2, PACKAGE_2)); 1634 } 1635 1636 @Test testSmallBatteryAndPluggedIn()1637 public void testSmallBatteryAndPluggedIn() throws Exception { 1638 // This is a small battery device 1639 mIsSmallBatteryDevice = true; 1640 1641 final AppStateTrackerTestable instance = newInstance(); 1642 callStart(instance); 1643 assertFalse(instance.isForceAllAppsStandbyEnabled()); 1644 1645 // Setting/experiment for all app standby for small battery is enabled 1646 mGlobalSettings.put(Global.FORCED_APP_STANDBY_FOR_SMALL_BATTERY_ENABLED, 1); 1647 instance.mFlagsObserver.onChange(true, 1648 Global.getUriFor(Global.FORCED_APP_STANDBY_FOR_SMALL_BATTERY_ENABLED)); 1649 assertTrue(instance.isForceAllAppsStandbyEnabled()); 1650 1651 // When battery is plugged in, force app standby is disabled 1652 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); 1653 intent.putExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_USB); 1654 mReceiver.onReceive(mMockContext, intent); 1655 assertFalse(instance.isForceAllAppsStandbyEnabled()); 1656 1657 // When battery stops plugged in, force app standby is enabled 1658 mReceiver.onReceive(mMockContext, new Intent(Intent.ACTION_BATTERY_CHANGED)); 1659 assertTrue(instance.isForceAllAppsStandbyEnabled()); 1660 } 1661 1662 @Test testNotSmallBatteryAndPluggedIn()1663 public void testNotSmallBatteryAndPluggedIn() throws Exception { 1664 // Not a small battery device, so plugged in status should not affect forced app standby 1665 mIsSmallBatteryDevice = false; 1666 1667 final AppStateTrackerTestable instance = newInstance(); 1668 callStart(instance); 1669 assertFalse(instance.isForceAllAppsStandbyEnabled()); 1670 1671 mPowerSaveMode = true; 1672 mPowerSaveObserver.accept(getPowerSaveState()); 1673 assertTrue(instance.isForceAllAppsStandbyEnabled()); 1674 1675 // When battery is plugged in, force app standby is unaffected 1676 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); 1677 intent.putExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_USB); 1678 mReceiver.onReceive(mMockContext, intent); 1679 assertTrue(instance.isForceAllAppsStandbyEnabled()); 1680 1681 // When battery stops plugged in, force app standby is unaffected 1682 mReceiver.onReceive(mMockContext, new Intent(Intent.ACTION_BATTERY_CHANGED)); 1683 assertTrue(instance.isForceAllAppsStandbyEnabled()); 1684 } 1685 1686 @Test testStateClearedOnPackageRemoved()1687 public void testStateClearedOnPackageRemoved() throws Exception { 1688 final AppStateTrackerTestable instance = newInstance(); 1689 callStart(instance); 1690 1691 instance.mActiveUids.put(UID_1, true); 1692 instance.mRunAnyRestrictedPackages.add(Pair.create(UID_1, PACKAGE_1)); 1693 instance.mExemptedBucketPackages.add(UserHandle.getUserId(UID_2), PACKAGE_2); 1694 1695 // Replace PACKAGE_1, nothing should change 1696 Intent packageRemoved = new Intent(Intent.ACTION_PACKAGE_REMOVED) 1697 .putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(UID_1)) 1698 .putExtra(Intent.EXTRA_UID, UID_1) 1699 .putExtra(Intent.EXTRA_REPLACING, true) 1700 .setData(Uri.fromParts(IntentFilter.SCHEME_PACKAGE, PACKAGE_1, null)); 1701 mReceiver.onReceive(mMockContext, packageRemoved); 1702 1703 assertEquals(1, instance.mActiveUids.size()); 1704 assertEquals(1, instance.mRunAnyRestrictedPackages.size()); 1705 assertEquals(1, instance.mExemptedBucketPackages.size()); 1706 1707 // Replace PACKAGE_2, nothing should change 1708 packageRemoved = new Intent(Intent.ACTION_PACKAGE_REMOVED) 1709 .putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(UID_2)) 1710 .putExtra(Intent.EXTRA_UID, UID_2) 1711 .putExtra(Intent.EXTRA_REPLACING, true) 1712 .setData(Uri.fromParts(IntentFilter.SCHEME_PACKAGE, PACKAGE_2, null)); 1713 mReceiver.onReceive(mMockContext, packageRemoved); 1714 1715 assertEquals(1, instance.mActiveUids.size()); 1716 assertEquals(1, instance.mRunAnyRestrictedPackages.size()); 1717 assertEquals(1, instance.mExemptedBucketPackages.size()); 1718 1719 // Remove PACKAGE_1 1720 packageRemoved = new Intent(Intent.ACTION_PACKAGE_REMOVED) 1721 .putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(UID_1)) 1722 .putExtra(Intent.EXTRA_UID, UID_1) 1723 .setData(Uri.fromParts(IntentFilter.SCHEME_PACKAGE, PACKAGE_1, null)); 1724 mReceiver.onReceive(mMockContext, packageRemoved); 1725 1726 assertEquals(0, instance.mActiveUids.size()); 1727 assertEquals(0, instance.mRunAnyRestrictedPackages.size()); 1728 assertEquals(1, instance.mExemptedBucketPackages.size()); 1729 1730 // Remove PACKAGE_2 1731 packageRemoved = new Intent(Intent.ACTION_PACKAGE_REMOVED) 1732 .putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(UID_2)) 1733 .putExtra(Intent.EXTRA_UID, UID_2) 1734 .setData(Uri.fromParts(IntentFilter.SCHEME_PACKAGE, PACKAGE_2, null)); 1735 mReceiver.onReceive(mMockContext, packageRemoved); 1736 1737 assertEquals(0, instance.mActiveUids.size()); 1738 assertEquals(0, instance.mRunAnyRestrictedPackages.size()); 1739 assertEquals(0, instance.mExemptedBucketPackages.size()); 1740 } 1741 array(int... appIds)1742 static int[] array(int... appIds) { 1743 Arrays.sort(appIds); 1744 return appIds; 1745 } 1746 1747 private final Random mRandom = new Random(); 1748 makeRandomArray()1749 int[] makeRandomArray() { 1750 final ArrayList<Integer> list = new ArrayList<>(); 1751 for (int i = 0; i < 5; i++) { 1752 if (mRandom.nextDouble() < 0.5) { 1753 list.add(i); 1754 } 1755 } 1756 return Arrays.stream(list.toArray(new Integer[list.size()])) 1757 .mapToInt(Integer::intValue).toArray(); 1758 } 1759 isAnyAppIdUnexemptSlow(int[] prevArray, int[] newArray)1760 static boolean isAnyAppIdUnexemptSlow(int[] prevArray, int[] newArray) { 1761 Arrays.sort(newArray); // Just in case... 1762 for (int p : prevArray) { 1763 if (Arrays.binarySearch(newArray, p) < 0) { 1764 return true; 1765 } 1766 } 1767 return false; 1768 } 1769 checkAnyAppIdUnexempt(int[] prevArray, int[] newArray, boolean expected)1770 private void checkAnyAppIdUnexempt(int[] prevArray, int[] newArray, boolean expected) { 1771 assertEquals("Input: " + Arrays.toString(prevArray) + " " + Arrays.toString(newArray), 1772 expected, AppStateTrackerImpl.isAnyAppIdUnexempt(prevArray, newArray)); 1773 1774 // Also test isAnyAppIdUnexempt. 1775 assertEquals("Input: " + Arrays.toString(prevArray) + " " + Arrays.toString(newArray), 1776 expected, isAnyAppIdUnexemptSlow(prevArray, newArray)); 1777 } 1778 1779 @Test isAnyAppIdUnexempt()1780 public void isAnyAppIdUnexempt() { 1781 checkAnyAppIdUnexempt(array(), array(), false); 1782 1783 checkAnyAppIdUnexempt(array(1), array(), true); 1784 checkAnyAppIdUnexempt(array(1), array(1), false); 1785 checkAnyAppIdUnexempt(array(1), array(0, 1), false); 1786 checkAnyAppIdUnexempt(array(1), array(0, 1, 2), false); 1787 checkAnyAppIdUnexempt(array(1), array(0, 1, 2), false); 1788 1789 checkAnyAppIdUnexempt(array(1, 2, 10), array(), true); 1790 checkAnyAppIdUnexempt(array(1, 2, 10), array(1, 2), true); 1791 checkAnyAppIdUnexempt(array(1, 2, 10), array(1, 2, 10), false); 1792 checkAnyAppIdUnexempt(array(1, 2, 10), array(2, 10), true); 1793 checkAnyAppIdUnexempt(array(1, 2, 10), array(0, 1, 2, 4, 3, 10), false); 1794 checkAnyAppIdUnexempt(array(1, 2, 10), array(0, 0, 1, 2, 10), false); 1795 1796 // Random test 1797 int trueCount = 0; 1798 final int count = 10000; 1799 for (int i = 0; i < count; i++) { 1800 final int[] array1 = makeRandomArray(); 1801 final int[] array2 = makeRandomArray(); 1802 1803 final boolean expected = isAnyAppIdUnexemptSlow(array1, array2); 1804 final boolean actual = AppStateTrackerImpl.isAnyAppIdUnexempt(array1, array2); 1805 1806 assertEquals("Input: " + Arrays.toString(array1) + " " + Arrays.toString(array2), 1807 expected, actual); 1808 if (expected) { 1809 trueCount++; 1810 } 1811 } 1812 1813 // Make sure makeRandomArray() didn't generate all same arrays by accident. 1814 assertTrue(trueCount > 0); 1815 assertTrue(trueCount < count); 1816 } 1817 } 1818