1 /* 2 * Copyright (C) 2017 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.comp; 17 18 import android.app.admin.DevicePolicyManager; 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.os.UserHandle; 22 import android.os.UserManager; 23 import android.test.AndroidTestCase; 24 import android.test.MoreAsserts; 25 import java.util.List; 26 27 public class ManagementTest extends AndroidTestCase { 28 29 private DevicePolicyManager mDevicePolicyManager; 30 31 @Override 32 protected void setUp() throws Exception { 33 super.setUp(); 34 mDevicePolicyManager = (DevicePolicyManager) 35 mContext.getSystemService(Context.DEVICE_POLICY_SERVICE); 36 } 37 38 public void testIsManagedProfile() { 39 assertNotNull(mDevicePolicyManager); 40 assertTrue(mDevicePolicyManager.isAdminActive( 41 AdminReceiver.getComponentName(getContext()))); 42 assertTrue(mDevicePolicyManager.isProfileOwnerApp(getContext().getPackageName())); 43 assertTrue(mDevicePolicyManager.isManagedProfile( 44 AdminReceiver.getComponentName(getContext()))); 45 } 46 47 public void testIsDeviceOwner() { 48 assertNotNull(mDevicePolicyManager); 49 assertTrue(mDevicePolicyManager.isAdminActive( 50 AdminReceiver.getComponentName(getContext()))); 51 assertTrue(mDevicePolicyManager.isDeviceOwnerApp(getContext().getPackageName())); 52 assertFalse(mDevicePolicyManager.isManagedProfile( 53 AdminReceiver.getComponentName(getContext()))); 54 } 55 56 /** 57 * Assumes that the managed profile is enabled. 58 * Otherwise, {@link Utils#getOtherProfile} won't return a profile. 59 */ 60 public void testOtherProfilesEqualsBindTargetUsers() { 61 UserHandle otherProfile = Utils.getOtherProfile(mContext); 62 assertNotNull(otherProfile); 63 64 List<UserHandle> allowedTargetUsers = mDevicePolicyManager.getBindDeviceAdminTargetUsers( 65 AdminReceiver.getComponentName(mContext)); 66 assertEquals(1, allowedTargetUsers.size()); 67 assertEquals(otherProfile, allowedTargetUsers.get(0)); 68 } 69 70 public void testProvisionManagedProfileAllowed() { 71 assertTrue(mDevicePolicyManager.isProvisioningAllowed( 72 DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE)); 73 } 74 75 public void testProvisionManagedProfileNotAllowed() { 76 assertFalse(mDevicePolicyManager.isProvisioningAllowed( 77 DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE)); 78 } 79 80 public void testWipeData() { 81 mDevicePolicyManager.wipeData(0); 82 } 83 84 public void testCanRemoveManagedProfile() { 85 UserHandle profileUserHandle = Utils.getOtherProfile(mContext); 86 assertNotNull(profileUserHandle); 87 assertTrue(mDevicePolicyManager.removeUser(AdminReceiver.getComponentName(mContext), 88 profileUserHandle)); 89 } 90 91 public void testCreateSecondaryUser() throws Exception { 92 ComponentName admin = AdminReceiver.getComponentName(mContext); 93 assertNotNull(mDevicePolicyManager.createAndManageUser(admin, "secondary-user", 94 admin, null, DevicePolicyManager.SKIP_SETUP_WIZARD)); 95 } 96 97 public void testNoBindDeviceAdminTargetUsers() { 98 MoreAsserts.assertEmpty(mDevicePolicyManager.getBindDeviceAdminTargetUsers( 99 AdminReceiver.getComponentName(mContext))); 100 } 101 102 public void testCannotStartManagedProfileInBackground() { 103 UserHandle profileUserHandle = Utils.getOtherProfile(mContext); 104 assertNotNull(profileUserHandle); 105 assertEquals(UserManager.USER_OPERATION_ERROR_MANAGED_PROFILE, 106 mDevicePolicyManager.startUserInBackground(AdminReceiver.getComponentName(mContext), 107 profileUserHandle)); 108 } 109 110 public void testCannotStopManagedProfile() { 111 UserHandle profileUserHandle = Utils.getOtherProfile(mContext); 112 assertNotNull(profileUserHandle); 113 assertEquals(UserManager.USER_OPERATION_ERROR_MANAGED_PROFILE, 114 mDevicePolicyManager.stopUser(AdminReceiver.getComponentName(mContext), 115 profileUserHandle)); 116 } 117 118 public void testCannotLogoutManagedProfile() { 119 assertEquals(UserManager.USER_OPERATION_ERROR_MANAGED_PROFILE, 120 mDevicePolicyManager.logoutUser(AdminReceiver.getComponentName(mContext))); 121 } 122 } 123