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()); 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(); }
public void testDistribution() { final int DIST_COUNT = 2; final int DEFAULT_COUNT = 3; File sitesFile = new File(context.getCacheDir(), "suggestedsites-" + SystemClock.uptimeMillis() + ".json"); tempFiles.add(sitesFile); assertFalse(sitesFile.exists()); File distFile = createDistSuggestedSitesFile(DIST_COUNT); tempFiles.add(distFile); assertTrue(distFile.exists()); // Init distribution with the mock file. TestDistribution distribution = new TestDistribution(context); distribution.setFileForLocale(Locale.getDefault(), distFile); distribution.start(); // Init suggested sites with default values. resources.setSuggestedSitesResource(generateSites(DEFAULT_COUNT)); SuggestedSites suggestedSites = new SuggestedSites(context, distribution, sitesFile); Object changeLock = new Object(); // Watch for change notifications on suggested sites. ContentResolver cr = context.getContentResolver(); ContentObserver observer = new TestObserver(changeLock); cr.registerContentObserver(BrowserContract.SuggestedSites.CONTENT_URI, false, observer); // The initial query will not contain the distribution sites // yet. This will happen asynchronously once the distribution // is installed. Cursor c1 = null; try { c1 = suggestedSites.get(DEFAULT_LIMIT); assertEquals(DEFAULT_COUNT, c1.getCount()); } finally { if (c1 != null) { c1.close(); } } synchronized (changeLock) { try { changeLock.wait(5000); } catch (InterruptedException ie) { fail("No change notification after fetching distribution file"); } } // Target file should exist after distribution is deployed. assertTrue(sitesFile.exists()); cr.unregisterContentObserver(observer); Cursor c2 = null; try { c2 = suggestedSites.get(DEFAULT_LIMIT); // The next query should contain the distribution contents. assertEquals(DIST_COUNT + DEFAULT_COUNT, c2.getCount()); // The first items should be from the distribution for (int i = 0; i < DIST_COUNT; i++) { c2.moveToPosition(i); String url = c2.getString(c2.getColumnIndexOrThrow(BrowserContract.SuggestedSites.URL)); assertEquals(DIST_PREFIX + "url" + i, url); String title = c2.getString(c2.getColumnIndexOrThrow(BrowserContract.SuggestedSites.TITLE)); assertEquals(DIST_PREFIX + "title" + i, title); } // The remaining items should be the default ones for (int i = 0; i < c2.getCount() - DIST_COUNT; i++) { c2.moveToPosition(i + DIST_COUNT); String url = c2.getString(c2.getColumnIndexOrThrow(BrowserContract.SuggestedSites.URL)); assertEquals("url" + i, url); String title = c2.getString(c2.getColumnIndexOrThrow(BrowserContract.SuggestedSites.TITLE)); assertEquals("title" + i, title); } } finally { if (c2 != null) { c2.close(); } } }
public void testImageUrlAndBgColor() { final int count = 3; resources.setSuggestedSitesResource(generateSites(count)); SuggestedSites suggestedSites = new SuggestedSites(context); // Suggested sites hasn't been loaded yet. for (int i = 0; i < count; i++) { String url = "url" + i; assertFalse(suggestedSites.contains(url)); assertNull(suggestedSites.getImageUrlForUrl(url)); assertNull(suggestedSites.getBackgroundColorForUrl(url)); } Cursor c = suggestedSites.get(DEFAULT_LIMIT); c.moveToPosition(-1); // We should have cached results after the get() call. while (c.moveToNext()) { String url = c.getString(c.getColumnIndexOrThrow(BrowserContract.SuggestedSites.URL)); assertTrue(suggestedSites.contains(url)); assertEquals("imageUrl" + c.getPosition(), suggestedSites.getImageUrlForUrl(url)); assertEquals("bgColor" + c.getPosition(), suggestedSites.getBackgroundColorForUrl(url)); } c.close(); // No valid values for unknown URLs. assertFalse(suggestedSites.contains("foo")); assertNull(suggestedSites.getImageUrlForUrl("foo")); assertNull(suggestedSites.getBackgroundColorForUrl("foo")); }