1 /* 2 * Copyright (C) 2021 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.bedstead.testapp; 18 19 import static android.app.AppOpsManager.OPSTR_START_FOREGROUND; 20 import static android.app.admin.DevicePolicyManager.OPERATION_SAFETY_REASON_DRIVING_DISTRACTION; 21 import static android.content.Context.RECEIVER_EXPORTED; 22 import static android.content.PermissionChecker.PERMISSION_GRANTED; 23 import static android.content.pm.PackageManager.FEATURE_TELEPHONY_SUBSCRIPTION; 24 import static android.os.Build.VERSION_CODES.Q; 25 import static android.os.Build.VERSION_CODES.S; 26 27 import static com.android.bedstead.nene.appops.AppOpsMode.ALLOWED; 28 import static com.android.bedstead.permissions.CommonPermissions.BLUETOOTH_CONNECT; 29 import static com.android.bedstead.permissions.CommonPermissions.READ_CONTACTS; 30 import static com.android.bedstead.testapps.TestAppsDeviceStateExtensionsKt.testApps; 31 import static com.android.eventlib.truth.EventLogsSubject.assertThat; 32 33 import static com.google.common.truth.Truth.assertThat; 34 35 import static org.testng.Assert.assertThrows; 36 37 import android.app.admin.DevicePolicyManager; 38 import android.content.Context; 39 import android.content.Intent; 40 import android.content.IntentFilter; 41 42 import com.android.bedstead.harrier.BedsteadJUnit4; 43 import com.android.bedstead.harrier.DeviceState; 44 import com.android.bedstead.harrier.annotations.RequireFeature; 45 import com.android.bedstead.harrier.annotations.RequireSdkVersion; 46 import com.android.bedstead.nene.TestApis; 47 import com.android.bedstead.nene.users.UserReference; 48 import com.android.bedstead.nene.utils.Poll; 49 import com.android.bedstead.permissions.PermissionContext; 50 import com.android.eventlib.EventLogs; 51 import com.android.eventlib.events.broadcastreceivers.BroadcastReceivedEvent; 52 53 import org.junit.ClassRule; 54 import org.junit.Ignore; 55 import org.junit.Rule; 56 import org.junit.Test; 57 import org.junit.runner.RunWith; 58 59 import java.time.Duration; 60 61 @RunWith(BedsteadJUnit4.class) 62 public final class TestAppInstanceTest { 63 64 @ClassRule 65 @Rule 66 public static final DeviceState sDeviceState = new DeviceState(); 67 68 private static final Context sContext = TestApis.context().instrumentedContext(); 69 private static final UserReference sUser = TestApis.users().instrumented(); 70 71 private static final TestApp sTestApp = testApps(sDeviceState).query() 72 .whereActivities().isNotEmpty() 73 .get(); 74 private static final TestApp sTestApp2 = testApps(sDeviceState).any(); 75 76 private static final String INTENT_ACTION = "com.android.bedstead.testapp.test_action"; 77 private static final IntentFilter INTENT_FILTER = new IntentFilter(INTENT_ACTION); 78 private static final Intent INTENT = new Intent(INTENT_ACTION); 79 private static final String INTENT_ACTION_2 = "com.android.bedstead.testapp.test_action2"; 80 private static final IntentFilter INTENT_FILTER_2 = new IntentFilter(INTENT_ACTION_2); 81 private static final Intent INTENT_2 = new Intent(INTENT_ACTION_2); 82 private static final Duration SHORT_TIMEOUT = Duration.ofSeconds(5); 83 84 @Test user_returnsUserReference()85 public void user_returnsUserReference() { 86 TestAppInstance testAppInstance = sTestApp.instance(sUser); 87 88 assertThat(testAppInstance.user()).isEqualTo(sUser); 89 } 90 91 @Test testApp_returnsTestApp()92 public void testApp_returnsTestApp() { 93 TestAppInstance testAppInstance = sTestApp.instance(sUser); 94 95 assertThat(testAppInstance.testApp()).isEqualTo(sTestApp); 96 } 97 98 @Test activities_any_returnsActivity()99 public void activities_any_returnsActivity() { 100 try (TestAppInstance testAppInstance = sTestApp.install()) { 101 assertThat(testAppInstance.activities().any()).isNotNull(); 102 } 103 } 104 105 @Test uninstall_uninstalls()106 public void uninstall_uninstalls() { 107 TestAppInstance testAppInstance = sTestApp.install(); 108 109 testAppInstance.uninstall(); 110 111 assertThat(TestApis.packages().find(sTestApp.packageName()) 112 .installedOnUser(sUser)).isFalse(); 113 } 114 115 @Test autoclose_uninstalls()116 public void autoclose_uninstalls() { 117 try (TestAppInstance testAppInstance = sTestApp.install()) { 118 // Intentionally empty 119 } 120 121 assertThat(TestApis.packages().find(sTestApp.packageName()) 122 .installedOnUser(sUser)).isFalse(); 123 } 124 125 @Test keepAlive_notInstalled_throwsException()126 public void keepAlive_notInstalled_throwsException() { 127 TestAppInstance testAppInstance = sTestApp.instance(sUser); 128 129 assertThrows(IllegalStateException.class, testAppInstance::keepAlive); 130 } 131 132 @Test 133 @Ignore("b/203758521 Need to re-add support for killing processes") killProcess_keepAlive_processIsRunningAgain()134 public void killProcess_keepAlive_processIsRunningAgain() { 135 try (TestAppInstance testAppInstance = sTestApp.install()) { 136 testAppInstance.keepAlive(); 137 138 testAppInstance.process().kill(); 139 140 Poll.forValue("running process", () -> sTestApp.pkg().runningProcess(sUser)) 141 .toNotBeNull() 142 .errorOnFail() 143 .await(); 144 } 145 } 146 147 // We cannot test that after stopKeepAlive it does not restart, as we'd have to wait an 148 // unbounded amount of time 149 150 @Test stop_processIsNotRunning()151 public void stop_processIsNotRunning() { 152 try (TestAppInstance testAppInstance = sTestApp.install()) { 153 testAppInstance.activities().any().start(); 154 155 testAppInstance.stop(); 156 157 assertThat(sTestApp.pkg().runningProcesses()).isEmpty(); 158 } 159 } 160 161 @Test stop_previouslyCalledKeepAlive_processDoesNotRestart()162 public void stop_previouslyCalledKeepAlive_processDoesNotRestart() { 163 try (TestAppInstance testAppInstance = sTestApp.install()) { 164 testAppInstance.activities().any().start(); 165 testAppInstance.keepAlive(); 166 167 testAppInstance.stop(); 168 169 assertThat(sTestApp.pkg().runningProcesses()).isEmpty(); 170 } 171 } 172 173 @Test process_isNotRunning_returnsNull()174 public void process_isNotRunning_returnsNull() { 175 try (TestAppInstance testAppInstance = sTestApp.install()) { 176 assertThat(testAppInstance.process()).isNull(); 177 } 178 } 179 180 @Test process_isRunning_isNotNull()181 public void process_isRunning_isNotNull() { 182 try (TestAppInstance testAppInstance = sTestApp.install()) { 183 testAppInstance.activities().any().start(); 184 185 Poll.forValue("TestApp process", testAppInstance::process) 186 .toNotBeNull() 187 .errorOnFail() 188 .await(); 189 } 190 } 191 192 @Test registerReceiver_receivesBroadcast()193 public void registerReceiver_receivesBroadcast() { 194 try (TestAppInstance testAppInstance = sTestApp.install()) { 195 testAppInstance.registerReceiver(INTENT_FILTER, RECEIVER_EXPORTED); 196 197 sContext.sendBroadcast(INTENT); 198 199 assertThat(testAppInstance.events().broadcastReceived() 200 .whereIntent().action().isEqualTo(INTENT_ACTION)) 201 .eventOccurred(); 202 } 203 } 204 205 @Test registerReceiver_multipleIntentFilters_receivesAllMatchingBroadcasts()206 public void registerReceiver_multipleIntentFilters_receivesAllMatchingBroadcasts() { 207 try (TestAppInstance testAppInstance = sTestApp.install()) { 208 testAppInstance.registerReceiver(INTENT_FILTER, RECEIVER_EXPORTED); 209 testAppInstance.registerReceiver(INTENT_FILTER_2, RECEIVER_EXPORTED); 210 211 sContext.sendBroadcast(INTENT); 212 sContext.sendBroadcast(INTENT_2); 213 214 assertThat(testAppInstance.events().broadcastReceived() 215 .whereIntent().action().isEqualTo(INTENT_ACTION)) 216 .eventOccurred(); 217 assertThat(testAppInstance.events().broadcastReceived() 218 .whereIntent().action().isEqualTo(INTENT_ACTION_2)) 219 .eventOccurred(); 220 } 221 } 222 223 @Test registerReceiver_processIsRunning()224 public void registerReceiver_processIsRunning() { 225 try (TestAppInstance testAppInstance = sTestApp.install()) { 226 227 testAppInstance.registerReceiver(INTENT_FILTER, RECEIVER_EXPORTED); 228 229 assertThat(sTestApp.pkg().runningProcess(sUser)).isNotNull(); 230 } 231 } 232 233 @Test stop_registeredReceiver_doesNotReceiveBroadcast()234 public void stop_registeredReceiver_doesNotReceiveBroadcast() { 235 try (TestAppInstance testAppInstance = sTestApp.install()) { 236 testAppInstance.registerReceiver(INTENT_FILTER, RECEIVER_EXPORTED); 237 238 testAppInstance.stop(); 239 sContext.sendBroadcast(INTENT); 240 241 EventLogs<BroadcastReceivedEvent> logs = 242 BroadcastReceivedEvent.queryPackage(sTestApp.packageName()) 243 .whereIntent().action().isEqualTo(INTENT_ACTION); 244 assertThat(logs.poll(SHORT_TIMEOUT)).isNull(); 245 } 246 } 247 248 @Test unregisterReceiver_registeredReceiver_doesNotReceiveBroadcast()249 public void unregisterReceiver_registeredReceiver_doesNotReceiveBroadcast() { 250 try (TestAppInstance testAppInstance = sTestApp.install()) { 251 testAppInstance.registerReceiver(INTENT_FILTER, RECEIVER_EXPORTED); 252 253 testAppInstance.unregisterReceiver(INTENT_FILTER); 254 sContext.sendBroadcast(INTENT); 255 256 EventLogs<BroadcastReceivedEvent> logs = 257 BroadcastReceivedEvent.queryPackage(sTestApp.packageName()) 258 .whereIntent().action().isEqualTo(INTENT_ACTION); 259 assertThat(logs.poll(SHORT_TIMEOUT)).isNull(); 260 } 261 } 262 263 @Test unregisterReceiver_doesNotUnregisterOtherReceivers()264 public void unregisterReceiver_doesNotUnregisterOtherReceivers() { 265 try (TestAppInstance testAppInstance = sTestApp.install()) { 266 testAppInstance.registerReceiver(INTENT_FILTER, RECEIVER_EXPORTED); 267 testAppInstance.registerReceiver(INTENT_FILTER_2, RECEIVER_EXPORTED); 268 269 testAppInstance.unregisterReceiver(INTENT_FILTER); 270 sContext.sendBroadcast(INTENT); 271 sContext.sendBroadcast(INTENT_2); 272 273 EventLogs<BroadcastReceivedEvent> logs = 274 BroadcastReceivedEvent.queryPackage(sTestApp.packageName()) 275 .whereIntent().action().isEqualTo(INTENT_ACTION); 276 EventLogs<BroadcastReceivedEvent> logs2 = 277 BroadcastReceivedEvent.queryPackage(sTestApp.packageName()) 278 .whereIntent().action().isEqualTo(INTENT_ACTION_2); 279 assertThat(logs.poll(SHORT_TIMEOUT)).isNull(); 280 assertThat(logs2.poll()).isNotNull(); 281 } 282 } 283 284 @Test keepAlive_processIsRunning()285 public void keepAlive_processIsRunning() { 286 try (TestAppInstance testAppInstance = sTestApp.install()) { 287 288 testAppInstance.keepAlive(); 289 290 assertThat(sTestApp.pkg().runningProcess(sUser)).isNotNull(); 291 } 292 } 293 294 @Test 295 @Ignore("b/203758521 need to re-add support for killing processes") registerReceiver_appIsKilled_stillReceivesBroadcast()296 public void registerReceiver_appIsKilled_stillReceivesBroadcast() { 297 try (TestAppInstance testAppInstance = sTestApp.install()) { 298 testAppInstance.registerReceiver(INTENT_FILTER, RECEIVER_EXPORTED); 299 sTestApp.pkg().runningProcess(sUser).kill(); 300 Poll.forValue("running process", () -> sTestApp.pkg().runningProcess(sUser)) 301 .toNotBeNull() 302 .errorOnFail() 303 .await(); 304 305 sContext.sendBroadcast(INTENT); 306 307 EventLogs<BroadcastReceivedEvent> logs = 308 BroadcastReceivedEvent.queryPackage(sTestApp.packageName()) 309 .whereIntent().action().isEqualTo(INTENT_ACTION); 310 assertThat(logs.poll()).isNotNull(); 311 } 312 } 313 314 @Test 315 @Ignore // TestApis are currently disabled in TestApp testApi_canCall()316 public void testApi_canCall() { 317 try (TestAppInstance testAppInstance = sTestApp.install()) { 318 // Arbitrary call which does not require specific permissions to confirm no crash 319 testAppInstance.devicePolicyManager().getFactoryResetProtectionPolicySupported(); 320 } 321 } 322 323 @Test systemApi_canCall()324 public void systemApi_canCall() { 325 try (TestAppInstance testAppInstance = sTestApp.install()) { 326 // Arbitrary call which does not require specific permissions to confirm no crash 327 testAppInstance.devicePolicyManager() 328 .createProvisioningIntentFromNfcIntent(new Intent()); 329 } 330 } 331 332 @Test 333 @RequireSdkVersion(min = S, reason = "isSafeOperation only available on S+") devicePolicyManager_returnsUsableInstance()334 public void devicePolicyManager_returnsUsableInstance() { 335 try (TestAppInstance testAppInstance = sTestApp.install()) { 336 // Arbitrary call which does not require specific permissions to confirm no crash 337 testAppInstance.devicePolicyManager() 338 .isSafeOperation(OPERATION_SAFETY_REASON_DRIVING_DISTRACTION); 339 } 340 } 341 342 @Test userManager_returnsUsableInstance()343 public void userManager_returnsUsableInstance() { 344 try (TestAppInstance testAppInstance = sTestApp.install()) { 345 // Arbitrary call which does not require specific permissions to confirm no crash 346 testAppInstance.userManager().getUserProfiles(); 347 } 348 } 349 350 @Test 351 @RequireSdkVersion(min = Q, reason = "Wifimanager API only available on Q+") wifiManager_returnsUsableInstance()352 public void wifiManager_returnsUsableInstance() { 353 try (TestAppInstance testAppInstance = sTestApp.install()) { 354 // Arbitrary call which does not require specific permissions to confirm no crash 355 testAppInstance.wifiManager().getMaxNumberOfNetworkSuggestionsPerApp(); 356 } 357 } 358 359 @Test hardwarePropertiesManager_returnsUsableInstance()360 public void hardwarePropertiesManager_returnsUsableInstance() { 361 try (TestAppInstance testAppInstance = sTestApp.install()) { 362 // Arbitrary call - there are no methods on this service which don't require permissions 363 assertThrows(SecurityException.class, () -> { 364 testAppInstance.hardwarePropertiesManager().getCpuUsages(); 365 }); 366 } 367 } 368 369 @Test packageManager_returnsUsableInstance()370 public void packageManager_returnsUsableInstance() { 371 try (TestAppInstance testAppInstance = sTestApp.install()) { 372 assertThat(testAppInstance.packageManager().hasSystemFeature("")).isFalse(); 373 } 374 } 375 376 @Test 377 @RequireFeature(FEATURE_TELEPHONY_SUBSCRIPTION) telephonyManager_returnsUsableInstance()378 public void telephonyManager_returnsUsableInstance() { 379 try (TestAppInstance testAppInstance = sTestApp.install()) { 380 testAppInstance.telephonyManager().hasCarrierPrivileges(); 381 } 382 } 383 384 @Test crossProfileApps_returnsUsableInstance()385 public void crossProfileApps_returnsUsableInstance() { 386 try (TestAppInstance testAppInstance = sTestApp.install()) { 387 assertThat(testAppInstance.crossProfileApps().getTargetUserProfiles()).isEmpty(); 388 } 389 } 390 391 @Test launcherApps_returnsUsableInstance()392 public void launcherApps_returnsUsableInstance() { 393 try (TestAppInstance testAppInstance = sTestApp.install()) { 394 assertThat(testAppInstance.launcherApps().hasShortcutHostPermission()).isFalse(); 395 } 396 } 397 398 @Test accountManager_returnsUsableInstance()399 public void accountManager_returnsUsableInstance() { 400 try (TestAppInstance testAppInstance = sTestApp.install()) { 401 // Arbitrary call which does not require specific permissions to confirm no crash 402 assertThat(testAppInstance.accountManager().getAccounts()).isNotNull(); 403 } 404 } 405 406 @Test context_returnsUsableInstance()407 public void context_returnsUsableInstance() { 408 try (TestAppInstance testAppInstance = sTestApp.install()) { 409 assertThat(testAppInstance.context().getSystemServiceName(DevicePolicyManager.class)) 410 .isEqualTo(Context.DEVICE_POLICY_SERVICE); 411 } 412 } 413 414 @Test context_getContentResolver_returnsUsableInstance()415 public void context_getContentResolver_returnsUsableInstance() { 416 try (TestAppInstance testAppInstance = sTestApp.install()) { 417 // Arbitrary call which does not require specific permissions to confirm no crash 418 assertThat(testAppInstance.context().getContentResolver().getPersistedUriPermissions()) 419 .isNotNull(); 420 } 421 } 422 423 @Test keyChain_returnsUsableInstance()424 public void keyChain_returnsUsableInstance() { 425 try (TestAppInstance testAppInstance = sTestApp.install()) { 426 assertThat(testAppInstance.keyChain().isKeyAlgorithmSupported("A")).isFalse(); 427 } 428 } 429 430 @Test bluetoothManager_returnsUsableInstance()431 public void bluetoothManager_returnsUsableInstance() { 432 try (TestAppInstance testAppInstance = sTestApp.install(); 433 PermissionContext p = 434 testAppInstance.permissions().withPermission(BLUETOOTH_CONNECT)) { 435 assertThat(testAppInstance.bluetoothManager().getConnectedDevices(/* profile= */ 7)) 436 .isEmpty(); 437 } 438 } 439 440 @Test notificationManager_returnsUsableInstance()441 public void notificationManager_returnsUsableInstance() { 442 try (TestAppInstance testAppInstance = sTestApp.install(); 443 PermissionContext p = 444 testAppInstance.permissions().withPermission(BLUETOOTH_CONNECT)) { 445 testAppInstance.notificationManager().areNotificationsEnabled(); 446 } 447 } 448 449 @Test mediaProjectionManager_returnsUsableInstance()450 public void mediaProjectionManager_returnsUsableInstance() { 451 try (TestAppInstance testAppInstance = sTestApp.install()) { 452 testAppInstance.mediaProjectionManager().createScreenCaptureIntent(); 453 } 454 } 455 456 @Test bluetoothManager_getAdapter_returnsUsableInstance()457 public void bluetoothManager_getAdapter_returnsUsableInstance() { 458 try (TestAppInstance testAppInstance = sTestApp.install()) { 459 // No exception 460 testAppInstance.bluetoothManager().getAdapter().isEnabled(); 461 } 462 } 463 464 @Test permissions_withPermission_permissionStateAppliesToCallsToTestApp()465 public void permissions_withPermission_permissionStateAppliesToCallsToTestApp() { 466 try (TestAppInstance testApp = sTestApp.install(); 467 PermissionContext p = testApp.permissions().withPermission(READ_CONTACTS)) { 468 assertThat(testApp.context().checkSelfPermission( 469 READ_CONTACTS)).isEqualTo(PERMISSION_GRANTED); 470 } 471 } 472 473 @Test permissions_withPermission_permissionStateDoesNotApplyToOtherTestApps()474 public void permissions_withPermission_permissionStateDoesNotApplyToOtherTestApps() { 475 try (TestAppInstance testApp = sTestApp.install(); 476 TestAppInstance testApp2 = sTestApp2.install(); 477 PermissionContext p = testApp.permissions().withPermission(READ_CONTACTS)) { 478 assertThat(testApp2.context().checkSelfPermission( 479 READ_CONTACTS)).isNotEqualTo(PERMISSION_GRANTED); 480 } 481 } 482 483 @Test permissions_withPermission_permissionStateDoesNotApplyToInstrumentedApp()484 public void permissions_withPermission_permissionStateDoesNotApplyToInstrumentedApp() { 485 try (TestAppInstance testApp = sTestApp.install(); 486 PermissionContext p = testApp.permissions().withPermission(READ_CONTACTS)) { 487 assertThat(sContext.checkSelfPermission( 488 READ_CONTACTS)).isNotEqualTo(PERMISSION_GRANTED); 489 } 490 } 491 492 @Test permissions_withPermission_permissionStateDoesNotApplyToInstrumentedAppAfterCall()493 public void permissions_withPermission_permissionStateDoesNotApplyToInstrumentedAppAfterCall() { 494 try (TestAppInstance testApp = sTestApp.install(); 495 PermissionContext p = testApp.permissions().withPermission(READ_CONTACTS)) { 496 testApp.context().checkSelfPermission(READ_CONTACTS); 497 498 assertThat(sContext.checkSelfPermission( 499 READ_CONTACTS)).isNotEqualTo(PERMISSION_GRANTED); 500 } 501 } 502 503 @Test permissions_withPermission_instrumentedPermissionStateDoesNotAffectTestApp()504 public void permissions_withPermission_instrumentedPermissionStateDoesNotAffectTestApp() { 505 try (TestAppInstance testApp = sTestApp.install(); 506 PermissionContext p = TestApis.permissions().withoutPermission(READ_CONTACTS); 507 PermissionContext p2 = testApp.permissions().withPermission(READ_CONTACTS)) { 508 assertThat(sContext.checkSelfPermission( 509 READ_CONTACTS)).isNotEqualTo(PERMISSION_GRANTED); 510 assertThat(testApp.context().checkSelfPermission( 511 READ_CONTACTS)).isEqualTo(PERMISSION_GRANTED); 512 } 513 } 514 515 @Test permissions_withAppOp_appOpIsAllowedForTestApp()516 public void permissions_withAppOp_appOpIsAllowedForTestApp() { 517 try (TestAppInstance testApp = sTestApp.install(); 518 PermissionContext p = testApp.permissions().withAppOp(OPSTR_START_FOREGROUND)) { 519 520 assertThat(sTestApp.pkg().appOps().get(OPSTR_START_FOREGROUND)).isEqualTo(ALLOWED); 521 } 522 } 523 524 @Test permissions_withoutAppOp_appOpIsNotAllowedForTestApp()525 public void permissions_withoutAppOp_appOpIsNotAllowedForTestApp() { 526 try (TestAppInstance testApp = sTestApp.install(); 527 PermissionContext p = testApp.permissions().withAppOp(OPSTR_START_FOREGROUND); 528 PermissionContext p2 = testApp.permissions().withoutAppOp(OPSTR_START_FOREGROUND)) { 529 assertThat(sTestApp.pkg().appOps().get(OPSTR_START_FOREGROUND)).isNotEqualTo(ALLOWED); 530 } 531 } 532 533 @Test callThrowsException_doesNotBlockUsage()534 public void callThrowsException_doesNotBlockUsage() { 535 try (TestAppInstance testApp = sTestApp.install()) { 536 for (int i = 0; i < 1000; i++) { 537 assertThrows(NullPointerException.class, 538 () -> testApp.devicePolicyManager().hasGrantedPolicy(null, 0)); 539 } 540 } 541 } 542 543 @Test telecomManager_returnsUsableInstance()544 public void telecomManager_returnsUsableInstance() { 545 try (TestAppInstance testAppInstance = sTestApp.install()) { 546 testAppInstance.telecomManager().getSystemDialerPackage(); 547 } 548 } 549 } 550