1 /* 2 * Copyright 2018, 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.managedprovisioning.finalization; 17 18 import static android.app.admin.DeviceAdminReceiver.ACTION_PROFILE_PROVISIONING_COMPLETE; 19 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE; 20 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE; 21 import static com.google.common.truth.Truth.assertThat; 22 import static org.mockito.ArgumentMatchers.any; 23 import static org.mockito.ArgumentMatchers.anyInt; 24 import static org.mockito.ArgumentMatchers.eq; 25 import static org.mockito.Mockito.never; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.when; 28 29 import android.content.ComponentName; 30 import android.content.Context; 31 import android.content.Intent; 32 import android.os.PersistableBundle; 33 import com.android.managedprovisioning.common.IllegalProvisioningArgumentException; 34 import com.android.managedprovisioning.common.Utils; 35 import com.android.managedprovisioning.model.ProvisioningParams; 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 41 /** 42 * Unit tests for {@link ProvisioningIntentProvider}. 43 */ 44 public class ProvisioningIntentProviderTest { 45 46 private static final String DEVICE_ADMIN_PACKAGE_NAME = "com.example.package"; 47 private static final ComponentName DEVICE_ADMIN_COMPONENT_NAME = 48 new ComponentName(DEVICE_ADMIN_PACKAGE_NAME, "com.android.AdminReceiver"); 49 private static final PersistableBundle ADMIN_EXTRAS_BUNDLE = 50 PersistableBundle.forPair("test_key", "test_value"); 51 52 private ProvisioningIntentProvider mProvisioningIntentProvider; 53 private ProvisioningParams mParams; 54 @Mock private Context mContext; 55 @Mock private Utils mUtils; 56 57 @Before setUp()58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 mProvisioningIntentProvider = new ProvisioningIntentProvider(); 61 mParams = new ProvisioningParams.Builder() 62 .setDeviceAdminComponentName(DEVICE_ADMIN_COMPONENT_NAME) 63 .setProvisioningAction(ACTION_PROVISION_MANAGED_PROFILE) 64 .setAdminExtrasBundle(ADMIN_EXTRAS_BUNDLE) 65 .build(); 66 } 67 68 @Test maybeLaunchDpc_success()69 public void maybeLaunchDpc_success() { 70 when(mUtils.canResolveIntentAsUser(any(), any(), anyInt())).thenReturn(true); 71 72 mProvisioningIntentProvider.maybeLaunchDpc(mParams, 0, mUtils, mContext); 73 74 verify(mContext).startActivityAsUser(any(), any()); 75 } 76 77 @Test maybeLaunchDpc_cannotResolveIntent()78 public void maybeLaunchDpc_cannotResolveIntent() { 79 when(mUtils.canResolveIntentAsUser(any(), any(), anyInt())).thenReturn(false); 80 81 mProvisioningIntentProvider.maybeLaunchDpc(mParams, 0, mUtils, mContext); 82 83 verify(mContext, never()).startActivityAsUser(any(), any()); 84 } 85 86 @Test createProvisioningIntent_success()87 public void createProvisioningIntent_success() { 88 Intent intent = mProvisioningIntentProvider 89 .createProvisioningCompleteIntent(mParams, 0, mUtils, mContext); 90 91 assertThat(intent.getAction()).isEqualTo(ACTION_PROFILE_PROVISIONING_COMPLETE); 92 assertThat(intent.getComponent()).isEqualTo(DEVICE_ADMIN_COMPONENT_NAME); 93 assertThat(intent.<PersistableBundle>getParcelableExtra( 94 EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE)).isEqualTo(ADMIN_EXTRAS_BUNDLE); 95 } 96 97 @Test createProvisioningIntent_cannotFindAdmin()98 public void createProvisioningIntent_cannotFindAdmin() 99 throws IllegalProvisioningArgumentException { 100 ProvisioningParams provisioningParams = new ProvisioningParams.Builder() 101 .setDeviceAdminPackageName(DEVICE_ADMIN_PACKAGE_NAME) 102 .setProvisioningAction(ACTION_PROVISION_MANAGED_PROFILE) 103 .build(); 104 when(mUtils.findDeviceAdmin(eq(DEVICE_ADMIN_PACKAGE_NAME), any(), any(), anyInt())) 105 .thenThrow(IllegalProvisioningArgumentException.class); 106 107 assertThat(mProvisioningIntentProvider.createProvisioningCompleteIntent( 108 provisioningParams, 0, mUtils, mContext)).isNull(); 109 } 110 } 111