| 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 FileWriter object. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
goog.setTestOnly('goog.testing.fs.FileWriter'); |
| 21 |
goog.provide('goog.testing.fs.FileWriter'); |
| 22 |
|
| 23 |
goog.require('goog.Timer'); |
| 24 |
goog.require('goog.events.EventTarget'); |
| 25 |
goog.require('goog.fs.Error'); |
| 26 |
goog.require('goog.fs.FileSaver'); |
| 27 |
goog.require('goog.string'); |
| 28 |
goog.require('goog.testing.fs.ProgressEvent'); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* A mock FileWriter object. This emits the same events as |
| 34 |
* {@link goog.fs.FileSaver} and {@link goog.fs.FileWriter}. |
| 35 |
* |
| 36 |
* @param {!goog.testing.fs.FileEntry} fileEntry The file entry to write to. |
| 37 |
* @constructor |
| 38 |
* @extends {goog.events.EventTarget} |
| 39 |
* @final |
| 40 |
*/ |
| 41 |
goog.testing.fs.FileWriter = function(fileEntry) { |
| 42 |
goog.testing.fs.FileWriter.base(this, 'constructor'); |
| 43 |
|
| 44 |
/** |
| 45 |
* The file entry to which to write. |
| 46 |
* @type {!goog.testing.fs.FileEntry} |
| 47 |
* @private |
| 48 |
*/ |
| 49 |
this.fileEntry_ = fileEntry; |
| 50 |
|
| 51 |
/** |
| 52 |
* The file blob to write to. |
| 53 |
* @type {!goog.testing.fs.File} |
| 54 |
* @private |
| 55 |
*/ |
| 56 |
this.file_ = fileEntry.fileSync(); |
| 57 |
|
| 58 |
/** |
| 59 |
* The current state of the writer. |
| 60 |
* @type {goog.fs.FileSaver.ReadyState} |
| 61 |
* @private |
| 62 |
*/ |
| 63 |
this.readyState_ = goog.fs.FileSaver.ReadyState.INIT; |
| 64 |
}; |
| 65 |
goog.inherits(goog.testing.fs.FileWriter, goog.events.EventTarget); |
| 66 |
|
| 67 |
|
| 68 |
/** |
| 69 |
* The most recent error experienced by this writer. |
| 70 |
* @type {goog.fs.Error} |
| 71 |
* @private |
| 72 |
*/ |
| 73 |
goog.testing.fs.FileWriter.prototype.error_; |
| 74 |
|
| 75 |
|
| 76 |
/** |
| 77 |
* Whether the current operation has been aborted. |
| 78 |
* @type {boolean} |
| 79 |
* @private |
| 80 |
*/ |
| 81 |
goog.testing.fs.FileWriter.prototype.aborted_ = false; |
| 82 |
|
| 83 |
|
| 84 |
/** |
| 85 |
* The current position in the file. |
| 86 |
* @type {number} |
| 87 |
* @private |
| 88 |
*/ |
| 89 |
goog.testing.fs.FileWriter.prototype.position_ = 0; |
| 90 |
|
| 91 |
|
| 92 |
/** |
| 93 |
* @see {goog.fs.FileSaver#getReadyState} |
| 94 |
* @return {goog.fs.FileSaver.ReadyState} The ready state. |
| 95 |
*/ |
| 96 |
goog.testing.fs.FileWriter.prototype.getReadyState = function() { |
| 97 |
return this.readyState_; |
| 98 |
}; |
| 99 |
|
| 100 |
|
| 101 |
/** |
| 102 |
* @see {goog.fs.FileSaver#getError} |
| 103 |
* @return {goog.fs.Error} The error. |
| 104 |
*/ |
| 105 |
goog.testing.fs.FileWriter.prototype.getError = function() { |
| 106 |
return this.error_; |
| 107 |
}; |
| 108 |
|
| 109 |
|
| 110 |
/** |
| 111 |
* @see {goog.fs.FileWriter#getPosition} |
| 112 |
* @return {number} The position. |
| 113 |
*/ |
| 114 |
goog.testing.fs.FileWriter.prototype.getPosition = function() { |
| 115 |
return this.position_; |
| 116 |
}; |
| 117 |
|
| 118 |
|
| 119 |
/** |
| 120 |
* @see {goog.fs.FileWriter#getLength} |
| 121 |
* @return {number} The length. |
| 122 |
*/ |
| 123 |
goog.testing.fs.FileWriter.prototype.getLength = function() { |
| 124 |
return this.file_.size; |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
/** |
| 129 |
* @see {goog.fs.FileSaver#abort} |
| 130 |
*/ |
| 131 |
goog.testing.fs.FileWriter.prototype.abort = function() { |
| 132 |
if (this.readyState_ != goog.fs.FileSaver.ReadyState.WRITING) { |
| 133 |
var msg = 'aborting save of ' + this.fileEntry_.getFullPath(); |
| 134 |
throw new goog.fs.Error({'name': 'InvalidStateError'}, msg); |
| 135 |
} |
| 136 |
|
| 137 |
this.aborted_ = true; |
| 138 |
}; |
| 139 |
|
| 140 |
|
| 141 |
/** |
| 142 |
* @see {goog.fs.FileWriter#write} |
| 143 |
* @param {!goog.testing.fs.Blob} blob The blob to write. |
| 144 |
*/ |
| 145 |
goog.testing.fs.FileWriter.prototype.write = function(blob) { |
| 146 |
if (this.readyState_ == goog.fs.FileSaver.ReadyState.WRITING) { |
| 147 |
var msg = 'writing to ' + this.fileEntry_.getFullPath(); |
| 148 |
throw new goog.fs.Error({'name': 'InvalidStateError'}, msg); |
| 149 |
} |
| 150 |
|
| 151 |
this.readyState_ = goog.fs.FileSaver.ReadyState.WRITING; |
| 152 |
goog.Timer.callOnce(function() { |
| 153 |
if (this.aborted_) { |
| 154 |
this.abort_(blob.size); |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
this.progressEvent_(goog.fs.FileSaver.EventType.WRITE_START, 0, blob.size); |
| 159 |
var fileString = this.file_.toString(); |
| 160 |
this.file_.setDataInternal( |
| 161 |
fileString.substring(0, this.position_) + blob.toString() + |
| 162 |
fileString.substring(this.position_ + blob.size, fileString.length)); |
| 163 |
this.position_ += blob.size; |
| 164 |
|
| 165 |
this.progressEvent_( |
| 166 |
goog.fs.FileSaver.EventType.WRITE, blob.size, blob.size); |
| 167 |
this.readyState_ = goog.fs.FileSaver.ReadyState.DONE; |
| 168 |
this.progressEvent_( |
| 169 |
goog.fs.FileSaver.EventType.WRITE_END, blob.size, blob.size); |
| 170 |
}, 0, this); |
| 171 |
}; |
| 172 |
|
| 173 |
|
| 174 |
/** |
| 175 |
* @see {goog.fs.FileWriter#truncate} |
| 176 |
* @param {number} size The size to truncate to. |
| 177 |
*/ |
| 178 |
goog.testing.fs.FileWriter.prototype.truncate = function(size) { |
| 179 |
if (this.readyState_ == goog.fs.FileSaver.ReadyState.WRITING) { |
| 180 |
var msg = 'truncating ' + this.fileEntry_.getFullPath(); |
| 181 |
throw new goog.fs.Error({'name': 'InvalidStateError'}, msg); |
| 182 |
} |
| 183 |
|
| 184 |
this.readyState_ = goog.fs.FileSaver.ReadyState.WRITING; |
| 185 |
goog.Timer.callOnce(function() { |
| 186 |
if (this.aborted_) { |
| 187 |
this.abort_(size); |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
this.progressEvent_(goog.fs.FileSaver.EventType.WRITE_START, 0, size); |
| 192 |
|
| 193 |
var fileString = this.file_.toString(); |
| 194 |
if (size > fileString.length) { |
| 195 |
this.file_.setDataInternal( |
| 196 |
fileString + goog.string.repeat('\0', size - fileString.length)); |
| 197 |
} else { |
| 198 |
this.file_.setDataInternal(fileString.substring(0, size)); |
| 199 |
} |
| 200 |
this.position_ = Math.min(this.position_, size); |
| 201 |
|
| 202 |
this.progressEvent_(goog.fs.FileSaver.EventType.WRITE, size, size); |
| 203 |
this.readyState_ = goog.fs.FileSaver.ReadyState.DONE; |
| 204 |
this.progressEvent_(goog.fs.FileSaver.EventType.WRITE_END, size, size); |
| 205 |
}, 0, this); |
| 206 |
}; |
| 207 |
|
| 208 |
|
| 209 |
/** |
| 210 |
* @see {goog.fs.FileWriter#seek} |
| 211 |
* @param {number} offset The offset to seek to. |
| 212 |
*/ |
| 213 |
goog.testing.fs.FileWriter.prototype.seek = function(offset) { |
| 214 |
if (this.readyState_ == goog.fs.FileSaver.ReadyState.WRITING) { |
| 215 |
var msg = 'truncating ' + this.fileEntry_.getFullPath(); |
| 216 |
throw new goog.fs.Error({name: 'InvalidStateError'}, msg); |
| 217 |
} |
| 218 |
|
| 219 |
if (offset < 0) { |
| 220 |
this.position_ = Math.max(0, this.file_.size + offset); |
| 221 |
} else { |
| 222 |
this.position_ = Math.min(offset, this.file_.size); |
| 223 |
} |
| 224 |
}; |
| 225 |
|
| 226 |
|
| 227 |
/** |
| 228 |
* Abort the current action and emit appropriate events. |
| 229 |
* |
| 230 |
* @param {number} total The total data that was to be processed, in bytes. |
| 231 |
* @private |
| 232 |
*/ |
| 233 |
goog.testing.fs.FileWriter.prototype.abort_ = function(total) { |
| 234 |
this.error_ = new goog.fs.Error( |
| 235 |
{'name': 'AbortError'}, 'saving ' + this.fileEntry_.getFullPath()); |
| 236 |
this.progressEvent_(goog.fs.FileSaver.EventType.ERROR, 0, total); |
| 237 |
this.progressEvent_(goog.fs.FileSaver.EventType.ABORT, 0, total); |
| 238 |
this.readyState_ = goog.fs.FileSaver.ReadyState.DONE; |
| 239 |
this.progressEvent_(goog.fs.FileSaver.EventType.WRITE_END, 0, total); |
| 240 |
this.aborted_ = false; |
| 241 |
}; |
| 242 |
|
| 243 |
|
| 244 |
/** |
| 245 |
* Dispatch a progress event. |
| 246 |
* |
| 247 |
* @param {goog.fs.FileSaver.EventType} type The type of the event. |
| 248 |
* @param {number} loaded The number of bytes processed. |
| 249 |
* @param {number} total The total data that was to be processed, in bytes. |
| 250 |
* @private |
| 251 |
*/ |
| 252 |
goog.testing.fs.FileWriter.prototype.progressEvent_ = function( |
| 253 |
type, loaded, total) { |
| 254 |
// On write, update the last modified date to the current (real or mock) time. |
| 255 |
if (type == goog.fs.FileSaver.EventType.WRITE) { |
| 256 |
this.file_.lastModifiedDate = new Date(goog.now()); |
| 257 |
} |
| 258 |
|
| 259 |
this.dispatchEvent(new goog.testing.fs.ProgressEvent(type, loaded, total)); |
| 260 |
}; |