| /* |
| * Copyright (C) 2014 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.android.cts.devicepolicy; |
| |
| import com.android.ddmlib.Log.LogLevel; |
| import com.android.tradefed.device.DeviceNotAvailableException; |
| import com.android.tradefed.log.LogUtil.CLog; |
| |
| /** |
| * Set of tests for Managed Profile use cases. |
| */ |
| public class ManagedProfileTest extends BaseDevicePolicyTest { |
| |
| private static final String MANAGED_PROFILE_PKG = "com.android.cts.managedprofile"; |
| private static final String MANAGED_PROFILE_APK = "CtsManagedProfileApp.apk"; |
| |
| private static final String INTENT_RECEIVER_PKG = "com.android.cts.intent.receiver"; |
| private static final String INTENT_RECEIVER_APK = "CtsIntentReceiverApp.apk"; |
| |
| private static final String ADMIN_RECEIVER_TEST_CLASS = |
| MANAGED_PROFILE_PKG + ".BaseManagedProfileTest$BasicAdminReceiver"; |
| |
| private int mUserId; |
| |
| @Override |
| protected void setUp() throws Exception { |
| super.setUp(); |
| |
| // We need multi user to be supported in order to create a profile of the user owner. |
| mHasFeature = mHasFeature && (getMaxNumberOfUsersSupported() > 1); |
| |
| if (mHasFeature) { |
| mUserId = createManagedProfile(); |
| installApp(MANAGED_PROFILE_APK); |
| setProfileOwner(MANAGED_PROFILE_PKG + "/" + ADMIN_RECEIVER_TEST_CLASS, mUserId); |
| startUser(mUserId); |
| } |
| } |
| |
| @Override |
| protected void tearDown() throws Exception { |
| if (mHasFeature) { |
| removeUser(mUserId); |
| getDevice().uninstallPackage(MANAGED_PROFILE_PKG); |
| } |
| |
| super.tearDown(); |
| } |
| |
| public void testManagedProfileSetup() throws Exception { |
| if (!mHasFeature) { |
| return; |
| } |
| assertTrue(runDeviceTestsAsUser( |
| MANAGED_PROFILE_PKG, MANAGED_PROFILE_PKG + ".ManagedProfileSetupTest", mUserId)); |
| } |
| |
| /** |
| * wipeData() test removes the managed profile, so it needs to separated from other tests. |
| */ |
| public void testWipeData() throws Exception { |
| if (!mHasFeature) { |
| return; |
| } |
| assertTrue(listUsers().contains(mUserId)); |
| assertTrue(runDeviceTestsAsUser( |
| MANAGED_PROFILE_PKG, MANAGED_PROFILE_PKG + ".WipeDataTest", mUserId)); |
| // Note: the managed profile is removed by this test, which will make removeUserCommand in |
| // tearDown() to complain, but that should be OK since its result is not asserted. |
| assertFalse(listUsers().contains(mUserId)); |
| } |
| |
| // TODO: Reinstate once we find another way of disabling an activity for a user (adb pm disable does |
| // not work at the moment on non-eng builds. |
| // public void testCrossProfileIntentFilters() throws Exception { |
| // if (!mHasFeature) { |
| // return; |
| // } |
| // // Set up activities: ManagedProfileActivity will only be enabled in the managed profile and |
| // // PrimaryUserActivity only in the primary one |
| // disableActivityForUser("ManagedProfileActivity", 0); |
| // disableActivityForUser("PrimaryUserActivity", mUserId); |
| // |
| // assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, |
| // MANAGED_PROFILE_PKG + ".ManagedProfileTest", mUserId)); |
| // |
| // // Set up filters from primary to managed profile |
| // String command = "am start -W --user " + mUserId + " " + MANAGED_PROFILE_PKG |
| // + "/.PrimaryUserFilterSetterActivity"; |
| // CLog.logAndDisplay(LogLevel.INFO, "Output for command " + command + ": " |
| // + getDevice().executeShellCommand(command)); |
| // assertTrue(runDeviceTests(MANAGED_PROFILE_PKG, MANAGED_PROFILE_PKG + ".PrimaryUserTest")); |
| // // TODO: Test with startActivity |
| // // TODO: Test with CtsVerifier for disambiguation cases |
| // } |
| |
| public void testCrossProfileContent() throws Exception { |
| if (!mHasFeature) { |
| return; |
| } |
| try { |
| installApp(INTENT_RECEIVER_APK); |
| |
| String command = "pm uninstall --user " + mUserId + " " + INTENT_RECEIVER_PKG; |
| CLog.logAndDisplay(LogLevel.INFO, "Output for command " + command + ": " |
| + getDevice().executeShellCommand(command)); |
| |
| assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, |
| MANAGED_PROFILE_PKG + ".crossprofilecontent.CrossProfileContentTest", mUserId)); |
| } finally { |
| getDevice().uninstallPackage(INTENT_RECEIVER_PKG); |
| } |
| } |
| |
| private void disableActivityForUser(String activityName, int userId) |
| throws DeviceNotAvailableException { |
| String command = "pm disable --user " + userId + " " + MANAGED_PROFILE_PKG + "/." |
| + activityName; |
| CLog.logAndDisplay(LogLevel.INFO, "Output for command " + command + ": " |
| + getDevice().executeShellCommand(command)); |
| } |
| |
| private int createManagedProfile() throws DeviceNotAvailableException { |
| String command = |
| "pm create-user --profileOf 0 --managed TestProfile_" + System.currentTimeMillis(); |
| String commandOutput = getDevice().executeShellCommand(command); |
| CLog.logAndDisplay(LogLevel.INFO, "Output for command " + command + ": " + commandOutput); |
| |
| // Extract the id of the new user. |
| String[] tokens = commandOutput.split("\\s+"); |
| assertTrue(tokens.length > 0); |
| assertEquals("Success:", tokens[0]); |
| return Integer.parseInt(tokens[tokens.length-1]); |
| } |
| |
| private void setProfileOwner(String componentName, int userId) |
| throws DeviceNotAvailableException { |
| String command = "dpm set-profile-owner '" + componentName + "' " + userId; |
| String commandOutput = getDevice().executeShellCommand(command); |
| CLog.logAndDisplay(LogLevel.INFO, "Output for command " + command + ": " + commandOutput); |
| assertTrue(commandOutput.startsWith("Success:")); |
| } |
| } |