Bug 1145410: Return valid results when querying the provider cache while it's empty or being populated. r=adw, a=sylvestre
authorMarina Samuel <msamuel@mozilla.com>
Fri, 20 Mar 2015 17:46:24 -0400
changeset 258193 56763fc69140
parent 258192 db2b58500934
child 258194 65f2aa5f2dd7
push id4619
push useredilee@gmail.com
push date2015-04-02 05:50 +0000
treeherdermozilla-beta@daf8a9291a9b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersadw, sylvestre
bugs1145410
milestone38.0
Bug 1145410: Return valid results when querying the provider cache while it's empty or being populated. r=adw, a=sylvestre
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);