OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 11 matching lines...) Expand all Loading... | |
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 * THE POSSIBILITY OF SUCH DAMAGE. | 23 * THE POSSIBILITY OF SUCH DAMAGE. |
24 */ | 24 */ |
25 | 25 |
26 // Export NetworkSimulator for use by other unittests. | 26 // Export NetworkSimulator for use by other unittests. |
27 function NetworkSimulator() | 27 function NetworkSimulator() |
28 { | 28 { |
29 this._pendingPromises = []; | 29 this._pendingPromises = []; |
30 }; | 30 }; |
31 | 31 |
32 NetworkSimulator._testInProgress = false; | |
33 | |
32 NetworkSimulator.prototype.schedulePromise = function(promise) { | 34 NetworkSimulator.prototype.schedulePromise = function(promise) { |
33 this._pendingPromises.push(promise); | 35 this._pendingPromises.push(promise); |
34 return promise; | 36 return promise; |
35 }; | 37 }; |
36 | 38 |
37 NetworkSimulator.prototype.resolvePromises = function() { | 39 NetworkSimulator.prototype.resolvePromises = function() { |
38 var self = this; | 40 var self = this; |
39 return new Promise(function(resolve, reject) { | 41 return new Promise(function(resolve, reject) { |
40 var pendingPromises = self._pendingPromises; | 42 var pendingPromises = self._pendingPromises; |
41 self._pendingPromises = []; | 43 self._pendingPromises = []; |
42 function all_resolved (results) { | 44 function all_resolved(results) { |
abarth-chromium
2014/07/20 16:56:17
allResolved ?
| |
43 if (self._pendingPromises.length) { | 45 if (self._pendingPromises.length) { |
44 resolve(self.resolvePromises()); | 46 resolve(self.resolvePromises()); |
45 return; | 47 return; |
46 } | 48 } |
47 resolve(results); | 49 resolve(results); |
48 } | 50 } |
49 Promise.all(pendingPromises).then(all_resolved, all_resolved); | 51 Promise.all(pendingPromises).then(all_resolved, all_resolved); |
50 }); | 52 }); |
51 }; | 53 }; |
52 | 54 |
53 NetworkSimulator.prototype.runTest = function(testCase) | 55 NetworkSimulator.prototype.runTest = function(testCase) |
54 { | 56 { |
57 if (NetworkSimulator._testInProgress) { | |
58 ok(false, "runTest calls cannot be nested"); | |
59 start(); | |
60 return; | |
61 } | |
62 | |
63 NetworkSimulator._testInProgress = true; | |
64 | |
55 var self = this; | 65 var self = this; |
56 var realNet = window.net; | |
57 return new Promise(function(resolve, reject) { | 66 return new Promise(function(resolve, reject) { |
67 var realNet = window.net; | |
68 | |
69 function reset() { | |
70 window.net = realNet; | |
71 NetworkSimulator._testInProgress = false; | |
72 } | |
58 | 73 |
59 // All net.* methods should return promises. This watches all | 74 // All net.* methods should return promises. This watches all |
60 // promises generated by test-overridden methods. | 75 // promises generated by test-overridden methods. |
61 window.net = {}; | 76 window.net = {}; |
62 ['probe', 'jsonp', 'get', 'post', | 77 ['probe', 'jsonp', 'get', 'post', |
63 'ajax', 'json', 'xml'].forEach(function(method) { | 78 'ajax', 'json', 'xml'].forEach(function(method) { |
64 if (method in self) { | 79 if (method in self) { |
65 net[method] = function() { | 80 net[method] = function() { |
66 return self.schedulePromise(self[method].apply(self, argume nts)); | 81 return self.schedulePromise(self[method].apply(self, argumen ts)); |
67 }; | 82 }; |
68 }; | 83 }; |
69 }); | 84 }); |
70 | 85 |
71 testCase(); | 86 try { |
87 testCase(); | |
88 } catch(e) { | |
89 // Make sure errors thrown in the test case don't leave window.net i n a bad state. | |
90 reset(); | |
91 ok(false, "Test case threw an error:" + e); | |
92 } | |
93 | |
72 self.resolvePromises().then(function() { | 94 self.resolvePromises().then(function() { |
73 window.net = realNet; | 95 reset(); |
74 equal(window.net, realNet); | 96 equal(window.net, realNet); |
75 resolve(); | 97 resolve(); |
76 }).catch(function(e) { | 98 }).catch(function(e) { |
99 reset(); | |
77 ok(false, "Failed to finish test: " + e); | 100 ok(false, "Failed to finish test: " + e); |
78 }); | 101 }); |
79 }); | 102 }); |
80 }; | 103 }; |
81 | 104 |
82 (function () { | 105 (function () { |
83 | 106 |
84 module("net"); | 107 module("net"); |
85 | 108 |
86 test("parseJSONP", 6, function() { | 109 test("parseJSONP", 6, function() { |
87 deepEqual(net._parseJSONP(""), {}); | 110 deepEqual(net._parseJSONP(""), {}); |
88 deepEqual(net._parseJSONP('p({"key": "value"})'), {"key": "value"}); | 111 deepEqual(net._parseJSONP('p({"key": "value"})'), {"key": "value"}); |
89 deepEqual(net._parseJSONP('ADD_RESULTS({"dummy":"data"});'), {"dummy":"data" }); | 112 deepEqual(net._parseJSONP('ADD_RESULTS({"dummy":"data"});'), {"dummy":"data" }); |
90 deepEqual(net._parseJSONP('{"dummy":"data"}'), {"dummy":"data"}); | 113 deepEqual(net._parseJSONP('{"dummy":"data"}'), {"dummy":"data"}); |
91 deepEqual(net._parseJSONP('ADD_RESULTS({"builder(1)":"data"});'), {"builder( 1)":"data"}); | 114 deepEqual(net._parseJSONP('ADD_RESULTS({"builder(1)":"data"});'), {"builder( 1)":"data"}); |
92 deepEqual(net._parseJSONP('{"builder(1)":"data"}'), {"builder(1)":"data"}); | 115 deepEqual(net._parseJSONP('{"builder(1)":"data"}'), {"builder(1)":"data"}); |
93 }); | 116 }); |
94 | 117 |
95 })(); | 118 })(); |
OLD | NEW |