OSDN Git Service

DO NOT MERGE. Grant MMS Uri permissions as the calling UID.
[android-x86/frameworks-base.git] / services / tests / servicestests / src / com / android / server / pm / UserRestrictionsUtilsTest.java
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.android.server.pm;
18
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;
24
25 import com.android.server.devicepolicy.DpmTestUtils;
26
27 /**
28  * Tests for {@link com.android.server.pm.UserRestrictionsUtils}.
29  *
30  * <p>Run with:<pre>
31    m FrameworksServicesTests &&
32    adb install \
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
36  * </pre>
37  */
38 @SmallTest
39 public class UserRestrictionsUtilsTest extends AndroidTestCase {
40     public void testNonNull() {
41         Bundle out = UserRestrictionsUtils.nonNull(null);
42         assertNotNull(out);
43         out.putBoolean("a", true); // Should not be Bundle.EMPTY.
44
45         Bundle in = new Bundle();
46         assertSame(in, UserRestrictionsUtils.nonNull(in));
47     }
48
49     public void testIsEmpty() {
50         assertTrue(UserRestrictionsUtils.isEmpty(null));
51         assertTrue(UserRestrictionsUtils.isEmpty(new Bundle()));
52         assertFalse(UserRestrictionsUtils.isEmpty(DpmTestUtils.newRestrictions("a")));
53     }
54
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());
60
61         out = UserRestrictionsUtils.clone(null);
62         assertNotNull(out);
63         out.putBoolean("a", true); // Should not be Bundle.EMPTY.
64     }
65
66     public void testMerge() {
67         Bundle a = DpmTestUtils.newRestrictions("a", "d");
68         Bundle b = DpmTestUtils.newRestrictions("b", "d", "e");
69
70         UserRestrictionsUtils.merge(a, b);
71
72         DpmTestUtils.assertRestrictions(DpmTestUtils.newRestrictions("a", "b", "d", "e"), a);
73
74         UserRestrictionsUtils.merge(a, null);
75
76         DpmTestUtils.assertRestrictions(DpmTestUtils.newRestrictions("a", "b", "d", "e"), a);
77
78         try {
79             UserRestrictionsUtils.merge(a, a);
80             fail();
81         } catch (IllegalArgumentException expected) {
82         }
83     }
84
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));
89     }
90
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));
101
102         user = 10;
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));
111     }
112
113     public void testSortToGlobalAndLocal() {
114         final Bundle local = new Bundle();
115         final Bundle global = new Bundle();
116
117         UserRestrictionsUtils.sortToGlobalAndLocal(null, global, local);
118         assertEquals(0, global.size());
119         assertEquals(0, local.size());
120
121         UserRestrictionsUtils.sortToGlobalAndLocal(Bundle.EMPTY, global, local);
122         assertEquals(0, global.size());
123         assertEquals(0, local.size());
124
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
132         ), global, local);
133
134
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,
139
140                 // These can only be set by DO.
141                 UserManager.DISALLOW_USB_FILE_TRANSFER,
142                 UserManager.DISALLOW_CONFIG_TETHERING
143         ), global);
144
145         DpmTestUtils.assertRestrictions(DpmTestUtils.newRestrictions(
146                 // They can be set by both DO/PO.
147                 UserManager.DISALLOW_OUTGOING_BEAM,
148                 UserManager.DISALLOW_APPS_CONTROL
149         ), local);
150     }
151
152     public void testAreEqual() {
153         assertTrue(UserRestrictionsUtils.areEqual(
154                 null,
155                 null));
156
157         assertTrue(UserRestrictionsUtils.areEqual(
158                 null,
159                 Bundle.EMPTY));
160
161         assertTrue(UserRestrictionsUtils.areEqual(
162                 Bundle.EMPTY,
163                 null));
164
165         assertTrue(UserRestrictionsUtils.areEqual(
166                 Bundle.EMPTY,
167                 Bundle.EMPTY));
168
169         assertTrue(UserRestrictionsUtils.areEqual(
170                 new Bundle(),
171                 Bundle.EMPTY));
172
173         assertFalse(UserRestrictionsUtils.areEqual(
174                 null,
175                 DpmTestUtils.newRestrictions("a")));
176
177         assertFalse(UserRestrictionsUtils.areEqual(
178                 DpmTestUtils.newRestrictions("a"),
179                 null));
180
181         assertTrue(UserRestrictionsUtils.areEqual(
182                 DpmTestUtils.newRestrictions("a"),
183                 DpmTestUtils.newRestrictions("a")));
184
185         assertFalse(UserRestrictionsUtils.areEqual(
186                 DpmTestUtils.newRestrictions("a"),
187                 DpmTestUtils.newRestrictions("a", "b")));
188
189         assertFalse(UserRestrictionsUtils.areEqual(
190                 DpmTestUtils.newRestrictions("a", "b"),
191                 DpmTestUtils.newRestrictions("a")));
192
193         assertFalse(UserRestrictionsUtils.areEqual(
194                 DpmTestUtils.newRestrictions("b", "a"),
195                 DpmTestUtils.newRestrictions("a", "a")));
196
197         // Make sure false restrictions are handled correctly.
198         final Bundle a = DpmTestUtils.newRestrictions("a");
199         a.putBoolean("b", true);
200
201         final Bundle b = DpmTestUtils.newRestrictions("a");
202         b.putBoolean("b", false);
203
204         assertFalse(UserRestrictionsUtils.areEqual(a, b));
205         assertFalse(UserRestrictionsUtils.areEqual(b, a));
206     }
207 }