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