test_localfile.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 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. var Cr = Components.results;
  7. var CC = Components.Constructor;
  8. var Ci = Components.interfaces;
  9. const MAX_TIME_DIFFERENCE = 2500;
  10. const MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
  11. var LocalFile = CC("@mozilla.org/file/local;1", "nsILocalFile", "initWithPath");
  12. function run_test()
  13. {
  14. test_toplevel_parent_is_null();
  15. test_normalize_crash_if_media_missing();
  16. test_file_modification_time();
  17. test_directory_modification_time();
  18. test_diskSpaceAvailable();
  19. }
  20. function test_toplevel_parent_is_null()
  21. {
  22. try
  23. {
  24. var lf = new LocalFile("C:\\");
  25. // not required by API, but a property on which the implementation of
  26. // parent == null relies for correctness
  27. do_check_true(lf.path.length == 2);
  28. do_check_true(lf.parent === null);
  29. }
  30. catch (e)
  31. {
  32. // not Windows
  33. do_check_eq(e.result, Cr.NS_ERROR_FILE_UNRECOGNIZED_PATH);
  34. }
  35. }
  36. function test_normalize_crash_if_media_missing()
  37. {
  38. const a="a".charCodeAt(0);
  39. const z="z".charCodeAt(0);
  40. for (var i = a; i <= z; ++i)
  41. {
  42. try
  43. {
  44. LocalFile(String.fromCharCode(i)+":.\\test").normalize();
  45. }
  46. catch (e)
  47. {
  48. }
  49. }
  50. }
  51. // Tests that changing a file's modification time is possible
  52. function test_file_modification_time()
  53. {
  54. var file = do_get_profile();
  55. file.append("testfile");
  56. // Should never happen but get rid of it anyway
  57. if (file.exists())
  58. file.remove(true);
  59. var now = Date.now();
  60. file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o644);
  61. do_check_true(file.exists());
  62. // Modification time may be out by up to 2 seconds on FAT filesystems. Test
  63. // with a bit of leeway, close enough probably means it is correct.
  64. var diff = Math.abs(file.lastModifiedTime - now);
  65. do_check_true(diff < MAX_TIME_DIFFERENCE);
  66. var yesterday = now - MILLIS_PER_DAY;
  67. file.lastModifiedTime = yesterday;
  68. diff = Math.abs(file.lastModifiedTime - yesterday);
  69. do_check_true(diff < MAX_TIME_DIFFERENCE);
  70. var tomorrow = now - MILLIS_PER_DAY;
  71. file.lastModifiedTime = tomorrow;
  72. diff = Math.abs(file.lastModifiedTime - tomorrow);
  73. do_check_true(diff < MAX_TIME_DIFFERENCE);
  74. var bug377307 = 1172950238000;
  75. file.lastModifiedTime = bug377307;
  76. diff = Math.abs(file.lastModifiedTime - bug377307);
  77. do_check_true(diff < MAX_TIME_DIFFERENCE);
  78. file.remove(true);
  79. }
  80. // Tests that changing a directory's modification time is possible
  81. function test_directory_modification_time()
  82. {
  83. var dir = do_get_profile();
  84. dir.append("testdir");
  85. // Should never happen but get rid of it anyway
  86. if (dir.exists())
  87. dir.remove(true);
  88. var now = Date.now();
  89. dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
  90. do_check_true(dir.exists());
  91. // Modification time may be out by up to 2 seconds on FAT filesystems. Test
  92. // with a bit of leeway, close enough probably means it is correct.
  93. var diff = Math.abs(dir.lastModifiedTime - now);
  94. do_check_true(diff < MAX_TIME_DIFFERENCE);
  95. var yesterday = now - MILLIS_PER_DAY;
  96. dir.lastModifiedTime = yesterday;
  97. diff = Math.abs(dir.lastModifiedTime - yesterday);
  98. do_check_true(diff < MAX_TIME_DIFFERENCE);
  99. var tomorrow = now - MILLIS_PER_DAY;
  100. dir.lastModifiedTime = tomorrow;
  101. diff = Math.abs(dir.lastModifiedTime - tomorrow);
  102. do_check_true(diff < MAX_TIME_DIFFERENCE);
  103. dir.remove(true);
  104. }
  105. function test_diskSpaceAvailable()
  106. {
  107. let file = do_get_profile();
  108. file.QueryInterface(Ci.nsILocalFile);
  109. let bytes = file.diskSpaceAvailable;
  110. do_check_true(bytes > 0);
  111. file.append("testfile");
  112. if (file.exists())
  113. file.remove(true);
  114. file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o644);
  115. bytes = file.diskSpaceAvailable;
  116. do_check_true(bytes > 0);
  117. file.remove(true);
  118. }