filereader.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. * @fileoverview Mock FileReader object.
  16. *
  17. */
  18. goog.setTestOnly('goog.testing.fs.FileReader');
  19. goog.provide('goog.testing.fs.FileReader');
  20. goog.require('goog.Timer');
  21. goog.require('goog.events.EventTarget');
  22. goog.require('goog.fs.Error');
  23. goog.require('goog.fs.FileReader');
  24. goog.require('goog.testing.fs.ProgressEvent');
  25. /**
  26. * A mock FileReader object. This emits the same events as
  27. * {@link goog.fs.FileReader}.
  28. *
  29. * @constructor
  30. * @extends {goog.events.EventTarget}
  31. */
  32. goog.testing.fs.FileReader = function() {
  33. goog.testing.fs.FileReader.base(this, 'constructor');
  34. /**
  35. * The current state of the reader.
  36. * @type {goog.fs.FileReader.ReadyState}
  37. * @private
  38. */
  39. this.readyState_ = goog.fs.FileReader.ReadyState.INIT;
  40. };
  41. goog.inherits(goog.testing.fs.FileReader, goog.events.EventTarget);
  42. /**
  43. * The most recent error experienced by this reader.
  44. * @type {goog.fs.Error}
  45. * @private
  46. */
  47. goog.testing.fs.FileReader.prototype.error_;
  48. /**
  49. * Whether the current operation has been aborted.
  50. * @type {boolean}
  51. * @private
  52. */
  53. goog.testing.fs.FileReader.prototype.aborted_ = false;
  54. /**
  55. * The blob this reader is reading from.
  56. * @type {goog.testing.fs.Blob}
  57. * @private
  58. */
  59. goog.testing.fs.FileReader.prototype.blob_;
  60. /**
  61. * The possible return types.
  62. * @enum {number}
  63. */
  64. goog.testing.fs.FileReader.ReturnType = {
  65. /**
  66. * Used when reading as text.
  67. */
  68. TEXT: 1,
  69. /**
  70. * Used when reading as binary string.
  71. */
  72. BINARY_STRING: 2,
  73. /**
  74. * Used when reading as array buffer.
  75. */
  76. ARRAY_BUFFER: 3,
  77. /**
  78. * Used when reading as data URL.
  79. */
  80. DATA_URL: 4
  81. };
  82. /**
  83. * The return type we're reading.
  84. * @type {goog.testing.fs.FileReader.ReturnType}
  85. * @private
  86. */
  87. goog.testing.fs.FileReader.returnType_;
  88. /**
  89. * @see {goog.fs.FileReader#getReadyState}
  90. * @return {goog.fs.FileReader.ReadyState} The current ready state.
  91. */
  92. goog.testing.fs.FileReader.prototype.getReadyState = function() {
  93. return this.readyState_;
  94. };
  95. /**
  96. * @see {goog.fs.FileReader#getError}
  97. * @return {goog.fs.Error} The current error.
  98. */
  99. goog.testing.fs.FileReader.prototype.getError = function() {
  100. return this.error_;
  101. };
  102. /**
  103. * @see {goog.fs.FileReader#abort}
  104. */
  105. goog.testing.fs.FileReader.prototype.abort = function() {
  106. if (this.readyState_ != goog.fs.FileReader.ReadyState.LOADING) {
  107. var msg = 'aborting read';
  108. throw new goog.fs.Error({'name': 'InvalidStateError'}, msg);
  109. }
  110. this.aborted_ = true;
  111. };
  112. /**
  113. * @see {goog.fs.FileReader#getResult}
  114. * @return {*} The result of the file read.
  115. */
  116. goog.testing.fs.FileReader.prototype.getResult = function() {
  117. if (this.readyState_ != goog.fs.FileReader.ReadyState.DONE) {
  118. return undefined;
  119. }
  120. if (this.error_) {
  121. return undefined;
  122. }
  123. if (this.returnType_ == goog.testing.fs.FileReader.ReturnType.TEXT) {
  124. return this.blob_.toString();
  125. } else if (
  126. this.returnType_ == goog.testing.fs.FileReader.ReturnType.ARRAY_BUFFER) {
  127. return this.blob_.toArrayBuffer();
  128. } else if (
  129. this.returnType_ == goog.testing.fs.FileReader.ReturnType.BINARY_STRING) {
  130. return this.blob_.toString();
  131. } else if (
  132. this.returnType_ == goog.testing.fs.FileReader.ReturnType.DATA_URL) {
  133. return this.blob_.toDataUrl();
  134. } else {
  135. return undefined;
  136. }
  137. };
  138. /**
  139. * Fires the read events.
  140. * @param {!goog.testing.fs.Blob} blob The blob to read from.
  141. * @private
  142. */
  143. goog.testing.fs.FileReader.prototype.read_ = function(blob) {
  144. this.blob_ = blob;
  145. if (this.readyState_ == goog.fs.FileReader.ReadyState.LOADING) {
  146. var msg = 'reading file';
  147. throw new goog.fs.Error({'name': 'InvalidStateError'}, msg);
  148. }
  149. this.readyState_ = goog.fs.FileReader.ReadyState.LOADING;
  150. goog.Timer.callOnce(function() {
  151. if (this.aborted_) {
  152. this.abort_(blob.size);
  153. return;
  154. }
  155. this.progressEvent_(goog.fs.FileReader.EventType.LOAD_START, 0, blob.size);
  156. this.progressEvent_(
  157. goog.fs.FileReader.EventType.LOAD, blob.size / 2, blob.size);
  158. this.progressEvent_(
  159. goog.fs.FileReader.EventType.LOAD, blob.size, blob.size);
  160. this.readyState_ = goog.fs.FileReader.ReadyState.DONE;
  161. this.progressEvent_(
  162. goog.fs.FileReader.EventType.LOAD, blob.size, blob.size);
  163. this.progressEvent_(
  164. goog.fs.FileReader.EventType.LOAD_END, blob.size, blob.size);
  165. }, 0, this);
  166. };
  167. /**
  168. * @see {goog.fs.FileReader#readAsBinaryString}
  169. * @param {!goog.testing.fs.Blob} blob The blob to read.
  170. */
  171. goog.testing.fs.FileReader.prototype.readAsBinaryString = function(blob) {
  172. this.returnType_ = goog.testing.fs.FileReader.ReturnType.BINARY_STRING;
  173. this.read_(blob);
  174. };
  175. /**
  176. * @see {goog.fs.FileReader#readAsArrayBuffer}
  177. * @param {!goog.testing.fs.Blob} blob The blob to read.
  178. */
  179. goog.testing.fs.FileReader.prototype.readAsArrayBuffer = function(blob) {
  180. this.returnType_ = goog.testing.fs.FileReader.ReturnType.ARRAY_BUFFER;
  181. this.read_(blob);
  182. };
  183. /**
  184. * @see {goog.fs.FileReader#readAsText}
  185. * @param {!goog.testing.fs.Blob} blob The blob to read.
  186. * @param {string=} opt_encoding The name of the encoding to use.
  187. */
  188. goog.testing.fs.FileReader.prototype.readAsText = function(blob, opt_encoding) {
  189. this.returnType_ = goog.testing.fs.FileReader.ReturnType.TEXT;
  190. this.read_(blob);
  191. };
  192. /**
  193. * @see {goog.fs.FileReader#readAsDataUrl}
  194. * @param {!goog.testing.fs.Blob} blob The blob to read.
  195. */
  196. goog.testing.fs.FileReader.prototype.readAsDataUrl = function(blob) {
  197. this.returnType_ = goog.testing.fs.FileReader.ReturnType.DATA_URL;
  198. this.read_(blob);
  199. };
  200. /**
  201. * Abort the current action and emit appropriate events.
  202. *
  203. * @param {number} total The total data that was to be processed, in bytes.
  204. * @private
  205. */
  206. goog.testing.fs.FileReader.prototype.abort_ = function(total) {
  207. this.error_ = new goog.fs.Error({'name': 'AbortError'}, 'reading file');
  208. this.progressEvent_(goog.fs.FileReader.EventType.ERROR, 0, total);
  209. this.progressEvent_(goog.fs.FileReader.EventType.ABORT, 0, total);
  210. this.readyState_ = goog.fs.FileReader.ReadyState.DONE;
  211. this.progressEvent_(goog.fs.FileReader.EventType.LOAD_END, 0, total);
  212. this.aborted_ = false;
  213. };
  214. /**
  215. * Dispatch a progress event.
  216. *
  217. * @param {goog.fs.FileReader.EventType} type The event type.
  218. * @param {number} loaded The number of bytes processed.
  219. * @param {number} total The total data that was to be processed, in bytes.
  220. * @private
  221. */
  222. goog.testing.fs.FileReader.prototype.progressEvent_ = function(
  223. type, loaded, total) {
  224. this.dispatchEvent(new goog.testing.fs.ProgressEvent(type, loaded, total));
  225. };