michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */ michael@0: /* ***** BEGIN LICENSE BLOCK ***** michael@0: * michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: * michael@0: * ***** END LICENSE BLOCK ***** */ michael@0: michael@0: /** michael@0: * Tests for the "DownloadPaths.jsm" JavaScript module. michael@0: */ michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: const Cr = Components.results; michael@0: michael@0: Cu.import("resource://gre/modules/DownloadPaths.jsm"); michael@0: michael@0: /** michael@0: * Provides a temporary save directory. michael@0: * michael@0: * @returns nsIFile pointing to the new or existing directory. michael@0: */ michael@0: function createTemporarySaveDirectory() michael@0: { michael@0: var saveDir = Cc["@mozilla.org/file/directory_service;1"]. michael@0: getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile); michael@0: saveDir.append("testsavedir"); michael@0: if (!saveDir.exists()) { michael@0: saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755); michael@0: } michael@0: return saveDir; michael@0: } michael@0: michael@0: function testSplitBaseNameAndExtension(aLeafName, [aBase, aExt]) michael@0: { michael@0: var [base, ext] = DownloadPaths.splitBaseNameAndExtension(aLeafName); michael@0: do_check_eq(base, aBase); michael@0: do_check_eq(ext, aExt); michael@0: michael@0: // If we modify the base name and concatenate it with the extension again, michael@0: // another roundtrip through the function should give a consistent result. michael@0: // The only exception is when we introduce an extension in a file name that michael@0: // didn't have one or that ended with one of the special cases like ".gz". If michael@0: // we avoid using a dot and we introduce at least another special character, michael@0: // the results are always consistent. michael@0: [base, ext] = DownloadPaths.splitBaseNameAndExtension("(" + base + ")" + ext); michael@0: do_check_eq(base, "(" + aBase + ")"); michael@0: do_check_eq(ext, aExt); michael@0: } michael@0: michael@0: function testCreateNiceUniqueFile(aTempFile, aExpectedLeafName) michael@0: { michael@0: var createdFile = DownloadPaths.createNiceUniqueFile(aTempFile); michael@0: do_check_eq(createdFile.leafName, aExpectedLeafName); michael@0: } michael@0: michael@0: function run_test() michael@0: { michael@0: // Usual file names. michael@0: testSplitBaseNameAndExtension("base", ["base", ""]); michael@0: testSplitBaseNameAndExtension("base.ext", ["base", ".ext"]); michael@0: testSplitBaseNameAndExtension("base.application", ["base", ".application"]); michael@0: testSplitBaseNameAndExtension("base.x.Z", ["base", ".x.Z"]); michael@0: testSplitBaseNameAndExtension("base.ext.Z", ["base", ".ext.Z"]); michael@0: testSplitBaseNameAndExtension("base.ext.gz", ["base", ".ext.gz"]); michael@0: testSplitBaseNameAndExtension("base.ext.Bz2", ["base", ".ext.Bz2"]); michael@0: testSplitBaseNameAndExtension("base..ext", ["base.", ".ext"]); michael@0: testSplitBaseNameAndExtension("base..Z", ["base.", ".Z"]); michael@0: testSplitBaseNameAndExtension("base. .Z", ["base. ", ".Z"]); michael@0: testSplitBaseNameAndExtension("base.base.Bz2", ["base.base", ".Bz2"]); michael@0: testSplitBaseNameAndExtension("base .ext", ["base ", ".ext"]); michael@0: michael@0: // Corner cases. A name ending with a dot technically has no extension, but michael@0: // we consider the ending dot separately from the base name so that modifying michael@0: // the latter never results in an extension being introduced accidentally. michael@0: // Names beginning with a dot are hidden files on Unix-like platforms and if michael@0: // their name doesn't contain another dot they should have no extension, but michael@0: // on Windows the whole name is considered as an extension. michael@0: testSplitBaseNameAndExtension("base.", ["base", "."]); michael@0: testSplitBaseNameAndExtension(".ext", ["", ".ext"]); michael@0: michael@0: // Unusual file names (not recommended as input to the function). michael@0: testSplitBaseNameAndExtension("base. ", ["base", ". "]); michael@0: testSplitBaseNameAndExtension("base ", ["base ", ""]); michael@0: testSplitBaseNameAndExtension("", ["", ""]); michael@0: testSplitBaseNameAndExtension(" ", [" ", ""]); michael@0: testSplitBaseNameAndExtension(" . ", [" ", ". "]); michael@0: testSplitBaseNameAndExtension(" .. ", [" .", ". "]); michael@0: testSplitBaseNameAndExtension(" .ext", [" ", ".ext"]); michael@0: testSplitBaseNameAndExtension(" .ext. ", [" .ext", ". "]); michael@0: testSplitBaseNameAndExtension(" .ext.gz ", [" .ext", ".gz "]); michael@0: michael@0: var destDir = createTemporarySaveDirectory(); michael@0: try { michael@0: // Single extension. michael@0: var tempFile = destDir.clone(); michael@0: tempFile.append("test.txt"); michael@0: testCreateNiceUniqueFile(tempFile, "test.txt"); michael@0: testCreateNiceUniqueFile(tempFile, "test(1).txt"); michael@0: testCreateNiceUniqueFile(tempFile, "test(2).txt"); michael@0: michael@0: // Double extension. michael@0: tempFile.leafName = "test.tar.gz"; michael@0: testCreateNiceUniqueFile(tempFile, "test.tar.gz"); michael@0: testCreateNiceUniqueFile(tempFile, "test(1).tar.gz"); michael@0: testCreateNiceUniqueFile(tempFile, "test(2).tar.gz"); michael@0: michael@0: // Test automatic shortening of long file names. We don't know exactly how michael@0: // many characters are removed, because it depends on the name of the folder michael@0: // where the file is located. michael@0: tempFile.leafName = new Array(256).join("T") + ".txt"; michael@0: var newFile = DownloadPaths.createNiceUniqueFile(tempFile); michael@0: do_check_true(newFile.leafName.length < tempFile.leafName.length); michael@0: do_check_eq(newFile.leafName.slice(-4), ".txt"); michael@0: michael@0: // Creating a valid file name from an invalid one is not always possible. michael@0: tempFile.append("file-under-long-directory.txt"); michael@0: try { michael@0: DownloadPaths.createNiceUniqueFile(tempFile); michael@0: do_throw("Exception expected with a long parent directory name.") michael@0: } catch (e) { michael@0: // An exception is expected, but we don't know which one exactly. michael@0: } michael@0: } finally { michael@0: // Clean up the temporary directory. michael@0: destDir.remove(true); michael@0: } michael@0: }