• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static android.app.admin.DevicePolicyManager.ENCRYPTION_STATUS_ACTIVATING;
4 import static android.app.admin.DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE;
5 import static android.app.admin.DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE_DEFAULT_KEY;
6 import static android.app.admin.DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE_PER_USER;
7 import static android.app.admin.DevicePolicyManager.ENCRYPTION_STATUS_INACTIVE;
8 import static android.app.admin.DevicePolicyManager.ENCRYPTION_STATUS_UNSUPPORTED;
9 import static android.app.admin.DevicePolicyManager.STATE_USER_SETUP_COMPLETE;
10 import static android.app.admin.DevicePolicyManager.STATE_USER_SETUP_INCOMPLETE;
11 import static android.app.admin.DevicePolicyManager.STATE_USER_UNMANAGED;
12 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
13 import static android.os.Build.VERSION_CODES.LOLLIPOP;
14 import static android.os.Build.VERSION_CODES.M;
15 import static android.os.Build.VERSION_CODES.N;
16 import static android.os.Build.VERSION_CODES.O;
17 // BEGIN-INTERNAL
18 import static android.os.Build.VERSION_CODES.Q;
19 // END-INTERNAL
20 import static com.google.common.truth.Truth.assertThat;
21 import static org.junit.Assert.fail;
22 import static org.robolectric.Shadows.shadowOf;
23 
24 import android.app.admin.DevicePolicyManager;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.content.pm.PackageManager;
28 import android.content.pm.PackageManager.NameNotFoundException;
29 import android.os.Bundle;
30 import android.os.UserManager;
31 import android.util.ArraySet;
32 
33 import androidx.test.core.app.ApplicationProvider;
34 import androidx.test.ext.junit.runners.AndroidJUnit4;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.List;
39 import java.util.Set;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.robolectric.annotation.Config;
45 
46 /** Unit tests for {@link ShadowDevicePolicyManager}. */
47 @RunWith(AndroidJUnit4.class)
48 public final class ShadowDevicePolicyManagerTest {
49 
50   private DevicePolicyManager devicePolicyManager;
51   private UserManager userManager;
52   private ComponentName testComponent;
53   private PackageManager packageManager;
54 
55   @Before
setUp()56   public void setUp() {
57     Context context = ApplicationProvider.getApplicationContext();
58     devicePolicyManager =
59         (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
60 
61     userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
62 
63     testComponent = new ComponentName("com.example.app", "DeviceAdminReceiver");
64 
65     packageManager = context.getPackageManager();
66   }
67 
68   @Test
69   @Config(minSdk = JELLY_BEAN_MR2)
isDeviceOwnerAppShouldReturnFalseForNonDeviceOwnerApp()70   public void isDeviceOwnerAppShouldReturnFalseForNonDeviceOwnerApp() {
71     // GIVEN an test package which is not the device owner app of the device
72     String testPackage = testComponent.getPackageName();
73 
74     // WHEN DevicePolicyManager#isDeviceOwnerApp is called with it
75     // THEN the method should return false
76     assertThat(devicePolicyManager.isDeviceOwnerApp(testPackage)).isFalse();
77   }
78 
79   @Test
80   @Config(minSdk = LOLLIPOP)
isDeviceOwnerShouldReturnFalseForProfileOwner()81   public void isDeviceOwnerShouldReturnFalseForProfileOwner() {
82     // GIVEN an test package which is the profile owner app of the device
83     String testPackage = testComponent.getPackageName();
84     shadowOf(devicePolicyManager).setProfileOwner(testComponent);
85 
86     // WHEN DevicePolicyManager#isDeviceOwnerApp is called with it
87     // THEN the method should return false
88     assertThat(devicePolicyManager.isDeviceOwnerApp(testPackage)).isFalse();
89   }
90 
91   @Test
92   @Config(minSdk = JELLY_BEAN_MR2)
isDeviceOwnerShouldReturnTrueForDeviceOwner()93   public void isDeviceOwnerShouldReturnTrueForDeviceOwner() {
94     // GIVEN an test package which is the device owner app of the device
95     String testPackage = testComponent.getPackageName();
96     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
97 
98     // WHEN DevicePolicyManager#isDeviceOwnerApp is called with it
99     // THEN the method should return true
100     assertThat(devicePolicyManager.isDeviceOwnerApp(testPackage)).isTrue();
101   }
102 
103   @Test
104   @Config(minSdk = JELLY_BEAN_MR2)
getDeviceOwnerShouldReturnDeviceOwnerPackageName()105   public void getDeviceOwnerShouldReturnDeviceOwnerPackageName() {
106     // GIVEN an test package which is the device owner app of the device
107     String testPackage = testComponent.getPackageName();
108     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
109 
110     // WHEN DevicePolicyManager#getDeviceOwner is called
111     // THEN the method should return the package name
112     assertThat(devicePolicyManager.getDeviceOwner()).isEqualTo(testPackage);
113   }
114 
115   @Test
116   @Config(minSdk = JELLY_BEAN_MR2)
getDeviceOwnerShouldReturnNullWhenThereIsNoDeviceOwner()117   public void getDeviceOwnerShouldReturnNullWhenThereIsNoDeviceOwner() {
118     // WHEN DevicePolicyManager#getProfileOwner is called without a device owner
119     // THEN the method should return null
120     assertThat(devicePolicyManager.getDeviceOwner()).isNull();
121   }
122 
123   @Test
124   @Config(minSdk = N)
isDeviceManagedShouldReturnTrueWhenThereIsADeviceOwner()125   public void isDeviceManagedShouldReturnTrueWhenThereIsADeviceOwner() {
126     // GIVEN a test component is the device owner app of the device
127     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
128 
129     // WHEN DevicePolicyManager#isDeviceManaged is called
130     // THEN the method should return true
131     assertThat(devicePolicyManager.isDeviceManaged()).isTrue();
132   }
133 
134   @Test
135   @Config(minSdk = N)
isDeviceManagedShouldReturnFalseWhenThereIsNoDeviceOwner()136   public void isDeviceManagedShouldReturnFalseWhenThereIsNoDeviceOwner() {
137     // WHEN DevicePolicyManager#isDeviceManaged is called without a device owner
138     // THEN the method should return false
139     assertThat(devicePolicyManager.isDeviceManaged()).isFalse();
140   }
141 
142   @Test
143   @Config(minSdk = LOLLIPOP)
isProfileOwnerAppShouldReturnFalseForNonProfileOwnerApp()144   public void isProfileOwnerAppShouldReturnFalseForNonProfileOwnerApp() {
145     // GIVEN an test package which is not the profile owner app of the device
146     String testPackage = testComponent.getPackageName();
147 
148     // WHEN DevicePolicyManager#isProfileOwnerApp is called with it
149     // THEN the method should return false
150     assertThat(devicePolicyManager.isProfileOwnerApp(testPackage)).isFalse();
151   }
152 
153   @Test
154   @Config(minSdk = LOLLIPOP)
isProfileOwnerShouldReturnFalseForDeviceOwner()155   public void isProfileOwnerShouldReturnFalseForDeviceOwner() {
156     // GIVEN an test package which is the device owner app of the device
157     String testPackage = testComponent.getPackageName();
158     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
159 
160     // WHEN DevicePolicyManager#isProfileOwnerApp is called with it
161     // THEN the method should return false
162     assertThat(devicePolicyManager.isProfileOwnerApp(testPackage)).isFalse();
163   }
164 
165   @Test
166   @Config(minSdk = LOLLIPOP)
isProfileOwnerShouldReturnTrueForProfileOwner()167   public void isProfileOwnerShouldReturnTrueForProfileOwner() {
168     // GIVEN an test package which is the profile owner app of the device
169     String testPackage = testComponent.getPackageName();
170     shadowOf(devicePolicyManager).setProfileOwner(testComponent);
171 
172     // WHEN DevicePolicyManager#isProfileOwnerApp is called with it
173     // THEN the method should return true
174     assertThat(devicePolicyManager.isProfileOwnerApp(testPackage)).isTrue();
175   }
176 
177   @Test
178   @Config(minSdk = LOLLIPOP)
getProfileOwnerShouldReturnDeviceOwnerComponentName()179   public void getProfileOwnerShouldReturnDeviceOwnerComponentName() {
180     // GIVEN an test package which is the profile owner app of the device
181     shadowOf(devicePolicyManager).setProfileOwner(testComponent);
182 
183     // WHEN DevicePolicyManager#getProfileOwner is called
184     // THEN the method should return the component
185     assertThat(devicePolicyManager.getProfileOwner()).isEqualTo(testComponent);
186   }
187 
188   @Test
189   @Config(minSdk = LOLLIPOP)
getProfileOwnerShouldReturnNullWhenThereIsNoProfileOwner()190   public void getProfileOwnerShouldReturnNullWhenThereIsNoProfileOwner() {
191     // WHEN DevicePolicyManager#getProfileOwner is called without a profile owner
192     // THEN the method should return null
193     assertThat(devicePolicyManager.getProfileOwner()).isNull();
194   }
195 
196   @Test
isAdminActiveShouldReturnFalseForNonAdminDevice()197   public void isAdminActiveShouldReturnFalseForNonAdminDevice() {
198     // GIVEN a test component which is not an active admin of the device
199     // WHEN DevicePolicyManager#isAdminActive is called with it
200     // THEN the method should return false
201     assertThat(devicePolicyManager.isAdminActive(testComponent)).isFalse();
202   }
203 
204   @Test
isAdminActiveShouldReturnTrueForAnyDeviceAdminDevice()205   public void isAdminActiveShouldReturnTrueForAnyDeviceAdminDevice() {
206     // GIVEN a test component which is an active admin of the device
207     shadowOf(devicePolicyManager).setActiveAdmin(testComponent);
208 
209     // WHEN DevicePolicyManager#isAdminActive is called with it
210     // THEN the method should return true
211     assertThat(devicePolicyManager.isAdminActive(testComponent)).isTrue();
212   }
213 
214   @Test
215   @Config(minSdk = JELLY_BEAN_MR2)
getActiveAdminsShouldReturnDeviceOwner()216   public void getActiveAdminsShouldReturnDeviceOwner() {
217     // GIVEN an test package which is the device owner app of the device
218     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
219 
220     // WHEN DevicePolicyManager#getActiveAdmins is called
221     // THEN the return of the method should include the device owner app
222     assertThat(devicePolicyManager.getActiveAdmins()).contains(testComponent);
223   }
224 
225   @Test
226   @Config(minSdk = LOLLIPOP)
getActiveAdminsShouldReturnProfileOwner()227   public void getActiveAdminsShouldReturnProfileOwner() {
228     // GIVEN an test package which is the profile owner app of the device
229     shadowOf(devicePolicyManager).setProfileOwner(testComponent);
230 
231     // WHEN DevicePolicyManager#getActiveAdmins is called
232     // THEN the return of the method should include the profile owner app
233     assertThat(devicePolicyManager.getActiveAdmins()).contains(testComponent);
234   }
235 
236   @Test
237   @Config(minSdk = LOLLIPOP)
addUserRestrictionShouldWorkAsIntendedForDeviceOwner()238   public void addUserRestrictionShouldWorkAsIntendedForDeviceOwner() {
239     // GIVEN a user restriction to set
240     String restrictionKey = "restriction key";
241 
242     // GIVEN the caller is the device owner
243     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
244 
245     // WHEN DevicePolicyManager#addUserRestriction is called with the key
246     devicePolicyManager.addUserRestriction(testComponent, restrictionKey);
247 
248     // THEN the restriction should be set for the current user
249     Bundle restrictions = userManager.getUserRestrictions();
250     assertThat(restrictions.getBoolean(restrictionKey)).isTrue();
251   }
252 
253   @Test
254   @Config(minSdk = LOLLIPOP)
addUserRestrictionShouldWorkAsIntendedForProfileOwner()255   public void addUserRestrictionShouldWorkAsIntendedForProfileOwner() {
256     // GIVEN a user restriction to set
257     String restrictionKey = "restriction key";
258 
259     // GIVEN the caller is the profile owner
260     shadowOf(devicePolicyManager).setProfileOwner(testComponent);
261 
262     // WHEN DevicePolicyManager#addUserRestriction is called with the key
263     devicePolicyManager.addUserRestriction(testComponent, restrictionKey);
264 
265     // THEN the restriction should be set for the current user
266     Bundle restrictions = userManager.getUserRestrictions();
267     assertThat(restrictions.getBoolean(restrictionKey)).isTrue();
268   }
269 
270   @Test
271   @Config(minSdk = LOLLIPOP)
clearUserRestrictionShouldWorkAsIntendedForActiveAdmins()272   public void clearUserRestrictionShouldWorkAsIntendedForActiveAdmins() {
273     // GIVEN the caller is the device owner, and thus an active admin
274     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
275 
276     // GIVEN a user restriction has set
277     String restrictionKey = "restriction key";
278     devicePolicyManager.addUserRestriction(testComponent, restrictionKey);
279 
280     // WHEN DevicePolicyManager#clearUserRestriction is called with the key
281     devicePolicyManager.clearUserRestriction(testComponent, restrictionKey);
282 
283     // THEN the restriction should be cleared for the current user
284     Bundle restrictions = userManager.getUserRestrictions();
285     assertThat(restrictions.getBoolean(restrictionKey)).isFalse();
286   }
287 
288   @Test
289   @Config(minSdk = LOLLIPOP)
isApplicationHiddenShouldReturnTrueForNotExistingApps()290   public void isApplicationHiddenShouldReturnTrueForNotExistingApps() {
291     // GIVEN the caller is the device owner, and thus an active admin
292     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
293 
294     // GIVEN package that is not installed
295     String app = "com.example.non.existing";
296 
297     // WHEN DevicePolicyManager#isApplicationHidden is called on the app
298     // THEN it should return true
299     assertThat(devicePolicyManager.isApplicationHidden(testComponent, app)).isTrue();
300   }
301 
302   @Test
303   @Config(minSdk = LOLLIPOP)
isApplicationHiddenShouldReturnFalseForAppsByDefault()304   public void isApplicationHiddenShouldReturnFalseForAppsByDefault() {
305     // GIVEN the caller is the device owner, and thus an active admin
306     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
307 
308     // GIVEN an app and it's never be set hidden or non hidden
309     String app = "com.example.non.hidden";
310     shadowOf(packageManager).addPackage(app);
311 
312     // WHEN DevicePolicyManager#isApplicationHidden is called on the app
313     // THEN it should return false
314     assertThat(devicePolicyManager.isApplicationHidden(testComponent, app)).isFalse();
315   }
316 
317   @Test
318   @Config(minSdk = LOLLIPOP)
isApplicationHiddenShouldReturnTrueForHiddenApps()319   public void isApplicationHiddenShouldReturnTrueForHiddenApps() {
320     // GIVEN the caller is the device owner, and thus an active admin
321     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
322 
323     // GIVEN an app and it is hidden
324     String hiddenApp = "com.example.hidden";
325     shadowOf(packageManager).addPackage(hiddenApp);
326     devicePolicyManager.setApplicationHidden(testComponent, hiddenApp, true);
327 
328     // WHEN DevicePolicyManager#isApplicationHidden is called on the app
329     // THEN it should return true
330     assertThat(devicePolicyManager.isApplicationHidden(testComponent, hiddenApp)).isTrue();
331   }
332 
333   @Test
334   @Config(minSdk = LOLLIPOP)
isApplicationHiddenShouldReturnFalseForNonHiddenApps()335   public void isApplicationHiddenShouldReturnFalseForNonHiddenApps() {
336     // GIVEN the caller is the device owner, and thus an active admin
337     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
338 
339     // GIVEN an app and it is not hidden
340     String nonHiddenApp = "com.example.non.hidden";
341     shadowOf(packageManager).addPackage(nonHiddenApp);
342     devicePolicyManager.setApplicationHidden(testComponent, nonHiddenApp, false);
343 
344     // WHEN DevicePolicyManager#isApplicationHidden is called on the app
345     // THEN it should return false
346     assertThat(devicePolicyManager.isApplicationHidden(testComponent, nonHiddenApp)).isFalse();
347   }
348 
349   @Test
350   @Config(minSdk = LOLLIPOP)
setApplicationHiddenShouldBeAbleToUnhideHiddenApps()351   public void setApplicationHiddenShouldBeAbleToUnhideHiddenApps() {
352     // GIVEN the caller is the device owner, and thus an active admin
353     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
354 
355     // GIVEN an app and it is hidden
356     String app = "com.example.hidden";
357     shadowOf(packageManager).addPackage(app);
358     devicePolicyManager.setApplicationHidden(testComponent, app, true);
359 
360     // WHEN DevicePolicyManager#setApplicationHidden is called on the app to unhide it
361     devicePolicyManager.setApplicationHidden(testComponent, app, false);
362 
363     // THEN the app shouldn't be hidden anymore
364     assertThat(devicePolicyManager.isApplicationHidden(testComponent, app)).isFalse();
365   }
366 
367   @Test
368   @Config(minSdk = LOLLIPOP)
setApplicationHiddenShouldReturnFalseForNotExistingApps()369   public void setApplicationHiddenShouldReturnFalseForNotExistingApps() {
370     // GIVEN the caller is the device owner, and thus an active admin
371     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
372 
373     // WHEN an app is not installed
374     String app = "com.example.not.installed";
375 
376     // THEN DevicePolicyManager#setApplicationHidden returns false
377     assertThat(devicePolicyManager.setApplicationHidden(testComponent, app, true)).isFalse();
378   }
379 
380   @Test
381   @Config(minSdk = LOLLIPOP)
wasPackageEverHiddenShouldReturnFalseForPackageNeverHidden()382   public void wasPackageEverHiddenShouldReturnFalseForPackageNeverHidden() {
383     // GIVEN the caller is the device owner, and thus an active admin
384     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
385 
386     // GIVEN an app and it's never be set hidden or non hidden
387     String app = "com.example.non.hidden";
388     shadowOf(packageManager).addPackage(app);
389 
390     // WHEN ShadowDevicePolicyManager#wasPackageEverHidden is called with the app
391     // THEN it should return false
392     assertThat(shadowOf(devicePolicyManager).wasPackageEverHidden(app)).isFalse();
393   }
394 
395   @Test
396   @Config(minSdk = LOLLIPOP)
wasPackageEverHiddenShouldReturnTrueForPackageWhichIsHidden()397   public void wasPackageEverHiddenShouldReturnTrueForPackageWhichIsHidden() {
398     // GIVEN the caller is the device owner, and thus an active admin
399     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
400 
401     // GIVEN an app and it's hidden
402     String hiddenApp = "com.example.hidden";
403     shadowOf(packageManager).addPackage(hiddenApp);
404     devicePolicyManager.setApplicationHidden(testComponent, hiddenApp, true);
405 
406     // WHEN ShadowDevicePolicyManager#wasPackageEverHidden is called with the app
407     // THEN it should return true
408     assertThat(shadowOf(devicePolicyManager).wasPackageEverHidden(hiddenApp)).isTrue();
409   }
410 
411   @Test
412   @Config(minSdk = LOLLIPOP)
wasPackageEverHiddenShouldReturnTrueForPackageWhichWasHidden()413   public void wasPackageEverHiddenShouldReturnTrueForPackageWhichWasHidden() {
414     // GIVEN the caller is the device owner, and thus an active admin
415     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
416 
417     // GIVEN an app and it was hidden
418     String app = "com.example.hidden";
419     shadowOf(packageManager).addPackage(app);
420     devicePolicyManager.setApplicationHidden(testComponent, app, true);
421     devicePolicyManager.setApplicationHidden(testComponent, app, false);
422 
423     // WHEN ShadowDevicePolicyManager#wasPackageEverHidden is called with the app
424     // THEN it should return true
425     assertThat(shadowOf(devicePolicyManager).wasPackageEverHidden(app)).isTrue();
426   }
427 
428   @Test
429   @Config(minSdk = LOLLIPOP)
enableSystemAppShouldWorkForActiveAdmins()430   public void enableSystemAppShouldWorkForActiveAdmins() {
431     // GIVEN the caller is the device owner, and thus an active admin
432     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
433 
434     // GIVEN a system app
435     String app = "com.example.system";
436 
437     // WHEN DevicePolicyManager#enableSystemApp is called with the app
438     devicePolicyManager.enableSystemApp(testComponent, app);
439 
440     // THEN the app should be enabled
441     assertThat(shadowOf(devicePolicyManager).wasSystemAppEnabled(app)).isTrue();
442   }
443 
444   @Test
445   @Config(minSdk = LOLLIPOP)
isUninstallBlockedShouldReturnFalseForAppsNeverBeingBlocked()446   public void isUninstallBlockedShouldReturnFalseForAppsNeverBeingBlocked() {
447     // GIVEN the caller is the device owner, and thus an active admin
448     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
449 
450     // GIVEN an app
451     String app = "com.example.app";
452 
453     // WHEN DevicePolicyManager#isUninstallBlocked is called with the app
454     // THEN it should return false
455     assertThat(devicePolicyManager.isUninstallBlocked(testComponent, app)).isFalse();
456   }
457 
458   @Test
459   @Config(minSdk = LOLLIPOP)
isUninstallBlockedShouldReturnTrueForAppsBeingUnblocked()460   public void isUninstallBlockedShouldReturnTrueForAppsBeingUnblocked() {
461     // GIVEN the caller is the device owner, and thus an active admin
462     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
463 
464     // GIVEN an app which is blocked from being uninstalled
465     String app = "com.example.app";
466     devicePolicyManager.setUninstallBlocked(testComponent, app, true);
467 
468     // WHEN DevicePolicyManager#UninstallBlocked is called with the app
469     // THEN it should return true
470     assertThat(devicePolicyManager.isUninstallBlocked(testComponent, app)).isTrue();
471   }
472 
473   @Test
474   @Config(minSdk = LOLLIPOP)
isUninstallBlockedShouldReturnFalseForAppsBeingBlocked()475   public void isUninstallBlockedShouldReturnFalseForAppsBeingBlocked() {
476     // GIVEN the caller is the device owner, and thus an active admin
477     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
478 
479     // GIVEN an app which is unblocked from being uninstalled
480     String app = "com.example.app";
481     devicePolicyManager.setUninstallBlocked(testComponent, app, true);
482     devicePolicyManager.setUninstallBlocked(testComponent, app, false);
483 
484     // WHEN DevicePolicyManager#UninstallBlocked is called with the app
485     // THEN it should return false
486     assertThat(devicePolicyManager.isUninstallBlocked(testComponent, app)).isFalse();
487   }
488 
489   @Test
490   @Config(minSdk = LOLLIPOP)
setApplicationRestrictionsShouldWorkAsIntendedForDeviceOwner()491   public void setApplicationRestrictionsShouldWorkAsIntendedForDeviceOwner() {
492     // GIVEN the caller is the device owner
493     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
494 
495     // GIVEN an application restriction bundle
496     Bundle restrictions = new Bundle();
497     restrictions.putString("key", "value");
498 
499     // GIVEN an app which the restriction is set to
500     String app = "com.example.app";
501 
502     // WHEN DevicePolicyManager#setApplicationRestrictions is called to set the restrictions to the
503     // app
504     devicePolicyManager.setApplicationRestrictions(testComponent, app, restrictions);
505 
506     // THEN the restrictions should be set correctly
507     Bundle actualRestrictions = devicePolicyManager.getApplicationRestrictions(testComponent, app);
508     assertThat(actualRestrictions.getString("key", "default value")).isEqualTo("value");
509   }
510 
511   @Test
512   @Config(minSdk = LOLLIPOP)
setApplicationRestrictionsShouldWorkAsIntendedForProfileOwner()513   public void setApplicationRestrictionsShouldWorkAsIntendedForProfileOwner() {
514     // GIVEN the caller is the profile owner
515     shadowOf(devicePolicyManager).setProfileOwner(testComponent);
516 
517     // GIVEN an application restriction bundle
518     Bundle restrictions = new Bundle();
519     restrictions.putString("key", "value");
520 
521     // GIVEN an app which the restriction is set to
522     String app = "com.example.app";
523 
524     // WHEN DevicePolicyManager#setApplicationRestrictions is called to set the restrictions to the
525     // app
526     devicePolicyManager.setApplicationRestrictions(testComponent, app, restrictions);
527 
528     // THEN the restrictions should be set correctly
529     Bundle actualRestrictions = devicePolicyManager.getApplicationRestrictions(testComponent, app);
530     assertThat(actualRestrictions.getString("key", "default value")).isEqualTo("value");
531   }
532 
533   @Test
534   @Config(minSdk = LOLLIPOP)
getApplicationRestrictionsShouldReturnEmptyBundleIfAppHasNone()535   public void getApplicationRestrictionsShouldReturnEmptyBundleIfAppHasNone() {
536     // GIVEN the caller is the device owner
537     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
538 
539     // GIVEN an app has no restrictions
540     String app = "com.example.app";
541 
542     // WHEN DevicePolicyManager#getApplicationRestrictions is called to get the restrictions of the
543     // app
544     // THEN it should return the empty bundle
545     assertThat(devicePolicyManager.getApplicationRestrictions(testComponent, app).isEmpty())
546         .isTrue();
547   }
548 
549   @Test
550   @Config(minSdk = LOLLIPOP)
getAccountTypesWithManagementDisabledShouldReturnNothingWhenNoAccountIsDislabed()551   public void getAccountTypesWithManagementDisabledShouldReturnNothingWhenNoAccountIsDislabed() {
552     // GIVEN no account type has ever been disabled
553 
554     // WHEN get disabled account types using
555     // DevicePolicyManager#getAccountTypesWithManagementDisabled
556     // THEN it should be empty
557     assertThat(devicePolicyManager.getAccountTypesWithManagementDisabled()).isEmpty();
558   }
559 
560   @Test
561   @Config(minSdk = LOLLIPOP)
getAccountTypesWithManagementDisabledShouldReturnDisabledAccountTypesIfAny()562   public void getAccountTypesWithManagementDisabledShouldReturnDisabledAccountTypesIfAny() {
563     // GIVEN the caller is the device owner
564     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
565 
566     // GIVEN a disabled account type
567     String disabledAccountType = "com.example.account.type";
568     devicePolicyManager.setAccountManagementDisabled(testComponent, disabledAccountType, true);
569 
570     // WHEN get disabled account types using
571     // DevicePolicyManager#getAccountTypesWithManagementDisabled
572     // THEN it should contain the disabled account type
573     assertThat(devicePolicyManager.getAccountTypesWithManagementDisabled())
574         .isEqualTo(new String[] {disabledAccountType});
575   }
576 
577   @Test
578   @Config(minSdk = LOLLIPOP)
getAccountTypesWithManagementDisabledShouldNotReturnReenabledAccountTypesIfAny()579   public void getAccountTypesWithManagementDisabledShouldNotReturnReenabledAccountTypesIfAny() {
580     // GIVEN the caller is the device owner
581     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
582 
583     // GIVEN a re-enabled account type
584     String reenabledAccountType = "com.example.account.type";
585     devicePolicyManager.setAccountManagementDisabled(testComponent, reenabledAccountType, true);
586     devicePolicyManager.setAccountManagementDisabled(testComponent, reenabledAccountType, false);
587 
588     // WHEN get disabled account types using
589     // DevicePolicyManager#getAccountTypesWithManagementDisabled
590     // THEN it should not contain the re-enabled account type
591     assertThat(devicePolicyManager.getAccountTypesWithManagementDisabled()).isEmpty();
592   }
593 
594   @Test
595   @Config(minSdk = N)
setOrganizationNameShouldWorkForPoSinceN()596   public void setOrganizationNameShouldWorkForPoSinceN() {
597     // GIVEN the caller is the profile owner
598     shadowOf(devicePolicyManager).setProfileOwner(testComponent);
599 
600     // WHEN setting an organization name
601     String organizationName = "TestOrg";
602     devicePolicyManager.setOrganizationName(testComponent, organizationName);
603 
604     // THEN the name should be set properly
605     assertThat(devicePolicyManager.getOrganizationName(testComponent).toString())
606         .isEqualTo(organizationName);
607   }
608 
609   @Test
610   @Config(minSdk = N)
setOrganizationNameShouldClearNameWithEmptyNameForPoSinceN()611   public void setOrganizationNameShouldClearNameWithEmptyNameForPoSinceN() {
612     // GIVEN the caller is the profile owner
613     shadowOf(devicePolicyManager).setProfileOwner(testComponent);
614 
615     // GIVEN that the profile has already set the name TestOrg
616     String organizationName = "TestOrg";
617     devicePolicyManager.setOrganizationName(testComponent, organizationName);
618 
619     // WHEN setting an organization name to empty
620     devicePolicyManager.setOrganizationName(testComponent, "");
621 
622     // THEN the name should be cleared
623     assertThat(devicePolicyManager.getOrganizationName(testComponent)).isNull();
624   }
625 
626   @Test
627   @Config(sdk = N)
setOrganizationNameShouldNotWorkForDoInN()628   public void setOrganizationNameShouldNotWorkForDoInN() {
629     // GIVEN the caller is the device owner
630     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
631 
632     // WHEN setting an organization name
633     // THEN the method should throw SecurityException
634     String organizationName = "TestOrg";
635     try {
636       devicePolicyManager.setOrganizationName(testComponent, organizationName);
637       fail("expected SecurityException");
638     } catch (SecurityException expected) {
639     }
640   }
641 
642   @Test
643   @Config(minSdk = O)
setOrganizationNameShouldWorkForDoSinceO()644   public void setOrganizationNameShouldWorkForDoSinceO() {
645     // GIVEN the caller is the device owner
646     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
647 
648     // WHEN setting an organization name
649     String organizationName = "TestOrg";
650     devicePolicyManager.setOrganizationName(testComponent, organizationName);
651 
652     // THEN the name should be set properly
653     assertThat(devicePolicyManager.getOrganizationName(testComponent).toString())
654         .isEqualTo(organizationName);
655   }
656 
657   @Test
658   @Config(minSdk = N)
setOrganizationColorShouldWorkForPoSinceN()659   public void setOrganizationColorShouldWorkForPoSinceN() {
660     // GIVEN the caller is the profile owner
661     shadowOf(devicePolicyManager).setProfileOwner(testComponent);
662 
663     // WHEN setting an organization color
664     int color = 0xFFFF00FF;
665     devicePolicyManager.setOrganizationColor(testComponent, color);
666 
667     // THEN the color should be set properly
668     assertThat(devicePolicyManager.getOrganizationColor(testComponent)).isEqualTo(color);
669   }
670 
671   @Test
672   @Config(minSdk = N)
getOrganizationColorShouldReturnDefaultColorIfNothingSet()673   public void getOrganizationColorShouldReturnDefaultColorIfNothingSet() {
674     // GIVEN the caller is the profile owner
675     shadowOf(devicePolicyManager).setProfileOwner(testComponent);
676 
677     // WHEN getting an organization color without setting it
678     // THEN the color returned should be the default color
679     assertThat(devicePolicyManager.getOrganizationColor(testComponent)).isEqualTo(0xFF008080);
680   }
681 
682   @Test
683   @Config(minSdk = N)
setOrganizationColorShouldNotWorkForDo()684   public void setOrganizationColorShouldNotWorkForDo() {
685     // GIVEN the caller is the device owner
686     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
687 
688     // WHEN setting an organization color
689     // THEN the method should throw SecurityException
690     int color = 0xFFFF00FF;
691     try {
692       devicePolicyManager.setOrganizationColor(testComponent, color);
693       fail("expected SecurityException");
694     } catch (SecurityException expected) {
695     }
696   }
697 
698   @Test
699   @Config(minSdk = LOLLIPOP)
getAutoTimeRequiredShouldWorkAsIntendedForDeviceOwner()700   public void getAutoTimeRequiredShouldWorkAsIntendedForDeviceOwner() {
701     // GIVEN the caller is the device owner
702     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
703 
704     // WHEN setAutoTimeRequired is called with true
705     devicePolicyManager.setAutoTimeRequired(testComponent, true);
706 
707     // THEN getAutoTimeRequired should return true
708     assertThat(devicePolicyManager.getAutoTimeRequired()).isTrue();
709   }
710 
711   @Test
712   @Config(minSdk = LOLLIPOP)
getAutoTimeRequiredShouldWorkAsIntendedForProfileOwner()713   public void getAutoTimeRequiredShouldWorkAsIntendedForProfileOwner() {
714     // GIVEN the caller is the profile owner
715     shadowOf(devicePolicyManager).setProfileOwner(testComponent);
716 
717     // WHEN setAutoTimeRequired is called with false
718     devicePolicyManager.setAutoTimeRequired(testComponent, false);
719 
720     // THEN getAutoTimeRequired should return false
721     assertThat(devicePolicyManager.getAutoTimeRequired()).isFalse();
722   }
723 
724   @Test
725   @Config(minSdk = LOLLIPOP)
getAutoTimeRequiredShouldReturnFalseIfNotSet()726   public void getAutoTimeRequiredShouldReturnFalseIfNotSet() {
727     // GIVEN the caller is the device owner
728     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
729 
730     // WHEN setAutoTimeRequired has not been called
731     // THEN getAutoTimeRequired should return false
732     assertThat(devicePolicyManager.getAutoTimeRequired()).isFalse();
733   }
734 
735   @Test
736   @Config(minSdk = LOLLIPOP)
getPermittedAccessibilityServicesShouldWorkAsIntendedForDeviceOwner()737   public void getPermittedAccessibilityServicesShouldWorkAsIntendedForDeviceOwner() {
738     List<String> accessibilityServices =
739         Arrays.asList("com.example.accessibility1", "com.example.accessibility2");
740 
741     // GIVEN the caller is the device owner
742     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
743 
744     // WHEN setPermittedAccessibilityServices is called with a valid list
745     devicePolicyManager.setPermittedAccessibilityServices(testComponent, accessibilityServices);
746 
747     // THEN getAutoTimeRequired should return the list
748     assertThat(devicePolicyManager.getPermittedAccessibilityServices(testComponent))
749         .isEqualTo(accessibilityServices);
750   }
751 
752   @Test
753   @Config(minSdk = LOLLIPOP)
getPermittedAccessibilityServicesShouldWorkAsIntendedForProfileOwner()754   public void getPermittedAccessibilityServicesShouldWorkAsIntendedForProfileOwner() {
755     List<String> accessibilityServices = new ArrayList<>();
756 
757     // GIVEN the caller is the profile owner
758     shadowOf(devicePolicyManager).setProfileOwner(testComponent);
759 
760     // WHEN setPermittedAccessibilityServices is called with an empty list
761     devicePolicyManager.setPermittedAccessibilityServices(testComponent, accessibilityServices);
762 
763     // THEN getAutoTimeRequired should return an empty list
764     assertThat(devicePolicyManager.getPermittedAccessibilityServices(testComponent)).isEmpty();
765   }
766 
767   @Test
768   @Config(minSdk = LOLLIPOP)
getPermittedAccessibilityServicesShouldReturnNullIfNullIsSet()769   public void getPermittedAccessibilityServicesShouldReturnNullIfNullIsSet() {
770     List<String> accessibilityServices = null;
771 
772     // GIVEN the caller is the device owner
773     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
774 
775     // WHEN setPermittedAccessibilityServices is called with a null list
776     devicePolicyManager.setPermittedAccessibilityServices(testComponent, accessibilityServices);
777 
778     // THEN getAutoTimeRequired should return null
779     assertThat(devicePolicyManager.getPermittedAccessibilityServices(testComponent)).isNull();
780   }
781 
782   @Test
783   @Config(minSdk = LOLLIPOP)
getPermittedInputMethodsShouldWorkAsIntendedForDeviceOwner()784   public void getPermittedInputMethodsShouldWorkAsIntendedForDeviceOwner() {
785     List<String> inputMethods = Arrays.asList("com.example.input1", "com.example.input2");
786 
787     // GIVEN the caller is the device owner
788     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
789 
790     // WHEN setPermittedInputMethods is called with a valid list
791     devicePolicyManager.setPermittedInputMethods(testComponent, inputMethods);
792 
793     // THEN getAutoTimeRequired should return the list
794     assertThat(devicePolicyManager.getPermittedInputMethods(testComponent)).isEqualTo(inputMethods);
795   }
796 
797   @Test
798   @Config(minSdk = LOLLIPOP)
getPermittedInputMethodsShouldWorkAsIntendedForProfileOwner()799   public void getPermittedInputMethodsShouldWorkAsIntendedForProfileOwner() {
800     List<String> inputMethods = new ArrayList<>();
801 
802     // GIVEN the caller is the profile owner
803     shadowOf(devicePolicyManager).setProfileOwner(testComponent);
804 
805     // WHEN setPermittedInputMethods is called with an empty list
806     devicePolicyManager.setPermittedInputMethods(testComponent, inputMethods);
807 
808     // THEN getAutoTimeRequired should return an empty list
809     assertThat(devicePolicyManager.getPermittedInputMethods(testComponent)).isEmpty();
810   }
811 
812   @Test
813   @Config(minSdk = LOLLIPOP)
getPermittedInputMethodsShouldReturnNullIfNullIsSet()814   public void getPermittedInputMethodsShouldReturnNullIfNullIsSet() {
815     List<String> inputMethods = null;
816 
817     // GIVEN the caller is the device owner
818     shadowOf(devicePolicyManager).setDeviceOwner(testComponent);
819 
820     // WHEN setPermittedInputMethods is called with a null list
821     devicePolicyManager.setPermittedInputMethods(testComponent, inputMethods);
822 
823     // THEN getAutoTimeRequired should return null
824     assertThat(devicePolicyManager.getPermittedInputMethods(testComponent)).isNull();
825   }
826 
827   @Test
getStorageEncryptionStatus_defaultValueIsUnsupported()828   public void getStorageEncryptionStatus_defaultValueIsUnsupported() {
829     final int status = devicePolicyManager.getStorageEncryptionStatus();
830     assertThat(status).isEqualTo(ENCRYPTION_STATUS_UNSUPPORTED);
831   }
832 
833   @Test
setStorageEncryptionStatus_IllegalValue()834   public void setStorageEncryptionStatus_IllegalValue() {
835     try {
836       shadowOf(devicePolicyManager).setStorageEncryptionStatus(-1);
837       fail("Expected IllegalArgumentException");
838     } catch (IllegalArgumentException e) {
839       assertThat(e.getMessage()).isEqualTo("Unknown status: -1");
840     }
841   }
842 
843   @Test
setStorageEncryptionStatus_Unsupported()844   public void setStorageEncryptionStatus_Unsupported() {
845     shadowOf(devicePolicyManager).setStorageEncryptionStatus(ENCRYPTION_STATUS_UNSUPPORTED);
846     assertThat(devicePolicyManager.getStorageEncryptionStatus())
847         .isEqualTo(ENCRYPTION_STATUS_UNSUPPORTED);
848   }
849 
850   @Test
setStorageEncryptionStatus_Active()851   public void setStorageEncryptionStatus_Active() {
852     shadowOf(devicePolicyManager).setStorageEncryptionStatus(ENCRYPTION_STATUS_ACTIVE);
853     assertThat(devicePolicyManager.getStorageEncryptionStatus())
854         .isEqualTo(ENCRYPTION_STATUS_ACTIVE);
855   }
856 
857   @Test
setStorageEncryptionStatus_Inactive()858   public void setStorageEncryptionStatus_Inactive() {
859     shadowOf(devicePolicyManager).setStorageEncryptionStatus(ENCRYPTION_STATUS_INACTIVE);
860     assertThat(devicePolicyManager.getStorageEncryptionStatus())
861         .isEqualTo(ENCRYPTION_STATUS_INACTIVE);
862   }
863 
864   @Test
setStorageEncryptionStatus_Activating()865   public void setStorageEncryptionStatus_Activating() {
866     shadowOf(devicePolicyManager).setStorageEncryptionStatus(ENCRYPTION_STATUS_ACTIVATING);
867     assertThat(devicePolicyManager.getStorageEncryptionStatus())
868         .isEqualTo(ENCRYPTION_STATUS_ACTIVATING);
869   }
870 
871   @Test
872   @Config(minSdk = M)
setStorageEncryptionStatus_ActiveDefaultKey()873   public void setStorageEncryptionStatus_ActiveDefaultKey() {
874     shadowOf(devicePolicyManager).setStorageEncryptionStatus(ENCRYPTION_STATUS_ACTIVE_DEFAULT_KEY);
875     assertThat(devicePolicyManager.getStorageEncryptionStatus())
876         .isEqualTo(ENCRYPTION_STATUS_ACTIVE_DEFAULT_KEY);
877   }
878 
879   @Test
880   @Config(minSdk = N)
setStorageEncryptionStatus_ActivePerUser()881   public void setStorageEncryptionStatus_ActivePerUser() {
882     shadowOf(devicePolicyManager).setStorageEncryptionStatus(ENCRYPTION_STATUS_ACTIVE_PER_USER);
883     assertThat(devicePolicyManager.getStorageEncryptionStatus())
884         .isEqualTo(ENCRYPTION_STATUS_ACTIVE_PER_USER);
885   }
886 
887   @Test
setPasswordQuality_Complex()888   public void setPasswordQuality_Complex() {
889     shadowOf(devicePolicyManager).setProfileOwner(testComponent);
890 
891     devicePolicyManager.setPasswordQuality(
892         testComponent, DevicePolicyManager.PASSWORD_QUALITY_COMPLEX);
893     devicePolicyManager.setPasswordMinimumLength(testComponent, 7);
894     devicePolicyManager.setPasswordMinimumLetters(testComponent, 2);
895     devicePolicyManager.setPasswordMinimumUpperCase(testComponent, 1);
896 
897     assertThat(devicePolicyManager.resetPassword("aaaa", 0)).isFalse();
898     assertThat(devicePolicyManager.resetPassword("aA2!", 0)).isFalse();
899     assertThat(devicePolicyManager.resetPassword("aaaA123", 0)).isFalse();
900     assertThat(devicePolicyManager.resetPassword("AAAA123", 0)).isFalse();
901     assertThat(devicePolicyManager.resetPassword("!!AAAaaa", 0)).isFalse();
902     assertThat(devicePolicyManager.resetPassword("aaAA123!", 0)).isTrue();
903   }
904 
905   @Test
906   @Config(minSdk = N)
setPackagesSuspended_suspendsPossible()907   public void setPackagesSuspended_suspendsPossible() throws Exception {
908     shadowOf(devicePolicyManager).setProfileOwner(testComponent);
909     shadowOf(packageManager).addPackage("installed");
910     String[] packages = new String[] {"installed", "not.installed"};
911 
912     assertThat(devicePolicyManager.setPackagesSuspended(testComponent, packages, true))
913         .isEqualTo(new String[] {"not.installed"});
914   }
915 
916   @Test
917   @Config(minSdk = N)
setPackagesSuspended_activateActive()918   public void setPackagesSuspended_activateActive() throws Exception {
919     shadowOf(devicePolicyManager).setProfileOwner(testComponent);
920     shadowOf(packageManager).addPackage("package");
921 
922     assertThat(
923             devicePolicyManager.setPackagesSuspended(
924                 testComponent, new String[] {"package"}, false))
925         .isEmpty();
926     assertThat(devicePolicyManager.isPackageSuspended(testComponent, "package")).isFalse();
927   }
928 
929   @Test
930   @Config(minSdk = N)
setPackagesSuspended_cycleSuspension()931   public void setPackagesSuspended_cycleSuspension() throws Exception {
932     shadowOf(devicePolicyManager).setProfileOwner(testComponent);
933     shadowOf(packageManager).addPackage("package");
934 
935     devicePolicyManager.setPackagesSuspended(testComponent, new String[] {"package"}, true);
936     devicePolicyManager.setPackagesSuspended(testComponent, new String[] {"package"}, false);
937 
938     assertThat(devicePolicyManager.isPackageSuspended(testComponent, "package")).isFalse();
939   }
940 
941   @Test
942   @Config(minSdk = N)
isPackagesSuspended_defaultsFalse()943   public void isPackagesSuspended_defaultsFalse() throws Exception {
944     shadowOf(devicePolicyManager).setProfileOwner(testComponent);
945     shadowOf(packageManager).addPackage("package");
946 
947     assertThat(devicePolicyManager.isPackageSuspended(testComponent, "package")).isFalse();
948   }
949 
950   @Test
951   @Config(minSdk = N)
isPackagesSuspended_trueForSuspended()952   public void isPackagesSuspended_trueForSuspended() throws Exception {
953     shadowOf(devicePolicyManager).setProfileOwner(testComponent);
954     shadowOf(packageManager).addPackage("package");
955 
956     devicePolicyManager.setPackagesSuspended(testComponent, new String[] {"package"}, true);
957 
958     assertThat(devicePolicyManager.isPackageSuspended(testComponent, "package")).isTrue();
959   }
960 
961   @Test
962   @Config(minSdk = N)
isPackagesSuspended_notInstalledPackage()963   public void isPackagesSuspended_notInstalledPackage() throws Exception {
964     shadowOf(devicePolicyManager).setProfileOwner(testComponent);
965 
966     try {
967       devicePolicyManager.isPackageSuspended(testComponent, "not.installed");
968       fail("expected NameNotFoundException");
969     } catch (NameNotFoundException expected) {
970       // expected
971     }
972   }
973 
974   @Test
975   @Config(minSdk = N)
isLinkedUser()976   public void isLinkedUser() {
977     assertThat(devicePolicyManager.getUserProvisioningState()).isEqualTo(STATE_USER_UNMANAGED);
978 
979     shadowOf(devicePolicyManager).setUserProvisioningState(STATE_USER_SETUP_COMPLETE);
980     assertThat(devicePolicyManager.getUserProvisioningState()).isEqualTo(STATE_USER_SETUP_COMPLETE);
981 
982     shadowOf(devicePolicyManager).setUserProvisioningState(STATE_USER_SETUP_INCOMPLETE);
983     assertThat(devicePolicyManager.getUserProvisioningState())
984         .isEqualTo(STATE_USER_SETUP_INCOMPLETE);
985 
986     shadowOf(devicePolicyManager).setUserProvisioningState(STATE_USER_UNMANAGED);
987     assertThat(devicePolicyManager.getUserProvisioningState()).isEqualTo(STATE_USER_UNMANAGED);
988   }
989 
990   @Test
991   @Config(minSdk = LOLLIPOP)
getProfileOwnerNameAsUser()992   public void getProfileOwnerNameAsUser() {
993     int userId = 0;
994     String orgName = "organization";
995     assertThat(devicePolicyManager.getProfileOwnerNameAsUser(userId)).isNull();
996 
997     shadowOf(devicePolicyManager).setProfileOwnerName(userId, orgName);
998 
999     assertThat(devicePolicyManager.getProfileOwnerNameAsUser(userId)).isEqualTo(orgName);
1000   }
1001 
1002   // BEGIN-INTERNAL
1003   @Test
1004   @Config(minSdk = Q)
setAndGetCrossProfileCalendarPackages()1005   public void setAndGetCrossProfileCalendarPackages() {
1006       final String testPackageName = "testPackage";
1007       shadowOf(devicePolicyManager).setProfileOwner(testComponent);
1008       devicePolicyManager.setCrossProfileCalendarPackages(testComponent,
1009               Collections.singleton(testPackageName));
1010       final Set<String> packages = devicePolicyManager.getCrossProfileCalendarPackages(
1011               testComponent);
1012       assertThat(packages.size()).isEqualTo(1);
1013       assertThat(packages.toArray()[0]).isEqualTo(testPackageName);
1014   }
1015   // END-INTERNAL
1016 
1017   @Test
1018   @Config(minSdk = LOLLIPOP)
getProfileOwnerAsUser()1019   public void getProfileOwnerAsUser() {
1020       shadowOf(devicePolicyManager).setProfileOwner(testComponent);
1021       final ComponentName admin = devicePolicyManager.getProfileOwnerAsUser(1);
1022       assertThat(admin).isEqualTo(testComponent);
1023   }
1024 }
1025