Bug 1235599 - Add basic UrlMetadata tests r?mcomella draft
authorAndrzej Hunt <ahunt@mozilla.com>
Mon, 06 Jun 2016 13:08:55 -0700
changeset 375843 b1ed5611b0142cc0999ccf129736a80af1664ecd
parent 375842 54234c8a292d90340d07928edb3913c2f9d0cb7a
child 522983 dbd1aafb8d931325f9c33b535d1fe3d4cf0ccc92
push id20397
push userahunt@mozilla.com
push dateMon, 06 Jun 2016 20:10:13 +0000
reviewersmcomella
bugs1235599
milestone49.0a1
Bug 1235599 - Add basic UrlMetadata tests r?mcomella MozReview-Commit-ID: J7zSI7E9jb7
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testBrowserProvider.java
--- a/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testBrowserProvider.java
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testBrowserProvider.java
@@ -1,20 +1,25 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.tests;
 
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Random;
 
-import org.mozilla.gecko.background.db.CursorDumper;
 import org.mozilla.gecko.db.BrowserContract;
 import org.mozilla.gecko.db.BrowserContract.UrlAnnotations.SyncStatus;
+import org.mozilla.gecko.db.URLMetadata;
+import org.mozilla.gecko.db.URLMetadataTable;
 
 import android.content.ContentProviderOperation;
 import android.content.ContentProviderResult;
 import android.content.ContentUris;
 import android.content.ContentValues;
 import android.content.OperationApplicationException;
 import android.database.Cursor;
 import android.net.Uri;
@@ -183,16 +188,26 @@ public class testBrowserProvider extends
         ContentValues thumbnailEntry = new ContentValues();
 
         thumbnailEntry.put(BrowserContract.Thumbnails.URL, pageUrl);
         thumbnailEntry.put(BrowserContract.Thumbnails.DATA, data.getBytes("UTF8"));
 
         return thumbnailEntry;
     }
 
+    private ContentValues createUrlMetadataEntry(final String url, final String tileImage, final String tileColor,
+                final String touchIcon) {
+        final ContentValues values = new ContentValues();
+        values.put(URLMetadataTable.URL_COLUMN, url);
+        values.put(URLMetadataTable.TILE_IMAGE_URL_COLUMN, tileImage);
+        values.put(URLMetadataTable.TILE_COLOR_COLUMN, tileColor);
+        values.put(URLMetadataTable.TOUCH_ICON_COLUMN, touchIcon);
+        return values;
+    }
+
     private ContentValues createUrlAnnotationEntry(final String url, final String key, final String value,
                 final long dateCreated) {
         final ContentValues values = new ContentValues();
         values.put(BrowserContract.UrlAnnotations.URL, url);
         values.put(BrowserContract.UrlAnnotations.KEY, key);
         values.put(BrowserContract.UrlAnnotations.VALUE, value);
         values.put(BrowserContract.UrlAnnotations.DATE_CREATED, dateCreated);
         values.put(BrowserContract.UrlAnnotations.DATE_MODIFIED, dateCreated);
@@ -238,16 +253,23 @@ public class testBrowserProvider extends
 
     private Cursor getUrlAnnotationByUrl(final String url) throws Exception {
         return mProvider.query(BrowserContract.UrlAnnotations.CONTENT_URI, null,
                 BrowserContract.UrlAnnotations.URL + " = ?",
                 new String[] { url },
                 null);
     }
 
+    private Cursor getUrlMetadataByUrl(final String url) throws Exception {
+        return mProvider.query(URLMetadataTable.CONTENT_URI, null,
+                URLMetadataTable.URL_COLUMN + " = ?",
+                new String[] { url },
+                null);
+    }
+
     @Override
     public void setUp() throws Exception {
         super.setUp(sBrowserProviderCallable, BrowserContract.AUTHORITY, "browser.db");
 
         mTests.add(new TestSpecialFolders());
 
         mTests.add(new TestInsertBookmarks());
         mTests.add(new TestInsertBookmarksFavicons());
@@ -264,16 +286,17 @@ public class testBrowserProvider extends
         mTests.add(new TestUpdateHistory());
         mTests.add(new TestUpdateHistoryFavicons());
         mTests.add(new TestUpdateOrInsertHistory());
         mTests.add(new TestInsertHistoryThumbnails());
         mTests.add(new TestUpdateHistoryThumbnails());
         mTests.add(new TestDeleteHistoryThumbnails());
 
         mTests.add(new TestInsertUrlAnnotations());
+        mTests.add(new TestInsertUrlMetadata());
 
         mTests.add(new TestBatchOperations());
 
         mTests.add(new TestCombinedView());
         mTests.add(new TestCombinedViewDisplay());
         mTests.add(new TestCombinedViewWithDeletedBookmark());
 
         mTests.add(new TestBrowserProviderNotifications());
@@ -1487,16 +1510,124 @@ public class testBrowserProvider extends
                     "Inserted url annotation has correct key");
             mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.UrlAnnotations.VALUE)), value,
                     "Inserted url annotation has correct value");
             mAsserter.is(c.getInt(c.getColumnIndex(BrowserContract.UrlAnnotations.SYNC_STATUS)), SyncStatus.NEW.getDBValue(),
                     "Inserted url annotation has default sync status");
         }
     }
 
+    private class TestInsertUrlMetadata extends TestCase {
+        @Override
+        public void test() throws Exception {
+            testInsertionViaContentProvider();
+            testInsertionViaUrlMetadata();
+            testRetrievalViaUrlMetadata();
+        }
+
+        final String url1 = "http://mozilla.org";
+        final String url2 = "http://hello.org";
+
+        private void testInsertionViaContentProvider() throws Exception {
+            final String tileImage = "http://mozilla.org/tileImage.png";
+            final String tileColor = "#FF0000";
+            final String touchIcon = "http://mozilla.org/touchIcon.png";
+
+            // We can only use update since the redirection machinery doesn't exist for insert
+            mProvider.update(URLMetadataTable.CONTENT_URI.buildUpon().appendQueryParameter(BrowserContract.PARAM_INSERT_IF_NEEDED, "true").build(),
+                    createUrlMetadataEntry(url1, tileImage, tileColor, touchIcon),
+                    URLMetadataTable.URL_COLUMN + "=?",
+                    new String[] {url1}
+            );
+
+            final Cursor c = getUrlMetadataByUrl(url1);
+            try {
+                mAsserter.is(c.moveToFirst(), true, "URL metadata inserted via Content Provider not found");
+            } finally {
+                c.close();
+            }
+        }
+
+        private void testInsertionViaUrlMetadata() throws Exception {
+            final String tileImage = "http://hello.org/tileImage.png";
+            final String tileColor = "#FF0000";
+            final String touchIcon = "http://hello.org/touchIcon.png";
+
+            final Map<String, Object> data = new HashMap<>();
+            data.put(URLMetadataTable.URL_COLUMN, url2);
+            data.put(URLMetadataTable.TILE_IMAGE_URL_COLUMN, tileImage);
+            data.put(URLMetadataTable.TILE_COLOR_COLUMN, tileColor);
+            data.put(URLMetadataTable.TOUCH_ICON_COLUMN, touchIcon);
+
+            getTestProfile().getDB().getURLMetadata().save(mResolver, data);
+
+            final Cursor c = getUrlMetadataByUrl(url2);
+            try {
+                mAsserter.is(c.moveToFirst(), true, "URL metadata inserted via UrlMetadata not found");
+            } finally {
+                c.close();
+            }
+        }
+
+        private void testRetrievalViaUrlMetadata() {
+            // LocalURLMetadata has some caching of results: we need to test that this caching
+            // doesn't prevent us from accessing data that might not have been loaded into the cache.
+            URLMetadata metadata = getTestProfile().getDB().getURLMetadata();
+
+            Map<String, Map<String, Object>> results;
+            Map<String, Object> urlData;
+
+            // 1: retrieve just touch Icons for URL 1
+            results = metadata.getForURLs(mResolver,
+                    Collections.singletonList(url1),
+                    Collections.singletonList(URLMetadataTable.TOUCH_ICON_COLUMN));
+
+            mAsserter.is(results.containsKey(url1), true, "URL 1 not found in results");
+
+            urlData = results.get(url1);
+            mAsserter.is(urlData.containsKey(URLMetadataTable.TOUCH_ICON_COLUMN), true, "touchIcon column missing in UrlMetadata results");
+
+            // 2: retrieve just tile color for URL 2
+            results = metadata.getForURLs(mResolver,
+                    Collections.singletonList(url2),
+                    Collections.singletonList(URLMetadataTable.TILE_COLOR_COLUMN));
+
+            mAsserter.is(results.containsKey(url2), true, "URL 2 not found in results");
+
+            urlData = results.get(url2);
+            mAsserter.is(urlData.containsKey(URLMetadataTable.TILE_COLOR_COLUMN), true, "touchIcon column missing in UrlMetadata results");
+
+
+            // 3: retrieve all columns for both URLs
+            results = metadata.getForURLs(mResolver,
+                    Arrays.asList(new String[] {
+                            url1,
+                            url2
+                    }),
+                    Arrays.asList(new String[] {
+                            URLMetadataTable.TILE_IMAGE_URL_COLUMN,
+                            URLMetadataTable.TILE_COLOR_COLUMN,
+                            URLMetadataTable.TOUCH_ICON_COLUMN
+                    }));
+
+            mAsserter.is(results.containsKey(url1), true, "URL 1 not found in results");
+            mAsserter.is(results.containsKey(url2), true, "URL 2 not found in results");
+
+            urlData = results.get(url1);
+            mAsserter.is(urlData.containsKey(URLMetadataTable.TILE_IMAGE_URL_COLUMN), true, "touchIcon column missing in UrlMetadata results");
+            mAsserter.is(urlData.containsKey(URLMetadataTable.TILE_COLOR_COLUMN), true, "touchIcon column missing in UrlMetadata results");
+            mAsserter.is(urlData.containsKey(URLMetadataTable.TOUCH_ICON_COLUMN), true, "touchIcon column missing in UrlMetadata results");
+
+            urlData = results.get(url2);
+            mAsserter.is(urlData.containsKey(URLMetadataTable.TILE_IMAGE_URL_COLUMN), true, "touchIcon column missing in UrlMetadata results");
+            mAsserter.is(urlData.containsKey(URLMetadataTable.TILE_COLOR_COLUMN), true, "touchIcon column missing in UrlMetadata results");
+            mAsserter.is(urlData.containsKey(URLMetadataTable.TOUCH_ICON_COLUMN), true, "touchIcon column missing in UrlMetadata results");
+        }
+    }
+
     private class TestCombinedView extends TestCase {
         @Override
         public void test() throws Exception {
             final String TITLE_1 = "Test Page 1";
             final String TITLE_2 = "Test Page 2";
             final String TITLE_3_HISTORY = "Test Page 3 (History Entry)";
             final String TITLE_3_BOOKMARK = "Test Page 3 (Bookmark Entry)";
             final String TITLE_3_BOOKMARK2 = "Test Page 3 (Bookmark Entry 2)";