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.multiuser; 18 19 import static android.Manifest.permission.CREATE_USERS; 20 import static android.Manifest.permission.INTERACT_ACROSS_USERS; 21 import static android.os.Build.VERSION_CODES.Q; 22 import static android.os.Build.VERSION_CODES.S; 23 24 import static com.android.bedstead.multiuser.MultiUserDeviceStateExtensionsKt.additionalUser; 25 import static com.android.bedstead.multiuser.MultiUserDeviceStateExtensionsKt.cloneProfile; 26 import static com.android.bedstead.multiuser.MultiUserDeviceStateExtensionsKt.secondaryUser; 27 import static com.android.bedstead.nene.types.OptionalBoolean.FALSE; 28 29 import static com.google.common.truth.Truth.assertThat; 30 import static com.google.common.truth.Truth.assertWithMessage; 31 32 import static org.junit.Assert.assertThrows; 33 34 import android.app.ActivityManager; 35 import android.content.Context; 36 import android.os.Process; 37 import android.os.UserHandle; 38 import android.os.UserManager; 39 import android.view.Display; 40 41 import com.android.bedstead.harrier.BedsteadJUnit4; 42 import com.android.bedstead.harrier.DeviceState; 43 import com.android.bedstead.harrier.annotations.EnsurePasswordNotSet; 44 import com.android.bedstead.harrier.annotations.RequireRunOnInitialUser; 45 import com.android.bedstead.harrier.annotations.RequireSdkVersion; 46 import com.android.bedstead.multiuser.annotations.EnsureHasAdditionalUser; 47 import com.android.bedstead.multiuser.annotations.EnsureHasCloneProfile; 48 import com.android.bedstead.multiuser.annotations.EnsureHasSecondaryUser; 49 import com.android.bedstead.multiuser.annotations.RequireNotHeadlessSystemUserMode; 50 import com.android.bedstead.multiuser.annotations.RequireNotVisibleBackgroundUsers; 51 import com.android.bedstead.multiuser.annotations.RequireRunNotOnSecondaryUser; 52 import com.android.bedstead.multiuser.annotations.RequireRunOnCloneProfile; 53 import com.android.bedstead.multiuser.annotations.RequireRunOnPrimaryUser; 54 import com.android.bedstead.multiuser.annotations.RequireVisibleBackgroundUsers; 55 import com.android.bedstead.nene.TestApis; 56 import com.android.bedstead.nene.exceptions.NeneException; 57 import com.android.bedstead.nene.users.UserReference; 58 import com.android.bedstead.permissions.PermissionContext; 59 import com.android.bedstead.permissions.annotations.EnsureHasPermission; 60 import android.cts.testapisreflection.TestApisReflectionKt; 61 62 import org.junit.ClassRule; 63 import org.junit.Ignore; 64 import org.junit.Rule; 65 import org.junit.Test; 66 import org.junit.runner.RunWith; 67 68 @RunWith(BedsteadJUnit4.class) 69 public class UserReferenceTest { 70 private static final int NON_EXISTING_USER_ID = 10000; 71 private static final int USER_ID = NON_EXISTING_USER_ID; 72 public static final UserHandle USER_HANDLE = UserHandle.of(USER_ID); 73 private static final Context sContext = TestApis.context().instrumentedContext(); 74 private static final UserManager sUserManager = sContext.getSystemService(UserManager.class); 75 private static final String PASSWORD = "1234"; 76 private static final String PIN = "1234"; 77 private static final String PATTERN = "1234"; 78 private static final String DIFFERENT_PASSWORD = "2345"; 79 private static final String DIFFERENT_PIN = "2345"; 80 private static final String DIFFERENT_PATTERN = "2345"; 81 82 83 @ClassRule @Rule 84 public static final DeviceState sDeviceState = new DeviceState(); 85 86 @Test of_returnsUserReferenceWithValidId()87 public void of_returnsUserReferenceWithValidId() { 88 assertThat(UserReference.of(USER_HANDLE).id()).isEqualTo(USER_ID); 89 } 90 91 @Test id_returnsId()92 public void id_returnsId() { 93 assertThat(TestApis.users().find(USER_ID).id()).isEqualTo(USER_ID); 94 } 95 96 @Test userHandle_referencesId()97 public void userHandle_referencesId() { 98 assertThat(TestApis.users().find(USER_ID).userHandle().getIdentifier()).isEqualTo(USER_ID); 99 } 100 101 @Test exists_doesNotExist_returnsFalse()102 public void exists_doesNotExist_returnsFalse() { 103 assertThat(TestApis.users().find(NON_EXISTING_USER_ID).exists()).isFalse(); 104 } 105 106 @Test 107 @EnsureHasSecondaryUser exists_doesExist_returnsTrue()108 public void exists_doesExist_returnsTrue() { 109 assertThat(secondaryUser(sDeviceState).exists()).isTrue(); 110 } 111 112 @Test 113 @EnsureHasAdditionalUser remove_userExists_removesUser()114 public void remove_userExists_removesUser() { 115 UserReference user = additionalUser(sDeviceState); 116 117 user.remove(); 118 119 assertThat(TestApis.users().all()).doesNotContain(user); 120 } 121 122 @Test 123 @EnsureHasCloneProfile() remove_cloneProfile_removeUser()124 public void remove_cloneProfile_removeUser() { 125 cloneProfile(sDeviceState).remove(); 126 127 assertThat(cloneProfile(sDeviceState).exists()).isFalse(); 128 } 129 130 @Test start_userDoesNotExist_throwsException()131 public void start_userDoesNotExist_throwsException() { 132 assertThrows(NeneException.class, 133 () -> TestApis.users().find(NON_EXISTING_USER_ID).start()); 134 } 135 136 @Test 137 @EnsureHasAdditionalUser start_userNotStarted_userIsUnlocked()138 public void start_userNotStarted_userIsUnlocked() { 139 additionalUser(sDeviceState).stop(); 140 141 additionalUser(sDeviceState).start(); 142 143 assertThat(additionalUser(sDeviceState).isUnlocked()).isTrue(); 144 } 145 146 @Test 147 @EnsureHasSecondaryUser start_userAlreadyStarted_doesNothing()148 public void start_userAlreadyStarted_doesNothing() { 149 secondaryUser(sDeviceState).start(); 150 151 secondaryUser(sDeviceState).start(); 152 153 assertThat(secondaryUser(sDeviceState).isUnlocked()).isTrue(); 154 } 155 156 @Test 157 @EnsureHasAdditionalUser 158 @RequireNotVisibleBackgroundUsers(reason = "because otherwise it wouldn't throw") start_onDisplay_notSupported_throwsException()159 public void start_onDisplay_notSupported_throwsException() { 160 UserReference user = additionalUser(sDeviceState).stop(); 161 162 assertThrows(UnsupportedOperationException.class, 163 () -> user.startVisibleOnDisplay(Display.DEFAULT_DISPLAY)); 164 165 assertWithMessage("%s is visible", user).that(user.isVisible()).isFalse(); 166 assertWithMessage("%s is running", user).that(user.isRunning()).isFalse(); 167 } 168 169 @Test 170 @EnsureHasAdditionalUser 171 @RequireVisibleBackgroundUsers(reason = "because that's what being tested") start_onDisplay_success()172 public void start_onDisplay_success() { 173 UserReference user = additionalUser(sDeviceState).stop(); 174 int displayId = getDisplayIdForStartingVisibleBackgroundUser(); 175 176 user.startVisibleOnDisplay(displayId); 177 178 try { 179 assertWithMessage("%s is visible", user).that(user.isVisible()).isTrue(); 180 assertWithMessage("%s is running", user).that(user.isRunning()).isTrue(); 181 } finally { 182 // Need to explicitly stop it, otherwise the display won't be available on failure 183 user.stop(); 184 } 185 } 186 187 @Test 188 @EnsureHasAdditionalUser 189 @RequireVisibleBackgroundUsers(reason = "because that's what being tested") start_onDisplay_userAlreadyStarted_doesNothing()190 public void start_onDisplay_userAlreadyStarted_doesNothing() { 191 UserReference user = additionalUser(sDeviceState).stop(); 192 int displayId = getDisplayIdForStartingVisibleBackgroundUser(); 193 194 user.startVisibleOnDisplay(displayId); 195 196 try { 197 user.startVisibleOnDisplay(displayId); 198 assertWithMessage("%s is visible", user).that(user.isVisible()).isTrue(); 199 assertWithMessage("%s is running", user).that(user.isRunning()).isTrue(); 200 } finally { 201 // Need to explicitly stop it, otherwise the display won't be available on failure 202 user.stop(); 203 } 204 } 205 206 @Test stop_userDoesNotExist_doesNothing()207 public void stop_userDoesNotExist_doesNothing() { 208 TestApis.users().find(NON_EXISTING_USER_ID).stop(); 209 } 210 211 @Test 212 @EnsureHasSecondaryUser 213 @RequireRunNotOnSecondaryUser stop_userStarted_userIsStopped()214 public void stop_userStarted_userIsStopped() { 215 secondaryUser(sDeviceState).stop(); 216 217 assertThat(secondaryUser(sDeviceState).isRunning()).isFalse(); 218 } 219 220 @Test 221 @EnsureHasSecondaryUser 222 @RequireRunNotOnSecondaryUser stop_userNotStarted_doesNothing()223 public void stop_userNotStarted_doesNothing() { 224 secondaryUser(sDeviceState).stop(); 225 226 secondaryUser(sDeviceState).stop(); 227 228 assertThat(secondaryUser(sDeviceState).isRunning()).isFalse(); 229 } 230 231 @Test 232 @EnsureHasSecondaryUser 233 @RequireRunNotOnSecondaryUser 234 @RequireSdkVersion(min = Q) switchTo_userIsSwitched()235 public void switchTo_userIsSwitched() { 236 secondaryUser(sDeviceState).switchTo(); 237 238 assertThat(TestApis.users().current()).isEqualTo(secondaryUser(sDeviceState)); 239 } 240 241 @Test 242 @RequireRunOnCloneProfile(switchedToParentUser = FALSE) switchTo_profile_switchesToParent()243 public void switchTo_profile_switchesToParent() { 244 cloneProfile(sDeviceState).switchTo(); 245 246 assertThat(TestApis.users().current()).isEqualTo(cloneProfile(sDeviceState).parent()); 247 } 248 249 @Test 250 @RequireRunOnInitialUser 251 @EnsureHasCloneProfile stop_isCloneProfileOfCurrentUser_stops()252 public void stop_isCloneProfileOfCurrentUser_stops() { 253 cloneProfile(sDeviceState).stop(); 254 255 assertThat(cloneProfile(sDeviceState).isRunning()).isFalse(); 256 } 257 258 @Test serialNo_returnsSerialNo()259 public void serialNo_returnsSerialNo() { 260 UserReference user = TestApis.users().instrumented(); 261 262 assertThat(user.serialNo()) 263 .isEqualTo(sUserManager.getSerialNumberForUser(Process.myUserHandle())); 264 } 265 266 @Test serialNo_userDoesNotExist_throwsException()267 public void serialNo_userDoesNotExist_throwsException() { 268 UserReference user = TestApis.users().find(NON_EXISTING_USER_ID); 269 270 assertThrows(NeneException.class, user::serialNo); 271 } 272 273 @Test 274 @EnsureHasPermission(CREATE_USERS) 275 @RequireSdkVersion(min = S, reason = "getUserName is only available on S+") name_returnsName()276 public void name_returnsName() { 277 UserReference user = TestApis.users().instrumented(); 278 279 assertThat(user.name()).isEqualTo(sUserManager.getUserName()); 280 } 281 282 @Test name_userDoesNotExist_throwsException()283 public void name_userDoesNotExist_throwsException() { 284 UserReference user = TestApis.users().find(NON_EXISTING_USER_ID); 285 286 assertThrows(NeneException.class, user::name); 287 } 288 289 @Test 290 @EnsureHasPermission(CREATE_USERS) 291 @RequireSdkVersion(min = S, reason = "getUserType is only available on S+") type_returnsType()292 public void type_returnsType() { 293 UserReference user = TestApis.users().instrumented(); 294 295 assertThat(user.type().name()).isEqualTo(TestApisReflectionKt.getUserType(sUserManager)); 296 } 297 298 @Test type_userDoesNotExist_throwsException()299 public void type_userDoesNotExist_throwsException() { 300 UserReference user = TestApis.users().find(NON_EXISTING_USER_ID); 301 302 assertThrows(NeneException.class, user::type); 303 } 304 305 @Test 306 @RequireRunOnPrimaryUser isPrimary_isPrimary_returnsTrue()307 public void isPrimary_isPrimary_returnsTrue() { 308 UserReference user = TestApis.users().instrumented(); 309 310 assertThat(user.isPrimary()).isTrue(); 311 } 312 313 @Test 314 @EnsureHasSecondaryUser isPrimary_isNotPrimary_returnsFalse()315 public void isPrimary_isNotPrimary_returnsFalse() { 316 UserReference user = secondaryUser(sDeviceState); 317 318 assertThat(user.isPrimary()).isFalse(); 319 } 320 321 @Test isPrimary_userDoesNotExist_throwsException()322 public void isPrimary_userDoesNotExist_throwsException() { 323 UserReference user = TestApis.users().find(NON_EXISTING_USER_ID); 324 325 assertThrows(NeneException.class, user::isPrimary); 326 } 327 328 @Test 329 @EnsureHasAdditionalUser isRunning_userNotStarted_returnsFalse()330 public void isRunning_userNotStarted_returnsFalse() { 331 additionalUser(sDeviceState).stop(); 332 333 assertThat(additionalUser(sDeviceState).isRunning()).isFalse(); 334 } 335 336 @Test 337 @EnsureHasAdditionalUser isRunning_userIsRunning_returnsTrue()338 public void isRunning_userIsRunning_returnsTrue() { 339 additionalUser(sDeviceState).start(); 340 341 assertThat(additionalUser(sDeviceState).isRunning()).isTrue(); 342 } 343 344 @Test isRunning_userDoesNotExist_returnsFalse()345 public void isRunning_userDoesNotExist_returnsFalse() { 346 UserReference user = TestApis.users().find(NON_EXISTING_USER_ID); 347 348 assertThat(user.isRunning()).isFalse(); 349 } 350 351 @Test isVisible_currentUser_returnsTrue()352 public void isVisible_currentUser_returnsTrue() { 353 UserReference user = TestApis.users().current(); 354 355 assertWithMessage("%s is visible", user).that(user.isVisible()).isTrue(); 356 } 357 358 @Test 359 @EnsureHasAdditionalUser 360 @RequireVisibleBackgroundUsers(reason = "because that's what being tested") isVisible_visibleBgUser_returnsTrue()361 public void isVisible_visibleBgUser_returnsTrue() { 362 int displayId = getDisplayIdForStartingVisibleBackgroundUser(); 363 364 UserReference user = additionalUser(sDeviceState).startVisibleOnDisplay(displayId); 365 366 try { 367 assertWithMessage("%s is visible", user).that(user.isVisible()).isTrue(); 368 } finally { 369 // Need to explicitly stop it, otherwise the display won't be available on failure 370 user.stop(); 371 } 372 } 373 374 @Test 375 @EnsureHasAdditionalUser isVisible_nonStartedUser_returnsFalse()376 public void isVisible_nonStartedUser_returnsFalse() { 377 UserReference user = additionalUser(sDeviceState).stop(); 378 379 assertWithMessage("%s is visible", user).that(user.isVisible()).isFalse(); 380 } 381 382 @Test 383 @EnsureHasAdditionalUser isVisible_bgUser_returnsFalse()384 public void isVisible_bgUser_returnsFalse() { 385 UserReference user = additionalUser(sDeviceState).start(); 386 387 assertWithMessage("%s is visible", user).that(user.isVisible()).isFalse(); 388 } 389 390 @Test isVisible_userDoesNotExist_returnsFalse()391 public void isVisible_userDoesNotExist_returnsFalse() { 392 UserReference user = TestApis.users().find(NON_EXISTING_USER_ID); 393 394 assertWithMessage("%s is visible", user).that(user.isVisible()).isFalse(); 395 } 396 397 @Test isVisibleBagroundNonProfileUser_currentUser_returnsTrue()398 public void isVisibleBagroundNonProfileUser_currentUser_returnsTrue() { 399 UserReference user = TestApis.users().current(); 400 401 assertWithMessage("%s is visible bg user", user) 402 .that(user.isVisibleBagroundNonProfileUser()).isFalse(); 403 } 404 405 @Test 406 @EnsureHasAdditionalUser 407 @RequireVisibleBackgroundUsers(reason = "because that's what being tested") isVisibleBagroundNonProfileUser_visibleBgUser_returnsTrue()408 public void isVisibleBagroundNonProfileUser_visibleBgUser_returnsTrue() { 409 int displayId = getDisplayIdForStartingVisibleBackgroundUser(); 410 411 UserReference user = additionalUser(sDeviceState).startVisibleOnDisplay(displayId); 412 413 try { 414 assertWithMessage("%s is visible bg user", user) 415 .that(user.isVisibleBagroundNonProfileUser()).isTrue(); 416 } finally { 417 // Need to explicitly stop it, otherwise the display won't be available on failure 418 user.stop(); 419 } 420 } 421 422 @Test 423 @EnsureHasAdditionalUser isVisibleBagroundNonProfileUser_nonStartedUser_returnsFalse()424 public void isVisibleBagroundNonProfileUser_nonStartedUser_returnsFalse() { 425 UserReference user = additionalUser(sDeviceState).stop(); 426 427 assertWithMessage("%s is visible bg user", user) 428 .that(user.isVisibleBagroundNonProfileUser()).isFalse(); 429 } 430 431 @Test 432 @EnsureHasAdditionalUser isVisibleBagroundNonProfileUser_bgUser_returnsFalse()433 public void isVisibleBagroundNonProfileUser_bgUser_returnsFalse() { 434 UserReference user = additionalUser(sDeviceState).start(); 435 436 assertWithMessage("%s is visible bg user", user) 437 .that(user.isVisibleBagroundNonProfileUser()).isFalse(); 438 } 439 440 @Test isVisibleBagroundNonProfileUser_userDoesNotExist_returnsFalse()441 public void isVisibleBagroundNonProfileUser_userDoesNotExist_returnsFalse() { 442 UserReference user = TestApis.users().find(NON_EXISTING_USER_ID); 443 444 assertWithMessage("%s is visible bg user", user) 445 .that(user.isVisibleBagroundNonProfileUser()).isFalse(); 446 } 447 448 @Test 449 @EnsureHasCloneProfile isVisibleBagroundNonProfileUser_profileUser_returnsFalse()450 public void isVisibleBagroundNonProfileUser_profileUser_returnsFalse() { 451 UserReference user = cloneProfile(sDeviceState).start(); 452 453 assertWithMessage("%s is visible bg user", user) 454 .that(user.isVisibleBagroundNonProfileUser()).isFalse(); 455 } 456 457 @Test isForeground_currentUser_returnsTrue()458 public void isForeground_currentUser_returnsTrue() { 459 UserReference user = TestApis.users().current(); 460 461 assertWithMessage("%s is foreground", user).that(user.isForeground()).isTrue(); 462 } 463 464 @Test 465 @EnsureHasAdditionalUser isForeground_nonStartedUser_returnsFalse()466 public void isForeground_nonStartedUser_returnsFalse() { 467 UserReference user = additionalUser(sDeviceState).stop(); 468 469 assertWithMessage("%s is foreground", user).that(user.isForeground()).isFalse(); 470 } 471 472 @Test 473 @EnsureHasAdditionalUser isForeground_bgUser_returnsFalse()474 public void isForeground_bgUser_returnsFalse() { 475 UserReference user = additionalUser(sDeviceState).start(); 476 477 assertWithMessage("%s is foreground", user).that(user.isForeground()).isFalse(); 478 } 479 480 @Test 481 @EnsureHasAdditionalUser 482 @RequireVisibleBackgroundUsers(reason = "because that's what being tested") isForeground_visibleBgUser_returnsFalse()483 public void isForeground_visibleBgUser_returnsFalse() { 484 int displayId = getDisplayIdForStartingVisibleBackgroundUser(); 485 486 UserReference user = additionalUser(sDeviceState).startVisibleOnDisplay(displayId); 487 488 try { 489 assertWithMessage("%s is foreground", user).that(user.isForeground()).isFalse(); 490 } finally { 491 // Need to explicitly stop it, otherwise the display won't be available on failure 492 user.stop(); 493 } 494 } 495 496 @Test isForeground_userDoesNotExist_returnsFalse()497 public void isForeground_userDoesNotExist_returnsFalse() { 498 UserReference user = TestApis.users().find(NON_EXISTING_USER_ID); 499 500 assertWithMessage("%s is foreground", user).that(user.isForeground()).isFalse(); 501 } 502 503 @Test 504 @EnsureHasAdditionalUser isUnlocked_userIsUnlocked_returnsTrue()505 public void isUnlocked_userIsUnlocked_returnsTrue() { 506 additionalUser(sDeviceState).start(); 507 508 assertThat(additionalUser(sDeviceState).isUnlocked()).isTrue(); 509 } 510 511 // TODO(b/203542772): add tests for locked state 512 513 @Test isUnlocked_userDoesNotExist_returnsFalse()514 public void isUnlocked_userDoesNotExist_returnsFalse() { 515 UserReference user = TestApis.users().find(NON_EXISTING_USER_ID); 516 517 assertThat(user.isUnlocked()).isFalse(); 518 } 519 520 @Test 521 @EnsureHasCloneProfile parent_returnsParent()522 public void parent_returnsParent() { 523 assertThat(cloneProfile(sDeviceState).parent()).isNotNull(); 524 } 525 526 @Test 527 @RequireRunOnInitialUser parent_noParent_returnsNull()528 public void parent_noParent_returnsNull() { 529 UserReference user = TestApis.users().instrumented(); 530 531 assertThat(user.parent()).isNull(); 532 } 533 534 @Test parent_userDoesNotExist_throwsException()535 public void parent_userDoesNotExist_throwsException() { 536 UserReference user = TestApis.users().find(NON_EXISTING_USER_ID); 537 538 assertThrows(NeneException.class, user::parent); 539 } 540 541 @Test 542 @EnsureHasAdditionalUser autoclose_removesUser()543 public void autoclose_removesUser() { 544 UserReference additionalUser = additionalUser(sDeviceState); 545 546 try (UserReference user = additionalUser) { 547 // We intentionally don't do anything here, just rely on the auto-close behaviour 548 } 549 550 assertThat(TestApis.users().all()).doesNotContain(additionalUser); 551 } 552 553 @Test 554 @EnsurePasswordNotSet 555 @RequireNotHeadlessSystemUserMode(reason = "b/248248444") setScreenLockDisabled()556 public void setScreenLockDisabled() { 557 try { 558 TestApis.users().instrumented().setScreenLockDisabled(true); 559 assertThat(TestApis.users().instrumented().getScreenLockDisabled()).isTrue(); 560 561 TestApis.users().instrumented().setPassword(PASSWORD); 562 assertThat(TestApis.users().instrumented().getScreenLockDisabled()).isFalse(); 563 564 TestApis.users().instrumented().clearPassword(); 565 assertThat(TestApis.users().instrumented().getScreenLockDisabled()).isTrue(); 566 567 TestApis.users().instrumented().setScreenLockDisabled(false); 568 assertThat(TestApis.users().instrumented().getScreenLockDisabled()).isFalse(); 569 } finally { 570 TestApis.users().instrumented().setScreenLockDisabled(false); 571 if (TestApis.users().instrumented().password() != null) { 572 TestApis.users().instrumented().clearPassword(PASSWORD); 573 } 574 } 575 } 576 577 @Test 578 @EnsurePasswordNotSet 579 @RequireNotHeadlessSystemUserMode(reason = "b/248248444") setPassword_hasLockCredential()580 public void setPassword_hasLockCredential() { 581 try { 582 TestApis.users().instrumented().setPassword(PASSWORD); 583 584 assertThat(TestApis.users().instrumented().hasLockCredential()).isTrue(); 585 } finally { 586 TestApis.users().instrumented().clearPassword(PASSWORD); 587 } 588 } 589 590 @Test 591 @EnsurePasswordNotSet 592 @RequireNotHeadlessSystemUserMode(reason = "b/248248444") setPin_hasLockCredential()593 public void setPin_hasLockCredential() { 594 try { 595 TestApis.users().instrumented().setPin(PIN); 596 597 assertThat(TestApis.users().instrumented().hasLockCredential()).isTrue(); 598 } finally { 599 TestApis.users().instrumented().clearPin(PIN); 600 } 601 } 602 603 @Test 604 @EnsurePasswordNotSet 605 @RequireNotHeadlessSystemUserMode(reason = "b/248248444") setPattern_hasLockCredential()606 public void setPattern_hasLockCredential() { 607 try { 608 TestApis.users().instrumented().setPattern(PATTERN); 609 610 assertThat(TestApis.users().instrumented().hasLockCredential()).isTrue(); 611 } finally { 612 TestApis.users().instrumented().clearPattern(PATTERN); 613 } 614 } 615 616 @Test 617 @EnsurePasswordNotSet 618 @RequireNotHeadlessSystemUserMode(reason = "b/248248444") clearPassword_hasLockCredential_returnsFalse()619 public void clearPassword_hasLockCredential_returnsFalse() { 620 TestApis.users().instrumented().setPassword(PASSWORD); 621 TestApis.users().instrumented().clearPassword(PASSWORD); 622 623 assertThat(TestApis.users().instrumented().hasLockCredential()).isFalse(); 624 } 625 626 @Test 627 @EnsurePasswordNotSet 628 @RequireNotHeadlessSystemUserMode(reason = "b/248248444") clearPin_hasLockCredential_returnsFalse()629 public void clearPin_hasLockCredential_returnsFalse() { 630 TestApis.users().instrumented().setPin(PIN); 631 TestApis.users().instrumented().clearPin(PIN); 632 633 assertThat(TestApis.users().instrumented().hasLockCredential()).isFalse(); 634 } 635 636 @Test 637 @EnsurePasswordNotSet 638 @RequireNotHeadlessSystemUserMode(reason = "b/248248444") clearPattern_hasLockCredential_returnsFalse()639 public void clearPattern_hasLockCredential_returnsFalse() { 640 TestApis.users().instrumented().setPattern(PATTERN); 641 TestApis.users().instrumented().clearPattern(PATTERN); 642 643 assertThat(TestApis.users().instrumented().hasLockCredential()).isFalse(); 644 } 645 646 @Test 647 @EnsurePasswordNotSet clearPassword_doesNotHavePassword_doesNothing()648 public void clearPassword_doesNotHavePassword_doesNothing() { 649 TestApis.users().instrumented().clearPassword(PASSWORD); 650 651 assertThat(TestApis.users().instrumented().hasLockCredential()).isFalse(); 652 } 653 654 @Test 655 @EnsurePasswordNotSet clearPin_doesNotHavePin_doesNothing()656 public void clearPin_doesNotHavePin_doesNothing() { 657 TestApis.users().instrumented().clearPin(PIN); 658 659 assertThat(TestApis.users().instrumented().hasLockCredential()).isFalse(); 660 } 661 662 @Test 663 @EnsurePasswordNotSet clearPattern_doesNotHavePattern_doesNothing()664 public void clearPattern_doesNotHavePattern_doesNothing() { 665 TestApis.users().instrumented().clearPattern(PATTERN); 666 667 assertThat(TestApis.users().instrumented().hasLockCredential()).isFalse(); 668 } 669 670 @Test 671 @EnsurePasswordNotSet 672 @RequireNotHeadlessSystemUserMode(reason = "b/248248444") clearPassword_incorrectOldPassword_throwsException()673 public void clearPassword_incorrectOldPassword_throwsException() { 674 try { 675 TestApis.users().instrumented().setPassword(PASSWORD); 676 677 assertThrows(NeneException.class, 678 () -> TestApis.users().instrumented().clearPassword(DIFFERENT_PASSWORD)); 679 } finally { 680 TestApis.users().instrumented().clearPassword(PASSWORD); 681 } 682 } 683 684 @Test 685 @EnsurePasswordNotSet 686 @RequireNotHeadlessSystemUserMode(reason = "b/248248444") clearPin_incorrectOldPin_throwsException()687 public void clearPin_incorrectOldPin_throwsException() { 688 try { 689 TestApis.users().instrumented().setPin(PIN); 690 691 assertThrows(NeneException.class, 692 () -> TestApis.users().instrumented().clearPin(DIFFERENT_PIN)); 693 } finally { 694 TestApis.users().instrumented().clearPin(PIN); 695 } 696 } 697 698 @Test 699 @EnsurePasswordNotSet 700 @RequireNotHeadlessSystemUserMode(reason = "b/248248444") clearPattern_incorrectOldPattern_throwsException()701 public void clearPattern_incorrectOldPattern_throwsException() { 702 try { 703 TestApis.users().instrumented().setPattern(PATTERN); 704 705 assertThrows(NeneException.class, 706 () -> TestApis.users().instrumented().clearPattern(DIFFERENT_PATTERN)); 707 } finally { 708 TestApis.users().instrumented().clearPattern(PATTERN); 709 } 710 } 711 712 @Test 713 @EnsurePasswordNotSet 714 @RequireNotHeadlessSystemUserMode(reason = "b/248248444") 715 @Ignore // This is no longer correct - as long as nene knows the existing password it will 716 // replace - we need to change the password outside of nene to simulate the actual case setPassword_alreadyHasPassword_throwsException()717 public void setPassword_alreadyHasPassword_throwsException() { 718 try { 719 TestApis.users().instrumented().setPassword(PASSWORD); 720 721 assertThrows(NeneException.class, 722 () -> TestApis.users().instrumented().setPassword(DIFFERENT_PASSWORD)); 723 } finally { 724 TestApis.users().instrumented().clearPassword(PASSWORD); 725 } 726 } 727 728 @Test 729 @EnsurePasswordNotSet 730 @RequireNotHeadlessSystemUserMode(reason = "b/248248444") 731 @Ignore // This is no longer correct - as long as nene knows the existing password it will 732 // replace - we need to change the password outside of nene to simulate the actual case setPin_alreadyHasPin_throwsException()733 public void setPin_alreadyHasPin_throwsException() { 734 try { 735 TestApis.users().instrumented().setPin(PIN); 736 737 assertThrows(NeneException.class, 738 () -> TestApis.users().instrumented().setPin(DIFFERENT_PIN)); 739 } finally { 740 TestApis.users().instrumented().clearPin(PIN); 741 } 742 } 743 744 @Test 745 @EnsurePasswordNotSet 746 @RequireNotHeadlessSystemUserMode(reason = "b/248248444") 747 @Ignore // This is no longer correct - as long as nene knows the existing password it will 748 // replace - we need to change the password outside of nene to simulate the actual case setPattern_alreadyHasPattern_throwsException()749 public void setPattern_alreadyHasPattern_throwsException() { 750 try { 751 TestApis.users().instrumented().setPattern(PATTERN); 752 753 assertThrows(NeneException.class, 754 () -> TestApis.users().instrumented().setPattern(DIFFERENT_PATTERN)); 755 } finally { 756 TestApis.users().instrumented().clearPattern(PATTERN); 757 } 758 } 759 760 @Test 761 @EnsurePasswordNotSet 762 @RequireNotHeadlessSystemUserMode(reason = "b/248248444") setPassword_clearAsPinAndPattern_throwsException()763 public void setPassword_clearAsPinAndPattern_throwsException() { 764 try { 765 TestApis.users().instrumented().setPassword(PASSWORD); 766 767 assertThrows(NeneException.class, 768 () -> TestApis.users().instrumented().clearPin(PASSWORD)); 769 770 assertThrows(NeneException.class, 771 () -> TestApis.users().instrumented().clearPattern(PASSWORD)); 772 } finally { 773 TestApis.users().instrumented().clearPassword(PASSWORD); 774 } 775 } 776 777 @Test 778 @EnsurePasswordNotSet 779 @RequireNotHeadlessSystemUserMode(reason = "b/248248444") setPin_clearAsPasswordAndPattern_throwsException()780 public void setPin_clearAsPasswordAndPattern_throwsException() { 781 try { 782 TestApis.users().instrumented().setPin(PIN); 783 784 assertThrows(NeneException.class, 785 () -> TestApis.users().instrumented().clearPassword(PIN)); 786 787 assertThrows(NeneException.class, 788 () -> TestApis.users().instrumented().clearPattern(PIN)); 789 } finally { 790 TestApis.users().instrumented().clearPin(PIN); 791 } 792 } 793 794 @Test 795 @EnsurePasswordNotSet 796 @RequireNotHeadlessSystemUserMode(reason = "b/248248444") setPattern_clearAsPasswordAndPin_throwsException()797 public void setPattern_clearAsPasswordAndPin_throwsException() { 798 try { 799 TestApis.users().instrumented().setPattern(PATTERN); 800 801 assertThrows(NeneException.class, 802 () -> TestApis.users().instrumented().clearPassword(PATTERN)); 803 804 assertThrows(NeneException.class, 805 () -> TestApis.users().instrumented().clearPin(PATTERN)); 806 } finally { 807 TestApis.users().instrumented().clearPattern(PATTERN); 808 } 809 } 810 811 @Test 812 @EnsurePasswordNotSet 813 @RequireNotHeadlessSystemUserMode(reason = "b/248248444") setPassword_getAsPinAndPattern_throwsException()814 public void setPassword_getAsPinAndPattern_throwsException() { 815 try { 816 TestApis.users().instrumented().setPassword(PASSWORD); 817 818 assertThrows(NeneException.class, 819 () -> TestApis.users().instrumented().pin()); 820 821 assertThrows(NeneException.class, 822 () -> TestApis.users().instrumented().pattern()); 823 } finally { 824 TestApis.users().instrumented().clearPassword(PASSWORD); 825 } 826 } 827 828 @Test 829 @EnsurePasswordNotSet 830 @RequireNotHeadlessSystemUserMode(reason = "b/248248444") setPin_getAsPasswordAndPattern_throwsException()831 public void setPin_getAsPasswordAndPattern_throwsException() { 832 try { 833 TestApis.users().instrumented().setPin(PIN); 834 835 assertThrows(NeneException.class, 836 () -> TestApis.users().instrumented().password()); 837 838 assertThrows(NeneException.class, 839 () -> TestApis.users().instrumented().pattern()); 840 } finally { 841 TestApis.users().instrumented().clearPin(PIN); 842 } 843 } 844 845 @Test 846 @EnsurePasswordNotSet 847 @RequireNotHeadlessSystemUserMode(reason = "b/248248444") setPattern_getAsPasswordAndPin_throwsException()848 public void setPattern_getAsPasswordAndPin_throwsException() { 849 try { 850 TestApis.users().instrumented().setPattern(PATTERN); 851 852 assertThrows(NeneException.class, 853 () -> TestApis.users().instrumented().password()); 854 855 assertThrows(NeneException.class, 856 () -> TestApis.users().instrumented().pin()); 857 } finally { 858 TestApis.users().instrumented().clearPattern(PATTERN); 859 } 860 } 861 862 // TODO(b/271153404): create new TestApis / users() API for this getDisplayIdForStartingVisibleBackgroundUser()863 private int getDisplayIdForStartingVisibleBackgroundUser() { 864 int[] displayIds; 865 try (PermissionContext p = 866 TestApis.permissions().withPermission(INTERACT_ACROSS_USERS)) { 867 displayIds = TestApisReflectionKt.getDisplayIdsForStartingVisibleBackgroundUsers( 868 sContext.getSystemService(ActivityManager.class)); 869 } 870 assertWithMessage("available displays").that(displayIds).isNotNull(); 871 assertWithMessage("# of available displays").that(displayIds.length).isAtLeast(1); 872 return displayIds[0]; 873 } 874 875 @Test remove_instrumentedUser_throwsException()876 public void remove_instrumentedUser_throwsException() { 877 assertThrows(NeneException.class, ()->TestApis.users().instrumented().remove()); 878 } 879 } 880