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.managedprovisioning.common 17 18 import android.app.Activity 19 import android.app.Service 20 import android.app.admin.DevicePolicyManager 21 import android.content.ComponentName 22 import android.content.Context 23 import android.content.Intent 24 import android.content.pm.ResolveInfo 25 import android.os.UserHandle 26 import androidx.test.core.app.ApplicationProvider 27 import com.android.managedprovisioning.analytics.MetricsWriterFactory 28 import com.android.managedprovisioning.analytics.ProvisioningAnalyticsTracker 29 import com.android.managedprovisioning.finalization.SendDpcBroadcastService 30 import com.android.managedprovisioning.model.ProvisioningParams 31 import com.google.common.truth.Truth.assertThat 32 import org.junit.Ignore 33 import org.junit.Test 34 import org.junit.runner.RunWith 35 import org.robolectric.Robolectric 36 import org.robolectric.RobolectricTestRunner 37 import org.robolectric.Shadows.shadowOf 38 39 @RunWith(RobolectricTestRunner::class) 40 class PolicyComplianceUtilsTest { 41 private val mContext = ApplicationProvider.getApplicationContext<Context>() 42 private val mPolicyComplianceUtils = PolicyComplianceUtils() 43 private val mUtils = Utils() 44 private val mManagedProvisioningSharedPreferences = ManagedProvisioningSharedPreferences(mContext) 45 private val mProvisioningAnalyticsTracker = ProvisioningAnalyticsTracker( 46 MetricsWriterFactory.getMetricsWriter(mContext, SettingsFacade()), 47 mManagedProvisioningSharedPreferences) 48 private val mTransitionHelper = TransitionHelper() 49 50 @Test isPolicyComplianceActivityResolvableForSystemUser_activityExists_returnsTruenull51 fun isPolicyComplianceActivityResolvableForSystemUser_activityExists_returnsTrue(): Unit { 52 val intent = createPolicyComplianceIntent() 53 shadowOf(mContext.packageManager).addResolveInfoForIntent(intent, ResolveInfo()) 54 val result = mPolicyComplianceUtils.isPolicyComplianceActivityResolvableForUser( 55 mContext, 56 createTrustedSourceParamsBuilder().build(), 57 mUtils, 58 UserHandle.SYSTEM) 59 assertThat(result).isTrue() 60 } 61 62 @Test isPolicyComplianceActivityResolvableForSystemUser_activityExists_returnsFalsenull63 fun isPolicyComplianceActivityResolvableForSystemUser_activityExists_returnsFalse(): Unit { 64 val result = mPolicyComplianceUtils.isPolicyComplianceActivityResolvableForUser( 65 mContext, 66 createTrustedSourceParamsBuilder().build(), 67 mUtils, 68 UserHandle.SYSTEM) 69 assertThat(result).isFalse() 70 } 71 72 @Ignore("b/218480743") 73 @Test startPolicyComplianceActivityForResultIfResolved_activityExists_isStartednull74 fun startPolicyComplianceActivityForResultIfResolved_activityExists_isStarted() { 75 val intent = createPolicyComplianceIntent() 76 shadowOf(mContext.packageManager).addResolveInfoForIntent(intent, ResolveInfo()) 77 val parentActivity = Robolectric.buildActivity(Activity::class.java).create().get() 78 val result = mPolicyComplianceUtils.startPolicyComplianceActivityForResultIfResolved( 79 parentActivity, 80 createTrustedSourceParamsBuilder().build(), 81 POLICY_COMPLIANCE_ACTIVITY_REQUEST_CODE, 82 mUtils, 83 mProvisioningAnalyticsTracker, 84 mTransitionHelper) 85 val startedIntent = shadowOf(parentActivity).peekNextStartedActivity() 86 assertThat(startedIntent.action).isEqualTo(DevicePolicyManager.ACTION_ADMIN_POLICY_COMPLIANCE) 87 assertThat(startedIntent.getPackage()).isEqualTo(TEST_MDM_PACKAGE_NAME) 88 assertThat(startedIntent.flags).isEqualTo(NO_FLAGS) 89 assertThat(result).isTrue() 90 } 91 92 @Ignore("b/218480743") 93 @Test startPolicyComplianceActivityForResultIfResolved_activityDoesNotExist_notStartednull94 fun startPolicyComplianceActivityForResultIfResolved_activityDoesNotExist_notStarted() { 95 val parentActivity = Robolectric.buildActivity(Activity::class.java).create().get() 96 val result = mPolicyComplianceUtils.startPolicyComplianceActivityForResultIfResolved( 97 parentActivity, 98 createTrustedSourceParamsBuilder().build(), 99 POLICY_COMPLIANCE_ACTIVITY_REQUEST_CODE, 100 mUtils, 101 mProvisioningAnalyticsTracker, 102 mTransitionHelper) 103 val startedIntent = shadowOf(parentActivity).peekNextStartedActivity() 104 assertThat(startedIntent).isNull() 105 assertThat(result).isFalse() 106 } 107 108 @Ignore("b/218480743") 109 @Test startPolicyComplianceActivityIfResolved_activityExists_isStartednull110 fun startPolicyComplianceActivityIfResolved_activityExists_isStarted() { 111 val intent = createPolicyComplianceIntent() 112 shadowOf(mContext.packageManager).addResolveInfoForIntent(intent, ResolveInfo()) 113 val parentActivity = Robolectric.buildActivity(Activity::class.java).create().get() 114 val result = mPolicyComplianceUtils.startPolicyComplianceActivityIfResolved( 115 parentActivity, 116 createTrustedSourceParamsBuilder().build(), 117 mUtils, 118 mProvisioningAnalyticsTracker) 119 val startedIntent = shadowOf(parentActivity).peekNextStartedActivity() 120 assertThat(startedIntent.action).isEqualTo(DevicePolicyManager.ACTION_ADMIN_POLICY_COMPLIANCE) 121 assertThat(startedIntent.getPackage()).isEqualTo(TEST_MDM_PACKAGE_NAME) 122 assertThat(startedIntent.flags).isEqualTo(NO_FLAGS) 123 assertThat(result).isTrue() 124 } 125 126 @Ignore("b/218480743") 127 @Test startPolicyComplianceActivityIfResolved_activityDoesNotExist_notStartednull128 fun startPolicyComplianceActivityIfResolved_activityDoesNotExist_notStarted() { 129 val parentActivity = Robolectric.buildActivity(Activity::class.java).create().get() 130 val result = mPolicyComplianceUtils.startPolicyComplianceActivityIfResolved( 131 parentActivity, 132 createTrustedSourceParamsBuilder().build(), 133 mUtils, 134 mProvisioningAnalyticsTracker) 135 val startedIntent = shadowOf(parentActivity).peekNextStartedActivity() 136 assertThat(startedIntent).isNull() 137 assertThat(result).isFalse() 138 } 139 140 @Test startPolicyComplianceActivityIfResolved_fromNonActivityContext_isStartedWithNewTaskFlagnull141 fun startPolicyComplianceActivityIfResolved_fromNonActivityContext_isStartedWithNewTaskFlag() { 142 val intent = createPolicyComplianceIntent() 143 shadowOf(mContext.packageManager).addResolveInfoForIntent(intent, ResolveInfo()) 144 val service: Service = Robolectric.buildService(SendDpcBroadcastService::class.java).create().get() 145 val result = mPolicyComplianceUtils.startPolicyComplianceActivityIfResolved( 146 service, 147 createTrustedSourceParamsBuilder().build(), 148 mUtils, 149 mProvisioningAnalyticsTracker) 150 val startedIntent = shadowOf(service).peekNextStartedActivity() 151 assertThat(startedIntent.action).isEqualTo(DevicePolicyManager.ACTION_ADMIN_POLICY_COMPLIANCE) 152 assertThat(startedIntent.getPackage()).isEqualTo(TEST_MDM_PACKAGE_NAME) 153 assertThat(startedIntent.flags).isEqualTo(Intent.FLAG_ACTIVITY_NEW_TASK) 154 assertThat(result).isTrue() 155 } 156 157 @Test isPolicyComplianceActivityResolvableForManagedUser_withSystemUser_activityExists_returnsTruenull158 fun isPolicyComplianceActivityResolvableForManagedUser_withSystemUser_activityExists_returnsTrue(): Unit { 159 val intent = createPolicyComplianceIntent() 160 shadowOf(mContext.packageManager).addResolveInfoForIntent(intent, ResolveInfo()) 161 val result = mPolicyComplianceUtils.isPolicyComplianceActivityResolvableForManagedUser( 162 mContext, 163 createTrustedSourceParamsBuilder().build(), 164 mUtils) 165 assertThat(result).isTrue() 166 } 167 168 @Test isPolicyComplianceActivityResolvableForManagedUser_withSystemUser_activityDoesNotExist_returnsFalsenull169 fun isPolicyComplianceActivityResolvableForManagedUser_withSystemUser_activityDoesNotExist_returnsFalse() { 170 val result = mPolicyComplianceUtils.isPolicyComplianceActivityResolvableForManagedUser( 171 mContext, 172 createTrustedSourceParamsBuilder().build(), 173 mUtils) 174 assertThat(result).isFalse() 175 } 176 createPolicyComplianceIntentnull177 private fun createPolicyComplianceIntent(): Intent { 178 val intent = Intent(DevicePolicyManager.ACTION_ADMIN_POLICY_COMPLIANCE) 179 intent.setPackage(TEST_MDM_PACKAGE_NAME) 180 return intent 181 } 182 183 companion object { 184 private const val TEST_MDM_PACKAGE_NAME = "mdm.package.name" 185 private const val TEST_MDM_ADMIN_RECEIVER = TEST_MDM_PACKAGE_NAME + ".AdminReceiver" 186 private val TEST_MDM_ADMIN = ComponentName(TEST_MDM_PACKAGE_NAME, 187 TEST_MDM_ADMIN_RECEIVER) 188 private const val POLICY_COMPLIANCE_ACTIVITY_REQUEST_CODE = 123 189 private const val NO_FLAGS = 0 createTrustedSourceParamsBuildernull190 private fun createTrustedSourceParamsBuilder(): ProvisioningParams.Builder { 191 return ProvisioningParams.Builder.builder() 192 .setProvisioningAction(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE) 193 .setDeviceAdminComponentName(TEST_MDM_ADMIN) 194 .setStartedByTrustedSource(true) 195 } 196 } 197 } 198