Debian Codegraph @

closure-library/closure/goog/testing/fs/entry_test.js

raw
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.EntryTest');
16 goog.setTestOnly('goog.testing.fs.EntryTest');
17
18 goog.require('goog.fs.DirectoryEntry');
19 goog.require('goog.fs.Error');
20 goog.require('goog.testing.MockClock');
21 goog.require('goog.testing.TestCase');
22 goog.require('goog.testing.fs.FileSystem');
23 goog.require('goog.testing.jsunit');
24
25 var fs, file, mockClock;
26
27 function setUpPage() {
28 // This test has a tendency to timeout on external Travis testing
29 // infrastructure. Up to 5s from 1s.
30 goog.testing.TestCase.getActiveTestCase().promiseTimeout = 5000;
31 }
32
33 function setUp() {
34 // Install the MockClock to create predictable timestamps for new files.
35 mockClock = new goog.testing.MockClock(true);
36
37 fs = new goog.testing.fs.FileSystem();
38 file = fs.getRoot()
39 .getDirectorySync('foo', goog.fs.DirectoryEntry.Behavior.CREATE)
40 .getFileSync('bar', goog.fs.DirectoryEntry.Behavior.CREATE);
41
42 // Uninstall the MockClock since it interferes with goog.Promise execution.
43 // Tests that require specific timing may reinstall the MockClock and manually
44 // advance promises using mockClock.tick().
45 mockClock.uninstall();
46 }
47
48 function testGetName() {
49 assertEquals('bar', file.getName());
50 }
51
52 function testGetFullPath() {
53 assertEquals('/foo/bar', file.getFullPath());
54 assertEquals('/', fs.getRoot().getFullPath());
55 }
56
57 function testGetFileSystem() {
58 assertEquals(fs, file.getFileSystem());
59 }
60
61 function testMoveTo() {
62 return file.moveTo(fs.getRoot()).then(function(newFile) {
63 assertTrue(file.deleted);
64 assertFalse(newFile.deleted);
65 assertEquals('/bar', newFile.getFullPath());
66 assertEquals(fs.getRoot(), newFile.parent);
67 assertEquals(newFile, fs.getRoot().getFileSync('bar'));
68 assertFalse(fs.getRoot().getDirectorySync('foo').hasChild('bar'));
69 });
70 }
71
72 function testMoveToNewName() {
73 // Advance the clock to an arbitrary, known time.
74 mockClock.install();
75 mockClock.tick(71);
76 var promise = file.moveTo(fs.getRoot(), 'baz')
77 .then(function(newFile) {
78 mockClock.tick();
79 assertTrue(file.deleted);
80 assertFalse(newFile.deleted);
81 assertEquals('/baz', newFile.getFullPath());
82 assertEquals(fs.getRoot(), newFile.parent);
83 assertEquals(newFile, fs.getRoot().getFileSync('baz'));
84
85 var oldParentDir = fs.getRoot().getDirectorySync('foo');
86 assertFalse(oldParentDir.hasChild('bar'));
87 assertFalse(oldParentDir.hasChild('baz'));
88
89 return oldParentDir.getLastModified();
90 })
91 .then(function(lastModifiedDate) {
92 assertEquals(71, lastModifiedDate.getTime());
93 var oldParentDir = fs.getRoot().getDirectorySync('foo');
94 return oldParentDir.getMetadata();
95 })
96 .then(function(metadata) {
97 assertEquals(71, metadata.modificationTime.getTime());
98 return fs.getRoot().getLastModified();
99 })
100 .then(function(rootLastModifiedDate) {
101 assertEquals(71, rootLastModifiedDate.getTime());
102 return fs.getRoot().getMetadata();
103 })
104 .then(function(rootMetadata) {
105 assertEquals(71, rootMetadata.modificationTime.getTime());
106 })
107 .thenAlways(function() { mockClock.uninstall(); });
108 mockClock.tick();
109 return promise;
110 }
111
112 function testMoveDeletedFile() {
113 return assertFailsWhenDeleted(function() {
114 return file.moveTo(fs.getRoot());
115 });
116 }
117
118 function testCopyTo() {
119 mockClock.install();
120 mockClock.tick(61);
121 var promise = file.copyTo(fs.getRoot())
122 .then(function(newFile) {
123 assertFalse(file.deleted);
124 assertFalse(newFile.deleted);
125 assertEquals('/bar', newFile.getFullPath());
126 assertEquals(fs.getRoot(), newFile.parent);
127 assertEquals(newFile, fs.getRoot().getFileSync('bar'));
128
129 var oldParentDir = fs.getRoot().getDirectorySync('foo');
130 assertEquals(file, oldParentDir.getFileSync('bar'));
131 return oldParentDir.getLastModified();
132 })
133 .then(function(lastModifiedDate) {
134 assertEquals(
135 'The original parent directory was not modified.', 0,
136 lastModifiedDate.getTime());
137 var oldParentDir = fs.getRoot().getDirectorySync('foo');
138 return oldParentDir.getMetadata();
139 })
140 .then(function(metadata) {
141 assertEquals(
142 'The original parent directory was not modified.', 0,
143 metadata.modificationTime.getTime());
144 return fs.getRoot().getLastModified();
145 })
146 .then(function(rootLastModifiedDate) {
147 assertEquals(61, rootLastModifiedDate.getTime());
148 return fs.getRoot().getMetadata();
149 })
150 .then(function(rootMetadata) {
151 assertEquals(61, rootMetadata.modificationTime.getTime());
152 })
153 .thenAlways(function() { mockClock.uninstall(); });
154 mockClock.tick();
155 return promise;
156 }
157
158 function testCopyToNewName() {
159 return file.copyTo(fs.getRoot(), 'baz').addCallback(function(newFile) {
160 assertFalse(file.deleted);
161 assertFalse(newFile.deleted);
162 assertEquals('/baz', newFile.getFullPath());
163 assertEquals(fs.getRoot(), newFile.parent);
164 assertEquals(newFile, fs.getRoot().getFileSync('baz'));
165 assertEquals(file, fs.getRoot().getDirectorySync('foo').getFileSync('bar'));
166 assertFalse(fs.getRoot().getDirectorySync('foo').hasChild('baz'));
167 });
168 }
169
170 function testCopyDeletedFile() {
171 return assertFailsWhenDeleted(function() {
172 return file.copyTo(fs.getRoot());
173 });
174 }
175
176 function testRemove() {
177 mockClock.install();
178 mockClock.tick(57);
179 var promise = file.remove()
180 .then(function() {
181 mockClock.tick();
182 var parentDir = fs.getRoot().getDirectorySync('foo');
183
184 assertTrue(file.deleted);
185 assertFalse(parentDir.hasChild('bar'));
186
187 return parentDir.getLastModified();
188 })
189 .then(function(date) {
190 assertEquals(57, date.getTime());
191 var parentDir = fs.getRoot().getDirectorySync('foo');
192 return parentDir.getMetadata();
193 })
194 .then(function(metadata) {
195 assertEquals(57, metadata.modificationTime.getTime());
196 })
197 .thenAlways(function() { mockClock.uninstall(); });
198 mockClock.tick();
199 return promise;
200 }
201
202 function testRemoveDeletedFile() {
203 return assertFailsWhenDeleted(function() { return file.remove(); });
204 }
205
206 function testGetParent() {
207 return file.getParent().then(function(p) {
208 assertEquals(file.parent, p);
209 assertEquals(fs.getRoot().getDirectorySync('foo'), p);
210 assertEquals('/foo', p.getFullPath());
211 });
212 }
213
214 function testGetDeletedFileParent() {
215 return assertFailsWhenDeleted(function() { return file.getParent(); });
216 }
217
218 function assertFailsWhenDeleted(fn) {
219 return file.remove().then(fn).then(
220 function() { fail('Expected an error'); },
221 function(err) {
222 assertEquals(goog.fs.Error.ErrorCode.NOT_FOUND, err.code);
223 });
224 }