1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/base/content/test/general/browser_bug816527.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,122 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +function test() { 1.9 + waitForExplicitFinish(); 1.10 + 1.11 + let testURL = "http://example.org/browser/browser/base/content/test/general/dummy_page.html"; 1.12 + 1.13 + function testOnWindow(aOptions, aCallback) { 1.14 + whenNewWindowLoaded(aOptions, function(aWin) { 1.15 + // execute should only be called when need, like when you are opening 1.16 + // web pages on the test. If calling executeSoon() is not necesary, then 1.17 + // call whenNewWindowLoaded() instead of testOnWindow() on your test. 1.18 + executeSoon(function() aCallback(aWin)); 1.19 + }); 1.20 + }; 1.21 + 1.22 + testOnWindow({}, function(aNormalWindow) { 1.23 + testOnWindow({private: true}, function(aPrivateWindow) { 1.24 + runTest(aNormalWindow, aPrivateWindow, false, function() { 1.25 + aNormalWindow.close(); 1.26 + aPrivateWindow.close(); 1.27 + testOnWindow({}, function(aNormalWindow) { 1.28 + testOnWindow({private: true}, function(aPrivateWindow) { 1.29 + runTest(aPrivateWindow, aNormalWindow, false, function() { 1.30 + aNormalWindow.close(); 1.31 + aPrivateWindow.close(); 1.32 + testOnWindow({private: true}, function(aPrivateWindow) { 1.33 + runTest(aPrivateWindow, aPrivateWindow, false, function() { 1.34 + aPrivateWindow.close(); 1.35 + testOnWindow({}, function(aNormalWindow) { 1.36 + runTest(aNormalWindow, aNormalWindow, true, function() { 1.37 + aNormalWindow.close(); 1.38 + finish(); 1.39 + }); 1.40 + }); 1.41 + }); 1.42 + }); 1.43 + }); 1.44 + }); 1.45 + }); 1.46 + }); 1.47 + }); 1.48 + }); 1.49 + 1.50 + function runTest(aSourceWindow, aDestWindow, aExpectSuccess, aCallback) { 1.51 + // Open the base tab 1.52 + let baseTab = aSourceWindow.gBrowser.addTab(testURL); 1.53 + baseTab.linkedBrowser.addEventListener("load", function() { 1.54 + // Wait for the tab to be fully loaded so matching happens correctly 1.55 + if (baseTab.linkedBrowser.currentURI.spec == "about:blank") 1.56 + return; 1.57 + baseTab.linkedBrowser.removeEventListener("load", arguments.callee, true); 1.58 + 1.59 + let testTab = aDestWindow.gBrowser.addTab(); 1.60 + 1.61 + waitForFocus(function() { 1.62 + // Select the testTab 1.63 + aDestWindow.gBrowser.selectedTab = testTab; 1.64 + 1.65 + // Ensure that this tab has no history entries 1.66 + ok(testTab.linkedBrowser.sessionHistory.count < 2, 1.67 + "The test tab has 1 or less history entries"); 1.68 + // Ensure that this tab is on about:blank 1.69 + is(testTab.linkedBrowser.currentURI.spec, "about:blank", 1.70 + "The test tab is on about:blank"); 1.71 + // Ensure that this tab's document has no child nodes 1.72 + ok(!testTab.linkedBrowser.contentDocument.body.hasChildNodes(), 1.73 + "The test tab has no child nodes"); 1.74 + ok(!testTab.hasAttribute("busy"), 1.75 + "The test tab doesn't have the busy attribute"); 1.76 + 1.77 + // Set the urlbar to include the moz-action 1.78 + aDestWindow.gURLBar.value = "moz-action:switchtab," + testURL; 1.79 + // Focus the urlbar so we can press enter 1.80 + aDestWindow.gURLBar.focus(); 1.81 + 1.82 + // We want to see if the switchtab action works. If it does, the 1.83 + // current tab will get closed, and that's what we detect with the 1.84 + // TabClose handler. If pressing enter triggers a load in that tab, 1.85 + // then the load handler will get called. Neither of these are 1.86 + // the desired effect here. So if the test goes successfully, it is 1.87 + // the timeout handler which gets called. 1.88 + // 1.89 + // The reason that we can't avoid the timeout here is because we are 1.90 + // trying to test something which should not happen, so we just need 1.91 + // to wait for a while and then check whether any bad things have 1.92 + // happened. 1.93 + 1.94 + function onTabClose(aEvent) { 1.95 + aDestWindow.gBrowser.tabContainer.removeEventListener("TabClose", onTabClose, false); 1.96 + aDestWindow.gBrowser.removeEventListener("load", onLoad, false); 1.97 + clearTimeout(timeout); 1.98 + // Should only happen when we expect success 1.99 + ok(aExpectSuccess, "Tab closed as expected"); 1.100 + aCallback(); 1.101 + } 1.102 + function onLoad(aEvent) { 1.103 + aDestWindow.gBrowser.tabContainer.removeEventListener("TabClose", onTabClose, false); 1.104 + aDestWindow.gBrowser.removeEventListener("load", onLoad, false); 1.105 + clearTimeout(timeout); 1.106 + // Should only happen when we expect success 1.107 + ok(aExpectSuccess, "Tab loaded as expected"); 1.108 + aCallback(); 1.109 + } 1.110 + 1.111 + aDestWindow.gBrowser.tabContainer.addEventListener("TabClose", onTabClose, false); 1.112 + aDestWindow.gBrowser.addEventListener("load", onLoad, false); 1.113 + let timeout = setTimeout(function() { 1.114 + aDestWindow.gBrowser.tabContainer.removeEventListener("TabClose", onTabClose, false); 1.115 + aDestWindow.gBrowser.removeEventListener("load", onLoad, false); 1.116 + aCallback(); 1.117 + }, 500); 1.118 + 1.119 + // Press enter! 1.120 + EventUtils.synthesizeKey("VK_RETURN", {}); 1.121 + }, aDestWindow); 1.122 + }, true); 1.123 + } 1.124 +} 1.125 +