1 /*
2  * Copyright (C) 2019 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 com.android.cts.devicepolicy.DeviceAdminFeaturesCheckerRule.RequiresAdditionalFeatures;
22 import com.android.tradefed.device.DeviceNotAvailableException;
23 import com.android.tradefed.log.LogUtil;
24 
25 // We need multi user to be supported in order to create a profile of the user owner.
26 @RequiresAdditionalFeatures({FEATURE_MANAGED_USERS})
27 public abstract class BaseManagedProfileTest extends BaseDevicePolicyTest {
28     protected static final String MANAGED_PROFILE_PKG = "com.android.cts.managedprofile";
29     protected static final String INTENT_SENDER_PKG = "com.android.cts.intent.sender";
30     protected static final String INTENT_RECEIVER_PKG = "com.android.cts.intent.receiver";
31     protected static final String TEST_APP_1_PKG = "com.android.cts.testapps.testapp1";
32     protected static final String TEST_APP_2_PKG = "com.android.cts.testapps.testapp2";
33     protected static final String TEST_APP_3_PKG = "com.android.cts.testapps.testapp3";
34     protected static final String TEST_APP_4_PKG = "com.android.cts.testapps.testapp4";
35     protected static final String ADMIN_RECEIVER_TEST_CLASS =
36             MANAGED_PROFILE_PKG + ".BaseManagedProfileTest$BasicAdminReceiver";
37     protected static final String INTENT_SENDER_APK = "CtsIntentSenderApp.apk";
38     protected static final String INTENT_RECEIVER_APK = "CtsIntentReceiverApp.apk";
39     protected static final String SIMPLE_APP_APK = "CtsSimpleApp.apk";
40     protected static final String TEST_APP_1_APK = "TestApp1.apk";
41     protected static final String TEST_APP_2_APK = "TestApp2.apk";
42     protected static final String TEST_APP_3_APK = "TestApp3.apk";
43     protected static final String TEST_APP_4_APK = "TestApp4.apk";
44     protected static final String SHARING_APP_1_APK = "SharingApp1.apk";
45     protected static final String SHARING_APP_2_APK = "SharingApp2.apk";
46     private static final String MANAGED_PROFILE_APK = "CtsManagedProfileApp.apk";
47     private static final String NOTIFICATION_PKG =
48             "com.android.cts.managedprofiletests.notificationsender";
49     protected int mParentUserId;
50     // ID of the profile we'll create. This will always be a profile of the parent.
51     protected int mProfileUserId;
52 
53     @Override
setUp()54     public void setUp() throws Exception {
55         super.setUp();
56 
57         if (mFeaturesCheckerRule.hasRequiredFeatures()) {
58 
59             removeTestUsers();
60             mParentUserId = mPrimaryUserId;
61             mProfileUserId = createManagedProfile(mParentUserId);
62             startUserAndWait(mProfileUserId);
63 
64             // Install the APK on both primary and profile user in one single transaction.
65             // If they were installed separately, the second installation would become an app
66             // update and result in the current running test process being killed.
67             installAppAsUser(MANAGED_PROFILE_APK, USER_ALL);
68             setProfileOwnerOrFail(MANAGED_PROFILE_PKG + "/" + ADMIN_RECEIVER_TEST_CLASS,
69                     mProfileUserId);
70             waitForUserUnlock(mProfileUserId);
71         }
72     }
73 
74     @Override
tearDown()75     public void tearDown() throws Exception {
76         removeUser(mProfileUserId);
77         getDevice().uninstallPackage(MANAGED_PROFILE_PKG);
78         getDevice().uninstallPackage(INTENT_SENDER_PKG);
79         getDevice().uninstallPackage(INTENT_RECEIVER_PKG);
80         getDevice().uninstallPackage(NOTIFICATION_PKG);
81         getDevice().uninstallPackage(TEST_APP_1_APK);
82         getDevice().uninstallPackage(TEST_APP_2_APK);
83         getDevice().uninstallPackage(TEST_APP_3_APK);
84         getDevice().uninstallPackage(TEST_APP_4_APK);
85         getDevice().uninstallPackage(SHARING_APP_1_APK);
86         getDevice().uninstallPackage(SHARING_APP_2_APK);
87 
88         super.tearDown();
89     }
90 
disableActivityForUser(String activityName, int userId)91     protected void disableActivityForUser(String activityName, int userId)
92             throws DeviceNotAvailableException {
93         String command = "am start -W --user " + userId
94                 + " --es extra-package " + MANAGED_PROFILE_PKG
95                 + " --es extra-class-name " + MANAGED_PROFILE_PKG + "." + activityName
96                 + " " + MANAGED_PROFILE_PKG + "/.ComponentDisablingActivity ";
97         LogUtil.CLog.d("Output for command " + command + ": "
98                 + getDevice().executeShellCommand(command));
99     }
100 }
101