Debian Codegraph @

closure-library/closure/goog/testing/net/mockiframeio.js

raw
1 // Copyright 2007 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 of IframeIo for unit testing.
17 */
18
19 goog.provide('goog.testing.net.MockIFrameIo');
20 goog.require('goog.events.EventTarget');
21 goog.require('goog.net.ErrorCode');
22 goog.require('goog.net.EventType');
23 goog.require('goog.net.IframeIo');
24 goog.require('goog.testing.TestQueue');
25
26
27
28 /**
29 * Mock implementation of goog.net.IframeIo. This doesn't provide a mock
30 * implementation for all cases, but it's not too hard to add them as needed.
31 * @param {goog.testing.TestQueue} testQueue Test queue for inserting test
32 * events.
33 * @constructor
34 * @extends {goog.events.EventTarget}
35 * @final
36 */
37 goog.testing.net.MockIFrameIo = function(testQueue) {
38 goog.events.EventTarget.call(this);
39
40 /**
41 * Queue of events write to
42 * @type {goog.testing.TestQueue}
43 * @private
44 */
45 this.testQueue_ = testQueue;
46
47 };
48 goog.inherits(goog.testing.net.MockIFrameIo, goog.events.EventTarget);
49
50
51 /**
52 * Whether MockIFrameIo is active.
53 * @type {boolean}
54 * @private
55 */
56 goog.testing.net.MockIFrameIo.prototype.active_ = false;
57
58
59 /**
60 * Last content.
61 * @type {string}
62 * @private
63 */
64 goog.testing.net.MockIFrameIo.prototype.lastContent_ = '';
65
66
67 /**
68 * Last error code.
69 * @type {goog.net.ErrorCode}
70 * @private
71 */
72 goog.testing.net.MockIFrameIo.prototype.lastErrorCode_ =
73 goog.net.ErrorCode.NO_ERROR;
74
75
76 /**
77 * Last error message.
78 * @type {string}
79 * @private
80 */
81 goog.testing.net.MockIFrameIo.prototype.lastError_ = '';
82
83
84 /**
85 * Last custom error.
86 * @type {Object}
87 * @private
88 */
89 goog.testing.net.MockIFrameIo.prototype.lastCustomError_ = null;
90
91
92 /**
93 * Last URI.
94 * @type {goog.Uri}
95 * @private
96 */
97 goog.testing.net.MockIFrameIo.prototype.lastUri_ = null;
98
99
100 /** @private {Function} */
101 goog.testing.net.MockIFrameIo.prototype.errorChecker_;
102
103
104 /** @private {boolean} */
105 goog.testing.net.MockIFrameIo.prototype.success_;
106
107
108 /** @private {boolean} */
109 goog.testing.net.MockIFrameIo.prototype.complete_;
110
111
112 /**
113 * Simulates the iframe send.
114 *
115 * @param {goog.Uri|string} uri Uri of the request.
116 * @param {string=} opt_method Default is GET, POST uses a form to submit the
117 * request.
118 * @param {boolean=} opt_noCache Append a timestamp to the request to avoid
119 * caching.
120 * @param {Object|goog.structs.Map=} opt_data Map of key-value pairs.
121 */
122 goog.testing.net.MockIFrameIo.prototype.send = function(
123 uri, opt_method, opt_noCache, opt_data) {
124 if (this.active_) {
125 throw new Error('[goog.net.IframeIo] Unable to send, already active.');
126 }
127
128 this.testQueue_.enqueue(['s', uri, opt_method, opt_noCache, opt_data]);
129 this.complete_ = false;
130 this.active_ = true;
131 };
132
133
134 /**
135 * Simulates the iframe send from a form.
136 * @param {Element} form Form element used to send the request to the server.
137 * @param {string=} opt_uri Uri to set for the destination of the request, by
138 * default the uri will come from the form.
139 * @param {boolean=} opt_noCache Append a timestamp to the request to avoid
140 * caching.
141 */
142 goog.testing.net.MockIFrameIo.prototype.sendFromForm = function(
143 form, opt_uri, opt_noCache) {
144 if (this.active_) {
145 throw new Error('[goog.net.IframeIo] Unable to send, already active.');
146 }
147
148 this.testQueue_.enqueue(['s', form, opt_uri, opt_noCache]);
149 this.complete_ = false;
150 this.active_ = true;
151 };
152
153
154 /**
155 * Simulates aborting the current Iframe request.
156 * @param {goog.net.ErrorCode=} opt_failureCode Optional error code to use -
157 * defaults to ABORT.
158 */
159 goog.testing.net.MockIFrameIo.prototype.abort = function(opt_failureCode) {
160 if (this.active_) {
161 this.testQueue_.enqueue(['a', opt_failureCode]);
162 this.complete_ = false;
163 this.active_ = false;
164 this.success_ = false;
165 this.lastErrorCode_ = opt_failureCode || goog.net.ErrorCode.ABORT;
166 this.dispatchEvent(goog.net.EventType.ABORT);
167 this.simulateReady();
168 }
169 };
170
171
172 /**
173 * Simulates receive of incremental data.
174 * @param {Object} data Data.
175 */
176 goog.testing.net.MockIFrameIo.prototype.simulateIncrementalData =
177 function(data) {
178 this.dispatchEvent(new goog.net.IframeIo.IncrementalDataEvent(data));
179 };
180
181
182 /**
183 * Simulates the iframe is done.
184 * @param {goog.net.ErrorCode} errorCode The error code for any error that
185 * should be simulated.
186 */
187 goog.testing.net.MockIFrameIo.prototype.simulateDone = function(errorCode) {
188 if (errorCode) {
189 this.success_ = false;
190 this.lastErrorCode_ = goog.net.ErrorCode.HTTP_ERROR;
191 this.lastError_ = this.getLastError();
192 this.dispatchEvent(goog.net.EventType.ERROR);
193 } else {
194 this.success_ = true;
195 this.lastErrorCode_ = goog.net.ErrorCode.NO_ERROR;
196 this.dispatchEvent(goog.net.EventType.SUCCESS);
197 }
198 this.complete_ = true;
199 this.dispatchEvent(goog.net.EventType.COMPLETE);
200 };
201
202
203 /**
204 * Simulates the IFrame is ready for the next request.
205 */
206 goog.testing.net.MockIFrameIo.prototype.simulateReady = function() {
207 this.dispatchEvent(goog.net.EventType.READY);
208 };
209
210
211 /**
212 * @return {boolean} True if transfer is complete.
213 */
214 goog.testing.net.MockIFrameIo.prototype.isComplete = function() {
215 return this.complete_;
216 };
217
218
219 /**
220 * @return {boolean} True if transfer was successful.
221 */
222 goog.testing.net.MockIFrameIo.prototype.isSuccess = function() {
223 return this.success_;
224 };
225
226
227 /**
228 * @return {boolean} True if a transfer is in progress.
229 */
230 goog.testing.net.MockIFrameIo.prototype.isActive = function() {
231 return this.active_;
232 };
233
234
235 /**
236 * Returns the last response text (i.e. the text content of the iframe).
237 * Assumes plain text!
238 * @return {string} Result from the server.
239 */
240 goog.testing.net.MockIFrameIo.prototype.getResponseText = function() {
241 return this.lastContent_;
242 };
243
244
245 /**
246 * Parses the content as JSON. This is a safe parse and may throw an error
247 * if the response is malformed.
248 * @return {Object} The parsed content.
249 */
250 goog.testing.net.MockIFrameIo.prototype.getResponseJson = function() {
251 return /** @type {!Object} */ (JSON.parse(this.lastContent_));
252 };
253
254
255 /**
256 * Get the uri of the last request.
257 * @return {goog.Uri} Uri of last request.
258 */
259 goog.testing.net.MockIFrameIo.prototype.getLastUri = function() {
260 return this.lastUri_;
261 };
262
263
264 /**
265 * Gets the last error code.
266 * @return {goog.net.ErrorCode} Last error code.
267 */
268 goog.testing.net.MockIFrameIo.prototype.getLastErrorCode = function() {
269 return this.lastErrorCode_;
270 };
271
272
273 /**
274 * Gets the last error message.
275 * @return {string} Last error message.
276 */
277 goog.testing.net.MockIFrameIo.prototype.getLastError = function() {
278 return goog.net.ErrorCode.getDebugMessage(this.lastErrorCode_);
279 };
280
281
282 /**
283 * Gets the last custom error.
284 * @return {Object} Last custom error.
285 */
286 goog.testing.net.MockIFrameIo.prototype.getLastCustomError = function() {
287 return this.lastCustomError_;
288 };
289
290
291 /**
292 * Sets the callback function used to check if a loaded IFrame is in an error
293 * state.
294 * @param {Function} fn Callback that expects a document object as it's single
295 * argument.
296 */
297 goog.testing.net.MockIFrameIo.prototype.setErrorChecker = function(fn) {
298 this.errorChecker_ = fn;
299 };
300
301
302 /**
303 * Gets the callback function used to check if a loaded IFrame is in an error
304 * state.
305 * @return {Function} A callback that expects a document object as it's single
306 * argument.
307 */
308 goog.testing.net.MockIFrameIo.prototype.getErrorChecker = function() {
309 return this.errorChecker_;
310 };