| 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 |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 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. | |
| 27 function NetworkSimulator() | |
| 28 { | |
| 29 this._pendingPromises = []; | |
| 30 }; | |
| 31 | |
| 32 NetworkSimulator._testInProgress = false; | |
| 33 | |
| 34 NetworkSimulator.prototype.schedulePromise = function(promise) { | |
| 35 this._pendingPromises.push(promise); | |
| 36 return promise; | |
| 37 }; | |
| 38 | |
| 39 NetworkSimulator.prototype.resolvePromises = function() { | |
| 40 var self = this; | |
| 41 return new Promise(function(resolve, reject) { | |
| 42 var pendingPromises = self._pendingPromises; | |
| 43 self._pendingPromises = []; | |
| 44 function allResolved(results) { | |
| 45 if (self._pendingPromises.length) { | |
| 46 resolve(self.resolvePromises()); | |
| 47 return; | |
| 48 } | |
| 49 resolve(results); | |
| 50 } | |
| 51 Promise.all(pendingPromises).then(allResolved, allResolved); | |
| 52 }); | |
| 53 }; | |
| 54 | |
| 55 NetworkSimulator.prototype.runTest = function(testCase) | |
| 56 { | |
| 57 if (NetworkSimulator._testInProgress) { | |
| 58 ok(false, "runTest calls cannot be nested"); | |
| 59 start(); | |
| 60 return; | |
| 61 } | |
| 62 | |
| 63 NetworkSimulator._testInProgress = true; | |
| 64 | |
| 65 var self = this; | |
| 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 } | |
| 73 | |
| 74 // All net.* methods should return promises. This watches all | |
| 75 // promises generated by test-overridden methods. | |
| 76 window.net = {}; | |
| 77 ['probe', 'jsonp', 'get', 'post', | |
| 78 'ajax', 'json', 'xml'].forEach(function(method) { | |
| 79 if (method in self) { | |
| 80 net[method] = function() { | |
| 81 return self.schedulePromise(self[method].apply(self, argumen
ts)); | |
| 82 }; | |
| 83 }; | |
| 84 }); | |
| 85 | |
| 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 | |
| 94 self.resolvePromises().then(function() { | |
| 95 reset(); | |
| 96 equal(window.net, realNet); | |
| 97 resolve(); | |
| 98 }).catch(function(e) { | |
| 99 reset(); | |
| 100 ok(false, "Failed to finish test: " + e); | |
| 101 }); | |
| 102 }); | |
| 103 }; | |
| 104 | |
| 105 (function () { | 26 (function () { |
| 106 | 27 |
| 107 module("net"); | 28 module("net"); |
| 108 | 29 |
| 109 test("parseJSONP", 6, function() { | 30 test("parseJSONP", 6, function() { |
| 110 deepEqual(net._parseJSONP(""), {}); | 31 deepEqual(net._parseJSONP(""), {}); |
| 111 deepEqual(net._parseJSONP('p({"key": "value"})'), {"key": "value"}); | 32 deepEqual(net._parseJSONP('p({"key": "value"})'), {"key": "value"}); |
| 112 deepEqual(net._parseJSONP('ADD_RESULTS({"dummy":"data"});'), {"dummy":"data"
}); | 33 deepEqual(net._parseJSONP('ADD_RESULTS({"dummy":"data"});'), {"dummy":"data"
}); |
| 113 deepEqual(net._parseJSONP('{"dummy":"data"}'), {"dummy":"data"}); | 34 deepEqual(net._parseJSONP('{"dummy":"data"}'), {"dummy":"data"}); |
| 114 deepEqual(net._parseJSONP('ADD_RESULTS({"builder(1)":"data"});'), {"builder(
1)":"data"}); | 35 deepEqual(net._parseJSONP('ADD_RESULTS({"builder(1)":"data"});'), {"builder(
1)":"data"}); |
| 115 deepEqual(net._parseJSONP('{"builder(1)":"data"}'), {"builder(1)":"data"}); | 36 deepEqual(net._parseJSONP('{"builder(1)":"data"}'), {"builder(1)":"data"}); |
| 116 }); | 37 }); |
| 117 | 38 |
| 118 })(); | 39 })(); |
| OLD | NEW |