1 /* 2 * Copyright (C) 2024 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.bedstead.enterprise 17 18 import android.app.admin.DevicePolicyManager 19 import android.os.Build 20 import android.os.Bundle 21 import com.android.bedstead.enterprise.annotations.DEFAULT_DPC_KEY 22 import com.android.bedstead.enterprise.annotations.DEFAULT_KEY 23 import com.android.bedstead.enterprise.annotations.EnsureHasDelegate 24 import com.android.bedstead.enterprise.annotations.EnsureHasDeviceOwner 25 import com.android.bedstead.enterprise.annotations.EnsureHasNoDeviceOwner 26 import com.android.bedstead.enterprise.annotations.EnsureHasNoDpc 27 import com.android.bedstead.enterprise.annotations.EnsureHasNoProfileOwner 28 import com.android.bedstead.enterprise.annotations.EnsureHasNoWorkProfile 29 import com.android.bedstead.enterprise.annotations.EnsureHasProfileOwner 30 import com.android.bedstead.enterprise.annotations.EnsureHasWorkProfile 31 import com.android.bedstead.enterprise.annotations.parameterized.IncludeRunOnBackgroundDeviceOwnerUser 32 import com.android.bedstead.enterprise.annotations.parameterized.IncludeRunOnCloneProfileAlongsideManagedProfileUsingParentInstance 33 import com.android.bedstead.enterprise.annotations.parameterized.IncludeRunOnCloneProfileAlongsideOrganizationOwnedProfileUsingParentInstance 34 import com.android.bedstead.enterprise.annotations.parameterized.IncludeRunOnFinancedDeviceOwnerUser 35 import com.android.bedstead.enterprise.annotations.parameterized.IncludeRunOnOrganizationOwnedProfileOwner 36 import com.android.bedstead.enterprise.annotations.parameterized.IncludeRunOnParentOfOrganizationOwnedProfileOwner 37 import com.android.bedstead.enterprise.annotations.parameterized.IncludeRunOnParentOfOrganizationOwnedProfileOwnerUsingParentInstance 38 import com.android.bedstead.enterprise.annotations.parameterized.IncludeRunOnParentOfProfileOwnerUsingParentInstance 39 import com.android.bedstead.enterprise.annotations.parameterized.IncludeRunOnParentOfProfileOwnerWithNoDeviceOwner 40 import com.android.bedstead.enterprise.annotations.parameterized.IncludeRunOnPrivateProfileAlongsideManagedProfileUsingParentInstance 41 import com.android.bedstead.enterprise.annotations.parameterized.IncludeRunOnPrivateProfileAlongsideOrganizationOwnedProfileUsingParentInstance 42 import com.android.bedstead.enterprise.annotations.parameterized.IncludeRunOnProfileOwnerProfileWithNoDeviceOwner 43 import com.android.bedstead.enterprise.annotations.parameterized.IncludeRunOnSecondaryUserInDifferentProfileGroupToProfileOwnerProfile 44 import com.android.bedstead.enterprise.annotations.parameterized.IncludeRunOnSingleDeviceOwnerUser 45 import com.android.bedstead.enterprise.annotations.parameterized.IncludeRunOnSystemDeviceOwnerUser 46 import com.android.bedstead.enterprise.annotations.parameterized.IncludeRunOnUnaffiliatedDeviceOwnerSecondaryUser 47 import com.android.bedstead.harrier.BedsteadJUnit4 48 import com.android.bedstead.harrier.DeviceState 49 import com.android.bedstead.harrier.UserType 50 import com.android.bedstead.harrier.annotations.EnsureHasSecondaryUser 51 import com.android.bedstead.harrier.annotations.RequireHeadlessSystemUserMode 52 import com.android.bedstead.harrier.annotations.enterprise.AdditionalQueryParameters 53 import com.android.bedstead.nene.TestApis.context 54 import com.android.bedstead.nene.TestApis.devicePolicy 55 import com.android.bedstead.nene.TestApis.users 56 import com.android.bedstead.nene.devicepolicy.DeviceOwnerType 57 import com.android.bedstead.nene.devicepolicy.ProfileOwner 58 import com.android.bedstead.nene.types.OptionalBoolean 59 import com.android.bedstead.nene.users.UserReference 60 import com.android.bedstead.nene.users.UserType.MANAGED_PROFILE_TYPE_NAME 61 import com.android.bedstead.remotedpc.RemoteDelegate 62 import com.android.bedstead.remotedpc.RemoteDpc 63 import com.android.queryable.annotations.BooleanQuery 64 import com.android.queryable.annotations.IntegerQuery 65 import com.android.queryable.annotations.Query 66 import com.google.common.truth.Truth.assertThat 67 import org.junit.ClassRule 68 import org.junit.Rule 69 import org.junit.Test 70 import org.junit.runner.RunWith 71 import org.testng.Assert.assertThrows 72 73 @RunWith(BedsteadJUnit4::class) 74 class EnterpriseAnnotationExecutorTest { 75 76 @Test 77 @EnsureHasDeviceOwner 78 @EnsureHasDelegate( 79 admin = EnsureHasDelegate.AdminType.DEVICE_OWNER, 80 scopes = [DevicePolicyManager.DELEGATION_CERT_INSTALL], 81 isPrimary = true 82 ) ensureHasPrimaryDelegateAnnotation_dpcReturnsDelegatenull83 fun ensureHasPrimaryDelegateAnnotation_dpcReturnsDelegate() { 84 assertThat(sDeviceState.dpc()).isInstanceOf(RemoteDelegate::class.java) 85 } 86 87 @Test 88 @EnsureHasDeviceOwner 89 @EnsureHasDelegate( 90 admin = EnsureHasDelegate.AdminType.DEVICE_OWNER, 91 scopes = [DevicePolicyManager.DELEGATION_CERT_INSTALL], 92 isPrimary = false 93 ) ensureHasNonPrimaryDelegateAnnotation_dpcReturnsDpcnull94 fun ensureHasNonPrimaryDelegateAnnotation_dpcReturnsDpc() { 95 assertThat(sDeviceState.dpc()).isInstanceOf(RemoteDpc::class.java) 96 } 97 98 @Test 99 @EnsureHasDeviceOwner 100 @EnsureHasDelegate( 101 admin = EnsureHasDelegate.AdminType.DEVICE_OWNER, 102 scopes = [DevicePolicyManager.DELEGATION_CERT_INSTALL], 103 isPrimary = true 104 ) ensureHasDelegateAnnotation_dpcCanUseDelegatedFunctionalitynull105 fun ensureHasDelegateAnnotation_dpcCanUseDelegatedFunctionality() { 106 assertThat( 107 sDeviceState.dpc().devicePolicyManager().getEnrollmentSpecificId() 108 ).isNotNull() 109 } 110 111 @Test 112 @EnsureHasDeviceOwner 113 @EnsureHasDelegate( 114 admin = EnsureHasDelegate.AdminType.DEVICE_OWNER, 115 scopes = [ 116 DevicePolicyManager.DELEGATION_CERT_INSTALL, 117 DevicePolicyManager.DELEGATION_APP_RESTRICTIONS 118 ], 119 isPrimary = true 120 ) ensureHasDelegateAnnotation_multipleScopes_dpcCanUseAllDelegatedFunctionalitynull121 fun ensureHasDelegateAnnotation_multipleScopes_dpcCanUseAllDelegatedFunctionality() { 122 assertThat( 123 sDeviceState.dpc().devicePolicyManager().getEnrollmentSpecificId() 124 ).isNotNull() 125 sDeviceState.dpc() 126 .devicePolicyManager() 127 .setApplicationRestrictions( 128 sDeviceState.dpc().componentName(), 129 sDeviceState.dpc().packageName(), 130 Bundle() 131 ) 132 } 133 134 @EnsureHasDeviceOwner(key = EnsureHasDeviceOwner.DEFAULT_KEY, isPrimary = true) 135 @AdditionalQueryParameters( 136 forTestApp = EnsureHasDeviceOwner.DEFAULT_KEY, 137 query = Query(targetSdkVersion = IntegerQuery(isEqualTo = 28)) 138 ) 139 @Test additionalQueryParameters_ensureHasDeviceOwner_isRespectednull140 fun additionalQueryParameters_ensureHasDeviceOwner_isRespected() { 141 assertThat(sDeviceState.dpc().testApp().targetSdkVersion()) 142 .isEqualTo(28) 143 } 144 145 @Test 146 @EnsureHasDeviceOwner deviceOwner_deviceOwnerIsSet_returnsDeviceOwnernull147 fun deviceOwner_deviceOwnerIsSet_returnsDeviceOwner() { 148 assertThat(sDeviceState.deviceOwner()).isNotNull() 149 } 150 151 @Test 152 @EnsureHasDeviceOwner(dpc = Query(targetSdkVersion = IntegerQuery(isEqualTo = 28))) ensureHasDeviceOwnerAnnotation_targetingV28_remoteDpcTargetsV28null153 fun ensureHasDeviceOwnerAnnotation_targetingV28_remoteDpcTargetsV28() { 154 val remoteDpc = RemoteDpc.forDevicePolicyController(devicePolicy().getDeviceOwner()) 155 assertThat(remoteDpc.testApp().pkg().targetSdkVersion()).isEqualTo(28) 156 } 157 158 @Test 159 @EnsureHasDeviceOwner(dpc = Query(targetSdkVersion = IntegerQuery(isGreaterThanOrEqualTo = 30))) ensureHasDeviceOwnerAnnoion_targetingGreaterThanOrEqualToV30_remoteDpcTargetsV30null160 fun ensureHasDeviceOwnerAnnoion_targetingGreaterThanOrEqualToV30_remoteDpcTargetsV30() { 161 val remoteDpc = RemoteDpc.forDevicePolicyController(devicePolicy().getDeviceOwner()) 162 assertThat(remoteDpc.testApp().pkg().targetSdkVersion()).isAtLeast(30) 163 } 164 165 @Test 166 @EnsureHasDeviceOwner(isPrimary = true) 167 @EnsureHasDelegate(admin = EnsureHasDelegate.AdminType.PRIMARY, scopes = []) ensureHasDelegateAnnotation_primaryAdminWithoutReplace_dpcReturnsDpcnull168 fun ensureHasDelegateAnnotation_primaryAdminWithoutReplace_dpcReturnsDpc() { 169 assertThat(sDeviceState.dpc()).isInstanceOf(RemoteDpc::class.java) 170 } 171 172 @Test 173 @EnsureHasDeviceOwner(isPrimary = true) 174 @EnsureHasDelegate(admin = EnsureHasDelegate.AdminType.PRIMARY, scopes = [], isPrimary = true) ensureHasDelegateAnnotation_primaryAdminAndReplace_dpcReturnsDelegatenull175 fun ensureHasDelegateAnnotation_primaryAdminAndReplace_dpcReturnsDelegate() { 176 assertThat(sDeviceState.dpc()).isInstanceOf(RemoteDelegate::class.java) 177 } 178 179 @EnsureHasDeviceOwner( 180 isPrimary = true, 181 key = "dpc", 182 dpc = Query(isHeadlessDOSingleUser = BooleanQuery(isEqualTo = OptionalBoolean.TRUE)) 183 ) 184 @Test additionalQueryParameters_isHeadlessDOSingleUser_isRespectednull185 fun additionalQueryParameters_isHeadlessDOSingleUser_isRespected() { 186 assertThat( 187 sDeviceState.dpc().testApp().metadata().getString("headless_do_single_user") 188 ).isEqualTo("true") 189 } 190 191 @EnsureHasDeviceOwner( 192 isPrimary = true, 193 key = "dpc", 194 dpc = Query(isHeadlessDOSingleUser = BooleanQuery(isEqualTo = OptionalBoolean.FALSE)) 195 ) 196 @Test additionalQueryParameters_isNotHeadlessDOSingleUser_isRespectednull197 fun additionalQueryParameters_isNotHeadlessDOSingleUser_isRespected() { 198 assertThat( 199 sDeviceState.dpc().testApp().metadata().getString("headless_do_single_user") 200 ).isNull() 201 } 202 203 @EnsureHasDeviceOwner 204 @Test ensureHasDeviceOwnerAnnotation_deviceOwnerIsSetnull205 fun ensureHasDeviceOwnerAnnotation_deviceOwnerIsSet() { 206 assertThat(devicePolicy().getDeviceOwner()).isNotNull() 207 } 208 209 @Test 210 @EnsureHasDeviceOwner ensureHasDeviceOwnerAnnotation_noQuerySpecified_setsDefaultRemoteDpcnull211 fun ensureHasDeviceOwnerAnnotation_noQuerySpecified_setsDefaultRemoteDpc() { 212 val deviceOwner = devicePolicy().getDeviceOwner() 213 assertThat(deviceOwner!!.pkg().packageName()) 214 .isEqualTo(RemoteDpc.REMOTE_DPC_APP_PACKAGE_NAME_OR_PREFIX) 215 assertThat(deviceOwner.pkg().targetSdkVersion()).isIn( 216 setOf(Build.VERSION.SDK_INT, Build.VERSION_CODES.CUR_DEVELOPMENT) 217 ) 218 } 219 220 @Test 221 @EnsureHasNoDeviceOwner ensureHasNoDeviceOwnerAnnotation_deviceOwnerIsNotSetnull222 fun ensureHasNoDeviceOwnerAnnotation_deviceOwnerIsNotSet() { 223 assertThat(devicePolicy().getDeviceOwner()).isNull() 224 } 225 226 @Test 227 @EnsureHasProfileOwner ensureHasProfileOwnerAnnotation_noQuerySpecified_setsDefaultRemoteDpcnull228 fun ensureHasProfileOwnerAnnotation_noQuerySpecified_setsDefaultRemoteDpc() { 229 val profileOwner = devicePolicy().getProfileOwner() 230 assertThat( 231 profileOwner!!.pkg().packageName() 232 ).isEqualTo(RemoteDpc.REMOTE_DPC_APP_PACKAGE_NAME_OR_PREFIX) 233 assertThat( 234 profileOwner.pkg().targetSdkVersion() 235 ).isIn(setOf(Build.VERSION.SDK_INT, Build.VERSION_CODES.CUR_DEVELOPMENT)) 236 } 237 238 @Test 239 @EnsureHasProfileOwner(dpc = Query(targetSdkVersion = IntegerQuery(isEqualTo = 28))) ensureHasProfileOwnerAnnotation_targetingV28_remoteDpcTargetsV28null240 fun ensureHasProfileOwnerAnnotation_targetingV28_remoteDpcTargetsV28() { 241 val remoteDpc = RemoteDpc.forDevicePolicyController(devicePolicy().getProfileOwner()) 242 assertThat(remoteDpc.testApp().pkg().targetSdkVersion()).isEqualTo(28) 243 } 244 245 @Test 246 @EnsureHasNoDeviceOwner deviceOwner_deviceOwnerIsNotSet_throwsExceptionnull247 fun deviceOwner_deviceOwnerIsNotSet_throwsException() { 248 assertThrows(IllegalStateException::class.java) { 249 sDeviceState.deviceOwner() 250 } 251 } 252 253 @Test 254 @EnsureHasProfileOwner ensureHasProfileOwnerAnnotation_defaultUser_profileOwnerIsSetnull255 fun ensureHasProfileOwnerAnnotation_defaultUser_profileOwnerIsSet() { 256 assertThat(devicePolicy().getProfileOwner(users().instrumented())).isNotNull() 257 } 258 259 @Test 260 @EnsureHasNoProfileOwner ensureHasNoProfileOwnerAnnotation_defaultUser_profileOwnerIsNotSetnull261 fun ensureHasNoProfileOwnerAnnotation_defaultUser_profileOwnerIsNotSet() { 262 assertThat(devicePolicy().getProfileOwner(users().instrumented())).isNull() 263 } 264 265 @EnsureHasSecondaryUser 266 @EnsureHasProfileOwner(onUser = UserType.SECONDARY_USER) ensureHasProfileOwnerAnnotation_otherUser_setsProfileOwnernull267 fun ensureHasProfileOwnerAnnotation_otherUser_setsProfileOwner() { 268 assertThat(devicePolicy().getProfileOwner(sDeviceState.secondaryUser())).isNotNull() 269 } 270 271 @Test 272 @EnsureHasSecondaryUser 273 @EnsureHasNoProfileOwner(onUser = UserType.SECONDARY_USER) ensureHasNoProfileOwnerAnnotation_otherUser_profileOwnerIsNotSetnull274 fun ensureHasNoProfileOwnerAnnotation_otherUser_profileOwnerIsNotSet() { 275 assertThat(devicePolicy().getProfileOwner(sDeviceState.secondaryUser())).isNull() 276 } 277 278 @Test 279 @EnsureHasNoProfileOwner profileOwner_defaultUser_profileOwnerIsNotSet_throwsExceptionnull280 fun profileOwner_defaultUser_profileOwnerIsNotSet_throwsException() { 281 assertThrows(IllegalStateException::class.java) { 282 sDeviceState.profileOwner() 283 } 284 } 285 286 @Test 287 @EnsureHasProfileOwner profileOwner_defaultUser_profileOwnerIsSet_returnsProfileOwnernull288 fun profileOwner_defaultUser_profileOwnerIsSet_returnsProfileOwner() { 289 assertThat(devicePolicy().getProfileOwner(users().instrumented())).isNotNull() 290 } 291 292 @Test 293 @EnsureHasSecondaryUser 294 @EnsureHasProfileOwner(onUser = UserType.SECONDARY_USER) profileOwner_otherUser_profileOwnerIsSet_returnsProfileOwnernull295 fun profileOwner_otherUser_profileOwnerIsSet_returnsProfileOwner() { 296 assertThat(sDeviceState.profileOwner(sDeviceState.secondaryUser())).isNotNull() 297 } 298 299 @Test 300 @EnsureHasSecondaryUser 301 @EnsureHasNoProfileOwner(onUser = UserType.SECONDARY_USER) 302 @EnsureHasNoProfileOwner profileOwner_otherUser_profileOwnerIsNotSet_throwsExceptionnull303 fun profileOwner_otherUser_profileOwnerIsNotSet_throwsException() { 304 assertThrows(IllegalStateException::class.java) { 305 sDeviceState.profileOwner() 306 } 307 } 308 309 @Test profileOwner_userType_onUserIsNull_throwsExceptionnull310 fun profileOwner_userType_onUserIsNull_throwsException() { 311 assertThrows(NullPointerException::class.java) { 312 sDeviceState.profileOwner(null as UserType?) 313 } 314 } 315 316 @Test profileOwner_userReference_onUserIsNull_throwsExceptionnull317 fun profileOwner_userReference_onUserIsNull_throwsException() { 318 assertThrows(NullPointerException::class.java) { 319 sDeviceState.profileOwner(null as UserReference?) 320 } 321 } 322 323 @EnsureHasProfileOwner(key = DEFAULT_KEY, isPrimary = true) 324 @AdditionalQueryParameters( 325 forTestApp = DEFAULT_KEY, 326 query = Query(targetSdkVersion = IntegerQuery(isEqualTo = 28)) 327 ) 328 @Test additionalQueryParameters_ensureHasProfileOwner_isRespectednull329 fun additionalQueryParameters_ensureHasProfileOwner_isRespected() { 330 assertThat(sDeviceState.dpc().testApp().targetSdkVersion()).isEqualTo(28) 331 } 332 333 @Test 334 @IncludeRunOnBackgroundDeviceOwnerUser includeRunOnBackgroundDeviceOwnerUserAnnotation_isRunningOnDeviceOwnerUsernull335 fun includeRunOnBackgroundDeviceOwnerUserAnnotation_isRunningOnDeviceOwnerUser() { 336 assertThat(users().instrumented()).isEqualTo(sDeviceState.dpc().user()) 337 } 338 339 @Test 340 @IncludeRunOnBackgroundDeviceOwnerUser includeRunOnBackgroundDeviceOwnerUserAnnotation_isNotCurrentUsernull341 fun includeRunOnBackgroundDeviceOwnerUserAnnotation_isNotCurrentUser() { 342 assertThat(users().current()).isNotEqualTo(users().instrumented()) 343 } 344 345 @Test 346 @IncludeRunOnCloneProfileAlongsideManagedProfileUsingParentInstance includeRunOnCloneProfileAlongsideProfileOwnerUsingParentInstance_runsOnCloneProfilenull347 fun includeRunOnCloneProfileAlongsideProfileOwnerUsingParentInstance_runsOnCloneProfile() { 348 assertThat(users().instrumented().type().name()).isEqualTo(CLONE_PROFILE_TYPE_NAME) 349 val dpcUser = sDeviceState.dpc().user() 350 assertThat(dpcUser.type().name()).isEqualTo(MANAGED_PROFILE_TYPE_NAME) 351 assertThat(devicePolicy().getProfileOwner(dpcUser)!!.isOrganizationOwned()).isFalse() 352 assertThat(sDeviceState.dpc().isParentInstance).isTrue() 353 } 354 355 @Test 356 @IncludeRunOnCloneProfileAlongsideOrganizationOwnedProfileUsingParentInstance includeRunOnCloneProfileAlongsideOrgOwnedProfileOwnerUsingParentInstance_runsOnCloneProfilenull357 fun includeRunOnCloneProfileAlongsideOrgOwnedProfileOwnerUsingParentInstance_runsOnCloneProfile() { 358 assertThat(users().instrumented().type().name()).isEqualTo(CLONE_PROFILE_TYPE_NAME) 359 val dpcUser = sDeviceState.dpc().user() 360 assertThat(dpcUser.type().name()).isEqualTo(MANAGED_PROFILE_TYPE_NAME) 361 assertThat(devicePolicy().getProfileOwner(dpcUser)!!.isOrganizationOwned()).isTrue() 362 assertThat(sDeviceState.dpc().isParentInstance).isTrue() 363 } 364 365 @Test 366 @IncludeRunOnFinancedDeviceOwnerUser includeRunOnFinancedDeviceOwnerUserAnnotation_financedDeviceOwnerTypeSetnull367 fun includeRunOnFinancedDeviceOwnerUserAnnotation_financedDeviceOwnerTypeSet() { 368 assertThat(devicePolicy().getDeviceOwner()!!.user()).isEqualTo(users().instrumented()) 369 assertThat(devicePolicy().getDeviceOwner()!!.getType()).isEqualTo(DeviceOwnerType.FINANCED) 370 } 371 372 @Test 373 @IncludeRunOnOrganizationOwnedProfileOwner includeRunOnOrganizationOwnedProfileOwnerAnnotation_isRunningOnOrganizationOwnedManagedProfilenull374 fun includeRunOnOrganizationOwnedProfileOwnerAnnotation_isRunningOnOrganizationOwnedManagedProfile() { 375 assertThat(users().instrumented().type().name()).isEqualTo(MANAGED_PROFILE_TYPE_NAME) 376 assertThat(devicePolicy().getProfileOwner()!!.isOrganizationOwned()).isTrue() 377 } 378 379 @Test 380 @IncludeRunOnOrganizationOwnedProfileOwner includeRunOnOrganizationOwnedProfileOwner_isOrganizationOwnednull381 fun includeRunOnOrganizationOwnedProfileOwner_isOrganizationOwned() { 382 assertThat( 383 (sDeviceState.profileOwner(sDeviceState.workProfile()) 384 .devicePolicyController() as ProfileOwner).isOrganizationOwned() 385 ).isTrue() 386 } 387 388 @Test 389 @IncludeRunOnParentOfOrganizationOwnedProfileOwner includeRunOnParentOfOrganizationOwnedProfileOwner_isRunningOnParentOfOrganizationOwnedProfileOwnernull390 fun includeRunOnParentOfOrganizationOwnedProfileOwner_isRunningOnParentOfOrganizationOwnedProfileOwner() { 391 val dpcUser = sDeviceState.dpc().user() 392 assertThat(dpcUser.type().name()).isEqualTo(MANAGED_PROFILE_TYPE_NAME) 393 assertThat(devicePolicy().getProfileOwner(dpcUser)!!.isOrganizationOwned()).isTrue() 394 assertThat(sDeviceState.dpc().isParentInstance).isFalse() 395 } 396 397 @Test 398 @IncludeRunOnParentOfOrganizationOwnedProfileOwnerUsingParentInstance includeRunOnParentOfOrganizationOwnedProfileOwnerUsingParentInstance_isRunningOnParentOfOrganizationOwnedProfileOwnerUsingParentInstancenull399 fun includeRunOnParentOfOrganizationOwnedProfileOwnerUsingParentInstance_isRunningOnParentOfOrganizationOwnedProfileOwnerUsingParentInstance() { 400 val dpcUser = sDeviceState.dpc().user() 401 assertThat(dpcUser.type().name()).isEqualTo(MANAGED_PROFILE_TYPE_NAME) 402 assertThat(devicePolicy().getProfileOwner(dpcUser)!!.isOrganizationOwned()).isTrue() 403 assertThat(sDeviceState.dpc().isParentInstance).isTrue() 404 } 405 406 @Test 407 @IncludeRunOnParentOfProfileOwnerUsingParentInstance includeRunOnParentOfProfileOwnerUsingProfileInstanceAnnotation_runningOnParentOfProfilenull408 fun includeRunOnParentOfProfileOwnerUsingProfileInstanceAnnotation_runningOnParentOfProfile() { 409 assertThat(sDeviceState.workProfile().parent()).isEqualTo(users().instrumented()) 410 } 411 412 @Test 413 @IncludeRunOnParentOfProfileOwnerUsingParentInstance includeRunOnParentOfProfileOwnerUsingProfileInstanceAnnotation_dpcIsOnProfilenull414 fun includeRunOnParentOfProfileOwnerUsingProfileInstanceAnnotation_dpcIsOnProfile() { 415 assertThat(sDeviceState.dpc().user()).isEqualTo(sDeviceState.workProfile()) 416 } 417 418 @Test 419 @IncludeRunOnParentOfProfileOwnerUsingParentInstance includeRunOnParentOfProfileOwnerUsingProfileInstanceAnnotation_devicePolicyManagerAffectsParentnull420 fun includeRunOnParentOfProfileOwnerUsingProfileInstanceAnnotation_devicePolicyManagerAffectsParent() { 421 val previousRequiredStrongAuthTimeout = 422 sLocalDevicePolicyManager.getRequiredStrongAuthTimeout( /* admin= */null) 423 try { 424 sDeviceState.dpc().devicePolicyManager().setRequiredStrongAuthTimeout( 425 sDeviceState.dpc().componentName(), 426 TIMEOUT 427 ) 428 assertThat( 429 sLocalDevicePolicyManager.getRequiredStrongAuthTimeout( /* admin= */null) 430 ).isEqualTo(TIMEOUT) 431 } finally { 432 sDeviceState.dpc().devicePolicyManager() 433 .setRequiredStrongAuthTimeout( 434 sDeviceState.dpc().componentName(), 435 previousRequiredStrongAuthTimeout 436 ) 437 } 438 } 439 440 @Test 441 @IncludeRunOnParentOfProfileOwnerWithNoDeviceOwner includeRunOnParentOfProfileOwnerAnnotation_isRunningOnParentOfProfileOwnernull442 fun includeRunOnParentOfProfileOwnerAnnotation_isRunningOnParentOfProfileOwner() { 443 assertThat(sDeviceState.workProfile()).isNotNull() 444 } 445 446 @Test 447 @IncludeRunOnPrivateProfileAlongsideManagedProfileUsingParentInstance includeRunOnPrivateProfileAlongsideProfileOwnerUsingParentInstance_runsOnPrivateProfilenull448 fun includeRunOnPrivateProfileAlongsideProfileOwnerUsingParentInstance_runsOnPrivateProfile() { 449 assertThat(users().instrumented().type().name()).isEqualTo(PRIVATE_PROFILE_TYPE_NAME) 450 val dpcUser = sDeviceState.dpc().user() 451 assertThat(dpcUser.type().name()).isEqualTo(MANAGED_PROFILE_TYPE_NAME) 452 assertThat(devicePolicy().getProfileOwner(dpcUser)!!.isOrganizationOwned()).isFalse() 453 assertThat(sDeviceState.dpc().isParentInstance).isTrue() 454 } 455 456 @Test 457 @IncludeRunOnPrivateProfileAlongsideOrganizationOwnedProfileUsingParentInstance includeRunOnPrivateProfileAlongsideOrgOwnedProfileOwnerUsingParentInstance_runsOnPrivateProfilenull458 fun includeRunOnPrivateProfileAlongsideOrgOwnedProfileOwnerUsingParentInstance_runsOnPrivateProfile() { 459 assertThat(users().instrumented().type().name()).isEqualTo(PRIVATE_PROFILE_TYPE_NAME) 460 val dpcUser = sDeviceState.dpc().user() 461 assertThat(dpcUser.type().name()).isEqualTo(MANAGED_PROFILE_TYPE_NAME) 462 assertThat(devicePolicy().getProfileOwner(dpcUser)!!.isOrganizationOwned()).isTrue() 463 assertThat(sDeviceState.dpc().isParentInstance).isTrue() 464 } 465 466 @Test 467 @IncludeRunOnProfileOwnerProfileWithNoDeviceOwner includeRunOnProfileOwnerAnnotation_hasProfileOwnernull468 fun includeRunOnProfileOwnerAnnotation_hasProfileOwner() { 469 assertThat(devicePolicy().getProfileOwner(users().instrumented())).isNotNull() 470 } 471 472 @Test 473 @IncludeRunOnSecondaryUserInDifferentProfileGroupToProfileOwnerProfile includeRunOnSecondaryUserInDifferentProfileGroupToProfileOwnerAnnotation_isRunningOnSecondaryUserInDifferentProfileGroupToProfileOwnernull474 fun includeRunOnSecondaryUserInDifferentProfileGroupToProfileOwnerAnnotation_isRunningOnSecondaryUserInDifferentProfileGroupToProfileOwner() { 475 assertThat( 476 users().instrumented().type().name() 477 ).isEqualTo(com.android.bedstead.nene.users.UserType.SECONDARY_USER_TYPE_NAME) 478 assertThat(sDeviceState.workProfile()).isNotEqualTo(users().instrumented()) 479 assertThat(devicePolicy().getProfileOwner(sDeviceState.workProfile())).isNotNull() 480 481 // TODO(scottjonathan): Assert profile groups are different 482 } 483 484 @Test 485 @IncludeRunOnSingleDeviceOwnerUser includeRunOnSingleDeviceOwnerUserAnnotation_isRunningOnSingleDeviceOwnerUsernull486 fun includeRunOnSingleDeviceOwnerUserAnnotation_isRunningOnSingleDeviceOwnerUser() { 487 assertThat(devicePolicy().getDeviceOwner()!!.user()).isEqualTo(users().instrumented()) 488 assertThat(devicePolicy().getDeviceOwner()!!.user()).isEqualTo(users().main()) 489 } 490 491 @Test 492 @RequireHeadlessSystemUserMode(reason = "Test") 493 @IncludeRunOnSingleDeviceOwnerUser ensureOnSingleUser_headlessDeviceTypeModeIsSingleUsernull494 fun ensureOnSingleUser_headlessDeviceTypeModeIsSingleUser() { 495 assertThat(sDeviceState.dpc().user().isMain()).isTrue() 496 } 497 498 @Test 499 @IncludeRunOnSystemDeviceOwnerUser includeRunOnSystemDeviceOwnerUserAnnotation_isRunningOnSystemDeviceOwnerUsernull500 fun includeRunOnSystemDeviceOwnerUserAnnotation_isRunningOnSystemDeviceOwnerUser() { 501 assertThat(devicePolicy().getDeviceOwner()!!.user()).isEqualTo(users().instrumented()) 502 assertThat(devicePolicy().getDeviceOwner()!!.user()).isEqualTo(users().system()) 503 } 504 505 @Test 506 @IncludeRunOnUnaffiliatedDeviceOwnerSecondaryUser includeRunOnNonAffiliatedDeviceOwnerSecondaryUserAnnotation_isRunningOnNonAffiliatedDeviceOwnerSecondaryUsernull507 fun includeRunOnNonAffiliatedDeviceOwnerSecondaryUserAnnotation_isRunningOnNonAffiliatedDeviceOwnerSecondaryUser() { 508 assertThat(devicePolicy().getDeviceOwner()!!.user()).isNotEqualTo(users().instrumented()) 509 assertThat( 510 users().instrumented().type().name() 511 ).isEqualTo(com.android.bedstead.nene.users.UserType.SECONDARY_USER_TYPE_NAME) 512 } 513 514 @Test 515 @EnsureHasWorkProfile workProfile_workProfileProvided_returnsWorkProfilenull516 fun workProfile_workProfileProvided_returnsWorkProfile() { 517 assertThat(sDeviceState.workProfile()).isNotNull() 518 } 519 520 @Test 521 @EnsureHasWorkProfile profile_profileIsProvided_returnsProfilenull522 fun profile_profileIsProvided_returnsProfile() { 523 assertThat(sDeviceState.profile(MANAGED_PROFILE_TYPE_NAME)).isNotNull() 524 } 525 526 @Test 527 @EnsureHasNoWorkProfile workProfile_noWorkProfile_throwsExceptionnull528 fun workProfile_noWorkProfile_throwsException() { 529 assertThrows(IllegalStateException::class.java) { sDeviceState.workProfile() } 530 } 531 532 @Test 533 @EnsureHasNoWorkProfile 534 @EnsureHasNoDeviceOwner workProfile_createdWorkProfile_throwsExceptionnull535 fun workProfile_createdWorkProfile_throwsException() { 536 users().createUser() 537 .parent(users().instrumented()) 538 .type(users().supportedType(MANAGED_PROFILE_TYPE_NAME)) 539 .create().use { 540 assertThrows(IllegalStateException::class.java) { 541 sDeviceState.workProfile() 542 } 543 } 544 } 545 546 @Test 547 @EnsureHasWorkProfile ensureHasWorkProfileAnnotation_workProfileExistsnull548 fun ensureHasWorkProfileAnnotation_workProfileExists() { 549 assertThat(users().findProfileOfType( 550 users().supportedType(MANAGED_PROFILE_TYPE_NAME), 551 users().instrumented() 552 )).isNotNull() 553 } 554 555 // TODO(scottjonathan): test the installTestApp argument 556 // TODO(scottjonathan): When supported, test the forUser argument 557 558 @Test 559 @EnsureHasNoWorkProfile ensureHasNoWorkProfileAnnotation_workProfileDoesNotExistnull560 fun ensureHasNoWorkProfileAnnotation_workProfileDoesNotExist() { 561 assertThat(users().findProfileOfType( 562 users().supportedType(MANAGED_PROFILE_TYPE_NAME), 563 users().instrumented() 564 )).isNull() 565 } 566 567 @Test 568 @EnsureHasWorkProfile( 569 switchedToParentUser = OptionalBoolean.FALSE 570 ) // We don't test the default as it's ANY ensureHasWorkProfile_specifyNotSwitchedToParentUser_parentIsNotCurrentUsernull571 fun ensureHasWorkProfile_specifyNotSwitchedToParentUser_parentIsNotCurrentUser() { 572 assertThat(users().current()).isNotEqualTo(sDeviceState.workProfile().parent()) 573 } 574 575 @Test 576 @EnsureHasWorkProfile(switchedToParentUser = OptionalBoolean.TRUE) ensureHasWorkProfile_specifySwitchedToParentUser_parentIsCurrentUsernull577 fun ensureHasWorkProfile_specifySwitchedToParentUser_parentIsCurrentUser() { 578 assertThat(users().current()).isEqualTo(sDeviceState.workProfile().parent()) 579 } 580 581 @Test 582 @EnsureHasNoProfileOwner ensureHasNoProfileOwnerAnnotation_currentUserHasNoProfileOwnernull583 fun ensureHasNoProfileOwnerAnnotation_currentUserHasNoProfileOwner() { 584 assertThat(devicePolicy().getProfileOwner(users().instrumented())).isNull() 585 } 586 587 @Test 588 @EnsureHasNoDeviceOwner ensureHasNoDeviceOwnerAnnotation_noDeviceOwnernull589 fun ensureHasNoDeviceOwnerAnnotation_noDeviceOwner() { 590 assertThat(devicePolicy().getDeviceOwner()).isNull() 591 } 592 593 @Test 594 @EnsureHasNoDpc ensureHasNoDpcAnnotation_currentUserHasNoProfileOwnernull595 fun ensureHasNoDpcAnnotation_currentUserHasNoProfileOwner() { 596 assertThat(devicePolicy().getProfileOwner(users().instrumented())).isNull() 597 } 598 599 @Test 600 @EnsureHasNoDpc ensureHasNoDpcAnnotation_noDeviceOwnernull601 fun ensureHasNoDpcAnnotation_noDeviceOwner() { 602 assertThat(devicePolicy().getDeviceOwner()).isNull() 603 } 604 605 @Test 606 @EnsureHasNoDpc ensureHasNoDpcAnnotation_workProfileDoesNotExistnull607 fun ensureHasNoDpcAnnotation_workProfileDoesNotExist() { 608 assertThat(users().findProfileOfType( 609 users().supportedType(MANAGED_PROFILE_TYPE_NAME), 610 users().instrumented() 611 )).isNull() 612 } 613 614 @Test 615 @EnsureHasWorkProfile(isOrganizationOwned = true) ensureHasWorkProfile_isOrganizationOwned_organizationOwnedIsTruenull616 fun ensureHasWorkProfile_isOrganizationOwned_organizationOwnedIsTrue() { 617 assertThat( 618 (sDeviceState.profileOwner( 619 sDeviceState.workProfile() 620 ).devicePolicyController() as ProfileOwner).isOrganizationOwned() 621 ).isTrue() 622 } 623 624 @Test 625 @EnsureHasWorkProfile(isOrganizationOwned = false) ensureHasWorkProfile_isNotOrganizationOwned_organizationOwnedIsFalsenull626 fun ensureHasWorkProfile_isNotOrganizationOwned_organizationOwnedIsFalse() { 627 assertThat((sDeviceState.profileOwner( 628 sDeviceState.workProfile() 629 ) 630 .devicePolicyController() as ProfileOwner).isOrganizationOwned()) 631 .isFalse() 632 } 633 634 @EnsureHasWorkProfile(dpcKey = DEFAULT_DPC_KEY, dpcIsPrimary = true) 635 @AdditionalQueryParameters( 636 forTestApp = DEFAULT_DPC_KEY, 637 query = Query(targetSdkVersion = IntegerQuery(isEqualTo = 28)) 638 ) 639 @Test additionalQueryParameters_ensureHasWorkProfile_isRespectednull640 fun additionalQueryParameters_ensureHasWorkProfile_isRespected() { 641 assertThat(sDeviceState.dpc().testApp().targetSdkVersion()).isEqualTo(28) 642 } 643 644 companion object { 645 @ClassRule 646 @Rule 647 @JvmField 648 val sDeviceState = DeviceState() 649 650 private const val CLONE_PROFILE_TYPE_NAME = "android.os.usertype.profile.CLONE" 651 private const val PRIVATE_PROFILE_TYPE_NAME = "android.os.usertype.profile.PRIVATE" 652 private const val TIMEOUT: Long = 4000000 653 private val sLocalDevicePolicyManager = context() 654 .instrumentedContext() 655 .getSystemService(DevicePolicyManager::class.java)!! 656 } 657 } 658