Debian Codegraph @

closure-library/closure/goog/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.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.forwardDeclare('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 * @deprecated Use goog.testing.net.MockIFrameIo instead.
37 */
38 goog.net.MockIFrameIo = function(testQueue) {
39 goog.events.EventTarget.call(this);
40
41 /**
42 * Queue of events write to
43 * @type {goog.testing.TestQueue}
44 * @private
45 */
46 this.testQueue_ = testQueue;
47
48 };
49 goog.inherits(goog.net.MockIFrameIo, goog.events.EventTarget);
50
51
52 /**
53 * Whether MockIFrameIo is active.
54 * @type {boolean}
55 * @private
56 */
57 goog.net.MockIFrameIo.prototype.active_ = false;
58
59
60 /**
61 * Last content.
62 * @type {string}
63 * @private
64 */
65 goog.net.MockIFrameIo.prototype.lastContent_ = '';
66
67
68 /**
69 * Last error code.
70 * @type {goog.net.ErrorCode}
71 * @private
72 */
73 goog.net.MockIFrameIo.prototype.lastErrorCode_ = goog.net.ErrorCode.NO_ERROR;
74
75
76 /**
77 * Last error message.
78 * @type {string}
79 * @private
80 */
81 goog.net.MockIFrameIo.prototype.lastError_ = '';
82
83
84 /**
85 * Last custom error.
86 * @type {Object}
87 * @private
88 */
89 goog.net.MockIFrameIo.prototype.lastCustomError_ = null;
90
91
92 /**
93 * Last URI.
94 * @type {goog.Uri}
95 * @private
96 */
97 goog.net.MockIFrameIo.prototype.lastUri_ = null;
98
99
100 /** @private {Function} */
101 goog.net.MockIFrameIo.prototype.errorChecker_;
102
103
104 /** @private {boolean} */
105 goog.net.MockIFrameIo.prototype.success_;
106
107
108 /** @private {boolean} */
109 goog.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.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.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.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.net.MockIFrameIo.prototype.simulateIncrementalData = function(data) {
177 this.dispatchEvent(new goog.net.IframeIo.IncrementalDataEvent(data));
178 };
179
180
181 /**
182 * Simulates the iframe is done.
183 * @param {goog.net.ErrorCode} errorCode The error code for any error that
184 * should be simulated.
185 */
186 goog.net.MockIFrameIo.prototype.simulateDone = function(errorCode) {
187 if (errorCode) {
188 this.success_ = false;
189 this.lastErrorCode_ = goog.net.ErrorCode.HTTP_ERROR;
190 this.lastError_ = this.getLastError();
191 this.dispatchEvent(goog.net.EventType.ERROR);
192 } else {
193 this.success_ = true;
194 this.lastErrorCode_ = goog.net.ErrorCode.NO_ERROR;
195 this.dispatchEvent(goog.net.EventType.SUCCESS);
196 }
197 this.complete_ = true;
198 this.dispatchEvent(goog.net.EventType.COMPLETE);
199 };
200
201
202 /**
203 * Simulates the IFrame is ready for the next request.
204 */
205 goog.net.MockIFrameIo.prototype.simulateReady = function() {
206 this.dispatchEvent(goog.net.EventType.READY);
207 };
208
209
210 /**
211 * @return {boolean} True if transfer is complete.
212 */
213 goog.net.MockIFrameIo.prototype.isComplete = function() {
214 return this.complete_;
215 };
216
217
218 /**
219 * @return {boolean} True if transfer was successful.
220 */
221 goog.net.MockIFrameIo.prototype.isSuccess = function() {
222 return this.success_;
223 };
224
225
226 /**
227 * @return {boolean} True if a transfer is in progress.
228 */
229 goog.net.MockIFrameIo.prototype.isActive = function() {
230 return this.active_;
231 };
232
233
234 /**
235 * Returns the last response text (i.e. the text content of the iframe).
236 * Assumes plain text!
237 * @return {string} Result from the server.
238 */
239 goog.net.MockIFrameIo.prototype.getResponseText = function() {
240 return this.lastContent_;
241 };
242
243
244 /**
245 * Parses the content as JSON. This is a safe parse and may throw an error
246 * if the response is malformed.
247 * @return {Object} The parsed content.
248 */
249 goog.net.MockIFrameIo.prototype.getResponseJson = function() {
250 return /** @type {!Object} */ (JSON.parse(this.lastContent_));
251 };
252
253
254 /**
255 * Get the uri of the last request.
256 * @return {goog.Uri} Uri of last request.
257 */
258 goog.net.MockIFrameIo.prototype.getLastUri = function() {
259 return this.lastUri_;
260 };
261
262
263 /**
264 * Gets the last error code.
265 * @return {goog.net.ErrorCode} Last error code.
266 */
267 goog.net.MockIFrameIo.prototype.getLastErrorCode = function() {
268 return this.lastErrorCode_;
269 };
270
271
272 /**
273 * Gets the last error message.
274 * @return {string} Last error message.
275 */
276 goog.net.MockIFrameIo.prototype.getLastError = function() {
277 return goog.net.ErrorCode.getDebugMessage(this.lastErrorCode_);
278 };
279
280
281 /**
282 * Gets the last custom error.
283 * @return {Object} Last custom error.
284 */
285 goog.net.MockIFrameIo.prototype.getLastCustomError = function() {
286 return this.lastCustomError_;
287 };
288
289
290 /**
291 * Sets the callback function used to check if a loaded IFrame is in an error
292 * state.
293 * @param {Function} fn Callback that expects a document object as it's single
294 * argument.
295 */
296 goog.net.MockIFrameIo.prototype.setErrorChecker = function(fn) {
297 this.errorChecker_ = fn;
298 };
299
300
301 /**
302 * Gets the callback function used to check if a loaded IFrame is in an error
303 * state.
304 * @return {Function} A callback that expects a document object as it's single
305 * argument.
306 */
307 goog.net.MockIFrameIo.prototype.getErrorChecker = function() {
308 return this.errorChecker_;
309 };