|
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim:set ts=2 sw=2 sts=2 et: */ |
|
3 |
|
4 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
5 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
6 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
7 |
|
8 const Cr = Components.results; |
|
9 const Ci = Components.interfaces; |
|
10 const Cc = Components.classes; |
|
11 const Cu = Components.utils; |
|
12 const CC = Components.Constructor; |
|
13 |
|
14 const LocalFile = CC("@mozilla.org/file/local;1", "nsILocalFile", "initWithPath"); |
|
15 |
|
16 Cu.import("resource://gre/modules/Services.jsm"); |
|
17 |
|
18 function run_test() |
|
19 { |
|
20 // This test makes sense only on Windows, so skip it on other platforms |
|
21 if ("nsILocalFileWin" in Ci |
|
22 && do_get_cwd() instanceof Ci.nsILocalFileWin) { |
|
23 |
|
24 let tempDir = Services.dirsvc.get("TmpD", Ci.nsILocalFile); |
|
25 tempDir.append("shortcutTesting"); |
|
26 tempDir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0666); |
|
27 |
|
28 test_create_noargs(tempDir); |
|
29 test_create_notarget(tempDir); |
|
30 test_create_targetonly(tempDir); |
|
31 test_create_normal(tempDir); |
|
32 test_create_unicode(tempDir); |
|
33 |
|
34 test_update_noargs(tempDir); |
|
35 test_update_notarget(tempDir); |
|
36 test_update_targetonly(tempDir); |
|
37 test_update_normal(tempDir); |
|
38 test_update_unicode(tempDir); |
|
39 } |
|
40 } |
|
41 |
|
42 function test_create_noargs(tempDir) |
|
43 { |
|
44 let shortcutFile = tempDir.clone(); |
|
45 shortcutFile.append("shouldNeverExist.lnk"); |
|
46 shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
47 |
|
48 let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); |
|
49 |
|
50 try |
|
51 { |
|
52 win.setShortcut(); |
|
53 do_throw("Creating a shortcut with no args (no target) should throw"); |
|
54 } |
|
55 catch(e if (e instanceof Ci.nsIException |
|
56 && e.result == Cr.NS_ERROR_FILE_TARGET_DOES_NOT_EXIST)) |
|
57 { |
|
58 |
|
59 } |
|
60 } |
|
61 |
|
62 function test_create_notarget(tempDir) |
|
63 { |
|
64 let shortcutFile = tempDir.clone(); |
|
65 shortcutFile.append("shouldNeverExist2.lnk"); |
|
66 shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
67 |
|
68 let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); |
|
69 |
|
70 try |
|
71 { |
|
72 win.setShortcut(null, |
|
73 do_get_cwd(), |
|
74 "arg1 arg2", |
|
75 "Shortcut with no target"); |
|
76 do_throw("Creating a shortcut with no target should throw"); |
|
77 } |
|
78 catch(e if (e instanceof Ci.nsIException |
|
79 && e.result == Cr.NS_ERROR_FILE_TARGET_DOES_NOT_EXIST)) |
|
80 { |
|
81 |
|
82 } |
|
83 } |
|
84 |
|
85 function test_create_targetonly(tempDir) |
|
86 { |
|
87 let shortcutFile = tempDir.clone(); |
|
88 shortcutFile.append("createdShortcut.lnk"); |
|
89 shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
90 |
|
91 let targetFile = tempDir.clone(); |
|
92 targetFile.append("shortcutTarget.exe"); |
|
93 targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
94 |
|
95 let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); |
|
96 |
|
97 win.setShortcut(targetFile); |
|
98 |
|
99 let shortcutTarget = LocalFile(shortcutFile.target); |
|
100 do_check_true(shortcutTarget.equals(targetFile)); |
|
101 } |
|
102 |
|
103 function test_create_normal(tempDir) |
|
104 { |
|
105 let shortcutFile = tempDir.clone(); |
|
106 shortcutFile.append("createdShortcut.lnk"); |
|
107 shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
108 |
|
109 let targetFile = tempDir.clone(); |
|
110 targetFile.append("shortcutTarget.exe"); |
|
111 targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
112 |
|
113 let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); |
|
114 |
|
115 win.setShortcut(targetFile, |
|
116 do_get_cwd(), |
|
117 "arg1 arg2", |
|
118 "Ordinary shortcut"); |
|
119 |
|
120 let shortcutTarget = LocalFile(shortcutFile.target); |
|
121 do_check_true(shortcutTarget.equals(targetFile)) |
|
122 } |
|
123 |
|
124 function test_create_unicode(tempDir) |
|
125 { |
|
126 let shortcutFile = tempDir.clone(); |
|
127 shortcutFile.append("createdShortcut.lnk"); |
|
128 shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
129 |
|
130 let targetFile = tempDir.clone(); |
|
131 targetFile.append("ṩhогТϾừ†Target.exe"); |
|
132 targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
133 |
|
134 let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); |
|
135 |
|
136 win.setShortcut(targetFile, |
|
137 do_get_cwd(), // XXX: This should probably be a unicode dir |
|
138 "ᾶṟǵ1 ᾶṟǵ2", |
|
139 "ῧṋіḉѻₑ"); |
|
140 |
|
141 let shortcutTarget = LocalFile(shortcutFile.target); |
|
142 do_check_true(shortcutTarget.equals(targetFile)) |
|
143 } |
|
144 |
|
145 function test_update_noargs(tempDir) |
|
146 { |
|
147 let shortcutFile = tempDir.clone(); |
|
148 shortcutFile.append("createdShortcut.lnk"); |
|
149 shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
150 |
|
151 let targetFile = tempDir.clone(); |
|
152 targetFile.append("shortcutTarget.exe"); |
|
153 targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
154 |
|
155 let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); |
|
156 |
|
157 win.setShortcut(targetFile, |
|
158 do_get_cwd(), |
|
159 "arg1 arg2", |
|
160 "A sample shortcut"); |
|
161 |
|
162 win.setShortcut(); |
|
163 |
|
164 let shortcutTarget = LocalFile(shortcutFile.target); |
|
165 do_check_true(shortcutTarget.equals(targetFile)) |
|
166 } |
|
167 |
|
168 function test_update_notarget(tempDir) |
|
169 { |
|
170 let shortcutFile = tempDir.clone(); |
|
171 shortcutFile.append("createdShortcut.lnk"); |
|
172 shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
173 |
|
174 let targetFile = tempDir.clone(); |
|
175 targetFile.append("shortcutTarget.exe"); |
|
176 targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
177 |
|
178 let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); |
|
179 |
|
180 win.setShortcut(targetFile, |
|
181 do_get_cwd(), |
|
182 "arg1 arg2", |
|
183 "A sample shortcut"); |
|
184 |
|
185 win.setShortcut(null, |
|
186 do_get_profile(), |
|
187 "arg3 arg4", |
|
188 "An UPDATED shortcut"); |
|
189 |
|
190 let shortcutTarget = LocalFile(shortcutFile.target); |
|
191 do_check_true(shortcutTarget.equals(targetFile)) |
|
192 } |
|
193 |
|
194 function test_update_targetonly(tempDir) |
|
195 { |
|
196 let shortcutFile = tempDir.clone(); |
|
197 shortcutFile.append("createdShortcut.lnk"); |
|
198 shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
199 |
|
200 let targetFile = tempDir.clone(); |
|
201 targetFile.append("shortcutTarget.exe"); |
|
202 targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
203 |
|
204 let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); |
|
205 |
|
206 win.setShortcut(targetFile, |
|
207 do_get_cwd(), |
|
208 "arg1 arg2", |
|
209 "A sample shortcut"); |
|
210 |
|
211 let newTargetFile = tempDir.clone(); |
|
212 newTargetFile.append("shortcutTarget.exe"); |
|
213 shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
214 |
|
215 win.setShortcut(newTargetFile); |
|
216 |
|
217 let shortcutTarget = LocalFile(shortcutFile.target); |
|
218 do_check_true(shortcutTarget.equals(newTargetFile)) |
|
219 } |
|
220 |
|
221 function test_update_normal(tempDir) |
|
222 { |
|
223 let shortcutFile = tempDir.clone(); |
|
224 shortcutFile.append("createdShortcut.lnk"); |
|
225 shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
226 |
|
227 let targetFile = tempDir.clone(); |
|
228 targetFile.append("shortcutTarget.exe"); |
|
229 targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
230 |
|
231 let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); |
|
232 |
|
233 win.setShortcut(targetFile, |
|
234 do_get_cwd(), |
|
235 "arg1 arg2", |
|
236 "A sample shortcut"); |
|
237 |
|
238 let newTargetFile = tempDir.clone(); |
|
239 newTargetFile.append("shortcutTarget.exe"); |
|
240 newTargetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
241 |
|
242 win.setShortcut(newTargetFile, |
|
243 do_get_profile(), |
|
244 "arg3 arg4", |
|
245 "An UPDATED shortcut"); |
|
246 |
|
247 let shortcutTarget = LocalFile(shortcutFile.target); |
|
248 do_check_true(shortcutTarget.equals(newTargetFile)) |
|
249 } |
|
250 |
|
251 function test_update_unicode(tempDir) |
|
252 { |
|
253 let shortcutFile = tempDir.clone(); |
|
254 shortcutFile.append("createdShortcut.lnk"); |
|
255 shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
256 |
|
257 let targetFile = tempDir.clone(); |
|
258 targetFile.append("shortcutTarget.exe"); |
|
259 targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
260 |
|
261 let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); |
|
262 |
|
263 win.setShortcut(targetFile, |
|
264 do_get_cwd(), |
|
265 "arg1 arg2", |
|
266 "A sample shortcut"); |
|
267 |
|
268 let newTargetFile = tempDir.clone(); |
|
269 newTargetFile.append("ṩhогТϾừ†Target.exe"); |
|
270 shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
|
271 |
|
272 win.setShortcut(newTargetFile, |
|
273 do_get_profile(), // XXX: This should probably be unicode |
|
274 "ᾶṟǵ3 ᾶṟǵ4", |
|
275 "A ῧṋіḉѻₑ shortcut"); |
|
276 |
|
277 let shortcutTarget = LocalFile(shortcutFile.target); |
|
278 do_check_true(shortcutTarget.equals(newTargetFile)) |
|
279 } |