michael@0: function test () { michael@0: waitForExplicitFinish(); michael@0: michael@0: var isHTTPS = false; michael@0: michael@0: gBrowser.selectedTab = gBrowser.addTab(); michael@0: gBrowser.selectedBrowser.addEventListener("load", function () { michael@0: if (isHTTPS) { michael@0: gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true); michael@0: } michael@0: let doc = gBrowser.contentDocument; michael@0: michael@0: michael@0: function testLocation(link, url, next) { michael@0: var tabOpenListener = new TabOpenListener(url, function () { michael@0: gBrowser.removeTab(this.tab); michael@0: }, function () { michael@0: next(); michael@0: }); michael@0: michael@0: doc.getElementById(link).click(); michael@0: } michael@0: michael@0: function testLink(link, name, next) { michael@0: addWindowListener("chrome://mozapps/content/downloads/unknownContentType.xul", function (win) { michael@0: is(doc.getElementById("unload-flag").textContent, "Okay", "beforeunload shouldn't have fired"); michael@0: is(win.document.getElementById("location").value, name, "file name should match"); michael@0: win.close(); michael@0: next(); michael@0: }); michael@0: michael@0: doc.getElementById(link).click(); michael@0: } michael@0: michael@0: testLink("link1", "test.txt", michael@0: testLink.bind(null, "link2", "video.ogg", michael@0: testLink.bind(null, "link3", "just some video", michael@0: testLink.bind(null, "link4", "with-target.txt", michael@0: testLink.bind(null, "link5", "javascript.txt", michael@0: testLink.bind(null, "link6", "test.blob", michael@0: testLocation.bind(null, "link7", "http://example.com/", michael@0: function () { michael@0: if (isHTTPS) { michael@0: gBrowser.removeCurrentTab(); michael@0: finish(); michael@0: } else { michael@0: // same test again with https: michael@0: isHTTPS = true; michael@0: content.location = "https://example.com:443/browser/browser/base/content/test/general/download_page.html"; michael@0: } michael@0: }))))))); michael@0: michael@0: }, true); michael@0: michael@0: content.location = "http://mochi.test:8888/browser/browser/base/content/test/general/download_page.html"; michael@0: } michael@0: michael@0: michael@0: function addWindowListener(aURL, aCallback) { michael@0: Services.wm.addListener({ michael@0: onOpenWindow: function(aXULWindow) { michael@0: info("window opened, waiting for focus"); michael@0: Services.wm.removeListener(this); michael@0: michael@0: var domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor) michael@0: .getInterface(Ci.nsIDOMWindow); michael@0: waitForFocus(function() { michael@0: is(domwindow.document.location.href, aURL, "should have seen the right window open"); michael@0: aCallback(domwindow); michael@0: }, domwindow); michael@0: }, michael@0: onCloseWindow: function(aXULWindow) { }, michael@0: onWindowTitleChange: function(aXULWindow, aNewTitle) { } michael@0: }); michael@0: } michael@0: michael@0: // This listens for the next opened tab and checks it is of the right url. michael@0: // opencallback is called when the new tab is fully loaded michael@0: // closecallback is called when the tab is closed michael@0: function TabOpenListener(url, opencallback, closecallback) { michael@0: this.url = url; michael@0: this.opencallback = opencallback; michael@0: this.closecallback = closecallback; michael@0: michael@0: gBrowser.tabContainer.addEventListener("TabOpen", this, false); michael@0: } michael@0: michael@0: TabOpenListener.prototype = { michael@0: url: null, michael@0: opencallback: null, michael@0: closecallback: null, michael@0: tab: null, michael@0: browser: null, michael@0: michael@0: handleEvent: function(event) { michael@0: if (event.type == "TabOpen") { michael@0: gBrowser.tabContainer.removeEventListener("TabOpen", this, false); michael@0: this.tab = event.originalTarget; michael@0: this.browser = this.tab.linkedBrowser; michael@0: gBrowser.addEventListener("pageshow", this, false); michael@0: } else if (event.type == "pageshow") { michael@0: if (event.target.location.href != this.url) michael@0: return; michael@0: gBrowser.removeEventListener("pageshow", this, false); michael@0: this.tab.addEventListener("TabClose", this, false); michael@0: var url = this.browser.contentDocument.location.href; michael@0: is(url, this.url, "Should have opened the correct tab"); michael@0: this.opencallback(this.tab, this.browser.contentWindow); michael@0: } else if (event.type == "TabClose") { michael@0: if (event.originalTarget != this.tab) michael@0: return; michael@0: this.tab.removeEventListener("TabClose", this, false); michael@0: this.opencallback = null; michael@0: this.tab = null; michael@0: this.browser = null; michael@0: // Let the window close complete michael@0: executeSoon(this.closecallback); michael@0: this.closecallback = null; michael@0: } michael@0: } michael@0: };