Bug 1145410: Return valid results when querying the provider cache while it's empty or being populated. r=adw
authorMarina Samuel <msamuel@mozilla.com>
Fri, 20 Mar 2015 17:46:24 -0400
changeset 263731 531fe47e635bfc99bea79bfd6f17577351d840f7
parent 263730 e851320aec2b7881217cc52929134b09630572fa
child 263732 a10307812cf83699cab915dacc4511596368397c
push id4718
push userraliiev@mozilla.com
push dateMon, 11 May 2015 18:39:53 +0000
treeherdermozilla-beta@c20c4ef55f08 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersadw
bugs1145410
milestone39.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1145410: Return valid results when querying the provider cache while it's empty or being populated. r=adw
toolkit/modules/NewTabUtils.jsm
toolkit/modules/tests/xpcshell/test_NewTabUtils.js
--- a/toolkit/modules/NewTabUtils.jsm
+++ b/toolkit/modules/NewTabUtils.jsm
@@ -1248,21 +1248,29 @@ this.NewTabUtils = {
       ExpirationFilter.init();
       Telemetry.init();
       return true;
     }
     return false;
   },
 
   getProviderLinks: function(aProvider) {
-    return Links._providers.get(aProvider).sortedLinks;
+    let cache = Links._providers.get(aProvider);
+    if (cache && cache.sortedLinks) {
+      return cache.sortedLinks;
+    }
+    return [];
   },
 
   isTopSiteGivenProvider: function(aSite, aProvider) {
-    return Links._providers.get(aProvider).siteMap.has(aSite);
+    let cache = Links._providers.get(aProvider);
+    if (cache && cache.siteMap) {
+      return cache.siteMap.has(aSite);
+    }
+    return false;
   },
 
   isTopPlacesSite: function(aSite) {
     return this.isTopSiteGivenProvider(aSite, PlacesProvider);
   },
 
   /**
    * Restores all sites that have been removed from the grid.
--- a/toolkit/modules/tests/xpcshell/test_NewTabUtils.js
+++ b/toolkit/modules/tests/xpcshell/test_NewTabUtils.js
@@ -11,16 +11,39 @@ Cu.import("resource://gre/modules/Servic
 
 const PREF_NEWTAB_ENHANCED = "browser.newtabpage.enhanced";
 
 function run_test() {
   Services.prefs.setBoolPref(PREF_NEWTAB_ENHANCED, true);
   run_next_test();
 }
 
+add_task(function validCacheMidPopulation() {
+  let expectedLinks = makeLinks(0, 3, 1);
+
+  let provider = new TestProvider(done => done(expectedLinks));
+  provider.maxNumLinks = expectedLinks.length;
+
+  NewTabUtils.initWithoutProviders();
+  NewTabUtils.links.addProvider(provider);
+  let promise = new Promise(resolve => NewTabUtils.links.populateCache(resolve));
+
+  // isTopSiteGivenProvider() and getProviderLinks() should still return results
+  // even when cache is empty or being populated.
+  do_check_false(NewTabUtils.isTopSiteGivenProvider("example1.com", provider));
+  do_check_links(NewTabUtils.getProviderLinks(provider), []);
+
+  yield promise;
+
+  // Once the cache is populated, we get the expected results
+  do_check_true(NewTabUtils.isTopSiteGivenProvider("example1.com", provider));
+  do_check_links(NewTabUtils.getProviderLinks(provider), expectedLinks);
+  NewTabUtils.links.removeProvider(provider);
+});
+
 add_task(function notifyLinkDelete() {
   let expectedLinks = makeLinks(0, 3, 1);
 
   let provider = new TestProvider(done => done(expectedLinks));
   provider.maxNumLinks = expectedLinks.length;
 
   NewTabUtils.initWithoutProviders();
   NewTabUtils.links.addProvider(provider);