integration_test.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.integrationTest');
  15. goog.setTestOnly('goog.testing.fs.integrationTest');
  16. goog.require('goog.Promise');
  17. goog.require('goog.events');
  18. goog.require('goog.fs');
  19. goog.require('goog.fs.DirectoryEntry');
  20. goog.require('goog.fs.Error');
  21. goog.require('goog.fs.FileSaver');
  22. goog.require('goog.testing.PropertyReplacer');
  23. goog.require('goog.testing.fs');
  24. goog.require('goog.testing.jsunit');
  25. var TEST_DIR = 'goog-fs-test-dir';
  26. var Behavior = goog.fs.DirectoryEntry.Behavior;
  27. var EventType = goog.fs.FileSaver.EventType;
  28. var ReadyState = goog.fs.FileSaver.ReadyState;
  29. var deferredFs = goog.testing.fs.getTemporary();
  30. function setUpPage() {
  31. goog.testing.fs.install(new goog.testing.PropertyReplacer());
  32. }
  33. function tearDown() {
  34. return loadTestDir().then(function(dir) { return dir.removeRecursively(); });
  35. }
  36. function testWriteFile() {
  37. return loadFile('test', Behavior.CREATE)
  38. .then(goog.partial(writeToFile, 'test content'))
  39. .then(goog.partial(checkFileContent, 'test content'));
  40. }
  41. function testRemoveFile() {
  42. return loadFile('test', Behavior.CREATE)
  43. .then(goog.partial(writeToFile, 'test content'))
  44. .then(function(fileEntry) { return fileEntry.remove(); })
  45. .then(goog.partial(checkFileRemoved, 'test'));
  46. }
  47. function testMoveFile() {
  48. var subdir = loadDirectory('subdir', Behavior.CREATE);
  49. var writtenFile = loadFile('test', Behavior.CREATE)
  50. .then(goog.partial(writeToFile, 'test content'));
  51. return goog.Promise.all([subdir, writtenFile])
  52. .then(function(results) {
  53. var dir = results[0];
  54. var fileEntry = results[1];
  55. return fileEntry.moveTo(dir);
  56. })
  57. .then(goog.partial(checkFileContent, 'test content'))
  58. .then(goog.partial(checkFileRemoved, 'test'));
  59. }
  60. function testCopyFile() {
  61. var file = loadFile('test', Behavior.CREATE);
  62. var subdir = loadDirectory('subdir', Behavior.CREATE);
  63. var writtenFile = file.then(goog.partial(writeToFile, 'test content'));
  64. return goog.Promise.all([subdir, writtenFile])
  65. .then(function(results) {
  66. var dir = results[0];
  67. var fileEntry = results[1];
  68. return fileEntry.copyTo(dir);
  69. })
  70. .then(goog.partial(checkFileContent, 'test content'))
  71. .then(function() { return file; })
  72. .then(goog.partial(checkFileContent, 'test content'));
  73. }
  74. function testAbortWrite() {
  75. var file = loadFile('test', Behavior.CREATE);
  76. file.then(goog.partial(startWrite, 'test content'))
  77. .then(function(writer) {
  78. writer.abort();
  79. return writer;
  80. })
  81. .then(goog.partial(waitForEvent, EventType.ABORT));
  82. return file.then(goog.partial(checkFileContent, ''));
  83. }
  84. function testSeek() {
  85. var file = loadFile('test', Behavior.CREATE);
  86. return file.then(goog.partial(writeToFile, 'test content'))
  87. .then(function(fileEntry) { return fileEntry.createWriter(); })
  88. .then(goog.partial(checkReadyState, ReadyState.INIT))
  89. .then(function(writer) {
  90. writer.seek(5);
  91. writer.write(goog.fs.getBlob('stuff and things'));
  92. return writer;
  93. })
  94. .then(goog.partial(checkReadyState, ReadyState.WRITING))
  95. .then(goog.partial(waitForEvent, EventType.WRITE))
  96. .then(function() { return file; })
  97. .then(goog.partial(checkFileContent, 'test stuff and things'));
  98. }
  99. function testTruncate() {
  100. var file = loadFile('test', Behavior.CREATE);
  101. return file.then(goog.partial(writeToFile, 'test content'))
  102. .then(function(fileEntry) { return fileEntry.createWriter(); })
  103. .then(goog.partial(checkReadyState, ReadyState.INIT))
  104. .then(function(writer) {
  105. writer.truncate(4);
  106. return writer;
  107. })
  108. .then(goog.partial(checkReadyState, ReadyState.WRITING))
  109. .then(goog.partial(waitForEvent, EventType.WRITE))
  110. .then(function() { return file; })
  111. .then(goog.partial(checkFileContent, 'test'));
  112. }
  113. function loadTestDir() {
  114. return deferredFs.then(function(fs) {
  115. return fs.getRoot().getDirectory(TEST_DIR, Behavior.CREATE);
  116. });
  117. }
  118. function loadFile(filename, behavior) {
  119. return loadTestDir().then(function(dir) {
  120. return dir.getFile(filename, behavior);
  121. });
  122. }
  123. function loadDirectory(filename, behavior) {
  124. return loadTestDir().then(function(dir) {
  125. return dir.getDirectory(filename, behavior);
  126. });
  127. }
  128. function startWrite(content, fileEntry) {
  129. return fileEntry.createWriter()
  130. .then(goog.partial(checkReadyState, ReadyState.INIT))
  131. .then(function(writer) {
  132. writer.write(goog.fs.getBlob(content));
  133. return writer;
  134. })
  135. .then(goog.partial(checkReadyState, ReadyState.WRITING));
  136. }
  137. function waitForEvent(type, target) {
  138. return new goog.Promise(function(resolve, reject) {
  139. goog.events.listenOnce(target, type, resolve);
  140. });
  141. }
  142. function writeToFile(content, fileEntry) {
  143. return startWrite(content, fileEntry)
  144. .then(goog.partial(waitForEvent, EventType.WRITE))
  145. .then(function() { return fileEntry; });
  146. }
  147. function checkFileContent(content, fileEntry) {
  148. return fileEntry.file()
  149. .then(function(blob) { return goog.fs.blobToString(blob); })
  150. .then(goog.partial(assertEquals, content));
  151. }
  152. function checkFileRemoved(filename) {
  153. return loadFile(filename)
  154. .then(goog.partial(fail, 'expected file to be removed'))
  155. .thenCatch(function(err) {
  156. assertEquals(err.code, goog.fs.Error.ErrorCode.NOT_FOUND);
  157. return true; // Go back to the non-rejected path.
  158. });
  159. }
  160. function checkReadyState(expectedState, writer) {
  161. assertEquals(expectedState, writer.getReadyState());
  162. return writer;
  163. }