| 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 |
|
| 15 |
goog.provide('goog.testing.fs.FileEntryTest'); |
| 16 |
goog.setTestOnly('goog.testing.fs.FileEntryTest'); |
| 17 |
|
| 18 |
goog.require('goog.testing.MockClock'); |
| 19 |
goog.require('goog.testing.fs.FileEntry'); |
| 20 |
goog.require('goog.testing.fs.FileSystem'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
|
| 23 |
var fs, file, fileEntry, mockClock, currentTime; |
| 24 |
|
| 25 |
function setUp() { |
| 26 |
// Temporarily install the MockClock for predictable timestamps on new files. |
| 27 |
mockClock = new goog.testing.MockClock(true); |
| 28 |
fs = new goog.testing.fs.FileSystem(); |
| 29 |
fileEntry = fs.getRoot().createDirectorySync('foo').createFileSync('bar'); |
| 30 |
|
| 31 |
// Uninstall the MockClock since it interferes with goog.Promise execution. |
| 32 |
// Tests that require specific timing may reinstall the MockClock and manually |
| 33 |
// advance promises using mockClock.tick(). |
| 34 |
mockClock.uninstall(); |
| 35 |
} |
| 36 |
|
| 37 |
function testIsFile() { |
| 38 |
assertTrue(fileEntry.isFile()); |
| 39 |
} |
| 40 |
|
| 41 |
function testIsDirectory() { |
| 42 |
assertFalse(fileEntry.isDirectory()); |
| 43 |
} |
| 44 |
|
| 45 |
function testFile() { |
| 46 |
var testFile = |
| 47 |
new goog.testing.fs.FileEntry(fs, fs.getRoot(), 'test', 'hello world'); |
| 48 |
return testFile.file().then(function(f) { |
| 49 |
assertEquals('test', f.name); |
| 50 |
assertEquals('hello world', f.toString()); |
| 51 |
}); |
| 52 |
} |
| 53 |
|
| 54 |
function testGetLastModified() { |
| 55 |
// Advance the clock to a known time. |
| 56 |
mockClock.install(); |
| 57 |
mockClock.tick(53); |
| 58 |
var testFile = new goog.testing.fs.FileEntry( |
| 59 |
fs, fs.getRoot(), 'timeTest', 'hello world'); |
| 60 |
var promise = testFile.getLastModified() |
| 61 |
.then(function(date) { assertEquals(53, date.getTime()); }) |
| 62 |
.thenAlways(function() { mockClock.uninstall(); }); |
| 63 |
mockClock.tick(); |
| 64 |
return promise; |
| 65 |
} |
| 66 |
|
| 67 |
function testGetMetadata() { |
| 68 |
// Advance the clock to a known time. |
| 69 |
mockClock.install(); |
| 70 |
mockClock.tick(54); |
| 71 |
var testFile = new goog.testing.fs.FileEntry( |
| 72 |
fs, fs.getRoot(), 'timeTest', 'hello world'); |
| 73 |
var promise = testFile.getMetadata() |
| 74 |
.then(function(metadata) { |
| 75 |
assertEquals(54, metadata.modificationTime.getTime()); |
| 76 |
}) |
| 77 |
.thenAlways(function() { mockClock.uninstall(); }); |
| 78 |
mockClock.tick(); |
| 79 |
return promise; |
| 80 |
} |