xpcom/tests/unit/test_windows_shortcut.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/unit/test_windows_shortcut.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,279 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et: */
     1.6 +
     1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.9 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.10 +
    1.11 +const Cr = Components.results;
    1.12 +const Ci = Components.interfaces;
    1.13 +const Cc = Components.classes;
    1.14 +const Cu = Components.utils;
    1.15 +const CC = Components.Constructor;
    1.16 +
    1.17 +const LocalFile = CC("@mozilla.org/file/local;1", "nsILocalFile", "initWithPath");
    1.18 +
    1.19 +Cu.import("resource://gre/modules/Services.jsm");
    1.20 +
    1.21 +function run_test()
    1.22 +{
    1.23 +  // This test makes sense only on Windows, so skip it on other platforms
    1.24 +  if ("nsILocalFileWin" in Ci
    1.25 +   && do_get_cwd() instanceof Ci.nsILocalFileWin) {
    1.26 +
    1.27 +    let tempDir = Services.dirsvc.get("TmpD", Ci.nsILocalFile);
    1.28 +    tempDir.append("shortcutTesting");
    1.29 +    tempDir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0666);
    1.30 +
    1.31 +    test_create_noargs(tempDir);
    1.32 +    test_create_notarget(tempDir);
    1.33 +    test_create_targetonly(tempDir);
    1.34 +    test_create_normal(tempDir);
    1.35 +    test_create_unicode(tempDir);
    1.36 +
    1.37 +    test_update_noargs(tempDir);
    1.38 +    test_update_notarget(tempDir);
    1.39 +    test_update_targetonly(tempDir);
    1.40 +    test_update_normal(tempDir);
    1.41 +    test_update_unicode(tempDir);
    1.42 +  }
    1.43 +}
    1.44 +
    1.45 +function test_create_noargs(tempDir)
    1.46 +{
    1.47 +  let shortcutFile = tempDir.clone();
    1.48 +  shortcutFile.append("shouldNeverExist.lnk");
    1.49 +  shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
    1.50 +
    1.51 +  let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin);
    1.52 +
    1.53 +  try
    1.54 +  {
    1.55 +    win.setShortcut();
    1.56 +    do_throw("Creating a shortcut with no args (no target) should throw");
    1.57 +  }
    1.58 +  catch(e if (e instanceof Ci.nsIException
    1.59 +             && e.result == Cr.NS_ERROR_FILE_TARGET_DOES_NOT_EXIST))
    1.60 +  {
    1.61 +
    1.62 +  }
    1.63 +}
    1.64 +
    1.65 +function test_create_notarget(tempDir)
    1.66 +{
    1.67 +  let shortcutFile = tempDir.clone();
    1.68 +  shortcutFile.append("shouldNeverExist2.lnk");
    1.69 +  shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
    1.70 +
    1.71 +  let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin);
    1.72 +
    1.73 +  try
    1.74 +  {
    1.75 +    win.setShortcut(null,
    1.76 +                    do_get_cwd(),
    1.77 +                    "arg1 arg2",
    1.78 +                    "Shortcut with no target");
    1.79 +    do_throw("Creating a shortcut with no target should throw");
    1.80 +  }
    1.81 +  catch(e if (e instanceof Ci.nsIException
    1.82 +             && e.result == Cr.NS_ERROR_FILE_TARGET_DOES_NOT_EXIST))
    1.83 +  {
    1.84 +
    1.85 +  }
    1.86 +}
    1.87 +
    1.88 +function test_create_targetonly(tempDir)
    1.89 +{
    1.90 +  let shortcutFile = tempDir.clone();
    1.91 +  shortcutFile.append("createdShortcut.lnk");
    1.92 +  shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
    1.93 +
    1.94 +  let targetFile = tempDir.clone();
    1.95 +  targetFile.append("shortcutTarget.exe");
    1.96 +  targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
    1.97 +
    1.98 +  let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin);
    1.99 +
   1.100 +  win.setShortcut(targetFile);
   1.101 +
   1.102 +  let shortcutTarget = LocalFile(shortcutFile.target);
   1.103 +  do_check_true(shortcutTarget.equals(targetFile));
   1.104 +}
   1.105 +
   1.106 +function test_create_normal(tempDir)
   1.107 +{
   1.108 +  let shortcutFile = tempDir.clone();
   1.109 +  shortcutFile.append("createdShortcut.lnk");
   1.110 +  shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.111 +
   1.112 +  let targetFile = tempDir.clone();
   1.113 +  targetFile.append("shortcutTarget.exe");
   1.114 +  targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.115 +
   1.116 +  let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin);
   1.117 +
   1.118 +  win.setShortcut(targetFile,
   1.119 +                  do_get_cwd(),
   1.120 +                  "arg1 arg2",
   1.121 +                  "Ordinary shortcut");
   1.122 +
   1.123 +  let shortcutTarget = LocalFile(shortcutFile.target);
   1.124 +  do_check_true(shortcutTarget.equals(targetFile))
   1.125 +}
   1.126 +
   1.127 +function test_create_unicode(tempDir)
   1.128 +{
   1.129 +  let shortcutFile = tempDir.clone();
   1.130 +  shortcutFile.append("createdShortcut.lnk");
   1.131 +  shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.132 +
   1.133 +  let targetFile = tempDir.clone();
   1.134 +  targetFile.append("ṩhогТϾừ†Target.exe");
   1.135 +  targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.136 +
   1.137 +  let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin);
   1.138 +
   1.139 +  win.setShortcut(targetFile,
   1.140 +                  do_get_cwd(), // XXX: This should probably be a unicode dir
   1.141 +                  "ᾶṟǵ1 ᾶṟǵ2",
   1.142 +                  "ῧṋіḉѻₑ");
   1.143 +
   1.144 +  let shortcutTarget = LocalFile(shortcutFile.target);
   1.145 +  do_check_true(shortcutTarget.equals(targetFile))
   1.146 +}
   1.147 +
   1.148 +function test_update_noargs(tempDir)
   1.149 +{
   1.150 +  let shortcutFile = tempDir.clone();
   1.151 +  shortcutFile.append("createdShortcut.lnk");
   1.152 +  shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.153 +
   1.154 +  let targetFile = tempDir.clone();
   1.155 +  targetFile.append("shortcutTarget.exe");
   1.156 +  targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.157 +
   1.158 +  let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin);
   1.159 +
   1.160 +  win.setShortcut(targetFile,
   1.161 +                  do_get_cwd(),
   1.162 +                  "arg1 arg2",
   1.163 +                  "A sample shortcut");
   1.164 +
   1.165 +  win.setShortcut();
   1.166 +
   1.167 +  let shortcutTarget = LocalFile(shortcutFile.target);
   1.168 +  do_check_true(shortcutTarget.equals(targetFile))
   1.169 +}
   1.170 +
   1.171 +function test_update_notarget(tempDir)
   1.172 +{
   1.173 +  let shortcutFile = tempDir.clone();
   1.174 +  shortcutFile.append("createdShortcut.lnk");
   1.175 +  shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.176 +
   1.177 +  let targetFile = tempDir.clone();
   1.178 +  targetFile.append("shortcutTarget.exe");
   1.179 +  targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.180 +
   1.181 +  let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin);
   1.182 +
   1.183 +  win.setShortcut(targetFile,
   1.184 +                  do_get_cwd(),
   1.185 +                  "arg1 arg2",
   1.186 +                  "A sample shortcut");
   1.187 +
   1.188 +  win.setShortcut(null,
   1.189 +                  do_get_profile(),
   1.190 +                  "arg3 arg4",
   1.191 +                  "An UPDATED shortcut");
   1.192 +
   1.193 +  let shortcutTarget = LocalFile(shortcutFile.target);
   1.194 +  do_check_true(shortcutTarget.equals(targetFile))
   1.195 +}
   1.196 +
   1.197 +function test_update_targetonly(tempDir)
   1.198 +{
   1.199 +  let shortcutFile = tempDir.clone();
   1.200 +  shortcutFile.append("createdShortcut.lnk");
   1.201 +  shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.202 +
   1.203 +  let targetFile = tempDir.clone();
   1.204 +  targetFile.append("shortcutTarget.exe");
   1.205 +  targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.206 +
   1.207 +  let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin);
   1.208 +
   1.209 +  win.setShortcut(targetFile,
   1.210 +                  do_get_cwd(),
   1.211 +                  "arg1 arg2",
   1.212 +                  "A sample shortcut");
   1.213 +
   1.214 +  let newTargetFile = tempDir.clone();
   1.215 +  newTargetFile.append("shortcutTarget.exe");
   1.216 +  shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.217 +
   1.218 +  win.setShortcut(newTargetFile);
   1.219 +
   1.220 +  let shortcutTarget = LocalFile(shortcutFile.target);
   1.221 +  do_check_true(shortcutTarget.equals(newTargetFile))
   1.222 +}
   1.223 +
   1.224 +function test_update_normal(tempDir)
   1.225 +{
   1.226 +  let shortcutFile = tempDir.clone();
   1.227 +  shortcutFile.append("createdShortcut.lnk");
   1.228 +  shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.229 +
   1.230 +  let targetFile = tempDir.clone();
   1.231 +  targetFile.append("shortcutTarget.exe");
   1.232 +  targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.233 +
   1.234 +  let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin);
   1.235 +
   1.236 +  win.setShortcut(targetFile,
   1.237 +                  do_get_cwd(),
   1.238 +                  "arg1 arg2",
   1.239 +                  "A sample shortcut");
   1.240 +
   1.241 +  let newTargetFile = tempDir.clone();
   1.242 +  newTargetFile.append("shortcutTarget.exe");
   1.243 +  newTargetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.244 +
   1.245 +  win.setShortcut(newTargetFile,
   1.246 +                  do_get_profile(),
   1.247 +                  "arg3 arg4",
   1.248 +                  "An UPDATED shortcut");
   1.249 +
   1.250 +  let shortcutTarget = LocalFile(shortcutFile.target);
   1.251 +  do_check_true(shortcutTarget.equals(newTargetFile))
   1.252 +}
   1.253 +
   1.254 +function test_update_unicode(tempDir)
   1.255 +{
   1.256 +  let shortcutFile = tempDir.clone();
   1.257 +  shortcutFile.append("createdShortcut.lnk");
   1.258 +  shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.259 +
   1.260 +  let targetFile = tempDir.clone();
   1.261 +  targetFile.append("shortcutTarget.exe");
   1.262 +  targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.263 +
   1.264 +  let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin);
   1.265 +
   1.266 +  win.setShortcut(targetFile,
   1.267 +                  do_get_cwd(),
   1.268 +                  "arg1 arg2",
   1.269 +                  "A sample shortcut");
   1.270 +
   1.271 +  let newTargetFile = tempDir.clone();
   1.272 +  newTargetFile.append("ṩhогТϾừ†Target.exe");
   1.273 +  shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.274 +
   1.275 +  win.setShortcut(newTargetFile,
   1.276 +                  do_get_profile(), // XXX: This should probably be unicode
   1.277 +                  "ᾶṟǵ3 ᾶṟǵ4",
   1.278 +                  "A ῧṋіḉѻₑ shortcut");
   1.279 +
   1.280 +  let shortcutTarget = LocalFile(shortcutFile.target);
   1.281 +  do_check_true(shortcutTarget.equals(newTargetFile))
   1.282 +}

mercurial