1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/downloads/test/unit/test_bug_401430.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,117 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +// This tests the "add to recent documents" functionality of the DM 1.9 + 1.10 +const nsIDownloadManager = Ci.nsIDownloadManager; 1.11 +const dm = Cc["@mozilla.org/download-manager;1"].getService(nsIDownloadManager); 1.12 + 1.13 +// Make sure Unicode is supported: 1.14 +// U+00E3 : LATIN SMALL LETTER A WITH TILDE 1.15 +// U+041B : CYRILLIC CAPITAL LETTER EL 1.16 +// U+3056 : HIRAGANA LETTER ZA 1.17 +const resultFileName = "test\u00e3\u041b\u3056" + Date.now() + ".doc"; 1.18 + 1.19 +// Milliseconds between polls. 1.20 +const POLL_REGISTRY_TIMEOUT = 200; 1.21 +// Max number of polls. 1.22 +const POLL_REGISTRY_MAX_LOOPS = 25; 1.23 + 1.24 +function checkResult() { 1.25 + // delete the saved file (this doesn't affect the "recent documents" list) 1.26 + var resultFile = do_get_file(resultFileName); 1.27 + resultFile.remove(false); 1.28 + 1.29 + // Need to poll RecentDocs value because the SHAddToRecentDocs call 1.30 + // doesn't update the registry immediately. 1.31 + do_timeout(POLL_REGISTRY_TIMEOUT, pollRecentDocs); 1.32 +} 1.33 + 1.34 +var gPollsCount = 0; 1.35 +function pollRecentDocs() { 1.36 + if (++gPollsCount > POLL_REGISTRY_MAX_LOOPS) { 1.37 + do_throw("Maximum time elapsed while polling RecentDocs."); 1.38 + do_test_finished(); 1.39 + return; 1.40 + } 1.41 + 1.42 + if (checkRecentDocsFor(resultFileName)) { 1.43 + print("Document found in RecentDocs"); 1.44 + do_test_finished(); 1.45 + } 1.46 + else 1.47 + do_timeout(POLL_REGISTRY_TIMEOUT, pollRecentDocs); 1.48 +} 1.49 + 1.50 +function checkRecentDocsFor(aFileName) { 1.51 + var recentDocsKey = Cc["@mozilla.org/windows-registry-key;1"]. 1.52 + createInstance(Ci.nsIWindowsRegKey); 1.53 + var recentDocsPath = 1.54 + "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RecentDocs"; 1.55 + recentDocsKey.open(Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, 1.56 + recentDocsPath, 1.57 + Ci.nsIWindowsRegKey.ACCESS_READ); 1.58 + var count = recentDocsKey.valueCount; 1.59 + for (var i = 0; i < count; ++i) { 1.60 + var valueName = recentDocsKey.getValueName(i); 1.61 + var binValue = recentDocsKey.readBinaryValue(valueName); 1.62 + 1.63 + // "fields" in the data are separated by \0 wide characters, which are 1.64 + // returned as two \0 "bytes" by readBinaryValue. Use only the first field. 1.65 + var fileNameRaw = binValue.split("\0\0")[0]; 1.66 + 1.67 + // Convert the filename from UTF-16LE. 1.68 + var fileName = ""; 1.69 + for (var c = 0; c < fileNameRaw.length; c += 2) 1.70 + fileName += String.fromCharCode(fileNameRaw.charCodeAt(c) | 1.71 + fileNameRaw.charCodeAt(c+1) * 256); 1.72 + 1.73 + if (aFileName == fileName) 1.74 + return true; 1.75 + } 1.76 + return false; 1.77 +} 1.78 + 1.79 +var httpserv = null; 1.80 +function run_test() 1.81 +{ 1.82 + if (oldDownloadManagerDisabled()) { 1.83 + return; 1.84 + } 1.85 + 1.86 + // This test functionality only implemented on Windows. 1.87 + // Is there a better way of doing this? 1.88 + var httpPH = Cc["@mozilla.org/network/protocol;1?name=http"]. 1.89 + getService(Ci.nsIHttpProtocolHandler); 1.90 + if (httpPH.platform != "Windows") 1.91 + return; 1.92 + 1.93 + // Don't finish until the download is finished 1.94 + do_test_pending(); 1.95 + 1.96 + httpserv = new HttpServer(); 1.97 + httpserv.registerDirectory("/", do_get_cwd()); 1.98 + httpserv.start(-1); 1.99 + 1.100 + var listener = { 1.101 + onDownloadStateChange: function test_401430_odsc(aState, aDownload) { 1.102 + if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED) { 1.103 + checkResult(); 1.104 + } 1.105 + }, 1.106 + onStateChange: function(a, b, c, d, e) { }, 1.107 + onProgressChange: function(a, b, c, d, e, f, g) { }, 1.108 + onSecurityChange: function(a, b, c, d) { } 1.109 + }; 1.110 + 1.111 + dm.addListener(listener); 1.112 + dm.addListener(getDownloadListener()); 1.113 + 1.114 + // need to save the file to the CWD, because the profile dir is in $TEMP, 1.115 + // and Windows apparently doesn't like putting things from $TEMP into 1.116 + // the recent files list. 1.117 + var dl = addDownload(httpserv, 1.118 + {resultFileName: resultFileName, 1.119 + targetFile: do_get_file(resultFileName, true)}); 1.120 +}