Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / inspector / throttler.html
blob35b0a9841845de17dfe7a26bbbadd4c026ed490b
1 <html>
2 <head>
3 <script src="../http/tests/inspector/inspector-test.js"></script>
4 <script>
6 function test()
8 var ProcessMock = function(name, runnable)
10 this._runnable = runnable;
11 this.processName = name;
12 this.run = this.run.bind(this);
13 this.run.processName = name;
15 this.startPromise = new Promise(onStart.bind(this));
16 this.finishPromise = new Promise(onFinish.bind(this));
18 function onStart(startCallback)
20 this._startCallback = startCallback;
23 function onFinish(finishCallback)
25 this._finishCallback = finishCallback;
29 ProcessMock.create = function(name, runnable)
31 return new ProcessMock(name, runnable);
34 ProcessMock.prototype = {
35 run: function()
37 InspectorTest.addResult("Process '" + this.processName + "' STARTED.");
38 this._startCallback();
39 if (this._runnable)
40 this._runnable.call(null);
41 return this.finishPromise;
44 finish: function()
46 this.startPromise.then(onFinish.bind(this));
48 function onFinish()
50 InspectorTest.addResult("Process '" + this.processName + "' FINISHED.");
51 this._finishCallback();
56 var throttler = new WebInspector.Throttler(1989);
57 var timeoutMock = new InspectorTest.TimeoutMock();
58 throttler._setTimeout = timeoutMock.setTimeout;
59 throttler._clearTimeout = timeoutMock.clearTimeout;
60 InspectorTest.addSniffer(throttler, "schedule", logSchedule, true);
62 function testSimpleSchedule(next, runningProcess)
64 assertThrottlerIdle();
65 throttler.schedule(ProcessMock.create("operation #1").run, false);
66 var process = ProcessMock.create("operation #2");
67 throttler.schedule(process.run);
69 var promise = Promise.resolve();
70 if (runningProcess) {
71 runningProcess.finish();
72 promise = waitForProcessFinish();
75 promise.then(function() {
76 assertThrottlerTimeout();
77 timeoutMock.fireAllTimers();
78 process.finish();
79 return waitForProcessFinish();
80 }).then(next);
83 function testAsSoonAsPossibleOverrideTimeout(next, runningProcess)
85 assertThrottlerIdle();
86 throttler.schedule(ProcessMock.create("operation #1").run);
87 var process = ProcessMock.create("operation #2");
88 throttler.schedule(process.run, true);
90 var promise = Promise.resolve();
91 if (runningProcess) {
92 runningProcess.finish();
93 promise = waitForProcessFinish();
96 promise.then(function() {
97 assertThrottlerTimeout();
98 timeoutMock.fireAllTimers();
99 process.finish();
100 return waitForProcessFinish();
101 }).then(next);
104 function testAlwaysExecuteLastScheduled(next, runningProcess)
106 assertThrottlerIdle();
107 var process = null;
108 for (var i = 0; i < 4; ++i) {
109 process = ProcessMock.create("operation #" + i);
110 throttler.schedule(process.run, i % 2 === 0);
112 var promise = Promise.resolve();
113 if (runningProcess) {
114 runningProcess.finish();
115 promise = waitForProcessFinish();
117 promise.then(function() {
118 assertThrottlerTimeout();
119 timeoutMock.fireAllTimers();
120 process.finish();
121 return waitForProcessFinish();
122 }).then(next);
125 InspectorTest.runTestSuite([
126 testSimpleSchedule,
128 testAsSoonAsPossibleOverrideTimeout,
130 testAlwaysExecuteLastScheduled,
132 function testSimpleScheduleDuringProcess(next)
134 var runningProcess = throttlerToRunningState();
135 runningProcess.startPromise.then(function() {
136 testSimpleSchedule(next, runningProcess);
140 function testAsSoonAsPossibleOverrideDuringProcess(next)
142 var runningProcess = throttlerToRunningState();
143 runningProcess.startPromise.then(function() {
144 testAsSoonAsPossibleOverrideTimeout(next, runningProcess);
148 function testAlwaysExecuteLastScheduledDuringProcess(next)
150 var runningProcess = throttlerToRunningState();
151 runningProcess.startPromise.then(function() {
152 testAlwaysExecuteLastScheduled(next, runningProcess);
156 function testScheduleFromProcess(next)
158 var nextProcess;
159 assertThrottlerIdle();
160 var process = ProcessMock.create("operation #1", processBody);
161 throttler.schedule(process.run);
162 assertThrottlerTimeout();
163 timeoutMock.fireAllTimers();
164 process.finish();
165 waitForProcessFinish().then(function() {
166 assertThrottlerTimeout();
167 timeoutMock.fireAllTimers();
168 nextProcess.finish();
169 return waitForProcessFinish();
170 }).then(next);
172 function processBody()
174 nextProcess = ProcessMock.create("operation #2");
175 throttler.schedule(nextProcess.run, false);
179 function testExceptionFromProcess(next)
181 var process = ProcessMock.create("operation #1", processBody);
182 throttler.schedule(process.run);
183 timeoutMock.fireAllTimers();
184 waitForProcessFinish().then(function() {
185 assertThrottlerIdle();
186 next();
189 function processBody()
191 throw new Error("Exception during process execution.");
196 function waitForProcessFinish()
198 var promiseResolve;
199 var hasFinished;
200 InspectorTest.addSniffer(WebInspector.Throttler.prototype, "_processCompletedForTests", onFinished);
201 function onFinished()
203 hasFinished = true;
204 if (promiseResolve)
205 promiseResolve();
207 return new Promise(function(success) {
208 promiseResolve = success;
209 if (hasFinished)
210 success();
214 function throttlerToRunningState()
216 assertThrottlerIdle();
217 var process = ProcessMock.create("long operation");
218 throttler.schedule(process.run);
219 assertThrottlerTimeout();
220 timeoutMock.fireAllTimers();
221 return process;
224 function assertThrottlerIdle()
226 var timeouts = timeoutMock.activeTimersTimeouts();
227 if (timeouts.length !== 0) {
228 InspectorTest.addResult("ERROR: throttler is not in idle state. Scheduled timers timeouts: [" + timeouts.sort().join(", ") + "]");
229 InspectorTest.completeTest();
230 return;
232 InspectorTest.addResult("Throttler is in IDLE state (doesn't have any timers set up)");
235 function assertThrottlerTimeout()
237 var timeouts = timeoutMock.activeTimersTimeouts();
238 if (timeouts.length === 0) {
239 InspectorTest.addResult("ERROR: throttler is not in timeout state. Scheduled timers timeouts are empty!");
240 InspectorTest.completeTest();
241 return;
243 InspectorTest.addResult("Throttler is in TIMEOUT state. Scheduled timers timeouts: [" + timeouts.sort().join(", ") + "]");
246 function logSchedule(operation, asSoonAsPossible)
248 InspectorTest.addResult("SCHEDULED: '" + operation.processName + "' asSoonAsPossible: " + asSoonAsPossible);
252 </script>
253 </head>
255 <body onload="runTest()">
256 <p>This test verifies throttler behavior.</p>
257 </body>
258 </html>