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_ROLE_HOLDER_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE;
20 import static android.app.admin.DevicePolicyManager.EXTRA_ROLE_HOLDER_PROVISIONING_INITIATOR_PACKAGE;
21 import static android.app.admin.DevicePolicyManager.EXTRA_ROLE_HOLDER_STATE;
22 
23 import static com.android.managedprovisioning.TestUtils.assertIntentsEqual;
24 
25 import static com.google.common.truth.Truth.assertThat;
26 
27 import static org.junit.Assert.assertThrows;
28 
29 import android.app.admin.DevicePolicyManager;
30 import android.content.Context;
31 import android.content.Intent;
32 import android.os.Bundle;
33 import android.os.PersistableBundle;
34 
35 import androidx.test.core.app.ApplicationProvider;
36 import androidx.test.filters.SmallTest;
37 
38 import com.google.android.setupcompat.util.WizardManagerHelper;
39 
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.junit.runners.JUnit4;
45 
46 import java.util.HashSet;
47 import java.util.Set;
48 
49 
50 @SmallTest
51 @RunWith(JUnit4.class)
52 public class DeviceManagementRoleHolderHelperTest {
53     private static final String ROLE_HOLDER_PACKAGE_NAME = "com.test.package";
54     private static final String ROLE_HOLDER_EMPTY_PACKAGE_NAME = "";
55     private static final String ROLE_HOLDER_NULL_PACKAGE_NAME = null;
56     private static final PersistableBundle TEST_ROLE_HOLDER_STATE = new PersistableBundle();
57     private static final String TEST_CALLING_PACKAGE = "test.calling.package";
58     private static final Bundle TEST_ADDITIONAL_EXTRAS = new Bundle();
59     private final Context mContext = ApplicationProvider.getApplicationContext();
60     public static final String TEST_EXTRA_KEY = "test_extra_key";
61     public static final String TEST_EXTRA_VALUE = "test_extra_value";
62     private static final Intent MANAGED_PROFILE_INTENT =
63             new Intent(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE)
64                     .putExtra(TEST_EXTRA_KEY, TEST_EXTRA_VALUE);
65     private static final Intent MANAGED_PROFILE_ROLE_HOLDER_INTENT =
66             createManagedProfileRoleHolderIntent();
67     private static final Intent MANAGED_PROFILE_ROLE_HOLDER_INTENT_WITH_MINIMAL_EXTRAS =
68             createManagedProfileRoleHolderIntentWithMinimalExtras();
69     private static final Intent PROVISION_TRUSTED_SOURCE_INTENT =
70             new Intent(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE)
71                     .putExtra(TEST_EXTRA_KEY, TEST_EXTRA_VALUE);
72     private static final Intent PROVISION_TRUSTED_SOURCE_ROLE_HOLDER_INTENT =
73             createTrustedSourceRoleHolderIntent();
74     private static final Intent PROVISION_TRUSTED_SOURCE_ROLE_HOLDER_INTENT_WITH_MINIMAL_EXTRAS =
75             createTrustedSourceRoleHolderIntentWithMinimalExtras();
76     private static final Intent FINANCED_DEVICE_INTENT =
77             new Intent(DevicePolicyManager.ACTION_PROVISION_FINANCED_DEVICE)
78                     .putExtra(TEST_EXTRA_KEY, TEST_EXTRA_VALUE);
79     private static final Intent PROVISION_FINALIZATION_INTENT =
80             new Intent(DevicePolicyManager.ACTION_PROVISION_FINALIZATION);
81     private static final Intent PROVISION_FINALIZATION_ROLE_HOLDER_INTENT =
82             new Intent(DevicePolicyManager.ACTION_ROLE_HOLDER_PROVISION_FINALIZATION)
83                     .setPackage(ROLE_HOLDER_PACKAGE_NAME);
84     private static final Intent MANAGED_PROVISIONING_INTENT = MANAGED_PROFILE_INTENT;
85     private static final Intent INVALID_MANAGED_PROVISIONING_INTENT =
86             new Intent("action.intent.test");
87     private Set<String> mRoleHolderHandledIntents;
88     private boolean mCanDelegateProvisioningToRoleHolder;
89     private final FeatureFlagChecker mFeatureFlagChecker =
90             () -> mCanDelegateProvisioningToRoleHolder;
91 
92     @Before
setUp()93     public void setUp() {
94         enableRoleHolderDelegation();
95         mRoleHolderHandledIntents = createRoleHolderRequiredIntentActionsSet();
96     }
97 
98     @After
tearDown()99     public void tearDown() {
100         disableRoleHolderDelegation();
101     }
102 
103     @Test
roleHolderHelperConstructor_roleHolderPackageNameEmpty_noExceptionThrown()104     public void roleHolderHelperConstructor_roleHolderPackageNameEmpty_noExceptionThrown() {
105         createRoleHolderHelper(ROLE_HOLDER_EMPTY_PACKAGE_NAME);
106     }
107 
108     @Test
isRoleHolderReadyForProvisioning_works()109     public void isRoleHolderReadyForProvisioning_works() {
110         DeviceManagementRoleHolderHelper roleHolderHelper = createRoleHolderHelper();
111 
112         assertThat(roleHolderHelper.isRoleHolderReadyForProvisioning(
113                 mContext, MANAGED_PROVISIONING_INTENT)).isTrue();
114     }
115 
116     @Test
isRoleHolderReadyForProvisioning_emptyRoleHolderPackageName_isFalse()117     public void isRoleHolderReadyForProvisioning_emptyRoleHolderPackageName_isFalse() {
118         DeviceManagementRoleHolderHelper roleHolderHelper =
119                 createRoleHolderHelper(ROLE_HOLDER_EMPTY_PACKAGE_NAME);
120 
121         assertThat(roleHolderHelper.isRoleHolderReadyForProvisioning(
122                 mContext, MANAGED_PROVISIONING_INTENT)).isFalse();
123     }
124 
125     @Test
isRoleHolderReadyForProvisioning_roleHolderDelegationDisabled_returnsFalse()126     public void isRoleHolderReadyForProvisioning_roleHolderDelegationDisabled_returnsFalse() {
127         DeviceManagementRoleHolderHelper roleHolderHelper = createRoleHolderHelper();
128         disableRoleHolderDelegation();
129 
130         assertThat(roleHolderHelper.isRoleHolderReadyForProvisioning(
131                 mContext, MANAGED_PROVISIONING_INTENT)).isFalse();
132     }
133 
134     @Test
isRoleHolderReadyForProvisioning_roleHolderNotInstalled_returnsFalse()135     public void isRoleHolderReadyForProvisioning_roleHolderNotInstalled_returnsFalse() {
136         DeviceManagementRoleHolderHelper roleHolderHelper =
137                 createRoleHolderHelperWithRoleHolderNotInstalled();
138 
139         assertThat(roleHolderHelper.isRoleHolderReadyForProvisioning(
140                 mContext, MANAGED_PROVISIONING_INTENT)).isFalse();
141     }
142 
143     @Test
isRoleHolderReadyForProvisioning_roleHolderStub_returnsFalse()144     public void isRoleHolderReadyForProvisioning_roleHolderStub_returnsFalse() {
145         DeviceManagementRoleHolderHelper roleHolderHelper =
146                 createRoleHolderHelperWithStubRoleHolder();
147 
148         assertThat(roleHolderHelper.isRoleHolderReadyForProvisioning(
149                 mContext, MANAGED_PROVISIONING_INTENT)).isFalse();
150     }
151 
152     @Test
isRoleHolderReadyForProvisioning_roleHolderInvalid_returnsFalse()153     public void isRoleHolderReadyForProvisioning_roleHolderInvalid_returnsFalse() {
154         DeviceManagementRoleHolderHelper roleHolderHelper =
155                 createRoleHolderHelperWithInvalidRoleHolder();
156 
157         assertThat(roleHolderHelper.isRoleHolderReadyForProvisioning(
158                 mContext, MANAGED_PROVISIONING_INTENT)).isFalse();
159     }
160 
161     @Test
isRoleHolderReadyForProvisioning_roleHolderResolvesRequiredIntents_returnsTrue()162     public void isRoleHolderReadyForProvisioning_roleHolderResolvesRequiredIntents_returnsTrue() {
163         DeviceManagementRoleHolderHelper roleHolderHelper =
164                 createRoleHolderHelperWithRoleHolderResolvesRequiredIntents(
165                         mRoleHolderHandledIntents);
166 
167         assertThat(roleHolderHelper.isRoleHolderReadyForProvisioning(
168                 mContext, MANAGED_PROVISIONING_INTENT)).isTrue();
169     }
170 
171     @Test
isRoleHolderReadyForProvisioning_roleHolderResolvesRequiredIntentsExceptManagedProfile_returnsFalse()172     public void isRoleHolderReadyForProvisioning_roleHolderResolvesRequiredIntentsExceptManagedProfile_returnsFalse() {
173         mRoleHolderHandledIntents.remove(
174                 DevicePolicyManager.ACTION_ROLE_HOLDER_PROVISION_MANAGED_PROFILE);
175         DeviceManagementRoleHolderHelper roleHolderHelper =
176                 createRoleHolderHelperWithRoleHolderResolvesRequiredIntents(
177                         mRoleHolderHandledIntents);
178 
179         assertThat(roleHolderHelper.isRoleHolderReadyForProvisioning(
180                 mContext, MANAGED_PROVISIONING_INTENT)).isFalse();
181     }
182 
183     @Test
isRoleHolderReadyForProvisioning_roleHolderResolvesRequiredIntentsExceptTrustedSource_returnsFalse()184     public void isRoleHolderReadyForProvisioning_roleHolderResolvesRequiredIntentsExceptTrustedSource_returnsFalse() {
185         mRoleHolderHandledIntents.remove(
186                 ACTION_ROLE_HOLDER_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE);
187         DeviceManagementRoleHolderHelper roleHolderHelper =
188                 createRoleHolderHelperWithRoleHolderResolvesRequiredIntents(
189                         mRoleHolderHandledIntents);
190 
191         assertThat(roleHolderHelper.isRoleHolderReadyForProvisioning(
192                 mContext, MANAGED_PROVISIONING_INTENT)).isFalse();
193     }
194 
195     @Test
isRoleHolderReadyForProvisioning_roleHolderResolvesRequiredIntentsExceptFinalization_returnsFalse()196     public void isRoleHolderReadyForProvisioning_roleHolderResolvesRequiredIntentsExceptFinalization_returnsFalse() {
197         mRoleHolderHandledIntents.remove(
198                 DevicePolicyManager.ACTION_ROLE_HOLDER_PROVISION_FINALIZATION);
199         DeviceManagementRoleHolderHelper roleHolderHelper =
200                 createRoleHolderHelperWithRoleHolderResolvesRequiredIntents(
201                         mRoleHolderHandledIntents);
202 
203         assertThat(roleHolderHelper.isRoleHolderReadyForProvisioning(
204                 mContext, MANAGED_PROVISIONING_INTENT)).isFalse();
205     }
206 
207     @Test
isRoleHolderReadyForProvisioning_provisioningStartedViaManagedProfileIntent_returnsTrue()208     public void isRoleHolderReadyForProvisioning_provisioningStartedViaManagedProfileIntent_returnsTrue() {
209         DeviceManagementRoleHolderHelper roleHolderHelper =
210                 createRoleHolderHelperWithRoleHolderResolvesRequiredIntents(
211                         mRoleHolderHandledIntents);
212 
213         assertThat(roleHolderHelper.isRoleHolderReadyForProvisioning(
214                 mContext, MANAGED_PROFILE_INTENT)).isTrue();
215     }
216 
217     @Test
isRoleHolderReadyForProvisioning_provisioningStartedViaTrustedSourceIntent_returnsTrue()218     public void isRoleHolderReadyForProvisioning_provisioningStartedViaTrustedSourceIntent_returnsTrue() {
219         DeviceManagementRoleHolderHelper roleHolderHelper =
220                 createRoleHolderHelperWithRoleHolderResolvesRequiredIntents(
221                         mRoleHolderHandledIntents);
222 
223         assertThat(roleHolderHelper.isRoleHolderReadyForProvisioning(
224                 mContext, PROVISION_TRUSTED_SOURCE_INTENT)).isTrue();
225     }
226 
227     @Test
isRoleHolderReadyForProvisioning_provisioningStartedViaFinancedDeviceIntent_returnsFalse()228     public void isRoleHolderReadyForProvisioning_provisioningStartedViaFinancedDeviceIntent_returnsFalse() {
229         DeviceManagementRoleHolderHelper roleHolderHelper =
230                 createRoleHolderHelperWithRoleHolderResolvesRequiredIntents(
231                         mRoleHolderHandledIntents);
232 
233         assertThat(roleHolderHelper.isRoleHolderReadyForProvisioning(
234                 mContext, FINANCED_DEVICE_INTENT)).isFalse();
235     }
236 
237     @Test
isRoleHolderReadyForProvisioning_provisioningStartedViaFinalizationIntent_returnsFalse()238     public void isRoleHolderReadyForProvisioning_provisioningStartedViaFinalizationIntent_returnsFalse() {
239         DeviceManagementRoleHolderHelper roleHolderHelper =
240                 createRoleHolderHelperWithRoleHolderResolvesRequiredIntents(
241                         mRoleHolderHandledIntents);
242 
243         assertThat(roleHolderHelper.isRoleHolderReadyForProvisioning(
244                 mContext, PROVISION_FINALIZATION_INTENT)).isFalse();
245     }
246 
247     @Test
createRoleHolderProvisioningIntent_invalidProvisioningIntent_throwsException()248     public void createRoleHolderProvisioningIntent_invalidProvisioningIntent_throwsException() {
249         DeviceManagementRoleHolderHelper roleHolderHelper = createRoleHolderHelper();
250 
251         assertThrows(IllegalArgumentException.class, () ->
252                 roleHolderHelper.createRoleHolderProvisioningIntent(
253                         INVALID_MANAGED_PROVISIONING_INTENT,
254                         TEST_ADDITIONAL_EXTRAS, TEST_CALLING_PACKAGE, TEST_ROLE_HOLDER_STATE
255                 ));
256     }
257 
258     @Test
createRoleHolderProvisioningIntent_managedProfileProvisioningIntent_works()259     public void createRoleHolderProvisioningIntent_managedProfileProvisioningIntent_works() {
260         DeviceManagementRoleHolderHelper roleHolderHelper = createRoleHolderHelper();
261 
262         assertIntentsEqual(
263                 roleHolderHelper.createRoleHolderProvisioningIntent(
264                         MANAGED_PROFILE_INTENT,
265                         TEST_ADDITIONAL_EXTRAS, TEST_CALLING_PACKAGE, TEST_ROLE_HOLDER_STATE
266                 ),
267                 MANAGED_PROFILE_ROLE_HOLDER_INTENT);
268     }
269 
270     @Test
271     public void
createRoleHolderProvisioningIntent_managedProfileProvisioningIntentWithNoStateOrCallingPackage_works()272             createRoleHolderProvisioningIntent_managedProfileProvisioningIntentWithNoStateOrCallingPackage_works() {
273         DeviceManagementRoleHolderHelper roleHolderHelper = createRoleHolderHelper();
274 
275         assertIntentsEqual(
276                 roleHolderHelper.createRoleHolderProvisioningIntent(
277                         MANAGED_PROFILE_INTENT,
278                         /* roleHolderState= */ TEST_ADDITIONAL_EXTRAS, null, null
279                         /* callingPackage= */),
280                 MANAGED_PROFILE_ROLE_HOLDER_INTENT_WITH_MINIMAL_EXTRAS);
281     }
282 
283     @Test
createRoleHolderProvisioningIntent_emptyRoleHolderPackageName_throwsException()284     public void createRoleHolderProvisioningIntent_emptyRoleHolderPackageName_throwsException() {
285         DeviceManagementRoleHolderHelper roleHolderHelper =
286                 createRoleHolderHelper(ROLE_HOLDER_EMPTY_PACKAGE_NAME);
287 
288         assertThrows(IllegalStateException.class, () ->
289                 roleHolderHelper.createRoleHolderProvisioningIntent(
290                         MANAGED_PROFILE_INTENT,
291                         TEST_ADDITIONAL_EXTRAS, TEST_CALLING_PACKAGE, TEST_ROLE_HOLDER_STATE
292                 ));
293     }
294 
295     @Test
createRoleHolderProvisioningIntent_trustedSourceProvisioningIntent_works()296     public void createRoleHolderProvisioningIntent_trustedSourceProvisioningIntent_works() {
297         DeviceManagementRoleHolderHelper roleHolderHelper = createRoleHolderHelper();
298 
299         assertIntentsEqual(
300                 roleHolderHelper.createRoleHolderProvisioningIntent(
301                         PROVISION_TRUSTED_SOURCE_INTENT,
302                         TEST_ADDITIONAL_EXTRAS, TEST_CALLING_PACKAGE, TEST_ROLE_HOLDER_STATE
303                 ),
304                 PROVISION_TRUSTED_SOURCE_ROLE_HOLDER_INTENT);
305     }
306 
307     @Test
308     public void
createRoleHolderProvisioningIntent_trustedSourceProvisioningIntentWithNoStateOrCallingPackage_works()309             createRoleHolderProvisioningIntent_trustedSourceProvisioningIntentWithNoStateOrCallingPackage_works() {
310         DeviceManagementRoleHolderHelper roleHolderHelper = createRoleHolderHelper();
311 
312         assertIntentsEqual(
313                 roleHolderHelper.createRoleHolderProvisioningIntent(
314                         PROVISION_TRUSTED_SOURCE_INTENT,
315                         /* roleHolderState= */ TEST_ADDITIONAL_EXTRAS, null, null
316                         /* callingPackage= */),
317                 PROVISION_TRUSTED_SOURCE_ROLE_HOLDER_INTENT_WITH_MINIMAL_EXTRAS);
318     }
319 
320     @Test
createRoleHolderProvisioningIntent_financedDeviceProvisioningIntent_throwsException()321     public void createRoleHolderProvisioningIntent_financedDeviceProvisioningIntent_throwsException() {
322         DeviceManagementRoleHolderHelper roleHolderHelper = createRoleHolderHelper();
323 
324         assertThrows(
325                 IllegalArgumentException.class,
326                 () -> roleHolderHelper.createRoleHolderProvisioningIntent(
327                         FINANCED_DEVICE_INTENT,
328                         TEST_ADDITIONAL_EXTRAS, TEST_CALLING_PACKAGE, TEST_ROLE_HOLDER_STATE
329                 ));
330     }
331 
332     @Test
createRoleHolderFinalizationIntent_works()333     public void createRoleHolderFinalizationIntent_works() {
334         DeviceManagementRoleHolderHelper roleHolderHelper = createRoleHolderHelper();
335 
336         assertIntentsEqual(
337                 roleHolderHelper.createRoleHolderFinalizationIntent(
338                         /* parentActivityIntent= */ null),
339                 PROVISION_FINALIZATION_ROLE_HOLDER_INTENT);
340     }
341 
342     @Test
createRoleHolderFinalizationIntent_emptyRoleHolderPackageName_throwsException()343     public void createRoleHolderFinalizationIntent_emptyRoleHolderPackageName_throwsException() {
344         DeviceManagementRoleHolderHelper roleHolderHelper =
345                 createRoleHolderHelper(ROLE_HOLDER_EMPTY_PACKAGE_NAME);
346 
347         assertThrows(IllegalStateException.class,
348                 () -> roleHolderHelper.createRoleHolderFinalizationIntent(
349                         /* parentActivityIntent= */ null));
350     }
351 
352     @Test
isRoleHolderProvisioningEnabled_roleHolderConfigured_returnsTrue()353     public void isRoleHolderProvisioningEnabled_roleHolderConfigured_returnsTrue() {
354         DeviceManagementRoleHolderHelper roleHolderHelper =
355                 createRoleHolderHelper(ROLE_HOLDER_PACKAGE_NAME);
356 
357         assertThat(roleHolderHelper.isRoleHolderProvisioningEnabled()).isTrue();
358     }
359 
360     @Test
isRoleHolderProvisioningEnabled_roleHolderNotConfigured_returnsFalse()361     public void isRoleHolderProvisioningEnabled_roleHolderNotConfigured_returnsFalse() {
362         DeviceManagementRoleHolderHelper roleHolderHelper =
363                 createRoleHolderHelper(ROLE_HOLDER_EMPTY_PACKAGE_NAME);
364 
365         assertThat(roleHolderHelper.isRoleHolderProvisioningEnabled()).isFalse();
366     }
367 
368     @Test
isRoleHolderProvisioningEnabled_featureFlagDisabled_returnsFalse()369     public void isRoleHolderProvisioningEnabled_featureFlagDisabled_returnsFalse() {
370         mCanDelegateProvisioningToRoleHolder = false;
371         DeviceManagementRoleHolderHelper roleHolderHelper =
372                 createRoleHolderHelper(ROLE_HOLDER_PACKAGE_NAME);
373 
374         assertThat(roleHolderHelper.isRoleHolderProvisioningEnabled()).isFalse();
375     }
376 
createRoleHolderHelper()377     private DeviceManagementRoleHolderHelper createRoleHolderHelper() {
378         return new DeviceManagementRoleHolderHelper(
379                 ROLE_HOLDER_PACKAGE_NAME,
380                 /* packageInstallChecker= */ (roleHolderPackageName) -> true,
381                 /* resolveIntentChecker= */ (intent, packageManager) -> true,
382                 /* roleHolderStubChecker= */ (packageName, packageManager) -> false,
383                 mFeatureFlagChecker);
384     }
385 
createRoleHolderHelper( String roleHolderPackageName)386     private DeviceManagementRoleHolderHelper createRoleHolderHelper(
387             String roleHolderPackageName) {
388         return new DeviceManagementRoleHolderHelper(
389                 roleHolderPackageName,
390                 /* packageInstallChecker= */ (packageName) -> true,
391                 /* resolveIntentChecker= */ (intent, packageManager) -> true,
392                 /* roleHolderStubChecker= */ (packageName, packageManager) -> false,
393                 mFeatureFlagChecker);
394     }
395 
createRoleHolderHelperWithRoleHolderNotInstalled()396     private DeviceManagementRoleHolderHelper createRoleHolderHelperWithRoleHolderNotInstalled() {
397         return new DeviceManagementRoleHolderHelper(
398                 ROLE_HOLDER_PACKAGE_NAME,
399                 /* packageInstallChecker= */ (roleHolderPackageName) -> false,
400                 /* resolveIntentChecker= */ (intent, packageManager) -> true,
401                 /* roleHolderStubChecker= */ (packageName, packageManager) -> false,
402                 mFeatureFlagChecker);
403     }
404 
createRoleHolderHelperWithStubRoleHolder()405     private DeviceManagementRoleHolderHelper createRoleHolderHelperWithStubRoleHolder() {
406         return new DeviceManagementRoleHolderHelper(
407                 ROLE_HOLDER_PACKAGE_NAME,
408                 /* packageInstallChecker= */ (roleHolderPackageName) -> true,
409                 /* resolveIntentChecker= */ (intent, packageManager) -> true,
410                 /* roleHolderStubChecker= */ (packageName, packageManager) -> true,
411                 mFeatureFlagChecker);
412     }
413 
createRoleHolderHelperWithInvalidRoleHolder()414     private DeviceManagementRoleHolderHelper createRoleHolderHelperWithInvalidRoleHolder() {
415         // A role holder is considered invalid if it is not able to resolve all the required intents
416         return new DeviceManagementRoleHolderHelper(
417                 ROLE_HOLDER_PACKAGE_NAME,
418                 /* packageInstallChecker= */ (roleHolderPackageName) -> true,
419                 /* resolveIntentChecker= */ (intent, packageManager) -> false,
420                 /* roleHolderStubChecker= */ (packageName, packageManager) -> false,
421                 mFeatureFlagChecker);
422     }
423 
424     private DeviceManagementRoleHolderHelper
createRoleHolderHelperWithRoleHolderResolvesRequiredIntents( Set<String> roleHolderHandledIntents)425             createRoleHolderHelperWithRoleHolderResolvesRequiredIntents(
426             Set<String> roleHolderHandledIntents) {
427         // A role holder is considered invalid if it is not able to resolve all the required intents
428         return new DeviceManagementRoleHolderHelper(
429                 ROLE_HOLDER_PACKAGE_NAME,
430                 /* packageInstallChecker= */ (roleHolderPackageName) -> true,
431                 /* resolveIntentChecker= */ (intent, packageManager) ->
432                         roleHolderHandledIntents.contains(intent.getAction()),
433                 /* roleHolderStubChecker= */ (packageName, packageManager) -> false,
434                 mFeatureFlagChecker);
435     }
436 
createRoleHolderRequiredIntentActionsSet()437     private static Set<String> createRoleHolderRequiredIntentActionsSet() {
438         Set<String> result = new HashSet<>();
439         result.add(DevicePolicyManager.ACTION_ROLE_HOLDER_PROVISION_MANAGED_PROFILE);
440         result.add(ACTION_ROLE_HOLDER_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE);
441         result.add(DevicePolicyManager.ACTION_ROLE_HOLDER_PROVISION_FINALIZATION);
442         return result;
443     }
444 
createManagedProfileRoleHolderIntent()445     private static Intent createManagedProfileRoleHolderIntent() {
446         Intent intent = new Intent(DevicePolicyManager.ACTION_ROLE_HOLDER_PROVISION_MANAGED_PROFILE)
447                 .putExtra(TEST_EXTRA_KEY, TEST_EXTRA_VALUE)
448                 .setPackage(ROLE_HOLDER_PACKAGE_NAME)
449                 .putExtra(EXTRA_ROLE_HOLDER_PROVISIONING_INITIATOR_PACKAGE, TEST_CALLING_PACKAGE)
450                 .putExtra(EXTRA_ROLE_HOLDER_STATE, TEST_ROLE_HOLDER_STATE);
451         WizardManagerHelper.copyWizardManagerExtras(MANAGED_PROFILE_INTENT, intent);
452         return intent;
453     }
454 
createManagedProfileRoleHolderIntentWithMinimalExtras()455     private static Intent createManagedProfileRoleHolderIntentWithMinimalExtras() {
456         Intent intent = new Intent(DevicePolicyManager.ACTION_ROLE_HOLDER_PROVISION_MANAGED_PROFILE)
457                 .putExtra(TEST_EXTRA_KEY, TEST_EXTRA_VALUE)
458                 .setPackage(ROLE_HOLDER_PACKAGE_NAME);
459         WizardManagerHelper.copyWizardManagerExtras(MANAGED_PROFILE_INTENT, intent);
460         return intent;
461     }
462 
createTrustedSourceRoleHolderIntent()463     private static Intent createTrustedSourceRoleHolderIntent() {
464         Intent intent = new Intent(ACTION_ROLE_HOLDER_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE)
465                 .putExtra(TEST_EXTRA_KEY, TEST_EXTRA_VALUE)
466                 .setPackage(ROLE_HOLDER_PACKAGE_NAME)
467                 .putExtra(EXTRA_ROLE_HOLDER_PROVISIONING_INITIATOR_PACKAGE, TEST_CALLING_PACKAGE)
468                 .putExtra(EXTRA_ROLE_HOLDER_STATE, TEST_ROLE_HOLDER_STATE);
469         WizardManagerHelper.copyWizardManagerExtras(PROVISION_TRUSTED_SOURCE_INTENT, intent);
470         return intent;
471     }
472 
createTrustedSourceRoleHolderIntentWithMinimalExtras()473     private static Intent createTrustedSourceRoleHolderIntentWithMinimalExtras() {
474         Intent intent = new Intent(ACTION_ROLE_HOLDER_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE)
475                 .putExtra(TEST_EXTRA_KEY, TEST_EXTRA_VALUE)
476                 .setPackage(ROLE_HOLDER_PACKAGE_NAME);
477         WizardManagerHelper.copyWizardManagerExtras(PROVISION_TRUSTED_SOURCE_INTENT, intent);
478         return intent;
479     }
480 
enableRoleHolderDelegation()481     private void enableRoleHolderDelegation() {
482         mCanDelegateProvisioningToRoleHolder = true;
483     }
484 
disableRoleHolderDelegation()485     private void disableRoleHolderDelegation() {
486         mCanDelegateProvisioningToRoleHolder = false;
487     }
488 }
489