Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 <?xml version="1.0"?>
2 <!--
3 /* Any copyright is dedicated to the Public Domain.
4 http://creativecommons.org/publicdomain/zero/1.0/ */
6 /**
7 * Bug 701607 - This test verifies that the destinationFileName and destinationFileURI
8 * annotations are set even for files that didn't have custom file names chosen, e.g.
9 * through the unknownContentType window.
10 */
11 -->
13 <window title="Test destinationFileURI annotation"
14 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
15 onload="init()">
17 <script type="application/javascript"
18 src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
19 <script type="application/javascript"
20 src="utils.js"/>
22 <script type="application/javascript">
23 <![CDATA[
25 const UCT_URI = "chrome://mozapps/content/downloads/unknownContentType.xul";
27 // This file will trigger the simple UI, where only the Save and Cancel buttons are available
28 const DOWNLOAD_URI = "http://mochi.test:8888/chrome/toolkit/mozapps/downloads/tests/chrome/unknownContentType_dialog_layout_data.pif";
29 const FILE_NAME = "unknownContentType_dialog_layout_data.pif";
31 let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"]
32 .getService(Ci.nsIWindowWatcher);
34 let dm = Cc["@mozilla.org/download-manager;1"]
35 .getService(Ci.nsIDownloadManager);
37 let os = Cc["@mozilla.org/observer-service;1"]
38 .getService(Ci.nsIObserverService);
40 Components.utils.import("resource://gre/modules/Services.jsm");
41 Components.utils.import("resource://gre/modules/PlacesUtils.jsm");
43 let checkDestination = false,
44 checkFileName = false;
46 SimpleTest.waitForExplicitFinish();
48 let annoObserver = {
49 onPageAnnotationSet: function AO_onPageAnnotationSet(aPage, aName){
50 if (aPage.spec == DOWNLOAD_URI) {
51 let value = PlacesUtils.annotations.getPageAnnotation(aPage, aName);
52 switch (aName) {
53 case "downloads/destinationFileURI":
54 checkDestination = true;
55 ok(value.indexOf(FILE_NAME) != -1, "file destination was set correctly");
56 break;
58 case "downloads/destinationFileName":
59 checkFileName = true;
60 ok(value == FILE_NAME, "file name was set correctly");
61 break;
62 }
63 }
64 },
65 onItemAnnotationSet: function() {},
66 onPageAnnotationRemoved: function() {},
67 onItemAnnotationRemoved: function() {}
68 }
70 let TestFinisher = {
71 _dmuiDone: false,
72 _callback: null,
74 observe: function(aSubject, aTopic, aData) {
75 os.removeObserver(TestFinisher, "download-manager-ui-done");
77 if (this._callback) {
78 SimpleTest.executeSoon(this._callback);
79 this._callback = null;
80 } else {
81 this._dmuiDone = true;
82 }
83 },
85 waitForDMUIDone: function(callback) {
86 if (this._dmuiDone) {
87 SimpleTest.executeSoon(callback);
88 } else {
89 this._callback = callback;
90 }
91 }
92 };
94 os.addObserver(TestFinisher, "download-manager-ui-done", false);
96 let downloadListener = {
98 onDownloadStateChange: function(aState, aDownload) {
99 if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED) {
100 is(aDownload.source.spec, DOWNLOAD_URI, "file was downloaded");
101 dm.removeDownload(aDownload.id);
103 try {
104 aDownload.targetFile.remove(false);
105 } catch (ex) {}
107 TestFinisher.waitForDMUIDone(endTest);
109 }
110 },
112 onStateChange: function() {},
113 onProgressChange: function() {},
114 onSecurityChange: function() {}
115 };
117 function init() {
118 var dmui = getDMUI();
119 if (!dmui) {
120 todo(false, "skip test for toolkit download manager UI");
121 SimpleTest.finish();
122 return;
123 }
125 // Ensure that the download manager callbacks always use the window UI instead
126 // of the panel in the browser's window.
127 Services.prefs.setBoolPref("browser.download.useToolkitUI", true);
129 // Check if the file we're trying to download already exists in the destination
130 // folder, and if so, remove it first to ensure that the test works properly.
131 let fileToDownload = dm.userDownloadsDirectory.clone();
132 fileToDownload.append(FILE_NAME);
133 if (fileToDownload.exists()) {
134 fileToDownload.remove(false);
135 info("File to download had to be removed as it already existed.");
136 }
138 ww.registerNotification(windowObserver);
139 PlacesUtils.annotations.addObserver(annoObserver, false);
140 dm.addListener(downloadListener);
142 let frame = document.getElementById("testframe");
143 frame.setAttribute("src", DOWNLOAD_URI);
144 }
146 let windowObserver = {
147 observe: function(aSubject, aTopic, aData) {
148 if (aTopic != "domwindowopened") {
149 return;
150 }
152 let win = aSubject.QueryInterface(Ci.nsIDOMEventTarget);
154 win.addEventListener("load", function onLoad(event) {
155 win.removeEventListener("load", onLoad, false);
157 if (win.location == UCT_URI) {
158 ww.unregisterNotification(windowObserver);
159 SimpleTest.executeSoon(function() {
160 win.document.documentElement._fireButtonEvent("accept");
161 win.close();
162 win = null;
163 });
164 }
165 }, false);
166 }
167 };
169 function endTest() {
170 ok(checkDestination, "file destination was set");
171 ok(checkFileName, "file name was set");
173 PlacesUtils.annotations.removeObserver(annoObserver);
174 dm.removeListener(downloadListener);
176 ww = null;
177 PlacesUtils = null;
178 dm = null;
179 os = null;
181 Cc["@mozilla.org/appshell/window-mediator;1"]
182 .getService(Ci.nsIWindowMediator)
183 .getMostRecentWindow("Download:Manager")
184 .close();
186 Services.prefs.clearUserPref("browser.download.useToolkitUI");
188 waitForClearHistory(SimpleTest.finish);
189 }
191 ]]>
192 </script>
194 <body xmlns="http://www.w3.org/1999/xhtml">
195 <p id="display"></p>
196 <div id="content" style="display:none;"></div>
197 <pre id="test"></pre>
198 </body>
200 <iframe xmlns="http://www.w3.org/1999/xhtml"
201 id="testframe">
202 </iframe>
203 </window>