michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: michael@0: let testURL = "http://example.org/browser/browser/base/content/test/general/dummy_page.html"; michael@0: michael@0: function testOnWindow(aOptions, aCallback) { michael@0: whenNewWindowLoaded(aOptions, function(aWin) { michael@0: // execute should only be called when need, like when you are opening michael@0: // web pages on the test. If calling executeSoon() is not necesary, then michael@0: // call whenNewWindowLoaded() instead of testOnWindow() on your test. michael@0: executeSoon(function() aCallback(aWin)); michael@0: }); michael@0: }; michael@0: michael@0: testOnWindow({}, function(aNormalWindow) { michael@0: testOnWindow({private: true}, function(aPrivateWindow) { michael@0: runTest(aNormalWindow, aPrivateWindow, false, function() { michael@0: aNormalWindow.close(); michael@0: aPrivateWindow.close(); michael@0: testOnWindow({}, function(aNormalWindow) { michael@0: testOnWindow({private: true}, function(aPrivateWindow) { michael@0: runTest(aPrivateWindow, aNormalWindow, false, function() { michael@0: aNormalWindow.close(); michael@0: aPrivateWindow.close(); michael@0: testOnWindow({private: true}, function(aPrivateWindow) { michael@0: runTest(aPrivateWindow, aPrivateWindow, false, function() { michael@0: aPrivateWindow.close(); michael@0: testOnWindow({}, function(aNormalWindow) { michael@0: runTest(aNormalWindow, aNormalWindow, true, function() { michael@0: aNormalWindow.close(); michael@0: finish(); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: michael@0: function runTest(aSourceWindow, aDestWindow, aExpectSuccess, aCallback) { michael@0: // Open the base tab michael@0: let baseTab = aSourceWindow.gBrowser.addTab(testURL); michael@0: baseTab.linkedBrowser.addEventListener("load", function() { michael@0: // Wait for the tab to be fully loaded so matching happens correctly michael@0: if (baseTab.linkedBrowser.currentURI.spec == "about:blank") michael@0: return; michael@0: baseTab.linkedBrowser.removeEventListener("load", arguments.callee, true); michael@0: michael@0: let testTab = aDestWindow.gBrowser.addTab(); michael@0: michael@0: waitForFocus(function() { michael@0: // Select the testTab michael@0: aDestWindow.gBrowser.selectedTab = testTab; michael@0: michael@0: // Ensure that this tab has no history entries michael@0: ok(testTab.linkedBrowser.sessionHistory.count < 2, michael@0: "The test tab has 1 or less history entries"); michael@0: // Ensure that this tab is on about:blank michael@0: is(testTab.linkedBrowser.currentURI.spec, "about:blank", michael@0: "The test tab is on about:blank"); michael@0: // Ensure that this tab's document has no child nodes michael@0: ok(!testTab.linkedBrowser.contentDocument.body.hasChildNodes(), michael@0: "The test tab has no child nodes"); michael@0: ok(!testTab.hasAttribute("busy"), michael@0: "The test tab doesn't have the busy attribute"); michael@0: michael@0: // Set the urlbar to include the moz-action michael@0: aDestWindow.gURLBar.value = "moz-action:switchtab," + testURL; michael@0: // Focus the urlbar so we can press enter michael@0: aDestWindow.gURLBar.focus(); michael@0: michael@0: // We want to see if the switchtab action works. If it does, the michael@0: // current tab will get closed, and that's what we detect with the michael@0: // TabClose handler. If pressing enter triggers a load in that tab, michael@0: // then the load handler will get called. Neither of these are michael@0: // the desired effect here. So if the test goes successfully, it is michael@0: // the timeout handler which gets called. michael@0: // michael@0: // The reason that we can't avoid the timeout here is because we are michael@0: // trying to test something which should not happen, so we just need michael@0: // to wait for a while and then check whether any bad things have michael@0: // happened. michael@0: michael@0: function onTabClose(aEvent) { michael@0: aDestWindow.gBrowser.tabContainer.removeEventListener("TabClose", onTabClose, false); michael@0: aDestWindow.gBrowser.removeEventListener("load", onLoad, false); michael@0: clearTimeout(timeout); michael@0: // Should only happen when we expect success michael@0: ok(aExpectSuccess, "Tab closed as expected"); michael@0: aCallback(); michael@0: } michael@0: function onLoad(aEvent) { michael@0: aDestWindow.gBrowser.tabContainer.removeEventListener("TabClose", onTabClose, false); michael@0: aDestWindow.gBrowser.removeEventListener("load", onLoad, false); michael@0: clearTimeout(timeout); michael@0: // Should only happen when we expect success michael@0: ok(aExpectSuccess, "Tab loaded as expected"); michael@0: aCallback(); michael@0: } michael@0: michael@0: aDestWindow.gBrowser.tabContainer.addEventListener("TabClose", onTabClose, false); michael@0: aDestWindow.gBrowser.addEventListener("load", onLoad, false); michael@0: let timeout = setTimeout(function() { michael@0: aDestWindow.gBrowser.tabContainer.removeEventListener("TabClose", onTabClose, false); michael@0: aDestWindow.gBrowser.removeEventListener("load", onLoad, false); michael@0: aCallback(); michael@0: }, 500); michael@0: michael@0: // Press enter! michael@0: EventUtils.synthesizeKey("VK_RETURN", {}); michael@0: }, aDestWindow); michael@0: }, true); michael@0: } michael@0: } michael@0: