1 /*
2  * Copyright (C) 2014 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 package com.android.cts.managedprofile;
17 
18 import android.app.admin.DevicePolicyManager;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.IntentFilter;
22 import android.content.pm.PackageManager;
23 import android.support.test.InstrumentationRegistry;
24 import android.test.ActivityInstrumentationTestCase2;
25 
26 import static com.android.cts.managedprofile.BaseManagedProfileTest.ADMIN_RECEIVER_COMPONENT;
27 
28 /**
29  * Test for {@link DevicePolicyManager#addCrossProfileIntentFilter} API.
30  *
31  * Note that it expects that there is an activity responding to {@code PrimaryUserActivity.ACTION}
32  * in the primary profile, one to {@code ManagedProfileActivity.ACTION} in the secondary profile,
33  * and one to {@code AllUsersActivity.ACTION} in both profiles.
34  */
35 public class ManagedProfileTest extends ActivityInstrumentationTestCase2<TestActivity> {
36 
37     private PackageManager mPackageManager;
38     private DevicePolicyManager mDevicePolicyManager;
39 
ManagedProfileTest()40     public ManagedProfileTest() {
41         super(TestActivity.class);
42     }
43 
44     @Override
setUp()45     protected void setUp() throws Exception {
46         super.setUp();
47         // As the way to access Instrumentation is changed in the new runner, we need to inject it
48         // manually into ActivityInstrumentationTestCase2. ActivityInstrumentationTestCase2 will
49         // be marked as deprecated and replaced with ActivityTestRule.
50         injectInstrumentation(InstrumentationRegistry.getInstrumentation());
51         mPackageManager = getActivity().getPackageManager();
52         mDevicePolicyManager = (DevicePolicyManager)
53                 getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);
54     }
55 
56     @Override
tearDown()57     protected void tearDown() throws Exception {
58         mDevicePolicyManager.clearCrossProfileIntentFilters(ADMIN_RECEIVER_COMPONENT);
59         super.tearDown();
60     }
61 
testClearCrossProfileIntentFilters()62     public void testClearCrossProfileIntentFilters() {
63         IntentFilter testIntentFilter = new IntentFilter();
64         testIntentFilter.addAction(PrimaryUserActivity.ACTION);
65         mDevicePolicyManager.addCrossProfileIntentFilter(ADMIN_RECEIVER_COMPONENT,
66                 testIntentFilter, DevicePolicyManager.FLAG_PARENT_CAN_ACCESS_MANAGED);
67         assertEquals(1, mPackageManager.queryIntentActivities(
68                 new Intent(PrimaryUserActivity.ACTION), /* flags = */ 0).size());
69 
70         mDevicePolicyManager.clearCrossProfileIntentFilters(ADMIN_RECEIVER_COMPONENT);
71 
72         assertTrue(mPackageManager.queryIntentActivities(
73                 new Intent(PrimaryUserActivity.ACTION), /* flags = */ 0).isEmpty());
74         getActivity().startActivity(ManagedProfileActivity.ACTION);
75         assertTrue(getActivity().checkActivityStarted());
76     }
77 
testAddCrossProfileIntentFilter_primary()78     public void testAddCrossProfileIntentFilter_primary() {
79         assertEquals(0, mPackageManager.queryIntentActivities(
80                 new Intent(PrimaryUserActivity.ACTION), /* flags = */ 0).size());
81 
82         IntentFilter testIntentFilter = new IntentFilter();
83         testIntentFilter.addAction(PrimaryUserActivity.ACTION);
84         mDevicePolicyManager.addCrossProfileIntentFilter(ADMIN_RECEIVER_COMPONENT,
85                 testIntentFilter, DevicePolicyManager.FLAG_PARENT_CAN_ACCESS_MANAGED);
86 
87         assertEquals(1, mPackageManager.queryIntentActivities(
88                 new Intent(PrimaryUserActivity.ACTION), /* flags = */ 0).size());
89         getActivity().startActivity(PrimaryUserActivity.ACTION);
90         assertTrue(getActivity().checkActivityStarted());
91     }
92 
testAddCrossProfileIntentFilter_all()93     public void testAddCrossProfileIntentFilter_all() {
94         assertEquals(1, mPackageManager.queryIntentActivities(
95                 new Intent(AllUsersActivity.ACTION), /* flags = */ 0).size());
96 
97         IntentFilter testIntentFilter = new IntentFilter();
98         testIntentFilter.addAction(AllUsersActivity.ACTION);
99         mDevicePolicyManager.addCrossProfileIntentFilter(ADMIN_RECEIVER_COMPONENT,
100                 testIntentFilter, DevicePolicyManager.FLAG_PARENT_CAN_ACCESS_MANAGED);
101 
102         assertEquals(2, mPackageManager.queryIntentActivities(
103                 new Intent(AllUsersActivity.ACTION), /* flags = */ 0).size());
104         // If we used startActivity(), the user would have a disambiguation dialog presented which
105         // requires human intervention, so we won't be testing like that
106     }
107 
testAddCrossProfileIntentFilter_managed()108     public void testAddCrossProfileIntentFilter_managed() {
109         assertEquals(1, mPackageManager.queryIntentActivities(
110                 new Intent(ManagedProfileActivity.ACTION), /* flags = */ 0).size());
111 
112         IntentFilter testIntentFilter = new IntentFilter();
113         testIntentFilter.addAction(ManagedProfileActivity.ACTION);
114         mDevicePolicyManager.addCrossProfileIntentFilter(ADMIN_RECEIVER_COMPONENT,
115                 testIntentFilter, DevicePolicyManager.FLAG_PARENT_CAN_ACCESS_MANAGED);
116 
117         // We should still be resolving in the profile
118         assertEquals(1, mPackageManager.queryIntentActivities(
119                 new Intent(ManagedProfileActivity.ACTION), /* flags = */ 0).size());
120         getActivity().startActivity(ManagedProfileActivity.ACTION);
121         assertTrue(getActivity().checkActivityStarted());
122     }
123 }
124