1 /* 2 * Copyright (C) 2019 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.cts.managedprofile; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.app.UiAutomation; 22 import android.os.Process; 23 import android.os.UserHandle; 24 import android.os.UserManager; 25 import android.test.AndroidTestCase; 26 27 import androidx.test.platform.app.InstrumentationRegistry; 28 29 import java.util.HashSet; 30 import java.util.List; 31 32 public class UserManagerTest extends AndroidTestCase { 33 34 private UserManager mUserManager; 35 private UiAutomation mUiAutomation; 36 37 @Override setUp()38 protected void setUp() throws Exception { 39 super.setUp(); 40 mUserManager = mContext.getSystemService(UserManager.class); 41 mUiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation(); 42 } 43 44 @Override tearDown()45 protected void tearDown() throws Exception { 46 mUiAutomation.dropShellPermissionIdentity(); 47 super.tearDown(); 48 } 49 testGetAllProfiles()50 public void testGetAllProfiles() { 51 List<UserHandle> profiles = mUserManager.getAllProfiles(); 52 assertThat(profiles).hasSize(2); 53 assertThat(profiles).contains(Process.myUserHandle()); 54 } 55 testCreateProfile_managedProfile()56 public void testCreateProfile_managedProfile() { 57 mUiAutomation.adoptShellPermissionIdentity("android.permission.CREATE_USERS"); 58 59 UserHandle newProfile = mUserManager.createProfile("testProfile1", 60 UserManager.USER_TYPE_PROFILE_MANAGED, new HashSet<String>()); 61 assertThat(newProfile).isNotNull(); 62 63 List<UserHandle> profiles = mUserManager.getAllProfiles(); 64 assertThat(profiles).contains(newProfile); 65 } 66 67 /** This test should be run as the managed profile 68 * by com.android.cts.devicepolicy.ManagedProfileTest 69 */ testIsProfileReturnsTrue_runAsProfile()70 public void testIsProfileReturnsTrue_runAsProfile() { 71 mUiAutomation.adoptShellPermissionIdentity("android.permission.INTERACT_ACROSS_USERS"); 72 assertThat(mUserManager.isProfile()).isTrue(); 73 } 74 75 /** This test should be run as the parent profile 76 * by com.android.cts.devicepolicy.ManagedProfileTest 77 */ testIsProfileReturnsFalse_runAsPrimary()78 public void testIsProfileReturnsFalse_runAsPrimary() { 79 mUiAutomation.adoptShellPermissionIdentity("android.permission.INTERACT_ACROSS_USERS"); 80 assertThat(mUserManager.isProfile()).isFalse(); 81 } 82 } 83