• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.managedprovisioning.task;
18 
19 import static android.app.AppOpsManager.MODE_ALLOWED;
20 import static android.app.AppOpsManager.MODE_DEFAULT;
21 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static org.robolectric.Shadows.shadowOf;
26 
27 import android.app.admin.DevicePolicyManager;
28 import android.content.ComponentName;
29 import android.content.ContextWrapper;
30 import android.content.pm.ApplicationInfo;
31 import android.content.pm.CrossProfileApps;
32 import android.content.pm.PackageInfo;
33 import android.content.pm.PackageManager;
34 
35 import androidx.test.core.app.ApplicationProvider;
36 
37 import com.android.managedprovisioning.common.ManagedProvisioningSharedPreferences;
38 import com.android.managedprovisioning.model.ProvisioningParams;
39 
40 import java.util.Arrays;
41 import java.util.HashSet;
42 import java.util.Set;
43 
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.Mock;
48 import org.mockito.MockitoAnnotations;
49 import org.robolectric.RobolectricTestRunner;
50 
51 /** Robolectric unit tests for {@link CreateManagedProfileTask}. */
52 @RunWith(RobolectricTestRunner.class)
53 public class CreateManagedProfileTaskRoboTest {
54     private static final ProvisioningParams TEST_PARAMS =
55             new ProvisioningParams.Builder()
56                     .setDeviceAdminComponentName(
57                             new ComponentName("com.example.testdeviceadmin", "TestDeviceAdmin"))
58                     .setProvisioningAction(ACTION_PROVISION_MANAGED_PROFILE)
59                     .build();
60     private static final int TEST_USER_ID = 0;
61 
62     private final ContextWrapper mContext = ApplicationProvider.getApplicationContext();
63     private final PackageManager mPackageManager = mContext.getPackageManager();
64     private final CrossProfileApps mCrossProfileApps =
65             mContext.getSystemService(CrossProfileApps.class);
66     private final DevicePolicyManager mDevicePolicyManager =
67             mContext.getSystemService(DevicePolicyManager.class);
68 
69     private @Mock AbstractProvisioningTask.Callback mCallback;
70 
71     @Before
initializeMocks()72     public void initializeMocks() {
73         MockitoAnnotations.initMocks(this);
74     }
75 
76     @Test
run_clearsNonDefaultInteractAcrossProfilesAppOps()77     public void run_clearsNonDefaultInteractAcrossProfilesAppOps() {
78         final String testPackage1 = "com.example.testapp1";
79         final String testPackage2 = "com.example.testapp2";
80         shadowOf(mPackageManager).installPackage(buildTestPackageInfo(testPackage1));
81         shadowOf(mPackageManager).installPackage(buildTestPackageInfo(testPackage2));
82         mCrossProfileApps.setInteractAcrossProfilesAppOp(testPackage1, MODE_DEFAULT);
83         mCrossProfileApps.setInteractAcrossProfilesAppOp(testPackage2, MODE_ALLOWED);
84         shadowOf(mDevicePolicyManager).setDefaultCrossProfilePackages(new HashSet<>());
85 
86         new CreateManagedProfileTask(mContext, TEST_PARAMS, mCallback).run(TEST_USER_ID);
87 
88         assertThat(shadowOf(mCrossProfileApps).getInteractAcrossProfilesAppOp(testPackage1))
89                 .isEqualTo(MODE_DEFAULT);
90         assertThat(shadowOf(mCrossProfileApps).getInteractAcrossProfilesAppOp(testPackage2))
91                 .isEqualTo(MODE_DEFAULT);
92     }
93 
94     @Test
run_grantsDefaultConfigurableInteractAcrossProfilesAppOps()95     public void run_grantsDefaultConfigurableInteractAcrossProfilesAppOps() {
96         final String crossProfilePackage = "com.example.testapp1";
97         Set<String> packages = new HashSet<>();
98         packages.add(crossProfilePackage);
99         shadowOf(mPackageManager).installPackage(buildTestPackageInfo(crossProfilePackage));
100         mCrossProfileApps.setInteractAcrossProfilesAppOp(crossProfilePackage, MODE_DEFAULT);
101         shadowOf(mCrossProfileApps).addCrossProfilePackage(crossProfilePackage);
102         shadowOf(mDevicePolicyManager).setDefaultCrossProfilePackages(packages);
103 
104         new CreateManagedProfileTask(mContext, TEST_PARAMS, mCallback).run(TEST_USER_ID);
105 
106         assertThat(shadowOf(mCrossProfileApps).getInteractAcrossProfilesAppOp(crossProfilePackage))
107                 .isEqualTo(MODE_ALLOWED);
108     }
109 
110     @Test
run_clearsDefaultNotConfigurableInteractAcrossProfilesAppOps()111     public void run_clearsDefaultNotConfigurableInteractAcrossProfilesAppOps() {
112         final String nonCrossProfilePackage = "com.example.testapp2";
113         Set<String> packages = new HashSet<>();
114         packages.add(nonCrossProfilePackage);
115         shadowOf(mPackageManager).installPackage(buildTestPackageInfo(nonCrossProfilePackage));
116         mCrossProfileApps.setInteractAcrossProfilesAppOp(nonCrossProfilePackage, MODE_ALLOWED);
117         shadowOf(mDevicePolicyManager).setDefaultCrossProfilePackages(packages);
118 
119         new CreateManagedProfileTask(mContext, TEST_PARAMS, mCallback).run(TEST_USER_ID);
120 
121         assertThat(shadowOf(mCrossProfileApps)
122                 .getInteractAcrossProfilesAppOp(nonCrossProfilePackage))
123                 .isEqualTo(MODE_DEFAULT);
124     }
125 
buildTestPackageInfo(String packageName)126     private PackageInfo buildTestPackageInfo(String packageName) {
127         final PackageInfo packageInfo = new PackageInfo();
128         packageInfo.packageName = packageName;
129         packageInfo.applicationInfo = new ApplicationInfo();
130         packageInfo.applicationInfo.packageName = packageName;
131         packageInfo.applicationInfo.name = "appName";
132         return packageInfo;
133     }
134 }
135