michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: 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 file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const Cr = Components.results; michael@0: const Ci = Components.interfaces; michael@0: const Cc = Components.classes; michael@0: const Cu = Components.utils; michael@0: const CC = Components.Constructor; michael@0: michael@0: const LocalFile = CC("@mozilla.org/file/local;1", "nsILocalFile", "initWithPath"); michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: function run_test() michael@0: { michael@0: // This test makes sense only on Windows, so skip it on other platforms michael@0: if ("nsILocalFileWin" in Ci michael@0: && do_get_cwd() instanceof Ci.nsILocalFileWin) { michael@0: michael@0: let tempDir = Services.dirsvc.get("TmpD", Ci.nsILocalFile); michael@0: tempDir.append("shortcutTesting"); michael@0: tempDir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0666); michael@0: michael@0: test_create_noargs(tempDir); michael@0: test_create_notarget(tempDir); michael@0: test_create_targetonly(tempDir); michael@0: test_create_normal(tempDir); michael@0: test_create_unicode(tempDir); michael@0: michael@0: test_update_noargs(tempDir); michael@0: test_update_notarget(tempDir); michael@0: test_update_targetonly(tempDir); michael@0: test_update_normal(tempDir); michael@0: test_update_unicode(tempDir); michael@0: } michael@0: } michael@0: michael@0: function test_create_noargs(tempDir) michael@0: { michael@0: let shortcutFile = tempDir.clone(); michael@0: shortcutFile.append("shouldNeverExist.lnk"); michael@0: shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); michael@0: michael@0: try michael@0: { michael@0: win.setShortcut(); michael@0: do_throw("Creating a shortcut with no args (no target) should throw"); michael@0: } michael@0: catch(e if (e instanceof Ci.nsIException michael@0: && e.result == Cr.NS_ERROR_FILE_TARGET_DOES_NOT_EXIST)) michael@0: { michael@0: michael@0: } michael@0: } michael@0: michael@0: function test_create_notarget(tempDir) michael@0: { michael@0: let shortcutFile = tempDir.clone(); michael@0: shortcutFile.append("shouldNeverExist2.lnk"); michael@0: shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); michael@0: michael@0: try michael@0: { michael@0: win.setShortcut(null, michael@0: do_get_cwd(), michael@0: "arg1 arg2", michael@0: "Shortcut with no target"); michael@0: do_throw("Creating a shortcut with no target should throw"); michael@0: } michael@0: catch(e if (e instanceof Ci.nsIException michael@0: && e.result == Cr.NS_ERROR_FILE_TARGET_DOES_NOT_EXIST)) michael@0: { michael@0: michael@0: } michael@0: } michael@0: michael@0: function test_create_targetonly(tempDir) michael@0: { michael@0: let shortcutFile = tempDir.clone(); michael@0: shortcutFile.append("createdShortcut.lnk"); michael@0: shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: let targetFile = tempDir.clone(); michael@0: targetFile.append("shortcutTarget.exe"); michael@0: targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); michael@0: michael@0: win.setShortcut(targetFile); michael@0: michael@0: let shortcutTarget = LocalFile(shortcutFile.target); michael@0: do_check_true(shortcutTarget.equals(targetFile)); michael@0: } michael@0: michael@0: function test_create_normal(tempDir) michael@0: { michael@0: let shortcutFile = tempDir.clone(); michael@0: shortcutFile.append("createdShortcut.lnk"); michael@0: shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: let targetFile = tempDir.clone(); michael@0: targetFile.append("shortcutTarget.exe"); michael@0: targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); michael@0: michael@0: win.setShortcut(targetFile, michael@0: do_get_cwd(), michael@0: "arg1 arg2", michael@0: "Ordinary shortcut"); michael@0: michael@0: let shortcutTarget = LocalFile(shortcutFile.target); michael@0: do_check_true(shortcutTarget.equals(targetFile)) michael@0: } michael@0: michael@0: function test_create_unicode(tempDir) michael@0: { michael@0: let shortcutFile = tempDir.clone(); michael@0: shortcutFile.append("createdShortcut.lnk"); michael@0: shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: let targetFile = tempDir.clone(); michael@0: targetFile.append("ṩhогТϾừ†Target.exe"); michael@0: targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); michael@0: michael@0: win.setShortcut(targetFile, michael@0: do_get_cwd(), // XXX: This should probably be a unicode dir michael@0: "ᾶṟǵ1 ᾶṟǵ2", michael@0: "ῧṋіḉѻₑ"); michael@0: michael@0: let shortcutTarget = LocalFile(shortcutFile.target); michael@0: do_check_true(shortcutTarget.equals(targetFile)) michael@0: } michael@0: michael@0: function test_update_noargs(tempDir) michael@0: { michael@0: let shortcutFile = tempDir.clone(); michael@0: shortcutFile.append("createdShortcut.lnk"); michael@0: shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: let targetFile = tempDir.clone(); michael@0: targetFile.append("shortcutTarget.exe"); michael@0: targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); michael@0: michael@0: win.setShortcut(targetFile, michael@0: do_get_cwd(), michael@0: "arg1 arg2", michael@0: "A sample shortcut"); michael@0: michael@0: win.setShortcut(); michael@0: michael@0: let shortcutTarget = LocalFile(shortcutFile.target); michael@0: do_check_true(shortcutTarget.equals(targetFile)) michael@0: } michael@0: michael@0: function test_update_notarget(tempDir) michael@0: { michael@0: let shortcutFile = tempDir.clone(); michael@0: shortcutFile.append("createdShortcut.lnk"); michael@0: shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: let targetFile = tempDir.clone(); michael@0: targetFile.append("shortcutTarget.exe"); michael@0: targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); michael@0: michael@0: win.setShortcut(targetFile, michael@0: do_get_cwd(), michael@0: "arg1 arg2", michael@0: "A sample shortcut"); michael@0: michael@0: win.setShortcut(null, michael@0: do_get_profile(), michael@0: "arg3 arg4", michael@0: "An UPDATED shortcut"); michael@0: michael@0: let shortcutTarget = LocalFile(shortcutFile.target); michael@0: do_check_true(shortcutTarget.equals(targetFile)) michael@0: } michael@0: michael@0: function test_update_targetonly(tempDir) michael@0: { michael@0: let shortcutFile = tempDir.clone(); michael@0: shortcutFile.append("createdShortcut.lnk"); michael@0: shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: let targetFile = tempDir.clone(); michael@0: targetFile.append("shortcutTarget.exe"); michael@0: targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); michael@0: michael@0: win.setShortcut(targetFile, michael@0: do_get_cwd(), michael@0: "arg1 arg2", michael@0: "A sample shortcut"); michael@0: michael@0: let newTargetFile = tempDir.clone(); michael@0: newTargetFile.append("shortcutTarget.exe"); michael@0: shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: win.setShortcut(newTargetFile); michael@0: michael@0: let shortcutTarget = LocalFile(shortcutFile.target); michael@0: do_check_true(shortcutTarget.equals(newTargetFile)) michael@0: } michael@0: michael@0: function test_update_normal(tempDir) michael@0: { michael@0: let shortcutFile = tempDir.clone(); michael@0: shortcutFile.append("createdShortcut.lnk"); michael@0: shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: let targetFile = tempDir.clone(); michael@0: targetFile.append("shortcutTarget.exe"); michael@0: targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); michael@0: michael@0: win.setShortcut(targetFile, michael@0: do_get_cwd(), michael@0: "arg1 arg2", michael@0: "A sample shortcut"); michael@0: michael@0: let newTargetFile = tempDir.clone(); michael@0: newTargetFile.append("shortcutTarget.exe"); michael@0: newTargetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: win.setShortcut(newTargetFile, michael@0: do_get_profile(), michael@0: "arg3 arg4", michael@0: "An UPDATED shortcut"); michael@0: michael@0: let shortcutTarget = LocalFile(shortcutFile.target); michael@0: do_check_true(shortcutTarget.equals(newTargetFile)) michael@0: } michael@0: michael@0: function test_update_unicode(tempDir) michael@0: { michael@0: let shortcutFile = tempDir.clone(); michael@0: shortcutFile.append("createdShortcut.lnk"); michael@0: shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: let targetFile = tempDir.clone(); michael@0: targetFile.append("shortcutTarget.exe"); michael@0: targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); michael@0: michael@0: win.setShortcut(targetFile, michael@0: do_get_cwd(), michael@0: "arg1 arg2", michael@0: "A sample shortcut"); michael@0: michael@0: let newTargetFile = tempDir.clone(); michael@0: newTargetFile.append("ṩhогТϾừ†Target.exe"); michael@0: shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: win.setShortcut(newTargetFile, michael@0: do_get_profile(), // XXX: This should probably be unicode michael@0: "ᾶṟǵ3 ᾶṟǵ4", michael@0: "A ῧṋіḉѻₑ shortcut"); michael@0: michael@0: let shortcutTarget = LocalFile(shortcutFile.target); michael@0: do_check_true(shortcutTarget.equals(newTargetFile)) michael@0: }