fs.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 implementations of the Closure HTML5 FileSystem wrapper
  16. * classes. These implementations are designed to be usable in any browser, so
  17. * they use none of the native FileSystem-related objects.
  18. *
  19. */
  20. goog.setTestOnly('goog.testing.fs');
  21. goog.provide('goog.testing.fs');
  22. goog.require('goog.Timer');
  23. goog.require('goog.array');
  24. goog.require('goog.async.Deferred');
  25. /** @suppress {extraRequire} */
  26. goog.require('goog.fs');
  27. goog.require('goog.testing.fs.Blob');
  28. goog.require('goog.testing.fs.FileSystem');
  29. /**
  30. * Get a filesystem object. Since these are mocks, there's no difference between
  31. * temporary and persistent filesystems.
  32. *
  33. * @param {number} size Ignored.
  34. * @return {!goog.async.Deferred} The deferred
  35. * {@link goog.testing.fs.FileSystem}.
  36. */
  37. goog.testing.fs.getTemporary = function(size) {
  38. var d = new goog.async.Deferred();
  39. goog.Timer.callOnce(
  40. goog.bind(d.callback, d, new goog.testing.fs.FileSystem()));
  41. return d;
  42. };
  43. /**
  44. * Get a filesystem object. Since these are mocks, there's no difference between
  45. * temporary and persistent filesystems.
  46. *
  47. * @param {number} size Ignored.
  48. * @return {!goog.async.Deferred} The deferred
  49. * {@link goog.testing.fs.FileSystem}.
  50. */
  51. goog.testing.fs.getPersistent = function(size) {
  52. return goog.testing.fs.getTemporary(size);
  53. };
  54. /**
  55. * Which object URLs have been granted for fake blobs.
  56. * @type {!Object<boolean>}
  57. * @private
  58. */
  59. goog.testing.fs.objectUrls_ = {};
  60. /**
  61. * Create a fake object URL for a given fake blob. This can be used as a real
  62. * URL, and it can be created and revoked normally.
  63. *
  64. * @param {!goog.testing.fs.Blob} blob The blob for which to create the URL.
  65. * @return {string} The URL.
  66. */
  67. goog.testing.fs.createObjectUrl = function(blob) {
  68. var url = blob.toDataUrl();
  69. goog.testing.fs.objectUrls_[url] = true;
  70. return url;
  71. };
  72. /**
  73. * Remove a URL that was created for a fake blob.
  74. *
  75. * @param {string} url The URL to revoke.
  76. */
  77. goog.testing.fs.revokeObjectUrl = function(url) {
  78. delete goog.testing.fs.objectUrls_[url];
  79. };
  80. /**
  81. * Return whether or not a URL has been granted for the given blob.
  82. *
  83. * @param {!goog.testing.fs.Blob} blob The blob to check.
  84. * @return {boolean} Whether a URL has been granted.
  85. */
  86. goog.testing.fs.isObjectUrlGranted = function(blob) {
  87. return (blob.toDataUrl()) in goog.testing.fs.objectUrls_;
  88. };
  89. /**
  90. * Concatenates one or more values together and converts them to a fake blob.
  91. *
  92. * @param {...(string|!goog.testing.fs.Blob)} var_args The values that will make
  93. * up the resulting blob.
  94. * @return {!goog.testing.fs.Blob} The blob.
  95. */
  96. goog.testing.fs.getBlob = function(var_args) {
  97. return new goog.testing.fs.Blob(goog.array.map(arguments, String).join(''));
  98. };
  99. /**
  100. * Creates a blob with the given properties.
  101. * See https://developer.mozilla.org/en-US/docs/Web/API/Blob for more details.
  102. *
  103. * @param {Array<string|!goog.testing.fs.Blob>} parts
  104. * The values that will make up the resulting blob.
  105. * @param {string=} opt_type The MIME type of the Blob.
  106. * @param {string=} opt_endings Specifies how strings containing newlines are to
  107. * be written out.
  108. * @return {!goog.testing.fs.Blob} The blob.
  109. */
  110. goog.testing.fs.getBlobWithProperties = function(parts, opt_type, opt_endings) {
  111. return new goog.testing.fs.Blob(
  112. goog.array.map(parts, String).join(''), opt_type);
  113. };
  114. /**
  115. * Returns the string value of a fake blob.
  116. *
  117. * @param {!goog.testing.fs.Blob} blob The blob to convert to a string.
  118. * @param {string=} opt_encoding Ignored.
  119. * @return {!goog.async.Deferred} The deferred string value of the blob.
  120. */
  121. goog.testing.fs.blobToString = function(blob, opt_encoding) {
  122. var d = new goog.async.Deferred();
  123. goog.Timer.callOnce(goog.bind(d.callback, d, blob.toString()));
  124. return d;
  125. };
  126. /**
  127. * Slices the blob. The returned blob contains data from the start byte
  128. * (inclusive) till the end byte (exclusive). Negative indices can be used
  129. * to count bytes from the end of the blob (-1 == blob.size - 1). Indices
  130. * are always clamped to blob range. If end is omitted, all the data till
  131. * the end of the blob is taken.
  132. *
  133. * @param {!goog.testing.fs.Blob} testBlob The blob to slice.
  134. * @param {number} start Index of the starting byte.
  135. * @param {number=} opt_end Index of the ending byte.
  136. * @return {goog.testing.fs.Blob} The new blob or null if not supported.
  137. */
  138. goog.testing.fs.sliceBlob = function(testBlob, start, opt_end) {
  139. return testBlob.slice(start, opt_end);
  140. };
  141. /**
  142. * Installs goog.testing.fs in place of the standard goog.fs. After calling
  143. * this, code that uses goog.fs should work without issue using goog.testing.fs.
  144. *
  145. * @param {!goog.testing.PropertyReplacer} stubs The property replacer for
  146. * stubbing out the original goog.fs functions.
  147. */
  148. goog.testing.fs.install = function(stubs) {
  149. // Prevent warnings that goog.fs may get optimized away. It's true this is
  150. // unsafe in compiled code, but it's only meant for tests.
  151. var fs = goog.getObjectByName('goog.fs');
  152. stubs.replace(fs, 'getTemporary', goog.testing.fs.getTemporary);
  153. stubs.replace(fs, 'getPersistent', goog.testing.fs.getPersistent);
  154. stubs.replace(fs, 'createObjectUrl', goog.testing.fs.createObjectUrl);
  155. stubs.replace(fs, 'revokeObjectUrl', goog.testing.fs.revokeObjectUrl);
  156. stubs.replace(fs, 'getBlob', goog.testing.fs.getBlob);
  157. stubs.replace(
  158. fs, 'getBlobWithProperties', goog.testing.fs.getBlobWithProperties);
  159. stubs.replace(fs, 'blobToString', goog.testing.fs.blobToString);
  160. stubs.replace(fs, 'browserSupportsObjectUrls', function() { return true; });
  161. };