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