toolkit/mozapps/downloads/tests/chrome/test_destinationURI_annotation.xul

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial