Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(626)

Side by Side Diff: appengine_apps/trooper_o_matic/lib/network-simulator.html

Issue 774323002: Moved trooper_o_matic to appengine/ (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 <!--
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
4 found in the LICENSE file.
5 -->
6
7 <script>
8 // TODO: Move sheriff-o-matic to this repository and merge the
9 // network-simulator library.
10
11 function NetworkSimulator(assert, done) {
12 this._assert = assert;
13 this._done = done;
14 this._pendingPromises = [];
15 }
16
17 NetworkSimulator._testInProgress = false;
18
19 NetworkSimulator.prototype.schedulePromise = function(promise) {
20 this._pendingPromises.push(promise);
21 return promise;
22 };
23
24 NetworkSimulator.prototype.resolvePromises = function() {
25 var self = this;
26 return new Promise(function(resolve, reject) {
27 var pendingPromises = self._pendingPromises;
28 self._pendingPromises = [];
29 function allResolved(results) {
30 if (self._pendingPromises.length) {
31 resolve(self.resolvePromises());
32 return;
33 }
34 resolve(results);
35 }
36 Promise.all(pendingPromises).then(allResolved, allResolved);
37 });
38 };
39
40 NetworkSimulator.prototype.runTest = function(testCase) {
41 if (NetworkSimulator._testInProgress) {
42 this._assert(false, "runTest calls cannot be nested");
43 this._done();
44 return;
45 }
46
47 NetworkSimulator._testInProgress = true;
48
49 var self = this;
50 var realNet = window.net;
51
52 function reset() {
53 window.net = realNet;
54 NetworkSimulator._testInProgress = false;
55 }
56
57 return Promise.resolve().then(function() {
58 // All net.* methods should return promises. This watches all
59 // promises generated by test-overridden methods.
60 window.net = {};
61 ['probe', 'jsonp', 'get', 'post',
62 'ajax', 'json', 'xml'].forEach(function(method) {
63 if (method in self) {
64 net[method] = function() {
65 return self.schedulePromise(self[method].apply(self, arguments));
66 };
67 };
68 });
69 }).then(function() {
70 return testCase();
71 }).catch(function(e) {
72 // Make sure errors thrown in the test case don't leave window.net in a bad state.
73 reset();
74 throw e;
75 }).then(function() {
76 return self.resolvePromises().then(function() {
77 reset();
78 self._assert(window.net == realNet);
79 }).catch(function(e) {
80 reset();
81 self._assert(false, "Failed to finish test:" + e);
82 })
83 }).then(this._done, this._done);
84 };
85 </script>
OLDNEW
« no previous file with comments | « appengine_apps/trooper_o_matic/lib/net.html ('k') | appengine_apps/trooper_o_matic/lib/sugar.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698