Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/net/mockiframeio.js
2868 views
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.json');
22
goog.require('goog.net.ErrorCode');
23
goog.require('goog.net.EventType');
24
goog.require('goog.net.IframeIo');
25
goog.forwardDeclare('goog.testing.TestQueue');
26
27
28
29
/**
30
* Mock implementation of goog.net.IframeIo. This doesn't provide a mock
31
* implementation for all cases, but it's not too hard to add them as needed.
32
* @param {goog.testing.TestQueue} testQueue Test queue for inserting test
33
* events.
34
* @constructor
35
* @extends {goog.events.EventTarget}
36
* @final
37
* @deprecated Use goog.testing.net.MockIFrameIo instead.
38
*/
39
goog.net.MockIFrameIo = function(testQueue) {
40
goog.events.EventTarget.call(this);
41
42
/**
43
* Queue of events write to
44
* @type {goog.testing.TestQueue}
45
* @private
46
*/
47
this.testQueue_ = testQueue;
48
49
};
50
goog.inherits(goog.net.MockIFrameIo, goog.events.EventTarget);
51
52
53
/**
54
* Whether MockIFrameIo is active.
55
* @type {boolean}
56
* @private
57
*/
58
goog.net.MockIFrameIo.prototype.active_ = false;
59
60
61
/**
62
* Last content.
63
* @type {string}
64
* @private
65
*/
66
goog.net.MockIFrameIo.prototype.lastContent_ = '';
67
68
69
/**
70
* Last error code.
71
* @type {goog.net.ErrorCode}
72
* @private
73
*/
74
goog.net.MockIFrameIo.prototype.lastErrorCode_ = goog.net.ErrorCode.NO_ERROR;
75
76
77
/**
78
* Last error message.
79
* @type {string}
80
* @private
81
*/
82
goog.net.MockIFrameIo.prototype.lastError_ = '';
83
84
85
/**
86
* Last custom error.
87
* @type {Object}
88
* @private
89
*/
90
goog.net.MockIFrameIo.prototype.lastCustomError_ = null;
91
92
93
/**
94
* Last URI.
95
* @type {goog.Uri}
96
* @private
97
*/
98
goog.net.MockIFrameIo.prototype.lastUri_ = null;
99
100
101
/** @private {Function} */
102
goog.net.MockIFrameIo.prototype.errorChecker_;
103
104
105
/** @private {boolean} */
106
goog.net.MockIFrameIo.prototype.success_;
107
108
109
/** @private {boolean} */
110
goog.net.MockIFrameIo.prototype.complete_;
111
112
113
/**
114
* Simulates the iframe send.
115
*
116
* @param {goog.Uri|string} uri Uri of the request.
117
* @param {string=} opt_method Default is GET, POST uses a form to submit the
118
* request.
119
* @param {boolean=} opt_noCache Append a timestamp to the request to avoid
120
* caching.
121
* @param {Object|goog.structs.Map=} opt_data Map of key-value pairs.
122
*/
123
goog.net.MockIFrameIo.prototype.send = function(
124
uri, opt_method, opt_noCache, opt_data) {
125
if (this.active_) {
126
throw Error('[goog.net.IframeIo] Unable to send, already active.');
127
}
128
129
this.testQueue_.enqueue(['s', uri, opt_method, opt_noCache, opt_data]);
130
this.complete_ = false;
131
this.active_ = true;
132
};
133
134
135
/**
136
* Simulates the iframe send from a form.
137
* @param {Element} form Form element used to send the request to the server.
138
* @param {string=} opt_uri Uri to set for the destination of the request, by
139
* default the uri will come from the form.
140
* @param {boolean=} opt_noCache Append a timestamp to the request to avoid
141
* caching.
142
*/
143
goog.net.MockIFrameIo.prototype.sendFromForm = function(
144
form, opt_uri, opt_noCache) {
145
if (this.active_) {
146
throw Error('[goog.net.IframeIo] Unable to send, already active.');
147
}
148
149
this.testQueue_.enqueue(['s', form, opt_uri, opt_noCache]);
150
this.complete_ = false;
151
this.active_ = true;
152
};
153
154
155
/**
156
* Simulates aborting the current Iframe request.
157
* @param {goog.net.ErrorCode=} opt_failureCode Optional error code to use -
158
* defaults to ABORT.
159
*/
160
goog.net.MockIFrameIo.prototype.abort = function(opt_failureCode) {
161
if (this.active_) {
162
this.testQueue_.enqueue(['a', opt_failureCode]);
163
this.complete_ = false;
164
this.active_ = false;
165
this.success_ = false;
166
this.lastErrorCode_ = opt_failureCode || goog.net.ErrorCode.ABORT;
167
this.dispatchEvent(goog.net.EventType.ABORT);
168
this.simulateReady();
169
}
170
};
171
172
173
/**
174
* Simulates receive of incremental data.
175
* @param {Object} data Data.
176
*/
177
goog.net.MockIFrameIo.prototype.simulateIncrementalData = 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.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.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.net.MockIFrameIo.prototype.isComplete = function() {
215
return this.complete_;
216
};
217
218
219
/**
220
* @return {boolean} True if transfer was successful.
221
*/
222
goog.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.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.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.net.MockIFrameIo.prototype.getResponseJson = function() {
251
return goog.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.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.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.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.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.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.net.MockIFrameIo.prototype.getErrorChecker = function() {
309
return this.errorChecker_;
310
};
311
312