Bug 575830 - Image zoom is reset when switching tabs [r=gavin]
authorMatt Brubeck <mbrubeck@mozilla.com>
Wed, 18 Jan 2012 16:01:19 -0800
changeset 86059 1f6244d044aa0797af698cb987f0e12608e82b7e
parent 86058 025fa101a51bdbb2758cccffaa790a8f38b04dd8
child 86060 d0c192e5bd41345e6acdc497e820150ae9aec484
push id805
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 18:17:35 +0000
treeherdermozilla-aurora@6fb3bf232436 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgavin
bugs575830
milestone12.0a1
Bug 575830 - Image zoom is reset when switching tabs [r=gavin]
browser/base/content/browser-fullZoom.js
browser/base/content/test/Makefile.in
browser/base/content/test/browser_bug386835.js
browser/base/content/test/browser_bug416661.js
browser/base/content/test/browser_bug575830.js
--- a/browser/base/content/browser-fullZoom.js
+++ b/browser/base/content/browser-fullZoom.js
@@ -227,16 +227,22 @@ var FullZoom = {
     // Avoid the cps roundtrip and apply the default/global pref.
     if (aURI.spec == "about:blank") {
       this._applyPrefToSetting(undefined, aBrowser);
       return;
     }
 
     let browser = aBrowser || gBrowser.selectedBrowser;
 
+    // Image documents should always start at 1, and are not affected by prefs.
+    if (!aIsTabSwitch && browser.contentDocument instanceof ImageDocument) {
+      ZoomManager.setZoomForBrowser(browser, 1);
+      return;
+    }
+
     if (Services.contentPrefs.hasCachedPref(aURI, this.name)) {
       let zoomValue = Services.contentPrefs.getPref(aURI, this.name);
       this._applyPrefToSetting(zoomValue, browser);
     } else {
       var self = this;
       Services.contentPrefs.getPref(aURI, this.name, function (aResult) {
         // Check that we're still where we expect to be in case this took a while.
         // Null check currentURI, since the window may have been destroyed before
@@ -298,39 +304,40 @@ var FullZoom = {
    * one.
    **/
   _applyPrefToSetting: function FullZoom__applyPrefToSetting(aValue, aBrowser) {
     if ((!this.siteSpecific) || gInPrintPreviewMode)
       return;
 
     var browser = aBrowser || (gBrowser && gBrowser.selectedBrowser);
     try {
-      if (browser.contentDocument instanceof Ci.nsIImageDocument)
-        ZoomManager.setZoomForBrowser(browser, 1);
-      else if (typeof aValue != "undefined")
+      if (browser.contentDocument instanceof ImageDocument)
+        return;
+
+      if (typeof aValue != "undefined")
         ZoomManager.setZoomForBrowser(browser, this._ensureValid(aValue));
       else if (typeof this.globalValue != "undefined")
         ZoomManager.setZoomForBrowser(browser, this.globalValue);
       else
         ZoomManager.setZoomForBrowser(browser, 1);
     }
     catch(ex) {}
   },
 
   _applySettingToPref: function FullZoom__applySettingToPref() {
     if (!this.siteSpecific || gInPrintPreviewMode ||
-        content.document instanceof Ci.nsIImageDocument)
+        content.document instanceof ImageDocument)
       return;
 
     var zoomLevel = ZoomManager.zoom;
     Services.contentPrefs.setPref(gBrowser.currentURI, this.name, zoomLevel);
   },
 
   _removePref: function FullZoom__removePref() {
-    if (!(content.document instanceof Ci.nsIImageDocument))
+    if (!(content.document instanceof ImageDocument))
       Services.contentPrefs.removePref(gBrowser.currentURI, this.name);
   },
 
 
   //**************************************************************************//
   // Utilities
 
   _ensureValid: function FullZoom__ensureValid(aValue) {
--- a/browser/base/content/test/Makefile.in
+++ b/browser/base/content/test/Makefile.in
@@ -140,16 +140,17 @@ endif
                  browser_bug561623.js \
                  browser_bug561636.js \
                  browser_bug562649.js \
                  browser_bug563588.js \
                  browser_bug565575.js \
                  browser_bug567306.js \
                  browser_zbug569342.js \
                  browser_bug575561.js \
+                 browser_bug575830.js \
                  browser_bug577121.js \
                  browser_bug578534.js \
                  browser_bug579872.js \
                  browser_bug580638.js \
                  browser_bug580956.js \
                  browser_bug581242.js \
                  browser_bug581253.js \
                  browser_bug581947.js \
--- a/browser/base/content/test/browser_bug386835.js
+++ b/browser/base/content/test/browser_bug386835.js
@@ -49,22 +49,20 @@ function thirdPageLoaded() {
     load(gTab1, gTestImage, imageLoaded);
   });
 
   gBrowser.selectedTab = gTab2;
 }
 
 function imageLoaded() {
   zoomTest(gTab1, 1, "Zoom should be 1 when image was loaded in the background");
-  afterZoom(function() {
-    zoomTest(gTab1, 1, "Zoom should still be 1 when tab with image is selected");
+  gBrowser.selectedTab = gTab1;
+  zoomTest(gTab1, 1, "Zoom should still be 1 when tab with image is selected");
 
-    executeSoon(imageZoomSwitch);
-  });
-  gBrowser.selectedTab = gTab1;
+  executeSoon(imageZoomSwitch);
 }
 
 function imageZoomSwitch() {
   navigate(BACK, function () {
     navigate(FORWARD, function () {
       zoomTest(gTab1, 1, "Tab 1 should not be zoomed when an image loads");
 
       afterZoom(function() {
@@ -131,17 +129,15 @@ function navigate(direction, cb) {
 
   if (direction == BACK)
     gBrowser.goBack();
   else if (direction == FORWARD)
     gBrowser.goForward();
 }
 
 function afterZoom(cb) {
-  let oldAPTS = FullZoom._applyPrefToSetting;
-  FullZoom._applyPrefToSetting = function(value, browser) {
-    if (!value)
-      value = undefined;
-    oldAPTS.call(FullZoom, value, browser);
-    FullZoom._applyPrefToSetting = oldAPTS;
+  let oldSZFB = ZoomManager.setZoomForBrowser;
+  ZoomManager.setZoomForBrowser = function(browser, value) {
+    oldSZFB.call(ZoomManager, browser, value);
+    ZoomManager.setZoomForBrowser = oldSZFB;
     executeSoon(cb);
   };
 }
--- a/browser/base/content/test/browser_bug416661.js
+++ b/browser/base/content/test/browser_bug416661.js
@@ -45,19 +45,17 @@ function test() {
 function afterZoomAndLoad(cb) {
   let didLoad = didZoom = false;
   tabElm.linkedBrowser.addEventListener("load", function() {
     tabElm.linkedBrowser.removeEventListener("load", arguments.callee, true);
     didLoad = true;
     if (didZoom)
       executeSoon(cb);
   }, true);
-  let oldAPTS = FullZoom._applyPrefToSetting;
-  FullZoom._applyPrefToSetting = function(value, browser) {
-    if (!value)
-      value = undefined;
-    oldAPTS.call(FullZoom, value, browser);
-    FullZoom._applyPrefToSetting = oldAPTS;
+  let oldSZFB = ZoomManager.setZoomForBrowser;
+  ZoomManager.setZoomForBrowser = function(browser, value) {
+    oldSZFB.call(ZoomManager, browser, value);
+    ZoomManager.setZoomForBrowser = oldSZFB;
     didZoom = true;
     if (didLoad)
       executeSoon(cb);
   };
 }
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/browser_bug575830.js
@@ -0,0 +1,37 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+function test() {
+  let tab1, tab2;
+  const TEST_IMAGE = "http://example.org/browser/browser/base/content/test/moz.png";
+
+  waitForExplicitFinish();
+  registerCleanupFunction(function cleanup() {
+    gBrowser.removeTab(tab1);
+    gBrowser.removeTab(tab2);
+  });
+
+  tab1 = gBrowser.addTab(TEST_IMAGE);
+  tab2 = gBrowser.addTab();
+  gBrowser.selectedTab = tab1;
+
+  tab1.linkedBrowser.addEventListener("load", function onload() {
+    tab1.linkedBrowser.removeEventListener("load", onload, true);
+    is(ZoomManager.zoom, 1, "initial zoom level for first should be 1");
+
+    FullZoom.enlarge();
+    let zoom = ZoomManager.zoom;
+    isnot(zoom, 1, "zoom level should have changed");
+
+    gBrowser.selectedTab = tab2;
+    is(ZoomManager.zoom, 1, "initial zoom level for second tab should be 1");
+
+    gBrowser.selectedTab = tab1;
+    is(ZoomManager.zoom, zoom, "zoom level for first tab should not have changed");
+
+    finish();
+  }, true);
+}
+