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