summaryrefslogtreecommitdiff
path: root/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/CreateAndManageUserTest.java
blob: 0280bdfa6a719845c524c8487a9b795dcf355e2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.cts.deviceowner;

import android.app.ActivityManager;
import android.app.Service;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.os.PersistableBundle;
import android.os.Process;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Semaphore;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * Test {@link DevicePolicyManager#createAndManageUser}.
 */
public class CreateAndManageUserTest extends BaseDeviceOwnerTest {
    private static final String TAG = "CreateAndManageUserTest";

    private static final String BROADCAST_EXTRA = "broadcastExtra";
    private static final String ACTION_EXTRA = "actionExtra";
    private static final String SERIAL_EXTRA = "serialExtra";
    private static final String PROFILE_OWNER_EXTRA = "profileOwnerExtra";
    private static final String SETUP_COMPLETE_EXTRA = "setupCompleteExtra";
    private static final int BROADCAST_TIMEOUT = 15_000;
    private static final int USER_SWITCH_DELAY = 10_000;

    private static final String AFFILIATION_ID = "affiliation.id";
    private static final String EXTRA_AFFILIATION_ID = "affiliationIdExtra";
    private static final String EXTRA_METHOD_NAME = "methodName";
    private static final long ON_ENABLED_TIMEOUT_SECONDS = 120;


    private PackageManager mPackageManager;
    private ActivityManager mActivityManager;
    private volatile boolean mReceived;
    private volatile boolean mTestProfileOwnerWasUsed;
    private volatile boolean mSetupComplete;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mPackageManager = mContext.getPackageManager();
        mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
    }

    @Override
    protected void tearDown() throws Exception {
        mDevicePolicyManager.clearUserRestriction(getWho(), UserManager.DISALLOW_ADD_USER);
        mDevicePolicyManager.clearUserRestriction(getWho(), UserManager.DISALLOW_REMOVE_USER);
        super.tearDown();
    }

    // This class is used by createAndManageUserTest as profile owner for the new user. When
    // enabled, it sends a broadcast to signal success.
    public static class TestProfileOwner extends DeviceAdminReceiver {
        @Override
        public void onEnabled(Context context, Intent intent) {
            if (intent.getBooleanExtra(BROADCAST_EXTRA, false)) {
                Intent i = new Intent(intent.getStringExtra(ACTION_EXTRA));
                UserManager userManager = (UserManager)
                        context.getSystemService(Context.USER_SERVICE);
                long serial = intent.getLongExtra(SERIAL_EXTRA, 0);
                UserHandle handle = userManager.getUserForSerialNumber(serial);
                i.putExtra(PROFILE_OWNER_EXTRA, true);
                // find value of user_setup_complete on new user, and send the result back
                try {
                    boolean setupComplete = (Settings.Secure.getInt(context.getContentResolver(),
                            "user_setup_complete") == 1);
                    i.putExtra(SETUP_COMPLETE_EXTRA, setupComplete);
                } catch (Settings.SettingNotFoundException e) {
                    fail("Did not find settings user_setup_complete");
                }

                context.sendBroadcastAsUser(i, handle);
            }
        }

        public static ComponentName getComponentName() {
            return new ComponentName(CreateAndManageUserTest.class.getPackage().getName(),
                    TestProfileOwner.class.getName());
        }
    }

    private void waitForBroadcastLocked() {
        // Wait for broadcast. Time is measured in a while loop because of spurious wakeups.
        final long initTime = System.currentTimeMillis();
        while (!mReceived) {
            try {
                wait(BROADCAST_TIMEOUT - (System.currentTimeMillis() - initTime));
            } catch (InterruptedException e) {
                fail("InterruptedException: " + e.getMessage());
            }
            if (!mReceived && System.currentTimeMillis() - initTime > BROADCAST_TIMEOUT) {
                fail("Timeout while waiting for broadcast after createAndManageUser.");
            }
        }
    }

    // This test will create a user that will get passed a bundle that we specify. The bundle will
    // contain an action and a serial (for user handle) to broadcast to notify the test that the
    // configuration was triggered.
    private void createAndManageUserTest(final int flags) {
        // This test sets a profile owner on the user, which requires the managed_users feature.
        if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS)) {
            return;
        }

        final boolean expectedSetupComplete = (flags & DevicePolicyManager.SKIP_SETUP_WIZARD) != 0;
        UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);

        UserHandle firstUser = Process.myUserHandle();
        final String testUserName = "TestUser_" + System.currentTimeMillis();
        String action = "com.android.cts.TEST_USER_ACTION";
        PersistableBundle bundle = new PersistableBundle();
        bundle.putBoolean(BROADCAST_EXTRA, true);
        bundle.putLong(SERIAL_EXTRA, userManager.getSerialNumberForUser(firstUser));
        bundle.putString(ACTION_EXTRA, action);

        mReceived = false;
        mTestProfileOwnerWasUsed = false;
        mSetupComplete = !expectedSetupComplete;
        BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                mReceived = true;
                if (intent.getBooleanExtra(PROFILE_OWNER_EXTRA, false)) {
                    mTestProfileOwnerWasUsed = true;
                }
                mSetupComplete = intent.getBooleanExtra(SETUP_COMPLETE_EXTRA,
                        !expectedSetupComplete);
                synchronized (CreateAndManageUserTest.this) {
                    CreateAndManageUserTest.this.notify();
                }
            }
        };

        IntentFilter filter = new IntentFilter();
        filter.addAction(action);
        mContext.registerReceiver(receiver, filter);

        synchronized (this) {
            UserHandle userHandle = mDevicePolicyManager.createAndManageUser(getWho(), testUserName,
                    TestProfileOwner.getComponentName(), bundle, flags);
            assertNotNull(userHandle);

            mDevicePolicyManager.switchUser(getWho(), userHandle);
            try {
                wait(USER_SWITCH_DELAY);
            } catch (InterruptedException e) {
                fail("InterruptedException: " + e.getMessage());
            }
            mDevicePolicyManager.switchUser(getWho(), firstUser);

            waitForBroadcastLocked();

            assertTrue(mReceived);
            assertTrue(mTestProfileOwnerWasUsed);
            assertEquals(expectedSetupComplete, mSetupComplete);

            assertTrue(mDevicePolicyManager.removeUser(getWho(), userHandle));

            userHandle = null;
        }

        mContext.unregisterReceiver(receiver);
    }

    public void testCreateAndManageUser() throws Exception {
        String testUserName = "TestUser_" + System.currentTimeMillis();

        UserHandle userHandle = mDevicePolicyManager.createAndManageUser(
                getWho(),
                testUserName,
                getWho(),
                null,
                /* flags */ 0);
        Log.d(TAG, "User create: " + userHandle);
    }

    public void testCreateAndManageUser_LowStorage() throws Exception {
        String testUserName = "TestUser_" + System.currentTimeMillis();

        try {
            mDevicePolicyManager.createAndManageUser(
                    getWho(),
                    testUserName,
                    getWho(),
                    null,
                /* flags */ 0);
            fail("createAndManageUser should throw UserOperationException");
        } catch (UserManager.UserOperationException e) {
            assertEquals(UserManager.USER_OPERATION_ERROR_LOW_STORAGE, e.getUserOperationResult());
        }
    }

    public void testCreateAndManageUser_MaxUsers() throws Exception {
        String testUserName = "TestUser_" + System.currentTimeMillis();

        try {
            mDevicePolicyManager.createAndManageUser(
                    getWho(),
                    testUserName,
                    getWho(),
                    null,
                /* flags */ 0);
            fail("createAndManageUser should throw UserOperationException");
        } catch (UserManager.UserOperationException e) {
            assertEquals(UserManager.USER_OPERATION_ERROR_MAX_USERS, e.getUserOperationResult());
        }
    }

    public void testCreateAndManageUser_GetSecondaryUsers() throws Exception {
        String testUserName = "TestUser_" + System.currentTimeMillis();

        UserHandle userHandle = mDevicePolicyManager.createAndManageUser(
                getWho(),
                testUserName,
                getWho(),
                null,
                /* flags */ 0);
        Log.d(TAG, "User create: " + userHandle);

        List<UserHandle> secondaryUsers = mDevicePolicyManager.getSecondaryUsers(getWho());
        assertEquals(1, secondaryUsers.size());
        assertEquals(userHandle, secondaryUsers.get(0));
    }

    public void testCreateAndManageUser_SwitchUser() throws Exception {
        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(
                getContext());

        String testUserName = "TestUser_" + System.currentTimeMillis();

        UserHandle userHandle = mDevicePolicyManager.createAndManageUser(
                getWho(),
                testUserName,
                getWho(),
                null,
                /* flags */ 0);
        Log.d(TAG, "User create: " + userHandle);

        LocalBroadcastReceiver broadcastReceiver = new LocalBroadcastReceiver();
        localBroadcastManager.registerReceiver(broadcastReceiver,
                new IntentFilter(BasicAdminReceiver.ACTION_USER_SWITCHED));
        try {
            assertTrue(mDevicePolicyManager.switchUser(getWho(), userHandle));
            assertEquals(userHandle, broadcastReceiver.waitForBroadcastReceived());
        } finally {
            localBroadcastManager.unregisterReceiver(broadcastReceiver);
        }
    }

    public void testCreateAndManageUser_CannotStopCurrentUser() throws Exception {
        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(
                getContext());

        String testUserName = "TestUser_" + System.currentTimeMillis();

        UserHandle userHandle = mDevicePolicyManager.createAndManageUser(
                getWho(),
                testUserName,
                getWho(),
                null,
                /* flags */ 0);
        Log.d(TAG, "User create: " + userHandle);

        LocalBroadcastReceiver broadcastReceiver = new LocalBroadcastReceiver();
        localBroadcastManager.registerReceiver(broadcastReceiver,
                new IntentFilter(BasicAdminReceiver.ACTION_USER_SWITCHED));
        try {
            assertTrue(mDevicePolicyManager.switchUser(getWho(), userHandle));
            assertEquals(userHandle, broadcastReceiver.waitForBroadcastReceived());
            assertEquals(UserManager.USER_OPERATION_ERROR_CURRENT_USER,
                    mDevicePolicyManager.stopUser(getWho(), userHandle));
        } finally {
            localBroadcastManager.unregisterReceiver(broadcastReceiver);
        }
    }

    public void testCreateAndManageUser_StartInBackground() throws Exception {
        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(
                getContext());

        String testUserName = "TestUser_" + System.currentTimeMillis();

        UserHandle userHandle = mDevicePolicyManager.createAndManageUser(
                getWho(),
                testUserName,
                getWho(),
                null,
                /* flags */ 0);
        Log.d(TAG, "User create: " + userHandle);

        LocalBroadcastReceiver broadcastReceiver = new LocalBroadcastReceiver();
        localBroadcastManager.registerReceiver(broadcastReceiver,
                new IntentFilter(BasicAdminReceiver.ACTION_USER_STARTED));

        try {
            // Start user in background and wait for onUserStarted
            assertEquals(UserManager.USER_OPERATION_SUCCESS,
                    mDevicePolicyManager.startUserInBackground(getWho(), userHandle));
            assertEquals(userHandle, broadcastReceiver.waitForBroadcastReceived());
        } finally {
            localBroadcastManager.unregisterReceiver(broadcastReceiver);
        }
    }

    public void testCreateAndManageUser_StartInBackground_MaxRunningUsers() throws Exception {
        String testUserName = "TestUser_" + System.currentTimeMillis();

        UserHandle userHandle = mDevicePolicyManager.createAndManageUser(
                getWho(),
                testUserName,
                getWho(),
                null,
                /* flags */ 0);
        Log.d(TAG, "User create: " + userHandle);

        // Start user in background and should receive max running users error
        assertEquals(UserManager.USER_OPERATION_ERROR_MAX_RUNNING_USERS,
                mDevicePolicyManager.startUserInBackground(getWho(), userHandle));
    }

    public void testCreateAndManageUser_StopUser() throws Exception {
        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(
                getContext());

        String testUserName = "TestUser_" + System.currentTimeMillis();

        UserHandle userHandle = mDevicePolicyManager.createAndManageUser(
                getWho(),
                testUserName,
                getWho(),
                null,
                /* flags */ 0);
        Log.d(TAG, "User create: " + userHandle);
        assertEquals(UserManager.USER_OPERATION_SUCCESS,
                mDevicePolicyManager.startUserInBackground(getWho(), userHandle));

        LocalBroadcastReceiver broadcastReceiver = new LocalBroadcastReceiver();
        localBroadcastManager.registerReceiver(broadcastReceiver,
                new IntentFilter(BasicAdminReceiver.ACTION_USER_STOPPED));

        Thread.sleep(USER_SWITCH_DELAY);

        try {
            assertEquals(UserManager.USER_OPERATION_SUCCESS,
                    mDevicePolicyManager.stopUser(getWho(), userHandle));
            assertEquals(userHandle, broadcastReceiver.waitForBroadcastReceived());
        } finally {
            localBroadcastManager.unregisterReceiver(broadcastReceiver);
        }
    }

    public void testCreateAndManageUser_StopEphemeralUser_DisallowRemoveUser() throws Exception {
        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(
                getContext());

        String testUserName = "TestUser_" + System.currentTimeMillis();

        // Set DISALLOW_REMOVE_USER restriction
        mDevicePolicyManager.addUserRestriction(getWho(), UserManager.DISALLOW_REMOVE_USER);

        UserHandle userHandle = mDevicePolicyManager.createAndManageUser(
                getWho(),
                testUserName,
                getWho(),
                null,
                DevicePolicyManager.MAKE_USER_EPHEMERAL);
        Log.d(TAG, "User create: " + userHandle);
        assertEquals(UserManager.USER_OPERATION_SUCCESS,
                mDevicePolicyManager.startUserInBackground(getWho(), userHandle));

        LocalBroadcastReceiver broadcastReceiver = new LocalBroadcastReceiver();
        localBroadcastManager.registerReceiver(broadcastReceiver,
                new IntentFilter(BasicAdminReceiver.ACTION_USER_STARTED));
        broadcastReceiver.waitForBroadcastReceived();
        localBroadcastManager.unregisterReceiver(broadcastReceiver);

        // Register broadcast receiver for the remove action.
        localBroadcastManager.registerReceiver(broadcastReceiver,
                new IntentFilter(BasicAdminReceiver.ACTION_USER_REMOVED));
        try {
            assertEquals(UserManager.USER_OPERATION_SUCCESS,
                    mDevicePolicyManager.stopUser(getWho(), userHandle));
            assertEquals(userHandle, broadcastReceiver.waitForBroadcastReceived());
        } finally {
            localBroadcastManager.unregisterReceiver(broadcastReceiver);
            // Clear DISALLOW_REMOVE_USER restriction
            mDevicePolicyManager.clearUserRestriction(getWho(), UserManager.DISALLOW_REMOVE_USER);
        }
    }

    @SuppressWarnings("unused")
    private static void logoutUser(Context context, DevicePolicyManager devicePolicyManager,
            ComponentName componentName) {
        assertEquals("cannot logout user", UserManager.USER_OPERATION_SUCCESS,
                devicePolicyManager.logoutUser(componentName));
    }

    public void testCreateAndManageUser_LogoutUser() throws Exception {
        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(
                getContext());

        LocalBroadcastReceiver broadcastReceiver = new LocalBroadcastReceiver();
        localBroadcastManager.registerReceiver(broadcastReceiver,
                new IntentFilter(BasicAdminReceiver.ACTION_USER_STOPPED));

        try {
            UserHandle userHandle = runCrossUserVerification(
                    /* createAndManageUserFlags */ 0, "logoutUser");
            assertEquals(userHandle, broadcastReceiver.waitForBroadcastReceived());
        } finally {
            localBroadcastManager.unregisterReceiver(broadcastReceiver);
        }
    }

    @SuppressWarnings("unused")
    private static void assertAffiliatedUser(Context context,
            DevicePolicyManager devicePolicyManager, ComponentName componentName) {
        assertTrue("not affiliated user", devicePolicyManager.isAffiliatedUser());
    }

    public void testCreateAndManageUser_Affiliated() throws Exception {
        runCrossUserVerification(/* createAndManageUserFlags */ 0, "assertAffiliatedUser");
        PrimaryUserService.assertCrossUserCallArrived();
    }

    @SuppressWarnings("unused")
    private static void assertEphemeralUser(Context context,
            DevicePolicyManager devicePolicyManager, ComponentName componentName) {
        assertTrue("not ephemeral user", devicePolicyManager.isEphemeralUser(componentName));
    }

    public void testCreateAndManageUser_Ephemeral() throws Exception {
        runCrossUserVerification(DevicePolicyManager.MAKE_USER_EPHEMERAL, "assertEphemeralUser");
        PrimaryUserService.assertCrossUserCallArrived();
    }

    @SuppressWarnings("unused")
    private static void assertAllSystemAppsInstalled(Context context,
            DevicePolicyManager devicePolicyManager, ComponentName componentName) {
        PackageManager packageManager = context.getPackageManager();
        // First get a set of installed package names
        Set<String> installedPackageNames = packageManager
                .getInstalledApplications(/* flags */ 0)
                .stream()
                .map(applicationInfo -> applicationInfo.packageName)
                .collect(Collectors.toSet());
        // Then filter all package names by those that are not installed
        Set<String> uninstalledPackageNames = packageManager
                .getInstalledApplications(PackageManager.MATCH_UNINSTALLED_PACKAGES)
                .stream()
                .map(applicationInfo -> applicationInfo.packageName)
                .filter(((Predicate<String>) installedPackageNames::contains).negate())
                .collect(Collectors.toSet());
        // Assert that all apps are installed
        assertTrue("system apps not installed: " + uninstalledPackageNames,
                uninstalledPackageNames.isEmpty());
    }

    public void testCreateAndManageUser_LeaveAllSystemApps() throws Exception {
        runCrossUserVerification(
                DevicePolicyManager.LEAVE_ALL_SYSTEM_APPS_ENABLED, "assertAllSystemAppsInstalled");
        PrimaryUserService.assertCrossUserCallArrived();
    }

    private UserHandle runCrossUserVerification(int createAndManageUserFlags, String methodName)
            throws Exception {
        String testUserName = "TestUser_" + System.currentTimeMillis();

        // Set affiliation id to allow communication.
        mDevicePolicyManager.setAffiliationIds(getWho(), Collections.singleton(AFFILIATION_ID));

        // Pack the affiliation id in a bundle so the secondary user can get it.
        PersistableBundle bundle = new PersistableBundle();
        bundle.putString(EXTRA_AFFILIATION_ID, AFFILIATION_ID);
        bundle.putString(EXTRA_METHOD_NAME, methodName);

        UserHandle userHandle = mDevicePolicyManager.createAndManageUser(
                getWho(),
                testUserName,
                SecondaryUserAdminReceiver.getComponentName(getContext()),
                bundle,
                createAndManageUserFlags);
        Log.d(TAG, "User create: " + userHandle);
        assertEquals(UserManager.USER_OPERATION_SUCCESS,
                mDevicePolicyManager.startUserInBackground(getWho(), userHandle));

        return userHandle;
    }

    public void testCreateAndManageUser_SkipSetupWizard() {
        createAndManageUserTest(DevicePolicyManager.SKIP_SETUP_WIZARD);
    }

    public void testCreateAndManageUser_DontSkipSetupWizard() {
        if (!mActivityManager.isRunningInTestHarness()) {
            // In test harness, the setup wizard will be disabled by default, so this test is always
            // failing.
            createAndManageUserTest(0);
        }
    }

    // createAndManageUser should circumvent the DISALLOW_ADD_USER restriction
    public void testCreateAndManageUser_AddRestrictionSet() {
        mDevicePolicyManager.addUserRestriction(getWho(), UserManager.DISALLOW_ADD_USER);

        UserHandle userHandle = mDevicePolicyManager.createAndManageUser(getWho(), "Test User",
                getWho(), null, 0);
        assertNotNull(userHandle);
    }

    public void testCreateAndManageUser_RemoveRestrictionSet() {
        mDevicePolicyManager.addUserRestriction(getWho(), UserManager.DISALLOW_REMOVE_USER);

        UserHandle userHandle = mDevicePolicyManager.createAndManageUser(getWho(), "Test User",
                getWho(), null, 0);
        assertNotNull(userHandle);

        boolean removed = mDevicePolicyManager.removeUser(getWho(), userHandle);
        // When the device owner itself has set the user restriction, it should still be allowed
        // to remove a user.
        assertTrue(removed);
    }

    public void testUserAddedOrRemovedBroadcasts() throws InterruptedException {
        LocalBroadcastReceiver receiver = new LocalBroadcastReceiver();
        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(
                getContext());
        localBroadcastManager.registerReceiver(receiver,
                new IntentFilter(BasicAdminReceiver.ACTION_USER_ADDED));
        UserHandle userHandle;
        try {
            userHandle = mDevicePolicyManager.createAndManageUser(getWho(), "Test User", getWho(),
                    null, 0);
            assertNotNull(userHandle);
            assertEquals(userHandle, receiver.waitForBroadcastReceived());
        } finally {
            localBroadcastManager.unregisterReceiver(receiver);
        }
        localBroadcastManager.registerReceiver(receiver,
                new IntentFilter(BasicAdminReceiver.ACTION_USER_REMOVED));
        try {
            assertTrue(mDevicePolicyManager.removeUser(getWho(), userHandle));
            assertEquals(userHandle, receiver.waitForBroadcastReceived());
        } finally {
            localBroadcastManager.unregisterReceiver(receiver);
        }
    }

    static class LocalBroadcastReceiver extends BroadcastReceiver {
        private LinkedBlockingQueue<UserHandle> mQueue = new LinkedBlockingQueue<UserHandle>(1);

        @Override
        public void onReceive(Context context, Intent intent) {
            UserHandle userHandle = intent.getParcelableExtra(BasicAdminReceiver.EXTRA_USER_HANDLE);
            Log.d(TAG, "broadcast receiver received " + intent + " with userHandle "
                    + userHandle);
            mQueue.offer(userHandle);

        }

        public UserHandle waitForBroadcastReceived() throws InterruptedException {
            return mQueue.poll(BROADCAST_TIMEOUT, TimeUnit.MILLISECONDS);
        }
    }

    public static final class PrimaryUserService extends Service {
        private static final Semaphore sSemaphore = new Semaphore(0);
        private static String sError = null;

        private final ICrossUserService.Stub mBinder = new ICrossUserService.Stub() {
            public void onEnabledCalled(String error) {
                Log.d(TAG, "onEnabledCalled on primary user");
                sError = error;
                sSemaphore.release();
            }
        };

        @Override
        public IBinder onBind(Intent intent) {
            return mBinder;
        }

        static void assertCrossUserCallArrived() throws Exception {
            assertTrue(sSemaphore.tryAcquire(ON_ENABLED_TIMEOUT_SECONDS, TimeUnit.SECONDS));
            if (sError != null) {
                throw new Exception(sError);
            }
        }
    }

    public static final class SecondaryUserAdminReceiver extends DeviceAdminReceiver {
        @Override
        public void onEnabled(Context context, Intent intent) {
            Log.d(TAG, "onEnabled called");
            DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
            ComponentName who = getComponentName(context);

            // Set affiliation ids
            dpm.setAffiliationIds(
                    who, Collections.singleton(intent.getStringExtra(EXTRA_AFFILIATION_ID)));

            String error = null;
            try {
                Method method = CreateAndManageUserTest.class.getDeclaredMethod(
                        intent.getStringExtra(EXTRA_METHOD_NAME), Context.class,
                        DevicePolicyManager.class, ComponentName.class);
                method.setAccessible(true);
                method.invoke(null, context, dpm, who);
            } catch (NoSuchMethodException | IllegalAccessException e) {
                error = e.toString();
            } catch (InvocationTargetException e) {
                error = e.getCause().toString();
            }

            // Call all affiliated users
            final List<UserHandle> targetUsers = dpm.getBindDeviceAdminTargetUsers(who);
            assertEquals(1, targetUsers.size());
            pingTargetUser(context, dpm, targetUsers.get(0), error);
        }

        private void pingTargetUser(Context context, DevicePolicyManager dpm, UserHandle target,
                String error) {
            Log.d(TAG, "Pinging target: " + target);
            final ServiceConnection serviceConnection = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    Log.d(TAG, "onServiceConnected is called in " + Thread.currentThread().getName());
                    ICrossUserService crossUserService = ICrossUserService
                            .Stub.asInterface(service);
                    try {
                        crossUserService.onEnabledCalled(error);
                    } catch (RemoteException re) {
                        Log.e(TAG, "Error when calling primary user", re);
                        // Do nothing, primary user will time out
                    }
                }

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    Log.d(TAG, "onServiceDisconnected is called");
                }
            };
            final Intent serviceIntent = new Intent(context, PrimaryUserService.class);
            assertTrue(dpm.bindDeviceAdminServiceAsUser(
                    getComponentName(context),
                    serviceIntent,
                    serviceConnection,
                    Context.BIND_AUTO_CREATE,
                    target));
        }

        public static ComponentName getComponentName(Context context) {
            return new ComponentName(context, SecondaryUserAdminReceiver.class);
        }
    }
}