1package org.robolectric.shadows;
2
3import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4import static android.os.Build.VERSION_CODES.LOLLIPOP;
5import static android.os.Build.VERSION_CODES.N;
6import static android.os.Build.VERSION_CODES.O;
7import static org.assertj.core.api.Assertions.assertThat;
8import static org.junit.Assert.fail;
9import static org.robolectric.Shadows.shadowOf;
10
11import android.app.admin.DevicePolicyManager;
12import android.content.ComponentName;
13import android.content.Context;
14import android.os.Bundle;
15import android.os.UserManager;
16import java.util.ArrayList;
17import java.util.Arrays;
18import java.util.List;
19import org.junit.Before;
20import org.junit.Test;
21import org.junit.runner.RunWith;
22import org.robolectric.RobolectricTestRunner;
23import org.robolectric.RuntimeEnvironment;
24import org.robolectric.annotation.Config;
25
26/** Unit tests for {@link ShadowDevicePolicyManager}. */
27@RunWith(RobolectricTestRunner.class)
28public final class ShadowDevicePolicyManagerTest {
29
30  private DevicePolicyManager devicePolicyManager;
31  private ShadowDevicePolicyManager shadowDevicePolicyManager;
32  private UserManager userManager;
33  private ComponentName testComponent;
34
35  @Before
36  public void setUp() {
37    devicePolicyManager =
38        (DevicePolicyManager)
39            RuntimeEnvironment.application.getSystemService(Context.DEVICE_POLICY_SERVICE);
40    shadowDevicePolicyManager = shadowOf(devicePolicyManager);
41
42    userManager =
43        (UserManager) RuntimeEnvironment.application.getSystemService(Context.USER_SERVICE);
44
45    testComponent = new ComponentName("com.example.app", "DeviceAdminReceiver");
46  }
47
48  @Test
49  @Config(minSdk = JELLY_BEAN_MR2)
50  public void isDeviceOwnerAppShouldReturnFalseForNonDeviceOwnerApp() {
51    // GIVEN an test package which is not the device owner app of the device
52    String testPackage = testComponent.getPackageName();
53
54    // WHEN DevicePolicyManager#isDeviceOwnerApp is called with it
55    // THEN the method should return false
56    assertThat(devicePolicyManager.isDeviceOwnerApp(testPackage)).isFalse();
57  }
58
59  @Test
60  @Config(minSdk = LOLLIPOP)
61  public void isDeviceOwnerShouldReturnFalseForProfileOwner() {
62    // GIVEN an test package which is the profile owner app of the device
63    String testPackage = testComponent.getPackageName();
64    shadowDevicePolicyManager.setProfileOwner(testComponent);
65
66    // WHEN DevicePolicyManager#isDeviceOwnerApp is called with it
67    // THEN the method should return false
68    assertThat(devicePolicyManager.isDeviceOwnerApp(testPackage)).isFalse();
69  }
70
71  @Test
72  @Config(minSdk = JELLY_BEAN_MR2)
73  public void isDeviceOwnerShouldReturnTrueForDeviceOwner() {
74    // GIVEN an test package which is the device owner app of the device
75    String testPackage = testComponent.getPackageName();
76    shadowDevicePolicyManager.setDeviceOwner(testComponent);
77
78    // WHEN DevicePolicyManager#isDeviceOwnerApp is called with it
79    // THEN the method should return true
80    assertThat(devicePolicyManager.isDeviceOwnerApp(testPackage)).isTrue();
81  }
82
83  @Test
84  @Config(minSdk = JELLY_BEAN_MR2)
85  public void getDeviceOwnerShouldReturnDeviceOwnerPackageName() {
86    // GIVEN an test package which is the device owner app of the device
87    String testPackage = testComponent.getPackageName();
88    shadowDevicePolicyManager.setDeviceOwner(testComponent);
89
90    // WHEN DevicePolicyManager#getDeviceOwner is called
91    // THEN the method should return the package name
92    assertThat(devicePolicyManager.getDeviceOwner()).isEqualTo(testPackage);
93  }
94
95  @Test
96  @Config(minSdk = JELLY_BEAN_MR2)
97  public void getDeviceOwnerShouldReturnNullWhenThereIsNoDeviceOwner() {
98    // WHEN DevicePolicyManager#getProfileOwner is called without a device owner
99    // THEN the method should return null
100    assertThat(devicePolicyManager.getDeviceOwner()).isNull();
101  }
102
103  @Test
104  @Config(minSdk = LOLLIPOP)
105  public void isProfileOwnerAppShouldReturnFalseForNonProfileOwnerApp() {
106    // GIVEN an test package which is not the profile owner app of the device
107    String testPackage = testComponent.getPackageName();
108
109    // WHEN DevicePolicyManager#isProfileOwnerApp is called with it
110    // THEN the method should return false
111    assertThat(devicePolicyManager.isProfileOwnerApp(testPackage)).isFalse();
112  }
113
114  @Test
115  @Config(minSdk = LOLLIPOP)
116  public void isProfileOwnerShouldReturnFalseForDeviceOwner() {
117    // GIVEN an test package which is the device owner app of the device
118    String testPackage = testComponent.getPackageName();
119    shadowDevicePolicyManager.setDeviceOwner(testComponent);
120
121    // WHEN DevicePolicyManager#isProfileOwnerApp is called with it
122    // THEN the method should return false
123    assertThat(devicePolicyManager.isProfileOwnerApp(testPackage)).isFalse();
124  }
125
126  @Test
127  @Config(minSdk = LOLLIPOP)
128  public void isProfileOwnerShouldReturnTrueForProfileOwner() {
129    // GIVEN an test package which is the profile owner app of the device
130    String testPackage = testComponent.getPackageName();
131    shadowDevicePolicyManager.setProfileOwner(testComponent);
132
133    // WHEN DevicePolicyManager#isProfileOwnerApp is called with it
134    // THEN the method should return true
135    assertThat(devicePolicyManager.isProfileOwnerApp(testPackage)).isTrue();
136  }
137
138  @Test
139  @Config(minSdk = LOLLIPOP)
140  public void getProfileOwnerShouldReturnDeviceOwnerComponentName() {
141    // GIVEN an test package which is the profile owner app of the device
142    shadowDevicePolicyManager.setProfileOwner(testComponent);
143
144    // WHEN DevicePolicyManager#getProfileOwner is called
145    // THEN the method should return the component
146    assertThat(devicePolicyManager.getProfileOwner()).isEqualTo(testComponent);
147  }
148
149  @Test
150  @Config(minSdk = LOLLIPOP)
151  public void getProfileOwnerShouldReturnNullWhenThereIsNoProfileOwner() {
152    // WHEN DevicePolicyManager#getProfileOwner is called without a profile owner
153    // THEN the method should return null
154    assertThat(devicePolicyManager.getProfileOwner()).isNull();
155  }
156
157
158  @Test
159  public void isAdminActiveShouldReturnFalseForNonAdminDevice() {
160    // GIVEN a test component which is not an active admin of the device
161    // WHEN DevicePolicyManager#isAdminActive is called with it
162    // THEN the method should return false
163    assertThat(devicePolicyManager.isAdminActive(testComponent)).isFalse();
164  }
165
166  @Test
167  public void isAdminActiveShouldReturnTrueForAnyDeviceAdminDevice() {
168    // GIVEN a test component which is an active admin of the device
169    shadowDevicePolicyManager.setActiveAdmin(testComponent);
170
171    // WHEN DevicePolicyManager#isAdminActive is called with it
172    // THEN the method should return true
173    assertThat(devicePolicyManager.isAdminActive(testComponent)).isTrue();
174  }
175
176  @Test
177  @Config(minSdk = JELLY_BEAN_MR2)
178  public void getActiveAdminsShouldReturnDeviceOwner() {
179    // GIVEN an test package which is the device owner app of the device
180    shadowDevicePolicyManager.setDeviceOwner(testComponent);
181
182    // WHEN DevicePolicyManager#getActiveAdmins is called
183    // THEN the return of the method should include the device owner app
184    assertThat(devicePolicyManager.getActiveAdmins()).contains(testComponent);
185  }
186
187  @Test
188  @Config(minSdk = LOLLIPOP)
189  public void getActiveAdminsShouldReturnProfileOwner() {
190    // GIVEN an test package which is the profile owner app of the device
191    shadowDevicePolicyManager.setProfileOwner(testComponent);
192
193    // WHEN DevicePolicyManager#getActiveAdmins is called
194    // THEN the return of the method should include the profile owner app
195    assertThat(devicePolicyManager.getActiveAdmins()).contains(testComponent);
196  }
197
198  @Test
199  @Config(minSdk = LOLLIPOP)
200  public void addUserRestrictionShouldWorkAsIntendedForDeviceOwner() {
201    // GIVEN a user restriction to set
202    String restrictionKey = "restriction key";
203
204    // GIVEN the caller is the device owner
205    shadowDevicePolicyManager.setDeviceOwner(testComponent);
206
207    // WHEN DevicePolicyManager#addUserRestriction is called with the key
208    devicePolicyManager.addUserRestriction(testComponent, restrictionKey);
209
210    // THEN the restriction should be set for the current user
211    Bundle restrictions = userManager.getUserRestrictions();
212    assertThat(restrictions.getBoolean(restrictionKey)).isTrue();
213  }
214
215  @Test
216  @Config(minSdk = LOLLIPOP)
217  public void addUserRestrictionShouldWorkAsIntendedForProfileOwner() {
218    // GIVEN a user restriction to set
219    String restrictionKey = "restriction key";
220
221    // GIVEN the caller is the profile owner
222    shadowDevicePolicyManager.setProfileOwner(testComponent);
223
224    // WHEN DevicePolicyManager#addUserRestriction is called with the key
225    devicePolicyManager.addUserRestriction(testComponent, restrictionKey);
226
227    // THEN the restriction should be set for the current user
228    Bundle restrictions = userManager.getUserRestrictions();
229    assertThat(restrictions.getBoolean(restrictionKey)).isTrue();
230  }
231
232  @Test
233  @Config(minSdk = LOLLIPOP)
234  public void clearUserRestrictionShouldWorkAsIntendedForActiveAdmins() {
235    // GIVEN the caller is the device owner, and thus an active admin
236    shadowDevicePolicyManager.setDeviceOwner(testComponent);
237
238    // GIVEN a user restriction has set
239    String restrictionKey = "restriction key";
240    devicePolicyManager.addUserRestriction(testComponent, restrictionKey);
241
242    // WHEN DevicePolicyManager#clearUserRestriction is called with the key
243    devicePolicyManager.clearUserRestriction(testComponent, restrictionKey);
244
245    // THEN the restriction should be cleared for the current user
246    Bundle restrictions = userManager.getUserRestrictions();
247    assertThat(restrictions.getBoolean(restrictionKey)).isFalse();
248  }
249
250  @Test
251  @Config(minSdk = LOLLIPOP)
252  public void isApplicationHiddenShouldReturnFalseForAppsByDefault() {
253    // GIVEN the caller is the device owner, and thus an active admin
254    shadowDevicePolicyManager.setDeviceOwner(testComponent);
255
256    // GIVEN an app and it's never be set hidden or non hidden
257    String app = "com.example.non.hidden";
258
259    // WHEN DevicePolicyManager#isApplicationHidden is called on the app
260    // THEN it should return false
261    assertThat(devicePolicyManager.isApplicationHidden(testComponent, app)).isFalse();
262  }
263
264  @Test
265  @Config(minSdk = LOLLIPOP)
266  public void isApplicationHiddenShouldReturnTrueForHiddenApps() {
267    // GIVEN the caller is the device owner, and thus an active admin
268    shadowDevicePolicyManager.setDeviceOwner(testComponent);
269
270    // GIVEN an app and it is hidden
271    String hiddenApp = "com.example.hidden";
272    devicePolicyManager.setApplicationHidden(testComponent, hiddenApp, true);
273
274    // WHEN DevicePolicyManager#isApplicationHidden is called on the app
275    // THEN it should return true
276    assertThat(devicePolicyManager.isApplicationHidden(testComponent, hiddenApp)).isTrue();
277  }
278
279  @Test
280  @Config(minSdk = LOLLIPOP)
281  public void isApplicationHiddenShouldReturnFalseForNonHiddenApps() {
282    // GIVEN the caller is the device owner, and thus an active admin
283    shadowDevicePolicyManager.setDeviceOwner(testComponent);
284
285    // GIVEN an app and it is not hidden
286    String nonHiddenApp = "com.example.non.hidden";
287    devicePolicyManager.setApplicationHidden(testComponent, nonHiddenApp, false);
288
289    // WHEN DevicePolicyManager#isApplicationHidden is called on the app
290    // THEN it should return false
291    assertThat(devicePolicyManager.isApplicationHidden(testComponent, nonHiddenApp)).isFalse();
292  }
293
294  @Test
295  @Config(minSdk = LOLLIPOP)
296  public void setApplicationHiddenShouldBeAbleToUnhideHiddenApps() {
297    // GIVEN the caller is the device owner, and thus an active admin
298    shadowDevicePolicyManager.setDeviceOwner(testComponent);
299
300    // GIVEN an app and it is hidden
301    String app = "com.example.hidden";
302    devicePolicyManager.setApplicationHidden(testComponent, app, true);
303
304    // WHEN DevicePolicyManager#setApplicationHidden is called on the app to unhide it
305    devicePolicyManager.setApplicationHidden(testComponent, app, false);
306
307    // THEN the app shouldn't be hidden anymore
308    assertThat(devicePolicyManager.isApplicationHidden(testComponent, app)).isFalse();
309  }
310
311  @Test
312  @Config(minSdk = LOLLIPOP)
313  public void wasPackageEverHiddenShouldReturnFalseForPackageNeverHidden() {
314    // GIVEN the caller is the device owner, and thus an active admin
315    shadowDevicePolicyManager.setDeviceOwner(testComponent);
316
317    // GIVEN an app and it's never be set hidden or non hidden
318    String app = "com.example.non.hidden";
319
320    // WHEN ShadowDevicePolicyManager#wasPackageEverHidden is called with the app
321    // THEN it should return false
322    assertThat(shadowDevicePolicyManager.wasPackageEverHidden(app)).isFalse();
323  }
324
325  @Test
326  @Config(minSdk = LOLLIPOP)
327  public void wasPackageEverHiddenShouldReturnTrueForPackageWhichIsHidden() {
328    // GIVEN the caller is the device owner, and thus an active admin
329    shadowDevicePolicyManager.setDeviceOwner(testComponent);
330
331    // GIVEN an app and it's hidden
332    String hiddenApp = "com.example.hidden";
333    devicePolicyManager.setApplicationHidden(testComponent, hiddenApp, true);
334
335    // WHEN ShadowDevicePolicyManager#wasPackageEverHidden is called with the app
336    // THEN it should return true
337    assertThat(shadowDevicePolicyManager.wasPackageEverHidden(hiddenApp)).isTrue();
338  }
339
340  @Test
341  @Config(minSdk = LOLLIPOP)
342  public void wasPackageEverHiddenShouldReturnTrueForPackageWhichWasHidden() {
343    // GIVEN the caller is the device owner, and thus an active admin
344    shadowDevicePolicyManager.setDeviceOwner(testComponent);
345
346    // GIVEN an app and it was hidden
347    String app = "com.example.hidden";
348    devicePolicyManager.setApplicationHidden(testComponent, app, true);
349    devicePolicyManager.setApplicationHidden(testComponent, app, false);
350
351    // WHEN ShadowDevicePolicyManager#wasPackageEverHidden is called with the app
352    // THEN it should return true
353    assertThat(shadowDevicePolicyManager.wasPackageEverHidden(app)).isTrue();
354  }
355
356  @Test
357  @Config(minSdk = LOLLIPOP)
358  public void enableSystemAppShouldWorkForActiveAdmins() {
359    // GIVEN the caller is the device owner, and thus an active admin
360    shadowDevicePolicyManager.setDeviceOwner(testComponent);
361
362    // GIVEN a system app
363    String app = "com.example.system";
364
365    // WHEN DevicePolicyManager#enableSystemApp is called with the app
366    devicePolicyManager.enableSystemApp(testComponent, app);
367
368    // THEN the app should be enabled
369    assertThat(shadowDevicePolicyManager.wasSystemAppEnabled(app)).isTrue();
370  }
371
372  @Test
373  @Config(minSdk = LOLLIPOP)
374  public void isUninstallBlockedShouldReturnFalseForAppsNeverBeingBlocked() {
375    // GIVEN the caller is the device owner, and thus an active admin
376    shadowDevicePolicyManager.setDeviceOwner(testComponent);
377
378    // GIVEN an app
379    String app = "com.example.app";
380
381    // WHEN DevicePolicyManager#isUninstallBlocked is called with the app
382    // THEN it should return false
383    assertThat(devicePolicyManager.isUninstallBlocked(testComponent, app)).isFalse();
384  }
385
386  @Test
387  @Config(minSdk = LOLLIPOP)
388  public void isUninstallBlockedShouldReturnTrueForAppsBeingUnblocked() {
389    // GIVEN the caller is the device owner, and thus an active admin
390    shadowDevicePolicyManager.setDeviceOwner(testComponent);
391
392    // GIVEN an app which is blocked from being uninstalled
393    String app = "com.example.app";
394    devicePolicyManager.setUninstallBlocked(testComponent, app, true);
395
396    // WHEN DevicePolicyManager#UninstallBlocked is called with the app
397    // THEN it should return true
398    assertThat(devicePolicyManager.isUninstallBlocked(testComponent, app)).isTrue();
399  }
400
401  @Test
402  @Config(minSdk = LOLLIPOP)
403  public void isUninstallBlockedShouldReturnFalseForAppsBeingBlocked() {
404    // GIVEN the caller is the device owner, and thus an active admin
405    shadowDevicePolicyManager.setDeviceOwner(testComponent);
406
407    // GIVEN an app which is unblocked from being uninstalled
408    String app = "com.example.app";
409    devicePolicyManager.setUninstallBlocked(testComponent, app, true);
410    devicePolicyManager.setUninstallBlocked(testComponent, app, false);
411
412    // WHEN DevicePolicyManager#UninstallBlocked is called with the app
413    // THEN it should return false
414    assertThat(devicePolicyManager.isUninstallBlocked(testComponent, app)).isFalse();
415  }
416
417  @Test
418  @Config(minSdk = LOLLIPOP)
419  public void setApplicationRestrictionsShouldWorkAsIntendedForDeviceOwner() {
420    // GIVEN the caller is the device owner
421    shadowDevicePolicyManager.setDeviceOwner(testComponent);
422
423    // GIVEN an application restriction bundle
424    Bundle restrictions = new Bundle();
425    restrictions.putString("key", "value");
426
427    // GIVEN an app which the restriction is set to
428    String app = "com.example.app";
429
430    // WHEN DevicePolicyManager#setApplicationRestrictions is called to set the restrictions to the
431    // app
432    devicePolicyManager.setApplicationRestrictions(testComponent, app, restrictions);
433
434    // THEN the restrictions should be set correctly
435    Bundle actualRestrictions = devicePolicyManager.getApplicationRestrictions(testComponent, app);
436    assertThat(actualRestrictions.getString("key", "default value")).isEqualTo("value");
437  }
438
439  @Test
440  @Config(minSdk = LOLLIPOP)
441  public void setApplicationRestrictionsShouldWorkAsIntendedForProfileOwner() {
442    // GIVEN the caller is the profile owner
443    shadowDevicePolicyManager.setProfileOwner(testComponent);
444
445    // GIVEN an application restriction bundle
446    Bundle restrictions = new Bundle();
447    restrictions.putString("key", "value");
448
449    // GIVEN an app which the restriction is set to
450    String app = "com.example.app";
451
452    // WHEN DevicePolicyManager#setApplicationRestrictions is called to set the restrictions to the
453    // app
454    devicePolicyManager.setApplicationRestrictions(testComponent, app, restrictions);
455
456    // THEN the restrictions should be set correctly
457    Bundle actualRestrictions = devicePolicyManager.getApplicationRestrictions(testComponent, app);
458    assertThat(actualRestrictions.getString("key", "default value")).isEqualTo("value");
459  }
460
461  @Test
462  @Config(minSdk = LOLLIPOP)
463  public void getApplicationRestrictionsShouldReturnEmptyBundleIfAppHasNone() {
464    // GIVEN the caller is the device owner
465    shadowDevicePolicyManager.setDeviceOwner(testComponent);
466
467    // GIVEN an app has no restrictions
468    String app = "com.example.app";
469
470    // WHEN DevicePolicyManager#getApplicationRestrictions is called to get the restrictions of the
471    // app
472    // THEN it should return the empty bundle
473    assertThat(devicePolicyManager.getApplicationRestrictions(testComponent, app))
474        .isEqualTo(Bundle.EMPTY);
475  }
476
477  @Test
478  @Config(minSdk = LOLLIPOP)
479  public void getAccountTypesWithManagementDisabledShouldReturnNothingWhenNoAccountIsDislabed() {
480    // GIVEN no account type has ever been disabled
481
482    // WHEN get disabled account types using
483    // DevicePolicyManager#getAccountTypesWithManagementDisabled
484    // THEN it should be empty
485    assertThat(devicePolicyManager.getAccountTypesWithManagementDisabled()).isEmpty();
486  }
487
488  @Test
489  @Config(minSdk = LOLLIPOP)
490  public void getAccountTypesWithManagementDisabledShouldReturnDisabledAccountTypesIfAny() {
491    // GIVEN the caller is the device owner
492    shadowDevicePolicyManager.setDeviceOwner(testComponent);
493
494    // GIVEN a disabled account type
495    String disabledAccountType = "com.example.account.type";
496    devicePolicyManager.setAccountManagementDisabled(testComponent, disabledAccountType, true);
497
498    // WHEN get disabled account types using
499    // DevicePolicyManager#getAccountTypesWithManagementDisabled
500    // THEN it should contain the disabled account type
501    assertThat(devicePolicyManager.getAccountTypesWithManagementDisabled())
502        .isEqualTo(new String[] {disabledAccountType});
503  }
504
505  @Test
506  @Config(minSdk = LOLLIPOP)
507  public void getAccountTypesWithManagementDisabledShouldNotReturnReenabledAccountTypesIfAny() {
508    // GIVEN the caller is the device owner
509    shadowDevicePolicyManager.setDeviceOwner(testComponent);
510
511    // GIVEN a re-enabled account type
512    String reenabledAccountType = "com.example.account.type";
513    devicePolicyManager.setAccountManagementDisabled(testComponent, reenabledAccountType, true);
514    devicePolicyManager.setAccountManagementDisabled(testComponent, reenabledAccountType, false);
515
516    // WHEN get disabled account types using
517    // DevicePolicyManager#getAccountTypesWithManagementDisabled
518    // THEN it should not contain the re-enabled account type
519    assertThat(devicePolicyManager.getAccountTypesWithManagementDisabled()).isEmpty();
520  }
521
522  @Test
523  @Config(minSdk = N)
524  public void setOrganizationNameShouldWorkForPoSinceN() {
525    // GIVEN the caller is the profile owner
526    shadowDevicePolicyManager.setProfileOwner(testComponent);
527
528    // WHEN setting an organization name
529    String organizationName = "TestOrg";
530    devicePolicyManager.setOrganizationName(testComponent, organizationName);
531
532    // THEN the name should be set properly
533    assertThat(devicePolicyManager.getOrganizationName(testComponent)).isEqualTo(organizationName);
534  }
535
536  @Test
537  @Config(minSdk = N)
538  public void setOrganizationNameShouldClearNameWithEmptyNameForPoSinceN() {
539    // GIVEN the caller is the profile owner
540    shadowDevicePolicyManager.setProfileOwner(testComponent);
541
542    // GIVEN that the profile has already set the name TestOrg
543    String organizationName = "TestOrg";
544    devicePolicyManager.setOrganizationName(testComponent, organizationName);
545
546    // WHEN setting an organization name to empty
547    devicePolicyManager.setOrganizationName(testComponent, "");
548
549    // THEN the name should be cleared
550    assertThat(devicePolicyManager.getOrganizationName(testComponent)).isNull();
551  }
552
553  @Test
554  @Config(sdk = N)
555  public void setOrganizationNameShouldNotWorkForDoInN() {
556    // GIVEN the caller is the device owner
557    shadowDevicePolicyManager.setDeviceOwner(testComponent);
558
559    // WHEN setting an organization name
560    // THEN the method should throw SecurityException
561    String organizationName = "TestOrg";
562    try {
563      devicePolicyManager.setOrganizationName(testComponent, organizationName);
564      fail("expected SecurityException");
565    } catch (SecurityException expected) {
566    }
567  }
568
569  @Test
570  @Config(minSdk = O)
571  public void setOrganizationNameShouldWorkForDoSinceO() {
572    // GIVEN the caller is the device owner
573    shadowDevicePolicyManager.setDeviceOwner(testComponent);
574
575    // WHEN setting an organization name
576    String organizationName = "TestOrg";
577    devicePolicyManager.setOrganizationName(testComponent, organizationName);
578
579    // THEN the name should be set properly
580    assertThat(devicePolicyManager.getOrganizationName(testComponent)).isEqualTo(organizationName);
581  }
582
583  @Test
584  @Config(minSdk = N)
585  public void setOrganizationColorShouldWorkForPoSinceN() {
586    // GIVEN the caller is the profile owner
587    shadowDevicePolicyManager.setProfileOwner(testComponent);
588
589    // WHEN setting an organization color
590    int color = 0xFFFF00FF;
591    devicePolicyManager.setOrganizationColor(testComponent, color);
592
593    // THEN the color should be set properly
594    assertThat(devicePolicyManager.getOrganizationColor(testComponent)).isEqualTo(color);
595  }
596
597  @Test
598  @Config(minSdk = N)
599  public void getOrganizationColorShouldReturnDefaultColorIfNothingSet() {
600    // GIVEN the caller is the profile owner
601    shadowDevicePolicyManager.setProfileOwner(testComponent);
602
603    // WHEN getting an organization color without setting it
604    // THEN the color returned should be the default color
605    assertThat(devicePolicyManager.getOrganizationColor(testComponent)).isEqualTo(0xFF008080);
606  }
607
608  @Test
609  @Config(minSdk = N)
610  public void setOrganizationColorShouldNotWorkForDo() {
611    // GIVEN the caller is the device owner
612    shadowDevicePolicyManager.setDeviceOwner(testComponent);
613
614    // WHEN setting an organization color
615    // THEN the method should throw SecurityException
616    int color = 0xFFFF00FF;
617    try {
618      devicePolicyManager.setOrganizationColor(testComponent, color);
619      fail("expected SecurityException");
620    } catch (SecurityException expected) {
621    }
622  }
623
624  @Test
625  @Config(minSdk = LOLLIPOP)
626  public void getAutoTimeRequiredShouldWorkAsIntendedForDeviceOwner() {
627    // GIVEN the caller is the device owner
628    shadowDevicePolicyManager.setDeviceOwner(testComponent);
629
630    // WHEN setAutoTimeRequired is called with true
631    devicePolicyManager.setAutoTimeRequired(testComponent, true);
632
633    // THEN getAutoTimeRequired should return true
634    assertThat(devicePolicyManager.getAutoTimeRequired()).isTrue();
635  }
636
637  @Test
638  @Config(minSdk = LOLLIPOP)
639  public void getAutoTimeRequiredShouldWorkAsIntendedForProfileOwner() {
640    // GIVEN the caller is the profile owner
641    shadowDevicePolicyManager.setProfileOwner(testComponent);
642
643    // WHEN setAutoTimeRequired is called with false
644    devicePolicyManager.setAutoTimeRequired(testComponent, false);
645
646    // THEN getAutoTimeRequired should return false
647    assertThat(devicePolicyManager.getAutoTimeRequired()).isFalse();
648  }
649
650  @Test
651  @Config(minSdk = LOLLIPOP)
652  public void getAutoTimeRequiredShouldReturnFalseIfNotSet() {
653    // GIVEN the caller is the device owner
654    shadowDevicePolicyManager.setDeviceOwner(testComponent);
655
656    // WHEN setAutoTimeRequired has not been called
657    // THEN getAutoTimeRequired should return false
658    assertThat(devicePolicyManager.getAutoTimeRequired()).isFalse();
659  }
660
661  @Test
662  @Config(minSdk = LOLLIPOP)
663  public void getPermittedAccessibilityServicesShouldWorkAsIntendedForDeviceOwner() {
664    List<String> accessibilityServices =
665        Arrays.asList("com.example.accessibility1", "com.example.accessibility2");
666
667    // GIVEN the caller is the device owner
668    shadowDevicePolicyManager.setDeviceOwner(testComponent);
669
670    // WHEN setPermittedAccessibilityServices is called with a valid list
671    devicePolicyManager.setPermittedAccessibilityServices(testComponent, accessibilityServices);
672
673    // THEN getAutoTimeRequired should return the list
674    assertThat(devicePolicyManager.getPermittedAccessibilityServices(testComponent))
675        .isEqualTo(accessibilityServices);
676  }
677
678  @Test
679  @Config(minSdk = LOLLIPOP)
680  public void getPermittedAccessibilityServicesShouldWorkAsIntendedForProfileOwner() {
681    List<String> accessibilityServices = new ArrayList<>();
682
683    // GIVEN the caller is the profile owner
684    shadowDevicePolicyManager.setProfileOwner(testComponent);
685
686    // WHEN setPermittedAccessibilityServices is called with an empty list
687    devicePolicyManager.setPermittedAccessibilityServices(testComponent, accessibilityServices);
688
689    // THEN getAutoTimeRequired should return an empty list
690    assertThat(devicePolicyManager.getPermittedAccessibilityServices(testComponent)).isEmpty();
691  }
692
693  @Test
694  @Config(minSdk = LOLLIPOP)
695  public void getPermittedAccessibilityServicesShouldReturnNullIfNullIsSet() {
696    List<String> accessibilityServices = null;
697
698    // GIVEN the caller is the device owner
699    shadowDevicePolicyManager.setDeviceOwner(testComponent);
700
701    // WHEN setPermittedAccessibilityServices is called with a null list
702    devicePolicyManager.setPermittedAccessibilityServices(testComponent, accessibilityServices);
703
704    // THEN getAutoTimeRequired should return null
705    assertThat(devicePolicyManager.getPermittedAccessibilityServices(testComponent)).isNull();
706  }
707
708  @Test
709  @Config(minSdk = LOLLIPOP)
710  public void getPermittedInputMethodsShouldWorkAsIntendedForDeviceOwner() {
711    List<String> inputMethods = Arrays.asList("com.example.input1", "com.example.input2");
712
713    // GIVEN the caller is the device owner
714    shadowDevicePolicyManager.setDeviceOwner(testComponent);
715
716    // WHEN setPermittedInputMethods is called with a valid list
717    devicePolicyManager.setPermittedInputMethods(testComponent, inputMethods);
718
719    // THEN getAutoTimeRequired should return the list
720    assertThat(devicePolicyManager.getPermittedInputMethods(testComponent)).isEqualTo(inputMethods);
721  }
722
723  @Test
724  @Config(minSdk = LOLLIPOP)
725  public void getPermittedInputMethodsShouldWorkAsIntendedForProfileOwner() {
726    List<String> inputMethods = new ArrayList<>();
727
728    // GIVEN the caller is the profile owner
729    shadowDevicePolicyManager.setProfileOwner(testComponent);
730
731    // WHEN setPermittedInputMethods is called with an empty list
732    devicePolicyManager.setPermittedInputMethods(testComponent, inputMethods);
733
734    // THEN getAutoTimeRequired should return an empty list
735    assertThat(devicePolicyManager.getPermittedInputMethods(testComponent)).isEmpty();
736  }
737
738  @Test
739  @Config(minSdk = LOLLIPOP)
740  public void getPermittedInputMethodsShouldReturnNullIfNullIsSet() {
741    List<String> inputMethods = null;
742
743    // GIVEN the caller is the device owner
744    shadowDevicePolicyManager.setDeviceOwner(testComponent);
745
746    // WHEN setPermittedInputMethods is called with a null list
747    devicePolicyManager.setPermittedInputMethods(testComponent, inputMethods);
748
749    // THEN getAutoTimeRequired should return null
750    assertThat(devicePolicyManager.getPermittedInputMethods(testComponent)).isNull();
751  }
752}
753