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: try { michael@0: if (Services.prefs.getBoolPref("browser.download.useJSTransfer")) { michael@0: return; michael@0: } michael@0: } catch (ex) { } michael@0: michael@0: waitForExplicitFinish(); michael@0: michael@0: let privateWin = null; michael@0: let itemCount = 0; michael@0: let sourceURL = michael@0: "http://example.org/tests/toolkit/components/downloads/test/browser/download.html"; michael@0: let linkURL = michael@0: "http://mochi.test:8888/tests/toolkit/components/downloads/test/browser/download.test"; michael@0: let linkURI = Services.io.newURI(linkURL, null, null); michael@0: let downloadDialogURL = michael@0: "chrome://mozapps/content/downloads/unknownContentType.xul"; michael@0: michael@0: let downloadListener = { michael@0: onDownloadStateChange: function(aState, aDownload) { michael@0: switch (aDownload.state) { michael@0: case Services.downloads.DOWNLOAD_FINISHED: michael@0: info("Download finished"); michael@0: Services.downloads.removeListener(downloadListener); michael@0: executeSoon(function() { checkDownload(aDownload); }); michael@0: break; michael@0: } michael@0: }, michael@0: onStateChange: function(a, b, c, d, e) { }, michael@0: onProgressChange: function(a, b, c, d, e, f, g) { }, michael@0: onSecurityChange: function(a, b, c, d) { } michael@0: }; michael@0: michael@0: let historyObserver = { michael@0: onVisit: function (aURI, aVisitID, aTime, aSessionID, aReferringID, aTransitionType) { michael@0: ok(false, "Download should not fired a visit notification: " + aURI.spec); michael@0: }, michael@0: onBeginUpdateBatch: function () {}, michael@0: onEndUpdateBatch: function () {}, michael@0: onTitleChanged: function () {}, michael@0: onBeforeDeleteURI: function () {}, michael@0: onDeleteURI: function () {}, michael@0: onClearHistory: function () {}, michael@0: onPageChanged: function () {}, michael@0: onDeleteVisits: function () {}, michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsINavHistoryObserver]) michael@0: }; michael@0: michael@0: let windowListener = { michael@0: onOpenWindow: function(aXULWindow) { michael@0: info("Window opened"); michael@0: Services.wm.removeListener(windowListener); michael@0: michael@0: let domWindow = michael@0: aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor). michael@0: getInterface(Ci.nsIDOMWindow); michael@0: waitForFocus(function() { michael@0: is(domWindow.document.location.href, downloadDialogURL, michael@0: "Should have seen the right window open"); michael@0: michael@0: executeSoon(function() { michael@0: let button = domWindow.document.documentElement.getButton("accept"); michael@0: button.disabled = false; michael@0: domWindow.document.documentElement.acceptDialog(); michael@0: }); michael@0: }, domWindow); michael@0: }, michael@0: onCloseWindow: function(aXULWindow) {}, michael@0: onWindowTitleChange: function(aXULWindow, aNewTitle) {} michael@0: }; michael@0: michael@0: registerCleanupFunction(function() { michael@0: privateWin.close(); michael@0: Services.prefs.clearUserPref("browser.download.manager.showAlertOnComplete"); michael@0: }); michael@0: michael@0: function getHistoryItemCount() { michael@0: let options = PlacesUtils.history.getNewQueryOptions(); michael@0: options.includeHidden = true; michael@0: options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY; michael@0: let query = PlacesUtils.history.getNewQuery(); michael@0: let root = PlacesUtils.history.executeQuery(query, options).root; michael@0: root.containerOpen = true; michael@0: let cc = root.childCount; michael@0: root.containerOpen = false; michael@0: return cc; michael@0: } michael@0: michael@0: function whenNewWindowLoaded(aIsPrivate, aCallback) { michael@0: let win = OpenBrowserWindow({private: aIsPrivate}); michael@0: win.addEventListener("load", function onLoad() { michael@0: win.removeEventListener("load", onLoad, false); michael@0: executeSoon(function() aCallback(win)); michael@0: }, false); michael@0: } michael@0: michael@0: function whenPageLoad(aWin, aURL, aCallback) { michael@0: let browser = aWin.gBrowser.selectedBrowser; michael@0: browser.addEventListener("load", function onLoad() { michael@0: browser.removeEventListener("load", onLoad, true); michael@0: executeSoon(function() aCallback(browser.contentDocument)); michael@0: }, true); michael@0: browser.loadURI(aURL); michael@0: } michael@0: michael@0: function checkDownload(aDownload) { michael@0: PlacesUtils.history.removeObserver(historyObserver); michael@0: ok(aDownload.isPrivate, "Download should be private"); michael@0: is(getHistoryItemCount(), itemCount, michael@0: "History items count should not change after a download"); michael@0: PlacesUtils.asyncHistory.isURIVisited(linkURI, function(aURI, aIsVisited) { michael@0: is(aIsVisited, false, "Download source should not be set as visited"); michael@0: // Clean up michael@0: if (aDownload.targetFile.exists()) { michael@0: aDownload.targetFile.remove(false); michael@0: } michael@0: waitForFocus(function() { michael@0: if (privateWin.DownloadsPanel.isPanelShowing) { michael@0: privateWin.DownloadsPanel.hidePanel(); michael@0: } michael@0: finish(); michael@0: }, privateWin); michael@0: }); michael@0: } michael@0: michael@0: // Disable alert service notifications michael@0: Services.prefs.setBoolPref("browser.download.manager.showAlertOnComplete", false); michael@0: michael@0: whenNewWindowLoaded(true, function(win) { michael@0: info("Start listeners"); michael@0: privateWin = win; michael@0: Services.wm.addListener(windowListener); michael@0: Services.downloads.addPrivacyAwareListener(downloadListener); michael@0: PlacesUtils.history.addObserver(historyObserver, false); michael@0: info("Load test page"); michael@0: whenPageLoad(win, sourceURL, function(doc) { michael@0: info("Start download"); michael@0: itemCount = getHistoryItemCount(); michael@0: doc.getElementById("download-link").click(); michael@0: }); michael@0: }); michael@0: }