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 | |
| 18 | import android.app.ActivityManager; |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 19 | import android.app.ActivityTaskManager; |
| 20 | import android.app.AppGlobals; |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 21 | import android.app.IActivityManager; |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 22 | import android.app.IStopUserCallback; |
| 23 | import android.app.UserSwitchObserver; |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 24 | import android.app.WaitResult; |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 25 | import android.content.BroadcastReceiver; |
| 26 | import android.content.Context; |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 27 | import android.content.IIntentReceiver; |
| 28 | import android.content.IIntentSender; |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 29 | import android.content.Intent; |
| 30 | import android.content.IntentFilter; |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 31 | import android.content.IntentSender; |
| 32 | import android.content.pm.IPackageInstaller; |
| 33 | import android.content.pm.PackageManager; |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 34 | import android.content.pm.UserInfo; |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 35 | import android.os.Bundle; |
| 36 | import android.os.IBinder; |
| 37 | import android.os.IProgressListener; |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 38 | import android.os.RemoteException; |
| 39 | import android.os.UserHandle; |
| 40 | import android.os.UserManager; |
Bookatz | 52b12ac | 2019-02-28 17:29:26 -0800 | [diff] [blame] | 41 | import android.util.Log; |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 42 | import android.view.WindowManagerGlobal; |
KOUSHIK PANUGANTI | 130f0a5 | 2018-12-17 14:41:06 -0800 | [diff] [blame] | 43 | |
| 44 | import androidx.test.InstrumentationRegistry; |
| 45 | import androidx.test.filters.LargeTest; |
| 46 | import androidx.test.runner.AndroidJUnit4; |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 47 | |
| 48 | import org.junit.After; |
| 49 | import org.junit.Before; |
| 50 | import org.junit.Rule; |
| 51 | import org.junit.Test; |
| 52 | import org.junit.runner.RunWith; |
| 53 | |
| 54 | import java.util.ArrayList; |
| 55 | import java.util.concurrent.CountDownLatch; |
| 56 | import java.util.concurrent.TimeUnit; |
| 57 | |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Perf tests for user life cycle events. |
| 60 | * |
| 61 | * Running the tests: |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 62 | * |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 63 | * make MultiUserPerfTests && |
| 64 | * adb install -r \ |
| 65 | * ${ANDROID_PRODUCT_OUT}/data/app/MultiUserPerfTests/MultiUserPerfTests.apk && |
Sudheer Shanka | 2d0278b | 2017-07-12 18:12:31 -0700 | [diff] [blame] | 66 | * adb shell am instrument -e class android.multiuser.UserLifecycleTests \ |
KOUSHIK PANUGANTI | 130f0a5 | 2018-12-17 14:41:06 -0800 | [diff] [blame] | 67 | * -w com.android.perftests.multiuser/androidx.test.runner.AndroidJUnitRunner |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 68 | * |
| 69 | * or |
| 70 | * |
Sudheer Shanka | 2d0278b | 2017-07-12 18:12:31 -0700 | [diff] [blame] | 71 | * bit MultiUserPerfTests:android.multiuser.UserLifecycleTests |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 72 | * |
| 73 | * 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] | 74 | * 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] | 75 | */ |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 76 | @LargeTest |
| 77 | @RunWith(AndroidJUnit4.class) |
Sudheer Shanka | 2d0278b | 2017-07-12 18:12:31 -0700 | [diff] [blame] | 78 | public class UserLifecycleTests { |
| 79 | private static final String TAG = UserLifecycleTests.class.getSimpleName(); |
| 80 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 81 | private static final int TIMEOUT_IN_SECOND = 30; |
| 82 | private static final int CHECK_USER_REMOVED_INTERVAL_MS = 200; |
| 83 | |
| 84 | private static final String DUMMY_PACKAGE_NAME = "perftests.multiuser.apps.dummyapp"; |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 85 | |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 86 | private UserManager mUm; |
| 87 | private ActivityManager mAm; |
| 88 | private IActivityManager mIam; |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 89 | private ArrayList<Integer> mUsersToRemove; |
| 90 | |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 91 | private final BenchmarkRunner mRunner = new BenchmarkRunner(); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 92 | @Rule |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 93 | public BenchmarkResultsReporter mReporter = new BenchmarkResultsReporter(mRunner); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 94 | |
| 95 | @Before |
| 96 | public void setUp() { |
| 97 | final Context context = InstrumentationRegistry.getContext(); |
| 98 | mUm = UserManager.get(context); |
| 99 | mAm = context.getSystemService(ActivityManager.class); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 100 | mIam = ActivityManager.getService(); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 101 | mUsersToRemove = new ArrayList<>(); |
| 102 | } |
| 103 | |
| 104 | @After |
| 105 | public void tearDown() { |
| 106 | for (int userId : mUsersToRemove) { |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 107 | try { |
| 108 | mUm.removeUser(userId); |
| 109 | } catch (Exception e) { |
| 110 | // Ignore |
| 111 | } |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | @Test |
Sudheer Shanka | a319f8b | 2017-07-11 15:28:48 -0700 | [diff] [blame] | 116 | public void createAndStartUser() throws Exception { |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 117 | while (mRunner.keepRunning()) { |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 118 | final int userId = createUser(); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 119 | |
| 120 | final CountDownLatch latch = new CountDownLatch(1); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 121 | registerBroadcastReceiver(Intent.ACTION_USER_STARTED, latch, userId); |
| 122 | // Don't use this.startUserInBackground() since only waiting until ACTION_USER_STARTED. |
| 123 | mIam.startUserInBackground(userId); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 124 | latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 125 | |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 126 | mRunner.pauseTiming(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 127 | removeUser(userId); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 128 | mRunner.resumeTiming(); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | @Test |
Sudheer Shanka | a319f8b | 2017-07-11 15:28:48 -0700 | [diff] [blame] | 133 | public void switchUser() throws Exception { |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 134 | while (mRunner.keepRunning()) { |
| 135 | mRunner.pauseTiming(); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 136 | final int startUser = mAm.getCurrentUser(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 137 | final int userId = createUser(); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 138 | mRunner.resumeTiming(); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 139 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 140 | switchUser(userId); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 141 | |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 142 | mRunner.pauseTiming(); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 143 | switchUser(startUser); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 144 | removeUser(userId); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 145 | mRunner.resumeTiming(); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Bookatz | 52b12ac | 2019-02-28 17:29:26 -0800 | [diff] [blame] | 149 | /** Tests switching to an already-created, but no-longer-running, user. */ |
| 150 | @Test |
| 151 | public void switchUser_stopped() throws Exception { |
| 152 | while (mRunner.keepRunning()) { |
| 153 | mRunner.pauseTiming(); |
| 154 | final int startUser = mAm.getCurrentUser(); |
| 155 | final int testUser = initializeNewUserAndSwitchBack(/* stopNewUser */ true); |
| 156 | final CountDownLatch latch = new CountDownLatch(1); |
| 157 | registerBroadcastReceiver(Intent.ACTION_USER_UNLOCKED, latch, testUser); |
| 158 | mRunner.resumeTiming(); |
| 159 | |
| 160 | mAm.switchUser(testUser); |
| 161 | boolean success = latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
| 162 | |
| 163 | mRunner.pauseTiming(); |
| 164 | attestTrue("Failed to achieve 2nd ACTION_USER_UNLOCKED for user " + testUser, success); |
| 165 | switchUser(startUser); |
| 166 | removeUser(testUser); |
| 167 | mRunner.resumeTiming(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** Tests switching to an already-created already-running non-owner user. */ |
| 172 | @Test |
| 173 | public void switchUser_running() throws Exception { |
| 174 | while (mRunner.keepRunning()) { |
| 175 | mRunner.pauseTiming(); |
| 176 | final int startUser = mAm.getCurrentUser(); |
| 177 | final int testUser = initializeNewUserAndSwitchBack(/* stopNewUser */ false); |
| 178 | mRunner.resumeTiming(); |
| 179 | |
| 180 | switchUser(testUser); |
| 181 | |
| 182 | mRunner.pauseTiming(); |
| 183 | attestTrue("Failed to switch to user " + testUser, mAm.isUserRunning(testUser)); |
| 184 | switchUser(startUser); |
| 185 | removeUser(testUser); |
| 186 | mRunner.resumeTiming(); |
| 187 | } |
| 188 | } |
| 189 | |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 190 | @Test |
Sudheer Shanka | a319f8b | 2017-07-11 15:28:48 -0700 | [diff] [blame] | 191 | public void stopUser() throws Exception { |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 192 | while (mRunner.keepRunning()) { |
| 193 | mRunner.pauseTiming(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 194 | final int userId = createUser(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 195 | final CountDownLatch latch = new CountDownLatch(1); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 196 | registerBroadcastReceiver(Intent.ACTION_USER_STARTED, latch, userId); |
| 197 | mIam.startUserInBackground(userId); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 198 | latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 199 | mRunner.resumeTiming(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 200 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 201 | stopUser(userId, false); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 202 | |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 203 | mRunner.pauseTiming(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 204 | removeUser(userId); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 205 | mRunner.resumeTiming(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | @Test |
Sudheer Shanka | a319f8b | 2017-07-11 15:28:48 -0700 | [diff] [blame] | 210 | public void lockedBootCompleted() throws Exception { |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 211 | while (mRunner.keepRunning()) { |
| 212 | mRunner.pauseTiming(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 213 | final int startUser = mAm.getCurrentUser(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 214 | final int userId = createUser(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 215 | final CountDownLatch latch = new CountDownLatch(1); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 216 | registerUserSwitchObserver(null, latch, userId); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 217 | mRunner.resumeTiming(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 218 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 219 | mAm.switchUser(userId); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 220 | latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 221 | |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 222 | mRunner.pauseTiming(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 223 | switchUser(startUser); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 224 | removeUser(userId); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 225 | mRunner.resumeTiming(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | @Test |
Bookatz | 5df3c64 | 2019-04-11 09:22:49 -0700 | [diff] [blame] | 230 | public void ephemeralUserStopped() throws Exception { |
| 231 | while (mRunner.keepRunning()) { |
| 232 | mRunner.pauseTiming(); |
| 233 | final int startUser = mAm.getCurrentUser(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 234 | final int userId = createUser(UserInfo.FLAG_EPHEMERAL | UserInfo.FLAG_DEMO); |
| 235 | switchUser(userId); |
Bookatz | 5df3c64 | 2019-04-11 09:22:49 -0700 | [diff] [blame] | 236 | final CountDownLatch latch = new CountDownLatch(1); |
| 237 | InstrumentationRegistry.getContext().registerReceiver(new BroadcastReceiver() { |
| 238 | @Override |
| 239 | public void onReceive(Context context, Intent intent) { |
| 240 | if (Intent.ACTION_USER_STOPPED.equals(intent.getAction()) && intent.getIntExtra( |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 241 | Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL) == userId) { |
Bookatz | 5df3c64 | 2019-04-11 09:22:49 -0700 | [diff] [blame] | 242 | latch.countDown(); |
| 243 | } |
| 244 | } |
| 245 | }, new IntentFilter(Intent.ACTION_USER_STOPPED)); |
| 246 | final CountDownLatch switchLatch = new CountDownLatch(1); |
| 247 | registerUserSwitchObserver(switchLatch, null, startUser); |
| 248 | mRunner.resumeTiming(); |
| 249 | |
| 250 | mAm.switchUser(startUser); |
| 251 | latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
| 252 | |
| 253 | mRunner.pauseTiming(); |
| 254 | switchLatch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 255 | removeUser(userId); |
Bookatz | 5df3c64 | 2019-04-11 09:22:49 -0700 | [diff] [blame] | 256 | mRunner.resumeTiming(); |
| 257 | } |
| 258 | } |
| 259 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 260 | /** Tests creating a new profile. */ |
| 261 | @Test |
| 262 | public void managedProfileCreate() throws Exception { |
| 263 | while (mRunner.keepRunning()) { |
| 264 | final int userId = createManagedProfile(); |
| 265 | |
| 266 | mRunner.pauseTiming(); |
| 267 | attestTrue("Failed creating profile " + userId, mUm.isManagedProfile(userId)); |
| 268 | removeUser(userId); |
| 269 | mRunner.resumeTiming(); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /** Tests starting (unlocking) a newly-created profile. */ |
Bookatz | 5df3c64 | 2019-04-11 09:22:49 -0700 | [diff] [blame] | 274 | @Test |
Sudheer Shanka | a319f8b | 2017-07-11 15:28:48 -0700 | [diff] [blame] | 275 | public void managedProfileUnlock() throws Exception { |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 276 | while (mRunner.keepRunning()) { |
| 277 | mRunner.pauseTiming(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 278 | final int userId = createManagedProfile(); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 279 | mRunner.resumeTiming(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 280 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 281 | startUserInBackground(userId); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 282 | |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 283 | mRunner.pauseTiming(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 284 | removeUser(userId); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 285 | mRunner.resumeTiming(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 289 | /** Tests starting (unlocking) an already-created, but no-longer-running, profile. */ |
Bookatz | 57908d9 | 2019-03-29 10:40:10 -0700 | [diff] [blame] | 290 | @Test |
| 291 | public void managedProfileUnlock_stopped() throws Exception { |
| 292 | while (mRunner.keepRunning()) { |
| 293 | mRunner.pauseTiming(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 294 | final int userId = createManagedProfile(); |
Bookatz | 57908d9 | 2019-03-29 10:40:10 -0700 | [diff] [blame] | 295 | // Start the profile initially, then stop it. Similar to setQuietModeEnabled. |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 296 | startUserInBackground(userId); |
| 297 | stopUser(userId, true); |
Bookatz | 57908d9 | 2019-03-29 10:40:10 -0700 | [diff] [blame] | 298 | mRunner.resumeTiming(); |
| 299 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 300 | startUserInBackground(userId); |
Bookatz | 57908d9 | 2019-03-29 10:40:10 -0700 | [diff] [blame] | 301 | |
| 302 | mRunner.pauseTiming(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 303 | removeUser(userId); |
Bookatz | 57908d9 | 2019-03-29 10:40:10 -0700 | [diff] [blame] | 304 | mRunner.resumeTiming(); |
| 305 | } |
| 306 | } |
| 307 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 308 | /** |
| 309 | * Tests starting (unlocking) and launching an already-installed app in a newly-created profile. |
| 310 | */ |
| 311 | @Test |
| 312 | public void managedProfileUnlockAndLaunchApp() throws Exception { |
| 313 | while (mRunner.keepRunning()) { |
| 314 | mRunner.pauseTiming(); |
| 315 | final int userId = createManagedProfile(); |
| 316 | WindowManagerGlobal.getWindowManagerService().dismissKeyguard(null, null); |
| 317 | installPreexistingApp(userId, DUMMY_PACKAGE_NAME); |
| 318 | mRunner.resumeTiming(); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 319 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 320 | startUserInBackground(userId); |
| 321 | startApp(userId, DUMMY_PACKAGE_NAME); |
| 322 | |
| 323 | mRunner.pauseTiming(); |
| 324 | removeUser(userId); |
| 325 | mRunner.resumeTiming(); |
| 326 | } |
| 327 | } |
| 328 | |
Bookatz | 5f87019 | 2019-05-30 10:04:30 -0700 | [diff] [blame^] | 329 | /** |
| 330 | * Tests starting (unlocking) and launching a previously-launched app |
| 331 | * in an already-created, but no-longer-running, profile. |
| 332 | * A sort of combination of {@link #managedProfileUnlockAndLaunchApp} and |
| 333 | * {@link #managedProfileUnlock_stopped}}. |
| 334 | */ |
| 335 | @Test |
| 336 | public void managedProfileUnlockAndLaunchApp_stopped() throws Exception { |
| 337 | while (mRunner.keepRunning()) { |
| 338 | mRunner.pauseTiming(); |
| 339 | final int userId = createManagedProfile(); |
| 340 | WindowManagerGlobal.getWindowManagerService().dismissKeyguard(null, null); |
| 341 | installPreexistingApp(userId, DUMMY_PACKAGE_NAME); |
| 342 | startUserInBackground(userId); |
| 343 | startApp(userId, DUMMY_PACKAGE_NAME); |
| 344 | stopUser(userId, true); |
| 345 | TimeUnit.SECONDS.sleep(1); // Brief cool-down before re-starting profile. |
| 346 | mRunner.resumeTiming(); |
| 347 | |
| 348 | startUserInBackground(userId); |
| 349 | startApp(userId, DUMMY_PACKAGE_NAME); |
| 350 | |
| 351 | mRunner.pauseTiming(); |
| 352 | removeUser(userId); |
| 353 | mRunner.resumeTiming(); |
| 354 | } |
| 355 | } |
| 356 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 357 | /** Tests installing a pre-existing app in a newly-created profile. */ |
| 358 | @Test |
| 359 | public void managedProfileInstall() throws Exception { |
| 360 | while (mRunner.keepRunning()) { |
| 361 | mRunner.pauseTiming(); |
| 362 | final int userId = createManagedProfile(); |
| 363 | mRunner.resumeTiming(); |
| 364 | |
| 365 | installPreexistingApp(userId, DUMMY_PACKAGE_NAME); |
| 366 | |
| 367 | mRunner.pauseTiming(); |
| 368 | removeUser(userId); |
| 369 | mRunner.resumeTiming(); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Tests creating a new profile, starting (unlocking) it, installing an app, |
| 375 | * and launching that app in it. |
| 376 | */ |
| 377 | @Test |
| 378 | public void managedProfileCreateUnlockInstallAndLaunchApp() throws Exception { |
| 379 | final String packageName = "perftests.multiuser.apps.dummyapp"; |
| 380 | while (mRunner.keepRunning()) { |
| 381 | mRunner.pauseTiming(); |
| 382 | WindowManagerGlobal.getWindowManagerService().dismissKeyguard(null, null); |
| 383 | mRunner.resumeTiming(); |
| 384 | |
| 385 | final int userId = createManagedProfile(); |
| 386 | startUserInBackground(userId); |
| 387 | installPreexistingApp(userId, DUMMY_PACKAGE_NAME); |
| 388 | startApp(userId, DUMMY_PACKAGE_NAME); |
| 389 | |
| 390 | mRunner.pauseTiming(); |
| 391 | removeUser(userId); |
| 392 | mRunner.resumeTiming(); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | /** Tests stopping a profile. */ |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 397 | @Test |
Sudheer Shanka | a319f8b | 2017-07-11 15:28:48 -0700 | [diff] [blame] | 398 | public void managedProfileStopped() throws Exception { |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 399 | while (mRunner.keepRunning()) { |
| 400 | mRunner.pauseTiming(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 401 | final int userId = createManagedProfile(); |
| 402 | startUserInBackground(userId); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 403 | mRunner.resumeTiming(); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 404 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 405 | stopUser(userId, true); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 406 | |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 407 | mRunner.pauseTiming(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 408 | removeUser(userId); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 409 | mRunner.resumeTiming(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 413 | /** Creates a new user, returning its userId. */ |
| 414 | private int createUser() { |
| 415 | return createUser(0); |
| 416 | } |
| 417 | |
| 418 | /** Creates a new user with the given flags, returning its userId. */ |
| 419 | private int createUser(int flags) { |
| 420 | int userId = mUm.createUser("TestUser", flags).id; |
| 421 | mUsersToRemove.add(userId); |
| 422 | return userId; |
| 423 | } |
| 424 | |
| 425 | /** Creates a managed (work) profile under the current user, returning its userId. */ |
| 426 | private int createManagedProfile() { |
| 427 | final UserInfo userInfo = mUm.createProfileForUser("TestProfile", |
| 428 | UserInfo.FLAG_MANAGED_PROFILE, mAm.getCurrentUser()); |
| 429 | mUsersToRemove.add(userInfo.id); |
| 430 | return userInfo.id; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Start user in background and wait for it to unlock (equivalent to ACTION_USER_UNLOCKED). |
| 435 | * To start in foreground instead, see {@link #switchUser(int)}. |
| 436 | * This should always be used for profiles since profiles cannot be started in foreground. |
| 437 | */ |
| 438 | private void startUserInBackground(int userId) { |
| 439 | final ProgressWaiter waiter = new ProgressWaiter(); |
| 440 | try { |
| 441 | mIam.startUserInBackgroundWithListener(userId, waiter); |
| 442 | boolean success = waiter.waitForFinish(TIMEOUT_IN_SECOND); |
| 443 | attestTrue("Failed to start user " + userId + " in background.", success); |
| 444 | } catch (RemoteException e) { |
| 445 | Log.e(TAG, "startUserInBackground failed", e); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | /** Starts the given user in the foreground. */ |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 450 | private void switchUser(int userId) throws Exception { |
| 451 | final CountDownLatch latch = new CountDownLatch(1); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 452 | registerUserSwitchObserver(latch, null, userId); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 453 | mAm.switchUser(userId); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 454 | latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 457 | private void stopUser(int userId, boolean force) throws Exception { |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 458 | final CountDownLatch latch = new CountDownLatch(1); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 459 | mIam.stopUser(userId, force /* force */, new IStopUserCallback.Stub() { |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 460 | @Override |
| 461 | public void userStopped(int userId) throws RemoteException { |
| 462 | latch.countDown(); |
| 463 | } |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 464 | |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 465 | @Override |
| 466 | public void userStopAborted(int userId) throws RemoteException { |
| 467 | } |
| 468 | }); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 469 | latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Bookatz | 52b12ac | 2019-02-28 17:29:26 -0800 | [diff] [blame] | 472 | /** |
| 473 | * Creates a user and waits for its ACTION_USER_UNLOCKED. |
| 474 | * Then switches to back to the original user and waits for its switchUser() to finish. |
| 475 | * |
| 476 | * @param stopNewUser whether to stop the new user after switching to otherUser. |
| 477 | * @return userId of the newly created user. |
| 478 | */ |
| 479 | private int initializeNewUserAndSwitchBack(boolean stopNewUser) throws Exception { |
| 480 | final int origUser = mAm.getCurrentUser(); |
| 481 | // First, create and switch to testUser, waiting for its ACTION_USER_UNLOCKED |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 482 | final int testUser = createUser(); |
Bookatz | 52b12ac | 2019-02-28 17:29:26 -0800 | [diff] [blame] | 483 | final CountDownLatch latch1 = new CountDownLatch(1); |
| 484 | registerBroadcastReceiver(Intent.ACTION_USER_UNLOCKED, latch1, testUser); |
| 485 | mAm.switchUser(testUser); |
| 486 | attestTrue("Failed to achieve initial ACTION_USER_UNLOCKED for user " + testUser, |
| 487 | latch1.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS)); |
| 488 | |
| 489 | // Second, switch back to origUser, waiting merely for switchUser() to finish |
| 490 | switchUser(origUser); |
| 491 | attestTrue("Didn't switch back to user, " + origUser, origUser == mAm.getCurrentUser()); |
| 492 | |
| 493 | if (stopNewUser) { |
| 494 | stopUser(testUser, true); |
| 495 | attestFalse("Failed to stop user " + testUser, mAm.isUserRunning(testUser)); |
| 496 | } |
| 497 | |
| 498 | return testUser; |
| 499 | } |
| 500 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 501 | /** |
| 502 | * Installs the given package in the given user. |
| 503 | */ |
| 504 | private void installPreexistingApp(int userId, String packageName) throws RemoteException { |
| 505 | final CountDownLatch latch = new CountDownLatch(1); |
| 506 | |
| 507 | final IntentSender sender = new IntentSender((IIntentSender) new IIntentSender.Stub() { |
| 508 | @Override |
| 509 | public void send(int code, Intent intent, String resolvedType, IBinder whitelistToken, |
| 510 | IIntentReceiver finishedReceiver, String requiredPermission, Bundle options) { |
| 511 | latch.countDown(); |
| 512 | } |
| 513 | }); |
| 514 | |
| 515 | final IPackageInstaller installer = AppGlobals.getPackageManager().getPackageInstaller(); |
Philip P. Moltmann | bfcffa0 | 2019-05-13 17:10:46 -0700 | [diff] [blame] | 516 | installer.installExistingPackage(packageName, |
| 517 | PackageManager.INSTALL_ALL_WHITELIST_RESTRICTED_PERMISSIONS, |
| 518 | PackageManager.INSTALL_REASON_UNKNOWN, sender, userId, null); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 519 | |
| 520 | try { |
| 521 | latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
| 522 | } catch (InterruptedException e) { |
| 523 | Log.e(TAG, "Thread interrupted unexpectedly.", e); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Launches the given package in the given user. |
| 529 | * Make sure the keyguard has been dismissed prior to calling. |
| 530 | */ |
| 531 | private void startApp(int userId, String packageName) throws RemoteException { |
| 532 | final Context context = InstrumentationRegistry.getContext(); |
| 533 | final WaitResult result = ActivityTaskManager.getService().startActivityAndWait(null, null, |
| 534 | context.getPackageManager().getLaunchIntentForPackage(packageName), |
| 535 | null, null, null, 0, 0, null, null, |
| 536 | userId); |
| 537 | attestTrue("User " + userId + " failed to start " + packageName, |
| 538 | result.result == ActivityManager.START_SUCCESS); |
| 539 | } |
| 540 | |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 541 | private void registerUserSwitchObserver(final CountDownLatch switchLatch, |
| 542 | final CountDownLatch bootCompleteLatch, final int userId) throws Exception { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 543 | ActivityManager.getService().registerUserSwitchObserver( |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 544 | new UserSwitchObserver() { |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 545 | @Override |
| 546 | public void onUserSwitchComplete(int newUserId) throws RemoteException { |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 547 | if (switchLatch != null && userId == newUserId) { |
| 548 | switchLatch.countDown(); |
| 549 | } |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | @Override |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 553 | public void onLockedBootComplete(int newUserId) { |
| 554 | if (bootCompleteLatch != null && userId == newUserId) { |
| 555 | bootCompleteLatch.countDown(); |
| 556 | } |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 557 | } |
Sudheer Shanka | 2d0278b | 2017-07-12 18:12:31 -0700 | [diff] [blame] | 558 | }, TAG); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 561 | private void registerBroadcastReceiver(final String action, final CountDownLatch latch, |
| 562 | final int userId) { |
| 563 | InstrumentationRegistry.getContext().registerReceiverAsUser(new BroadcastReceiver() { |
| 564 | @Override |
| 565 | public void onReceive(Context context, Intent intent) { |
| 566 | if (action.equals(intent.getAction()) && intent.getIntExtra( |
| 567 | Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL) == userId) { |
| 568 | latch.countDown(); |
| 569 | } |
| 570 | } |
| 571 | }, UserHandle.of(userId), new IntentFilter(action), null, null); |
| 572 | } |
| 573 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 574 | private class ProgressWaiter extends IProgressListener.Stub { |
| 575 | private final CountDownLatch mFinishedLatch = new CountDownLatch(1); |
| 576 | |
| 577 | @Override |
| 578 | public void onStarted(int id, Bundle extras) {} |
| 579 | |
| 580 | @Override |
| 581 | public void onProgress(int id, int progress, Bundle extras) {} |
| 582 | |
| 583 | @Override |
| 584 | public void onFinished(int id, Bundle extras) { |
| 585 | mFinishedLatch.countDown(); |
| 586 | } |
| 587 | |
| 588 | public boolean waitForFinish(long timeoutSecs) { |
| 589 | try { |
| 590 | return mFinishedLatch.await(timeoutSecs, TimeUnit.SECONDS); |
| 591 | } catch (InterruptedException e) { |
| 592 | Log.e(TAG, "Thread interrupted unexpectedly.", e); |
| 593 | return false; |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 598 | private void removeUser(int userId) { |
| 599 | try { |
| 600 | mUm.removeUser(userId); |
| 601 | final long startTime = System.currentTimeMillis(); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 602 | final long timeoutInMs = TIMEOUT_IN_SECOND * 1000; |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 603 | while (mUm.getUserInfo(userId) != null && |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 604 | System.currentTimeMillis() - startTime < timeoutInMs) { |
| 605 | TimeUnit.MILLISECONDS.sleep(CHECK_USER_REMOVED_INTERVAL_MS); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 606 | } |
| 607 | } catch (InterruptedException e) { |
| 608 | Thread.currentThread().interrupt(); |
| 609 | } catch (Exception e) { |
| 610 | // Ignore |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 611 | } |
| 612 | if (mUm.getUserInfo(userId) != null) { |
| 613 | mUsersToRemove.add(userId); |
| 614 | } |
| 615 | } |
Bookatz | 52b12ac | 2019-02-28 17:29:26 -0800 | [diff] [blame] | 616 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 617 | private void attestTrue(String message, boolean assertion) { |
| 618 | if (!assertion) { |
Bookatz | 52b12ac | 2019-02-28 17:29:26 -0800 | [diff] [blame] | 619 | Log.w(TAG, message); |
| 620 | } |
| 621 | } |
| 622 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame] | 623 | private void attestFalse(String message, boolean assertion) { |
| 624 | attestTrue(message, !assertion); |
Bookatz | 52b12ac | 2019-02-28 17:29:26 -0800 | [diff] [blame] | 625 | } |
John Reck | 62e5fea | 2016-10-12 15:27:52 -0700 | [diff] [blame] | 626 | } |