1 /*
2  * Copyright (C) 2014 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 
17 package com.android.cts.devicepolicy;
18 
19 import com.android.ddmlib.Log.LogLevel;
20 import com.android.tradefed.device.DeviceNotAvailableException;
21 import com.android.tradefed.log.LogUtil.CLog;
22 
23 import junit.framework.AssertionFailedError;
24 
25 import java.util.concurrent.Callable;
26 
27 /**
28  * Set of tests for Managed Profile use cases.
29  */
30 public class ManagedProfileTest extends BaseDevicePolicyTest {
31 
32     private static final String MANAGED_PROFILE_PKG = "com.android.cts.managedprofile";
33     private static final String MANAGED_PROFILE_APK = "CtsManagedProfileApp.apk";
34 
35     private static final String DEVICE_OWNER_PKG = "com.android.cts.deviceowner";
36     private static final String DEVICE_OWNER_APK = "CtsDeviceOwnerApp.apk";
37     private static final String DEVICE_OWNER_ADMIN =
38             DEVICE_OWNER_PKG + ".BaseDeviceOwnerTest$BasicAdminReceiver";
39 
40     private static final String INTENT_SENDER_PKG = "com.android.cts.intent.sender";
41     private static final String INTENT_SENDER_APK = "CtsIntentSenderApp.apk";
42 
43     private static final String INTENT_RECEIVER_PKG = "com.android.cts.intent.receiver";
44     private static final String INTENT_RECEIVER_APK = "CtsIntentReceiverApp.apk";
45 
46     private static final String WIFI_CONFIG_CREATOR_PKG = "com.android.cts.wificonfigcreator";
47     private static final String WIFI_CONFIG_CREATOR_APK = "CtsWifiConfigCreator.apk";
48 
49     private static final String WIDGET_PROVIDER_APK = "CtsWidgetProviderApp.apk";
50     private static final String WIDGET_PROVIDER_PKG = "com.android.cts.widgetprovider";
51 
52     private static final String DIRECTORY_PROVIDER_APK = "CtsContactDirectoryProvider.apk";
53     private static final String DIRECTORY_PROVIDER_PKG
54             = "com.android.cts.contactdirectoryprovider";
55     private static final String PRIMARY_DIRECTORY_PREFIX = "Primary";
56     private static final String MANAGED_DIRECTORY_PREFIX = "Managed";
57     private static final String DIRECTORY_PRIVOIDER_URI
58             = "content://com.android.cts.contact.directory.provider/";
59     private static final String SET_CUSTOM_DIRECTORY_PREFIX_METHOD = "set_prefix";
60 
61     private static final String ADMIN_RECEIVER_TEST_CLASS =
62             MANAGED_PROFILE_PKG + ".BaseManagedProfileTest$BasicAdminReceiver";
63 
64     private static final String FEATURE_BLUETOOTH = "android.hardware.bluetooth";
65     private static final String FEATURE_CAMERA = "android.hardware.camera";
66     private static final String FEATURE_WIFI = "android.hardware.wifi";
67     private static final String FEATURE_TELEPHONY = "android.hardware.telephony";
68     private static final String FEATURE_CONNECTION_SERVICE = "android.software.connectionservice";
69 
70     private static final String ADD_RESTRICTION_COMMAND = "add-restriction";
71 
72     private int mParentUserId;
73 
74     // ID of the profile we'll create. This will always be a profile of the parent.
75     private int mProfileUserId;
76     private String mPackageVerifier;
77 
78     private boolean mHasNfcFeature;
79 
80     @Override
setUp()81     protected void setUp() throws Exception {
82         super.setUp();
83 
84         // We need multi user to be supported in order to create a profile of the user owner.
85         mHasFeature = mHasFeature && hasDeviceFeature(
86                 "android.software.managed_users");
87         mHasNfcFeature = hasDeviceFeature("android.hardware.nfc");
88 
89         if (mHasFeature) {
90             removeTestUsers();
91             mParentUserId = mPrimaryUserId;
92             mProfileUserId = createManagedProfile(mParentUserId);
93 
94             installAppAsUser(MANAGED_PROFILE_APK, mParentUserId);
95             installAppAsUser(MANAGED_PROFILE_APK, mProfileUserId);
96             setProfileOwnerOrFail(MANAGED_PROFILE_PKG + "/" + ADMIN_RECEIVER_TEST_CLASS,
97                     mProfileUserId);
98             startUser(mProfileUserId);
99         }
100     }
101 
102     @Override
tearDown()103     protected void tearDown() throws Exception {
104         if (mHasFeature) {
105             removeUser(mProfileUserId);
106             getDevice().uninstallPackage(MANAGED_PROFILE_PKG);
107             getDevice().uninstallPackage(INTENT_SENDER_PKG);
108             getDevice().uninstallPackage(INTENT_RECEIVER_PKG);
109         }
110         super.tearDown();
111     }
112 
testManagedProfileSetup()113     public void testManagedProfileSetup() throws Exception {
114         if (!mHasFeature) {
115             return;
116         }
117         assertTrue(runDeviceTestsAsUser(
118                 MANAGED_PROFILE_PKG, MANAGED_PROFILE_PKG + ".ManagedProfileSetupTest",
119                 mProfileUserId));
120     }
121 
122     /**
123      *  wipeData() test removes the managed profile, so it needs to separated from other tests.
124      */
testWipeData()125     public void testWipeData() throws Exception {
126         if (!mHasFeature) {
127             return;
128         }
129         assertTrue(listUsers().contains(mProfileUserId));
130         assertTrue(runDeviceTestsAsUser(
131                 MANAGED_PROFILE_PKG, MANAGED_PROFILE_PKG + ".WipeDataTest", mProfileUserId));
132         // Note: the managed profile is removed by this test, which will make removeUserCommand in
133         // tearDown() to complain, but that should be OK since its result is not asserted.
134         assertFalse(listUsers().contains(mProfileUserId));
135     }
136 
testMaxOneManagedProfile()137     public void testMaxOneManagedProfile() throws Exception {
138         int newUserId = -1;
139         try {
140             newUserId = createManagedProfile(mParentUserId);
141         } catch (AssertionFailedError expected) {
142         }
143         if (newUserId > 0) {
144             removeUser(newUserId);
145             fail(mHasFeature ? "Device must allow creating only one managed profile"
146                     : "Device must not allow creating a managed profile");
147         }
148     }
149 
150     /**
151      * Verify that removing a managed profile will remove all networks owned by that profile.
152      */
testProfileWifiCleanup()153     public void testProfileWifiCleanup() throws Exception {
154         if (!mHasFeature || !hasDeviceFeature(FEATURE_WIFI)) {
155             return;
156         }
157         assertTrue("WiFi config already exists and could not be removed", runDeviceTestsAsUser(
158                 MANAGED_PROFILE_PKG, ".WifiTest", "testRemoveWifiNetworkIfExists", mParentUserId));
159 
160         installAppAsUser(WIFI_CONFIG_CREATOR_APK, mProfileUserId);
161         assertTrue("Failed to add WiFi config", runDeviceTestsAsUser(
162                 MANAGED_PROFILE_PKG, ".WifiTest", "testAddWifiNetwork", mProfileUserId));
163 
164         // Now delete the user - should undo the effect of testAddWifiNetwork.
165         removeUser(mProfileUserId);
166         assertTrue("WiFi config not removed after deleting profile", runDeviceTestsAsUser(
167                 MANAGED_PROFILE_PKG, ".WifiTest", "testWifiNetworkDoesNotExist",
168                 mParentUserId));
169     }
170 
testCrossProfileIntentFilters()171     public void testCrossProfileIntentFilters() throws Exception {
172         if (!mHasFeature) {
173             return;
174         }
175         // Set up activities: ManagedProfileActivity will only be enabled in the managed profile and
176         // PrimaryUserActivity only in the primary one
177         disableActivityForUser("ManagedProfileActivity", mParentUserId);
178         disableActivityForUser("PrimaryUserActivity", mProfileUserId);
179 
180         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG,
181                 MANAGED_PROFILE_PKG + ".ManagedProfileTest", mProfileUserId));
182 
183         // Set up filters from primary to managed profile
184         String command = "am start -W --user " + mProfileUserId  + " " + MANAGED_PROFILE_PKG
185                 + "/.PrimaryUserFilterSetterActivity";
186         CLog.d("Output for command " + command + ": "
187               + getDevice().executeShellCommand(command));
188         assertTrue(runDeviceTestsAsUser(
189                 MANAGED_PROFILE_PKG, MANAGED_PROFILE_PKG + ".PrimaryUserTest", mParentUserId));
190         // TODO: Test with startActivity
191     }
192 
testAppLinks()193     public void testAppLinks() throws Exception {
194         if (!mHasFeature) {
195             return;
196         }
197         // Disable all pre-existing browsers in the managed profile so they don't interfere with
198         // intents resolution.
199         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileUtils",
200                 "testDisableAllBrowsers", mProfileUserId));
201         installAppAsUser(INTENT_RECEIVER_APK, mParentUserId);
202         installAppAsUser(INTENT_SENDER_APK, mParentUserId);
203         installAppAsUser(INTENT_RECEIVER_APK, mProfileUserId);
204         installAppAsUser(INTENT_SENDER_APK, mProfileUserId);
205 
206         changeVerificationStatus(mParentUserId, INTENT_RECEIVER_PKG, "ask");
207         changeVerificationStatus(mProfileUserId, INTENT_RECEIVER_PKG, "ask");
208         // We should have two receivers: IntentReceiverActivity and BrowserActivity in the
209         // managed profile
210         assertAppLinkResult("testTwoReceivers");
211 
212         changeUserRestrictionForUser("allow_parent_profile_app_linking", ADD_RESTRICTION_COMMAND,
213                 mProfileUserId);
214         // Now we should also have one receiver in the primary user, so three receivers in total.
215         assertAppLinkResult("testThreeReceivers");
216 
217         changeVerificationStatus(mParentUserId, INTENT_RECEIVER_PKG, "never");
218         // The primary user one has been set to never: we should only have the managed profile ones.
219         assertAppLinkResult("testTwoReceivers");
220 
221         changeVerificationStatus(mProfileUserId, INTENT_RECEIVER_PKG, "never");
222         // Now there's only the browser in the managed profile left
223         assertAppLinkResult("testReceivedByBrowserActivityInManaged");
224 
225         changeVerificationStatus(mProfileUserId, INTENT_RECEIVER_PKG, "always");
226         changeVerificationStatus(mParentUserId, INTENT_RECEIVER_PKG, "always");
227         // We have one always in the primary user and one always in the managed profile: the managed
228         // profile one should have precedence.
229         assertAppLinkResult("testReceivedByAppLinkActivityInManaged");
230     }
231 
232 
testSettingsIntents()233     public void testSettingsIntents() throws Exception {
234         if (!mHasFeature) {
235             return;
236         }
237 
238         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".SettingsIntentsTest",
239                 mProfileUserId));
240     }
241 
testCrossProfileContent()242     public void testCrossProfileContent() throws Exception {
243         if (!mHasFeature) {
244             return;
245         }
246         installAppAsUser(INTENT_RECEIVER_APK, mParentUserId);
247         installAppAsUser(INTENT_SENDER_APK, mParentUserId);
248         installAppAsUser(INTENT_RECEIVER_APK, mProfileUserId);
249         installAppAsUser(INTENT_SENDER_APK, mProfileUserId);
250 
251         // Test from parent to managed
252         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileUtils",
253                 "testRemoveAllFilters", mProfileUserId));
254         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileUtils",
255                 "testAddManagedCanAccessParentFilters", mProfileUserId));
256         assertTrue(runDeviceTestsAsUser(INTENT_SENDER_PKG, ".ContentTest", mParentUserId));
257 
258         // Test from managed to parent
259         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileUtils",
260                 "testRemoveAllFilters", mProfileUserId));
261         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileUtils",
262                 "testAddParentCanAccessManagedFilters", mProfileUserId));
263         assertTrue(runDeviceTestsAsUser(INTENT_SENDER_PKG, ".ContentTest", mProfileUserId));
264 
265     }
266 
testCrossProfileCopyPaste()267     public void testCrossProfileCopyPaste() throws Exception {
268         if (!mHasFeature) {
269             return;
270         }
271         installAppAsUser(INTENT_RECEIVER_APK, mParentUserId);
272         installAppAsUser(INTENT_SENDER_APK, mParentUserId);
273         installAppAsUser(INTENT_RECEIVER_APK, mProfileUserId);
274         installAppAsUser(INTENT_SENDER_APK, mProfileUserId);
275 
276         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileUtils",
277                 "testAllowCrossProfileCopyPaste", mProfileUserId));
278         // Test that managed can see what is copied in the parent.
279         testCrossProfileCopyPasteInternal(mProfileUserId, true);
280         // Test that the parent can see what is copied in managed.
281         testCrossProfileCopyPasteInternal(mParentUserId, true);
282 
283         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileUtils",
284                 "testDisallowCrossProfileCopyPaste", mProfileUserId));
285         // Test that managed can still see what is copied in the parent.
286         testCrossProfileCopyPasteInternal(mProfileUserId, true);
287         // Test that the parent cannot see what is copied in managed.
288         testCrossProfileCopyPasteInternal(mParentUserId, false);
289     }
290 
testCrossProfileCopyPasteInternal(int userId, boolean shouldSucceed)291     private void testCrossProfileCopyPasteInternal(int userId, boolean shouldSucceed)
292             throws DeviceNotAvailableException {
293         final String direction = (userId == mParentUserId)
294                 ? "testAddManagedCanAccessParentFilters"
295                 : "testAddParentCanAccessManagedFilters";
296         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileUtils",
297                 "testRemoveAllFilters", mProfileUserId));
298         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileUtils",
299                 direction, mProfileUserId));
300         if (shouldSucceed) {
301             assertTrue(runDeviceTestsAsUser(INTENT_SENDER_PKG, ".CopyPasteTest",
302                     "testCanReadAcrossProfiles", userId));
303             assertTrue(runDeviceTestsAsUser(INTENT_SENDER_PKG, ".CopyPasteTest",
304                     "testIsNotified", userId));
305         } else {
306             assertTrue(runDeviceTestsAsUser(INTENT_SENDER_PKG, ".CopyPasteTest",
307                     "testCannotReadAcrossProfiles", userId));
308         }
309     }
310 
311     // TODO: This test is not specific to managed profiles, but applies to multi-user in general.
312     // Move it to a MultiUserTest class when there is one. Should probably move
313     // SetPolicyActivity to a more generic apk too as it might be useful for different kinds
314     // of tests (same applies to ComponentDisablingActivity).
testNoDebuggingFeaturesRestriction()315     public void testNoDebuggingFeaturesRestriction() throws Exception {
316         if (!mHasFeature) {
317             return;
318         }
319         // If adb is running as root, then the adb uid is 0 instead of SHELL_UID,
320         // so the DISALLOW_DEBUGGING_FEATURES restriction does not work and this test
321         // fails.
322         if (getDevice().isAdbRoot()) {
323             CLog.logAndDisplay(LogLevel.WARN,
324                     "Cannot test testNoDebuggingFeaturesRestriction() in eng/userdebug build");
325             return;
326         }
327         String restriction = "no_debugging_features";  // UserManager.DISALLOW_DEBUGGING_FEATURES
328 
329         String addRestrictionCommandOutput =
330                 changeUserRestrictionForUser(restriction, ADD_RESTRICTION_COMMAND, mProfileUserId);
331         assertTrue("Command was expected to succeed " + addRestrictionCommandOutput,
332                 addRestrictionCommandOutput.contains("Status: ok"));
333 
334         // This should now fail, as the shell is not available to start activities under a different
335         // user once the restriction is in place.
336         addRestrictionCommandOutput =
337                 changeUserRestrictionForUser(restriction, ADD_RESTRICTION_COMMAND, mProfileUserId);
338         assertTrue(
339                 "Expected SecurityException when starting the activity "
340                         + addRestrictionCommandOutput,
341                 addRestrictionCommandOutput.contains("SecurityException"));
342     }
343 
344     // Test the bluetooth API from a managed profile.
testBluetooth()345     public void testBluetooth() throws Exception {
346         boolean mHasBluetooth = hasDeviceFeature(FEATURE_BLUETOOTH);
347         if (!mHasFeature || !mHasBluetooth) {
348             return ;
349         }
350 
351         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".BluetoothTest",
352                 "testEnableDisable", mProfileUserId));
353         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".BluetoothTest",
354                 "testGetAddress", mProfileUserId));
355         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".BluetoothTest",
356                 "testListenUsingRfcommWithServiceRecord", mProfileUserId));
357         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".BluetoothTest",
358                 "testGetRemoteDevice", mProfileUserId));
359     }
360 
testCameraPolicy()361     public void testCameraPolicy() throws Exception {
362         boolean hasCamera = hasDeviceFeature(FEATURE_CAMERA);
363         if (!mHasFeature || !hasCamera) {
364             return;
365         }
366         try {
367             setDeviceAdmin(MANAGED_PROFILE_PKG + "/.PrimaryUserDeviceAdmin", mParentUserId);
368 
369             // Disable managed profile camera.
370             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CameraPolicyTest",
371                     "testDisableCameraInManagedProfile",
372                     mProfileUserId));
373             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CameraPolicyTest",
374                     "testIsCameraEnabledInPrimaryProfile",
375                     mParentUserId));
376 
377             // Enable managed profile camera.
378             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CameraPolicyTest",
379                     "testEnableCameraInManagedProfile",
380                     mProfileUserId));
381             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CameraPolicyTest",
382                     "testIsCameraEnabledInPrimaryProfile",
383                     mParentUserId));
384 
385             // Disable primary profile camera.
386             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CameraPolicyTest",
387                     "testDisableCameraInPrimaryProfile",
388                     mParentUserId));
389             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CameraPolicyTest",
390                     "testIsCameraEnabledInManagedProfile",
391                     mProfileUserId));
392 
393             // Enable primary profile camera.
394             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CameraPolicyTest",
395                     "testEnableCameraInPrimaryProfile",
396                     mParentUserId));
397             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CameraPolicyTest",
398                     "testIsCameraEnabledInManagedProfile",
399                     mProfileUserId));
400         } finally {
401             final String adminHelperClass = ".PrimaryUserAdminHelper";
402             assertTrue("Clear device admin failed", runDeviceTestsAsUser(MANAGED_PROFILE_PKG,
403                     adminHelperClass, "testClearDeviceAdmin", mParentUserId));
404         }
405     }
406 
407 
testManagedContactsUris()408     public void testManagedContactsUris() throws Exception {
409         runManagedContactsTest(new Callable<Void>() {
410             @Override
411             public Void call() throws Exception {
412                 ContactsTestSet contactsTestSet = new ContactsTestSet(ManagedProfileTest.this,
413                         MANAGED_PROFILE_PKG, mParentUserId, mProfileUserId);
414 
415                 contactsTestSet.setCallerIdEnabled(true);
416                 contactsTestSet.setContactsSearchEnabled(true);
417                 contactsTestSet.checkIfCanLookupEnterpriseContacts(true);
418                 contactsTestSet.checkIfCanFilterEnterpriseContacts(true);
419                 contactsTestSet.checkIfCanFilterSelfContacts();
420                 return null;
421             }
422         });
423     }
424 
testManagedQuickContacts()425     public void testManagedQuickContacts() throws Exception {
426         runManagedContactsTest(new Callable<Void>() {
427             @Override
428             public Void call() throws Exception {
429                 assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".ContactsTest",
430                         "testQuickContact", mParentUserId));
431                 return null;
432             }
433         });
434     }
435 
testManagedContactsPolicies()436     public void testManagedContactsPolicies() throws Exception {
437         runManagedContactsTest(new Callable<Void>() {
438             @Override
439             public Void call() throws Exception {
440                 ContactsTestSet contactsTestSet = new ContactsTestSet(ManagedProfileTest.this,
441                         MANAGED_PROFILE_PKG, mParentUserId, mProfileUserId);
442                 try {
443                     contactsTestSet.setCallerIdEnabled(true);
444                     contactsTestSet.setContactsSearchEnabled(false);
445                     contactsTestSet.checkIfCanLookupEnterpriseContacts(true);
446                     contactsTestSet.checkIfCanFilterEnterpriseContacts(false);
447                     contactsTestSet.checkIfCanFilterSelfContacts();
448                     contactsTestSet.setCallerIdEnabled(false);
449                     contactsTestSet.setContactsSearchEnabled(true);
450                     contactsTestSet.checkIfCanLookupEnterpriseContacts(false);
451                     contactsTestSet.checkIfCanFilterEnterpriseContacts(true);
452                     contactsTestSet.checkIfCanFilterSelfContacts();
453                     contactsTestSet.setCallerIdEnabled(false);
454                     contactsTestSet.setContactsSearchEnabled(false);
455                     contactsTestSet.checkIfCanLookupEnterpriseContacts(false);
456                     contactsTestSet.checkIfCanFilterEnterpriseContacts(false);
457                     contactsTestSet.checkIfCanFilterSelfContacts();
458                     contactsTestSet.checkIfNoEnterpriseDirectoryFound();
459                     return null;
460                 } finally {
461                     // reset policies
462                     contactsTestSet.setCallerIdEnabled(true);
463                     contactsTestSet.setContactsSearchEnabled(true);
464                 }
465             }
466         });
467     }
468 
testOrganizationInfo()469     public void testOrganizationInfo() throws Exception {
470         if (!mHasFeature) {
471             return;
472         }
473         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".OrganizationInfoTest",
474                 "testDefaultOrganizationColor", mProfileUserId));
475         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".OrganizationInfoTest",
476                 "testDefaultOrganizationNameIsNull", mProfileUserId));
477         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".OrganizationInfoTest",
478                 mProfileUserId));
479     }
480 
testPasswordMinimumRestrictions()481     public void testPasswordMinimumRestrictions() throws Exception {
482         if (!mHasFeature) {
483             return;
484         }
485         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PasswordMinimumRestrictionsTest",
486                 mProfileUserId));
487     }
488 
testBluetoothContactSharingDisabled()489     public void testBluetoothContactSharingDisabled() throws Exception {
490         if (!mHasFeature) {
491             return;
492         }
493         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".ContactsTest",
494                 "testSetBluetoothContactSharingDisabled_setterAndGetter", mProfileUserId));
495     }
496 
testCannotSetProfileOwnerAgain()497     public void testCannotSetProfileOwnerAgain() throws Exception {
498         if (!mHasFeature) {
499             return;
500         }
501         // verify that we can't set the same admin receiver as profile owner again
502         assertFalse(setProfileOwner(
503                 MANAGED_PROFILE_PKG + "/" + ADMIN_RECEIVER_TEST_CLASS, mProfileUserId,
504                 /*expectFailure*/ true));
505 
506         // verify that we can't set a different admin receiver as profile owner
507         installAppAsUser(DEVICE_OWNER_APK, mProfileUserId);
508         assertFalse(setProfileOwner(DEVICE_OWNER_PKG + "/" + DEVICE_OWNER_ADMIN, mProfileUserId,
509                 /*expectFailure*/ true));
510     }
511 
testCannotSetDeviceOwnerWhenProfilePresent()512     public void testCannotSetDeviceOwnerWhenProfilePresent() throws Exception {
513         if (!mHasFeature) {
514             return;
515         }
516 
517         try {
518             installAppAsUser(DEVICE_OWNER_APK, mParentUserId);
519             assertFalse(setDeviceOwner(DEVICE_OWNER_PKG + "/" + DEVICE_OWNER_ADMIN, mParentUserId,
520                     /*expectFailure*/ true));
521         } finally {
522             // make sure we clean up in case we succeeded in setting the device owner
523             removeAdmin(DEVICE_OWNER_PKG + "/" + DEVICE_OWNER_ADMIN, mParentUserId);
524             getDevice().uninstallPackage(DEVICE_OWNER_PKG);
525         }
526     }
527 
testNfcRestriction()528     public void testNfcRestriction() throws Exception {
529         if (!mHasFeature || !mHasNfcFeature) {
530             return;
531         }
532 
533         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".NfcTest",
534                 "testNfcShareEnabled", mProfileUserId));
535         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".NfcTest",
536                 "testNfcShareEnabled", mParentUserId));
537 
538         String restriction = "no_outgoing_beam";  // UserManager.DISALLOW_OUTGOING_BEAM
539         String command = "add-restriction";
540 
541         String addRestrictionCommandOutput =
542                 changeUserRestrictionForUser(restriction, command, mProfileUserId);
543         assertTrue("Command was expected to succeed " + addRestrictionCommandOutput,
544                 addRestrictionCommandOutput.contains("Status: ok"));
545 
546         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".NfcTest",
547                 "testNfcShareDisabled", mProfileUserId));
548         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".NfcTest",
549                 "testNfcShareEnabled", mParentUserId));
550     }
551 
testCrossProfileWidgets()552     public void testCrossProfileWidgets() throws Exception {
553         if (!mHasFeature) {
554             return;
555         }
556 
557         try {
558             installAppAsUser(WIDGET_PROVIDER_APK, mProfileUserId);
559             installAppAsUser(WIDGET_PROVIDER_APK, mParentUserId);
560             getDevice().executeShellCommand("appwidget grantbind --user " + mParentUserId
561                     + " --package " + WIDGET_PROVIDER_PKG);
562             startWidgetHostService();
563 
564             String commandOutput = changeCrossProfileWidgetForUser(WIDGET_PROVIDER_PKG,
565                     "add-cross-profile-widget", mProfileUserId);
566             assertTrue("Command was expected to succeed " + commandOutput,
567                     commandOutput.contains("Status: ok"));
568 
569             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileWidgetTest",
570                     "testCrossProfileWidgetProviderAdded", mProfileUserId));
571             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG,
572                     ".CrossProfileWidgetPrimaryUserTest",
573                     "testHasCrossProfileWidgetProvider_true", mParentUserId));
574             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG,
575                     ".CrossProfileWidgetPrimaryUserTest",
576                     "testHostReceivesWidgetUpdates_true", mParentUserId));
577 
578             commandOutput = changeCrossProfileWidgetForUser(WIDGET_PROVIDER_PKG,
579                     "remove-cross-profile-widget", mProfileUserId);
580             assertTrue("Command was expected to succeed " + commandOutput,
581                     commandOutput.contains("Status: ok"));
582 
583             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileWidgetTest",
584                     "testCrossProfileWidgetProviderRemoved", mProfileUserId));
585             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG,
586                     ".CrossProfileWidgetPrimaryUserTest",
587                     "testHasCrossProfileWidgetProvider_false", mParentUserId));
588             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG,
589                     ".CrossProfileWidgetPrimaryUserTest",
590                     "testHostReceivesWidgetUpdates_false", mParentUserId));
591         } finally {
592             changeCrossProfileWidgetForUser(WIDGET_PROVIDER_PKG, "remove-cross-profile-widget",
593                     mProfileUserId);
594             getDevice().uninstallPackage(WIDGET_PROVIDER_PKG);
595         }
596     }
597 
testIsProvisioningAllowed()598     public void testIsProvisioningAllowed() throws DeviceNotAvailableException {
599         if (!mHasFeature) {
600             return;
601         }
602         // In Managed profile user when managed profile is provisioned
603         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PreManagedProfileTest",
604                 "testIsProvisioningAllowedFalse", mProfileUserId));
605 
606         // In parent user when managed profile is provisioned
607         // It's allowed to provision again by removing the previous profile
608         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PreManagedProfileTest",
609                 "testIsProvisioningAllowedTrue", mParentUserId));
610     }
611 
setDirectoryPrefix(String directoryName, int userId)612     private void setDirectoryPrefix(String directoryName, int userId)
613             throws DeviceNotAvailableException {
614         String command = "content call --uri " + DIRECTORY_PRIVOIDER_URI
615                 + " --user " + userId
616                 + " --method " + SET_CUSTOM_DIRECTORY_PREFIX_METHOD
617                 + " --arg " + directoryName;
618         CLog.d("Output for command " + command + ": "
619                 + getDevice().executeShellCommand(command));
620     }
621 
testPhoneAccountVisibility()622     public void testPhoneAccountVisibility() throws Exception  {
623         if (!mHasFeature) {
624             return;
625         }
626         if (!shouldRunTelecomTest()) {
627             return;
628         }
629         try {
630             // Register phone account in parent user.
631             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
632                     "testRegisterPhoneAccount",
633                     mParentUserId));
634             // The phone account should not be visible in managed user.
635             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
636                     "testPhoneAccountNotRegistered",
637                     mProfileUserId));
638         } finally {
639             // Unregister the phone account.
640             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
641                     "testUnregisterPhoneAccount",
642                     mParentUserId));
643         }
644 
645         try {
646             // Register phone account in profile user.
647             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
648                     "testRegisterPhoneAccount",
649                     mProfileUserId));
650             // The phone account should not be visible in parent user.
651             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
652                     "testPhoneAccountNotRegistered",
653                     mParentUserId));
654         } finally {
655             // Unregister the phone account.
656             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
657                     "testUnregisterPhoneAccount",
658                     mProfileUserId));
659         }
660     }
661 
testManagedCall()662     public void testManagedCall() throws Exception {
663         if (!mHasFeature) {
664             return;
665         }
666         if (!shouldRunTelecomTest()) {
667             return;
668         }
669         // Place a outgoing call through work phone account using TelecomManager and verify the
670         // call is inserted properly.
671         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
672                 "testOutgoingCallUsingTelecomManager",
673                 mProfileUserId));
674         // Make sure the call is not inserted into parent user.
675         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
676                 "testEnsureCallNotInserted",
677                 mParentUserId));
678 
679         // Place a outgoing call through work phone account using ACTION_CALL and verify the call
680         // is inserted properly.
681         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
682                 "testOutgoingCallUsingActionCall",
683                 mProfileUserId));
684         // Make sure the call is not inserted into parent user.
685         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
686                 "testEnsureCallNotInserted",
687                 mParentUserId));
688 
689         // Add an incoming call with parent user's phone account and verify the call is inserted
690         // properly.
691         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
692                 "testIncomingCall",
693                 mProfileUserId));
694         // Make sure the call is not inserted into parent user.
695         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PhoneAccountTest",
696                 "testEnsureCallNotInserted",
697                 mParentUserId));
698     }
699 
testTrustAgentInfo()700     public void testTrustAgentInfo() throws Exception {
701         if (!mHasFeature) {
702             return;
703         }
704         // Set and get trust agent config using child dpm instance.
705         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".TrustAgentInfoTest",
706                 "testSetAndGetTrustAgentConfiguration_child",
707                 mProfileUserId));
708         // Set and get trust agent config using parent dpm instance.
709         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".TrustAgentInfoTest",
710                 "testSetAndGetTrustAgentConfiguration_parent",
711                 mProfileUserId));
712         // Unified case
713         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".TrustAgentInfoTest",
714                 "testSetTrustAgentConfiguration_bothHaveTrustAgentConfigAndUnified",
715                 mProfileUserId));
716         // Non-unified case, this test must run last because we have no way to clear work side
717         // password.
718         assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".TrustAgentInfoTest",
719                 "testSetTrustAgentConfiguration_bothHaveTrustAgentConfigAndNonUnified",
720                 mProfileUserId));
721     }
722 
disableActivityForUser(String activityName, int userId)723     private void disableActivityForUser(String activityName, int userId)
724             throws DeviceNotAvailableException {
725         String command = "am start -W --user " + userId
726                 + " --es extra-package " + MANAGED_PROFILE_PKG
727                 + " --es extra-class-name " + MANAGED_PROFILE_PKG + "." + activityName
728                 + " " + MANAGED_PROFILE_PKG + "/.ComponentDisablingActivity ";
729         CLog.d("Output for command " + command + ": "
730                 + getDevice().executeShellCommand(command));
731     }
732 
changeUserRestrictionForUser(String key, String command, int userId)733     private String changeUserRestrictionForUser(String key, String command, int userId)
734             throws DeviceNotAvailableException {
735         String adbCommand = "am start -W --user " + userId
736                 + " -c android.intent.category.DEFAULT "
737                 + " --es extra-command " + command
738                 + " --es extra-restriction-key " + key
739                 + " " + MANAGED_PROFILE_PKG + "/.SetPolicyActivity";
740         // Don't log output because sometimes used expecting failures.
741         CLog.d("Running command " + adbCommand);
742         String commandOutput = getDevice().executeShellCommand(adbCommand);
743         return commandOutput;
744     }
745 
changeCrossProfileWidgetForUser(String packageName, String command, int userId)746     private String changeCrossProfileWidgetForUser(String packageName, String command, int userId)
747             throws DeviceNotAvailableException {
748         String adbCommand = "am start -W --user " + userId
749                 + " -c android.intent.category.DEFAULT "
750                 + " --es extra-command " + command
751                 + " --es extra-package-name " + packageName
752                 + " " + MANAGED_PROFILE_PKG + "/.SetPolicyActivity";
753         String commandOutput = getDevice().executeShellCommand(adbCommand);
754         CLog.d("Output for command " + adbCommand + ": " + commandOutput);
755         return commandOutput;
756     }
757 
758     // status should be one of never, undefined, ask, always
changeVerificationStatus(int userId, String packageName, String status)759     private void changeVerificationStatus(int userId, String packageName, String status)
760             throws DeviceNotAvailableException {
761         String command = "pm set-app-link --user " + userId + " " + packageName + " " + status;
762         CLog.d("Output for command " + command + ": "
763                 + getDevice().executeShellCommand(command));
764     }
765 
startWidgetHostService()766     protected void startWidgetHostService() throws Exception {
767         String command = "am startservice --user " + mParentUserId
768                 + " -a " + WIDGET_PROVIDER_PKG + ".REGISTER_CALLBACK "
769                 + "--ei user-extra " + getUserSerialNumber(mProfileUserId)
770                 + " " + WIDGET_PROVIDER_PKG + "/.SimpleAppWidgetHostService";
771         CLog.d("Output for command " + command + ": "
772               + getDevice().executeShellCommand(command));
773     }
774 
assertAppLinkResult(String methodName)775     private void assertAppLinkResult(String methodName) throws DeviceNotAvailableException {
776         assertTrue(runDeviceTestsAsUser(INTENT_SENDER_PKG, ".AppLinkTest", methodName,
777                 mProfileUserId));
778     }
779 
shouldRunTelecomTest()780     private boolean shouldRunTelecomTest() throws DeviceNotAvailableException {
781         return hasDeviceFeature(FEATURE_TELEPHONY) && hasDeviceFeature(FEATURE_CONNECTION_SERVICE);
782     }
783 
runManagedContactsTest(Callable<Void> callable)784     private void runManagedContactsTest(Callable<Void> callable) throws Exception {
785         if (!mHasFeature) {
786             return;
787         }
788 
789         try {
790             // Allow cross profile contacts search.
791             // TODO test both on and off.
792             getDevice().executeShellCommand(
793                     "settings put --user " + mProfileUserId
794                     + " secure managed_profile_contact_remote_search 1");
795 
796             // Add test account
797             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".ContactsTest",
798                     "testAddTestAccount", mParentUserId));
799             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".ContactsTest",
800                     "testAddTestAccount", mProfileUserId));
801 
802             // Install directory provider to both primary and managed profile
803             installAppAsUser(DIRECTORY_PROVIDER_APK, mProfileUserId);
804             installAppAsUser(DIRECTORY_PROVIDER_APK, mParentUserId);
805             setDirectoryPrefix(PRIMARY_DIRECTORY_PREFIX, mParentUserId);
806             setDirectoryPrefix(MANAGED_DIRECTORY_PREFIX, mProfileUserId);
807 
808             // Check enterprise directory API works
809             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".ContactsTest",
810                     "testGetDirectoryListInPrimaryProfile", mParentUserId));
811 
812             // Insert Primary profile Contacts
813             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".ContactsTest",
814                     "testPrimaryProfilePhoneAndEmailLookup_insertedAndfound", mParentUserId));
815             // Insert Managed profile Contacts
816             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".ContactsTest",
817                     "testManagedProfilePhoneAndEmailLookup_insertedAndfound", mProfileUserId));
818             // Insert a primary contact with same phone & email as other
819             // enterprise contacts
820             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".ContactsTest",
821                     "testPrimaryProfileDuplicatedPhoneEmailContact_insertedAndfound",
822                     mParentUserId));
823             // Insert a enterprise contact with same phone & email as other
824             // primary contacts
825             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".ContactsTest",
826                     "testManagedProfileDuplicatedPhoneEmailContact_insertedAndfound",
827                     mProfileUserId));
828 
829             callable.call();
830 
831         } finally {
832             // Clean up in managed profile and primary profile
833             runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".ContactsTest",
834                     "testCurrentProfileContacts_removeContacts", mProfileUserId);
835             runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".ContactsTest",
836                     "testCurrentProfileContacts_removeContacts", mParentUserId);
837             getDevice().uninstallPackage(DIRECTORY_PROVIDER_PKG);
838         }
839     }
840 
841 
842     /*
843      * Container for running ContactsTest under multi-user environment
844      */
845     private static class ContactsTestSet {
846 
847         private ManagedProfileTest mManagedProfileTest;
848         private String mManagedProfilePackage;
849         private int mParentUserId;
850         private int mProfileUserId;
851 
ContactsTestSet(ManagedProfileTest managedProfileTest, String managedProfilePackage, int parentUserId, int profileUserId)852         public ContactsTestSet(ManagedProfileTest managedProfileTest, String managedProfilePackage,
853                 int parentUserId, int profileUserId) {
854             mManagedProfileTest = managedProfileTest;
855             mManagedProfilePackage = managedProfilePackage;
856             mParentUserId = parentUserId;
857             mProfileUserId = profileUserId;
858         }
859 
runDeviceTestsAsUser(String pkgName, String testClassName, String testMethodName, Integer userId)860         private boolean runDeviceTestsAsUser(String pkgName, String testClassName,
861                 String testMethodName, Integer userId) throws DeviceNotAvailableException {
862             return mManagedProfileTest.runDeviceTestsAsUser(pkgName, testClassName, testMethodName,
863                     userId);
864         }
865 
866         // Enable / Disable cross profile caller id
setCallerIdEnabled(boolean enabled)867         public void setCallerIdEnabled(boolean enabled) throws DeviceNotAvailableException {
868             if (enabled) {
869                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
870                         "testSetCrossProfileCallerIdDisabled_false", mProfileUserId));
871             } else {
872                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
873                         "testSetCrossProfileCallerIdDisabled_true", mProfileUserId));
874             }
875         }
876 
877         // Enable / Disable cross profile contacts search
setContactsSearchEnabled(boolean enabled)878         public void setContactsSearchEnabled(boolean enabled) throws DeviceNotAvailableException {
879             if (enabled) {
880                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
881                         "testSetCrossProfileContactsSearchDisabled_false", mProfileUserId));
882             } else {
883                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
884                         "testSetCrossProfileContactsSearchDisabled_true", mProfileUserId));
885             }
886         }
887 
checkIfCanLookupEnterpriseContacts(boolean expected)888         public void checkIfCanLookupEnterpriseContacts(boolean expected)
889                 throws DeviceNotAvailableException {
890             // Primary user cannot use ordinary phone/email lookup api to access
891             // managed contacts
892             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
893                     "testPrimaryProfilePhoneLookup_canNotAccessEnterpriseContact", mParentUserId));
894             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
895                     "testPrimaryProfileEmailLookup_canNotAccessEnterpriseContact", mParentUserId));
896             // Primary user can use ENTERPRISE_CONTENT_FILTER_URI to access
897             // primary contacts
898             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
899                     "testPrimaryProfileEnterprisePhoneLookup_canAccessPrimaryContact",
900                     mParentUserId));
901             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
902                     "testPrimaryProfileEnterpriseEmailLookup_canAccessPrimaryContact",
903                     mParentUserId));
904             // When there exist contacts with the same phone/email in primary &
905             // enterprise,
906             // primary user can use ENTERPRISE_CONTENT_FILTER_URI to access the
907             // primary contact.
908             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
909                     "testPrimaryProfileEnterpriseEmailLookupDuplicated_canAccessPrimaryContact",
910                     mParentUserId));
911             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
912                     "testPrimaryProfileEnterprisePhoneLookupDuplicated_canAccessPrimaryContact",
913                     mParentUserId));
914 
915             // Managed user cannot use ordinary phone/email lookup api to access
916             // primary contacts
917             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
918                     "testManagedProfilePhoneLookup_canNotAccessPrimaryContact", mProfileUserId));
919             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
920                     "testManagedProfileEmailLookup_canNotAccessPrimaryContact", mProfileUserId));
921             // Managed user can use ENTERPRISE_CONTENT_FILTER_URI to access
922             // enterprise contacts
923             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
924                     "testManagedProfileEnterprisePhoneLookup_canAccessEnterpriseContact",
925                     mProfileUserId));
926             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
927                     "testManagedProfileEnterpriseEmailLookup_canAccessEnterpriseContact",
928                     mProfileUserId));
929             // Managed user cannot use ENTERPRISE_CONTENT_FILTER_URI to access
930             // primary contacts
931             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
932                     "testManagedProfileEnterprisePhoneLookup_canNotAccessPrimaryContact",
933                     mProfileUserId));
934             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
935                     "testManagedProfileEnterpriseEmailLookup_canNotAccessPrimaryContact",
936                     mProfileUserId));
937             // When there exist contacts with the same phone/email in primary &
938             // enterprise,
939             // managed user can use ENTERPRISE_CONTENT_FILTER_URI to access the
940             // enterprise contact.
941             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
942                     "testManagedProfileEnterpriseEmailLookupDuplicated_canAccessEnterpriseContact",
943                     mProfileUserId));
944             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
945                     "testManagedProfileEnterprisePhoneLookupDuplicated_canAccessEnterpriseContact",
946                     mProfileUserId));
947 
948             // Check if phone lookup can access primary directories
949             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
950                     "testPrimaryProfileEnterprisePhoneLookup_canAccessPrimaryDirectories",
951                     mParentUserId));
952 
953             // Check if email lookup can access primary directories
954             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
955                     "testPrimaryProfileEnterpriseEmailLookup_canAccessPrimaryDirectories",
956                     mParentUserId));
957 
958             if (expected) {
959                 // Primary user can use ENTERPRISE_CONTENT_FILTER_URI to access
960                 // managed profile contacts
961                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
962                         "testPrimaryProfileEnterprisePhoneLookup_canAccessEnterpriseContact",
963                         mParentUserId));
964                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
965                         "testPrimaryProfileEnterpriseEmailLookup_canAccessEnterpriseContact",
966                         mParentUserId));
967 
968                 // Make sure SIP enterprise lookup works too.
969                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
970                         "testPrimaryProfileEnterpriseSipLookup_canAccessEnterpriseContact",
971                         mParentUserId));
972 
973                 // Check if phone lookup can access enterprise directories
974                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
975                         "testPrimaryProfileEnterprisePhoneLookup_canAccessManagedDirectories",
976                         mParentUserId));
977 
978                 // Check if email lookup can access enterprise directories
979                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
980                         "testPrimaryProfileEnterpriseEmailLookup_canAccessManagedDirectories",
981                         mParentUserId));
982             } else {
983                 // Primary user cannot use ENTERPRISE_CONTENT_FILTER_URI to
984                 // access managed contacts
985                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
986                         "testPrimaryProfileEnterprisePhoneLookup_canNotAccessEnterpriseContact",
987                         mParentUserId));
988                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
989                         "testPrimaryProfileEnterprisePhoneLookup_canNotAccessManagedDirectories",
990                         mParentUserId));
991 
992                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
993                         "testPrimaryProfileEnterpriseEmailLookup_canNotAccessManagedDirectories",
994                         mParentUserId));
995                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
996                         "testPrimaryProfileEnterprisePhoneLookup_canNotAccessManagedDirectories",
997                         mParentUserId));
998             }
999         }
1000 
checkIfCanFilterSelfContacts()1001         public void checkIfCanFilterSelfContacts() throws DeviceNotAvailableException {
1002             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
1003                     "testPrimaryProfileEnterpriseCallableFilter_canAccessPrimaryDirectories",
1004                     mParentUserId));
1005             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
1006                     "testManagedProfileEnterpriseCallableFilter_canAccessManagedDirectories",
1007                     mProfileUserId));
1008 
1009             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
1010                     "testPrimaryProfileEnterpriseEmailFilter_canAccessPrimaryDirectories",
1011                     mParentUserId));
1012             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
1013                     "testEnterpriseProfileEnterpriseEmailFilter_canAccessManagedDirectories",
1014                     mProfileUserId));
1015 
1016             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
1017                     "testPrimaryProfileEnterpriseContactFilter_canAccessPrimaryDirectories",
1018                     mParentUserId));
1019             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
1020                     "testManagedProfileEnterpriseContactFilter_canAccessManagedDirectories",
1021                     mProfileUserId));
1022 
1023             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
1024                     "testPrimaryProfileEnterprisePhoneFilter_canAccessPrimaryDirectories",
1025                     mParentUserId));
1026             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
1027                     "testManagedProfileEnterprisePhoneFilter_canAccessManagedDirectories",
1028                     mProfileUserId));
1029         }
1030 
checkIfCanFilterEnterpriseContacts(boolean expected)1031         public void checkIfCanFilterEnterpriseContacts(boolean expected)
1032                 throws DeviceNotAvailableException {
1033             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
1034                     "testFilterUriWhenDirectoryParamMissing", mParentUserId));
1035             if (expected) {
1036                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
1037                         "testPrimaryProfileEnterpriseCallableFilter_canAccessManagedDirectories",
1038                         mParentUserId));
1039                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
1040                         "testPrimaryProfileEnterpriseEmailFilter_canAccessManagedDirectories",
1041                         mParentUserId));
1042                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
1043                         "testPrimaryProfileEnterpriseContactFilter_canAccessManagedDirectories",
1044                         mParentUserId));
1045                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
1046                         "testPrimaryProfileEnterprisePhoneFilter_canAccessManagedDirectories",
1047                         mParentUserId));
1048             } else {
1049                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
1050                         "testPrimaryProfileEnterpriseCallableFilter_canNotAccessManagedDirectories",
1051                         mParentUserId));
1052                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
1053                         "testPrimaryProfileEnterpriseEmailFilter_canNotAccessManagedDirectories",
1054                         mParentUserId));
1055                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
1056                         "testPrimaryProfileEnterpriseContactFilter_canNotAccessManagedDirectories",
1057                         mParentUserId));
1058                 assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
1059                         "testPrimaryProfileEnterprisePhoneFilter_canNotAccessManagedDirectories",
1060                         mParentUserId));
1061             }
1062         }
1063 
checkIfNoEnterpriseDirectoryFound()1064         public void checkIfNoEnterpriseDirectoryFound() throws DeviceNotAvailableException {
1065             assertTrue(runDeviceTestsAsUser(mManagedProfilePackage, ".ContactsTest",
1066                     "testPrimaryProfileEnterpriseDirectories_canNotAccessManagedDirectories",
1067                     mParentUserId));
1068         }
1069     }
1070 }
1071