Bug 1017574 - Force suggested sites refresh on locale changes (r=mfinkle)

This commit is contained in:
Lucas Rocha 2014-05-29 21:30:32 +01:00
parent 80d65af72d
commit 9d3309a14a
2 changed files with 51 additions and 2 deletions

View File

@ -17,6 +17,7 @@ import java.io.IOException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.json.JSONArray;
@ -86,6 +87,7 @@ public class SuggestedSites {
private final Context context;
private Map<String, Site> cachedSites;
private Locale cachedLocale;
public SuggestedSites(Context appContext) {
context = appContext;
@ -145,6 +147,7 @@ public class SuggestedSites {
// Update cached list of sites
cachedSites = Collections.unmodifiableMap(sites);
cachedLocale = Locale.getDefault();
}
private boolean isEnabled() {
@ -166,7 +169,17 @@ public class SuggestedSites {
* @param limit maximum number of suggested sites.
*/
public Cursor get(int limit) {
return get(limit, null);
return get(limit, Locale.getDefault());
}
/**
* Returns a {@code Cursor} with the list of suggested websites.
*
* @param limit maximum number of suggested sites.
* @param locale the target locale.
*/
public Cursor get(int limit, Locale locale) {
return get(limit, locale, null);
}
/**
@ -176,6 +189,17 @@ public class SuggestedSites {
* @param excludeUrls list of URLs to be excluded from the list.
*/
public Cursor get(int limit, List<String> excludeUrls) {
return get(limit, Locale.getDefault(), excludeUrls);
}
/**
* Returns a {@code Cursor} with the list of suggested websites.
*
* @param limit maximum number of suggested sites.
* @param locale the target locale.
* @param excludeUrls list of URLs to be excluded from the list.
*/
public Cursor get(int limit, Locale locale, List<String> excludeUrls) {
final MatrixCursor cursor = new MatrixCursor(COLUMNS);
// Return an empty cursor if suggested sites have been
@ -184,7 +208,7 @@ public class SuggestedSites {
return cursor;
}
if (cachedSites == null) {
if (cachedSites == null || !locale.equals(cachedLocale)) {
Log.d(LOGTAG, "No cached sites, refreshing.");
refresh();
}

View File

@ -16,6 +16,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.json.JSONArray;
@ -242,4 +243,28 @@ public class TestSuggestedSites extends BrowserTestCase {
assertNull(suggestedSites.getImageUrlForUrl("foo"));
assertNull(suggestedSites.getBackgroundColorForUrl("foo"));
}
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());
c.close();
resources.setSuggestedSitesResource(generateSites(5));
// Second load with same locale should return same results
// even though the contents of the resource have changed.
c = suggestedSites.get(DEFAULT_LIMIT, Locale.UK);
assertEquals(3, c.getCount());
c.close();
// Changing the locale forces the cached list to be refreshed.
c = suggestedSites.get(DEFAULT_LIMIT, Locale.US);
assertEquals(5, c.getCount());
c.close();
}
}