Fix event color query (which fixes the failing CTS test).

Bug:7261590
Change-Id: I72c60af1ae8d563db7693fdf7d87972dfc80ab82
diff --git a/src/com/android/providers/calendar/CalendarProvider2.java b/src/com/android/providers/calendar/CalendarProvider2.java
index 0bc80c5..9ef0e35 100644
--- a/src/com/android/providers/calendar/CalendarProvider2.java
+++ b/src/com/android/providers/calendar/CalendarProvider2.java
@@ -203,7 +203,7 @@
     private static final String SQL_WHERE_CALENDAR_COLOR = Calendars.ACCOUNT_NAME + "=? AND "
             + Calendars.ACCOUNT_TYPE + "=? AND " + Calendars.CALENDAR_COLOR_KEY + "=?";
 
-    private static final String SQL_WHERE_EVENT_COLOR = Calendars._ID + " in (SELECT _id from "
+    private static final String SQL_WHERE_EVENT_COLOR = "calendar_id in (SELECT _id from "
             + Tables.CALENDARS + " WHERE " + Events.ACCOUNT_NAME + "=? AND " + Events.ACCOUNT_TYPE
             + "=?) AND " + Events.EVENT_COLOR_KEY + "=?";
 
diff --git a/tests/src/com/android/providers/calendar/CalendarProvider2Test.java b/tests/src/com/android/providers/calendar/CalendarProvider2Test.java
index 597bf29..764585a 100644
--- a/tests/src/com/android/providers/calendar/CalendarProvider2Test.java
+++ b/tests/src/com/android/providers/calendar/CalendarProvider2Test.java
@@ -83,6 +83,8 @@
     private static final String[] WHERE_CALENDARS_ARGS = {
         "1"
     };
+    private static final String WHERE_COLOR_ACCOUNT_AND_INDEX = Colors.ACCOUNT_NAME + "=? AND "
+            + Colors.ACCOUNT_TYPE + "=? AND " + Colors.COLOR_KEY + "=?";
     private static final String DEFAULT_SORT_ORDER = "begin ASC";
 
     private CalendarProvider2ForTesting mProvider;
@@ -1315,7 +1317,19 @@
         mMetaData.clearInstanceRange();
     }
 
-    public void testInsertAndUpdateColor() throws Exception {
+    /**
+     * Creates an updated URI that includes query parameters that identify the source as a
+     * sync adapter.
+     */
+    static Uri asSyncAdapter(Uri uri, String account, String accountType) {
+        return uri.buildUpon()
+                .appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER,
+                        "true")
+                .appendQueryParameter(Calendars.ACCOUNT_NAME, account)
+                .appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType).build();
+    }
+    
+    public void testInsertUpdateDeleteColor() throws Exception {
         // Calendar Color
         long colorType = Colors.TYPE_CALENDAR;
         String colorKey = "123";
@@ -1336,7 +1350,6 @@
         // Event Color
         colorType = Colors.TYPE_EVENT;
         colorValue += 11;
-
         colorId = insertAndCheckColor(colorType, colorKey, colorValue);
         try {
             insertAndCheckColor(colorType, colorKey, colorValue);
@@ -1345,9 +1358,63 @@
             // good
         }
 
+        // Create an event with the old color value.
+        int calendarId0 = insertCal("Calendar0", DEFAULT_TIMEZONE);
+        String title = "colorTest";
+        ContentValues cv = this.eventInfoToContentValues(calendarId0, mEvents[0]);
+        cv.put(Events.EVENT_COLOR_KEY, colorKey);
+        cv.put(Events.TITLE, title);
+        Uri uri = insertEvent(calendarId0, mEvents[0], cv);
+        Cursor c = mResolver.query(uri, new String[] {Events.EVENT_COLOR},  null, null, null);
+        try {
+            // Confirm the color is set.
+            c.moveToFirst();
+            assertEquals(colorValue, c.getInt(0));
+        } finally {
+            if (c != null) {
+                c.close();
+            }
+        }
+
         // Test Update
         colorValue += 11;
         updateAndCheckColor(colorId, colorType, colorKey, colorValue);
+
+        // Check if color was updated in event.
+        c = mResolver.query(uri, new String[] {Events.EVENT_COLOR}, null, null, null);
+        try {
+            c.moveToFirst();
+            assertEquals(colorValue, c.getInt(0));
+        } finally {
+            if (c != null) {
+                c.close();
+            }
+        }
+
+        // Test Delete
+        Uri colSyncUri = asSyncAdapter(Colors.CONTENT_URI, DEFAULT_ACCOUNT,
+                DEFAULT_ACCOUNT_TYPE);
+        try {
+            // Delete should fail if color referenced by an event.
+            mResolver.delete(colSyncUri, WHERE_COLOR_ACCOUNT_AND_INDEX,
+                    new String[] {DEFAULT_ACCOUNT, DEFAULT_ACCOUNT_TYPE, colorKey});
+            fail("Should not allow deleting referenced color");
+        } catch (UnsupportedOperationException e) {
+            // Exception expected.
+        }
+        Cursor cursor = mResolver.query(Colors.CONTENT_URI, new String[] {Colors.COLOR_KEY},
+                Colors.COLOR_KEY + "=? AND " + Colors.COLOR_TYPE + "=?",
+                new String[] {colorKey, Long.toString(colorType)}, null);
+        assertEquals(1, cursor.getCount());
+
+        // Try again, by deleting the event, then the color.
+        assertEquals(1, deleteMatchingEvents(title, DEFAULT_ACCOUNT, DEFAULT_ACCOUNT_TYPE));
+        mResolver.delete(colSyncUri, WHERE_COLOR_ACCOUNT_AND_INDEX,
+                new String[] {DEFAULT_ACCOUNT, DEFAULT_ACCOUNT_TYPE, colorKey});
+        cursor = mResolver.query(Colors.CONTENT_URI, new String[] {Colors.COLOR_KEY},
+                Colors.COLOR_KEY + "=? AND " + Colors.COLOR_TYPE + "=?",
+                new String[] {colorKey, Long.toString(colorType)}, null);
+        assertEquals(0, cursor.getCount());
     }
 
     private void checkColor(long colorId, long colorType, String colorKey, long color) {
@@ -1360,7 +1427,6 @@
                 Colors._ID,          // 5
                 Colors.DATA,         // 6
         };
-
         Cursor cursor = mResolver.query(Colors.CONTENT_URI, projection, Colors.COLOR_KEY
                 + "=? AND " + Colors.COLOR_TYPE + "=?", new String[] {
                 colorKey, Long.toString(colorType)