|
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim:set ts=2 sw=2 sts=2 et: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 const Cr = Components.results; |
|
8 const CC = Components.Constructor; |
|
9 const Ci = Components.interfaces; |
|
10 |
|
11 const MAX_TIME_DIFFERENCE = 2500; |
|
12 const MILLIS_PER_DAY = 1000 * 60 * 60 * 24; |
|
13 |
|
14 var LocalFile = CC("@mozilla.org/file/local;1", "nsILocalFile", "initWithPath"); |
|
15 |
|
16 function run_test() |
|
17 { |
|
18 test_toplevel_parent_is_null(); |
|
19 test_normalize_crash_if_media_missing(); |
|
20 test_file_modification_time(); |
|
21 test_directory_modification_time(); |
|
22 test_diskSpaceAvailable(); |
|
23 } |
|
24 |
|
25 function test_toplevel_parent_is_null() |
|
26 { |
|
27 try |
|
28 { |
|
29 var lf = new LocalFile("C:\\"); |
|
30 |
|
31 // not required by API, but a property on which the implementation of |
|
32 // parent == null relies for correctness |
|
33 do_check_true(lf.path.length == 2); |
|
34 |
|
35 do_check_true(lf.parent === null); |
|
36 } |
|
37 catch (e) |
|
38 { |
|
39 // not Windows |
|
40 do_check_eq(e.result, Cr.NS_ERROR_FILE_UNRECOGNIZED_PATH); |
|
41 } |
|
42 } |
|
43 |
|
44 function test_normalize_crash_if_media_missing() |
|
45 { |
|
46 const a="a".charCodeAt(0); |
|
47 const z="z".charCodeAt(0); |
|
48 for (var i = a; i <= z; ++i) |
|
49 { |
|
50 try |
|
51 { |
|
52 LocalFile(String.fromCharCode(i)+":.\\test").normalize(); |
|
53 } |
|
54 catch (e) |
|
55 { |
|
56 } |
|
57 } |
|
58 } |
|
59 |
|
60 // Tests that changing a file's modification time is possible |
|
61 function test_file_modification_time() |
|
62 { |
|
63 var file = do_get_profile(); |
|
64 file.append("testfile"); |
|
65 |
|
66 // Should never happen but get rid of it anyway |
|
67 if (file.exists()) |
|
68 file.remove(true); |
|
69 |
|
70 var now = Date.now(); |
|
71 file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0644); |
|
72 do_check_true(file.exists()); |
|
73 |
|
74 // Modification time may be out by up to 2 seconds on FAT filesystems. Test |
|
75 // with a bit of leeway, close enough probably means it is correct. |
|
76 var diff = Math.abs(file.lastModifiedTime - now); |
|
77 do_check_true(diff < MAX_TIME_DIFFERENCE); |
|
78 |
|
79 var yesterday = now - MILLIS_PER_DAY; |
|
80 file.lastModifiedTime = yesterday; |
|
81 |
|
82 diff = Math.abs(file.lastModifiedTime - yesterday); |
|
83 do_check_true(diff < MAX_TIME_DIFFERENCE); |
|
84 |
|
85 var tomorrow = now - MILLIS_PER_DAY; |
|
86 file.lastModifiedTime = tomorrow; |
|
87 |
|
88 diff = Math.abs(file.lastModifiedTime - tomorrow); |
|
89 do_check_true(diff < MAX_TIME_DIFFERENCE); |
|
90 |
|
91 var bug377307 = 1172950238000; |
|
92 file.lastModifiedTime = bug377307; |
|
93 |
|
94 diff = Math.abs(file.lastModifiedTime - bug377307); |
|
95 do_check_true(diff < MAX_TIME_DIFFERENCE); |
|
96 |
|
97 file.remove(true); |
|
98 } |
|
99 |
|
100 // Tests that changing a directory's modification time is possible |
|
101 function test_directory_modification_time() |
|
102 { |
|
103 var dir = do_get_profile(); |
|
104 dir.append("testdir"); |
|
105 |
|
106 // Should never happen but get rid of it anyway |
|
107 if (dir.exists()) |
|
108 dir.remove(true); |
|
109 |
|
110 var now = Date.now(); |
|
111 dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755); |
|
112 do_check_true(dir.exists()); |
|
113 |
|
114 // Modification time may be out by up to 2 seconds on FAT filesystems. Test |
|
115 // with a bit of leeway, close enough probably means it is correct. |
|
116 var diff = Math.abs(dir.lastModifiedTime - now); |
|
117 do_check_true(diff < MAX_TIME_DIFFERENCE); |
|
118 |
|
119 var yesterday = now - MILLIS_PER_DAY; |
|
120 dir.lastModifiedTime = yesterday; |
|
121 |
|
122 diff = Math.abs(dir.lastModifiedTime - yesterday); |
|
123 do_check_true(diff < MAX_TIME_DIFFERENCE); |
|
124 |
|
125 var tomorrow = now - MILLIS_PER_DAY; |
|
126 dir.lastModifiedTime = tomorrow; |
|
127 |
|
128 diff = Math.abs(dir.lastModifiedTime - tomorrow); |
|
129 do_check_true(diff < MAX_TIME_DIFFERENCE); |
|
130 |
|
131 dir.remove(true); |
|
132 } |
|
133 |
|
134 function test_diskSpaceAvailable() |
|
135 { |
|
136 let file = do_get_profile(); |
|
137 file.QueryInterface(Ci.nsILocalFile); |
|
138 |
|
139 let bytes = file.diskSpaceAvailable; |
|
140 do_check_true(bytes > 0); |
|
141 |
|
142 file.append("testfile"); |
|
143 if (file.exists()) |
|
144 file.remove(true); |
|
145 file.create(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("644", 8)); |
|
146 |
|
147 bytes = file.diskSpaceAvailable; |
|
148 do_check_true(bytes > 0); |
|
149 |
|
150 file.remove(true); |
|
151 } |