michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // This tests the "add to recent documents" functionality of the DM michael@0: michael@0: const nsIDownloadManager = Ci.nsIDownloadManager; michael@0: const dm = Cc["@mozilla.org/download-manager;1"].getService(nsIDownloadManager); michael@0: michael@0: // Make sure Unicode is supported: michael@0: // U+00E3 : LATIN SMALL LETTER A WITH TILDE michael@0: // U+041B : CYRILLIC CAPITAL LETTER EL michael@0: // U+3056 : HIRAGANA LETTER ZA michael@0: const resultFileName = "test\u00e3\u041b\u3056" + Date.now() + ".doc"; michael@0: michael@0: // Milliseconds between polls. michael@0: const POLL_REGISTRY_TIMEOUT = 200; michael@0: // Max number of polls. michael@0: const POLL_REGISTRY_MAX_LOOPS = 25; michael@0: michael@0: function checkResult() { michael@0: // delete the saved file (this doesn't affect the "recent documents" list) michael@0: var resultFile = do_get_file(resultFileName); michael@0: resultFile.remove(false); michael@0: michael@0: // Need to poll RecentDocs value because the SHAddToRecentDocs call michael@0: // doesn't update the registry immediately. michael@0: do_timeout(POLL_REGISTRY_TIMEOUT, pollRecentDocs); michael@0: } michael@0: michael@0: var gPollsCount = 0; michael@0: function pollRecentDocs() { michael@0: if (++gPollsCount > POLL_REGISTRY_MAX_LOOPS) { michael@0: do_throw("Maximum time elapsed while polling RecentDocs."); michael@0: do_test_finished(); michael@0: return; michael@0: } michael@0: michael@0: if (checkRecentDocsFor(resultFileName)) { michael@0: print("Document found in RecentDocs"); michael@0: do_test_finished(); michael@0: } michael@0: else michael@0: do_timeout(POLL_REGISTRY_TIMEOUT, pollRecentDocs); michael@0: } michael@0: michael@0: function checkRecentDocsFor(aFileName) { michael@0: var recentDocsKey = Cc["@mozilla.org/windows-registry-key;1"]. michael@0: createInstance(Ci.nsIWindowsRegKey); michael@0: var recentDocsPath = michael@0: "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RecentDocs"; michael@0: recentDocsKey.open(Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, michael@0: recentDocsPath, michael@0: Ci.nsIWindowsRegKey.ACCESS_READ); michael@0: var count = recentDocsKey.valueCount; michael@0: for (var i = 0; i < count; ++i) { michael@0: var valueName = recentDocsKey.getValueName(i); michael@0: var binValue = recentDocsKey.readBinaryValue(valueName); michael@0: michael@0: // "fields" in the data are separated by \0 wide characters, which are michael@0: // returned as two \0 "bytes" by readBinaryValue. Use only the first field. michael@0: var fileNameRaw = binValue.split("\0\0")[0]; michael@0: michael@0: // Convert the filename from UTF-16LE. michael@0: var fileName = ""; michael@0: for (var c = 0; c < fileNameRaw.length; c += 2) michael@0: fileName += String.fromCharCode(fileNameRaw.charCodeAt(c) | michael@0: fileNameRaw.charCodeAt(c+1) * 256); michael@0: michael@0: if (aFileName == fileName) michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: var httpserv = null; michael@0: function run_test() michael@0: { michael@0: if (oldDownloadManagerDisabled()) { michael@0: return; michael@0: } michael@0: michael@0: // This test functionality only implemented on Windows. michael@0: // Is there a better way of doing this? michael@0: var httpPH = Cc["@mozilla.org/network/protocol;1?name=http"]. michael@0: getService(Ci.nsIHttpProtocolHandler); michael@0: if (httpPH.platform != "Windows") michael@0: return; michael@0: michael@0: // Don't finish until the download is finished michael@0: do_test_pending(); michael@0: michael@0: httpserv = new HttpServer(); michael@0: httpserv.registerDirectory("/", do_get_cwd()); michael@0: httpserv.start(-1); michael@0: michael@0: var listener = { michael@0: onDownloadStateChange: function test_401430_odsc(aState, aDownload) { michael@0: if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED) { michael@0: checkResult(); michael@0: } michael@0: }, michael@0: onStateChange: function(a, b, c, d, e) { }, michael@0: onProgressChange: function(a, b, c, d, e, f, g) { }, michael@0: onSecurityChange: function(a, b, c, d) { } michael@0: }; michael@0: michael@0: dm.addListener(listener); michael@0: dm.addListener(getDownloadListener()); michael@0: michael@0: // need to save the file to the CWD, because the profile dir is in $TEMP, michael@0: // and Windows apparently doesn't like putting things from $TEMP into michael@0: // the recent files list. michael@0: var dl = addDownload(httpserv, michael@0: {resultFileName: resultFileName, michael@0: targetFile: do_get_file(resultFileName, true)}); michael@0: }