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