entry_test.js 8.3 KB

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