Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
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 try {
7 if (Services.prefs.getBoolPref("browser.download.useJSTransfer")) {
8 return;
9 }
10 } catch (ex) { }
12 waitForExplicitFinish();
14 let privateWin = null;
15 let itemCount = 0;
16 let sourceURL =
17 "http://example.org/tests/toolkit/components/downloads/test/browser/download.html";
18 let linkURL =
19 "http://mochi.test:8888/tests/toolkit/components/downloads/test/browser/download.test";
20 let linkURI = Services.io.newURI(linkURL, null, null);
21 let downloadDialogURL =
22 "chrome://mozapps/content/downloads/unknownContentType.xul";
24 let downloadListener = {
25 onDownloadStateChange: function(aState, aDownload) {
26 switch (aDownload.state) {
27 case Services.downloads.DOWNLOAD_FINISHED:
28 info("Download finished");
29 Services.downloads.removeListener(downloadListener);
30 executeSoon(function() { checkDownload(aDownload); });
31 break;
32 }
33 },
34 onStateChange: function(a, b, c, d, e) { },
35 onProgressChange: function(a, b, c, d, e, f, g) { },
36 onSecurityChange: function(a, b, c, d) { }
37 };
39 let historyObserver = {
40 onVisit: function (aURI, aVisitID, aTime, aSessionID, aReferringID, aTransitionType) {
41 ok(false, "Download should not fired a visit notification: " + aURI.spec);
42 },
43 onBeginUpdateBatch: function () {},
44 onEndUpdateBatch: function () {},
45 onTitleChanged: function () {},
46 onBeforeDeleteURI: function () {},
47 onDeleteURI: function () {},
48 onClearHistory: function () {},
49 onPageChanged: function () {},
50 onDeleteVisits: function () {},
51 QueryInterface: XPCOMUtils.generateQI([Ci.nsINavHistoryObserver])
52 };
54 let windowListener = {
55 onOpenWindow: function(aXULWindow) {
56 info("Window opened");
57 Services.wm.removeListener(windowListener);
59 let domWindow =
60 aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).
61 getInterface(Ci.nsIDOMWindow);
62 waitForFocus(function() {
63 is(domWindow.document.location.href, downloadDialogURL,
64 "Should have seen the right window open");
66 executeSoon(function() {
67 let button = domWindow.document.documentElement.getButton("accept");
68 button.disabled = false;
69 domWindow.document.documentElement.acceptDialog();
70 });
71 }, domWindow);
72 },
73 onCloseWindow: function(aXULWindow) {},
74 onWindowTitleChange: function(aXULWindow, aNewTitle) {}
75 };
77 registerCleanupFunction(function() {
78 privateWin.close();
79 Services.prefs.clearUserPref("browser.download.manager.showAlertOnComplete");
80 });
82 function getHistoryItemCount() {
83 let options = PlacesUtils.history.getNewQueryOptions();
84 options.includeHidden = true;
85 options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY;
86 let query = PlacesUtils.history.getNewQuery();
87 let root = PlacesUtils.history.executeQuery(query, options).root;
88 root.containerOpen = true;
89 let cc = root.childCount;
90 root.containerOpen = false;
91 return cc;
92 }
94 function whenNewWindowLoaded(aIsPrivate, aCallback) {
95 let win = OpenBrowserWindow({private: aIsPrivate});
96 win.addEventListener("load", function onLoad() {
97 win.removeEventListener("load", onLoad, false);
98 executeSoon(function() aCallback(win));
99 }, false);
100 }
102 function whenPageLoad(aWin, aURL, aCallback) {
103 let browser = aWin.gBrowser.selectedBrowser;
104 browser.addEventListener("load", function onLoad() {
105 browser.removeEventListener("load", onLoad, true);
106 executeSoon(function() aCallback(browser.contentDocument));
107 }, true);
108 browser.loadURI(aURL);
109 }
111 function checkDownload(aDownload) {
112 PlacesUtils.history.removeObserver(historyObserver);
113 ok(aDownload.isPrivate, "Download should be private");
114 is(getHistoryItemCount(), itemCount,
115 "History items count should not change after a download");
116 PlacesUtils.asyncHistory.isURIVisited(linkURI, function(aURI, aIsVisited) {
117 is(aIsVisited, false, "Download source should not be set as visited");
118 // Clean up
119 if (aDownload.targetFile.exists()) {
120 aDownload.targetFile.remove(false);
121 }
122 waitForFocus(function() {
123 if (privateWin.DownloadsPanel.isPanelShowing) {
124 privateWin.DownloadsPanel.hidePanel();
125 }
126 finish();
127 }, privateWin);
128 });
129 }
131 // Disable alert service notifications
132 Services.prefs.setBoolPref("browser.download.manager.showAlertOnComplete", false);
134 whenNewWindowLoaded(true, function(win) {
135 info("Start listeners");
136 privateWin = win;
137 Services.wm.addListener(windowListener);
138 Services.downloads.addPrivacyAwareListener(downloadListener);
139 PlacesUtils.history.addObserver(historyObserver, false);
140 info("Load test page");
141 whenPageLoad(win, sourceURL, function(doc) {
142 info("Start download");
143 itemCount = getHistoryItemCount();
144 doc.getElementById("download-link").click();
145 });
146 });
147 }