fileentry_test.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.testing.fs.FileEntryTest');
  15. goog.setTestOnly('goog.testing.fs.FileEntryTest');
  16. goog.require('goog.testing.MockClock');
  17. goog.require('goog.testing.fs.FileEntry');
  18. goog.require('goog.testing.fs.FileSystem');
  19. goog.require('goog.testing.jsunit');
  20. var fs, file, fileEntry, mockClock, currentTime;
  21. function setUp() {
  22. // Temporarily install the MockClock for predictable timestamps on new files.
  23. mockClock = new goog.testing.MockClock(true);
  24. fs = new goog.testing.fs.FileSystem();
  25. fileEntry = fs.getRoot().createDirectorySync('foo').createFileSync('bar');
  26. // Uninstall the MockClock since it interferes with goog.Promise execution.
  27. // Tests that require specific timing may reinstall the MockClock and manually
  28. // advance promises using mockClock.tick().
  29. mockClock.uninstall();
  30. }
  31. function testIsFile() {
  32. assertTrue(fileEntry.isFile());
  33. }
  34. function testIsDirectory() {
  35. assertFalse(fileEntry.isDirectory());
  36. }
  37. function testFile() {
  38. var testFile =
  39. new goog.testing.fs.FileEntry(fs, fs.getRoot(), 'test', 'hello world');
  40. return testFile.file().then(function(f) {
  41. assertEquals('test', f.name);
  42. assertEquals('hello world', f.toString());
  43. });
  44. }
  45. function testGetLastModified() {
  46. // Advance the clock to a known time.
  47. mockClock.install();
  48. mockClock.tick(53);
  49. var testFile = new goog.testing.fs.FileEntry(
  50. fs, fs.getRoot(), 'timeTest', 'hello world');
  51. var promise = testFile.getLastModified()
  52. .then(function(date) { assertEquals(53, date.getTime()); })
  53. .thenAlways(function() { mockClock.uninstall(); });
  54. mockClock.tick();
  55. return promise;
  56. }
  57. function testGetMetadata() {
  58. // Advance the clock to a known time.
  59. mockClock.install();
  60. mockClock.tick(54);
  61. var testFile = new goog.testing.fs.FileEntry(
  62. fs, fs.getRoot(), 'timeTest', 'hello world');
  63. var promise = testFile.getMetadata()
  64. .then(function(metadata) {
  65. assertEquals(54, metadata.modificationTime.getTime());
  66. })
  67. .thenAlways(function() { mockClock.uninstall(); });
  68. mockClock.tick();
  69. return promise;
  70. }