Add test for EnterpriseGuard to test against the newly added user setting
am: a27f51a485

* commit 'a27f51a4850ae800fde5952a658f4882bd5a550b':
  Add test for EnterpriseGuard to test against the newly added user setting

Change-Id: If1ad40b0290b63047a2919f55d8559902dd4c440
diff --git a/src/com/android/providers/contacts/enterprise/EnterprisePolicyGuard.java b/src/com/android/providers/contacts/enterprise/EnterprisePolicyGuard.java
index 7fe37ee..4684105 100644
--- a/src/com/android/providers/contacts/enterprise/EnterprisePolicyGuard.java
+++ b/src/com/android/providers/contacts/enterprise/EnterprisePolicyGuard.java
@@ -72,10 +72,7 @@
                 !mDpm.getCrossProfileContactsSearchDisabled(currentHandle);
         final boolean isBluetoothContactSharingEnabled =
                 !mDpm.getBluetoothContactSharingDisabled(currentHandle);
-        final boolean isContactRemoteSearchUserEnabled =
-                Settings.Secure.getInt(
-                        mContext.getContentResolver(),
-                        MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 0) == 1;
+        final boolean isContactRemoteSearchUserEnabled = isContactRemoteSearchUserSettingEnabled();
 
         final String directory = uri.getQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY);
 
@@ -205,4 +202,10 @@
                 return false;
         }
     }
+
+    protected boolean isContactRemoteSearchUserSettingEnabled() {
+        return Settings.Secure.getInt(
+                mContext.getContentResolver(),
+                MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 0) == 1;
+    }
 }
diff --git a/tests/src/com/android/providers/contacts/enterprise/EnterprisePolicyGuardTest.java b/tests/src/com/android/providers/contacts/enterprise/EnterprisePolicyGuardTest.java
index 505ce21..99ff9a1 100644
--- a/tests/src/com/android/providers/contacts/enterprise/EnterprisePolicyGuardTest.java
+++ b/tests/src/com/android/providers/contacts/enterprise/EnterprisePolicyGuardTest.java
@@ -16,31 +16,25 @@
 package com.android.providers.contacts.enterprise;
 
 import android.app.admin.DevicePolicyManager;
+import android.content.ContentResolver;
 import android.content.Context;
 import android.content.pm.UserInfo;
 import android.net.Uri;
 import android.os.UserHandle;
 import android.os.UserManager;
+import android.provider.ContactsContract;
 import android.provider.ContactsContract.Directory;
 import android.test.AndroidTestCase;
 import android.test.mock.MockContext;
 import android.test.suitebuilder.annotation.SmallTest;
 
-import java.lang.reflect.Method;
+import org.mockito.Matchers;
+
 import java.util.Arrays;
 import java.util.List;
 
-import org.mockito.Matchers;
-import org.mockito.stubbing.Answer;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.MockitoAnnotations;
-
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.doAnswer;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.verify;
 
 
 /**
@@ -84,6 +78,10 @@
     private static final Uri URI_OTHER =
             Uri.parse("content://com.android.contacts/contacts/" + CONTACT_ID);
 
+    // Please notice that the directory id should be < ENTERPRISE_BASE because the id should be
+    // substracted before passing to enterprise side.
+    private static final int REMOTE_DIRECTORY_ID = 10;
+
     @Override
     protected void setUp() throws Exception {
         super.setUp();
@@ -98,7 +96,7 @@
     }
 
     public void testDirectorySupport() {
-        EnterprisePolicyGuard guard = new EnterprisePolicyGuard(getContext());
+        EnterprisePolicyGuard guard = new EnterprisePolicyGuardTestable(getContext(), true);
         checkDirectorySupport(guard, URI_PHONE_LOOKUP, true);
         checkDirectorySupport(guard, URI_EMAILS_LOOKUP, true);
         checkDirectorySupport(guard, URI_CONTACTS_FILTER, true);
@@ -114,13 +112,12 @@
     }
 
 
-    public void testCrossProfile() {
+    public void testCrossProfile_userSettingOn() {
         Context context;
         EnterprisePolicyGuard guard;
-
         // All enabled.
         context = getMockContext(true, true);
-        guard = new EnterprisePolicyGuard(context);
+        guard = new EnterprisePolicyGuardTestable(context, true);
         checkCrossProfile(guard, URI_PHONE_LOOKUP, true);
         checkCrossProfile(guard, URI_EMAILS_LOOKUP, true);
         checkCrossProfile(guard, URI_CONTACTS_FILTER, true);
@@ -136,7 +133,7 @@
 
         // Only ContactsSearch is disabled
         context = getMockContext(true, /* isContactsSearchEnabled= */ false);
-        guard = new EnterprisePolicyGuard(context);
+        guard = new EnterprisePolicyGuardTestable(context, true);
         checkCrossProfile(guard, URI_PHONE_LOOKUP, true);
         checkCrossProfile(guard, URI_EMAILS_LOOKUP, true);
         checkCrossProfile(guard, URI_CONTACTS_FILTER, false);
@@ -152,7 +149,7 @@
 
         // Only CallerId is disabled
         context = getMockContext(/* isCallerIdEnabled= */ false, true);
-        guard = new EnterprisePolicyGuard(context);
+        guard = new EnterprisePolicyGuardTestable(context, true);
         checkCrossProfile(guard, URI_PHONE_LOOKUP, false);
         checkCrossProfile(guard, URI_EMAILS_LOOKUP, false);
         checkCrossProfile(guard, URI_CONTACTS_FILTER, true);
@@ -168,8 +165,8 @@
 
         // CallerId and ContactsSearch are disabled
         context = getMockContext(/* isCallerIdEnabled= */ false,
-                /* isContactsSearchEnabled= */ false);
-        guard = new EnterprisePolicyGuard(context);
+            /* isContactsSearchEnabled= */ false);
+        guard = new EnterprisePolicyGuardTestable(context, true);
         checkCrossProfile(guard, URI_PHONE_LOOKUP, false);
         checkCrossProfile(guard, URI_EMAILS_LOOKUP, false);
         checkCrossProfile(guard, URI_CONTACTS_FILTER, false);
@@ -184,6 +181,45 @@
         checkCrossProfile(guard, URI_OTHER, false);
     }
 
+    public void testCrossProfile_userSettingOff() {
+        // All enabled.
+        Context context = getMockContext(true, true);
+        EnterprisePolicyGuard guard = new EnterprisePolicyGuardTestable(context, false);
+        // All directory supported Uris with remote directory id should not allowed.
+        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);
+    }
+
+    private static Uri appendRemoteDirectoryId(Uri uri) {
+        return appendDirectoryId(uri, REMOTE_DIRECTORY_ID);
+    }
+
+    private static Uri appendDirectoryId(Uri uri, int directoryId) {
+        return uri.buildUpon().appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY,
+                String.valueOf(directoryId)).build();
+    }
+
+
     private static void checkDirectorySupport(EnterprisePolicyGuard guard, Uri uri,
             boolean expected) {
         if (expected) {
@@ -264,4 +300,19 @@
         }
     }
 
+    private static class EnterprisePolicyGuardTestable extends EnterprisePolicyGuard {
+        private boolean mIsContactRemoteSearchUserSettingEnabled;
+
+        public EnterprisePolicyGuardTestable(Context context,
+                boolean isContactRemoteSearchUserSettingEnabled) {
+            super(context);
+            mIsContactRemoteSearchUserSettingEnabled = isContactRemoteSearchUserSettingEnabled;
+        }
+
+        @Override
+        protected boolean isContactRemoteSearchUserSettingEnabled() {
+            return mIsContactRemoteSearchUserSettingEnabled;
+        }
+    }
+
 }