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 
17 package com.android.cts.devicepolicy;
18 
19 import static com.android.cts.devicepolicy.DeviceAdminFeaturesCheckerRule.FEATURE_MANAGED_USERS;
20 
21 import android.platform.test.annotations.FlakyTest;
22 
23 import com.android.cts.devicepolicy.DeviceAdminFeaturesCheckerRule.RequiresAdditionalFeatures;
24 import com.android.tradefed.log.LogUtil.CLog;
25 
26 import org.junit.Test;
27 
28 import java.util.Collections;
29 
30 /**
31  * Set of tests for LauncherApps with managed profiles.
32  */
33 @RequiresAdditionalFeatures({FEATURE_MANAGED_USERS})
34 public class LauncherAppsProfileTest extends BaseLauncherAppsTest {
35 
36     private static final String MANAGED_PROFILE_PKG = "com.android.cts.managedprofile";
37     private static final String MANAGED_PROFILE_APK = "CtsManagedProfileApp.apk";
38     private static final String ADMIN_RECEIVER_TEST_CLASS =
39             MANAGED_PROFILE_PKG + ".BaseManagedProfileTest$BasicAdminReceiver";
40     private static final String LAUNCHER_TESTS_HAS_LAUNCHER_ACTIVITY_APK =
41             "CtsHasLauncherActivityApp.apk";
42 
43     private int mProfileUserId;
44     private int mParentUserId;
45     private String mProfileSerialNumber;
46     private String mMainUserSerialNumber;
47 
48     @Override
setUp()49     public void setUp() throws Exception {
50         super.setUp();
51 
52         removeTestUsers();
53         // Create a managed profile
54         mParentUserId = mPrimaryUserId;
55         mProfileUserId = createManagedProfile(mParentUserId);
56         installAppAsUser(MANAGED_PROFILE_APK, mProfileUserId);
57         setProfileOwnerOrFail(MANAGED_PROFILE_PKG + "/" + ADMIN_RECEIVER_TEST_CLASS,
58                 mProfileUserId);
59         mProfileSerialNumber = Integer.toString(getUserSerialNumber(mProfileUserId));
60         mMainUserSerialNumber = Integer.toString(getUserSerialNumber(mParentUserId));
61         startUserAndWait(mProfileUserId);
62 
63         // Install test APK on primary user and the managed profile.
64         installTestApps(USER_ALL);
65     }
66 
67     @Override
tearDown()68     public void tearDown() throws Exception {
69         removeUser(mProfileUserId);
70         uninstallTestApps();
71         getDevice().uninstallPackage(LAUNCHER_TESTS_HAS_LAUNCHER_ACTIVITY_APK);
72 
73         super.tearDown();
74     }
75 
76     @Test
testGetActivitiesWithProfile()77     public void testGetActivitiesWithProfile() throws Exception {
78         // Install app for all users.
79         installAppAsUser(SIMPLE_APP_APK, mParentUserId);
80         installAppAsUser(SIMPLE_APP_APK, mProfileUserId);
81 
82         // Run tests to check SimpleApp exists in both profile and main user.
83         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
84                 LAUNCHER_TESTS_CLASS, "testSimpleAppInstalledForUser",
85                 mParentUserId, Collections.singletonMap(PARAM_TEST_USER, mProfileSerialNumber));
86         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
87                 LAUNCHER_TESTS_CLASS, "testSimpleAppInstalledForUser",
88                 mParentUserId, Collections.singletonMap(PARAM_TEST_USER, mMainUserSerialNumber));
89 
90         // Run the same test on the managed profile.  This still works.
91         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
92                 LAUNCHER_TESTS_CLASS, "testSimpleAppInstalledForUser",
93                 mProfileUserId, Collections.singletonMap(PARAM_TEST_USER, mProfileSerialNumber));
94 
95         // This tries to access main prorfile from work profiel, which is not allowed.
96         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
97                 LAUNCHER_TESTS_CLASS, "testAccessPrimaryProfileFromManagedProfile",
98                 mProfileUserId, Collections.singletonMap(PARAM_TEST_USER, mMainUserSerialNumber));
99 
100         // Test for getProfiles.
101         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
102                 LAUNCHER_TESTS_CLASS, "testGetProfiles_fromMainProfile",
103                 mParentUserId);
104         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
105                 LAUNCHER_TESTS_CLASS, "testGetProfiles_fromManagedProfile",
106                 mProfileUserId);
107     }
108 
109     @Test
testProfileOwnerAppHiddenInPrimaryProfile()110     public void testProfileOwnerAppHiddenInPrimaryProfile() throws Exception {
111         String command = "pm disable --user " + mParentUserId + " " + MANAGED_PROFILE_PKG
112                 + "/.PrimaryUserFilterSetterActivity";
113         CLog.d("Output for command " + command + ": " + getDevice().executeShellCommand(command));
114         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
115                 LAUNCHER_TESTS_CLASS, "testProfileOwnerInjectedActivityNotFound",
116                 mParentUserId, Collections.singletonMap(PARAM_TEST_USER, mMainUserSerialNumber));
117     }
118 
119     @Test
testNoHiddenActivityInProfile()120     public void testNoHiddenActivityInProfile() throws Exception {
121         // Install app for all users.
122         installAppAsUser(LAUNCHER_TESTS_HAS_LAUNCHER_ACTIVITY_APK, mParentUserId);
123         installAppAsUser(LAUNCHER_TESTS_HAS_LAUNCHER_ACTIVITY_APK, mProfileUserId);
124 
125         // Run tests to check SimpleApp exists in both profile and main user.
126         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
127                 LAUNCHER_TESTS_CLASS, "testDoPoNoTestAppInjectedActivityFound",
128                 mParentUserId, Collections.singletonMap(PARAM_TEST_USER, mProfileSerialNumber));
129         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
130                 LAUNCHER_TESTS_CLASS, "testHasLauncherActivityAppHasAppDetailsActivityInjected",
131                 mParentUserId, Collections.singletonMap(PARAM_TEST_USER, mMainUserSerialNumber));
132     }
133 
134     @FlakyTest
135     @Test
testLauncherCallbackPackageAddedProfile()136     public void testLauncherCallbackPackageAddedProfile() throws Exception {
137         startCallbackService(mPrimaryUserId);
138         installAppAsUser(SIMPLE_APP_APK, mProfileUserId);
139         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
140                 LAUNCHER_TESTS_CLASS,
141                 "testPackageAddedCallbackForUser",
142                 mParentUserId, Collections.singletonMap(PARAM_TEST_USER, mProfileSerialNumber));
143     }
144 
145     @FlakyTest
146     @Test
testLauncherCallbackPackageRemovedProfile()147     public void testLauncherCallbackPackageRemovedProfile() throws Exception {
148         installAppAsUser(SIMPLE_APP_APK, mProfileUserId);
149         startCallbackService(mPrimaryUserId);
150         getDevice().uninstallPackage(SIMPLE_APP_PKG);
151         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
152                 LAUNCHER_TESTS_CLASS,
153                 "testPackageRemovedCallbackForUser",
154                 mParentUserId, Collections.singletonMap(PARAM_TEST_USER, mProfileSerialNumber));
155     }
156 
157     @FlakyTest
158     @Test
testLauncherCallbackPackageChangedProfile()159     public void testLauncherCallbackPackageChangedProfile() throws Exception {
160         installAppAsUser(SIMPLE_APP_APK, mProfileUserId);
161         startCallbackService(mPrimaryUserId);
162         installAppAsUser(SIMPLE_APP_APK, /* grantPermissions */ true, /* dontKillApp */ true,
163                 mProfileUserId);
164         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
165                 LAUNCHER_TESTS_CLASS,
166                 "testPackageChangedCallbackForUser",
167                 mParentUserId, Collections.singletonMap(PARAM_TEST_USER, mProfileSerialNumber));
168     }
169 
170     @Test
testReverseAccessNoThrow()171     public void testReverseAccessNoThrow() throws Exception {
172         installAppAsUser(SIMPLE_APP_APK, mProfileUserId);
173 
174         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
175                 LAUNCHER_TESTS_CLASS, "testReverseAccessNoThrow",
176                 mProfileUserId, Collections.singletonMap(PARAM_TEST_USER, mMainUserSerialNumber));
177     }
178 }
179