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 | |
| 329 | /** Tests installing a pre-existing app in a newly-created profile. */ |
| 330 | @Test |
| 331 | public void managedProfileInstall() throws Exception { |
| 332 | while (mRunner.keepRunning()) { |
| 333 | mRunner.pauseTiming(); |
| 334 | final int userId = createManagedProfile(); |
| 335 | mRunner.resumeTiming(); |
| 336 | |
| 337 | installPreexistingApp(userId, DUMMY_PACKAGE_NAME); |
| 338 | |
| 339 | mRunner.pauseTiming(); |
| 340 | removeUser(userId); |
| 341 | mRunner.resumeTiming(); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Tests creating a new profile, starting (unlocking) it, installing an app, |
| 347 | * and launching that app in it. |
| 348 | */ |
| 349 | @Test |
| 350 | public void managedProfileCreateUnlockInstallAndLaunchApp() throws Exception { |
| 351 | final String packageName = "perftests.multiuser.apps.dummyapp"; |
| 352 | while (mRunner.keepRunning()) { |
| 353 | mRunner.pauseTiming(); |
| 354 | WindowManagerGlobal.getWindowManagerService().dismissKeyguard(null, null); |
| 355 | mRunner.resumeTiming(); |
| 356 | |
| 357 | final int userId = createManagedProfile(); |
| 358 | startUserInBackground(userId); |
| 359 | installPreexistingApp(userId, DUMMY_PACKAGE_NAME); |
| 360 | startApp(userId, DUMMY_PACKAGE_NAME); |
| 361 | |
| 362 | mRunner.pauseTiming(); |
| 363 | removeUser(userId); |
| 364 | mRunner.resumeTiming(); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | /** Tests stopping a profile. */ |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 369 | @Test |
Sudheer Shanka | a319f8b | 2017-07-11 15:28:48 -0700 | [diff] [blame] | 370 | public void managedProfileStopped() throws Exception { |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 371 | while (mRunner.keepRunning()) { |
| 372 | mRunner.pauseTiming(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame^] | 373 | final int userId = createManagedProfile(); |
| 374 | startUserInBackground(userId); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 375 | mRunner.resumeTiming(); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 376 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame^] | 377 | stopUser(userId, true); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 378 | |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 379 | mRunner.pauseTiming(); |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame^] | 380 | removeUser(userId); |
Sudheer Shanka | 5f76e1f | 2017-06-25 19:40:53 -0700 | [diff] [blame] | 381 | mRunner.resumeTiming(); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame^] | 385 | /** Creates a new user, returning its userId. */ |
| 386 | private int createUser() { |
| 387 | return createUser(0); |
| 388 | } |
| 389 | |
| 390 | /** Creates a new user with the given flags, returning its userId. */ |
| 391 | private int createUser(int flags) { |
| 392 | int userId = mUm.createUser("TestUser", flags).id; |
| 393 | mUsersToRemove.add(userId); |
| 394 | return userId; |
| 395 | } |
| 396 | |
| 397 | /** Creates a managed (work) profile under the current user, returning its userId. */ |
| 398 | private int createManagedProfile() { |
| 399 | final UserInfo userInfo = mUm.createProfileForUser("TestProfile", |
| 400 | UserInfo.FLAG_MANAGED_PROFILE, mAm.getCurrentUser()); |
| 401 | mUsersToRemove.add(userInfo.id); |
| 402 | return userInfo.id; |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Start user in background and wait for it to unlock (equivalent to ACTION_USER_UNLOCKED). |
| 407 | * To start in foreground instead, see {@link #switchUser(int)}. |
| 408 | * This should always be used for profiles since profiles cannot be started in foreground. |
| 409 | */ |
| 410 | private void startUserInBackground(int userId) { |
| 411 | final ProgressWaiter waiter = new ProgressWaiter(); |
| 412 | try { |
| 413 | mIam.startUserInBackgroundWithListener(userId, waiter); |
| 414 | boolean success = waiter.waitForFinish(TIMEOUT_IN_SECOND); |
| 415 | attestTrue("Failed to start user " + userId + " in background.", success); |
| 416 | } catch (RemoteException e) { |
| 417 | Log.e(TAG, "startUserInBackground failed", e); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /** Starts the given user in the foreground. */ |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 422 | private void switchUser(int userId) throws Exception { |
| 423 | final CountDownLatch latch = new CountDownLatch(1); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 424 | registerUserSwitchObserver(latch, null, userId); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 425 | mAm.switchUser(userId); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 426 | latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 429 | private void stopUser(int userId, boolean force) throws Exception { |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 430 | final CountDownLatch latch = new CountDownLatch(1); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 431 | mIam.stopUser(userId, force /* force */, new IStopUserCallback.Stub() { |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 432 | @Override |
| 433 | public void userStopped(int userId) throws RemoteException { |
| 434 | latch.countDown(); |
| 435 | } |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 436 | |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 437 | @Override |
| 438 | public void userStopAborted(int userId) throws RemoteException { |
| 439 | } |
| 440 | }); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 441 | latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Bookatz | 52b12ac | 2019-02-28 17:29:26 -0800 | [diff] [blame] | 444 | /** |
| 445 | * Creates a user and waits for its ACTION_USER_UNLOCKED. |
| 446 | * Then switches to back to the original user and waits for its switchUser() to finish. |
| 447 | * |
| 448 | * @param stopNewUser whether to stop the new user after switching to otherUser. |
| 449 | * @return userId of the newly created user. |
| 450 | */ |
| 451 | private int initializeNewUserAndSwitchBack(boolean stopNewUser) throws Exception { |
| 452 | final int origUser = mAm.getCurrentUser(); |
| 453 | // First, create and switch to testUser, waiting for its ACTION_USER_UNLOCKED |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame^] | 454 | final int testUser = createUser(); |
Bookatz | 52b12ac | 2019-02-28 17:29:26 -0800 | [diff] [blame] | 455 | final CountDownLatch latch1 = new CountDownLatch(1); |
| 456 | registerBroadcastReceiver(Intent.ACTION_USER_UNLOCKED, latch1, testUser); |
| 457 | mAm.switchUser(testUser); |
| 458 | attestTrue("Failed to achieve initial ACTION_USER_UNLOCKED for user " + testUser, |
| 459 | latch1.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS)); |
| 460 | |
| 461 | // Second, switch back to origUser, waiting merely for switchUser() to finish |
| 462 | switchUser(origUser); |
| 463 | attestTrue("Didn't switch back to user, " + origUser, origUser == mAm.getCurrentUser()); |
| 464 | |
| 465 | if (stopNewUser) { |
| 466 | stopUser(testUser, true); |
| 467 | attestFalse("Failed to stop user " + testUser, mAm.isUserRunning(testUser)); |
| 468 | } |
| 469 | |
| 470 | return testUser; |
| 471 | } |
| 472 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame^] | 473 | /** |
| 474 | * Installs the given package in the given user. |
| 475 | */ |
| 476 | private void installPreexistingApp(int userId, String packageName) throws RemoteException { |
| 477 | final CountDownLatch latch = new CountDownLatch(1); |
| 478 | |
| 479 | final IntentSender sender = new IntentSender((IIntentSender) new IIntentSender.Stub() { |
| 480 | @Override |
| 481 | public void send(int code, Intent intent, String resolvedType, IBinder whitelistToken, |
| 482 | IIntentReceiver finishedReceiver, String requiredPermission, Bundle options) { |
| 483 | latch.countDown(); |
| 484 | } |
| 485 | }); |
| 486 | |
| 487 | final IPackageInstaller installer = AppGlobals.getPackageManager().getPackageInstaller(); |
| 488 | installer.installExistingPackage(packageName, 0, PackageManager.INSTALL_REASON_UNKNOWN, |
| 489 | sender, userId); |
| 490 | |
| 491 | try { |
| 492 | latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS); |
| 493 | } catch (InterruptedException e) { |
| 494 | Log.e(TAG, "Thread interrupted unexpectedly.", e); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * Launches the given package in the given user. |
| 500 | * Make sure the keyguard has been dismissed prior to calling. |
| 501 | */ |
| 502 | private void startApp(int userId, String packageName) throws RemoteException { |
| 503 | final Context context = InstrumentationRegistry.getContext(); |
| 504 | final WaitResult result = ActivityTaskManager.getService().startActivityAndWait(null, null, |
| 505 | context.getPackageManager().getLaunchIntentForPackage(packageName), |
| 506 | null, null, null, 0, 0, null, null, |
| 507 | userId); |
| 508 | attestTrue("User " + userId + " failed to start " + packageName, |
| 509 | result.result == ActivityManager.START_SUCCESS); |
| 510 | } |
| 511 | |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 512 | private void registerUserSwitchObserver(final CountDownLatch switchLatch, |
| 513 | final CountDownLatch bootCompleteLatch, final int userId) throws Exception { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 514 | ActivityManager.getService().registerUserSwitchObserver( |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 515 | new UserSwitchObserver() { |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 516 | @Override |
| 517 | public void onUserSwitchComplete(int newUserId) throws RemoteException { |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 518 | if (switchLatch != null && userId == newUserId) { |
| 519 | switchLatch.countDown(); |
| 520 | } |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | @Override |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 524 | public void onLockedBootComplete(int newUserId) { |
| 525 | if (bootCompleteLatch != null && userId == newUserId) { |
| 526 | bootCompleteLatch.countDown(); |
| 527 | } |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 528 | } |
Sudheer Shanka | 2d0278b | 2017-07-12 18:12:31 -0700 | [diff] [blame] | 529 | }, TAG); |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 532 | private void registerBroadcastReceiver(final String action, final CountDownLatch latch, |
| 533 | final int userId) { |
| 534 | InstrumentationRegistry.getContext().registerReceiverAsUser(new BroadcastReceiver() { |
| 535 | @Override |
| 536 | public void onReceive(Context context, Intent intent) { |
| 537 | if (action.equals(intent.getAction()) && intent.getIntExtra( |
| 538 | Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL) == userId) { |
| 539 | latch.countDown(); |
| 540 | } |
| 541 | } |
| 542 | }, UserHandle.of(userId), new IntentFilter(action), null, null); |
| 543 | } |
| 544 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame^] | 545 | private class ProgressWaiter extends IProgressListener.Stub { |
| 546 | private final CountDownLatch mFinishedLatch = new CountDownLatch(1); |
| 547 | |
| 548 | @Override |
| 549 | public void onStarted(int id, Bundle extras) {} |
| 550 | |
| 551 | @Override |
| 552 | public void onProgress(int id, int progress, Bundle extras) {} |
| 553 | |
| 554 | @Override |
| 555 | public void onFinished(int id, Bundle extras) { |
| 556 | mFinishedLatch.countDown(); |
| 557 | } |
| 558 | |
| 559 | public boolean waitForFinish(long timeoutSecs) { |
| 560 | try { |
| 561 | return mFinishedLatch.await(timeoutSecs, TimeUnit.SECONDS); |
| 562 | } catch (InterruptedException e) { |
| 563 | Log.e(TAG, "Thread interrupted unexpectedly.", e); |
| 564 | return false; |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 569 | private void removeUser(int userId) { |
| 570 | try { |
| 571 | mUm.removeUser(userId); |
| 572 | final long startTime = System.currentTimeMillis(); |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 573 | final long timeoutInMs = TIMEOUT_IN_SECOND * 1000; |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 574 | while (mUm.getUserInfo(userId) != null && |
Tony Mak | ec7c696 | 2017-05-26 15:38:09 +0100 | [diff] [blame] | 575 | System.currentTimeMillis() - startTime < timeoutInMs) { |
| 576 | TimeUnit.MILLISECONDS.sleep(CHECK_USER_REMOVED_INTERVAL_MS); |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 577 | } |
| 578 | } catch (InterruptedException e) { |
| 579 | Thread.currentThread().interrupt(); |
| 580 | } catch (Exception e) { |
| 581 | // Ignore |
Sudheer Shanka | 53c23fd | 2016-08-17 19:34:58 -0700 | [diff] [blame] | 582 | } |
| 583 | if (mUm.getUserInfo(userId) != null) { |
| 584 | mUsersToRemove.add(userId); |
| 585 | } |
| 586 | } |
Bookatz | 52b12ac | 2019-02-28 17:29:26 -0800 | [diff] [blame] | 587 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame^] | 588 | private void attestTrue(String message, boolean assertion) { |
| 589 | if (!assertion) { |
Bookatz | 52b12ac | 2019-02-28 17:29:26 -0800 | [diff] [blame] | 590 | Log.w(TAG, message); |
| 591 | } |
| 592 | } |
| 593 | |
Bookatz | 312da05 | 2019-04-11 09:24:54 -0700 | [diff] [blame^] | 594 | private void attestFalse(String message, boolean assertion) { |
| 595 | attestTrue(message, !assertion); |
Bookatz | 52b12ac | 2019-02-28 17:29:26 -0800 | [diff] [blame] | 596 | } |
John Reck | 62e5fea | 2016-10-12 15:27:52 -0700 | [diff] [blame] | 597 | } |