test_windows_shortcut.js 8.2 KB

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