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 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.robolectric.Shadows.shadowOf;
25 
26 import android.Manifest;
27 import android.app.AppOpsManager;
28 import android.app.admin.DevicePolicyManager;
29 import android.content.Context;
30 import android.content.pm.CrossProfileApps;
31 import android.content.pm.PackageManager;
32 import android.os.UserManager;
33 
34 import com.android.managedprovisioning.analytics.MetricsWriterFactory;
35 import com.android.managedprovisioning.analytics.ProvisioningAnalyticsTracker;
36 import com.android.managedprovisioning.common.ManagedProvisioningSharedPreferences;
37 import com.android.managedprovisioning.common.SettingsFacade;
38 
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.robolectric.RobolectricTestRunner;
42 import org.robolectric.RuntimeEnvironment;
43 
44 import org.junit.runner.RunWith;
45 
46 import java.util.HashSet;
47 import java.util.Set;
48 
49 /**
50  * Unit-tests for {@link UpdateInteractAcrossProfilesAppOpTask}.
51  */
52 @RunWith(RobolectricTestRunner.class)
53 public class UpdateInteractAcrossProfilesAppOpTaskTest {
54     private static final String TEST_PACKAGE_NAME_1 = "com.test.packagea";
55     private static final String TEST_PACKAGE_NAME_2 = "com.test.packageb";
56     private static final int TEST_USER_ID = 123;
57 
58     private final Context mContext = RuntimeEnvironment.application;
59     private final UserManager mUserManager = mContext.getSystemService(UserManager.class);
60     private final DevicePolicyManager mDevicePolicyManager =
61             mContext.getSystemService(DevicePolicyManager.class);
62     private final AppOpsManager mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
63     private final PackageManager mPackageManager = mContext.getPackageManager();
64     private final TestTaskCallback mCallback = new TestTaskCallback();
65     private final CrossProfileApps mCrossProfileApps =
66             mContext.getSystemService(CrossProfileApps.class);
67     private final ManagedProvisioningSharedPreferences mManagedProvisioningSharedPreferences
68             = new ManagedProvisioningSharedPreferences(mContext);
69     private final ProvisioningAnalyticsTracker mProvisioningAnalyticsTracker =
70             new ProvisioningAnalyticsTracker(
71                     MetricsWriterFactory.getMetricsWriter(mContext, new SettingsFacade()),
72                     mManagedProvisioningSharedPreferences);
73     private final UpdateInteractAcrossProfilesAppOpTask task =
74             new UpdateInteractAcrossProfilesAppOpTask(
75                     mContext, /* params= */ null, mCallback, mProvisioningAnalyticsTracker);
76 
77     @Before
setUp()78     public void setUp() {
79         shadowOf(mUserManager).addUser(TEST_USER_ID, "Username", /* flags= */ 0);
80     }
81 
82     @Test
run_firstRun_appOpIsNotReset()83     public void run_firstRun_appOpIsNotReset() {
84         setDefaultCrossProfilePackages(TEST_PACKAGE_NAME_1, TEST_PACKAGE_NAME_2);
85         mCrossProfileApps.setInteractAcrossProfilesAppOp(TEST_PACKAGE_NAME_2, MODE_ALLOWED);
86 
87         task.run(TEST_USER_ID);
88 
89         assertThat(shadowOf(mCrossProfileApps).getInteractAcrossProfilesAppOp(TEST_PACKAGE_NAME_2))
90                 .isEqualTo(MODE_ALLOWED);
91     }
92 
93     @Test
run_packageIsRemovedButStillConfigurable_appOpIsNotReset()94     public void run_packageIsRemovedButStillConfigurable_appOpIsNotReset() {
95         setDefaultCrossProfilePackages(TEST_PACKAGE_NAME_1, TEST_PACKAGE_NAME_2);
96         mCrossProfileApps.setInteractAcrossProfilesAppOp(TEST_PACKAGE_NAME_2, MODE_ALLOWED);
97         shadowOf(mCrossProfileApps).addCrossProfilePackage(TEST_PACKAGE_NAME_2);
98         task.run(TEST_USER_ID);
99 
100         setDefaultCrossProfilePackages(TEST_PACKAGE_NAME_1);
101         task.run(TEST_USER_ID);
102 
103         assertThat(shadowOf(mCrossProfileApps).getInteractAcrossProfilesAppOp(TEST_PACKAGE_NAME_2))
104                 .isEqualTo(MODE_ALLOWED);
105     }
106 
107     @Test
run_packageIsNoLongerConfigurable_appOpIsReset()108     public void run_packageIsNoLongerConfigurable_appOpIsReset() {
109         setDefaultCrossProfilePackages(TEST_PACKAGE_NAME_1, TEST_PACKAGE_NAME_2);
110         mCrossProfileApps.setInteractAcrossProfilesAppOp(TEST_PACKAGE_NAME_2, MODE_ALLOWED);
111         // By default the configurable list is empty
112         task.run(TEST_USER_ID);
113 
114         setDefaultCrossProfilePackages(TEST_PACKAGE_NAME_1);
115         task.run(TEST_USER_ID);
116 
117         assertThat(shadowOf(mCrossProfileApps).getInteractAcrossProfilesAppOp(TEST_PACKAGE_NAME_2))
118                 .isEqualTo(MODE_DEFAULT);
119     }
120 
121     @Test
run_packageIsNotRemoved_appOpIsNotReset()122     public void run_packageIsNotRemoved_appOpIsNotReset() {
123         setDefaultCrossProfilePackages(TEST_PACKAGE_NAME_1, TEST_PACKAGE_NAME_2);
124         mCrossProfileApps.setInteractAcrossProfilesAppOp(TEST_PACKAGE_NAME_2, MODE_ALLOWED);
125         task.run(TEST_USER_ID);
126 
127         setDefaultCrossProfilePackages(TEST_PACKAGE_NAME_2);
128         task.run(TEST_USER_ID);
129 
130         assertThat(shadowOf(mCrossProfileApps).getInteractAcrossProfilesAppOp(TEST_PACKAGE_NAME_2))
131                 .isEqualTo(MODE_ALLOWED);
132     }
133 
setDefaultCrossProfilePackages(String... defaultCrossProfilePackages)134     private void setDefaultCrossProfilePackages(String... defaultCrossProfilePackages) {
135         Set<String> packages = new HashSet<>();
136         for (String packageName : defaultCrossProfilePackages) {
137             packages.add(packageName);
138         }
139 
140         shadowOf(mDevicePolicyManager).setDefaultCrossProfilePackages(packages);
141     }
142 }
143