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 package com.android.cts.deviceowner; 17 18 import static com.android.compatibility.common.util.SystemUtil.callWithShellPermissionIdentity; 19 import static com.android.compatibility.common.util.SystemUtil.eventually; 20 21 import static com.google.common.truth.Truth.assertWithMessage; 22 23 import android.annotation.UserIdInt; 24 import android.app.admin.DevicePolicyManager; 25 import android.content.ComponentName; 26 import android.content.Context; 27 import android.content.pm.UserInfo; 28 import android.os.UserHandle; 29 import android.os.UserManager; 30 import android.util.Log; 31 32 //TODO(b/174859111): move to automotive specific module 33 /** 34 * Device owner tests specific for devices that use 35 * {@link android.os.UserManager#isHeadlessSystemUserMode()}. 36 */ 37 public final class HeadlessSystemUserTest extends BaseDeviceOwnerTest { 38 39 private static final String TAG = HeadlessSystemUserTest.class.getSimpleName(); 40 41 // To be used in cases where it needs to test the DPM of the current user (as 42 // mDevicePolicyManager wraps calls to user 0's DeviceOwner DPM); 43 private DevicePolicyManager mCurrentUserDpm; 44 45 private UserManager mUserManager; 46 47 @Override setUp()48 protected void setUp() throws Exception { 49 super.setUp(); 50 51 mCurrentUserDpm = mContext.getSystemService(DevicePolicyManager.class); 52 mUserManager = mContext.getSystemService(UserManager.class); 53 54 Log.d(TAG, "setUp(): userId=" + mUserId); 55 56 } 57 testProfileOwnerIsSetOnCurrentUser()58 public void testProfileOwnerIsSetOnCurrentUser() { 59 ComponentName admin = mCurrentUserDpm.getProfileOwner(); 60 61 assertProfileOwner(admin, mUserId); 62 } 63 testProfileOwnerIsSetOnNewUser()64 public void testProfileOwnerIsSetOnNewUser() throws Exception { 65 UserInfo user = null; 66 try { 67 user = callWithShellPermissionIdentity(() -> mUserManager 68 .createUser("testProfileOwnerIsSetOnNewUser", /* flags= */ 0)); 69 assertWithMessage("new user").that(user).isNotNull(); 70 Log.d(TAG, "Created user " + user.toFullString()); 71 final int userId = user.id; 72 73 // Must try a couple times as PO is asynchronously set after user is created. 74 // TODO(b/178102911): use a callback instead 75 eventually(() -> { 76 Context newUserContext = mContext.createContextAsUser(UserHandle.of(userId), 77 /* flags=*/ 0); 78 DevicePolicyManager newUserDpm = newUserContext 79 .getSystemService(DevicePolicyManager.class); 80 assertProfileOwner(newUserDpm.getProfileOwner(), userId); 81 }); 82 83 } finally { 84 if (user != null) { 85 final int userId = user.id; 86 Log.d(TAG, "Removing user " + userId); 87 boolean removed = callWithShellPermissionIdentity( 88 () -> mUserManager.removeUser(userId)); 89 assertWithMessage("user %s removed", userId).that(removed).isTrue(); 90 } 91 } 92 } 93 assertProfileOwner(ComponentName admin, @UserIdInt int userId)94 private void assertProfileOwner(ComponentName admin, @UserIdInt int userId) { 95 assertWithMessage("Component %s is profile owner for user %s", admin, userId) 96 .that(admin).isEqualTo(getWho()); 97 } 98 } 99