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 CTFailureGroup(key, failures, annotation) { | 8 function NetworkSimulator(assert, done) { |
9 this.key = key; | 9 this._assert = assert; |
10 this.failures = failures; | 10 this._done = done; |
11 this.annotation = annotation || {}; | 11 this._pendingPromises = []; |
12 this._computeProperties(); | |
13 } | 12 } |
14 | 13 |
15 CTFailureGroup.prototype.snoozeUntil = function(time) { | 14 NetworkSimulator._testInProgress = false; |
16 return this._annotate({ | 15 |
17 snoozeTime: time, | 16 NetworkSimulator.prototype.schedulePromise = function(promise) { |
| 17 this._pendingPromises.push(promise); |
| 18 return promise; |
| 19 }; |
| 20 |
| 21 NetworkSimulator.prototype.resolvePromises = function() { |
| 22 var self = this; |
| 23 return new Promise(function(resolve, reject) { |
| 24 var pendingPromises = self._pendingPromises; |
| 25 self._pendingPromises = []; |
| 26 function allResolved(results) { |
| 27 if (self._pendingPromises.length) { |
| 28 resolve(self.resolvePromises()); |
| 29 return; |
| 30 } |
| 31 resolve(results); |
| 32 } |
| 33 Promise.all(pendingPromises).then(allResolved, allResolved); |
18 }); | 34 }); |
19 }; | 35 }; |
20 | 36 |
21 CTFailureGroup.prototype.unsnooze = function() { | 37 NetworkSimulator.prototype.runTest = function(testCase) { |
22 return this._annotate({ | 38 if (NetworkSimulator._testInProgress) { |
23 snoozeTime: undefined, | 39 this._assert(false, "runTest calls cannot be nested"); |
| 40 this._done(); |
| 41 return; |
| 42 } |
| 43 |
| 44 NetworkSimulator._testInProgress = true; |
| 45 |
| 46 var self = this; |
| 47 return new Promise(function(resolve, reject) { |
| 48 var realNet = window.net; |
| 49 |
| 50 function reset() { |
| 51 window.net = realNet; |
| 52 NetworkSimulator._testInProgress = false; |
| 53 } |
| 54 |
| 55 // All net.* methods should return promises. This watches all |
| 56 // promises generated by test-overridden methods. |
| 57 window.net = {}; |
| 58 ['probe', 'jsonp', 'get', 'post', |
| 59 'ajax', 'json', 'xml'].forEach(function(method) { |
| 60 if (method in self) { |
| 61 net[method] = function() { |
| 62 return self.schedulePromise(self[method].apply(self, arguments)); |
| 63 }; |
| 64 }; |
| 65 }); |
| 66 |
| 67 try { |
| 68 testCase(); |
| 69 } catch(e) { |
| 70 // Make sure errors thrown in the test case don't leave window.net in a ba
d state. |
| 71 reset(); |
| 72 self._assert(false, "Test case threw an error:" + e); |
| 73 } |
| 74 |
| 75 self.resolvePromises().then(function() { |
| 76 reset(); |
| 77 self._assert(window.net == realNet); |
| 78 resolve(); |
| 79 }).catch(function(e) { |
| 80 reset(); |
| 81 self._assert(false, "Failed to finish test: " + e); |
| 82 }); |
24 }); | 83 }); |
25 }; | 84 }; |
26 | |
27 CTFailureGroup.prototype._computeProperties = function() { | |
28 this.isSnoozed = Date.now() < this.annotation.snoozeTime; | |
29 }; | |
30 | |
31 CTFailureGroup.prototype._annotate = function(newAnnotation) { | |
32 // FIXME: Post the new annotation to frontend rather than storing locally. | |
33 return CTFailureGroup.fetchAnnotations().then(function(annotations) { | |
34 var annotation = annotations[this.key] || {}; | |
35 | |
36 Object.keys(newAnnotation, function(key, value) { | |
37 if (value === 'undefined') { | |
38 delete annotation[key]; | |
39 } else { | |
40 annotation[key] = value; | |
41 } | |
42 }); | |
43 | |
44 if (Object.size(annotation) == 0) { | |
45 delete annotations[this.key]; | |
46 } else { | |
47 annotations[this.key] = annotation; | |
48 } | |
49 | |
50 localStorage.CTFailureGroupAnnotations = JSON.stringify(annotations); | |
51 | |
52 this.annotation = annotation; | |
53 this._computeProperties(); | |
54 }.bind(this)); | |
55 }; | |
56 | |
57 CTFailureGroup.fetchAnnotations = function() { | |
58 // FIXME: Fetch annotations from frontend. | |
59 var stored = localStorage.CTFailureGroupAnnotations; | |
60 var annotations = stored ? JSON.parse(stored) : {}; | |
61 return Promise.resolve(annotations); | |
62 }; | |
63 </script> | 85 </script> |
OLD | NEW |