michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const Cr = Components.results; michael@0: const CC = Components.Constructor; michael@0: const Ci = Components.interfaces; michael@0: michael@0: const MAX_TIME_DIFFERENCE = 2500; michael@0: const MILLIS_PER_DAY = 1000 * 60 * 60 * 24; michael@0: michael@0: var LocalFile = CC("@mozilla.org/file/local;1", "nsILocalFile", "initWithPath"); michael@0: michael@0: function run_test() michael@0: { michael@0: test_toplevel_parent_is_null(); michael@0: test_normalize_crash_if_media_missing(); michael@0: test_file_modification_time(); michael@0: test_directory_modification_time(); michael@0: test_diskSpaceAvailable(); michael@0: } michael@0: michael@0: function test_toplevel_parent_is_null() michael@0: { michael@0: try michael@0: { michael@0: var lf = new LocalFile("C:\\"); michael@0: michael@0: // not required by API, but a property on which the implementation of michael@0: // parent == null relies for correctness michael@0: do_check_true(lf.path.length == 2); michael@0: michael@0: do_check_true(lf.parent === null); michael@0: } michael@0: catch (e) michael@0: { michael@0: // not Windows michael@0: do_check_eq(e.result, Cr.NS_ERROR_FILE_UNRECOGNIZED_PATH); michael@0: } michael@0: } michael@0: michael@0: function test_normalize_crash_if_media_missing() michael@0: { michael@0: const a="a".charCodeAt(0); michael@0: const z="z".charCodeAt(0); michael@0: for (var i = a; i <= z; ++i) michael@0: { michael@0: try michael@0: { michael@0: LocalFile(String.fromCharCode(i)+":.\\test").normalize(); michael@0: } michael@0: catch (e) michael@0: { michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Tests that changing a file's modification time is possible michael@0: function test_file_modification_time() michael@0: { michael@0: var file = do_get_profile(); michael@0: file.append("testfile"); michael@0: michael@0: // Should never happen but get rid of it anyway michael@0: if (file.exists()) michael@0: file.remove(true); michael@0: michael@0: var now = Date.now(); michael@0: file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0644); michael@0: do_check_true(file.exists()); michael@0: michael@0: // Modification time may be out by up to 2 seconds on FAT filesystems. Test michael@0: // with a bit of leeway, close enough probably means it is correct. michael@0: var diff = Math.abs(file.lastModifiedTime - now); michael@0: do_check_true(diff < MAX_TIME_DIFFERENCE); michael@0: michael@0: var yesterday = now - MILLIS_PER_DAY; michael@0: file.lastModifiedTime = yesterday; michael@0: michael@0: diff = Math.abs(file.lastModifiedTime - yesterday); michael@0: do_check_true(diff < MAX_TIME_DIFFERENCE); michael@0: michael@0: var tomorrow = now - MILLIS_PER_DAY; michael@0: file.lastModifiedTime = tomorrow; michael@0: michael@0: diff = Math.abs(file.lastModifiedTime - tomorrow); michael@0: do_check_true(diff < MAX_TIME_DIFFERENCE); michael@0: michael@0: var bug377307 = 1172950238000; michael@0: file.lastModifiedTime = bug377307; michael@0: michael@0: diff = Math.abs(file.lastModifiedTime - bug377307); michael@0: do_check_true(diff < MAX_TIME_DIFFERENCE); michael@0: michael@0: file.remove(true); michael@0: } michael@0: michael@0: // Tests that changing a directory's modification time is possible michael@0: function test_directory_modification_time() michael@0: { michael@0: var dir = do_get_profile(); michael@0: dir.append("testdir"); michael@0: michael@0: // Should never happen but get rid of it anyway michael@0: if (dir.exists()) michael@0: dir.remove(true); michael@0: michael@0: var now = Date.now(); michael@0: dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755); michael@0: do_check_true(dir.exists()); michael@0: michael@0: // Modification time may be out by up to 2 seconds on FAT filesystems. Test michael@0: // with a bit of leeway, close enough probably means it is correct. michael@0: var diff = Math.abs(dir.lastModifiedTime - now); michael@0: do_check_true(diff < MAX_TIME_DIFFERENCE); michael@0: michael@0: var yesterday = now - MILLIS_PER_DAY; michael@0: dir.lastModifiedTime = yesterday; michael@0: michael@0: diff = Math.abs(dir.lastModifiedTime - yesterday); michael@0: do_check_true(diff < MAX_TIME_DIFFERENCE); michael@0: michael@0: var tomorrow = now - MILLIS_PER_DAY; michael@0: dir.lastModifiedTime = tomorrow; michael@0: michael@0: diff = Math.abs(dir.lastModifiedTime - tomorrow); michael@0: do_check_true(diff < MAX_TIME_DIFFERENCE); michael@0: michael@0: dir.remove(true); michael@0: } michael@0: michael@0: function test_diskSpaceAvailable() michael@0: { michael@0: let file = do_get_profile(); michael@0: file.QueryInterface(Ci.nsILocalFile); michael@0: michael@0: let bytes = file.diskSpaceAvailable; michael@0: do_check_true(bytes > 0); michael@0: michael@0: file.append("testfile"); michael@0: if (file.exists()) michael@0: file.remove(true); michael@0: file.create(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("644", 8)); michael@0: michael@0: bytes = file.diskSpaceAvailable; michael@0: do_check_true(bytes > 0); michael@0: michael@0: file.remove(true); michael@0: }