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