directoryentry_test.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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.DirectoryEntryTest');
  15. goog.setTestOnly('goog.testing.fs.DirectoryEntryTest');
  16. goog.require('goog.array');
  17. goog.require('goog.fs.DirectoryEntry');
  18. goog.require('goog.fs.Error');
  19. goog.require('goog.testing.MockClock');
  20. goog.require('goog.testing.TestCase');
  21. goog.require('goog.testing.fs.FileSystem');
  22. goog.require('goog.testing.jsunit');
  23. var Behavior = goog.fs.DirectoryEntry.Behavior;
  24. var fs, dir, mockClock;
  25. function setUpPage() {
  26. // This test has a tendency to timeout on external Travis testing
  27. // infrastructure. Up to 5s from 1s.
  28. goog.testing.TestCase.getActiveTestCase().promiseTimeout = 5000;
  29. }
  30. function setUp() {
  31. // Install the MockClock to create predictable timestamps for new files.
  32. mockClock = new goog.testing.MockClock(true);
  33. fs = new goog.testing.fs.FileSystem();
  34. dir = fs.getRoot().createDirectorySync('foo');
  35. dir.createDirectorySync('subdir').createFileSync('subfile');
  36. dir.createFileSync('file');
  37. mockClock.uninstall();
  38. }
  39. function testIsFile() {
  40. assertFalse(dir.isFile());
  41. }
  42. function testIsDirectory() {
  43. assertTrue(dir.isDirectory());
  44. }
  45. function testRemoveWithChildren() {
  46. dir.getFileSync('bar', Behavior.CREATE);
  47. return dir.remove().then(fail, function(e) {
  48. assertEquals(goog.fs.Error.ErrorCode.INVALID_MODIFICATION, e.code);
  49. });
  50. }
  51. function testRemoveWithoutChildren() {
  52. var emptyDir = dir.getDirectorySync('empty', Behavior.CREATE);
  53. return emptyDir.remove().then(function() {
  54. assertTrue(emptyDir.deleted);
  55. assertFalse(fs.getRoot().hasChild('empty'));
  56. });
  57. }
  58. function testRemoveRootRecursively() {
  59. var root = fs.getRoot();
  60. return root.removeRecursively().then(function() {
  61. assertTrue(dir.deleted);
  62. assertFalse(fs.getRoot().deleted);
  63. });
  64. }
  65. function testGetFile() {
  66. return dir.getFile('file')
  67. .then(function(file) {
  68. assertEquals(dir.getFileSync('file'), file);
  69. assertEquals('file', file.getName());
  70. assertEquals('/foo/file', file.getFullPath());
  71. assertTrue(file.isFile());
  72. return dir.getLastModified();
  73. })
  74. .then(function(date) {
  75. assertEquals(
  76. 'Reading a file should not update the modification date.', 0,
  77. date.getTime());
  78. return dir.getMetadata();
  79. })
  80. .then(function(metadata) {
  81. assertEquals(
  82. 'Reading a file should not update the metadata.', 0,
  83. metadata.modificationTime.getTime());
  84. });
  85. }
  86. function testGetFileFromSubdir() {
  87. return dir.getFile('subdir/subfile').then(function(file) {
  88. assertEquals(dir.getDirectorySync('subdir').getFileSync('subfile'), file);
  89. assertEquals('subfile', file.getName());
  90. assertEquals('/foo/subdir/subfile', file.getFullPath());
  91. assertTrue(file.isFile());
  92. });
  93. }
  94. function testGetAbsolutePaths() {
  95. return fs.getRoot()
  96. .getFile('/foo/subdir/subfile')
  97. .then(function(subfile) {
  98. assertEquals('/foo/subdir/subfile', subfile.getFullPath());
  99. return fs.getRoot().getDirectory('//foo////');
  100. })
  101. .then(function(foo) {
  102. assertEquals('/foo', foo.getFullPath());
  103. return foo.getDirectory('/');
  104. })
  105. .then(function(root) {
  106. assertEquals('/', root.getFullPath());
  107. return root.getDirectory('/////');
  108. })
  109. .then(function(root) { assertEquals('/', root.getFullPath()); });
  110. }
  111. function testCreateFile() {
  112. // Advance the clock to an arbitrary, known time.
  113. mockClock.install();
  114. mockClock.tick(43);
  115. var promise =
  116. dir.getLastModified()
  117. .then(function(date) { assertEquals(0, date.getTime()); })
  118. .then(function() { return dir.getFile('bar', Behavior.CREATE); })
  119. .then(function(file) {
  120. mockClock.tick();
  121. assertEquals('bar', file.getName());
  122. assertEquals('/foo/bar', file.getFullPath());
  123. assertEquals(dir, file.parent);
  124. assertTrue(file.isFile());
  125. return dir.getLastModified();
  126. })
  127. .then(function(date) {
  128. assertEquals(43, date.getTime());
  129. return dir.getMetadata();
  130. })
  131. .then(function(metadata) {
  132. assertEquals(43, metadata.modificationTime.getTime());
  133. })
  134. .thenAlways(function() { mockClock.uninstall(); });
  135. mockClock.tick();
  136. return promise;
  137. }
  138. function testCreateFileThatAlreadyExists() {
  139. mockClock.install();
  140. mockClock.tick(47);
  141. var existingFile = dir.getFileSync('file');
  142. var promise = dir.getFile('file', Behavior.CREATE)
  143. .then(function(file) {
  144. assertEquals('file', file.getName());
  145. assertEquals('/foo/file', file.getFullPath());
  146. assertEquals(dir, file.parent);
  147. assertEquals(existingFile, file);
  148. assertTrue(file.isFile());
  149. return dir.getLastModified();
  150. })
  151. .then(function(date) {
  152. assertEquals(47, date.getTime());
  153. return dir.getMetadata();
  154. })
  155. .then(function(metadata) {
  156. assertEquals(47, metadata.modificationTime.getTime());
  157. })
  158. .thenAlways(function() { mockClock.uninstall(); });
  159. mockClock.tick();
  160. return promise;
  161. }
  162. function testCreateFileInSubdir() {
  163. return dir.getFile('subdir/bar', Behavior.CREATE).then(function(file) {
  164. assertEquals('bar', file.getName());
  165. assertEquals('/foo/subdir/bar', file.getFullPath());
  166. assertEquals(dir.getDirectorySync('subdir'), file.parent);
  167. assertTrue(file.isFile());
  168. });
  169. }
  170. function testCreateFileExclusive() {
  171. return dir.getFile('bar', Behavior.CREATE_EXCLUSIVE).then(function(file) {
  172. assertEquals('bar', file.getName());
  173. assertEquals('/foo/bar', file.getFullPath());
  174. assertEquals(dir, file.parent);
  175. assertTrue(file.isFile());
  176. });
  177. }
  178. function testGetNonExistentFile() {
  179. return dir.getFile('bar').then(fail, function(e) {
  180. assertEquals(goog.fs.Error.ErrorCode.NOT_FOUND, e.code);
  181. });
  182. }
  183. function testGetNonExistentFileInSubdir() {
  184. return dir.getFile('subdir/bar').then(fail, function(e) {
  185. assertEquals(goog.fs.Error.ErrorCode.NOT_FOUND, e.code);
  186. });
  187. }
  188. function testGetFileInNonExistentSubdir() {
  189. return dir.getFile('bar/subfile').then(fail, function(e) {
  190. assertEquals(goog.fs.Error.ErrorCode.NOT_FOUND, e.code);
  191. });
  192. }
  193. function testGetFileThatsActuallyADirectory() {
  194. return dir.getFile('subdir').then(fail, function(e) {
  195. assertEquals(goog.fs.Error.ErrorCode.TYPE_MISMATCH, e.code);
  196. });
  197. }
  198. function testCreateFileInNonExistentSubdir() {
  199. return dir.getFile('bar/newfile', Behavior.CREATE).then(fail, function(e) {
  200. assertEquals(goog.fs.Error.ErrorCode.NOT_FOUND, e.code);
  201. });
  202. }
  203. function testCreateFileThatsActuallyADirectory() {
  204. return dir.getFile('subdir', Behavior.CREATE).then(fail, function(e) {
  205. assertEquals(goog.fs.Error.ErrorCode.TYPE_MISMATCH, e.code);
  206. });
  207. }
  208. function testCreateExclusiveExistingFile() {
  209. return dir.getFile('file', Behavior.CREATE_EXCLUSIVE).then(fail, function(e) {
  210. assertEquals(goog.fs.Error.ErrorCode.INVALID_MODIFICATION, e.code);
  211. });
  212. }
  213. function testListEmptyDirectory() {
  214. var emptyDir = fs.getRoot().getDirectorySync('empty', Behavior.CREATE);
  215. return emptyDir.listDirectory().then(function(entryList) {
  216. assertSameElements([], entryList);
  217. });
  218. }
  219. function testListDirectory() {
  220. var root = fs.getRoot();
  221. root.getDirectorySync('dir1', Behavior.CREATE);
  222. root.getDirectorySync('dir2', Behavior.CREATE);
  223. root.getFileSync('file1', Behavior.CREATE);
  224. root.getFileSync('file2', Behavior.CREATE);
  225. return fs.getRoot().listDirectory().then(function(entryList) {
  226. assertSameElements(
  227. ['dir1', 'dir2', 'file1', 'file2', 'foo'],
  228. goog.array.map(entryList, function(entry) { return entry.getName(); }));
  229. });
  230. }
  231. function testCreatePath() {
  232. return dir.createPath('baz/bat')
  233. .then(function(batDir) {
  234. assertEquals('/foo/baz/bat', batDir.getFullPath());
  235. return batDir.createPath('../zazzle');
  236. })
  237. .then(function(zazzleDir) {
  238. assertEquals('/foo/baz/zazzle', zazzleDir.getFullPath());
  239. return zazzleDir.createPath('/elements/actinides/neptunium/');
  240. })
  241. .then(function(elDir) {
  242. assertEquals('/elements/actinides/neptunium', elDir.getFullPath());
  243. });
  244. }