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 com.android.ddmlib.Log.LogLevel;
20 import com.android.tradefed.device.DeviceNotAvailableException;
21 import com.android.tradefed.log.LogUtil.CLog;
22 
23 /**
24  * Set of tests for LauncherApps with managed profiles.
25  */
26 public class LauncherAppsProfileTest extends BaseLauncherAppsTest {
27 
28     private int mProfileUserId;
29     private int mProfileSerialNumber;
30     private int mMainUserSerialNumber;
31 
32     @Override
setUp()33     protected void setUp() throws Exception {
34         super.setUp();
35 
36         // We need multi user to be supported in order to create a profile of the user owner.
37         mHasFeature = mHasFeature && (getMaxNumberOfUsersSupported() > 1);
38 
39         if (mHasFeature) {
40             removeTestUsers();
41             installTestApps();
42             // Create a managed profile
43             mProfileUserId = createManagedProfile();
44             installApp(MANAGED_PROFILE_APK);
45             setProfileOwner(MANAGED_PROFILE_PKG + "/" + ADMIN_RECEIVER_TEST_CLASS, mProfileUserId);
46             mProfileSerialNumber = getUserSerialNumber(mProfileUserId);
47             mMainUserSerialNumber = getUserSerialNumber(0);
48             startUser(mProfileUserId);
49         }
50     }
51 
52     @Override
tearDown()53     protected void tearDown() throws Exception {
54         if (mHasFeature) {
55             removeUser(mProfileUserId);
56             uninstallTestApps();
57             getDevice().uninstallPackage(MANAGED_PROFILE_PKG);
58         }
59         super.tearDown();
60     }
61 
testGetActivitiesWithProfile()62     public void testGetActivitiesWithProfile() throws Exception {
63         if (!mHasFeature) {
64             return;
65         }
66         // Install app for all users.
67         installApp(SIMPLE_APP_APK);
68         try {
69             // Run tests to check SimpleApp exists in both profile and main user.
70             assertTrue(runDeviceTests(LAUNCHER_TESTS_PKG,
71                     LAUNCHER_TESTS_CLASS,
72                     "testSimpleAppInstalledForUser",
73                             0, "-e testUser " + mProfileSerialNumber));
74             assertTrue(runDeviceTests(LAUNCHER_TESTS_PKG,
75                     LAUNCHER_TESTS_PKG + ".LauncherAppsTests", "testSimpleAppInstalledForUser",
76                             0, "-e testUser " + mMainUserSerialNumber));
77         } finally {
78             getDevice().uninstallPackage(SIMPLE_APP_PKG);
79         }
80     }
81 
testLauncherCallbackPackageAddedProfile()82     public void testLauncherCallbackPackageAddedProfile() throws Exception {
83         if (!mHasFeature) {
84             return;
85         }
86         startCallbackService();
87         installApp(SIMPLE_APP_APK);
88         try {
89             assertTrue(runDeviceTests(LAUNCHER_TESTS_PKG,
90                     LAUNCHER_TESTS_CLASS,
91                             "testPackageAddedCallbackForUser",
92                             0, "-e testUser " + mProfileSerialNumber));
93         } finally {
94             getDevice().uninstallPackage(SIMPLE_APP_PKG);
95         }
96     }
97 
testLauncherCallbackPackageRemovedProfile()98     public void testLauncherCallbackPackageRemovedProfile() throws Exception {
99         if (!mHasFeature) {
100             return;
101         }
102         installApp(SIMPLE_APP_APK);
103         try {
104             startCallbackService();
105             getDevice().uninstallPackage(SIMPLE_APP_PKG);
106             assertTrue(runDeviceTests(LAUNCHER_TESTS_PKG,
107                     LAUNCHER_TESTS_CLASS,
108                             "testPackageRemovedCallbackForUser",
109                             0, "-e testUser " + mProfileSerialNumber));
110         } finally {
111             getDevice().uninstallPackage(SIMPLE_APP_PKG);
112         }
113     }
114 
testLauncherCallbackPackageChangedProfile()115     public void testLauncherCallbackPackageChangedProfile() throws Exception {
116         if (!mHasFeature) {
117             return;
118         }
119         installApp(SIMPLE_APP_APK);
120         try {
121             startCallbackService();
122             installApp(SIMPLE_APP_APK);
123             assertTrue(runDeviceTests(LAUNCHER_TESTS_PKG,
124                     LAUNCHER_TESTS_CLASS,
125                             "testPackageChangedCallbackForUser",
126                             0, "-e testUser " + mProfileSerialNumber));
127         } finally {
128             getDevice().uninstallPackage(SIMPLE_APP_PKG);
129         }
130     }
131 }
132