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.tests.packagemanager.multiuser.host; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertTrue; 21 import static org.junit.Assume.assumeTrue; 22 23 import com.android.tradefed.device.DeviceNotAvailableException; 24 import com.android.tradefed.log.LogUtil; 25 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 26 27 import org.junit.After; 28 import org.junit.Before; 29 30 import java.util.ArrayList; 31 import java.util.Collections; 32 import java.util.List; 33 import java.util.Map; 34 35 import javax.annotation.Nullable; 36 37 public class PackageManagerMultiUserTestBase extends BaseHostJUnit4Test { 38 private static final String RUNNER = "androidx.test.runner.AndroidJUnitRunner"; 39 private static final String TEST_PACKAGE = "com.android.tests.packagemanager.multiuser.app"; 40 private static final String TEST_CLASS = ".PackageManagerMultiUserTest"; 41 private static final long DEFAULT_TIMEOUT = 10 * 60 * 1000L; 42 43 private List<Integer> mCreatedUsers; 44 protected int mUserId; 45 46 @Before setUp()47 public void setUp() throws Exception { 48 assumeTrue("Device does not support multiple users", 49 getDevice().getMaxNumberOfUsersSupported() > 1); 50 mUserId = getDevice().getCurrentUser(); 51 mCreatedUsers = new ArrayList<>(); 52 } 53 54 /** Remove created users after tests. */ 55 @After tearDown()56 public void tearDown() throws Exception { 57 if (mCreatedUsers == null) { 58 return; 59 } 60 for (int userId : mCreatedUsers) { 61 removeUser(userId); 62 } 63 } 64 runDeviceTestAsUser( String pkgName, @Nullable String testClassName, @Nullable String testMethodName, int userId, Map<String, String> params)65 protected void runDeviceTestAsUser( 66 String pkgName, @Nullable String testClassName, 67 @Nullable String testMethodName, int userId, 68 Map<String, String> params) throws DeviceNotAvailableException { 69 if (testClassName != null && testClassName.startsWith(".")) { 70 testClassName = pkgName + testClassName; 71 } 72 73 runDeviceTests( 74 getDevice(), 75 RUNNER, 76 pkgName, 77 testClassName, 78 testMethodName, 79 userId, 80 DEFAULT_TIMEOUT, 81 DEFAULT_TIMEOUT, 82 0L /* maxInstrumentationTimeoutMs */, 83 true /* checkResults */, 84 false /* isHiddenApiCheckDisabled */, 85 params == null ? Collections.emptyMap() : params); 86 } 87 runDeviceTestAsUser(String testMethodName, int userId, Map<String, String> params)88 protected void runDeviceTestAsUser(String testMethodName, int userId, 89 Map<String, String> params) 90 throws DeviceNotAvailableException { 91 runDeviceTestAsUser(TEST_PACKAGE, TEST_CLASS, testMethodName, userId, params); 92 } 93 createUser()94 protected int createUser() throws Exception { 95 String command = "pm create-user TestUser_" + System.currentTimeMillis(); 96 LogUtil.CLog.d("Starting command " + command); 97 String commandOutput = getDevice().executeShellCommand(command); 98 LogUtil.CLog.d("Output for command " + command + ": " + commandOutput); 99 100 // Extract the id of the new user. 101 String[] tokens = commandOutput.split("\\s+"); 102 assertTrue(tokens.length > 0); 103 assertEquals("Success:", tokens[0]); 104 int userId = Integer.parseInt(tokens[tokens.length - 1]); 105 mCreatedUsers.add(userId); 106 107 setupUser(userId); 108 return userId; 109 } 110 setupUser(int userId)111 protected void setupUser(int userId) throws Exception { 112 installExistingPackageForUser(TEST_PACKAGE, userId); 113 } 114 removeUser(int userId)115 protected void removeUser(int userId) throws Exception { 116 String command = "pm remove-user " + userId; 117 LogUtil.CLog.d("Starting command " + command); 118 String commandOutput = getDevice().executeShellCommand(command); 119 LogUtil.CLog.d("Output for command " + command + ": " + commandOutput); 120 } 121 installExistingPackageForUser(String pkgName, int userId)122 protected void installExistingPackageForUser(String pkgName, int userId) throws Exception { 123 String command = "pm install-existing --user " + userId + " " + pkgName; 124 LogUtil.CLog.d("Starting command " + command); 125 String commandOutput = getDevice().executeShellCommand(command); 126 LogUtil.CLog.d("Output for command " + command + ": " + commandOutput); 127 } 128 isPackageInstalledForUser(String pkgName, int userId)129 protected boolean isPackageInstalledForUser(String pkgName, int userId) throws Exception { 130 return getDevice().isPackageInstalled(pkgName, String.valueOf(userId)); 131 } 132 } 133