Bug 1076438 - Add tracking ID to SuggestedSites API. r=lucasr
authorBrian Nicholson <bnicholson@mozilla.com>
Thu, 02 Oct 2014 09:48:50 -0700
changeset 208635 279afba13f4c109111417b3987205d6fe0f0eaed
parent 208634 4a837d6936a03dd402ef6cdd31b1cc582284e979
child 208636 948719006fe8983a4a5cd7b2c96a98026dd5973f
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewerslucasr
bugs1076438
milestone35.0a1
Bug 1076438 - Add tracking ID to SuggestedSites API. r=lucasr
mobile/android/base/db/BrowserDB.java
mobile/android/base/db/SuggestedSites.java
mobile/android/tests/browser/junit3/src/TestSuggestedSites.java
--- a/mobile/android/base/db/BrowserDB.java
+++ b/mobile/android/base/db/BrowserDB.java
@@ -265,9 +265,13 @@ public class BrowserDB {
     public static int getSuggestedBackgroundColorForUrl(String url) {
         final String bgColor = sSuggestedSites.getBackgroundColorForUrl(url);
         if (bgColor != null) {
             return Color.parseColor(bgColor);
         }
 
         return 0;
     }
+
+    public static int getTrackingIdForUrl(String url) {
+        return sSuggestedSites.getTrackingIdForUrl(url);
+    }
 }
--- a/mobile/android/base/db/SuggestedSites.java
+++ b/mobile/android/base/db/SuggestedSites.java
@@ -79,67 +79,78 @@ public class SuggestedSites {
     private static final String FILENAME = "suggestedsites.json";
 
     private static final String[] COLUMNS = new String[] {
         BrowserContract.SuggestedSites._ID,
         BrowserContract.SuggestedSites.URL,
         BrowserContract.SuggestedSites.TITLE,
     };
 
+    public static final int TRACKING_ID_NONE = -1;
+
+    private static final String JSON_KEY_TRACKING_ID = "trackingid";
     private static final String JSON_KEY_URL = "url";
     private static final String JSON_KEY_TITLE = "title";
     private static final String JSON_KEY_IMAGE_URL = "imageurl";
     private static final String JSON_KEY_BG_COLOR = "bgcolor";
 
     private static class Site {
         public final String url;
         public final String title;
         public final String imageUrl;
         public final String bgColor;
+        public final int trackingId;
 
         public Site(JSONObject json) throws JSONException {
+            this.trackingId = json.isNull(JSON_KEY_TRACKING_ID) ? TRACKING_ID_NONE : json.getInt(JSON_KEY_TRACKING_ID);
             this.url = json.getString(JSON_KEY_URL);
             this.title = json.getString(JSON_KEY_TITLE);
             this.imageUrl = json.getString(JSON_KEY_IMAGE_URL);
             this.bgColor = json.getString(JSON_KEY_BG_COLOR);
 
             validate();
         }
 
-        public Site(String url, String title, String imageUrl, String bgColor) {
+        public Site(int trackingId, String url, String title, String imageUrl, String bgColor) {
+            this.trackingId = trackingId;
             this.url = url;
             this.title = title;
             this.imageUrl = imageUrl;
             this.bgColor = bgColor;
 
             validate();
         }
 
         private void validate() {
-            // Site instances must have non-empty values for all properties.
+            // Site instances must have non-empty values for all properties except IDs.
             if (TextUtils.isEmpty(url) ||
                 TextUtils.isEmpty(title) ||
                 TextUtils.isEmpty(imageUrl) ||
                 TextUtils.isEmpty(bgColor)) {
                 throw new IllegalStateException("Suggested sites must have a URL, title, " +
                                                 "image URL, and background color.");
             }
         }
 
         @Override
         public String toString() {
-            return "{ url = " + url + "\n" +
+            return "{ trackingId = " + trackingId + "\n" +
+                     "url = " + url + "\n" +
                      "title = " + title + "\n" +
                      "imageUrl = " + imageUrl + "\n" +
                      "bgColor = " + bgColor + " }";
         }
 
         public JSONObject toJSON() throws JSONException {
             final JSONObject json = new JSONObject();
 
+            if (trackingId >= 0) {
+                json.put(JSON_KEY_TRACKING_ID, trackingId);
+            }
+
             json.put(JSON_KEY_URL, url);
             json.put(JSON_KEY_TITLE, title);
             json.put(JSON_KEY_IMAGE_URL, imageUrl);
             json.put(JSON_KEY_BG_COLOR, bgColor);
 
             return json;
         }
     }
@@ -509,16 +520,21 @@ public class SuggestedSites {
         return (site != null ? site.imageUrl : null);
     }
 
     public String getBackgroundColorForUrl(String url) {
         final Site site = getSiteForUrl(url);
         return (site != null ? site.bgColor : null);
     }
 
+    public int getTrackingIdForUrl(String url) {
+        final Site site = getSiteForUrl(url);
+        return (site != null ? site.trackingId : TRACKING_ID_NONE);
+    }
+
     private Set<String> loadBlacklist() {
         Log.d(LOGTAG, "Loading blacklisted suggested sites from SharedPreferences.");
         final Set<String> blacklist = new HashSet<String>();
 
         final SharedPreferences preferences = GeckoSharedPrefs.forProfile(context);
         final String sitesString = preferences.getString(PREF_SUGGESTED_SITES_HIDDEN, null);
 
         if (sitesString != null) {
--- a/mobile/android/tests/browser/junit3/src/TestSuggestedSites.java
+++ b/mobile/android/tests/browser/junit3/src/TestSuggestedSites.java
@@ -142,21 +142,28 @@ public class TestSuggestedSites extends 
     private TestResources resources;
     private List<File> tempFiles;
 
     private String generateSites(int n) {
         return generateSites(n, "");
     }
 
     private String generateSites(int n, String prefix) {
+        return generateSites(n, false, prefix);
+    }
+
+    private String generateSites(int n, boolean includeIds, String prefix) {
         JSONArray sites = new JSONArray();
 
         try {
             for (int i = 0; i < n; i++) {
                 JSONObject site = new JSONObject();
+                if (includeIds) {
+                    site.put("trackingid", i);
+                }
                 site.put("url", prefix + "url" + i);
                 site.put("title", prefix + "title" + i);
                 site.put("imageurl", prefix + "imageUrl" + i);
                 site.put("bgcolor", prefix + "bgColor" + i);
 
                 sites.put(site);
             }
         } catch (Exception e) {
@@ -368,16 +375,48 @@ public class TestSuggestedSites extends 
         c.close();
 
         // No valid values for unknown URLs.
         assertFalse(suggestedSites.contains("foo"));
         assertNull(suggestedSites.getImageUrlForUrl("foo"));
         assertNull(suggestedSites.getBackgroundColorForUrl("foo"));
     }
 
+    public void testTrackingIds() {
+        final int count = 3;
+
+        // Test suggested sites with IDs.
+        resources.setSuggestedSitesResource(generateSites(count, true, ""));
+        SuggestedSites suggestedSites = new SuggestedSites(context);
+        Cursor c = suggestedSites.get(DEFAULT_LIMIT);
+        assertEquals(count, c.getCount());
+
+        for (int i = 0; i < count; i++) {
+            c.moveToNext();
+            String url = c.getString(c.getColumnIndexOrThrow(BrowserContract.SuggestedSites.URL));
+            assertTrue(suggestedSites.contains(url));
+            assertEquals(i, suggestedSites.getTrackingIdForUrl(url));
+        }
+        c.close();
+
+        // Test suggested sites where IDs are undefined.
+        resources.setSuggestedSitesResource(generateSites(count, false, ""));
+        suggestedSites = new SuggestedSites(context);
+        c = suggestedSites.get(DEFAULT_LIMIT);
+        assertEquals(count, c.getCount());
+
+        for (int i = 0; i < count; i++) {
+            c.moveToNext();
+            String url = c.getString(c.getColumnIndexOrThrow(BrowserContract.SuggestedSites.URL));
+            assertTrue(suggestedSites.contains(url));
+            assertEquals(SuggestedSites.TRACKING_ID_NONE, suggestedSites.getTrackingIdForUrl(url));
+        }
+        c.close();
+    }
+
     public void testLocaleChanges() {
         resources.setSuggestedSitesResource(generateSites(3));
 
         SuggestedSites suggestedSites = new SuggestedSites(context);
 
         // Initial load with predefined locale
         Cursor c = suggestedSites.get(DEFAULT_LIMIT, Locale.UK);
         assertEquals(3, c.getCount());