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