diff options
Diffstat (limited to 'tests/src/com/android/providers/contacts/enterprise/EnterprisePolicyGuardTest.java')
-rw-r--r-- | tests/src/com/android/providers/contacts/enterprise/EnterprisePolicyGuardTest.java | 69 |
1 files changed, 60 insertions, 9 deletions
diff --git a/tests/src/com/android/providers/contacts/enterprise/EnterprisePolicyGuardTest.java b/tests/src/com/android/providers/contacts/enterprise/EnterprisePolicyGuardTest.java index 2e5241fb..5aedd8d0 100644 --- a/tests/src/com/android/providers/contacts/enterprise/EnterprisePolicyGuardTest.java +++ b/tests/src/com/android/providers/contacts/enterprise/EnterprisePolicyGuardTest.java @@ -17,6 +17,7 @@ package com.android.providers.contacts.enterprise; import android.app.admin.DevicePolicyManager; import android.content.Context; +import android.content.pm.PackageManager; import android.content.pm.UserInfo; import android.net.Uri; import android.os.UserHandle; @@ -31,6 +32,9 @@ import org.mockito.Matchers; import java.util.Arrays; import java.util.List; +import static android.Manifest.permission.INTERACT_ACROSS_USERS; +import static android.content.pm.PackageManager.PERMISSION_GRANTED; + import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -49,6 +53,7 @@ public class EnterprisePolicyGuardTest extends FixedAndroidTestCase { private static final String CONTACT_EMAIL = "david.green@android.com"; private static final String CONTACT_PHONE = "+1234567890"; private static final long DIRECTORY_ID = Directory.ENTERPRISE_DEFAULT; + private static final String CALLING_PACKAGE = "package"; private static final Uri URI_CONTACTS_ID_PHOTO = Uri.parse("content://com.android.contacts/contacts/" + CONTACT_ID + "/photo"); @@ -179,6 +184,32 @@ public class EnterprisePolicyGuardTest extends FixedAndroidTestCase { checkCrossProfile(guard, URI_CONTACTS_ID_PHOTO, false); checkCrossProfile(guard, URI_CONTACTS_ID_DISPLAY_PHOTO, false); checkCrossProfile(guard, URI_OTHER, false); + + // ManagedProfile is paused + context = getMockContext(true, true, false); + guard = new EnterprisePolicyGuardTestable(context, true); + checkCrossProfile(guard, appendRemoteDirectoryId(URI_PHONE_LOOKUP), false); + checkCrossProfile(guard, appendRemoteDirectoryId(URI_EMAILS_LOOKUP), false); + checkCrossProfile(guard, appendRemoteDirectoryId(URI_CONTACTS_FILTER), false); + checkCrossProfile(guard, appendRemoteDirectoryId(URI_PHONES_FILTER), false); + checkCrossProfile(guard, appendRemoteDirectoryId(URI_CALLABLES_FILTER), false); + checkCrossProfile(guard, appendRemoteDirectoryId(URI_EMAILS_FILTER), false); + checkCrossProfile(guard, URI_DIRECTORY_FILE, false); + + // Always allow uri with no directory support. + checkCrossProfile(guard, URI_DIRECTORIES, true); + checkCrossProfile(guard, URI_DIRECTORIES_ID, true); + checkCrossProfile(guard, URI_CONTACTS_ID_PHOTO, true); + checkCrossProfile(guard, URI_CONTACTS_ID_DISPLAY_PHOTO, true); + checkCrossProfile(guard, URI_OTHER, false); + + // Always allow uri with no remote directory id. + checkCrossProfile(guard, URI_PHONE_LOOKUP, true); + checkCrossProfile(guard, URI_EMAILS_LOOKUP, true); + checkCrossProfile(guard, URI_CONTACTS_FILTER, true); + checkCrossProfile(guard, URI_PHONES_FILTER, true); + checkCrossProfile(guard, URI_CALLABLES_FILTER, true); + checkCrossProfile(guard, URI_EMAILS_FILTER, true); } public void testCrossProfile_userSettingOff() { @@ -210,6 +241,7 @@ public class EnterprisePolicyGuardTest extends FixedAndroidTestCase { checkCrossProfile(guard, URI_EMAILS_FILTER, true); } + private static Uri appendRemoteDirectoryId(Uri uri) { return appendDirectoryId(uri, REMOTE_DIRECTORY_ID); } @@ -231,13 +263,13 @@ public class EnterprisePolicyGuardTest extends FixedAndroidTestCase { } } - private static void checkCrossProfile(EnterprisePolicyGuard guard, Uri uri, boolean expected) { + private void checkCrossProfile(EnterprisePolicyGuard guard, Uri uri, boolean expected) { if (expected) { assertTrue("Expected true but got false for uri: " + uri, - guard.isCrossProfileAllowed(uri)); + guard.isCrossProfileAllowed(uri, CALLING_PACKAGE)); } else { assertFalse("Expected false but got true for uri: " + uri, - guard.isCrossProfileAllowed(uri)); + guard.isCrossProfileAllowed(uri, CALLING_PACKAGE)); } } @@ -256,11 +288,16 @@ public class EnterprisePolicyGuardTest extends FixedAndroidTestCase { private Context getMockContext(boolean isCallerIdEnabled, boolean isContactsSearchEnabled) { + return getMockContext(isCallerIdEnabled, isContactsSearchEnabled, true); + } + + private Context getMockContext(boolean isCallerIdEnabled, boolean isContactsSearchEnabled, + boolean isManagedProfileEnabled) { DevicePolicyManager mockDpm = mock(DevicePolicyManager.class); - when(mockDpm.getCrossProfileCallerIdDisabled(Matchers.<UserHandle>any())) - .thenReturn(!isCallerIdEnabled); - when(mockDpm.getCrossProfileContactsSearchDisabled(Matchers.<UserHandle>any())) - .thenReturn(!isContactsSearchEnabled); + when(mockDpm.hasManagedProfileCallerIdAccess(Matchers.any(),Matchers.any())) + .thenReturn(isCallerIdEnabled); + when(mockDpm.hasManagedProfileContactsAccess(Matchers.any(),Matchers.any())) + .thenReturn(isContactsSearchEnabled); List<UserInfo> userInfos = MANAGED_USERINFO_LIST; UserManager mockUm = mock(UserManager.class); @@ -268,8 +305,14 @@ public class EnterprisePolicyGuardTest extends FixedAndroidTestCase { when(mockUm.getUsers()).thenReturn(userInfos); when(mockUm.getProfiles(Matchers.anyInt())).thenReturn(userInfos); when(mockUm.getProfileParent(WORK_USER_ID)).thenReturn(CURRENT_USER_INFO); + when(mockUm.isQuietModeEnabled(UserHandle.of(WORK_USER_ID))) + .thenReturn(!isManagedProfileEnabled); - Context mockContext = new TestMockContext(getContext(), mockDpm, mockUm); + PackageManager mockPm = mock(PackageManager.class); + when(mockPm.checkPermission(INTERACT_ACROSS_USERS, CALLING_PACKAGE)) + .thenReturn(PERMISSION_GRANTED); + + Context mockContext = new TestMockContext(getContext(), mockDpm, mockUm, mockPm); return mockContext; } @@ -278,11 +321,19 @@ public class EnterprisePolicyGuardTest extends FixedAndroidTestCase { private Context mRealContext; private DevicePolicyManager mDpm; private UserManager mUm; + private PackageManager mPm; - public TestMockContext(Context realContext, DevicePolicyManager dpm, UserManager um) { + public TestMockContext( + Context realContext, DevicePolicyManager dpm, UserManager um, PackageManager pm) { mRealContext = realContext; mDpm = dpm; mUm = um; + mPm = pm; + } + + @Override + public PackageManager getPackageManager() { + return mPm; } public Object getSystemService(String name) { |