Debian Codegraph @

closure-library/closure/goog/net/iframeio_test.js

raw
1 // Copyright 2006 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 goog.provide('goog.net.IframeIoTest');
16 goog.setTestOnly('goog.net.IframeIoTest');
17
18 goog.require('goog.debug');
19 goog.require('goog.debug.DivConsole');
20 goog.require('goog.debug.LogManager');
21 goog.require('goog.dom');
22 goog.require('goog.dom.TagName');
23 goog.require('goog.events');
24 goog.require('goog.events.EventType');
25 goog.require('goog.log');
26 goog.require('goog.log.Level');
27 goog.require('goog.net.IframeIo');
28 goog.require('goog.testing.events');
29 goog.require('goog.testing.events.Event');
30 goog.require('goog.testing.jsunit');
31 goog.require('goog.userAgent');
32
33 // MANUAL TESTS - The tests should be run in the browser from the Closure Test
34 // Server
35
36 // Set up a logger to track responses
37 goog.debug.LogManager.getRoot().setLevel(goog.log.Level.INFO);
38 var logconsole;
39 var testLogger = goog.log.getLogger('test');
40
41 function setUpPage() {
42 var logconsole = new goog.debug.DivConsole(document.getElementById('log'));
43 logconsole.setCapturing(true);
44 }
45
46
47 /** Creates an iframeIo instance and sets up the test environment */
48 function getTestIframeIo() {
49 logconsole.addSeparator();
50 logconsole.getFormatter().resetRelativeTimeStart();
51
52 var io = new goog.net.IframeIo();
53 io.setErrorChecker(checkForError);
54
55 goog.events.listen(io, 'success', onSuccess);
56 goog.events.listen(io, 'error', onError);
57 goog.events.listen(io, 'ready', onReady);
58
59 return io;
60 }
61
62
63 /**
64 * Checks for error strings returned by the GSE and error variables that
65 * the Gmail server and GFE set on certain errors.
66 */
67 function checkForError(doc) {
68 var win = goog.dom.getWindow(doc);
69 var text = doc.body.textContent || doc.body.innerText || '';
70 var gseError = text.match(/([^\n]+)\nError ([0-9]{3})/);
71 if (gseError) {
72 return '(Error ' + gseError[2] + ') ' + gseError[1];
73 } else if (win.gmail_error) {
74 return win.gmail_error + 700;
75 } else if (win.rc) {
76 return 600 + win.rc % 100;
77 } else {
78 return null;
79 }
80 }
81
82
83 /** Logs the status of an iframeIo object */
84 function logStatus(i) {
85 goog.log.fine(
86 testLogger, 'Is complete/success/active: ' +
87 [i.isComplete(), i.isSuccess(), i.isActive()].join('/'));
88 }
89
90 function onSuccess(e) {
91 goog.log.warning(testLogger, 'Request Succeeded');
92 logStatus(e.target);
93 }
94
95 function onError(e) {
96 goog.log.warning(testLogger, 'Request Errored: ' + e.target.getLastError());
97 logStatus(e.target);
98 }
99
100 function onReady(e) {
101 goog.log.info(
102 testLogger, 'Test finished and iframe ready, disposing test object');
103 e.target.dispose();
104 }
105
106
107
108 function simpleGet() {
109 var io = getTestIframeIo();
110 goog.events.listen(io, 'complete', onSimpleTestComplete);
111 io.send('/iframeio/ping', 'GET');
112 }
113
114
115 function simplePost() {
116 var io = getTestIframeIo();
117 goog.events.listen(io, 'complete', onSimpleTestComplete);
118 io.send('/iframeio/ping', 'POST');
119 }
120
121 function onSimpleTestComplete(e) {
122 goog.log.info(testLogger, 'ResponseText: ' + e.target.getResponseText());
123 }
124
125 function abort() {
126 var io = getTestIframeIo();
127 goog.events.listen(io, 'complete', onAbortComplete);
128 goog.events.listen(io, 'abort', onAbort);
129 io.send('/iframeio/ping', 'GET');
130 io.abort();
131 }
132
133 function onAbortComplete(e) {
134 goog.log.info(testLogger, 'Hmm, request should have been aborted');
135 }
136
137 function onAbort(e) {
138 goog.log.info(testLogger, 'Request aborted');
139 }
140
141
142 function errorGse404() {
143 var io = getTestIframeIo();
144 io.send('/iframeio/404', 'GET');
145 }
146
147 function jsonEcho(method) {
148 var io = getTestIframeIo();
149 goog.events.listen(io, 'complete', onJsonComplete);
150 var data = {'p1': 'x', 'p2': 'y', 'p3': 'z', 'r': 10};
151 io.send('/iframeio/jsonecho?q1=a&q2=b&q3=c&r=5', method, false, data);
152 }
153
154 function onJsonComplete(e) {
155 goog.log.info(testLogger, 'ResponseText: ' + e.target.getResponseText());
156 var json = e.target.getResponseJson();
157 goog.log.info(
158 testLogger, 'ResponseJson:\n' + goog.debug.deepExpose(json, true));
159 }
160
161
162
163 function sendFromForm() {
164 var io = getTestIframeIo();
165 goog.events.listen(io, 'success', onUploadSuccess);
166 goog.events.listen(io, 'error', onUploadError);
167 io.sendFromForm(document.getElementById('uploadform'));
168 }
169
170 function onUploadSuccess(e) {
171 goog.log.log(testLogger, goog.log.Level.SHOUT, 'Upload Succeeded');
172 goog.log.info(testLogger, 'ResponseText: ' + e.target.getResponseText());
173 }
174
175 function onUploadError(e) {
176 goog.log.log(testLogger, goog.log.Level.SHOUT, 'Upload Errored');
177 goog.log.info(testLogger, 'ResponseText: ' + e.target.getResponseText());
178 }
179
180
181 function redirect1() {
182 var io = getTestIframeIo();
183 io.send('/iframeio/redirect', 'GET');
184 }
185
186 function redirect2() {
187 var io = getTestIframeIo();
188 io.send('/iframeio/move', 'GET');
189 }
190
191 function badUrl() {
192 var io = getTestIframeIo();
193 io.send('http://news.bbc.co.uk', 'GET');
194 }
195
196 function localUrl1() {
197 var io = getTestIframeIo();
198 goog.events.listen(io, 'complete', onLocalSuccess);
199 io.send('c:\test.txt', 'GET');
200 }
201
202 function localUrl2() {
203 var io = getTestIframeIo();
204 goog.events.listen(io, 'success', onLocalSuccess);
205 io.send('//test.txt', 'GET');
206 }
207
208 function onLocalSuccess(e) {
209 goog.log.info(
210 testLogger, 'The file was found:\n' + e.target.getResponseText());
211 }
212
213 function getServerTime(noCache) {
214 var io = getTestIframeIo();
215 goog.events.listen(io, 'success', onTestCacheSuccess);
216 io.send('/iframeio/datetime', 'GET', noCache);
217 }
218
219 function onTestCacheSuccess(e) {
220 goog.log.info(testLogger, 'Date reported: ' + e.target.getResponseText());
221 }
222
223
224 function errorGmail() {
225 var io = getTestIframeIo();
226 goog.events.listen(io, 'error', onGmailError);
227 io.send('/iframeio/gmailerror', 'GET');
228 }
229
230 function onGmailError(e) {
231 goog.log.info(testLogger, 'Gmail error: ' + e.target.getLastError());
232 }
233
234
235 function errorGfe() {
236 var io = getTestIframeIo();
237 goog.events.listen(io, 'error', onGfeError);
238 io.send('/iframeio/gfeerror', 'GET');
239 }
240
241 function onGfeError(e) {
242 goog.log.info(testLogger, 'GFE error: ' + e.target.getLastError());
243 }
244
245
246
247 function incremental() {
248 var io = getTestIframeIo();
249 io.send('/iframeio/incremental', 'GET');
250 }
251
252 window['P'] = function(iframe, data) {
253 goog.net.IframeIo.getInstanceByName(iframe.name);
254 goog.log.info(testLogger, 'Data received - ' + data);
255 };
256
257
258 function postForm() {
259 var io = getTestIframeIo();
260 goog.events.listen(io, 'complete', onJsonComplete);
261 io.sendFromForm(document.getElementById('testfrm'));
262 }
263 // UNIT TESTS - to be run via the JsUnit testRunner
264
265 // TODO(user): How to unit test all of this? Creating a MockIframe could
266 // help for the IE code path, but since the other browsers require weird
267 // behaviors this becomes very tricky.
268
269
270 function testGetForm() {
271 var frm1 = goog.net.IframeIo.getForm_;
272 var frm2 = goog.net.IframeIo.getForm_;
273 assertEquals(frm1, frm2);
274 }
275
276
277 function testAddFormInputs() {
278 var form = goog.dom.createElement(goog.dom.TagName.FORM);
279 goog.net.IframeIo.addFormInputs_(form, {'a': 1, 'b': 2, 'c': 3});
280 var inputs = goog.dom.getElementsByTagName(goog.dom.TagName.INPUT, form);
281 assertEquals(3, inputs.length);
282 for (var i = 0; i < inputs.length; i++) {
283 assertEquals('hidden', inputs[i].type);
284 var n = inputs[i].name;
285 assertEquals(n == 'a' ? '1' : n == 'b' ? '2' : '3', inputs[i].value);
286 }
287 }
288
289 function testAddFormArrayInputs() {
290 var form = goog.dom.createElement(goog.dom.TagName.FORM);
291 var data = {'a': ['blue', 'green'], 'b': ['red', 'pink', 'white']};
292 goog.net.IframeIo.addFormInputs_(form, data);
293 var inputs = goog.dom.getElementsByTagName(goog.dom.TagName.INPUT, form);
294 assertEquals(5, inputs.length);
295 for (var i = 0; i < inputs.length; i++) {
296 assertEquals('hidden', inputs[i].type);
297 var n = inputs[i].name;
298 assertContains(inputs[i].value, data[n]);
299 }
300 }
301
302 function testNotIgnoringResponse() {
303 // This test can't run in IE because we can't forge the check for
304 // iframe.readyState = 'complete'.
305 if (goog.userAgent.IE) {
306 return;
307 }
308 var iframeIo = new goog.net.IframeIo();
309 iframeIo.send('about:blank');
310 // Simulate the frame finishing loading.
311 goog.testing.events.fireBrowserEvent(
312 new goog.testing.events.Event(
313 goog.events.EventType.LOAD, iframeIo.getRequestIframe()));
314 assertTrue(iframeIo.isComplete());
315 }
316
317 function testIgnoreResponse() {
318 var iframeIo = new goog.net.IframeIo();
319 iframeIo.setIgnoreResponse(true);
320 iframeIo.send('about:blank');
321 // Simulate the frame finishing loading.
322 goog.testing.events.fireBrowserEvent(
323 new goog.testing.events.Event(
324 goog.events.EventType.LOAD, iframeIo.getRequestIframe()));
325 // Although the request is complete, the IframeIo isn't paying attention.
326 assertFalse(iframeIo.isComplete());
327 }