blob: f8d6d2d82c852d8a53d89e87d3aea6d4f72b71e0 [file] [log] [blame]
<!doctype html>
<html>
<head>
<script src="../../http/tests/inspector/resources/inspector-test.js"></script>
<script>
function test()
{
let suite = InspectorTest.createAsyncSuite("Throttle");
suite.addTestCase({
name: "Basic throttle",
test(resolve, reject) {
const delay = 50;
const duration = 110;
const maximumNumberOfCalls = Math.ceil(duration / delay) + 1; // Add one for the leading edge. Math.ceil accounts for the trailing edge.
let lastCallTime = NaN;
let numberOfCalls = 0;
let object = {
test(isTrailing) {
let now = Date.now();
if (++numberOfCalls > maximumNumberOfCalls) {
reject("Throttled function called too many times.");
return;
}
if (!isNaN(lastCallTime)) {
let actualDelay = now - lastCallTime;
if (actualDelay < delay) {
InspectorTest.fail(`Delay should be at least ${delay} ms. Was ${actualDelay} ms.`);
reject();
return;
}
}
lastCallTime = now;
if (isTrailing) {
InspectorTest.pass(`All calls delayed at least ${delay} ms.`);
resolve();
}
}
};
InspectorTest.log(`Call throttled function every 1 ms for ${duration} ms.`);
let throttleProxy = object.throttle(delay);
throttleProxy.test();
for (let i = 1; i <= duration; i++)
setTimeout(() => { throttleProxy.test(i === duration); }, i);
}
});
suite.addTestCase({
name: "Throttle proxy uniqueness",
test(resolve, reject) {
let object = {};
let firstProxy = object.throttle(1);
let secondProxy = object.throttle(1);
InspectorTest.expectNotEqual(firstProxy, secondProxy, "Each call to throttle should return a new proxy.");
resolve();
}
});
suite.addTestCase({
name: "Throttled function arguments",
test(resolve, reject) {
let values = [1, 2, 3];
let isLeadingCall = true;
let object = {
test(value) {
if (isLeadingCall) {
isLeadingCall = false;
return;
}
let expected = values.lastValue;
InspectorTest.expectEqual(value, expected, "Trailing call invoked with most recent arguments.");
resolve();
}
};
let i = 0;
let throttleProxy = object.throttle(50);
for (let value of values)
setTimeout(() => { throttleProxy.test(value); }, i++);
}
});
suite.addTestCase({
name: "Cancel throttle",
test(resolve, reject) {
let canceled = false;
let object = {
test() {
if (!canceled)
return;
InspectorTest.fail("Called throttled function after cancel.");
reject();
}
};
let throttleProxy = object.throttle(20);
throttleProxy.test();
throttleProxy.test();
object.test.cancelThrottle();
canceled = true;
InspectorTest.log("Canceled throttled function.");
setTimeout(() => {
InspectorTest.pass("Throttled function canceled.");
resolve();
}, 50);
}
});
suite.runTestCasesAndFinish();
}
</script>
</head>
<body onload="runTest()">
<p>Test throttle proxy support.</p>
</body>
</html>