summaryrefslogtreecommitdiff
path: root/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java
blob: bc40dbd3afda2635a40f9fd4b83091e5122e8f93 (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
/*
 * Copyright (C) 2014 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.devicepolicy;

import static com.android.cts.devicepolicy.metrics.DevicePolicyEventLogVerifier.assertMetricsLogged;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import android.platform.test.annotations.FlakyTest;
import android.platform.test.annotations.LargeTest;
import android.stats.devicepolicy.EventId;

import com.android.cts.devicepolicy.DeviceAdminFeaturesCheckerRule.DoesNotRequireFeature;
import com.android.cts.devicepolicy.metrics.DevicePolicyEventWrapper;
import com.android.ddmlib.Log.LogLevel;
import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.log.LogUtil.CLog;
import com.android.tradefed.util.RunInterruptedException;
import com.android.tradefed.util.RunUtil;

import org.junit.Ignore;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Set of tests for Managed Profile use cases.
 */
public final class ManagedProfileTest extends BaseManagedProfileTest {

    private static final String DEVICE_OWNER_PKG = "com.android.cts.deviceowner";
    private static final String DEVICE_OWNER_APK = "CtsDeviceOwnerApp.apk";
    private static final String DEVICE_OWNER_ADMIN =
            DEVICE_OWNER_PKG + ".BaseDeviceOwnerTest$BasicAdminReceiver";

    @Test
    public void testManagedProfileSetup() throws Exception {
        runDeviceTestsAsUser(
                MANAGED_PROFILE_PKG, MANAGED_PROFILE_PKG + ".ManagedProfileSetupTest",
                mProfileUserId);
    }

    @DoesNotRequireFeature
    @Test
    public void testMaxOneManagedProfile() throws Exception {
        int newUserId = -1;
        try {
            newUserId = createManagedProfile(mParentUserId);
        } catch (AssertionError expected) {
        }
        if (newUserId > 0) {
            removeUser(newUserId);
            if (mFeaturesCheckerRule.hasRequiredFeatures()) {
                // Exception is Android TV which can create multiple managed profiles
                if (!isTv()) {
                    fail("Device must allow creating only one managed profile");
                }
            } else {
                fail("Device must not allow creating a managed profile");
            }
        }
    }

    /**
     * Verify that removing a managed profile will remove all networks owned by that profile.
     */
    @Test
    public void testProfileWifiCleanup() throws Exception {
        assumeHasWifiFeature();

        try (LocationModeSetter locationModeSetter = new LocationModeSetter(getDevice())) {
            locationModeSetter.setLocationEnabled(true);
            runDeviceTestsAsUser(
                    MANAGED_PROFILE_PKG, ".WifiTest", "testRemoveWifiNetworkIfExists",
                    mParentUserId);

            runDeviceTestsAsUser(
                    MANAGED_PROFILE_PKG, ".WifiTest", "testAddWifiNetwork", mProfileUserId);

            // Now delete the user - should undo the effect of testAddWifiNetwork.
            removeUser(mProfileUserId);
            runDeviceTestsAsUser(
                    MANAGED_PROFILE_PKG, ".WifiTest", "testWifiNetworkDoesNotExist",
                    mParentUserId);
        }
    }

    @Test
    public void testSettingsIntents() throws Exception {
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".SettingsIntentsTest",
                mProfileUserId);
    }

    /** Tests for the API helper class. */
    @Test
    public void testCurrentApiHelper() throws Exception {
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CurrentApiHelperTest",
                mProfileUserId);
    }

    /** Test: unsupported public APIs are disabled on a parent profile. */
    @Test
    public void testParentProfileApiDisabled() throws Exception {
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".ParentProfileTest",
                "testParentProfileApiDisabled", mProfileUserId);
    }

    @Test
    public void testCannotCallMethodsOnParentProfile() throws Exception {
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".ParentProfileTest",
                "testCannotWipeParentProfile", mProfileUserId);

        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".ParentProfileTest",
                "testCannotCallSetDefaultSmsApplicationOnParentProfile", mProfileUserId);
    }

    // TODO: This test is not specific to managed profiles, but applies to multi-user in general.
    // Move it to a MultiUserTest class when there is one. Should probably move
    // SetPolicyActivity to a more generic apk too as it might be useful for different kinds
    // of tests (same applies to ComponentDisablingActivity).
    @Test
    public void testNoDebuggingFeaturesRestriction() throws Exception {
        // If adb is running as root, then the adb uid is 0 instead of SHELL_UID,
        // so the DISALLOW_DEBUGGING_FEATURES restriction does not work and this test
        // fails.
        if (getDevice().isAdbRoot()) {
            CLog.logAndDisplay(LogLevel.WARN,
                    "Cannot test testNoDebuggingFeaturesRestriction() in eng/userdebug build");
            return;
        }
        String restriction = "no_debugging_features";  // UserManager.DISALLOW_DEBUGGING_FEATURES

        changeUserRestrictionOrFail(restriction, true, mProfileUserId);


        // This should now fail, as the shell is not available to start activities under a different
        // user once the restriction is in place.
        String addRestrictionCommandOutput =
                changeUserRestriction(restriction, true, mProfileUserId);
        assertTrue(
                "Expected SecurityException when starting the activity "
                        + addRestrictionCommandOutput,
                addRestrictionCommandOutput.contains("SecurityException"));
    }

    @Test
    public void testOrganizationInfo() throws Exception {
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".OrganizationInfoTest",
                "testDefaultOrganizationColor", mProfileUserId);
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".OrganizationInfoTest",
                "testDefaultOrganizationNameIsNull", mProfileUserId);
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".OrganizationInfoTest",
                mProfileUserId);
        assertMetricsLogged(getDevice(), () -> {
            runDeviceTestsAsUser(
                    MANAGED_PROFILE_PKG, MANAGED_PROFILE_PKG + ".OrganizationInfoTest",
                    "testSetOrganizationColor", mProfileUserId);
        }, new DevicePolicyEventWrapper.Builder(EventId.SET_ORGANIZATION_COLOR_VALUE)
                .setAdminPackageName(MANAGED_PROFILE_PKG)
                .build());
    }

    @Test
    public void testDevicePolicyManagerParentSupport() throws Exception {
        runDeviceTestsAsUser(
                MANAGED_PROFILE_PKG, ".DevicePolicyManagerParentSupportTest", mProfileUserId);
    }

    @Test
    public void testBluetoothContactSharingDisabled() throws Exception {
        assertMetricsLogged(getDevice(), () -> {
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".ContactsTest",
                    "testSetBluetoothContactSharingDisabled_setterAndGetter", mProfileUserId);
        }, new DevicePolicyEventWrapper
                    .Builder(EventId.SET_BLUETOOTH_CONTACT_SHARING_DISABLED_VALUE)
                    .setAdminPackageName(MANAGED_PROFILE_PKG)
                    .setBoolean(false)
                    .build(),
            new DevicePolicyEventWrapper
                    .Builder(EventId.SET_BLUETOOTH_CONTACT_SHARING_DISABLED_VALUE)
                    .setAdminPackageName(MANAGED_PROFILE_PKG)
                    .setBoolean(true)
                    .build());
    }

    @Test
    public void testCannotSetProfileOwnerAgain() throws Exception {
        // verify that we can't set the same admin receiver as profile owner again
        assertFalse(setProfileOwner(
                MANAGED_PROFILE_PKG + "/" + ADMIN_RECEIVER_TEST_CLASS, mProfileUserId,
                /*expectFailure*/ true));

        // verify that we can't set a different admin receiver as profile owner
        installAppAsUser(DEVICE_OWNER_APK, mProfileUserId);
        assertFalse(setProfileOwner(DEVICE_OWNER_PKG + "/" + DEVICE_OWNER_ADMIN, mProfileUserId,
                /*expectFailure*/ true));
    }

    @LargeTest
    @Test
    public void testCannotSetDeviceOwnerWhenProfilePresent() throws Exception {
        try {
            installAppAsUser(DEVICE_OWNER_APK, mParentUserId);
            assertFalse(setDeviceOwner(DEVICE_OWNER_PKG + "/" + DEVICE_OWNER_ADMIN, mParentUserId,
                    /*expectFailure*/ true));
        } finally {
            // make sure we clean up in case we succeeded in setting the device owner
            removeAdmin(DEVICE_OWNER_PKG + "/" + DEVICE_OWNER_ADMIN, mParentUserId);
            getDevice().uninstallPackage(DEVICE_OWNER_PKG);
        }
    }

    @Test
    public void testNfcRestriction() throws Exception {
        assumeHasNfcFeatures();

        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".NfcTest",
                "testNfcShareEnabled", mProfileUserId);
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".NfcTest",
                "testNfcShareEnabled", mParentUserId);

        changeUserRestrictionOrFail("no_outgoing_beam" /* UserManager.DISALLOW_OUTGOING_BEAM */,
                true, mProfileUserId);

        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".NfcTest",
                "testNfcShareDisabled", mProfileUserId);
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".NfcTest",
                "testNfcShareEnabled", mParentUserId);
    }

    @Test
    public void testIsProvisioningAllowed() throws DeviceNotAvailableException {
        // Not allowed to add a managed profile from another managed profile.
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PreManagedProfileTest",
                "testIsProvisioningAllowedFalse", mProfileUserId);

        // Not allowed to add a managed profile to the parent user if one already exists.
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PreManagedProfileTest",
                "testIsProvisioningAllowedFalse", mParentUserId);
    }

    @Test
    public void testPhoneAccountVisibility() throws Exception {
        assumeHasTelephonyAndConnectionServiceFeatures();

        try {
            // Register phone account in parent user.
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
                    "testRegisterPhoneAccount",
                    mParentUserId);
            // The phone account should not be visible in managed user.
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
                    "testPhoneAccountNotRegistered",
                    mProfileUserId);
        } finally {
            // Unregister the phone account.
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
                    "testUnregisterPhoneAccount",
                    mParentUserId);
        }

        try {
            // Register phone account in profile user.
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
                    "testRegisterPhoneAccount",
                    mProfileUserId);
            // The phone account should not be visible in parent user.
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
                    "testPhoneAccountNotRegistered",
                    mParentUserId);
        } finally {
            // Unregister the phone account.
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
                    "testUnregisterPhoneAccount",
                    mProfileUserId);
        }
    }

    @LargeTest
    @Test
    public void testManagedCall() throws Exception {
        assumeHasTelephonyAndConnectionServiceFeatures();

        getDevice().executeShellCommand("telecom set-default-dialer " + MANAGED_PROFILE_PKG);

        // Place a outgoing call through work phone account using TelecomManager and verify the
        // call is inserted properly.
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
                "testOutgoingCallUsingTelecomManager",
                mProfileUserId);
        // Make sure the call is not inserted into parent user.
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
                "testEnsureCallNotInserted",
                mParentUserId);

        // Place a outgoing call through work phone account using ACTION_CALL and verify the call
        // is inserted properly.
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
                "testOutgoingCallUsingActionCall",
                mProfileUserId);
        // Make sure the call is not inserted into parent user.
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
                "testEnsureCallNotInserted",
                mParentUserId);

        // Add an incoming call with parent user's phone account and verify the call is inserted
        // properly.
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
                "testIncomingCall",
                mProfileUserId);
        // Make sure the call is not inserted into parent user.
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
                "testEnsureCallNotInserted",
                mParentUserId);

        // Add an incoming missed call with parent user's phone account and verify the call is
        // inserted properly.
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
                "testIncomingMissedCall",
                mProfileUserId);
        // Make sure the call is not inserted into parent user.
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
                "testEnsureCallNotInserted",
                mParentUserId);
    }

    @Test
    public void testTrustAgentInfo() throws Exception {
        assumeHasSecureLockScreenFeature();

        // Set and get trust agent config using child dpm instance.
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".TrustAgentInfoTest",
                "testSetAndGetTrustAgentConfiguration_child",
                mProfileUserId);
        // Set and get trust agent config using parent dpm instance.
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".TrustAgentInfoTest",
                "testSetAndGetTrustAgentConfiguration_parent",
                mProfileUserId);
        // Unified case
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".TrustAgentInfoTest",
                "testSetTrustAgentConfiguration_bothHaveTrustAgentConfigAndUnified",
                mProfileUserId);
        // Non-unified case
        try {
            changeUserCredential(TEST_PASSWORD, null, mProfileUserId);
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".TrustAgentInfoTest",
                    "testSetTrustAgentConfiguration_bothHaveTrustAgentConfigAndNonUnified",
                    mProfileUserId);
        } finally {
            changeUserCredential(null, TEST_PASSWORD, mProfileUserId);
        }
    }

    @Test
    public void testProfileOwnerOnPersonalDeviceCannotGetDeviceIdentifiers() throws Exception {
        // The Profile Owner should have access to all device identifiers.
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".DeviceIdentifiersTest",
                "testProfileOwnerOnPersonalDeviceCannotGetDeviceIdentifiers", mProfileUserId);
    }

    @Test
    public void testSetProfileNameLogged() throws Exception {
        assertMetricsLogged(getDevice(), () -> {
            runDeviceTestsAsUser(
                    MANAGED_PROFILE_PKG, MANAGED_PROFILE_PKG + ".DevicePolicyLoggingTest",
                    "testSetProfileNameLogged", mProfileUserId);
        }, new DevicePolicyEventWrapper.Builder(EventId.SET_PROFILE_NAME_VALUE)
                .setAdminPackageName(MANAGED_PROFILE_PKG)
                .build());
    }

    @Test
    public void userManagerIsManagedProfileReturnsCorrectValues() throws Exception {
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".UserManagerTest",
                "testIsManagedProfileReturnsTrue", mProfileUserId);

        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".UserManagerTest",
                "testIsManagedProfileReturnsFalse", mPrimaryUserId);
    }

    @Test
    public void testCanGetWorkShortcutIconDrawableFromPersonalProfile()
            throws DeviceNotAvailableException {
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".LauncherAppsTest",
                "addDynamicShortcuts", mProfileUserId);
        try {
            Map<String, String> params = new HashMap<>();
            params.put("otherProfileUserId", String.valueOf(mProfileUserId));
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".LauncherAppsTest",
                    "shortcutIconDrawable_currentToOtherProfile_withUsersFullPermission_isNotNull",
                    mPrimaryUserId, params);
        } finally {
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".LauncherAppsTest",
                    "removeAllDynamicShortcuts", mProfileUserId);
        }
    }

    @Test
    public void testCanGetPersonalShortcutIconDrawableFromWorkProfile()
            throws DeviceNotAvailableException {
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".LauncherAppsTest",
                "addDynamicShortcuts", mPrimaryUserId);
        try {
            Map<String, String> params = new HashMap<>();
            params.put("otherProfileUserId", String.valueOf(mPrimaryUserId));
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".LauncherAppsTest",
                    "shortcutIconDrawable_currentToOtherProfile_withUsersFullPermission_isNotNull",
                    mProfileUserId, params);
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".LauncherAppsTest",
                    "shortcutIconDrawable_currentToOtherProfile_withoutUsersFullPermission_isNull",
                    mProfileUserId, params);
        } finally {
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".LauncherAppsTest",
                    "removeAllDynamicShortcuts", mPrimaryUserId);
        }
    }

    @Test
    public void testCanGetProfiles() throws Exception {
        // getAllProfiles should contain both the primary and profile
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".UserManagerTest",
                "testGetAllProfiles", mPrimaryUserId);

        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".UserManagerTest",
                "testGetAllProfiles", mProfileUserId);

        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".UserManagerTest",
                "testIsProfileReturnsFalse_runAsPrimary", mPrimaryUserId);

        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".UserManagerTest",
                "testIsProfileReturnsTrue_runAsProfile", mProfileUserId);
    }

    @Test
    public void testCanCreateProfile() throws Exception {
        // remove pre-created profile
        removeUser(mProfileUserId);

        // create profile from installed app
        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".UserManagerTest",
                "testCreateProfile_managedProfile", mPrimaryUserId);
    }

    @Test
    public void testResolverActivityLaunchedFromPersonalProfileWithSelectedWorkTab()
            throws Exception {
        installAppAsUser(SHARING_APP_1_APK, mPrimaryUserId);
        installAppAsUser(SHARING_APP_2_APK, mPrimaryUserId);
        installAppAsUser(SHARING_APP_1_APK, mProfileUserId);
        installAppAsUser(SHARING_APP_2_APK, mProfileUserId);
        try {
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileSharingTest",
                    "addCrossProfileIntents", mProfileUserId);
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileSharingTest",
                    "startSwitchToOtherProfileIntent", mPrimaryUserId);
            assertResolverActivityInForeground(mPrimaryUserId);
        } finally {
            pressHome();
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileSharingTest",
                    "clearCrossProfileIntents", mProfileUserId);
        }
    }

    @Test
    public void testResolverActivityLaunchedFromWorkProfileWithSelectedPersonalTab()
            throws Exception {
        installAppAsUser(SHARING_APP_1_APK, mPrimaryUserId);
        installAppAsUser(SHARING_APP_2_APK, mPrimaryUserId);
        installAppAsUser(SHARING_APP_1_APK, mProfileUserId);
        installAppAsUser(SHARING_APP_2_APK, mProfileUserId);
        try {
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileSharingTest",
                    "addCrossProfileIntents", mProfileUserId);
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileSharingTest",
                    "startSwitchToOtherProfileIntent", mProfileUserId);

            // TODO(b/223178698): Investigate potential increase in latency
            RunUtil.getDefault().sleep(30000);

            assertResolverActivityInForeground(mProfileUserId);
        } finally {
            pressHome();
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileSharingTest",
                    "clearCrossProfileIntents", mProfileUserId);
        }
    }

    @Test
    public void testChooserActivityLaunchedFromPersonalProfileWithSelectedWorkTab()
            throws Exception {
        installAppAsUser(SHARING_APP_1_APK, mPrimaryUserId);
        installAppAsUser(SHARING_APP_2_APK, mPrimaryUserId);
        installAppAsUser(SHARING_APP_1_APK, mProfileUserId);
        installAppAsUser(SHARING_APP_2_APK, mProfileUserId);
        try {
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileSharingTest",
                    "addCrossProfileIntents", mProfileUserId);
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileSharingTest",
                    "startSwitchToOtherProfileIntent_chooser", mPrimaryUserId);
            assertChooserActivityInForeground(mPrimaryUserId);
        } finally {
            pressHome();
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileSharingTest",
                    "clearCrossProfileIntents", mProfileUserId);
        }
    }

    @Test
    public void testChooserActivityLaunchedFromWorkProfileWithSelectedPersonalTab()
            throws Exception {
        installAppAsUser(SHARING_APP_1_APK, mPrimaryUserId);
        installAppAsUser(SHARING_APP_2_APK, mPrimaryUserId);
        installAppAsUser(SHARING_APP_1_APK, mProfileUserId);
        installAppAsUser(SHARING_APP_2_APK, mProfileUserId);
        RunUtil.getDefault().sleep(3000);
        try {
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileSharingTest",
                    "addCrossProfileIntents", mProfileUserId);
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileSharingTest",
                    "startSwitchToOtherProfileIntent_chooser", mProfileUserId);

            RunUtil.getDefault().sleep(30000);

            assertChooserActivityInForeground(mProfileUserId);
        } finally {
            pressHome();
            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileSharingTest",
                    "clearCrossProfileIntents", mProfileUserId);
        }
    }

    private void pressHome() throws Exception {
        executeShellCommand("input keyevent KEYCODE_HOME");
    }

    private void assertChooserActivityInForeground(int userId)
            throws Exception {
        try {
            assertActivityInForeground("android/com.android.internal.app.ChooserActivity", userId);
        } catch (AssertionError e) {
            CLog.v("ChooserActivity is not the default: " + e);
            assertActivityInForeground(resolveActivity("android.intent.action.CHOOSER"), userId);
        }
    }

    private void assertResolverActivityInForeground(int userId)
            throws Exception {
        try {
            assertActivityInForeground("android/com.android.internal.app.ResolverActivity", userId);
        } catch (AssertionError e) {
            CLog.v("ResolverActivity is not the default: " + e);
            assertActivityInForeground(getCustomResolverActivity(), userId);
        }
    }

    private void assertActivityInForeground(String fullActivityName, int userId)
            throws DeviceNotAvailableException {
        final long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(30);
        while (System.nanoTime() <= deadline) {
            String commandOutput = getDevice().executeShellCommand(
                    "dumpsys activity activities | grep Resumed:");
            if (commandOutput.contains("u" + userId + " " + fullActivityName)) {
                return;
            }
            try {
                RunUtil.getDefault().sleep(100);
            } catch (RunInterruptedException e){
                e.printStackTrace();
                Thread.currentThread().interrupt();
            }
        }
        fail("Activity " + fullActivityName + " didn't become foreground");
    }

    private void changeUserRestrictionOrFail(String key, boolean value, int userId)
            throws DeviceNotAvailableException {
        changeUserRestrictionOrFail(key, value, userId, MANAGED_PROFILE_PKG);
    }

    private String changeUserRestriction(String key, boolean value, int userId)
            throws DeviceNotAvailableException {
        return changeUserRestriction(key, value, userId, MANAGED_PROFILE_PKG);
    }

    private String resolveActivity(String action) throws Exception  {
        // Template output of 'pm resolve-activity -a {action} --brief' :
        // priority=0 preferredOrder=0 match=0x0 specificIndex=-1 isDefault=false
        // com.bar/.foo or com.bar/com.bar.foo
        final String[] outputs = getDevice().executeShellCommand(
                    "pm resolve-activity -a " + action + " --brief").split("\n");

        assertTrue("Result of shell command: 'pm resolve-activity -a " + action
                   + " --brief' is " + outputs[0], outputs.length >= 2);

        return outputs[1];
    }

    private String getCustomResolverActivity() throws Exception {
        final String[] outputs = getDevice().executeShellCommand(
                "cmd overlay lookup android android:string/config_customResolverActivity")
                .split("\n");

        String customResolverActivity = resolveActivity("android.intent.action.SEND");
        if (outputs != null && outputs.length >= 1 && outputs[0] != null && !outputs[0].isEmpty()) {
            customResolverActivity = outputs[0];
        }
        return customResolverActivity;
    }
}