OLD | NEW |
---|---|
1 <!-- | 1 <!-- |
2 Copyright 2014 The Chromium Authors. All rights reserved. | 2 Copyright 2014 The Chromium Authors. All rights reserved. |
3 Use of this source code is governed by a BSD-style license that can be | 3 Use of this source code is governed by a BSD-style license that can be |
4 found in the LICENSE file. | 4 found in the LICENSE file. |
5 --> | 5 --> |
6 | 6 |
7 <script> | 7 <script> |
8 function NetworkSimulator(assert, done) { | 8 function NetworkSimulator(assert, done) { |
9 this._assert = assert; | 9 this._assert = assert; |
10 this._done = done; | 10 this._done = done; |
(...skipping 26 matching lines...) Expand all Loading... | |
37 NetworkSimulator.prototype.runTest = function(testCase) { | 37 NetworkSimulator.prototype.runTest = function(testCase) { |
38 if (NetworkSimulator._testInProgress) { | 38 if (NetworkSimulator._testInProgress) { |
39 this._assert(false, "runTest calls cannot be nested"); | 39 this._assert(false, "runTest calls cannot be nested"); |
40 this._done(); | 40 this._done(); |
41 return; | 41 return; |
42 } | 42 } |
43 | 43 |
44 NetworkSimulator._testInProgress = true; | 44 NetworkSimulator._testInProgress = true; |
45 | 45 |
46 var self = this; | 46 var self = this; |
47 return new Promise(function(resolve, reject) { | 47 var realNet = window.net; |
Jeffrey Yasskin
2014/09/03 22:11:50
I think it's an antipattern to use the resolve/rej
| |
48 var realNet = window.net; | |
49 | 48 |
50 function reset() { | 49 function reset() { |
51 window.net = realNet; | 50 window.net = realNet; |
52 NetworkSimulator._testInProgress = false; | 51 NetworkSimulator._testInProgress = false; |
53 } | 52 } |
54 | 53 |
54 return Promise.resolve().then(function() { | |
55 // All net.* methods should return promises. This watches all | 55 // All net.* methods should return promises. This watches all |
56 // promises generated by test-overridden methods. | 56 // promises generated by test-overridden methods. |
57 window.net = {}; | 57 window.net = {}; |
58 ['probe', 'jsonp', 'get', 'post', | 58 ['probe', 'jsonp', 'get', 'post', |
59 'ajax', 'json', 'xml'].forEach(function(method) { | 59 'ajax', 'json', 'xml'].forEach(function(method) { |
60 if (method in self) { | 60 if (method in self) { |
61 net[method] = function() { | 61 net[method] = function() { |
62 return self.schedulePromise(self[method].apply(self, arguments)); | 62 return self.schedulePromise(self[method].apply(self, arguments)); |
63 }; | 63 }; |
64 }; | 64 }; |
65 }); | 65 }); |
66 | 66 }).then(function() { |
67 try { | 67 return testCase(); |
68 testCase(); | 68 }).catch(function(e) { |
69 } catch(e) { | 69 // Make sure errors thrown in the test case don't leave window.net in a bad state. |
70 // Make sure errors thrown in the test case don't leave window.net in a ba d state. | 70 reset(); |
71 reset(); | 71 throw e; |
72 self._assert(false, "Test case threw an error:" + e); | 72 }).then(function() { |
73 } | 73 return self.resolvePromises().then(function() { |
Jeffrey Yasskin
2014/09/03 22:11:50
I'm not sure we need the resolvePromises() step an
| |
74 | |
75 self.resolvePromises().then(function() { | |
76 reset(); | 74 reset(); |
77 self._assert(window.net == realNet); | 75 self._assert(window.net == realNet); |
78 resolve(); | |
79 }).catch(function(e) { | 76 }).catch(function(e) { |
80 reset(); | 77 reset(); |
81 self._assert(false, "Failed to finish test: " + e); | 78 self._assert(false, "Failed to finish test:" + e); |
Jeffrey Yasskin
2014/09/03 22:11:50
I'm not sure if we want to throw a string here or
ojan
2014/09/04 18:26:53
I think this is fine for now. Or, we could just do
Jeffrey Yasskin
2014/09/04 18:48:11
Two _assert calls doesn't help: Chai uses exceptio
| |
82 }); | 79 }) |
83 }); | 80 }).then(this._done, this._done); |
Jeffrey Yasskin
2014/09/03 22:11:50
Mocha actually supports returning promises from th
ojan
2014/09/04 18:26:53
sgtm
Jeffrey Yasskin
2014/09/04 18:48:11
After experimenting a bit, it looks safer to keep
| |
84 }; | 81 }; |
85 </script> | 82 </script> |
OLD | NEW |