browser/base/content/test/general/browser_bug816527.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 function test() {
     6   waitForExplicitFinish();
     8   let testURL = "http://example.org/browser/browser/base/content/test/general/dummy_page.html";
    10   function testOnWindow(aOptions, aCallback) {
    11     whenNewWindowLoaded(aOptions, function(aWin) {
    12       // execute should only be called when need, like when you are opening
    13       // web pages on the test. If calling executeSoon() is not necesary, then
    14       // call whenNewWindowLoaded() instead of testOnWindow() on your test.
    15       executeSoon(function() aCallback(aWin));
    16     });
    17   };
    19   testOnWindow({}, function(aNormalWindow) {
    20     testOnWindow({private: true}, function(aPrivateWindow) {
    21       runTest(aNormalWindow, aPrivateWindow, false, function() {
    22         aNormalWindow.close();
    23         aPrivateWindow.close();
    24         testOnWindow({}, function(aNormalWindow) {
    25           testOnWindow({private: true}, function(aPrivateWindow) {
    26             runTest(aPrivateWindow, aNormalWindow, false, function() {
    27               aNormalWindow.close();
    28               aPrivateWindow.close();
    29               testOnWindow({private: true}, function(aPrivateWindow) {
    30                 runTest(aPrivateWindow, aPrivateWindow, false, function() {
    31                   aPrivateWindow.close();
    32                   testOnWindow({}, function(aNormalWindow) {
    33                     runTest(aNormalWindow, aNormalWindow, true, function() {
    34                       aNormalWindow.close();
    35                       finish();
    36                     });
    37                   });
    38                 });
    39               });
    40             });
    41           });
    42         });
    43       });
    44     });
    45   });
    47   function runTest(aSourceWindow, aDestWindow, aExpectSuccess, aCallback) {
    48     // Open the base tab
    49     let baseTab = aSourceWindow.gBrowser.addTab(testURL);
    50     baseTab.linkedBrowser.addEventListener("load", function() {
    51       // Wait for the tab to be fully loaded so matching happens correctly
    52       if (baseTab.linkedBrowser.currentURI.spec == "about:blank")
    53         return;
    54       baseTab.linkedBrowser.removeEventListener("load", arguments.callee, true);
    56       let testTab = aDestWindow.gBrowser.addTab();
    58       waitForFocus(function() {
    59         // Select the testTab
    60         aDestWindow.gBrowser.selectedTab = testTab;
    62         // Ensure that this tab has no history entries
    63         ok(testTab.linkedBrowser.sessionHistory.count < 2,
    64            "The test tab has 1 or less history entries");
    65         // Ensure that this tab is on about:blank
    66         is(testTab.linkedBrowser.currentURI.spec, "about:blank",
    67            "The test tab is on about:blank");
    68         // Ensure that this tab's document has no child nodes
    69         ok(!testTab.linkedBrowser.contentDocument.body.hasChildNodes(),
    70            "The test tab has no child nodes");
    71         ok(!testTab.hasAttribute("busy"),
    72            "The test tab doesn't have the busy attribute");
    74         // Set the urlbar to include the moz-action
    75         aDestWindow.gURLBar.value = "moz-action:switchtab," + testURL;
    76         // Focus the urlbar so we can press enter
    77         aDestWindow.gURLBar.focus();
    79         // We want to see if the switchtab action works.  If it does, the
    80         // current tab will get closed, and that's what we detect with the
    81         // TabClose handler.  If pressing enter triggers a load in that tab,
    82         // then the load handler will get called.  Neither of these are
    83         // the desired effect here.  So if the test goes successfully, it is
    84         // the timeout handler which gets called.
    85         //
    86         // The reason that we can't avoid the timeout here is because we are
    87         // trying to test something which should not happen, so we just need
    88         // to wait for a while and then check whether any bad things have
    89         // happened.
    91         function onTabClose(aEvent) {
    92           aDestWindow.gBrowser.tabContainer.removeEventListener("TabClose", onTabClose, false);
    93           aDestWindow.gBrowser.removeEventListener("load", onLoad, false);
    94           clearTimeout(timeout);
    95           // Should only happen when we expect success
    96           ok(aExpectSuccess, "Tab closed as expected");
    97           aCallback();
    98         }
    99         function onLoad(aEvent) {
   100           aDestWindow.gBrowser.tabContainer.removeEventListener("TabClose", onTabClose, false);
   101           aDestWindow.gBrowser.removeEventListener("load", onLoad, false);
   102           clearTimeout(timeout);
   103           // Should only happen when we expect success
   104           ok(aExpectSuccess, "Tab loaded as expected");
   105           aCallback();
   106         }
   108         aDestWindow.gBrowser.tabContainer.addEventListener("TabClose", onTabClose, false);
   109         aDestWindow.gBrowser.addEventListener("load", onLoad, false);
   110         let timeout = setTimeout(function() {
   111           aDestWindow.gBrowser.tabContainer.removeEventListener("TabClose", onTabClose, false);
   112           aDestWindow.gBrowser.removeEventListener("load", onLoad, false);
   113           aCallback();
   114         }, 500);
   116         // Press enter!
   117         EventUtils.synthesizeKey("VK_RETURN", {});
   118       }, aDestWindow);
   119     }, true);
   120   }
   121 }

mercurial