gecko-dev/dom/console/tests/test_timer.html

117 строки
2.7 KiB
HTML
Исходник Обычный вид История

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test for timeStart/timeLog/timeEnd in console</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="application/javascript">
SimpleTest.waitForExplicitFinish();
var reduceTimePrecisionPrevPrefValue = SpecialPowers.getBoolPref("privacy.reduceTimerPrecision");
SpecialPowers.setBoolPref("privacy.reduceTimerPrecision", false);
function ConsoleListener() {
SpecialPowers.addObserver(this, "console-api-log-event");
}
ConsoleListener.prototype = {
observe(aSubject, aTopic, aData) {
let obj = aSubject.wrappedJSObject;
if (obj.arguments[0] != 'test') {
return;
}
if (!this._cb) {
ok(false, "Callback not set!");
return;
}
if (!this._cb(obj)) {
return;
}
this._cb = null;
this._resolve();
},
shutdown() {
SpecialPowers.removeObserver(this, "console-api-log-event");
},
waitFor(cb) {
return new Promise(resolve => {
this._cb = cb;
this._resolve = resolve;
});
},
};
let listener = new ConsoleListener();
// Timer creation:
async function runTest() {
let cl = listener.waitFor(obj => {
return ("timer" in obj) &&
("name" in obj.timer) &&
obj.timer.name == 'test';
});
console.time('test');
await cl;
ok(true, "Console.time received!");
// Timer check:
cl = listener.waitFor(obj => {
return ("timer" in obj) &&
("name" in obj.timer) &&
obj.timer.name == 'test' &&
("duration" in obj.timer) &&
obj.timer.duration > 0 &&
obj.arguments[1] == 1 &&
obj.arguments[2] == 2 &&
obj.arguments[3] == 3 &&
obj.arguments[4] == 4;
});
console.timeLog('test', 1, 2, 3, 4);
await cl;
ok(true, "Console.timeLog received!");
// Time deleted:
cl = listener.waitFor(obj => {
return ("timer" in obj) &&
("name" in obj.timer) &&
obj.timer.name == 'test' &&
("duration" in obj.timer) &&
obj.timer.duration > 0;
});
console.timeEnd('test');
await cl;
ok(true, "Console.timeEnd received!");
// Here an error:
cl = listener.waitFor(obj => {
return ("timer" in obj) &&
("name" in obj.timer) &&
obj.timer.name == 'test' &&
("error" in obj.timer);
});
console.timeLog('test');
await cl;
ok(true, "Console.time with error received!");
}
runTest().then(() => {
listener.shutdown();
SpecialPowers.setBoolPref("privacy.reduceTimerPrecision", reduceTimePrecisionPrevPrefValue);
SimpleTest.finish();
});
</script>
</body>
</html>