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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */
michael@0 3 /* ***** BEGIN LICENSE BLOCK *****
michael@0 4 *
michael@0 5 * Any copyright is dedicated to the Public Domain.
michael@0 6 * http://creativecommons.org/publicdomain/zero/1.0/
michael@0 7 *
michael@0 8 * ***** END LICENSE BLOCK ***** */
michael@0 9
michael@0 10 /**
michael@0 11 * Tests for the "DownloadPaths.jsm" JavaScript module.
michael@0 12 */
michael@0 13
michael@0 14 const Cc = Components.classes;
michael@0 15 const Ci = Components.interfaces;
michael@0 16 const Cu = Components.utils;
michael@0 17 const Cr = Components.results;
michael@0 18
michael@0 19 Cu.import("resource://gre/modules/DownloadPaths.jsm");
michael@0 20
michael@0 21 /**
michael@0 22 * Provides a temporary save directory.
michael@0 23 *
michael@0 24 * @returns nsIFile pointing to the new or existing directory.
michael@0 25 */
michael@0 26 function createTemporarySaveDirectory()
michael@0 27 {
michael@0 28 var saveDir = Cc["@mozilla.org/file/directory_service;1"].
michael@0 29 getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile);
michael@0 30 saveDir.append("testsavedir");
michael@0 31 if (!saveDir.exists()) {
michael@0 32 saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
michael@0 33 }
michael@0 34 return saveDir;
michael@0 35 }
michael@0 36
michael@0 37 function testSplitBaseNameAndExtension(aLeafName, [aBase, aExt])
michael@0 38 {
michael@0 39 var [base, ext] = DownloadPaths.splitBaseNameAndExtension(aLeafName);
michael@0 40 do_check_eq(base, aBase);
michael@0 41 do_check_eq(ext, aExt);
michael@0 42
michael@0 43 // If we modify the base name and concatenate it with the extension again,
michael@0 44 // another roundtrip through the function should give a consistent result.
michael@0 45 // The only exception is when we introduce an extension in a file name that
michael@0 46 // didn't have one or that ended with one of the special cases like ".gz". If
michael@0 47 // we avoid using a dot and we introduce at least another special character,
michael@0 48 // the results are always consistent.
michael@0 49 [base, ext] = DownloadPaths.splitBaseNameAndExtension("(" + base + ")" + ext);
michael@0 50 do_check_eq(base, "(" + aBase + ")");
michael@0 51 do_check_eq(ext, aExt);
michael@0 52 }
michael@0 53
michael@0 54 function testCreateNiceUniqueFile(aTempFile, aExpectedLeafName)
michael@0 55 {
michael@0 56 var createdFile = DownloadPaths.createNiceUniqueFile(aTempFile);
michael@0 57 do_check_eq(createdFile.leafName, aExpectedLeafName);
michael@0 58 }
michael@0 59
michael@0 60 function run_test()
michael@0 61 {
michael@0 62 // Usual file names.
michael@0 63 testSplitBaseNameAndExtension("base", ["base", ""]);
michael@0 64 testSplitBaseNameAndExtension("base.ext", ["base", ".ext"]);
michael@0 65 testSplitBaseNameAndExtension("base.application", ["base", ".application"]);
michael@0 66 testSplitBaseNameAndExtension("base.x.Z", ["base", ".x.Z"]);
michael@0 67 testSplitBaseNameAndExtension("base.ext.Z", ["base", ".ext.Z"]);
michael@0 68 testSplitBaseNameAndExtension("base.ext.gz", ["base", ".ext.gz"]);
michael@0 69 testSplitBaseNameAndExtension("base.ext.Bz2", ["base", ".ext.Bz2"]);
michael@0 70 testSplitBaseNameAndExtension("base..ext", ["base.", ".ext"]);
michael@0 71 testSplitBaseNameAndExtension("base..Z", ["base.", ".Z"]);
michael@0 72 testSplitBaseNameAndExtension("base. .Z", ["base. ", ".Z"]);
michael@0 73 testSplitBaseNameAndExtension("base.base.Bz2", ["base.base", ".Bz2"]);
michael@0 74 testSplitBaseNameAndExtension("base .ext", ["base ", ".ext"]);
michael@0 75
michael@0 76 // Corner cases. A name ending with a dot technically has no extension, but
michael@0 77 // we consider the ending dot separately from the base name so that modifying
michael@0 78 // the latter never results in an extension being introduced accidentally.
michael@0 79 // Names beginning with a dot are hidden files on Unix-like platforms and if
michael@0 80 // their name doesn't contain another dot they should have no extension, but
michael@0 81 // on Windows the whole name is considered as an extension.
michael@0 82 testSplitBaseNameAndExtension("base.", ["base", "."]);
michael@0 83 testSplitBaseNameAndExtension(".ext", ["", ".ext"]);
michael@0 84
michael@0 85 // Unusual file names (not recommended as input to the function).
michael@0 86 testSplitBaseNameAndExtension("base. ", ["base", ". "]);
michael@0 87 testSplitBaseNameAndExtension("base ", ["base ", ""]);
michael@0 88 testSplitBaseNameAndExtension("", ["", ""]);
michael@0 89 testSplitBaseNameAndExtension(" ", [" ", ""]);
michael@0 90 testSplitBaseNameAndExtension(" . ", [" ", ". "]);
michael@0 91 testSplitBaseNameAndExtension(" .. ", [" .", ". "]);
michael@0 92 testSplitBaseNameAndExtension(" .ext", [" ", ".ext"]);
michael@0 93 testSplitBaseNameAndExtension(" .ext. ", [" .ext", ". "]);
michael@0 94 testSplitBaseNameAndExtension(" .ext.gz ", [" .ext", ".gz "]);
michael@0 95
michael@0 96 var destDir = createTemporarySaveDirectory();
michael@0 97 try {
michael@0 98 // Single extension.
michael@0 99 var tempFile = destDir.clone();
michael@0 100 tempFile.append("test.txt");
michael@0 101 testCreateNiceUniqueFile(tempFile, "test.txt");
michael@0 102 testCreateNiceUniqueFile(tempFile, "test(1).txt");
michael@0 103 testCreateNiceUniqueFile(tempFile, "test(2).txt");
michael@0 104
michael@0 105 // Double extension.
michael@0 106 tempFile.leafName = "test.tar.gz";
michael@0 107 testCreateNiceUniqueFile(tempFile, "test.tar.gz");
michael@0 108 testCreateNiceUniqueFile(tempFile, "test(1).tar.gz");
michael@0 109 testCreateNiceUniqueFile(tempFile, "test(2).tar.gz");
michael@0 110
michael@0 111 // Test automatic shortening of long file names. We don't know exactly how
michael@0 112 // many characters are removed, because it depends on the name of the folder
michael@0 113 // where the file is located.
michael@0 114 tempFile.leafName = new Array(256).join("T") + ".txt";
michael@0 115 var newFile = DownloadPaths.createNiceUniqueFile(tempFile);
michael@0 116 do_check_true(newFile.leafName.length < tempFile.leafName.length);
michael@0 117 do_check_eq(newFile.leafName.slice(-4), ".txt");
michael@0 118
michael@0 119 // Creating a valid file name from an invalid one is not always possible.
michael@0 120 tempFile.append("file-under-long-directory.txt");
michael@0 121 try {
michael@0 122 DownloadPaths.createNiceUniqueFile(tempFile);
michael@0 123 do_throw("Exception expected with a long parent directory name.")
michael@0 124 } catch (e) {
michael@0 125 // An exception is expected, but we don't know which one exactly.
michael@0 126 }
michael@0 127 } finally {
michael@0 128 // Clean up the temporary directory.
michael@0 129 destDir.remove(true);
michael@0 130 }
michael@0 131 }

mercurial