Bug 607482 - Part 2: Adjust the test so that the code doesn't need to unset the attribute asynchronously; r=dao a=gavin
authorEhsan Akhgari <ehsan@mozilla.com>
Sun, 14 Nov 2010 03:22:21 -0500
changeset 58614 23262313e08b2b7c2dd9ff8bff0c9515b9a73923
parent 58613 8515f6bd4306c6e7cd5c9df44e0618598215d80c
child 58615 445e8a8d79b2901826d1d400b176dae79b841d43
push id1
push usershaver@mozilla.com
push dateTue, 04 Jan 2011 17:58:04 +0000
reviewersdao, gavin
bugs607482
milestone2.0b8pre
Bug 607482 - Part 2: Adjust the test so that the code doesn't need to unset the attribute asynchronously; r=dao a=gavin
browser/base/content/browser.js
browser/components/privatebrowsing/test/browser/browser_privatebrowsing_fastswitch.js
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -7493,21 +7493,18 @@ let gPrivateBrowsingUI = {
     if (aTopic == "private-browsing") {
       if (aData == "enter")
         this.onEnterPrivateBrowsing();
       else if (aData == "exit")
         this.onExitPrivateBrowsing();
     }
     else if (aTopic == "private-browsing-transition-complete") {
       if (this._disableUIOnToggle) {
-        // use setTimeout here in order to make the code testable
-        setTimeout(function() {
-          document.getElementById("Tools:PrivateBrowsing")
-                  .removeAttribute("disabled");
-        }, 0);
+        document.getElementById("Tools:PrivateBrowsing")
+                .removeAttribute("disabled");
       }
     }
   },
 
   _shouldEnter: function PBUI__shouldEnter() {
     try {
       // Never prompt if the session is not going to be closed, or if user has
       // already requested not to be prompted.
--- a/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_fastswitch.js
+++ b/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_fastswitch.js
@@ -43,42 +43,50 @@ function test() {
   let pb = Cc["@mozilla.org/privatebrowsing;1"].
            getService(Ci.nsIPrivateBrowsingService);
   let pbCmd = document.getElementById("Tools:PrivateBrowsing");
   waitForExplicitFinish();
 
   let pass = 1;
   function observer(aSubject, aTopic, aData) {
     switch (aTopic) {
-      case "private-browsing":
-        setTimeout(function () {
-          ok(document.getElementById("Tools:PrivateBrowsing").hasAttribute("disabled"),
-             "The private browsing command should be disabled immediately after the mode switch");
-        }, 0);
-        break;
-
       case "private-browsing-transition-complete":
         if (pass++ == 1) {
           setTimeout(function () {
             ok(!pbCmd.hasAttribute("disabled"),
                "The private browsing command should be re-enabled after entering the private browsing mode");
 
             pb.privateBrowsingEnabled = false;
           }, 100);
         }
         else {
           setTimeout(function () {
             ok(!pbCmd.hasAttribute("disabled"),
                "The private browsing command should be re-enabled after exiting the private browsing mode");
 
-            Services.obs.removeObserver(observer, "private-browsing");
-            Services.obs.removeObserver(observer, "private-browsing-transition-complete");
             finish();
           }, 100);
         }
         break;
     }
+    Services.obs.removeObserver(observer, "private-browsing-transition-complete");
   }
-  Services.obs.addObserver(observer, "private-browsing", false);
-  Services.obs.addObserver(observer, "private-browsing-transition-complete", false);
+  let originalOnEnter = gPrivateBrowsingUI.onEnterPrivateBrowsing;
+  gPrivateBrowsingUI.onEnterPrivateBrowsing = function() {
+    originalOnEnter.apply(gPrivateBrowsingUI, arguments);
+    ok(pbCmd.hasAttribute("disabled"),
+       "The private browsing command should be disabled right after entering the private browsing mode");
+    Services.obs.addObserver(observer, "private-browsing-transition-complete", false);
+  };
+  let originalOnExit = gPrivateBrowsingUI.onExitPrivateBrowsing;
+  gPrivateBrowsingUI.onExitPrivateBrowsing = function() {
+    originalOnExit.apply(gPrivateBrowsingUI, arguments);
+    ok(pbCmd.hasAttribute("disabled"),
+       "The private browsing command should be disabled right after exiting the private browsing mode");
+    Services.obs.addObserver(observer, "private-browsing-transition-complete", false);
+  };
+  registerCleanupFunction(function() {
+    gPrivateBrowsingUI.onEnterPrivateBrowsing = originalOnEnter;
+    gPrivateBrowsingUI.onExitPrivateBrowsing = originalOnExit;
+  });
 
   pb.privateBrowsingEnabled = true;
 }