Bug 1076438 - Add tracking ID to SuggestedSites API. r=lucasr

This commit is contained in:
Brian Nicholson 2014-10-02 09:48:50 -07:00
Родитель 09e2ca928a
Коммит 4814f3a90a
3 изменённых файлов: 62 добавлений и 3 удалений

Просмотреть файл

@ -270,4 +270,8 @@ public class BrowserDB {
return 0;
}
public static int getTrackingIdForUrl(String url) {
return sSuggestedSites.getTrackingIdForUrl(url);
}
}

Просмотреть файл

@ -84,6 +84,9 @@ public class SuggestedSites {
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";
@ -94,8 +97,10 @@ public class SuggestedSites {
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);
@ -104,7 +109,8 @@ public class SuggestedSites {
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;
@ -114,7 +120,7 @@ public class SuggestedSites {
}
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) ||
@ -126,7 +132,8 @@ public class SuggestedSites {
@Override
public String toString() {
return "{ url = " + url + "\n" +
return "{ trackingId = " + trackingId + "\n" +
"url = " + url + "\n" +
"title = " + title + "\n" +
"imageUrl = " + imageUrl + "\n" +
"bgColor = " + bgColor + " }";
@ -135,6 +142,10 @@ public class SuggestedSites {
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);
@ -514,6 +525,11 @@ public class SuggestedSites {
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>();

Просмотреть файл

@ -147,11 +147,18 @@ public class TestSuggestedSites extends BrowserTestCase {
}
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);
@ -373,6 +380,38 @@ public class TestSuggestedSites extends BrowserTestCase {
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));