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

Side by Side Diff: extensions/test/data/unit_test_environment_specific_bindings.js

Issue 509813002: Implement the client side of Serial I/O on data pipe. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@serial-io
Patch Set: address comments Created 6 years, 3 months 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 var nativesPromise = requireAsync('testNatives'); 5 var nativesPromise = requireAsync('testNatives');
6 var sendRequestNatives = requireNative('sendRequest');
6 7
7 function registerHooks(api) { 8 function registerHooks(api) {
8 var chromeTest = api.compiledApi; 9 var chromeTest = api.compiledApi;
9 var apiFunctions = api.apiFunctions; 10 var apiFunctions = api.apiFunctions;
10 11
11 apiFunctions.setHandleRequest('notifyPass', function() { 12 apiFunctions.setHandleRequest('notifyPass', function() {
12 nativesPromise.then(function(natives) { 13 nativesPromise.then(function(natives) {
13 natives.NotifyPass(); 14 natives.NotifyPass();
14 }); 15 });
15 }); 16 });
(...skipping 18 matching lines...) Expand all
34 Promise.resolve().then(function() { 35 Promise.resolve().then(function() {
35 runNextTest(); 36 runNextTest();
36 }); 37 });
37 } 38 }
38 39
39 function exportTests(tests, runTests, exports) { 40 function exportTests(tests, runTests, exports) {
40 $Array.forEach(tests, function(test) { 41 $Array.forEach(tests, function(test) {
41 exports[test.name] = function() { 42 exports[test.name] = function() {
42 runTests([test]); 43 runTests([test]);
43 return true; 44 return true;
44 } 45 };
45 }); 46 });
46 } 47 }
47 48
49 /**
50 * A fake implementation of setTimeout and clearTimeout.
51 * @constructor
52 */
53 function TimeoutManager() {
54 this.timeouts_ = {};
55 this.nextTimeoutId_ = 0;
56 this.currentTime = 0;
57 this.autorunEnabled_ = false;
58 }
59
60 /**
61 * Installs setTimeout and clearTimeout into the global object.
62 */
63 TimeoutManager.prototype.installGlobals = function() {
64 var global = sendRequestNatives.GetGlobal({});
65 global.setTimeout = this.setTimeout_.bind(this);
66 global.clearTimeout = this.clearTimeout_.bind(this);
67 };
68
69 /**
70 * Enables auto-running of timeout callbacks. Until disabled by a call to
71 * disableAutorun, any timeout callbacks set by calls to setTimeout (including
72 * before the call to enableAutorun) will cause the currentTime to be advanced
73 * to the time of the timeout.
74 */
75 TimeoutManager.prototype.enableAutorun = function() {
76 this.autorunEnabled_ = true;
77 Promise.resolve().then(this.autoRun_.bind(this));
78 };
79
80 /**
81 * Disables auto-running of timeout callbacks.
82 */
83 TimeoutManager.prototype.disableAutorun = function() {
84 this.autorunEnabled_ = false;
85 };
86
87 /**
88 * Runs timeout callbacks with earliest timeout.
89 * @private
90 */
91 TimeoutManager.prototype.autoRun_ = function() {
92 if (!this.autorunEnabled_ || $Object.keys(this.timeouts_).length == 0)
93 return;
94
95 // Bucket the timeouts by their timeout time.
96 var timeoutsByTimeout = {};
97 var timeoutIds = $Object.keys(this.timeouts_);
98 for (var i = 0; i < timeoutIds.length; i++) {
99 var timeout = this.timeouts_[timeoutIds[i]];
100 var timeMs = timeout.timeMs;
101 if (!timeoutsByTimeout[timeMs])
102 timeoutsByTimeout[timeMs] = [];
103 timeoutsByTimeout[timeMs].push(timeout);
104 }
105 this.currentTime =
106 $Function.apply(Math.min, null, $Object.keys((timeoutsByTimeout)));
107 // Run all timeouts in the earliest timeout bucket.
108 var timeouts = timeoutsByTimeout[this.currentTime];
109 for (var i = 0; i < timeouts.length; i++) {
110 var currentTimeout = timeouts[i];
111 if (!this.timeouts_[currentTimeout.id])
112 continue;
113 delete this.timeouts_[currentTimeout.id];
114 try {
115 currentTimeout.target();
116 } catch (e) {
117 console.log('error calling timeout target ' + e.stack);
118 }
119 }
120 // Continue running any later callbacks.
121 Promise.resolve().then(this.autoRun_.bind(this));
122 };
123
124 /**
125 * A fake implementation of setTimeout. This does not support passing callback
126 * arguments.
127 * @private
128 */
129 TimeoutManager.prototype.setTimeout_ = function(target, timeoutMs) {
130 var timeoutId = this.nextTimeoutId_++;
131 this.timeouts_[timeoutId] = {
132 id: timeoutId,
133 target: target,
134 timeMs: timeoutMs + this.currentTime,
135 };
136 if (this.autorunEnabled_)
137 Promise.resolve().then(this.autoRun_.bind(this));
138 return timeoutId;
139 };
140
141 /**
142 * A fake implementation of clearTimeout.
143 * @private
144 */
145 TimeoutManager.prototype.clearTimeout_ = function(timeoutId) {
146 if (this.timeouts_[timeoutId])
147 delete this.timeouts_[timeoutId];
148 };
149
48 exports.registerHooks = registerHooks; 150 exports.registerHooks = registerHooks;
49 exports.testDone = testDone; 151 exports.testDone = testDone;
50 exports.exportTests = exportTests; 152 exports.exportTests = exportTests;
153 exports.TimeoutManager = TimeoutManager;
OLDNEW
« extensions/test/data/serial_unittest.js ('K') | « extensions/test/data/serial_unittest.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698