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