toolkit/mozapps/downloads/tests/unit/test_DownloadPaths.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/downloads/tests/unit/test_DownloadPaths.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,131 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */
     1.6 +/* ***** BEGIN LICENSE BLOCK *****
     1.7 + *
     1.8 + * Any copyright is dedicated to the Public Domain.
     1.9 + * http://creativecommons.org/publicdomain/zero/1.0/
    1.10 + *
    1.11 + * ***** END LICENSE BLOCK ***** */
    1.12 +
    1.13 +/**
    1.14 + * Tests for the "DownloadPaths.jsm" JavaScript module.
    1.15 + */
    1.16 +
    1.17 +const Cc = Components.classes;
    1.18 +const Ci = Components.interfaces;
    1.19 +const Cu = Components.utils;
    1.20 +const Cr = Components.results;
    1.21 +
    1.22 +Cu.import("resource://gre/modules/DownloadPaths.jsm");
    1.23 +
    1.24 +/**
    1.25 + * Provides a temporary save directory.
    1.26 + *
    1.27 + * @returns nsIFile pointing to the new or existing directory.
    1.28 + */
    1.29 +function createTemporarySaveDirectory()
    1.30 +{
    1.31 +  var saveDir = Cc["@mozilla.org/file/directory_service;1"].
    1.32 +                getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile);
    1.33 +  saveDir.append("testsavedir");
    1.34 +  if (!saveDir.exists()) {
    1.35 +    saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
    1.36 +  }
    1.37 +  return saveDir;
    1.38 +}
    1.39 +
    1.40 +function testSplitBaseNameAndExtension(aLeafName, [aBase, aExt])
    1.41 +{
    1.42 +  var [base, ext] = DownloadPaths.splitBaseNameAndExtension(aLeafName);
    1.43 +  do_check_eq(base, aBase);
    1.44 +  do_check_eq(ext, aExt);
    1.45 +
    1.46 +  // If we modify the base name and concatenate it with the extension again,
    1.47 +  // another roundtrip through the function should give a consistent result.
    1.48 +  // The only exception is when we introduce an extension in a file name that
    1.49 +  // didn't have one or that ended with one of the special cases like ".gz". If
    1.50 +  // we avoid using a dot and we introduce at least another special character,
    1.51 +  // the results are always consistent.
    1.52 +  [base, ext] = DownloadPaths.splitBaseNameAndExtension("(" + base + ")" + ext);
    1.53 +  do_check_eq(base, "(" + aBase + ")");
    1.54 +  do_check_eq(ext, aExt);
    1.55 +}
    1.56 +
    1.57 +function testCreateNiceUniqueFile(aTempFile, aExpectedLeafName)
    1.58 +{
    1.59 +  var createdFile = DownloadPaths.createNiceUniqueFile(aTempFile);
    1.60 +  do_check_eq(createdFile.leafName, aExpectedLeafName);
    1.61 +}
    1.62 +
    1.63 +function run_test()
    1.64 +{
    1.65 +  // Usual file names.
    1.66 +  testSplitBaseNameAndExtension("base",             ["base", ""]);
    1.67 +  testSplitBaseNameAndExtension("base.ext",         ["base", ".ext"]);
    1.68 +  testSplitBaseNameAndExtension("base.application", ["base", ".application"]);
    1.69 +  testSplitBaseNameAndExtension("base.x.Z",         ["base", ".x.Z"]);
    1.70 +  testSplitBaseNameAndExtension("base.ext.Z",       ["base", ".ext.Z"]);
    1.71 +  testSplitBaseNameAndExtension("base.ext.gz",      ["base", ".ext.gz"]);
    1.72 +  testSplitBaseNameAndExtension("base.ext.Bz2",     ["base", ".ext.Bz2"]);
    1.73 +  testSplitBaseNameAndExtension("base..ext",        ["base.", ".ext"]);
    1.74 +  testSplitBaseNameAndExtension("base..Z",          ["base.", ".Z"]);
    1.75 +  testSplitBaseNameAndExtension("base. .Z",         ["base. ", ".Z"]);
    1.76 +  testSplitBaseNameAndExtension("base.base.Bz2",    ["base.base", ".Bz2"]);
    1.77 +  testSplitBaseNameAndExtension("base  .ext",       ["base  ", ".ext"]);
    1.78 +
    1.79 +  // Corner cases. A name ending with a dot technically has no extension, but
    1.80 +  // we consider the ending dot separately from the base name so that modifying
    1.81 +  // the latter never results in an extension being introduced accidentally.
    1.82 +  // Names beginning with a dot are hidden files on Unix-like platforms and if
    1.83 +  // their name doesn't contain another dot they should have no extension, but
    1.84 +  // on Windows the whole name is considered as an extension.
    1.85 +  testSplitBaseNameAndExtension("base.",            ["base", "."]);
    1.86 +  testSplitBaseNameAndExtension(".ext",             ["", ".ext"]);
    1.87 +
    1.88 +  // Unusual file names (not recommended as input to the function).
    1.89 +  testSplitBaseNameAndExtension("base. ",           ["base", ". "]);
    1.90 +  testSplitBaseNameAndExtension("base ",            ["base ", ""]);
    1.91 +  testSplitBaseNameAndExtension("",                 ["", ""]);
    1.92 +  testSplitBaseNameAndExtension(" ",                [" ", ""]);
    1.93 +  testSplitBaseNameAndExtension(" . ",              [" ", ". "]);
    1.94 +  testSplitBaseNameAndExtension(" .. ",             [" .", ". "]);
    1.95 +  testSplitBaseNameAndExtension(" .ext",            [" ", ".ext"]);
    1.96 +  testSplitBaseNameAndExtension(" .ext. ",          [" .ext", ". "]);
    1.97 +  testSplitBaseNameAndExtension(" .ext.gz ",        [" .ext", ".gz "]);
    1.98 +
    1.99 +  var destDir = createTemporarySaveDirectory();
   1.100 +  try {
   1.101 +    // Single extension.
   1.102 +    var tempFile = destDir.clone();
   1.103 +    tempFile.append("test.txt");
   1.104 +    testCreateNiceUniqueFile(tempFile, "test.txt");
   1.105 +    testCreateNiceUniqueFile(tempFile, "test(1).txt");
   1.106 +    testCreateNiceUniqueFile(tempFile, "test(2).txt");
   1.107 +
   1.108 +    // Double extension.
   1.109 +    tempFile.leafName = "test.tar.gz";
   1.110 +    testCreateNiceUniqueFile(tempFile, "test.tar.gz");
   1.111 +    testCreateNiceUniqueFile(tempFile, "test(1).tar.gz");
   1.112 +    testCreateNiceUniqueFile(tempFile, "test(2).tar.gz");
   1.113 +
   1.114 +    // Test automatic shortening of long file names. We don't know exactly how
   1.115 +    // many characters are removed, because it depends on the name of the folder
   1.116 +    // where the file is located.
   1.117 +    tempFile.leafName = new Array(256).join("T") + ".txt";
   1.118 +    var newFile = DownloadPaths.createNiceUniqueFile(tempFile);
   1.119 +    do_check_true(newFile.leafName.length < tempFile.leafName.length);
   1.120 +    do_check_eq(newFile.leafName.slice(-4), ".txt");
   1.121 +
   1.122 +    // Creating a valid file name from an invalid one is not always possible.
   1.123 +    tempFile.append("file-under-long-directory.txt");
   1.124 +    try {
   1.125 +      DownloadPaths.createNiceUniqueFile(tempFile);
   1.126 +      do_throw("Exception expected with a long parent directory name.")
   1.127 +    } catch (e) {
   1.128 +      // An exception is expected, but we don't know which one exactly.
   1.129 +    }
   1.130 +  } finally {
   1.131 +    // Clean up the temporary directory.
   1.132 +    destDir.remove(true);
   1.133 +  }
   1.134 +}

mercurial