xpcom/tests/unit/test_localfile.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/unit/test_localfile.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,151 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +const Cr = Components.results;
    1.11 +const CC = Components.Constructor;
    1.12 +const Ci = Components.interfaces;
    1.13 +
    1.14 +const MAX_TIME_DIFFERENCE = 2500;
    1.15 +const MILLIS_PER_DAY      = 1000 * 60 * 60 * 24;
    1.16 +
    1.17 +var LocalFile = CC("@mozilla.org/file/local;1", "nsILocalFile", "initWithPath");
    1.18 +
    1.19 +function run_test()
    1.20 +{
    1.21 +  test_toplevel_parent_is_null();
    1.22 +  test_normalize_crash_if_media_missing();
    1.23 +  test_file_modification_time();
    1.24 +  test_directory_modification_time();
    1.25 +  test_diskSpaceAvailable();
    1.26 +}
    1.27 +
    1.28 +function test_toplevel_parent_is_null()
    1.29 +{
    1.30 +  try
    1.31 +  {
    1.32 +    var lf = new LocalFile("C:\\");
    1.33 +
    1.34 +    // not required by API, but a property on which the implementation of
    1.35 +    // parent == null relies for correctness
    1.36 +    do_check_true(lf.path.length == 2);
    1.37 +
    1.38 +    do_check_true(lf.parent === null);
    1.39 +  }
    1.40 +  catch (e)
    1.41 +  {
    1.42 +    // not Windows
    1.43 +    do_check_eq(e.result, Cr.NS_ERROR_FILE_UNRECOGNIZED_PATH);
    1.44 +  }
    1.45 +}
    1.46 +
    1.47 +function test_normalize_crash_if_media_missing()
    1.48 +{
    1.49 +  const a="a".charCodeAt(0);
    1.50 +  const z="z".charCodeAt(0);
    1.51 +  for (var i = a; i <= z; ++i)
    1.52 +  {
    1.53 +    try
    1.54 +    {
    1.55 +      LocalFile(String.fromCharCode(i)+":.\\test").normalize();
    1.56 +    }
    1.57 +    catch (e)
    1.58 +    {
    1.59 +    }
    1.60 +  }
    1.61 +}
    1.62 +
    1.63 +// Tests that changing a file's modification time is possible   
    1.64 +function test_file_modification_time()
    1.65 +{
    1.66 +  var file = do_get_profile();
    1.67 +  file.append("testfile");
    1.68 +
    1.69 +  // Should never happen but get rid of it anyway
    1.70 +  if (file.exists())
    1.71 +    file.remove(true);
    1.72 +
    1.73 +  var now = Date.now();
    1.74 +  file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0644);
    1.75 +  do_check_true(file.exists());
    1.76 +
    1.77 +  // Modification time may be out by up to 2 seconds on FAT filesystems. Test
    1.78 +  // with a bit of leeway, close enough probably means it is correct.
    1.79 +  var diff = Math.abs(file.lastModifiedTime - now);
    1.80 +  do_check_true(diff < MAX_TIME_DIFFERENCE);
    1.81 +
    1.82 +  var yesterday = now - MILLIS_PER_DAY;
    1.83 +  file.lastModifiedTime = yesterday;
    1.84 +
    1.85 +  diff = Math.abs(file.lastModifiedTime - yesterday);
    1.86 +  do_check_true(diff < MAX_TIME_DIFFERENCE);
    1.87 +
    1.88 +  var tomorrow = now - MILLIS_PER_DAY;
    1.89 +  file.lastModifiedTime = tomorrow;
    1.90 +
    1.91 +  diff = Math.abs(file.lastModifiedTime - tomorrow);
    1.92 +  do_check_true(diff < MAX_TIME_DIFFERENCE);
    1.93 +
    1.94 +  var bug377307 = 1172950238000;
    1.95 +  file.lastModifiedTime = bug377307;
    1.96 +
    1.97 +  diff = Math.abs(file.lastModifiedTime - bug377307);
    1.98 +  do_check_true(diff < MAX_TIME_DIFFERENCE);
    1.99 +
   1.100 +  file.remove(true);
   1.101 +}
   1.102 +
   1.103 +// Tests that changing a directory's modification time is possible   
   1.104 +function test_directory_modification_time()
   1.105 +{
   1.106 +  var dir = do_get_profile();
   1.107 +  dir.append("testdir");
   1.108 +
   1.109 +  // Should never happen but get rid of it anyway
   1.110 +  if (dir.exists())
   1.111 +    dir.remove(true);
   1.112 +
   1.113 +  var now = Date.now();
   1.114 +  dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
   1.115 +  do_check_true(dir.exists());
   1.116 +
   1.117 +  // Modification time may be out by up to 2 seconds on FAT filesystems. Test
   1.118 +  // with a bit of leeway, close enough probably means it is correct.
   1.119 +  var diff = Math.abs(dir.lastModifiedTime - now);
   1.120 +  do_check_true(diff < MAX_TIME_DIFFERENCE);
   1.121 +
   1.122 +  var yesterday = now - MILLIS_PER_DAY;
   1.123 +  dir.lastModifiedTime = yesterday;
   1.124 +
   1.125 +  diff = Math.abs(dir.lastModifiedTime - yesterday);
   1.126 +  do_check_true(diff < MAX_TIME_DIFFERENCE);
   1.127 +
   1.128 +  var tomorrow = now - MILLIS_PER_DAY;
   1.129 +  dir.lastModifiedTime = tomorrow;
   1.130 +
   1.131 +  diff = Math.abs(dir.lastModifiedTime - tomorrow);
   1.132 +  do_check_true(diff < MAX_TIME_DIFFERENCE);
   1.133 +
   1.134 +  dir.remove(true);
   1.135 +}
   1.136 +
   1.137 +function test_diskSpaceAvailable()
   1.138 +{
   1.139 +  let file = do_get_profile();
   1.140 +  file.QueryInterface(Ci.nsILocalFile);
   1.141 +
   1.142 +  let bytes = file.diskSpaceAvailable;
   1.143 +  do_check_true(bytes > 0);
   1.144 +
   1.145 +  file.append("testfile");
   1.146 +  if (file.exists())
   1.147 +    file.remove(true);
   1.148 +  file.create(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("644", 8));
   1.149 +
   1.150 +  bytes = file.diskSpaceAvailable;
   1.151 +  do_check_true(bytes > 0);
   1.152 +
   1.153 +  file.remove(true);
   1.154 +}

mercurial