1 /* 2 * Copyright 2016, 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.managedprovisioning.finalization; 18 19 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE; 20 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE; 21 import static android.app.admin.DevicePolicyManager.STATE_USER_PROFILE_COMPLETE; 22 import static android.app.admin.DevicePolicyManager.STATE_USER_SETUP_COMPLETE; 23 import static android.app.admin.DevicePolicyManager.STATE_USER_SETUP_FINALIZED; 24 import static android.app.admin.DevicePolicyManager.STATE_USER_SETUP_INCOMPLETE; 25 import static android.app.admin.DevicePolicyManager.STATE_USER_UNMANAGED; 26 import static android.content.Context.DEVICE_POLICY_SERVICE; 27 import static org.mockito.Mockito.verify; 28 import static org.mockito.Mockito.when; 29 30 import android.app.admin.DevicePolicyManager; 31 import android.content.Context; 32 import android.os.UserHandle; 33 import android.test.AndroidTestCase; 34 import android.test.suitebuilder.annotation.SmallTest; 35 36 import com.android.managedprovisioning.common.SettingsFacade; 37 import com.android.managedprovisioning.common.Utils; 38 import com.android.managedprovisioning.model.ProvisioningParams; 39 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 43 /** 44 * Unit tests for {@link UserProvisioningStateHelper}. 45 */ 46 public class UserProvisioningStateHelperTest extends AndroidTestCase { 47 private static final int PRIMARY_USER_ID = 1; 48 private static final int MANAGED_PROFILE_USER_ID = 2; 49 private static final String TEST_MDM_PACKAGE_NAME = "mdm.package.name"; 50 51 @Mock private Context mContext; 52 @Mock private DevicePolicyManager mDevicePolicyManager; 53 @Mock private Utils mUtils; 54 @Mock private SettingsFacade mSettingsFacade; 55 56 private UserProvisioningStateHelper mHelper; 57 58 public void setUp() { 59 // this is necessary for mockito to work 60 System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString()); 61 MockitoAnnotations.initMocks(this); 62 63 when(mContext.getSystemService(DEVICE_POLICY_SERVICE)).thenReturn(mDevicePolicyManager); 64 65 mHelper = new UserProvisioningStateHelper( 66 mContext, 67 mUtils, 68 mSettingsFacade, 69 PRIMARY_USER_ID); 70 } 71 72 @SmallTest 73 public void testInitiallyDone_ProfileAfterSuw() { 74 // GIVEN that we've provisioned a managed profile after SUW 75 final ProvisioningParams params = createProvisioningParams(ACTION_PROVISION_MANAGED_PROFILE, 76 false); 77 when(mSettingsFacade.isUserSetupCompleted(mContext)).thenReturn(true); 78 when(mUtils.getManagedProfile(mContext)).thenReturn(UserHandle.of(MANAGED_PROFILE_USER_ID)); 79 80 // WHEN calling markUserProvisioningStateInitiallyDone 81 mHelper.markUserProvisioningStateInitiallyDone(params); 82 83 // THEN the managed profile's state should be set to FINALIZED 84 verify(mDevicePolicyManager).setUserProvisioningState(STATE_USER_SETUP_FINALIZED, 85 MANAGED_PROFILE_USER_ID); 86 } 87 88 @SmallTest 89 public void testInitiallyDone_ProfileDuringSuw() { 90 // GIVEN that we've provisioned a managed profile during SUW 91 final ProvisioningParams params = createProvisioningParams(ACTION_PROVISION_MANAGED_PROFILE, 92 false); 93 when(mSettingsFacade.isUserSetupCompleted(mContext)).thenReturn(false); 94 when(mUtils.getManagedProfile(mContext)).thenReturn(UserHandle.of(MANAGED_PROFILE_USER_ID)); 95 96 // WHEN calling markUserProvisioningStateInitiallyDone 97 mHelper.markUserProvisioningStateInitiallyDone(params); 98 99 // THEN the managed profile's state should be set to COMPLETE 100 verify(mDevicePolicyManager).setUserProvisioningState(STATE_USER_SETUP_COMPLETE, 101 MANAGED_PROFILE_USER_ID); 102 // THEN the primary user's state should be set to PROFILE_COMPLETE 103 verify(mDevicePolicyManager).setUserProvisioningState(STATE_USER_PROFILE_COMPLETE, 104 PRIMARY_USER_ID); 105 } 106 107 @SmallTest 108 public void testInitiallyDone_DeviceOwnerSkipUserSetup() { 109 // GIVEN that we've provisioned a device owner with skip user setup true 110 final ProvisioningParams params = createProvisioningParams(ACTION_PROVISION_MANAGED_DEVICE, 111 true); 112 when(mSettingsFacade.isUserSetupCompleted(mContext)).thenReturn(false); 113 114 // WHEN calling markUserProvisioningStateInitiallyDone 115 mHelper.markUserProvisioningStateInitiallyDone(params); 116 117 // THEN the primary user's state should be set to COMPLETE 118 verify(mDevicePolicyManager).setUserProvisioningState(STATE_USER_SETUP_COMPLETE, 119 PRIMARY_USER_ID); 120 } 121 122 @SmallTest 123 public void testInitiallyDone_DeviceOwnerDontSkipUserSetup() { 124 // GIVEN that we've provisioned a device owner with skip user setup false 125 final ProvisioningParams params = createProvisioningParams(ACTION_PROVISION_MANAGED_DEVICE, 126 false); 127 when(mSettingsFacade.isUserSetupCompleted(mContext)).thenReturn(false); 128 129 // WHEN calling markUserProvisioningStateInitiallyDone 130 mHelper.markUserProvisioningStateInitiallyDone(params); 131 132 // THEN the primary user's state should be set to INCOMPLETE 133 verify(mDevicePolicyManager).setUserProvisioningState(STATE_USER_SETUP_INCOMPLETE, 134 PRIMARY_USER_ID); 135 } 136 137 @SmallTest 138 public void testFinalized_ManagedProfile() { 139 // GIVEN that we've provisioned a managed profile 140 final ProvisioningParams params = createProvisioningParams(ACTION_PROVISION_MANAGED_PROFILE, 141 false); 142 when(mUtils.getManagedProfile(mContext)).thenReturn(UserHandle.of(MANAGED_PROFILE_USER_ID)); 143 144 // WHEN calling markUserProvisioningStateFinalized 145 mHelper.markUserProvisioningStateFinalized(params); 146 147 // THEN the managed profile's state should be set to FINALIZED 148 verify(mDevicePolicyManager).setUserProvisioningState(STATE_USER_SETUP_FINALIZED, 149 MANAGED_PROFILE_USER_ID); 150 // THEN the primary user's state should be set to UNMANAGED 151 verify(mDevicePolicyManager).setUserProvisioningState(STATE_USER_UNMANAGED, 152 PRIMARY_USER_ID); 153 } 154 155 @SmallTest 156 public void testFinalized_DeviceOwner() { 157 // GIVEN that we've provisioned a device owner with skip user setup false 158 final ProvisioningParams params = createProvisioningParams(ACTION_PROVISION_MANAGED_DEVICE, 159 false); 160 161 // WHEN calling markUserProvisioningStateFinalized 162 mHelper.markUserProvisioningStateFinalized(params); 163 164 // THEN the primary user's state should be set to FINALIZED 165 verify(mDevicePolicyManager).setUserProvisioningState(STATE_USER_SETUP_FINALIZED, 166 PRIMARY_USER_ID); 167 } 168 169 @SmallTest 170 public void testIsStateUnmanagedOrFinalized() { 171 assertTrue(isStateUnmanagedOrFinalizedWithCurrentState(STATE_USER_UNMANAGED)); 172 assertTrue(isStateUnmanagedOrFinalizedWithCurrentState(STATE_USER_SETUP_FINALIZED)); 173 assertFalse(isStateUnmanagedOrFinalizedWithCurrentState(STATE_USER_PROFILE_COMPLETE)); 174 assertFalse(isStateUnmanagedOrFinalizedWithCurrentState(STATE_USER_SETUP_INCOMPLETE)); 175 assertFalse(isStateUnmanagedOrFinalizedWithCurrentState(STATE_USER_PROFILE_COMPLETE)); 176 } 177 178 private boolean isStateUnmanagedOrFinalizedWithCurrentState(int currentState) { 179 when(mDevicePolicyManager.getUserProvisioningState()).thenReturn(currentState); 180 return mHelper.isStateUnmanagedOrFinalized(); 181 } 182 183 private ProvisioningParams createProvisioningParams(String action, boolean skipUserSetup) { 184 return new ProvisioningParams.Builder() 185 .setDeviceAdminPackageName(TEST_MDM_PACKAGE_NAME) 186 .setProvisioningAction(action) 187 .setSkipUserSetup(skipUserSetup) 188 .build(); 189 } 190 } 191