Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | package android.multiuser; |
| 17 | |
jovanak | 5982cd9 | 2019-08-08 11:27:56 -0700 | [diff] [blame] | 18 | import static org.junit.Assume.assumeTrue; |
| 19 | |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 20 | import android.app.ActivityManager; |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 21 | import android.app.ActivityTaskManager; |
| 22 | import android.app.AppGlobals; |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 23 | import android.app.IActivityManager; |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 24 | import android.app.IStopUserCallback; |
| 25 | import android.app.UserSwitchObserver; |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 26 | import android.app.WaitResult; |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 27 | import android.content.BroadcastReceiver; |
| 28 | import android.content.Context; |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 29 | import android.content.IIntentReceiver; |
| 30 | import android.content.IIntentSender; |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 31 | import android.content.Intent; |
| 32 | import android.content.IntentFilter; |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 33 | import android.content.IntentSender; |
| 34 | import android.content.pm.IPackageInstaller; |
| 35 | import android.content.pm.PackageManager; |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 36 | import android.content.pm.UserInfo; |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 37 | import android.os.Bundle; |
| 38 | import android.os.IBinder; |
| 39 | import android.os.IProgressListener; |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 40 | import android.os.RemoteException; |
Bookatz | 04d7ae5 | 2019-08-05 14:07:12 -0700 | [diff] [blame] | 41 | import android.os.SystemProperties; |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 42 | import android.os.UserHandle; |
| 43 | import android.os.UserManager; |
Bookatz | 04d7ae5 | 2019-08-05 14:07:12 -0700 | [diff] [blame] | 44 | import android.perftests.utils.ShellHelper; |
Bookatz | 52b12ac | 2019-02-28 17:29:26 -0800 | [diff] [blame] | 45 | import android.util.Log; |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 46 | import android.view.WindowManagerGlobal; |
KOUSHIK PANUGANTI | 130f0a5 | 2018-12-17 14:41:06 -0800 | [diff] [blame] | 47 | |
| 48 | import androidx.test.InstrumentationRegistry; |
| 49 | import androidx.test.filters.LargeTest; |
| 50 | import androidx.test.runner.AndroidJUnit4; |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 51 | |
| 52 | import org.junit.After; |
| 53 | import org.junit.Before; |
| 54 | import org.junit.Rule; |
| 55 | import org.junit.Test; |
| 56 | import org.junit.runner.RunWith; |
| 57 | |
| 58 | import java.util.ArrayList; |
| 59 | import java.util.concurrent.CountDownLatch; |
| 60 | import java.util.concurrent.TimeUnit; |
| 61 | |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Perf tests for user life cycle events. |
| 64 | * |
| 65 | * Running the tests: |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 66 | * |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 67 | * make MultiUserPerfTests && |
| 68 | * adb install -r \ |
| 69 | * ${ANDROID_PRODUCT_OUT}/data/app/MultiUserPerfTests/MultiUserPerfTests.apk && |
Sudheer Shanka | 2d0278b | 2017-07-12 18:12:31 -0700 | [diff] [blame] | 70 | * adb shell am instrument -e class android.multiuser.UserLifecycleTests \ |
KOUSHIK PANUGANTI | 130f0a5 | 2018-12-17 14:41:06 -0800 | [diff] [blame] | 71 | * -w com.android.perftests.multiuser/androidx.test.runner.AndroidJUnitRunner |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 72 | * |
| 73 | * or |
| 74 | * |
Sudheer Shanka | 2d0278b | 2017-07-12 18:12:31 -0700 | [diff] [blame] | 75 | * bit MultiUserPerfTests:android.multiuser.UserLifecycleTests |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 76 | * |
| 77 | * Note: If you use bit for running the tests, benchmark results won't be printed on the host side. |
Sudheer Shanka | 2d0278b | 2017-07-12 18:12:31 -0700 | [diff] [blame] | 78 | * But in either case, results can be checked on the device side 'adb logcat -s UserLifecycleTests' |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 79 | */ |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 80 | @LargeTest |
| 81 | @RunWith(AndroidJUnit4.class) |
Sudheer Shanka | 2d0278b | 2017-07-12 18:12:31 -0700 | [diff] [blame] | 82 | public class UserLifecycleTests { |
| 83 | private static final String TAG = UserLifecycleTests.class.getSimpleName(); |
| 84 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 85 | private static final int TIMEOUT_IN_SECOND = 30; |
| 86 | private static final int CHECK_USER_REMOVED_INTERVAL_MS = 200; |
| 87 | |
| 88 | private static final String DUMMY_PACKAGE_NAME = "perftests.multiuser.apps.dummyapp"; |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 89 | |
Bookatz | 04d7ae5 | 2019-08-05 14:07:12 -0700 | [diff] [blame] | 90 | // Copy of UserSystemPackageInstaller whitelist mode constants. |
| 91 | private static final String PACKAGE_WHITELIST_MODE_PROP = |
| 92 | "persist.debug.user.package_whitelist_mode"; |
| 93 | private static final int USER_TYPE_PACKAGE_WHITELIST_MODE_DISABLE = 0; |
| 94 | private static final int USER_TYPE_PACKAGE_WHITELIST_MODE_ENFORCE = 0b001; |
| 95 | private static final int USER_TYPE_PACKAGE_WHITELIST_MODE_IMPLICIT_WHITELIST = 0b100; |
| 96 | private static final int USER_TYPE_PACKAGE_WHITELIST_MODE_DEVICE_DEFAULT = -1; |
| 97 | |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 98 | private UserManager mUm; |
| 99 | private ActivityManager mAm; |
| 100 | private IActivityManager mIam; |
jovanak | 5982cd9 | 2019-08-08 11:27:56 -0700 | [diff] [blame] | 101 | private PackageManager mPm; |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 102 | private ArrayList<Integer> mUsersToRemove; |
jovanak | 5982cd9 | 2019-08-08 11:27:56 -0700 | [diff] [blame] | 103 | private boolean mHasManagedUserFeature; |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 104 | |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 105 | private final BenchmarkRunner mRunner = new BenchmarkRunner(); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 106 | @Rule |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 107 | public BenchmarkResultsReporter mReporter = new BenchmarkResultsReporter(mRunner); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 108 | |
| 109 | @Before |
| 110 | public void setUp() { |
| 111 | final Context context = InstrumentationRegistry.getContext(); |
| 112 | mUm = UserManager.get(context); |
| 113 | mAm = context.getSystemService(ActivityManager.class); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 114 | mIam = ActivityManager.getService(); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 115 | mUsersToRemove = new ArrayList<>(); |
jovanak | 5982cd9 | 2019-08-08 11:27:56 -0700 | [diff] [blame] | 116 | mPm = context.getPackageManager(); |
| 117 | mHasManagedUserFeature = mPm.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @After |
| 121 | public void tearDown() { |
| 122 | for (int userId : mUsersToRemove) { |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 123 | try { |
| 124 | mUm.removeUser(userId); |
| 125 | } catch (Exception e) { |
| 126 | // Ignore |
| 127 | } |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | @Test |
jovanak | 6e6c6bf | 2019-09-04 11:14:49 -0700 | [diff] [blame] | 132 | public void createUser() { |
| 133 | while (mRunner.keepRunning()) { |
| 134 | final int userId = createUserNoFlags(); |
| 135 | |
| 136 | mRunner.pauseTiming(); |
| 137 | removeUser(userId); |
| 138 | mRunner.resumeTiming(); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | @Test |
Sudheer Shanka | a319f8b | 2017-07-11 15:28:48 -0700 | [diff] [blame] | 143 | public void createAndStartUser() throws Exception { |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 144 | while (mRunner.keepRunning()) { |
jovanak | 6e6c6bf | 2019-09-04 11:14:49 -0700 | [diff] [blame] | 145 | final int userId = createUserNoFlags(); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 146 | |
| 147 | final CountDownLatch latch = new CountDownLatch(1); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 148 | registerBroadcastReceiver(Intent.ACTION_USER_STARTED, latch, userId); |
jovanak | 288a18f | 2019-12-17 17:25:26 -0800 | [diff] [blame] | 149 | // Don't use this.startUserInBackgroundAndWaitForUnlock() since only waiting until |
| 150 | // ACTION_USER_STARTED. |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 151 | mIam.startUserInBackground(userId); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 152 | latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 153 | |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 154 | mRunner.pauseTiming(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 155 | removeUser(userId); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 156 | mRunner.resumeTiming(); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
jovanak | 288a18f | 2019-12-17 17:25:26 -0800 | [diff] [blame] | 160 | /** |
| 161 | * Measures the time until ACTION_USER_STARTED is received. |
| 162 | */ |
| 163 | @Test |
| 164 | public void startUser() throws Exception { |
| 165 | while (mRunner.keepRunning()) { |
| 166 | mRunner.pauseTiming(); |
| 167 | final int userId = createUserNoFlags(); |
| 168 | final CountDownLatch latch = new CountDownLatch(1); |
| 169 | registerBroadcastReceiver(Intent.ACTION_USER_STARTED, latch, userId); |
| 170 | mRunner.resumeTiming(); |
| 171 | |
| 172 | mIam.startUserInBackground(userId); |
| 173 | latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
| 174 | |
| 175 | mRunner.pauseTiming(); |
| 176 | removeUser(userId); |
| 177 | mRunner.resumeTiming(); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Measures the time until unlock listener is triggered and user is unlocked. |
| 183 | */ |
| 184 | @Test |
| 185 | public void startAndUnlockUser() throws Exception { |
| 186 | while (mRunner.keepRunning()) { |
| 187 | mRunner.pauseTiming(); |
| 188 | final int userId = createUserNoFlags(); |
| 189 | mRunner.resumeTiming(); |
| 190 | |
| 191 | // Waits for UserState.mUnlockProgress.finish(). |
| 192 | startUserInBackgroundAndWaitForUnlock(userId); |
| 193 | |
| 194 | mRunner.pauseTiming(); |
| 195 | removeUser(userId); |
| 196 | mRunner.resumeTiming(); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | |
| 201 | |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 202 | @Test |
Sudheer Shanka | a319f8b | 2017-07-11 15:28:48 -0700 | [diff] [blame] | 203 | public void switchUser() throws Exception { |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 204 | while (mRunner.keepRunning()) { |
| 205 | mRunner.pauseTiming(); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 206 | final int startUser = mAm.getCurrentUser(); |
jovanak | 6e6c6bf | 2019-09-04 11:14:49 -0700 | [diff] [blame] | 207 | final int userId = createUserNoFlags(); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 208 | mRunner.resumeTiming(); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 209 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 210 | switchUser(userId); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 211 | |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 212 | mRunner.pauseTiming(); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 213 | switchUser(startUser); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 214 | removeUser(userId); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 215 | mRunner.resumeTiming(); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
Bookatz | 52b12ac | 2019-02-28 17:29:26 -0800 | [diff] [blame] | 219 | /** Tests switching to an already-created, but no-longer-running, user. */ |
| 220 | @Test |
| 221 | public void switchUser_stopped() throws Exception { |
| 222 | while (mRunner.keepRunning()) { |
| 223 | mRunner.pauseTiming(); |
| 224 | final int startUser = mAm.getCurrentUser(); |
| 225 | final int testUser = initializeNewUserAndSwitchBack(/* stopNewUser */ true); |
| 226 | final CountDownLatch latch = new CountDownLatch(1); |
| 227 | registerBroadcastReceiver(Intent.ACTION_USER_UNLOCKED, latch, testUser); |
| 228 | mRunner.resumeTiming(); |
| 229 | |
| 230 | mAm.switchUser(testUser); |
| 231 | boolean success = latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
| 232 | |
| 233 | mRunner.pauseTiming(); |
| 234 | attestTrue("Failed to achieve 2nd ACTION_USER_UNLOCKED for user " + testUser, success); |
| 235 | switchUser(startUser); |
| 236 | removeUser(testUser); |
| 237 | mRunner.resumeTiming(); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /** Tests switching to an already-created already-running non-owner user. */ |
| 242 | @Test |
| 243 | public void switchUser_running() throws Exception { |
| 244 | while (mRunner.keepRunning()) { |
| 245 | mRunner.pauseTiming(); |
| 246 | final int startUser = mAm.getCurrentUser(); |
| 247 | final int testUser = initializeNewUserAndSwitchBack(/* stopNewUser */ false); |
| 248 | mRunner.resumeTiming(); |
| 249 | |
| 250 | switchUser(testUser); |
| 251 | |
| 252 | mRunner.pauseTiming(); |
| 253 | attestTrue("Failed to switch to user " + testUser, mAm.isUserRunning(testUser)); |
| 254 | switchUser(startUser); |
| 255 | removeUser(testUser); |
| 256 | mRunner.resumeTiming(); |
| 257 | } |
| 258 | } |
| 259 | |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 260 | @Test |
Sudheer Shanka | a319f8b | 2017-07-11 15:28:48 -0700 | [diff] [blame] | 261 | public void stopUser() throws Exception { |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 262 | while (mRunner.keepRunning()) { |
| 263 | mRunner.pauseTiming(); |
jovanak | 6e6c6bf | 2019-09-04 11:14:49 -0700 | [diff] [blame] | 264 | final int userId = createUserNoFlags(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 265 | final CountDownLatch latch = new CountDownLatch(1); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 266 | registerBroadcastReceiver(Intent.ACTION_USER_STARTED, latch, userId); |
| 267 | mIam.startUserInBackground(userId); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 268 | latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 269 | mRunner.resumeTiming(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 270 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 271 | stopUser(userId, false); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 272 | |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 273 | mRunner.pauseTiming(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 274 | removeUser(userId); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 275 | mRunner.resumeTiming(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | @Test |
Sudheer Shanka | a319f8b | 2017-07-11 15:28:48 -0700 | [diff] [blame] | 280 | public void lockedBootCompleted() throws Exception { |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 281 | while (mRunner.keepRunning()) { |
| 282 | mRunner.pauseTiming(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 283 | final int startUser = mAm.getCurrentUser(); |
jovanak | 6e6c6bf | 2019-09-04 11:14:49 -0700 | [diff] [blame] | 284 | final int userId = createUserNoFlags(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 285 | final CountDownLatch latch = new CountDownLatch(1); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 286 | registerUserSwitchObserver(null, latch, userId); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 287 | mRunner.resumeTiming(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 288 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 289 | mAm.switchUser(userId); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 290 | latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 291 | |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 292 | mRunner.pauseTiming(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 293 | switchUser(startUser); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 294 | removeUser(userId); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 295 | mRunner.resumeTiming(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | @Test |
Bookatz | 5df3c64 | 2019-04-11 09:22:49 -0700 | [diff] [blame] | 300 | public void ephemeralUserStopped() throws Exception { |
| 301 | while (mRunner.keepRunning()) { |
| 302 | mRunner.pauseTiming(); |
| 303 | final int startUser = mAm.getCurrentUser(); |
jovanak | 6e6c6bf | 2019-09-04 11:14:49 -0700 | [diff] [blame] | 304 | final int userId = createUserWithFlags(UserInfo.FLAG_EPHEMERAL | UserInfo.FLAG_DEMO); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 305 | switchUser(userId); |
Bookatz | 5df3c64 | 2019-04-11 09:22:49 -0700 | [diff] [blame] | 306 | final CountDownLatch latch = new CountDownLatch(1); |
| 307 | InstrumentationRegistry.getContext().registerReceiver(new BroadcastReceiver() { |
| 308 | @Override |
| 309 | public void onReceive(Context context, Intent intent) { |
| 310 | if (Intent.ACTION_USER_STOPPED.equals(intent.getAction()) && intent.getIntExtra( |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 311 | Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL) == userId) { |
Bookatz | 5df3c64 | 2019-04-11 09:22:49 -0700 | [diff] [blame] | 312 | latch.countDown(); |
| 313 | } |
| 314 | } |
| 315 | }, new IntentFilter(Intent.ACTION_USER_STOPPED)); |
| 316 | final CountDownLatch switchLatch = new CountDownLatch(1); |
| 317 | registerUserSwitchObserver(switchLatch, null, startUser); |
| 318 | mRunner.resumeTiming(); |
| 319 | |
| 320 | mAm.switchUser(startUser); |
| 321 | latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
| 322 | |
| 323 | mRunner.pauseTiming(); |
| 324 | switchLatch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 325 | removeUser(userId); |
Bookatz | 5df3c64 | 2019-04-11 09:22:49 -0700 | [diff] [blame] | 326 | mRunner.resumeTiming(); |
| 327 | } |
| 328 | } |
| 329 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 330 | /** Tests creating a new profile. */ |
| 331 | @Test |
| 332 | public void managedProfileCreate() throws Exception { |
jovanak | 5982cd9 | 2019-08-08 11:27:56 -0700 | [diff] [blame] | 333 | assumeTrue(mHasManagedUserFeature); |
| 334 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 335 | while (mRunner.keepRunning()) { |
| 336 | final int userId = createManagedProfile(); |
| 337 | |
| 338 | mRunner.pauseTiming(); |
| 339 | attestTrue("Failed creating profile " + userId, mUm.isManagedProfile(userId)); |
| 340 | removeUser(userId); |
| 341 | mRunner.resumeTiming(); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /** Tests starting (unlocking) a newly-created profile. */ |
Bookatz | 5df3c64 | 2019-04-11 09:22:49 -0700 | [diff] [blame] | 346 | @Test |
Sudheer Shanka | a319f8b | 2017-07-11 15:28:48 -0700 | [diff] [blame] | 347 | public void managedProfileUnlock() throws Exception { |
jovanak | 5982cd9 | 2019-08-08 11:27:56 -0700 | [diff] [blame] | 348 | assumeTrue(mHasManagedUserFeature); |
| 349 | |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 350 | while (mRunner.keepRunning()) { |
| 351 | mRunner.pauseTiming(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 352 | final int userId = createManagedProfile(); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 353 | mRunner.resumeTiming(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 354 | |
jovanak | 288a18f | 2019-12-17 17:25:26 -0800 | [diff] [blame] | 355 | startUserInBackgroundAndWaitForUnlock(userId); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 356 | |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 357 | mRunner.pauseTiming(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 358 | removeUser(userId); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 359 | mRunner.resumeTiming(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 363 | /** Tests starting (unlocking) an already-created, but no-longer-running, profile. */ |
Bookatz | 57908d9 | 2019-03-29 10:40:10 -0700 | [diff] [blame] | 364 | @Test |
| 365 | public void managedProfileUnlock_stopped() throws Exception { |
jovanak | 5982cd9 | 2019-08-08 11:27:56 -0700 | [diff] [blame] | 366 | assumeTrue(mHasManagedUserFeature); |
| 367 | |
Bookatz | 57908d9 | 2019-03-29 10:40:10 -0700 | [diff] [blame] | 368 | while (mRunner.keepRunning()) { |
| 369 | mRunner.pauseTiming(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 370 | final int userId = createManagedProfile(); |
Bookatz | 57908d9 | 2019-03-29 10:40:10 -0700 | [diff] [blame] | 371 | // Start the profile initially, then stop it. Similar to setQuietModeEnabled. |
jovanak | 288a18f | 2019-12-17 17:25:26 -0800 | [diff] [blame] | 372 | startUserInBackgroundAndWaitForUnlock(userId); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 373 | stopUser(userId, true); |
Bookatz | 57908d9 | 2019-03-29 10:40:10 -0700 | [diff] [blame] | 374 | mRunner.resumeTiming(); |
| 375 | |
jovanak | 288a18f | 2019-12-17 17:25:26 -0800 | [diff] [blame] | 376 | startUserInBackgroundAndWaitForUnlock(userId); |
Bookatz | 57908d9 | 2019-03-29 10:40:10 -0700 | [diff] [blame] | 377 | |
| 378 | mRunner.pauseTiming(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 379 | removeUser(userId); |
Bookatz | 57908d9 | 2019-03-29 10:40:10 -0700 | [diff] [blame] | 380 | mRunner.resumeTiming(); |
| 381 | } |
| 382 | } |
| 383 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 384 | /** |
| 385 | * Tests starting (unlocking) and launching an already-installed app in a newly-created profile. |
| 386 | */ |
| 387 | @Test |
| 388 | public void managedProfileUnlockAndLaunchApp() throws Exception { |
jovanak | 5982cd9 | 2019-08-08 11:27:56 -0700 | [diff] [blame] | 389 | assumeTrue(mHasManagedUserFeature); |
| 390 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 391 | while (mRunner.keepRunning()) { |
| 392 | mRunner.pauseTiming(); |
| 393 | final int userId = createManagedProfile(); |
| 394 | WindowManagerGlobal.getWindowManagerService().dismissKeyguard(null, null); |
| 395 | installPreexistingApp(userId, DUMMY_PACKAGE_NAME); |
| 396 | mRunner.resumeTiming(); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 397 | |
jovanak | 288a18f | 2019-12-17 17:25:26 -0800 | [diff] [blame] | 398 | startUserInBackgroundAndWaitForUnlock(userId); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 399 | startApp(userId, DUMMY_PACKAGE_NAME); |
| 400 | |
| 401 | mRunner.pauseTiming(); |
| 402 | removeUser(userId); |
| 403 | mRunner.resumeTiming(); |
| 404 | } |
| 405 | } |
| 406 | |
Bookatz | 5f87019 | 2019-05-30 10:04:30 -0700 | [diff] [blame] | 407 | /** |
| 408 | * Tests starting (unlocking) and launching a previously-launched app |
| 409 | * in an already-created, but no-longer-running, profile. |
| 410 | * A sort of combination of {@link #managedProfileUnlockAndLaunchApp} and |
| 411 | * {@link #managedProfileUnlock_stopped}}. |
| 412 | */ |
| 413 | @Test |
| 414 | public void managedProfileUnlockAndLaunchApp_stopped() throws Exception { |
jovanak | 5982cd9 | 2019-08-08 11:27:56 -0700 | [diff] [blame] | 415 | assumeTrue(mHasManagedUserFeature); |
| 416 | |
Bookatz | 5f87019 | 2019-05-30 10:04:30 -0700 | [diff] [blame] | 417 | while (mRunner.keepRunning()) { |
| 418 | mRunner.pauseTiming(); |
| 419 | final int userId = createManagedProfile(); |
| 420 | WindowManagerGlobal.getWindowManagerService().dismissKeyguard(null, null); |
| 421 | installPreexistingApp(userId, DUMMY_PACKAGE_NAME); |
jovanak | 288a18f | 2019-12-17 17:25:26 -0800 | [diff] [blame] | 422 | startUserInBackgroundAndWaitForUnlock(userId); |
Bookatz | 5f87019 | 2019-05-30 10:04:30 -0700 | [diff] [blame] | 423 | startApp(userId, DUMMY_PACKAGE_NAME); |
| 424 | stopUser(userId, true); |
| 425 | TimeUnit.SECONDS.sleep(1); // Brief cool-down before re-starting profile. |
| 426 | mRunner.resumeTiming(); |
| 427 | |
jovanak | 288a18f | 2019-12-17 17:25:26 -0800 | [diff] [blame] | 428 | startUserInBackgroundAndWaitForUnlock(userId); |
Bookatz | 5f87019 | 2019-05-30 10:04:30 -0700 | [diff] [blame] | 429 | startApp(userId, DUMMY_PACKAGE_NAME); |
| 430 | |
| 431 | mRunner.pauseTiming(); |
| 432 | removeUser(userId); |
| 433 | mRunner.resumeTiming(); |
| 434 | } |
| 435 | } |
| 436 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 437 | /** Tests installing a pre-existing app in a newly-created profile. */ |
| 438 | @Test |
| 439 | public void managedProfileInstall() throws Exception { |
jovanak | 5982cd9 | 2019-08-08 11:27:56 -0700 | [diff] [blame] | 440 | assumeTrue(mHasManagedUserFeature); |
| 441 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 442 | while (mRunner.keepRunning()) { |
| 443 | mRunner.pauseTiming(); |
| 444 | final int userId = createManagedProfile(); |
| 445 | mRunner.resumeTiming(); |
| 446 | |
| 447 | installPreexistingApp(userId, DUMMY_PACKAGE_NAME); |
| 448 | |
| 449 | mRunner.pauseTiming(); |
| 450 | removeUser(userId); |
| 451 | mRunner.resumeTiming(); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Tests creating a new profile, starting (unlocking) it, installing an app, |
| 457 | * and launching that app in it. |
| 458 | */ |
| 459 | @Test |
| 460 | public void managedProfileCreateUnlockInstallAndLaunchApp() throws Exception { |
jovanak | 5982cd9 | 2019-08-08 11:27:56 -0700 | [diff] [blame] | 461 | assumeTrue(mHasManagedUserFeature); |
| 462 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 463 | while (mRunner.keepRunning()) { |
| 464 | mRunner.pauseTiming(); |
| 465 | WindowManagerGlobal.getWindowManagerService().dismissKeyguard(null, null); |
| 466 | mRunner.resumeTiming(); |
| 467 | |
| 468 | final int userId = createManagedProfile(); |
jovanak | 288a18f | 2019-12-17 17:25:26 -0800 | [diff] [blame] | 469 | startUserInBackgroundAndWaitForUnlock(userId); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 470 | installPreexistingApp(userId, DUMMY_PACKAGE_NAME); |
| 471 | startApp(userId, DUMMY_PACKAGE_NAME); |
| 472 | |
| 473 | mRunner.pauseTiming(); |
| 474 | removeUser(userId); |
| 475 | mRunner.resumeTiming(); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | /** Tests stopping a profile. */ |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 480 | @Test |
Sudheer Shanka | a319f8b | 2017-07-11 15:28:48 -0700 | [diff] [blame] | 481 | public void managedProfileStopped() throws Exception { |
jovanak | 5982cd9 | 2019-08-08 11:27:56 -0700 | [diff] [blame] | 482 | assumeTrue(mHasManagedUserFeature); |
| 483 | |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 484 | while (mRunner.keepRunning()) { |
| 485 | mRunner.pauseTiming(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 486 | final int userId = createManagedProfile(); |
jovanak | 288a18f | 2019-12-17 17:25:26 -0800 | [diff] [blame] | 487 | startUserInBackgroundAndWaitForUnlock(userId); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 488 | mRunner.resumeTiming(); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 489 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 490 | stopUser(userId, true); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 491 | |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 492 | mRunner.pauseTiming(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 493 | removeUser(userId); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 494 | mRunner.resumeTiming(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
Bookatz | 04d7ae5 | 2019-08-05 14:07:12 -0700 | [diff] [blame] | 498 | // TODO: This is just a POC. Do this properly and add more. |
| 499 | /** Tests starting (unlocking) a newly-created profile using the user-type-pkg-whitelist. */ |
| 500 | @Test |
| 501 | public void managedProfileUnlock_usingWhitelist() throws Exception { |
| 502 | assumeTrue(mHasManagedUserFeature); |
| 503 | final int origMode = getUserTypePackageWhitelistMode(); |
| 504 | setUserTypePackageWhitelistMode(USER_TYPE_PACKAGE_WHITELIST_MODE_ENFORCE |
| 505 | | USER_TYPE_PACKAGE_WHITELIST_MODE_IMPLICIT_WHITELIST); |
| 506 | |
| 507 | try { |
| 508 | while (mRunner.keepRunning()) { |
| 509 | mRunner.pauseTiming(); |
| 510 | final int userId = createManagedProfile(); |
| 511 | mRunner.resumeTiming(); |
| 512 | |
jovanak | 288a18f | 2019-12-17 17:25:26 -0800 | [diff] [blame] | 513 | startUserInBackgroundAndWaitForUnlock(userId); |
Bookatz | 04d7ae5 | 2019-08-05 14:07:12 -0700 | [diff] [blame] | 514 | |
| 515 | mRunner.pauseTiming(); |
| 516 | removeUser(userId); |
| 517 | mRunner.resumeTiming(); |
| 518 | } |
| 519 | } finally { |
| 520 | setUserTypePackageWhitelistMode(origMode); |
| 521 | } |
| 522 | } |
| 523 | /** Tests starting (unlocking) a newly-created profile NOT using the user-type-pkg-whitelist. */ |
| 524 | @Test |
| 525 | public void managedProfileUnlock_notUsingWhitelist() throws Exception { |
| 526 | assumeTrue(mHasManagedUserFeature); |
| 527 | final int origMode = getUserTypePackageWhitelistMode(); |
| 528 | setUserTypePackageWhitelistMode(USER_TYPE_PACKAGE_WHITELIST_MODE_DISABLE); |
| 529 | |
| 530 | try { |
| 531 | while (mRunner.keepRunning()) { |
| 532 | mRunner.pauseTiming(); |
| 533 | final int userId = createManagedProfile(); |
| 534 | mRunner.resumeTiming(); |
| 535 | |
jovanak | 288a18f | 2019-12-17 17:25:26 -0800 | [diff] [blame] | 536 | startUserInBackgroundAndWaitForUnlock(userId); |
Bookatz | 04d7ae5 | 2019-08-05 14:07:12 -0700 | [diff] [blame] | 537 | |
| 538 | mRunner.pauseTiming(); |
| 539 | removeUser(userId); |
| 540 | mRunner.resumeTiming(); |
| 541 | } |
| 542 | } finally { |
| 543 | setUserTypePackageWhitelistMode(origMode); |
| 544 | } |
| 545 | } |
| 546 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 547 | /** Creates a new user, returning its userId. */ |
jovanak | 6e6c6bf | 2019-09-04 11:14:49 -0700 | [diff] [blame] | 548 | private int createUserNoFlags() { |
| 549 | return createUserWithFlags(/* flags= */ 0); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /** Creates a new user with the given flags, returning its userId. */ |
jovanak | 6e6c6bf | 2019-09-04 11:14:49 -0700 | [diff] [blame] | 553 | private int createUserWithFlags(int flags) { |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 554 | int userId = mUm.createUser("TestUser", flags).id; |
| 555 | mUsersToRemove.add(userId); |
| 556 | return userId; |
| 557 | } |
| 558 | |
| 559 | /** Creates a managed (work) profile under the current user, returning its userId. */ |
| 560 | private int createManagedProfile() { |
| 561 | final UserInfo userInfo = mUm.createProfileForUser("TestProfile", |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 562 | UserManager.USER_TYPE_PROFILE_MANAGED, /* flags */ 0, mAm.getCurrentUser()); |
Bookatz | 04d7ae5 | 2019-08-05 14:07:12 -0700 | [diff] [blame] | 563 | if (userInfo == null) { |
| 564 | throw new IllegalStateException("Creating managed profile failed. Most likely there is " |
| 565 | + "already a pre-existing profile on the device."); |
| 566 | } |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 567 | mUsersToRemove.add(userInfo.id); |
| 568 | return userInfo.id; |
| 569 | } |
| 570 | |
| 571 | /** |
jovanak | 288a18f | 2019-12-17 17:25:26 -0800 | [diff] [blame] | 572 | * Start user in background and wait for it to unlock by waiting for |
| 573 | * UserState.mUnlockProgress.finish(). |
| 574 | * <p> To start in foreground instead, see {@link #switchUser(int)}. |
| 575 | * <p> This should always be used for profiles since profiles cannot be started in foreground. |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 576 | */ |
jovanak | 288a18f | 2019-12-17 17:25:26 -0800 | [diff] [blame] | 577 | private void startUserInBackgroundAndWaitForUnlock(int userId) { |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 578 | final ProgressWaiter waiter = new ProgressWaiter(); |
| 579 | try { |
| 580 | mIam.startUserInBackgroundWithListener(userId, waiter); |
| 581 | boolean success = waiter.waitForFinish(TIMEOUT_IN_SECOND); |
| 582 | attestTrue("Failed to start user " + userId + " in background.", success); |
| 583 | } catch (RemoteException e) { |
jovanak | 288a18f | 2019-12-17 17:25:26 -0800 | [diff] [blame] | 584 | Log.e(TAG, "startUserInBackgroundAndWaitForUnlock failed", e); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
| 588 | /** Starts the given user in the foreground. */ |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 589 | private void switchUser(int userId) throws Exception { |
| 590 | final CountDownLatch latch = new CountDownLatch(1); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 591 | registerUserSwitchObserver(latch, null, userId); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 592 | mAm.switchUser(userId); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 593 | latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 596 | private void stopUser(int userId, boolean force) throws Exception { |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 597 | final CountDownLatch latch = new CountDownLatch(1); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 598 | mIam.stopUser(userId, force /* force */, new IStopUserCallback.Stub() { |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 599 | @Override |
| 600 | public void userStopped(int userId) throws RemoteException { |
| 601 | latch.countDown(); |
| 602 | } |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 603 | |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 604 | @Override |
| 605 | public void userStopAborted(int userId) throws RemoteException { |
| 606 | } |
| 607 | }); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 608 | latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Bookatz | 52b12ac | 2019-02-28 17:29:26 -0800 | [diff] [blame] | 611 | /** |
| 612 | * Creates a user and waits for its ACTION_USER_UNLOCKED. |
| 613 | * Then switches to back to the original user and waits for its switchUser() to finish. |
| 614 | * |
| 615 | * @param stopNewUser whether to stop the new user after switching to otherUser. |
| 616 | * @return userId of the newly created user. |
| 617 | */ |
| 618 | private int initializeNewUserAndSwitchBack(boolean stopNewUser) throws Exception { |
| 619 | final int origUser = mAm.getCurrentUser(); |
| 620 | // First, create and switch to testUser, waiting for its ACTION_USER_UNLOCKED |
jovanak | 6e6c6bf | 2019-09-04 11:14:49 -0700 | [diff] [blame] | 621 | final int testUser = createUserNoFlags(); |
Bookatz | 52b12ac | 2019-02-28 17:29:26 -0800 | [diff] [blame] | 622 | final CountDownLatch latch1 = new CountDownLatch(1); |
| 623 | registerBroadcastReceiver(Intent.ACTION_USER_UNLOCKED, latch1, testUser); |
| 624 | mAm.switchUser(testUser); |
| 625 | attestTrue("Failed to achieve initial ACTION_USER_UNLOCKED for user " + testUser, |
| 626 | latch1.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS)); |
| 627 | |
| 628 | // Second, switch back to origUser, waiting merely for switchUser() to finish |
| 629 | switchUser(origUser); |
| 630 | attestTrue("Didn't switch back to user, " + origUser, origUser == mAm.getCurrentUser()); |
| 631 | |
| 632 | if (stopNewUser) { |
| 633 | stopUser(testUser, true); |
| 634 | attestFalse("Failed to stop user " + testUser, mAm.isUserRunning(testUser)); |
| 635 | } |
| 636 | |
| 637 | return testUser; |
| 638 | } |
| 639 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 640 | /** |
| 641 | * Installs the given package in the given user. |
| 642 | */ |
| 643 | private void installPreexistingApp(int userId, String packageName) throws RemoteException { |
| 644 | final CountDownLatch latch = new CountDownLatch(1); |
| 645 | |
| 646 | final IntentSender sender = new IntentSender((IIntentSender) new IIntentSender.Stub() { |
| 647 | @Override |
| 648 | public void send(int code, Intent intent, String resolvedType, IBinder whitelistToken, |
| 649 | IIntentReceiver finishedReceiver, String requiredPermission, Bundle options) { |
| 650 | latch.countDown(); |
| 651 | } |
| 652 | }); |
| 653 | |
| 654 | final IPackageInstaller installer = AppGlobals.getPackageManager().getPackageInstaller(); |
Philip P. Moltmann | bfcffa0 | 2019-05-13 17:10:46 -0700 | [diff] [blame] | 655 | installer.installExistingPackage(packageName, |
| 656 | PackageManager.INSTALL_ALL_WHITELIST_RESTRICTED_PERMISSIONS, |
| 657 | PackageManager.INSTALL_REASON_UNKNOWN, sender, userId, null); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 658 | |
| 659 | try { |
| 660 | latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
| 661 | } catch (InterruptedException e) { |
| 662 | Log.e(TAG, "Thread interrupted unexpectedly.", e); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * Launches the given package in the given user. |
| 668 | * Make sure the keyguard has been dismissed prior to calling. |
| 669 | */ |
| 670 | private void startApp(int userId, String packageName) throws RemoteException { |
| 671 | final Context context = InstrumentationRegistry.getContext(); |
Robert Horvath | 5f47c59 | 2019-07-24 16:28:29 +0200 | [diff] [blame] | 672 | final WaitResult result = ActivityTaskManager.getService().startActivityAndWait(null, |
Philip P. Moltmann | 9c5226f | 2020-01-10 08:53:43 -0800 | [diff] [blame^] | 673 | context.getPackageName(), context.getFeatureId(), |
Robert Horvath | 5f47c59 | 2019-07-24 16:28:29 +0200 | [diff] [blame] | 674 | context.getPackageManager().getLaunchIntentForPackage(packageName), null, null, |
| 675 | null, 0, 0, null, null, userId); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 676 | attestTrue("User " + userId + " failed to start " + packageName, |
| 677 | result.result == ActivityManager.START_SUCCESS); |
| 678 | } |
| 679 | |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 680 | private void registerUserSwitchObserver(final CountDownLatch switchLatch, |
| 681 | final CountDownLatch bootCompleteLatch, final int userId) throws Exception { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 682 | ActivityManager.getService().registerUserSwitchObserver( |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 683 | new UserSwitchObserver() { |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 684 | @Override |
| 685 | public void onUserSwitchComplete(int newUserId) throws RemoteException { |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 686 | if (switchLatch != null && userId == newUserId) { |
| 687 | switchLatch.countDown(); |
| 688 | } |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | @Override |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 692 | public void onLockedBootComplete(int newUserId) { |
| 693 | if (bootCompleteLatch != null && userId == newUserId) { |
| 694 | bootCompleteLatch.countDown(); |
| 695 | } |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 696 | } |
Sudheer Shanka | 2d0278b | 2017-07-12 18:12:31 -0700 | [diff] [blame] | 697 | }, TAG); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 700 | private void registerBroadcastReceiver(final String action, final CountDownLatch latch, |
| 701 | final int userId) { |
| 702 | InstrumentationRegistry.getContext().registerReceiverAsUser(new BroadcastReceiver() { |
| 703 | @Override |
| 704 | public void onReceive(Context context, Intent intent) { |
| 705 | if (action.equals(intent.getAction()) && intent.getIntExtra( |
| 706 | Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL) == userId) { |
| 707 | latch.countDown(); |
| 708 | } |
| 709 | } |
| 710 | }, UserHandle.of(userId), new IntentFilter(action), null, null); |
| 711 | } |
| 712 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 713 | private class ProgressWaiter extends IProgressListener.Stub { |
| 714 | private final CountDownLatch mFinishedLatch = new CountDownLatch(1); |
| 715 | |
| 716 | @Override |
| 717 | public void onStarted(int id, Bundle extras) {} |
| 718 | |
| 719 | @Override |
| 720 | public void onProgress(int id, int progress, Bundle extras) {} |
| 721 | |
| 722 | @Override |
| 723 | public void onFinished(int id, Bundle extras) { |
| 724 | mFinishedLatch.countDown(); |
| 725 | } |
| 726 | |
| 727 | public boolean waitForFinish(long timeoutSecs) { |
| 728 | try { |
| 729 | return mFinishedLatch.await(timeoutSecs, TimeUnit.SECONDS); |
| 730 | } catch (InterruptedException e) { |
| 731 | Log.e(TAG, "Thread interrupted unexpectedly.", e); |
| 732 | return false; |
| 733 | } |
| 734 | } |
| 735 | } |
| 736 | |
Bookatz | 04d7ae5 | 2019-08-05 14:07:12 -0700 | [diff] [blame] | 737 | /** Gets the PACKAGE_WHITELIST_MODE_PROP System Property. */ |
| 738 | private int getUserTypePackageWhitelistMode() { |
| 739 | return SystemProperties.getInt(PACKAGE_WHITELIST_MODE_PROP, |
| 740 | USER_TYPE_PACKAGE_WHITELIST_MODE_DEVICE_DEFAULT); |
| 741 | } |
| 742 | |
| 743 | /** Sets the PACKAGE_WHITELIST_MODE_PROP System Property to the given value. */ |
| 744 | private void setUserTypePackageWhitelistMode(int mode) { |
| 745 | String result = ShellHelper.runShellCommand( |
| 746 | String.format("setprop %s %d", PACKAGE_WHITELIST_MODE_PROP, mode)); |
| 747 | attestFalse("Failed to set sysprop " + PACKAGE_WHITELIST_MODE_PROP + ": " + result, |
| 748 | result != null && result.contains("Failed")); |
| 749 | } |
| 750 | |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 751 | private void removeUser(int userId) { |
| 752 | try { |
| 753 | mUm.removeUser(userId); |
| 754 | final long startTime = System.currentTimeMillis(); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 755 | final long timeoutInMs = TIMEOUT_IN_SECOND * 1000; |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 756 | while (mUm.getUserInfo(userId) != null && |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 757 | System.currentTimeMillis() - startTime < timeoutInMs) { |
| 758 | TimeUnit.MILLISECONDS.sleep(CHECK_USER_REMOVED_INTERVAL_MS); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 759 | } |
| 760 | } catch (InterruptedException e) { |
| 761 | Thread.currentThread().interrupt(); |
| 762 | } catch (Exception e) { |
| 763 | // Ignore |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 764 | } |
| 765 | if (mUm.getUserInfo(userId) != null) { |
| 766 | mUsersToRemove.add(userId); |
| 767 | } |
| 768 | } |
Bookatz | 52b12ac | 2019-02-28 17:29:26 -0800 | [diff] [blame] | 769 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 770 | private void attestTrue(String message, boolean assertion) { |
| 771 | if (!assertion) { |
Bookatz | 52b12ac | 2019-02-28 17:29:26 -0800 | [diff] [blame] | 772 | Log.w(TAG, message); |
| 773 | } |
| 774 | } |
| 775 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 776 | private void attestFalse(String message, boolean assertion) { |
| 777 | attestTrue(message, !assertion); |
Bookatz | 52b12ac | 2019-02-28 17:29:26 -0800 | [diff] [blame] | 778 | } |
John Reck | 62e5fea | 2016-10-12 15:27:52 -0700 | [diff] [blame] | 779 | } |