filereader_test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.FileReaderTest');
  15. goog.setTestOnly('goog.testing.fs.FileReaderTest');
  16. goog.require('goog.Promise');
  17. goog.require('goog.array');
  18. goog.require('goog.events');
  19. goog.require('goog.fs.Error');
  20. goog.require('goog.fs.FileReader');
  21. goog.require('goog.object');
  22. goog.require('goog.testing.events.EventObserver');
  23. goog.require('goog.testing.fs.FileReader');
  24. goog.require('goog.testing.fs.FileSystem');
  25. goog.require('goog.testing.jsunit');
  26. var EventType = goog.fs.FileReader.EventType;
  27. var ReadyState = goog.fs.FileReader.ReadyState;
  28. /** @type {!goog.testing.fs.File} */
  29. var file;
  30. /** @type {!goog.testing.fs.FileReader} */
  31. var reader;
  32. /** @type {!goog.testing.events.EventObserver} */
  33. var observer;
  34. /** @const */
  35. var hasArrayBuffer = goog.isDef(goog.global.ArrayBuffer);
  36. function setUp() {
  37. var observedEvents = [];
  38. var fs = new goog.testing.fs.FileSystem();
  39. var fileEntry = fs.getRoot().createDirectorySync('foo').createFileSync('bar');
  40. file = fileEntry.fileSync();
  41. file.setDataInternal('test content');
  42. reader = new goog.testing.fs.FileReader();
  43. // Observe all file events fired by the FileReader.
  44. observer = new goog.testing.events.EventObserver();
  45. goog.events.listen(reader, goog.object.getValues(EventType), observer);
  46. }
  47. function tearDown() {
  48. goog.dispose(reader);
  49. }
  50. function testRead() {
  51. assertEquals(ReadyState.INIT, reader.getReadyState());
  52. assertUndefined(reader.getResult());
  53. return new goog
  54. .Promise(function(resolve, reject) {
  55. goog.events.listen(reader, EventType.LOAD_END, resolve);
  56. reader.readAsText(file);
  57. assertEquals(ReadyState.LOADING, reader.getReadyState());
  58. })
  59. .then(function(result) {
  60. assertEquals(file.toString(), reader.getResult());
  61. assertEquals(ReadyState.DONE, reader.getReadyState());
  62. assertArrayEquals(
  63. [
  64. EventType.LOAD_START, EventType.LOAD, EventType.LOAD,
  65. EventType.LOAD, EventType.LOAD_END
  66. ],
  67. goog.array.map(
  68. observer.getEvents(), function(e) { return e.type; }));
  69. });
  70. }
  71. function testReadAsArrayBuffer() {
  72. if (!hasArrayBuffer) {
  73. // Skip if array buffer is not supported
  74. return;
  75. }
  76. return new goog
  77. .Promise(function(resolve, reject) {
  78. goog.events.listen(reader, EventType.LOAD_END, resolve);
  79. reader.readAsArrayBuffer(file);
  80. assertEquals(ReadyState.LOADING, reader.getReadyState());
  81. })
  82. .then(function(result) {
  83. assertElementsEquals(file.toArrayBuffer(), reader.getResult());
  84. assertEquals(ReadyState.DONE, reader.getReadyState());
  85. assertArrayEquals(
  86. [
  87. EventType.LOAD_START, EventType.LOAD, EventType.LOAD,
  88. EventType.LOAD, EventType.LOAD_END
  89. ],
  90. goog.array.map(
  91. observer.getEvents(), function(e) { return e.type; }));
  92. });
  93. }
  94. function testReadAsDataUrl() {
  95. return new goog
  96. .Promise(function(resolve, reject) {
  97. goog.events.listen(reader, EventType.LOAD_END, resolve);
  98. reader.readAsDataUrl(file);
  99. assertEquals(ReadyState.LOADING, reader.getReadyState());
  100. })
  101. .then(function(result) {
  102. assertEquals(file.toDataUrl(), reader.getResult());
  103. assertEquals(ReadyState.DONE, reader.getReadyState());
  104. assertArrayEquals(
  105. [
  106. EventType.LOAD_START, EventType.LOAD, EventType.LOAD,
  107. EventType.LOAD, EventType.LOAD_END
  108. ],
  109. goog.array.map(
  110. observer.getEvents(), function(e) { return e.type; }));
  111. });
  112. }
  113. function testAbort() {
  114. return new goog
  115. .Promise(function(resolve, reject) {
  116. goog.events.listen(reader, EventType.LOAD_END, resolve);
  117. reader.readAsText(file);
  118. assertEquals(ReadyState.LOADING, reader.getReadyState());
  119. reader.abort();
  120. })
  121. .then(function(result) {
  122. assertUndefined(reader.getResult());
  123. assertEquals(ReadyState.DONE, reader.getReadyState());
  124. assertArrayEquals(
  125. [EventType.ERROR, EventType.ABORT, EventType.LOAD_END],
  126. goog.array.map(
  127. observer.getEvents(), function(e) { return e.type; }));
  128. });
  129. }
  130. function testAbortBeforeRead() {
  131. var err = assertThrows(function() { reader.abort(); });
  132. assertEquals(goog.fs.Error.ErrorCode.INVALID_STATE, err.code);
  133. }
  134. function testReadDuringRead() {
  135. var err = assertThrows(function() {
  136. reader.readAsText(file);
  137. reader.readAsText(file);
  138. });
  139. assertEquals(goog.fs.Error.ErrorCode.INVALID_STATE, err.code);
  140. }