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 */
16package android.content;
17
18import android.os.Bundle;
19import android.os.Parcelable;
20import android.test.AndroidTestCase;
21
22import java.util.Arrays;
23import java.util.HashSet;
24import java.util.List;
25import java.util.Set;
26
27public class RestrictionsManagerTest extends AndroidTestCase {
28    private RestrictionsManager mRm;
29
30    @Override
31    protected void setUp() throws Exception {
32        super.setUp();
33        mRm = (RestrictionsManager) mContext.getSystemService(Context.RESTRICTIONS_SERVICE);
34    }
35
36    public void testGetManifestRestrictions() {
37        String packageName = getContext().getPackageName();
38        List<RestrictionEntry> manifestRestrictions = mRm.getManifestRestrictions(packageName);
39        assertEquals(6, manifestRestrictions.size());
40        Set<String> verifiedKeys = new HashSet<>(Arrays.asList("bundle_key", "bundle_array_key",
41                "bundle_array_bundle_key"));
42        for (RestrictionEntry entry : manifestRestrictions) {
43            if ("bundle_key".equals(entry.getKey())) {
44                assertEquals("bundle_key entry should have 2 children entries",
45                        2, entry.getRestrictions().length);
46                verifiedKeys.remove(entry.getKey());
47            } else if ("bundle_array_key".equals(entry.getKey())) {
48                assertEquals("bundle_array_key should have 2 children entries",
49                        2, entry.getRestrictions().length);
50                assertNotNull(entry.getRestrictions());
51                for (RestrictionEntry childEntry : entry.getRestrictions()) {
52                    if ("bundle_array_bundle_key".equals(childEntry.getKey())) {
53                        assertNotNull(childEntry.getRestrictions());
54                        assertEquals("bundle_array_bundle_key should have 1 child entry",
55                                1, childEntry.getRestrictions().length);
56                        verifiedKeys.remove(childEntry.getKey());
57                    }
58                }
59                verifiedKeys.remove(entry.getKey());
60            }
61        }
62        assertTrue("Entries" + verifiedKeys + " were not found", verifiedKeys.isEmpty());
63    }
64
65    public void testConvertRestrictionsToBundle() {
66        String packageName = getContext().getPackageName();
67        List<RestrictionEntry> manifestRestrictions = mRm.getManifestRestrictions(packageName);
68        Bundle bundle = RestrictionsManager.convertRestrictionsToBundle(manifestRestrictions);
69        assertEquals(6, bundle.size());
70        Bundle childBundle = bundle.getBundle("bundle_key");
71        assertNotNull(childBundle);
72        assertEquals(2, childBundle.size());
73        Parcelable[] childBundleArray = bundle.getParcelableArray("bundle_array_key");
74        assertEquals(2, childBundleArray.length);
75    }
76
77}
78