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 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is XPCOM unit tests.
18 * The Initial Developer of the Original Code is
19 * Jeff Walden <jwalden+code@mit.edu>.
20 * Portions created by the Initial Developer are Copyright (C) 2007
21 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 const Cr = Components.results;
40 const CC = Components.Constructor;
41 const Ci = Components.interfaces;
43 const MAX_TIME_DIFFERENCE = 2500;
44 const MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
46 var LocalFile = CC("@mozilla.org/file/local;1", "nsILocalFile", "initWithPath");
50 test_toplevel_parent_is_null();
51 test_normalize_crash_if_media_missing();
52 test_file_modification_time();
53 test_directory_modification_time();
56 function test_toplevel_parent_is_null()
60 var lf = new LocalFile("C:\\");
62 // not required by API, but a property on which the implementation of
63 // parent == null relies for correctness
64 do_check_true(lf.path.length == 2);
66 do_check_true(lf.parent === null);
71 do_check_eq(e.result, Cr.NS_ERROR_FILE_UNRECOGNIZED_PATH);
75 function test_normalize_crash_if_media_missing()
77 const a="a".charCodeAt(0);
78 const z="z".charCodeAt(0);
79 for (var i = a; i <= z; ++i)
83 LocalFile(String.fromCharCode(i)+":.\\test").normalize();
91 // Tests that changing a file's modification time is possible
92 function test_file_modification_time()
94 var file = do_get_profile();
95 file.append("testfile");
97 // Should never happen but get rid of it anyway
101 var now = Date.now();
102 file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0644);
103 do_check_true(file.exists());
105 // Modification time may be out by up to 2 seconds on FAT filesystems. Test
106 // with a bit of leeway, close enough probably means it is correct.
107 var diff = Math.abs(file.lastModifiedTime - now);
108 do_check_true(diff < MAX_TIME_DIFFERENCE);
110 var yesterday = now - MILLIS_PER_DAY;
111 file.lastModifiedTime = yesterday;
113 diff = Math.abs(file.lastModifiedTime - yesterday);
114 do_check_true(diff < MAX_TIME_DIFFERENCE);
116 var tomorrow = now - MILLIS_PER_DAY;
117 file.lastModifiedTime = tomorrow;
119 diff = Math.abs(file.lastModifiedTime - tomorrow);
120 do_check_true(diff < MAX_TIME_DIFFERENCE);
125 // Tests that changing a directory's modification time is possible
126 function test_directory_modification_time()
128 var dir = do_get_profile();
129 dir.append("testdir");
131 // Should never happen but get rid of it anyway
135 var now = Date.now();
136 dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
137 do_check_true(dir.exists());
139 // Modification time may be out by up to 2 seconds on FAT filesystems. Test
140 // with a bit of leeway, close enough probably means it is correct.
141 var diff = Math.abs(dir.lastModifiedTime - now);
142 do_check_true(diff < MAX_TIME_DIFFERENCE);
144 var yesterday = now - MILLIS_PER_DAY;
145 dir.lastModifiedTime = yesterday;
147 diff = Math.abs(dir.lastModifiedTime - yesterday);
148 do_check_true(diff < MAX_TIME_DIFFERENCE);
150 var tomorrow = now - MILLIS_PER_DAY;
151 dir.lastModifiedTime = tomorrow;
153 diff = Math.abs(dir.lastModifiedTime - tomorrow);
154 do_check_true(diff < MAX_TIME_DIFFERENCE);