toolkit/components/downloads/test/unit/test_bug_401430.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 // This tests the "add to recent documents" functionality of the DM
michael@0 6
michael@0 7 const nsIDownloadManager = Ci.nsIDownloadManager;
michael@0 8 const dm = Cc["@mozilla.org/download-manager;1"].getService(nsIDownloadManager);
michael@0 9
michael@0 10 // Make sure Unicode is supported:
michael@0 11 // U+00E3 : LATIN SMALL LETTER A WITH TILDE
michael@0 12 // U+041B : CYRILLIC CAPITAL LETTER EL
michael@0 13 // U+3056 : HIRAGANA LETTER ZA
michael@0 14 const resultFileName = "test\u00e3\u041b\u3056" + Date.now() + ".doc";
michael@0 15
michael@0 16 // Milliseconds between polls.
michael@0 17 const POLL_REGISTRY_TIMEOUT = 200;
michael@0 18 // Max number of polls.
michael@0 19 const POLL_REGISTRY_MAX_LOOPS = 25;
michael@0 20
michael@0 21 function checkResult() {
michael@0 22 // delete the saved file (this doesn't affect the "recent documents" list)
michael@0 23 var resultFile = do_get_file(resultFileName);
michael@0 24 resultFile.remove(false);
michael@0 25
michael@0 26 // Need to poll RecentDocs value because the SHAddToRecentDocs call
michael@0 27 // doesn't update the registry immediately.
michael@0 28 do_timeout(POLL_REGISTRY_TIMEOUT, pollRecentDocs);
michael@0 29 }
michael@0 30
michael@0 31 var gPollsCount = 0;
michael@0 32 function pollRecentDocs() {
michael@0 33 if (++gPollsCount > POLL_REGISTRY_MAX_LOOPS) {
michael@0 34 do_throw("Maximum time elapsed while polling RecentDocs.");
michael@0 35 do_test_finished();
michael@0 36 return;
michael@0 37 }
michael@0 38
michael@0 39 if (checkRecentDocsFor(resultFileName)) {
michael@0 40 print("Document found in RecentDocs");
michael@0 41 do_test_finished();
michael@0 42 }
michael@0 43 else
michael@0 44 do_timeout(POLL_REGISTRY_TIMEOUT, pollRecentDocs);
michael@0 45 }
michael@0 46
michael@0 47 function checkRecentDocsFor(aFileName) {
michael@0 48 var recentDocsKey = Cc["@mozilla.org/windows-registry-key;1"].
michael@0 49 createInstance(Ci.nsIWindowsRegKey);
michael@0 50 var recentDocsPath =
michael@0 51 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RecentDocs";
michael@0 52 recentDocsKey.open(Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
michael@0 53 recentDocsPath,
michael@0 54 Ci.nsIWindowsRegKey.ACCESS_READ);
michael@0 55 var count = recentDocsKey.valueCount;
michael@0 56 for (var i = 0; i < count; ++i) {
michael@0 57 var valueName = recentDocsKey.getValueName(i);
michael@0 58 var binValue = recentDocsKey.readBinaryValue(valueName);
michael@0 59
michael@0 60 // "fields" in the data are separated by \0 wide characters, which are
michael@0 61 // returned as two \0 "bytes" by readBinaryValue. Use only the first field.
michael@0 62 var fileNameRaw = binValue.split("\0\0")[0];
michael@0 63
michael@0 64 // Convert the filename from UTF-16LE.
michael@0 65 var fileName = "";
michael@0 66 for (var c = 0; c < fileNameRaw.length; c += 2)
michael@0 67 fileName += String.fromCharCode(fileNameRaw.charCodeAt(c) |
michael@0 68 fileNameRaw.charCodeAt(c+1) * 256);
michael@0 69
michael@0 70 if (aFileName == fileName)
michael@0 71 return true;
michael@0 72 }
michael@0 73 return false;
michael@0 74 }
michael@0 75
michael@0 76 var httpserv = null;
michael@0 77 function run_test()
michael@0 78 {
michael@0 79 if (oldDownloadManagerDisabled()) {
michael@0 80 return;
michael@0 81 }
michael@0 82
michael@0 83 // This test functionality only implemented on Windows.
michael@0 84 // Is there a better way of doing this?
michael@0 85 var httpPH = Cc["@mozilla.org/network/protocol;1?name=http"].
michael@0 86 getService(Ci.nsIHttpProtocolHandler);
michael@0 87 if (httpPH.platform != "Windows")
michael@0 88 return;
michael@0 89
michael@0 90 // Don't finish until the download is finished
michael@0 91 do_test_pending();
michael@0 92
michael@0 93 httpserv = new HttpServer();
michael@0 94 httpserv.registerDirectory("/", do_get_cwd());
michael@0 95 httpserv.start(-1);
michael@0 96
michael@0 97 var listener = {
michael@0 98 onDownloadStateChange: function test_401430_odsc(aState, aDownload) {
michael@0 99 if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED) {
michael@0 100 checkResult();
michael@0 101 }
michael@0 102 },
michael@0 103 onStateChange: function(a, b, c, d, e) { },
michael@0 104 onProgressChange: function(a, b, c, d, e, f, g) { },
michael@0 105 onSecurityChange: function(a, b, c, d) { }
michael@0 106 };
michael@0 107
michael@0 108 dm.addListener(listener);
michael@0 109 dm.addListener(getDownloadListener());
michael@0 110
michael@0 111 // need to save the file to the CWD, because the profile dir is in $TEMP,
michael@0 112 // and Windows apparently doesn't like putting things from $TEMP into
michael@0 113 // the recent files list.
michael@0 114 var dl = addDownload(httpserv,
michael@0 115 {resultFileName: resultFileName,
michael@0 116 targetFile: do_get_file(resultFileName, true)});
michael@0 117 }

mercurial