Add CTS for Enterprise Email Lookup with directory id

Bug: 26537067
Change-Id: I7b5ca2cfb15cfd3c59819373e86ef1b82de6b651
diff --git a/hostsidetests/devicepolicy/app/ContactDirectoryProvider/src/com/android/cts/contactdirectoryprovider/DirectoryProvider.java b/hostsidetests/devicepolicy/app/ContactDirectoryProvider/src/com/android/cts/contactdirectoryprovider/DirectoryProvider.java
index f4faa13..8e040b7 100644
--- a/hostsidetests/devicepolicy/app/ContactDirectoryProvider/src/com/android/cts/contactdirectoryprovider/DirectoryProvider.java
+++ b/hostsidetests/devicepolicy/app/ContactDirectoryProvider/src/com/android/cts/contactdirectoryprovider/DirectoryProvider.java
@@ -47,6 +47,7 @@
     private static final int GAL_PHONE_FILTER = GAL_BASE + 5;
     private static final int GAL_PHONE_LOOKUP = GAL_BASE + 6;
     private static final int GAL_CALLABLES_FILTER = GAL_BASE + 7;
+    private static final int GAL_EMAIL_LOOKUP = GAL_BASE + 8;
 
     private final UriMatcher mURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
 
@@ -62,6 +63,7 @@
         mURIMatcher.addURI(AUTHORITY, "data/phones/filter/*", GAL_PHONE_FILTER);
         mURIMatcher.addURI(AUTHORITY, "phone_lookup/*", GAL_PHONE_LOOKUP);
         mURIMatcher.addURI(AUTHORITY, "data/callables/filter/*", GAL_CALLABLES_FILTER);
+        mURIMatcher.addURI(AUTHORITY, "data/emails/lookup/*", GAL_EMAIL_LOOKUP);
         mSharedPrefs = getContext().getSharedPreferences(CONFIG_NAME, Context.MODE_PRIVATE);
         return true;
     }
@@ -101,7 +103,8 @@
             case GAL_EMAIL_FILTER:
             case GAL_PHONE_FILTER:
             case GAL_PHONE_LOOKUP:
-            case GAL_CALLABLES_FILTER: {
+            case GAL_CALLABLES_FILTER:
+            case GAL_EMAIL_LOOKUP: {
                 // TODO: Add all CTS tests for these APIs
                 final MatrixCursor cursor = new MatrixCursor(projection);
                 final Object[] row = new Object[projection.length];
diff --git a/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/ContactsTest.java b/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/ContactsTest.java
index b7cefd5..d116f24 100644
--- a/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/ContactsTest.java
+++ b/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/ContactsTest.java
@@ -465,6 +465,44 @@
         assertTrue(hasManagedDirectory);
     }
 
+    public void testPrimaryProfileEnterpriseEmailLookup_canAccessPrimaryDirectories() {
+        assertFalse(isManagedProfile());
+
+        // local directory
+        final ContactInfo defaultContactInfo
+                = getContactInfoFromEnterpriseEmailLookupUriInDirectory(PRIMARY_CONTACT_EMAIL,
+                Directory.DEFAULT);
+        assertNotNull(defaultContactInfo);
+        assertEquals(PRIMARY_CONTACT_DISPLAY_NAME, defaultContactInfo.displayName);
+
+        // remote directory
+        final long directoryId = getPrimaryRemoteDirectoryId();
+        final ContactInfo directoryContactInfo
+                = getContactInfoFromEnterpriseEmailLookupUriInDirectory(PRIMARY_CONTACT_EMAIL,
+                directoryId);
+        assertNotNull(directoryContactInfo);
+        assertEquals(PRIMARY_DIRECTORY_CONTACT_NAME, directoryContactInfo.displayName);
+    }
+
+    public void testPrimaryProfileEnterpriseEmailLookup_canAccessManagedDirectories() {
+        assertFalse(isManagedProfile());
+
+        // local directory
+        final ContactInfo defaultContactInfo
+                = getContactInfoFromEnterpriseEmailLookupUriInDirectory(MANAGED_CONTACT_EMAIL,
+                Directory.ENTERPRISE_DEFAULT);
+        assertNotNull(defaultContactInfo);
+        assertEquals(MANAGED_CONTACT_DISPLAY_NAME, defaultContactInfo.displayName);
+
+        // remote directory
+        final long directoryId = getEnterpriseRemoteDirectoryId();
+        final ContactInfo directoryContactInfo
+                = getContactInfoFromEnterpriseEmailLookupUriInDirectory(MANAGED_CONTACT_EMAIL,
+                directoryId);
+        assertNotNull(directoryContactInfo);
+        assertEquals(MANAGED_DIRECTORY_CONTACT_NAME, directoryContactInfo.displayName);
+    }
+
     public void testPrimaryProfileEnterprisePhoneLookup_canAccessPrimaryDirectories() {
         assertFalse(isManagedProfile());
 
@@ -817,6 +855,15 @@
                 Email.PHOTO_URI, Email.PHOTO_THUMBNAIL_URI, Email.PHOTO_ID);
     }
 
+    private ContactInfo getContactInfoFromEnterpriseEmailLookupUriInDirectory(String email,
+            long directoryId) {
+        Uri uri = Email.ENTERPRISE_CONTENT_LOOKUP_URI.buildUpon().appendPath(email)
+                .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY,
+                        String.valueOf(directoryId)).build();
+        return getContactInfoFromUri(uri, Email.CONTACT_ID, Email.DISPLAY_NAME_PRIMARY,
+                Email.PHOTO_URI, Email.PHOTO_THUMBNAIL_URI, Email.PHOTO_ID);
+    }
+
     private ContactInfo getContactInfoFromEnterpriseCallableFilterUriInDirectory(String filter,
             long directoryId) {
         final Uri uri = Uri.withAppendedPath(Callable.ENTERPRISE_CONTENT_FILTER_URI, filter)
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java
index 63f7f2f..4664138 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java
@@ -554,6 +554,16 @@
                     "testPrimaryProfileEnterprisePhoneLookup_canAccessManagedDirectories",
                     mParentUserId));
 
+            // Check if email lookup can access primary directories
+            assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".ContactsTest",
+                    "testPrimaryProfileEnterpriseEmailLookup_canAccessPrimaryDirectories",
+                    mParentUserId));
+
+            // Check if email lookup can access enterprise directories
+            assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".ContactsTest",
+                    "testPrimaryProfileEnterpriseEmailLookup_canAccessManagedDirectories",
+                    mParentUserId));
+
             assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".ContactsTest",
                     "testPrimaryProfileEnterpriseCallableFilter_canAccessPrimaryDirectories",
                     mParentUserId));