Bug 1042204 - Control the enhancement of history tiles with a pref [r=adw a=sylvestre]
authorEd Lee <edilee@mozilla.com>
Wed, 23 Jul 2014 11:02:51 -0700
changeset 217666 9f9d2a640709d1578bf72f9d1dd19fd728732449
parent 217665 35f5ce3cfaaa7f3748074c8b74427573ac6a5c94
child 217667 6d6b0c029f7171dec6cb8b963041794f4932db03
push id515
push userraliiev@mozilla.com
push dateMon, 06 Oct 2014 12:51:51 +0000
treeherdermozilla-release@267c7a481bef [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersadw, sylvestre
bugs1042204
milestone33.0a2
Bug 1042204 - Control the enhancement of history tiles with a pref [r=adw a=sylvestre] Only show enhanced history when the pref allows it. Update newtab pages when enhanced pref changes.
browser/app/profile/firefox.js
browser/base/content/newtab/page.js
browser/base/content/newtab/sites.js
browser/base/content/test/newtab/browser.ini
browser/base/content/test/newtab/browser_newtab_enhanced.js
browser/base/content/test/newtab/head.js
toolkit/modules/NewTabUtils.jsm
--- a/browser/app/profile/firefox.js
+++ b/browser/app/profile/firefox.js
@@ -1506,16 +1506,19 @@ pref("browser.panorama.animate_zoom", tr
 // Defines the url to be used for new tabs.
 pref("browser.newtab.url", "about:newtab");
 // Activates preloading of the new tab url.
 pref("browser.newtab.preload", true);
 
 // Toggles the content of 'about:newtab'. Shows the grid when enabled.
 pref("browser.newtabpage.enabled", true);
 
+// Toggles the enhancement of history content of 'about:newtab'
+pref("browser.newtabpage.enhanced", false);
+
 // number of rows of newtab grid
 pref("browser.newtabpage.rows", 3);
 
 // number of columns of newtab grid
 pref("browser.newtabpage.columns", 3);
 
 // directory tiles download URL
 pref("browser.newtabpage.directory.source", "chrome://global/content/directoryLinks.json");
--- a/browser/base/content/newtab/page.js
+++ b/browser/base/content/newtab/page.js
@@ -46,16 +46,21 @@ let gPage = {
   /**
    * Listens for notifications specific to this page.
    */
   observe: function Page_observe(aSubject, aTopic, aData) {
     if (aTopic == "nsPref:changed") {
       let enabled = gAllPages.enabled;
       this._updateAttributes(enabled);
 
+      // Update thumbnails to the new enhanced setting
+      if (aData == "browser.newtabpage.enhanced") {
+        this.update();
+      }
+
       // Initialize the whole page if we haven't done that, yet.
       if (enabled) {
         this._init();
       } else {
         gUndoDialog.hide();
       }
     } else if (aTopic == "page-thumbnail:create" && gGrid.ready) {
       for (let site of gGrid.sites) {
--- a/browser/base/content/newtab/sites.js
+++ b/browser/base/content/newtab/sites.js
@@ -148,17 +148,19 @@ Site.prototype = {
       BackgroundPageThumbs.captureIfMissing(this.url);
     }
   },
 
   /**
    * Refreshes the thumbnail for the site.
    */
   refreshThumbnail: function Site_refreshThumbnail() {
-    let link = DirectoryLinksProvider.getEnhancedLink(this.link) || this.link;
+    // Only enhance tiles if that feature is turned on
+    let link = gAllPages.enhanced && DirectoryLinksProvider.getEnhancedLink(this.link) ||
+               this.link;
 
     let thumbnail = this._querySelector(".newtab-thumbnail");
     if (link.bgColor) {
       thumbnail.style.backgroundColor = link.bgColor;
     }
 
     let uri = link.imageURI || PageThumbs.getThumbnailURL(this.url);
     thumbnail.style.backgroundImage = 'url("' + uri + '")';
--- a/browser/base/content/test/newtab/browser.ini
+++ b/browser/base/content/test/newtab/browser.ini
@@ -18,16 +18,17 @@ skip-if = os == "mac" # Intermittent fai
 [browser_newtab_bug876313.js]
 [browser_newtab_bug991111.js]
 [browser_newtab_bug991210.js]
 [browser_newtab_bug998387.js]
 [browser_newtab_disable.js]
 [browser_newtab_drag_drop.js]
 [browser_newtab_drag_drop_ext.js]
 [browser_newtab_drop_preview.js]
+[browser_newtab_enhanced.js]
 [browser_newtab_focus.js]
 [browser_newtab_perwindow_private_browsing.js]
 [browser_newtab_reportLinkAction.js]
 [browser_newtab_reset.js]
 [browser_newtab_search.js]
 support-files =
   searchEngineNoLogo.xml
   searchEngine1xLogo.xml
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/newtab/browser_newtab_enhanced.js
@@ -0,0 +1,55 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+const PRELOAD_PREF = "browser.newtab.preload";
+
+gDirectorySource = "data:application/json," + JSON.stringify({
+  "en-US": [{
+    url: "http://example.com/",
+    enhancedImageURI: "data:image/png;base64,helloWORLD",
+    type: "organic"
+  }]
+});
+
+function runTests() {
+  let origEnhanced = NewTabUtils.allPages.enhanced;
+  registerCleanupFunction(() => {
+    Services.prefs.clearUserPref(PRELOAD_PREF);
+    NewTabUtils.allPages.enhanced = origEnhanced;
+  });
+
+  Services.prefs.setBoolPref(PRELOAD_PREF, false);
+
+  function getData(cellNum) {
+    let siteNode = getCell(cellNum).site.node;
+    return {
+      type: siteNode.getAttribute("type"),
+      enhanced: siteNode.querySelector(".enhanced-content").style.backgroundImage,
+    };
+  }
+
+  // Make the page have a directory link followed by a history link
+  yield setLinks("1");
+
+  // Test with enhanced = false
+  NewTabUtils.allPages.enhanced = false;
+  yield addNewTabPageTab();
+  let {type, enhanced} = getData(0);
+  is(type, "organic", "directory link is organic");
+  isnot(enhanced, "", "directory link has enhanced image");
+
+  let {type, enhanced} = getData(1);
+  is(type, "history", "history link is history");
+  is(enhanced, "", "history link has no enhanced image");
+
+  // Test with enhanced = true
+  NewTabUtils.allPages.enhanced = true;
+  yield addNewTabPageTab();
+  let {type, enhanced} = getData(0);
+  is(type, "organic", "directory link is still organic");
+  isnot(enhanced, "", "directory link still has enhanced image");
+
+  let {type, enhanced} = getData(1);
+  is(type, "enhanced", "history link now is enhanced");
+  isnot(enhanced, "", "history link now has enhanced image");
+}
--- a/browser/base/content/test/newtab/head.js
+++ b/browser/base/content/test/newtab/head.js
@@ -586,16 +586,17 @@ function createDragEvent(aEventType, aDa
  * Resumes testing when all pages have been updated.
  * @param aCallback Called when done. If not specified, TestRunner.next is used.
  * @param aOnlyIfHidden If true, this resumes testing only when an update that
  *                      applies to pre-loaded, hidden pages is observed.  If
  *                      false, this resumes testing when any update is observed.
  */
 function whenPagesUpdated(aCallback, aOnlyIfHidden=false) {
   let page = {
+    observe: _ => _,
     update: function (onlyIfHidden=false) {
       if (onlyIfHidden == aOnlyIfHidden) {
         NewTabUtils.allPages.unregister(this);
         executeSoon(aCallback || TestRunner.next);
       }
     }
   };
 
--- a/toolkit/modules/NewTabUtils.jsm
+++ b/toolkit/modules/NewTabUtils.jsm
@@ -37,18 +37,19 @@ XPCOMUtils.defineLazyGetter(this, "gCryp
 
 XPCOMUtils.defineLazyGetter(this, "gUnicodeConverter", function () {
   let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
                     .createInstance(Ci.nsIScriptableUnicodeConverter);
   converter.charset = 'utf8';
   return converter;
 });
 
-// The preference that tells whether this feature is enabled.
+// Boolean preferences that control newtab content
 const PREF_NEWTAB_ENABLED = "browser.newtabpage.enabled";
+const PREF_NEWTAB_ENHANCED = "browser.newtabpage.enhanced";
 
 // The preference that tells the number of rows of the newtab grid.
 const PREF_NEWTAB_ROWS = "browser.newtabpage.rows";
 
 // The preference that tells the number of columns of the newtab grid.
 const PREF_NEWTAB_COLUMNS = "browser.newtabpage.columns";
 
 // The maximum number of results PlacesProvider retrieves from history.
@@ -205,16 +206,21 @@ let AllPages = {
   _pages: [],
 
   /**
    * Cached value that tells whether the New Tab Page feature is enabled.
    */
   _enabled: null,
 
   /**
+   * Cached value that tells whether the New Tab Page feature is enhanced.
+   */
+  _enhanced: null,
+
+  /**
    * Adds a page to the internal list of pages.
    * @param aPage The page to register.
    */
   register: function AllPages_register(aPage) {
     this._pages.push(aPage);
     this._addObserver();
   },
 
@@ -242,16 +248,34 @@ let AllPages = {
    * Enables or disables the 'New Tab Page' feature.
    */
   set enabled(aEnabled) {
     if (this.enabled != aEnabled)
       Services.prefs.setBoolPref(PREF_NEWTAB_ENABLED, !!aEnabled);
   },
 
   /**
+   * Returns whether the history tiles are enhanced.
+   */
+  get enhanced() {
+    if (this._enhanced === null)
+      this._enhanced = Services.prefs.getBoolPref(PREF_NEWTAB_ENHANCED);
+
+    return this._enhanced;
+  },
+
+  /**
+   * Enables or disables the enhancement of history tiles feature.
+   */
+  set enhanced(aEnhanced) {
+    if (this.enhanced != aEnhanced)
+      Services.prefs.setBoolPref(PREF_NEWTAB_ENHANCED, !!aEnhanced);
+  },
+
+  /**
    * Returns the number of registered New Tab Pages (i.e. the number of open
    * about:newtab instances).
    */
   get length() {
     return this._pages.length;
   },
 
   /**
@@ -283,30 +307,38 @@ let AllPages = {
 
   /**
    * Implements the nsIObserver interface to get notified when the preference
    * value changes or when a new copy of a page thumbnail is available.
    */
   observe: function AllPages_observe(aSubject, aTopic, aData) {
     if (aTopic == "nsPref:changed") {
       // Clear the cached value.
-      this._enabled = null;
+      switch (aData) {
+        case PREF_NEWTAB_ENABLED:
+          this._enabled = null;
+          break;
+        case PREF_NEWTAB_ENHANCED:
+          this._enhanced = null;
+          break;
+      }
     }
     // and all notifications get forwarded to each page.
     this._pages.forEach(function (aPage) {
       aPage.observe(aSubject, aTopic, aData);
     }, this);
   },
 
   /**
    * Adds a preference and new thumbnail observer and turns itself into a
    * no-op after the first invokation.
    */
   _addObserver: function AllPages_addObserver() {
     Services.prefs.addObserver(PREF_NEWTAB_ENABLED, this, true);
+    Services.prefs.addObserver(PREF_NEWTAB_ENHANCED, this, true);
     Services.obs.addObserver(this, "page-thumbnail:create", true);
     this._addObserver = function () {};
   },
 
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
                                          Ci.nsISupportsWeakReference])
 };