~ubuntu-branches/ubuntu/saucy/whoopsie-daisy/saucy

« back to all changes in this revision

Viewing changes to backend/stats/static/js/yui/3.4.1/tests/jsonp/tests/src/jsonp.js

  • Committer: Package Import Robot
  • Author(s): Evan Dandrea
  • Date: 2012-04-18 13:04:36 UTC
  • Revision ID: package-import@ubuntu.com-20120418130436-vmt93p8fds516lws
Tags: 0.1.32
Fix failing tests on powerpc and ARM.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
var suite = new Y.Test.Suite("Y.JSONPRequest and Y.jsonp with jsonp-url"),
2
 
    // Dirty dirty hack to future proof duck type for onload where
3
 
    // possible
4
 
    onScriptLoad = (function () {
5
 
        var onload = true,
6
 
            onrsc = true;
7
 
 
8
 
        return function (node, callback) {
9
 
            var loaded = false;
10
 
 
11
 
            if (onload) {
12
 
                node.onload = function () {
13
 
                    // prefer onload to onreadystatechange
14
 
                    onload = true;
15
 
                    this.onload = this.onreadystatechange = onrsc = null;
16
 
                    if (!loaded) {
17
 
                        callback();
18
 
                        loaded = true;
19
 
                    }
20
 
                };
21
 
            }
22
 
            if (onrsc) {
23
 
                node.onreadystatechange = function () {
24
 
                    if (!('loaded|complete'.indexOf(this.readyState) % 7)) {
25
 
                        // assume no onload support until onload says so.
26
 
                        // this leaks one onload function.
27
 
                        this.onreadystatechange = null;
28
 
                        // just in case onload fired first (which it shouldn't)
29
 
                        if (onrsc && !loaded) {
30
 
                            onload = false;
31
 
                            callback();
32
 
                            loaded = true;
33
 
                        }
34
 
                    }
35
 
                };
36
 
            }
37
 
        };
38
 
    })();
39
 
 
40
 
suite.add(new Y.Test.Case({
41
 
    name: "send",
42
 
 
43
 
    "config charset should be set via Y.Get.script": function () {
44
 
        var test = this,
45
 
            scripts = Y.all('script')._nodes,
46
 
            newScript;
47
 
 
48
 
        Y.jsonp("server/service.php?callback={callback}", function () {});
49
 
 
50
 
        newScript = Y.Array.filter(Y.all('script')._nodes, function (s) {
51
 
            return Y.Array.indexOf(scripts, s) === -1;
52
 
        })[0];
53
 
 
54
 
        Y.Assert.areSame("utf-8", newScript.charset);
55
 
 
56
 
        scripts.push(newScript);
57
 
 
58
 
        Y.jsonp("server/service.php?callback={callback}", {
59
 
            on: {
60
 
                success: function () {}
61
 
            },
62
 
            charset: "GBK"
63
 
        });
64
 
 
65
 
        newScript = Y.Array.filter(Y.all('script')._nodes, function (s) {
66
 
            return Y.Array.indexOf(scripts, s) === -1;
67
 
        })[0];
68
 
 
69
 
        Y.Assert.areSame("GBK", newScript.getAttribute("charset"));
70
 
 
71
 
        // to allow JSONP the chance to clean up the callback registry
72
 
        // before other tests begin.  Race condition, yes.  The alternative
73
 
        // was to wait() and resume() in the success callback, but for some
74
 
        // reason Test.Runner cleared the test's wait timer before the
75
 
        // success callback, so either the test fails because resume() is
76
 
        // called when Test.Runner doesn't think it's waiting, OR the test
77
 
        // fails if resume() is only called if Test.Runner thinks it is waiting
78
 
        // (which it doesn't) so resume() is never called.
79
 
        test.wait(function () {}, 1000);
80
 
    },
81
 
 
82
 
    "config attributes should be set via Y.Get.script": function () {
83
 
        var test = this,
84
 
            scripts = Y.all('script')._nodes,
85
 
            newScript;
86
 
 
87
 
        Y.jsonp("server/service.php?callback={callback}", {
88
 
            on: {
89
 
                success: function () { test.resume(); }
90
 
            },
91
 
            attributes: {
92
 
                // passing an attribute that is less likely to be skipped over
93
 
                // by browser whitelisting (if they do that now or will later)
94
 
                language: "javascript"
95
 
            }
96
 
        });
97
 
 
98
 
        newScript = Y.Array.filter(Y.all('script')._nodes, function (s) {
99
 
            return Y.Array.indexOf(scripts, s) === -1;
100
 
        })[0];
101
 
 
102
 
        Y.Assert.areSame("javascript", newScript.getAttribute("language"));
103
 
 
104
 
        // to allow JSONP the chance to clean up the callback registry before
105
 
        // other tests begin.
106
 
        test.wait();
107
 
    }
108
 
}));
109
 
 
110
 
suite.add(new Y.Test.Case({
111
 
    name : "callbacks",
112
 
        
113
 
    _should: {
114
 
        ignore: {
115
 
            // Get (v3.3) doesn't support onerror in recent webkit
116
 
            "failure handler in callback object should execute": true
117
 
        }
118
 
    },
119
 
 
120
 
    "callback function as second arg should be success handler": function () {
121
 
        var self = this;
122
 
 
123
 
        Y.jsonp("server/service.php?&callback={callback}", function (json) {
124
 
            //console.log(Y.Object.keys(YUI.Env.JSONP), "callback function as second arg should be success handler");
125
 
            self.resume(function () {
126
 
                Y.Assert.isObject(json);
127
 
            });
128
 
        });
129
 
 
130
 
        self.wait();
131
 
    },
132
 
 
133
 
    "success handler in callback object should execute": function () {
134
 
        var self = this;
135
 
 
136
 
        Y.jsonp("server/service.php?&callback={callback}", {
137
 
            on: {
138
 
                success: function (json) {
139
 
                    //console.log(Y.Object.keys(YUI.Env.JSONP), "success handler in callback object should execute");
140
 
                    self.resume(function () {
141
 
                        Y.Assert.isObject(json);
142
 
                    });
143
 
                }
144
 
            }
145
 
        });
146
 
 
147
 
        self.wait();
148
 
    },
149
 
 
150
 
    "failure handler in callback object should execute": function () {
151
 
        var self = this;
152
 
 
153
 
        Y.jsonp("server/404.php?&callback={callback}", {
154
 
            on: {
155
 
                success: function (json) {
156
 
                    self.resume(function () {
157
 
                        Y.Assert.fail("Success handler called from 404 response");
158
 
                    });
159
 
                },
160
 
                failure: function () {
161
 
                    //console.log("failure handler in callback object should execute");
162
 
                    // Pass
163
 
                    self.resume(function () {});
164
 
                }
165
 
            }
166
 
        });
167
 
 
168
 
        self.wait();
169
 
    },
170
 
 
171
 
    "failure handler in callback object should not execute for successful io": function () {
172
 
        var self = this;
173
 
 
174
 
        Y.jsonp("server/service.php?&callback={callback}", {
175
 
            on: {
176
 
                success: function (json) {
177
 
                    //console.log(Y.Object.keys(YUI.Env.JSONP), "failure handler in callback object should not execute for successful io");
178
 
                    // Pass
179
 
                    self.resume(function () {});
180
 
                },
181
 
                failure: function () {
182
 
                    self.resume(function () {
183
 
                        Y.Assert.fail("Failure handler called after successful response");
184
 
                    });
185
 
                }
186
 
            }
187
 
        });
188
 
 
189
 
        self.wait();
190
 
    },
191
 
 
192
 
    "test multiple send() from an instance of Y.JSONPRequest": function () {
193
 
        var self = this,
194
 
            count = 0,
195
 
            service;
196
 
 
197
 
        service = new Y.JSONPRequest("server/service.php?callback={callback}", {
198
 
            on: {
199
 
                success: function (json) {
200
 
                    //console.log(Y.Object.keys(YUI.Env.JSONP), "test multiple send() from an instance of Y.JSONPRequest");
201
 
                    if (++count === 3) {
202
 
                        self.resume(function () {
203
 
                            // Pass
204
 
                            Y.Assert.areSame(count, 3);
205
 
                        });
206
 
                    }
207
 
                }
208
 
            }
209
 
        });
210
 
 
211
 
        service.send().send().send();
212
 
 
213
 
        this.wait();
214
 
    }
215
 
 
216
 
    // failure for bogus response data (not yet implemented)
217
 
    // missing {callback} (not sure how to test. No callback would be attached.
218
 
    //      Maybe have the service create a property on YUI and have the test
219
 
    //      look for that after a time?)
220
 
    // missing success handler (logs a warning)
221
 
    // JSONPRequest + send
222
 
    // JSONPRequest + send with config overrides (not yet implemented)
223
 
}));
224
 
 
225
 
suite.add(new Y.Test.Case({
226
 
    name : "context and args"
227
 
        
228
 
}));
229
 
 
230
 
suite.add(new Y.Test.Case({
231
 
    name : "format"
232
 
        
233
 
}));
234
 
 
235
 
suite.add(new Y.Test.Case({
236
 
    name : "allowCache",
237
 
        
238
 
    "allowCache should preserve the same callback": function () {
239
 
        var test = this,
240
 
            remaining = 2,
241
 
            callback,
242
 
            jsonp = new Y.JSONPRequest('server/service.php?&callback={callback}', {
243
 
                allowCache: true,
244
 
                on: {
245
 
                    success: function (data) {
246
 
                        //console.log(Y.Object.keys(YUI.Env.JSONP), "allowCache should preserve the same callback");
247
 
                        if (callback) {
248
 
                            if (callback !== data.callback) {
249
 
                                test.resume(function () {
250
 
                                    Y.Assert.areSame(callback, data.callback, "callback proxy name should be reused");
251
 
                                });
252
 
                            } else if (--remaining) {
253
 
                                jsonp.send();
254
 
                            } else {
255
 
                                test.resume(function () {
256
 
                                    // Pass
257
 
                                });
258
 
                            }
259
 
                        } else {
260
 
                            callback = data.callback;
261
 
                            jsonp.send();
262
 
                        }
263
 
 
264
 
                    }
265
 
                }
266
 
            });
267
 
 
268
 
        jsonp.send();
269
 
 
270
 
        this.wait();
271
 
    },
272
 
 
273
 
    "allowCache should not clear proxy if another send() is pending response":
274
 
    function () {
275
 
        var test = this,
276
 
            callbacks = [],
277
 
            jsonp = new Y.JSONPRequest('server/service.php?&callback={callback}', {
278
 
                allowCache: true,
279
 
                on: {
280
 
                    success: function (data) {
281
 
                        //console.log(Y.Object.keys(YUI.Env.JSONP), "allowCache should not clear proxy if another send() is pending response");
282
 
                        callbacks.push(data.callback);
283
 
 
284
 
                        if (callbacks.length > 2) {
285
 
                            test.resume(function () {
286
 
                                Y.Assert.areSame(callbacks[0], callbacks[1]);
287
 
                                Y.Assert.areSame(callbacks[1], callbacks[2]);
288
 
                                Y.Assert.isUndefined(YUI.Env.JSONP[callbacks[0]]);
289
 
                            });
290
 
                        } else if (!YUI.Env.JSONP[data.callback.split(/\./).pop()]) {
291
 
                            test.resume(function () {
292
 
                                Y.Assert.fail("proxy cleared prematurely");
293
 
                            });
294
 
                        }
295
 
 
296
 
                    }
297
 
                }
298
 
            });
299
 
 
300
 
        jsonp.send();
301
 
        jsonp.send();
302
 
        jsonp.send();
303
 
 
304
 
        this.wait();
305
 
    }
306
 
 
307
 
}));
308
 
 
309
 
suite.add(new Y.Test.Case({
310
 
    name : "timeout",
311
 
        
312
 
    "timeout should not flush the global proxy": function () {
313
 
        var test = this,
314
 
            timeoutCalled = false,
315
 
            jsonpProxies = Y.Object.keys(YUI.Env.JSONP).length,
316
 
            jsonp = new Y.JSONPRequest('server/service.php?&wait=2&callback={callback}', {
317
 
                timeout: 1000,
318
 
                on: {
319
 
                    success: function (data) {
320
 
                        //console.log(Y.Object.keys(YUI.Env.JSONP), "timeout should not flush the global proxy");
321
 
                        test.resume(function () {
322
 
                            Y.Assert.fail("Success callback executed after timeout");
323
 
                        });
324
 
                    },
325
 
                    timeout: function () {
326
 
                        //console.log(Y.Object.keys(YUI.Env.JSONP), "timeout should not flush the global proxy (timeout)");
327
 
                        timeoutCalled = true;
328
 
                    }
329
 
                }
330
 
            }),
331
 
            scripts = Y.all('script')._nodes,
332
 
            newScript;
333
 
 
334
 
        Y.Assert.areSame(0, jsonpProxies, "Whar these leavings from?");
335
 
 
336
 
        jsonp.send();
337
 
 
338
 
        // Success is measured by the success callback NOT being executed,
339
 
        // but we're in an async operation, so I need something to trigger
340
 
        // a test.resume(..).  So I'm finding the added script tag and
341
 
        // hooking a separate onload handler to it, which would execute after
342
 
        // the success handler if it is called due to callbacks executing in
343
 
        // subscription order.  Not pretty, but better than a longer setTimeout?
344
 
        newScript = Y.Array.filter(Y.all('script')._nodes, function (s) {
345
 
            return Y.Array.indexOf(scripts, s) === -1;
346
 
        })[0];
347
 
 
348
 
        onScriptLoad(newScript, function () {
349
 
            //console.log("__yui_wait: " + test.__yui_wait + " (should be a setTimeout int)");
350
 
            // If the success callback is triggered, it will resume the test,
351
 
            // and clear the wait() timeout, so having another resume() here
352
 
            // will just blow up.  The test has already failed if !_waiting
353
 
            if (Y.Test.Runner._waiting) {
354
 
                test.resume(function () {
355
 
                    Y.Assert.isTrue(timeoutCalled);
356
 
                    Y.Assert.areSame(jsonpProxies, Y.Object.keys(YUI.Env.JSONP).length);
357
 
                });
358
 
            }
359
 
        });
360
 
 
361
 
        test.wait(3000);
362
 
    }
363
 
 
364
 
    /*
365
 
    ,
366
 
 
367
 
    "timeout should not flush the global proxy across multiple send calls": function () {
368
 
        // README
369
 
        // Stubbed from the test above.  This test needs to contact the
370
 
        // serverat the same url, but get varying delays.  This means to
371
 
        // properly test, the server needs to behave randomly, and this test
372
 
        // needs to iterate until that random behavior matches the expected
373
 
        // test behavior.  Which is icky.
374
 
        var test = this,
375
 
            timeoutCalled = false,
376
 
            jsonp = new Y.JSONPRequest('server/service.php?wait=2&callback={callback}', {
377
 
                timeout: 1000,
378
 
                on: {
379
 
                    success: function (data) {
380
 
                        test.resume(function () {
381
 
                            Y.Assert.fail("Success callback executed after timeout");
382
 
                        });
383
 
                    },
384
 
                    timeout: function () {
385
 
                        timeoutCalled = true;
386
 
                    }
387
 
                }
388
 
            }),
389
 
            scripts = Y.all('script')._nodes,
390
 
            newScript;
391
 
 
392
 
        jsonp.send();
393
 
 
394
 
        newScript = Y.Array.filter(Y.all('script')._nodes, function (s) {
395
 
            return Y.Array.indexOf(scripts, s) === -1;
396
 
        })[0];
397
 
 
398
 
        Y.on('load', function () {
399
 
            test.resume(function () {
400
 
                Y.Assert.isTrue(timeoutCalled);
401
 
            });
402
 
        }, newScript);
403
 
 
404
 
        test.wait(3000);
405
 
    }
406
 
    */
407
 
}));
408
 
 
409
 
Y.Test.Runner.add(suite);