2 * Copyright (C) 2015 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.server.pm;
19 import android.os.Bundle;
20 import android.os.UserHandle;
21 import android.os.UserManager;
22 import android.test.AndroidTestCase;
23 import android.test.suitebuilder.annotation.SmallTest;
25 import com.android.server.devicepolicy.DpmTestUtils;
28 * Tests for {@link com.android.server.pm.UserRestrictionsUtils}.
31 m FrameworksServicesTests &&
33 -r out/target/product/hammerhead/data/app/FrameworksServicesTests/FrameworksServicesTests.apk &&
34 adb shell am instrument -e class com.android.server.pm.UserRestrictionsUtilsTest \
35 -w com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner
39 public class UserRestrictionsUtilsTest extends AndroidTestCase {
40 public void testNonNull() {
41 Bundle out = UserRestrictionsUtils.nonNull(null);
43 out.putBoolean("a", true); // Should not be Bundle.EMPTY.
45 Bundle in = new Bundle();
46 assertSame(in, UserRestrictionsUtils.nonNull(in));
49 public void testIsEmpty() {
50 assertTrue(UserRestrictionsUtils.isEmpty(null));
51 assertTrue(UserRestrictionsUtils.isEmpty(new Bundle()));
52 assertFalse(UserRestrictionsUtils.isEmpty(DpmTestUtils.newRestrictions("a")));
55 public void testClone() {
56 Bundle in = new Bundle();
57 Bundle out = UserRestrictionsUtils.clone(in);
58 assertNotSame(in, out);
59 DpmTestUtils.assertRestrictions(out, new Bundle());
61 out = UserRestrictionsUtils.clone(null);
63 out.putBoolean("a", true); // Should not be Bundle.EMPTY.
66 public void testMerge() {
67 Bundle a = DpmTestUtils.newRestrictions("a", "d");
68 Bundle b = DpmTestUtils.newRestrictions("b", "d", "e");
70 UserRestrictionsUtils.merge(a, b);
72 DpmTestUtils.assertRestrictions(DpmTestUtils.newRestrictions("a", "b", "d", "e"), a);
74 UserRestrictionsUtils.merge(a, null);
76 DpmTestUtils.assertRestrictions(DpmTestUtils.newRestrictions("a", "b", "d", "e"), a);
79 UserRestrictionsUtils.merge(a, a);
81 } catch (IllegalArgumentException expected) {
85 public void testCanDeviceOwnerChange() {
86 assertFalse(UserRestrictionsUtils.canDeviceOwnerChange(UserManager.DISALLOW_RECORD_AUDIO));
87 assertFalse(UserRestrictionsUtils.canDeviceOwnerChange(UserManager.DISALLOW_WALLPAPER));
88 assertTrue(UserRestrictionsUtils.canDeviceOwnerChange(UserManager.DISALLOW_ADD_USER));
91 public void testCanProfileOwnerChange() {
92 int user = UserHandle.USER_SYSTEM;
93 assertFalse(UserRestrictionsUtils.canProfileOwnerChange(
94 UserManager.DISALLOW_RECORD_AUDIO, user));
95 assertFalse(UserRestrictionsUtils.canProfileOwnerChange(
96 UserManager.DISALLOW_WALLPAPER, user));
97 assertTrue(UserRestrictionsUtils.canProfileOwnerChange(
98 UserManager.DISALLOW_ADD_USER, user));
99 assertTrue(UserRestrictionsUtils.canProfileOwnerChange(
100 UserManager.DISALLOW_ADJUST_VOLUME, user));
103 assertFalse(UserRestrictionsUtils.canProfileOwnerChange(
104 UserManager.DISALLOW_RECORD_AUDIO, user));
105 assertFalse(UserRestrictionsUtils.canProfileOwnerChange(
106 UserManager.DISALLOW_WALLPAPER, user));
107 assertFalse(UserRestrictionsUtils.canProfileOwnerChange(
108 UserManager.DISALLOW_ADD_USER, user));
109 assertTrue(UserRestrictionsUtils.canProfileOwnerChange(
110 UserManager.DISALLOW_ADJUST_VOLUME, user));
113 public void testSortToGlobalAndLocal() {
114 final Bundle local = new Bundle();
115 final Bundle global = new Bundle();
117 UserRestrictionsUtils.sortToGlobalAndLocal(null, global, local);
118 assertEquals(0, global.size());
119 assertEquals(0, local.size());
121 UserRestrictionsUtils.sortToGlobalAndLocal(Bundle.EMPTY, global, local);
122 assertEquals(0, global.size());
123 assertEquals(0, local.size());
125 UserRestrictionsUtils.sortToGlobalAndLocal(DpmTestUtils.newRestrictions(
126 UserManager.DISALLOW_ADJUST_VOLUME,
127 UserManager.DISALLOW_UNMUTE_MICROPHONE,
128 UserManager.DISALLOW_USB_FILE_TRANSFER,
129 UserManager.DISALLOW_CONFIG_TETHERING,
130 UserManager.DISALLOW_OUTGOING_BEAM,
131 UserManager.DISALLOW_APPS_CONTROL
135 DpmTestUtils.assertRestrictions(DpmTestUtils.newRestrictions(
136 // These can be set by PO too, but when DO sets them, they're global.
137 UserManager.DISALLOW_ADJUST_VOLUME,
138 UserManager.DISALLOW_UNMUTE_MICROPHONE,
140 // These can only be set by DO.
141 UserManager.DISALLOW_USB_FILE_TRANSFER,
142 UserManager.DISALLOW_CONFIG_TETHERING
145 DpmTestUtils.assertRestrictions(DpmTestUtils.newRestrictions(
146 // They can be set by both DO/PO.
147 UserManager.DISALLOW_OUTGOING_BEAM,
148 UserManager.DISALLOW_APPS_CONTROL
152 public void testAreEqual() {
153 assertTrue(UserRestrictionsUtils.areEqual(
157 assertTrue(UserRestrictionsUtils.areEqual(
161 assertTrue(UserRestrictionsUtils.areEqual(
165 assertTrue(UserRestrictionsUtils.areEqual(
169 assertTrue(UserRestrictionsUtils.areEqual(
173 assertFalse(UserRestrictionsUtils.areEqual(
175 DpmTestUtils.newRestrictions("a")));
177 assertFalse(UserRestrictionsUtils.areEqual(
178 DpmTestUtils.newRestrictions("a"),
181 assertTrue(UserRestrictionsUtils.areEqual(
182 DpmTestUtils.newRestrictions("a"),
183 DpmTestUtils.newRestrictions("a")));
185 assertFalse(UserRestrictionsUtils.areEqual(
186 DpmTestUtils.newRestrictions("a"),
187 DpmTestUtils.newRestrictions("a", "b")));
189 assertFalse(UserRestrictionsUtils.areEqual(
190 DpmTestUtils.newRestrictions("a", "b"),
191 DpmTestUtils.newRestrictions("a")));
193 assertFalse(UserRestrictionsUtils.areEqual(
194 DpmTestUtils.newRestrictions("b", "a"),
195 DpmTestUtils.newRestrictions("a", "a")));
197 // Make sure false restrictions are handled correctly.
198 final Bundle a = DpmTestUtils.newRestrictions("a");
199 a.putBoolean("b", true);
201 final Bundle b = DpmTestUtils.newRestrictions("a");
202 b.putBoolean("b", false);
204 assertFalse(UserRestrictionsUtils.areEqual(a, b));
205 assertFalse(UserRestrictionsUtils.areEqual(b, a));