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.remotedpc; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.testng.Assert.assertThrows; 22 23 import android.content.ComponentName; 24 import android.os.UserHandle; 25 26 import com.android.bedstead.harrier.BedsteadJUnit4; 27 import com.android.bedstead.harrier.DeviceState; 28 import com.android.bedstead.harrier.annotations.AfterClass; 29 import com.android.bedstead.harrier.annotations.BeforeClass; 30 import com.android.bedstead.harrier.annotations.enterprise.EnsureHasNoDeviceOwner; 31 import com.android.bedstead.nene.TestApis; 32 import com.android.bedstead.nene.devicepolicy.DeviceOwner; 33 import com.android.bedstead.nene.devicepolicy.ProfileOwner; 34 import com.android.bedstead.nene.exceptions.NeneException; 35 import com.android.bedstead.nene.users.UserReference; 36 import com.android.bedstead.nene.users.UserType; 37 import com.android.bedstead.testapp.TestApp; 38 import com.android.bedstead.testapp.TestAppProvider; 39 import com.android.eventlib.premade.EventLibDeviceAdminReceiver; 40 41 import org.junit.ClassRule; 42 import org.junit.Rule; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 46 @RunWith(BedsteadJUnit4.class) 47 public class RemoteDpcTest { 48 // TODO(scottjonathan): Add annotations to ensure that there is no DO/PO on appropriate methods 49 // TODO(180478924): We shouldn't need to hardcode this 50 private static final String DEVICE_ADMIN_TESTAPP_PACKAGE_NAME = "android.DeviceAdminTestApp"; 51 private static final ComponentName NON_REMOTE_DPC_COMPONENT = 52 new ComponentName(DEVICE_ADMIN_TESTAPP_PACKAGE_NAME, 53 EventLibDeviceAdminReceiver.class.getName()); 54 55 @ClassRule @Rule 56 public static DeviceState sDeviceState = new DeviceState(); 57 58 private static final TestApis sTestApis = new TestApis(); 59 private static TestApp sNonRemoteDpcTestApp; 60 private static final UserReference sUser = sTestApis.users().instrumented(); 61 private static final UserReference NON_EXISTING_USER_REFERENCE = 62 sTestApis.users().find(99999); 63 private static final UserHandle NON_EXISTING_USER_HANDLE = 64 NON_EXISTING_USER_REFERENCE.userHandle(); 65 66 @BeforeClass setupClass()67 public static void setupClass() { 68 sNonRemoteDpcTestApp = new TestAppProvider().query() 69 // TODO(scottjonathan): Query by feature not package name 70 .wherePackageName().isEqualTo(DEVICE_ADMIN_TESTAPP_PACKAGE_NAME) 71 .get(); 72 73 sNonRemoteDpcTestApp.install(sUser); 74 } 75 76 @AfterClass teardownClass()77 public static void teardownClass() { 78 sNonRemoteDpcTestApp.uninstall(sUser); 79 } 80 81 @Test 82 @EnsureHasNoDeviceOwner deviceOwner_noDeviceOwner_returnsNull()83 public void deviceOwner_noDeviceOwner_returnsNull() { 84 assertThat(RemoteDpc.deviceOwner()).isNull(); 85 } 86 87 @Test deviceOwner_nonRemoteDpcDeviceOwner_returnsNull()88 public void deviceOwner_nonRemoteDpcDeviceOwner_returnsNull() { 89 DeviceOwner deviceOwner = 90 sTestApis.devicePolicy().setDeviceOwner(sUser, NON_REMOTE_DPC_COMPONENT); 91 try { 92 assertThat(RemoteDpc.deviceOwner()).isNull(); 93 } finally { 94 deviceOwner.remove(); 95 } 96 } 97 98 @Test deviceOwner_remoteDpcDeviceOwner_returnsInstance()99 public void deviceOwner_remoteDpcDeviceOwner_returnsInstance() { 100 RemoteDpc remoteDPC = RemoteDpc.setAsDeviceOwner(sUser); 101 102 try { 103 assertThat(RemoteDpc.deviceOwner()).isNotNull(); 104 } finally { 105 remoteDPC.devicePolicyController().remove(); 106 } 107 } 108 109 @Test profileOwner_noProfileOwner_returnsNull()110 public void profileOwner_noProfileOwner_returnsNull() { 111 assertThat(RemoteDpc.profileOwner()).isNull(); 112 } 113 114 @Test profileOwner_nonRemoteDpcProfileOwner_returnsNull()115 public void profileOwner_nonRemoteDpcProfileOwner_returnsNull() { 116 ProfileOwner profileOwner = 117 sTestApis.devicePolicy().setProfileOwner(sUser, NON_REMOTE_DPC_COMPONENT); 118 try { 119 assertThat(RemoteDpc.profileOwner()).isNull(); 120 } finally { 121 profileOwner.remove(); 122 } 123 } 124 125 @Test profileOwner_remoteDpcProfileOwner_returnsInstance()126 public void profileOwner_remoteDpcProfileOwner_returnsInstance() { 127 RemoteDpc remoteDPC = RemoteDpc.setAsProfileOwner(sUser); 128 try { 129 assertThat(RemoteDpc.profileOwner()).isNotNull(); 130 } finally { 131 remoteDPC.devicePolicyController().remove(); 132 } 133 } 134 135 @Test profileOwner_userHandle_null_throwsException()136 public void profileOwner_userHandle_null_throwsException() { 137 assertThrows(NullPointerException.class, () -> RemoteDpc.profileOwner((UserHandle) null)); 138 } 139 140 @Test 141 @EnsureHasNoDeviceOwner profileOwner_userHandle_noProfileOwner_returnsNull()142 public void profileOwner_userHandle_noProfileOwner_returnsNull() { 143 UserReference profile = sTestApis.users().createUser() 144 .parent(sUser) 145 .type(sTestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 146 .createAndStart(); 147 try { 148 assertThat(RemoteDpc.profileOwner(profile.userHandle())).isNull(); 149 } finally { 150 profile.remove(); 151 } 152 } 153 154 @Test 155 @EnsureHasNoDeviceOwner profileOwner_userHandle_nonRemoteDpcProfileOwner_returnsNull()156 public void profileOwner_userHandle_nonRemoteDpcProfileOwner_returnsNull() { 157 UserReference profile = sTestApis.users().createUser() 158 .parent(sUser) 159 .type(sTestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 160 .createAndStart(); 161 sNonRemoteDpcTestApp.install(profile); 162 try { 163 sTestApis.devicePolicy().setProfileOwner(profile, NON_REMOTE_DPC_COMPONENT); 164 165 assertThat(RemoteDpc.profileOwner(profile.userHandle())).isNull(); 166 } finally { 167 profile.remove(); 168 } 169 } 170 171 @Test 172 @EnsureHasNoDeviceOwner profileOwner_userHandle_remoteDpcProfileOwner_returnsInstance()173 public void profileOwner_userHandle_remoteDpcProfileOwner_returnsInstance() { 174 UserReference profile = sTestApis.users().createUser() 175 .parent(sUser) 176 .type(sTestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 177 .createAndStart(); 178 RemoteDpc.setAsProfileOwner(profile); 179 try { 180 assertThat(RemoteDpc.profileOwner(profile.userHandle())).isNotNull(); 181 } finally { 182 profile.remove(); 183 } 184 } 185 186 @Test profileOwner_userReference_null_throwsException()187 public void profileOwner_userReference_null_throwsException() { 188 assertThrows(NullPointerException.class, 189 () -> RemoteDpc.profileOwner((UserReference) null)); 190 } 191 192 @Test 193 @EnsureHasNoDeviceOwner profileOwner_userReference_noProfileOwner_returnsNull()194 public void profileOwner_userReference_noProfileOwner_returnsNull() { 195 UserReference profile = sTestApis.users().createUser() 196 .parent(sUser) 197 .type(sTestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 198 .createAndStart(); 199 try { 200 assertThat(RemoteDpc.profileOwner(profile)).isNull(); 201 } finally { 202 profile.remove(); 203 } 204 } 205 206 @Test 207 @EnsureHasNoDeviceOwner profileOwner_userReference_nonRemoteDpcProfileOwner_returnsNull()208 public void profileOwner_userReference_nonRemoteDpcProfileOwner_returnsNull() { 209 UserReference profile = sTestApis.users().createUser() 210 .parent(sUser) 211 .type(sTestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 212 .createAndStart(); 213 sNonRemoteDpcTestApp.install(profile); 214 try { 215 sTestApis.devicePolicy().setProfileOwner(profile, NON_REMOTE_DPC_COMPONENT); 216 217 assertThat(RemoteDpc.profileOwner(profile)).isNull(); 218 } finally { 219 profile.remove(); 220 } 221 } 222 223 @Test 224 @EnsureHasNoDeviceOwner profileOwner_userReference_remoteDpcProfileOwner_returnsInstance()225 public void profileOwner_userReference_remoteDpcProfileOwner_returnsInstance() { 226 UserReference profile = sTestApis.users().createUser() 227 .parent(sUser) 228 .type(sTestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 229 .createAndStart(); 230 RemoteDpc.setAsProfileOwner(profile); 231 try { 232 assertThat(RemoteDpc.profileOwner(profile)).isNotNull(); 233 } finally { 234 profile.remove(); 235 } 236 } 237 238 @Test any_noDeviceOwner_noProfileOwner_returnsNull()239 public void any_noDeviceOwner_noProfileOwner_returnsNull() { 240 assertThat(RemoteDpc.any()).isNull(); 241 } 242 243 @Test any_noDeviceOwner_nonRemoteDpcProfileOwner_returnsNull()244 public void any_noDeviceOwner_nonRemoteDpcProfileOwner_returnsNull() { 245 ProfileOwner profileOwner = sTestApis.devicePolicy().setProfileOwner(sUser, 246 NON_REMOTE_DPC_COMPONENT); 247 248 try { 249 assertThat(RemoteDpc.any()).isNull(); 250 } finally { 251 profileOwner.remove(); 252 } 253 } 254 255 @Test any_nonRemoteDpcDeviceOwner_noProfileOwner_returnsNull()256 public void any_nonRemoteDpcDeviceOwner_noProfileOwner_returnsNull() { 257 DeviceOwner deviceOwner = sTestApis.devicePolicy().setDeviceOwner(sUser, 258 NON_REMOTE_DPC_COMPONENT); 259 260 try { 261 assertThat(RemoteDpc.any()).isNull(); 262 } finally { 263 deviceOwner.remove(); 264 } 265 } 266 267 @Test any_remoteDpcDeviceOwner_returnsDeviceOwner()268 public void any_remoteDpcDeviceOwner_returnsDeviceOwner() { 269 RemoteDpc remoteDPC = RemoteDpc.setAsDeviceOwner(sUser); 270 271 try { 272 assertThat(RemoteDpc.any()).isNotNull(); 273 } finally { 274 remoteDPC.devicePolicyController().remove(); 275 } 276 } 277 278 @Test any_remoteDpcProfileOwner_returnsProfileOwner()279 public void any_remoteDpcProfileOwner_returnsProfileOwner() { 280 RemoteDpc remoteDPC = RemoteDpc.setAsProfileOwner(sUser); 281 282 try { 283 assertThat(RemoteDpc.any()).isNotNull(); 284 } finally { 285 remoteDPC.devicePolicyController().remove(); 286 } 287 } 288 289 @Test any_userHandle_null_throwsException()290 public void any_userHandle_null_throwsException() { 291 assertThrows(NullPointerException.class, () -> RemoteDpc.any((UserHandle) null)); 292 } 293 294 @Test any_userHandle_noDeviceOwner_noProfileOwner_returnsNull()295 public void any_userHandle_noDeviceOwner_noProfileOwner_returnsNull() { 296 assertThat(RemoteDpc.any(sUser.userHandle())).isNull(); 297 } 298 299 @Test 300 @EnsureHasNoDeviceOwner any_userHandle_noDeviceOwner_nonRemoteDpcProfileOwner_returnsNull()301 public void any_userHandle_noDeviceOwner_nonRemoteDpcProfileOwner_returnsNull() { 302 UserReference profile = sTestApis.users().createUser() 303 .parent(sUser) 304 .type(sTestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 305 .createAndStart(); 306 sNonRemoteDpcTestApp.install(profile); 307 try { 308 sTestApis.devicePolicy().setProfileOwner(profile, NON_REMOTE_DPC_COMPONENT); 309 310 assertThat(RemoteDpc.any(profile.userHandle())).isNull(); 311 } finally { 312 profile.remove(); 313 } 314 } 315 316 @Test any_userHandle_nonRemoteDpcDeviceOwner_noProfileOwner_returnsNull()317 public void any_userHandle_nonRemoteDpcDeviceOwner_noProfileOwner_returnsNull() { 318 DeviceOwner deviceOwner = sTestApis.devicePolicy().setDeviceOwner(sUser, 319 NON_REMOTE_DPC_COMPONENT); 320 321 try { 322 assertThat(RemoteDpc.any(sUser.userHandle())).isNull(); 323 } finally { 324 deviceOwner.remove(); 325 } 326 } 327 328 @Test any_userHandle_remoteDpcDeviceOwner_returnsDeviceOwner()329 public void any_userHandle_remoteDpcDeviceOwner_returnsDeviceOwner() { 330 RemoteDpc deviceOwner = RemoteDpc.setAsDeviceOwner(sUser); 331 332 try { 333 assertThat(RemoteDpc.any(sUser.userHandle())).isEqualTo(deviceOwner); 334 } finally { 335 deviceOwner.devicePolicyController().remove(); 336 } 337 } 338 339 @Test 340 @EnsureHasNoDeviceOwner any_userHandle_remoteDpcProfileOwner_returnsProfileOwner()341 public void any_userHandle_remoteDpcProfileOwner_returnsProfileOwner() { 342 UserReference profile = sTestApis.users().createUser() 343 .parent(sUser) 344 .type(sTestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 345 .createAndStart(); 346 try { 347 RemoteDpc profileOwner = RemoteDpc.setAsProfileOwner(profile); 348 349 assertThat(RemoteDpc.any(profile.userHandle())).isEqualTo(profileOwner); 350 } finally { 351 profile.remove(); 352 } 353 } 354 355 @Test any_userReference_null_throwsException()356 public void any_userReference_null_throwsException() { 357 assertThrows(NullPointerException.class, () -> RemoteDpc.any((UserReference) null)); 358 } 359 360 @Test any_userReference_noDeviceOwner_noProfileOwner_returnsNull()361 public void any_userReference_noDeviceOwner_noProfileOwner_returnsNull() { 362 assertThat(RemoteDpc.any(sUser)).isNull(); 363 } 364 365 @Test 366 @EnsureHasNoDeviceOwner any_userReference_noDeviceOwner_nonRemoteDpcProfileOwner_returnsNull()367 public void any_userReference_noDeviceOwner_nonRemoteDpcProfileOwner_returnsNull() { 368 UserReference profile = sTestApis.users().createUser() 369 .parent(sUser) 370 .type(sTestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 371 .createAndStart(); 372 sNonRemoteDpcTestApp.install(profile); 373 try { 374 sTestApis.devicePolicy().setProfileOwner(profile, NON_REMOTE_DPC_COMPONENT); 375 376 assertThat(RemoteDpc.any(profile)).isNull(); 377 } finally { 378 profile.remove(); 379 } 380 } 381 382 @Test any_userReference_nonRemoteDpcDeviceOwner_noProfileOwner_returnsNull()383 public void any_userReference_nonRemoteDpcDeviceOwner_noProfileOwner_returnsNull() { 384 DeviceOwner deviceOwner = sTestApis.devicePolicy().setDeviceOwner(sUser, 385 NON_REMOTE_DPC_COMPONENT); 386 387 try { 388 assertThat(RemoteDpc.any(sUser)).isNull(); 389 } finally { 390 deviceOwner.remove(); 391 } 392 } 393 394 @Test any_userReference_remoteDpcDeviceOwner_returnsDeviceOwner()395 public void any_userReference_remoteDpcDeviceOwner_returnsDeviceOwner() { 396 RemoteDpc deviceOwner = RemoteDpc.setAsDeviceOwner(sUser); 397 398 try { 399 assertThat(RemoteDpc.any(sUser)).isEqualTo(deviceOwner); 400 } finally { 401 deviceOwner.devicePolicyController().remove(); 402 } 403 } 404 405 @Test 406 @EnsureHasNoDeviceOwner any_userReference_remoteDpcProfileOwner_returnsProfileOwner()407 public void any_userReference_remoteDpcProfileOwner_returnsProfileOwner() { 408 UserReference profile = sTestApis.users().createUser() 409 .parent(sUser) 410 .type(sTestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 411 .createAndStart(); 412 try { 413 RemoteDpc profileOwner = RemoteDpc.setAsProfileOwner(profile); 414 415 assertThat(RemoteDpc.any(profile)).isEqualTo(profileOwner); 416 } finally { 417 profile.remove(); 418 } 419 } 420 421 @Test setAsDeviceOwner_userHandle_null_throwsException()422 public void setAsDeviceOwner_userHandle_null_throwsException() { 423 assertThrows(NullPointerException.class, 424 () -> RemoteDpc.setAsDeviceOwner((UserHandle) null)); 425 } 426 427 @Test setAsDeviceOwner_userHandle_nonExistingUser_throwsException()428 public void setAsDeviceOwner_userHandle_nonExistingUser_throwsException() { 429 assertThrows(NeneException.class, 430 () -> RemoteDpc.setAsDeviceOwner(NON_EXISTING_USER_HANDLE)); 431 } 432 433 @Test setAsDeviceOwner_userHandle_alreadySet_doesNothing()434 public void setAsDeviceOwner_userHandle_alreadySet_doesNothing() { 435 RemoteDpc.setAsDeviceOwner(sUser.userHandle()); 436 437 DeviceOwner deviceOwner = sTestApis.devicePolicy().getDeviceOwner(); 438 try { 439 RemoteDpc.setAsDeviceOwner(sUser.userHandle()); 440 441 deviceOwner = sTestApis.devicePolicy().getDeviceOwner(); 442 assertThat(deviceOwner).isNotNull(); 443 } finally { 444 if (deviceOwner != null) { 445 deviceOwner.remove(); 446 } 447 } 448 } 449 450 @Test setAsDeviceOwner_userHandle_alreadyHasDeviceOwner_replacesDeviceOwner()451 public void setAsDeviceOwner_userHandle_alreadyHasDeviceOwner_replacesDeviceOwner() { 452 sTestApis.devicePolicy().setDeviceOwner(sUser, NON_REMOTE_DPC_COMPONENT); 453 454 try { 455 RemoteDpc remoteDPC = RemoteDpc.setAsDeviceOwner(sUser.userHandle()); 456 457 DeviceOwner deviceOwner = sTestApis.devicePolicy().getDeviceOwner(); 458 assertThat(deviceOwner).isEqualTo(remoteDPC.devicePolicyController()); 459 } finally { 460 sTestApis.devicePolicy().getDeviceOwner().remove(); 461 } 462 } 463 464 @Test setAsDeviceOwner_userHandle_doesNotHaveDeviceOwner_setsDeviceOwner()465 public void setAsDeviceOwner_userHandle_doesNotHaveDeviceOwner_setsDeviceOwner() { 466 RemoteDpc.setAsDeviceOwner(sUser.userHandle()); 467 468 DeviceOwner deviceOwner = sTestApis.devicePolicy().getDeviceOwner(); 469 try { 470 assertThat(deviceOwner).isNotNull(); 471 } finally { 472 if (deviceOwner != null) { 473 deviceOwner.remove(); 474 } 475 } 476 } 477 478 @Test setAsDeviceOwner_userReference_null_throwsException()479 public void setAsDeviceOwner_userReference_null_throwsException() { 480 assertThrows(NullPointerException.class, 481 () -> RemoteDpc.setAsDeviceOwner((UserReference) null)); 482 } 483 484 @Test setAsDeviceOwner_userReference_nonExistingUser_throwsException()485 public void setAsDeviceOwner_userReference_nonExistingUser_throwsException() { 486 assertThrows(NeneException.class, 487 () -> RemoteDpc.setAsDeviceOwner(NON_EXISTING_USER_REFERENCE)); 488 } 489 490 @Test setAsDeviceOwner_userReference_alreadySet_doesNothing()491 public void setAsDeviceOwner_userReference_alreadySet_doesNothing() { 492 RemoteDpc.setAsDeviceOwner(sUser); 493 494 DeviceOwner deviceOwner = sTestApis.devicePolicy().getDeviceOwner(); 495 try { 496 RemoteDpc.setAsDeviceOwner(sUser); 497 498 deviceOwner = sTestApis.devicePolicy().getDeviceOwner(); 499 assertThat(deviceOwner).isNotNull(); 500 } finally { 501 if (deviceOwner != null) { 502 deviceOwner.remove(); 503 } 504 } 505 } 506 507 @Test setAsDeviceOwner_userReference_alreadyHasDeviceOwner_replacesDeviceOwner()508 public void setAsDeviceOwner_userReference_alreadyHasDeviceOwner_replacesDeviceOwner() { 509 sTestApis.devicePolicy().setDeviceOwner(sUser, NON_REMOTE_DPC_COMPONENT); 510 511 try { 512 RemoteDpc remoteDPC = RemoteDpc.setAsDeviceOwner(sUser); 513 514 DeviceOwner deviceOwner = sTestApis.devicePolicy().getDeviceOwner(); 515 assertThat(deviceOwner).isEqualTo(remoteDPC.devicePolicyController()); 516 } finally { 517 sTestApis.devicePolicy().getDeviceOwner().remove(); 518 } 519 } 520 521 @Test setAsDeviceOwner_userReference_doesNotHaveDeviceOwner_setsDeviceOwner()522 public void setAsDeviceOwner_userReference_doesNotHaveDeviceOwner_setsDeviceOwner() { 523 RemoteDpc.setAsDeviceOwner(sUser); 524 525 DeviceOwner deviceOwner = sTestApis.devicePolicy().getDeviceOwner(); 526 try { 527 assertThat(deviceOwner).isNotNull(); 528 } finally { 529 if (deviceOwner != null) { 530 deviceOwner.remove(); 531 } 532 } 533 } 534 535 @Test setAsProfileOwner_userHandle_null_throwsException()536 public void setAsProfileOwner_userHandle_null_throwsException() { 537 assertThrows(NullPointerException.class, 538 () -> RemoteDpc.setAsProfileOwner((UserHandle) null)); 539 } 540 541 @Test setAsProfileOwner_userHandle_nonExistingUser_throwsException()542 public void setAsProfileOwner_userHandle_nonExistingUser_throwsException() { 543 assertThrows(NeneException.class, 544 () -> RemoteDpc.setAsProfileOwner(NON_EXISTING_USER_HANDLE)); 545 } 546 547 @Test 548 @EnsureHasNoDeviceOwner setAsProfileOwner_userHandle_alreadySet_doesNothing()549 public void setAsProfileOwner_userHandle_alreadySet_doesNothing() { 550 UserReference profile = sTestApis.users().createUser() 551 .parent(sUser) 552 .type(sTestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 553 .createAndStart(); 554 try { 555 RemoteDpc.setAsProfileOwner(profile.userHandle()); 556 557 RemoteDpc.setAsProfileOwner(profile.userHandle()); 558 559 assertThat(sTestApis.devicePolicy().getProfileOwner(profile)).isNotNull(); 560 } finally { 561 profile.remove(); 562 } 563 } 564 565 @Test 566 @EnsureHasNoDeviceOwner setAsProfileOwner_userHandle_alreadyHasProfileOwner_replacesProfileOwner()567 public void setAsProfileOwner_userHandle_alreadyHasProfileOwner_replacesProfileOwner() { 568 UserReference profile = sTestApis.users().createUser() 569 .parent(sUser) 570 .type(sTestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 571 .createAndStart(); 572 sNonRemoteDpcTestApp.install(profile); 573 try { 574 sTestApis.devicePolicy().setProfileOwner(profile, NON_REMOTE_DPC_COMPONENT); 575 576 RemoteDpc remoteDPC = RemoteDpc.setAsProfileOwner(profile.userHandle()); 577 578 assertThat(sTestApis.devicePolicy().getProfileOwner(profile)) 579 .isEqualTo(remoteDPC.devicePolicyController()); 580 } finally { 581 profile.remove(); 582 } 583 } 584 585 @Test 586 @EnsureHasNoDeviceOwner setAsProfileOwner_userHandle_doesNotHaveProfileOwner_setsProfileOwner()587 public void setAsProfileOwner_userHandle_doesNotHaveProfileOwner_setsProfileOwner() { 588 UserReference profile = sTestApis.users().createUser() 589 .parent(sUser) 590 .type(sTestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 591 .createAndStart(); 592 try { 593 RemoteDpc.setAsProfileOwner(profile.userHandle()); 594 595 assertThat(sTestApis.devicePolicy().getProfileOwner(profile)).isNotNull(); 596 } finally { 597 profile.remove(); 598 } 599 } 600 601 @Test setAsProfileOwner_userReference_null_throwsException()602 public void setAsProfileOwner_userReference_null_throwsException() { 603 assertThrows(NullPointerException.class, 604 () -> RemoteDpc.setAsProfileOwner((UserReference) null)); 605 } 606 607 @Test setAsProfileOwner_userReference_nonExistingUser_throwsException()608 public void setAsProfileOwner_userReference_nonExistingUser_throwsException() { 609 assertThrows(NeneException.class, 610 () -> RemoteDpc.setAsProfileOwner(NON_EXISTING_USER_REFERENCE)); 611 } 612 613 @Test 614 @EnsureHasNoDeviceOwner setAsProfileOwner_userReference_alreadySet_doesNothing()615 public void setAsProfileOwner_userReference_alreadySet_doesNothing() { 616 UserReference profile = sTestApis.users().createUser() 617 .parent(sUser) 618 .type(sTestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 619 .createAndStart(); 620 try { 621 RemoteDpc.setAsProfileOwner(profile); 622 623 RemoteDpc.setAsProfileOwner(profile); 624 625 assertThat(sTestApis.devicePolicy().getProfileOwner(profile)).isNotNull(); 626 } finally { 627 profile.remove(); 628 } 629 } 630 631 @Test 632 @EnsureHasNoDeviceOwner setAsProfileOwner_userReference_alreadyHasProfileOwner_replacesProfileOwner()633 public void setAsProfileOwner_userReference_alreadyHasProfileOwner_replacesProfileOwner() { 634 UserReference profile = sTestApis.users().createUser() 635 .parent(sUser) 636 .type(sTestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 637 .createAndStart(); 638 sNonRemoteDpcTestApp.install(profile); 639 try { 640 sTestApis.devicePolicy().setProfileOwner(profile, NON_REMOTE_DPC_COMPONENT); 641 642 RemoteDpc remoteDPC = RemoteDpc.setAsProfileOwner(profile); 643 644 assertThat(sTestApis.devicePolicy().getProfileOwner(profile)) 645 .isEqualTo(remoteDPC.devicePolicyController()); 646 } finally { 647 profile.remove(); 648 } 649 } 650 651 @Test 652 @EnsureHasNoDeviceOwner setAsProfileOwner_userReference_doesNotHaveProfileOwner_setsProfileOwner()653 public void setAsProfileOwner_userReference_doesNotHaveProfileOwner_setsProfileOwner() { 654 UserReference profile = sTestApis.users().createUser() 655 .parent(sUser) 656 .type(sTestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 657 .createAndStart(); 658 try { 659 RemoteDpc.setAsProfileOwner(profile); 660 661 assertThat(sTestApis.devicePolicy().getProfileOwner(profile)).isNotNull(); 662 } finally { 663 profile.remove(); 664 } 665 } 666 667 @Test devicePolicyController_returnsDevicePolicyController()668 public void devicePolicyController_returnsDevicePolicyController() { 669 RemoteDpc remoteDPC = RemoteDpc.setAsDeviceOwner(sUser); 670 671 try { 672 assertThat(remoteDPC.devicePolicyController()) 673 .isEqualTo(sTestApis.devicePolicy().getDeviceOwner()); 674 } finally { 675 remoteDPC.remove(); 676 } 677 } 678 679 @Test remove_deviceOwner_removes()680 public void remove_deviceOwner_removes() { 681 RemoteDpc remoteDPC = RemoteDpc.setAsDeviceOwner(sUser); 682 683 remoteDPC.remove(); 684 685 assertThat(sTestApis.devicePolicy().getDeviceOwner()).isNull(); 686 } 687 688 @Test 689 @EnsureHasNoDeviceOwner remove_profileOwner_removes()690 public void remove_profileOwner_removes() { 691 try (UserReference profile = sTestApis.users().createUser() 692 .parent(sUser) 693 .type(sTestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 694 .createAndStart()) { 695 RemoteDpc remoteDPC = RemoteDpc.setAsProfileOwner(profile); 696 697 remoteDPC.remove(); 698 699 assertThat(sTestApis.devicePolicy().getProfileOwner(profile)).isNull(); 700 } 701 } 702 703 // TODO(scottjonathan): Do we need to support the case where there is both a DO and a PO on 704 // older versions of Android? 705 706 @Test frameworkCall_makesCall()707 public void frameworkCall_makesCall() { 708 RemoteDpc remoteDPC = RemoteDpc.setAsDeviceOwner(sUser); 709 710 try { 711 // Checking that the call succeeds 712 remoteDPC.devicePolicyManager().getCurrentFailedPasswordAttempts(); 713 } finally { 714 remoteDPC.remove(); 715 } 716 } 717 718 @Test 719 @EnsureHasNoDeviceOwner frameworkCall_onProfile_makesCall()720 public void frameworkCall_onProfile_makesCall() { 721 try (UserReference profile = sTestApis.users().createUser() 722 .parent(sUser) 723 .type(sTestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 724 .createAndStart()) { 725 RemoteDpc remoteDPC = RemoteDpc.setAsProfileOwner(profile); 726 727 // Checking that the call succeeds 728 remoteDPC.devicePolicyManager().isUsingUnifiedPassword(); 729 } 730 } 731 732 @Test frameworkCallRequiresProfileOwner_notProfileOwner_throwsSecurityException()733 public void frameworkCallRequiresProfileOwner_notProfileOwner_throwsSecurityException() { 734 RemoteDpc remoteDPC = RemoteDpc.setAsDeviceOwner(sUser); 735 736 try { 737 assertThrows(SecurityException.class, 738 () -> remoteDPC.devicePolicyManager().isUsingUnifiedPassword()); 739 } finally { 740 remoteDPC.remove(); 741 } 742 } 743 744 @Test forDevicePolicyController_nullDevicePolicyController_throwsException()745 public void forDevicePolicyController_nullDevicePolicyController_throwsException() { 746 assertThrows(NullPointerException.class, () -> RemoteDpc.forDevicePolicyController(null)); 747 } 748 749 @Test forDevicePolicyController_nonRemoteDpcDevicePolicyController_throwsException()750 public void forDevicePolicyController_nonRemoteDpcDevicePolicyController_throwsException() { 751 DeviceOwner deviceOwner = sTestApis.devicePolicy().setDeviceOwner(sUser, 752 NON_REMOTE_DPC_COMPONENT); 753 754 try { 755 assertThrows(IllegalStateException.class, 756 () -> RemoteDpc.forDevicePolicyController(deviceOwner)); 757 } finally { 758 deviceOwner.remove(); 759 } 760 } 761 762 @Test forDevicePolicyController_remoteDpcDevicePolicyController_returnsRemoteDpc()763 public void forDevicePolicyController_remoteDpcDevicePolicyController_returnsRemoteDpc() { 764 RemoteDpc remoteDPC = RemoteDpc.setAsDeviceOwner(sUser); 765 766 try { 767 assertThat(RemoteDpc.forDevicePolicyController(remoteDPC.devicePolicyController())) 768 .isNotNull(); 769 } finally { 770 remoteDPC.remove(); 771 } 772 773 } 774 }