Commit 0ea7bb10 authored by Ken Rockot's avatar Ken Rockot Committed by Chromium LUCI CQ

Migrate battery status API web tests to testharness

This moves them from js-test to testharness and does some substantial
cleanup of asynchronous logic at the same time.

This is in preparation to move the tests into wpt_internal and use Mojo
JS modules.

Bug: 1004256
Change-Id: I47a53d3e88d777158c9ad1a65bf0500284abcb5a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2639871Reviewed-by: default avatarTim Volodine <timvolodine@chromium.org>
Commit-Queue: Ken Rockot <rockot@google.com>
Cr-Commit-Position: refs/heads/master@{#845999}
parent fb6d0dcd
Test basic API definitions.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
PASS promise is defined.
PASS promise.then is defined.
batteryStatusSuccess invoked
PASS battery is defined.
PASS battery is non-null.
PASS battery.charging is defined.
PASS battery.chargingTime is defined.
PASS battery.dischargingTime is defined.
PASS battery.level is defined.
PASS batteryInfo is defined.
PASS lastSetMockBatteryInfo is defined.
PASS batteryInfo.charging is lastSetMockBatteryInfo.charging
PASS batteryInfo.chargingTime is lastSetMockBatteryInfo.chargingTime
PASS batteryInfo.dischargingTime is lastSetMockBatteryInfo.dischargingTime
PASS batteryInfo.level is lastSetMockBatteryInfo.level
PASS typeof battery.onchargingchange == 'object' is true
PASS typeof battery.onchargingtimechange == 'object' is true
PASS typeof battery.ondischargingtimechange == 'object' is true
PASS typeof battery.onlevelchange == 'object' is true
PASS 'onchargingchange' in battery is true
PASS 'onchargingtimechange' in battery is true
PASS 'ondischargingtimechange' in battery is true
PASS 'onlevelchange' in battery is true
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE html>
<html>
<body>
<script src="../resources/js-test.js"></script>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<script src="file:///gen/services/device/public/mojom/battery_monitor.mojom.js"></script>
<script src="resources/mock-battery-monitor.js"></script>
<script>
description("Test basic API definitions.");
if (!window.testRunner)
debug('This test cannot be run without the TestRunner');
// Clean-up any unused battery manager objects from previous tests.
gc();
jsTestIsAsync = true;
testRunner.waitUntilDone();
var battery;
function batteryStatusSuccess(batteryManager) {
debug('batteryStatusSuccess invoked');
battery = batteryManager;
shouldBeDefined("battery");
shouldBeNonNull("battery");
shouldBeDefined("battery.charging");
shouldBeDefined("battery.chargingTime");
shouldBeDefined("battery.dischargingTime");
shouldBeDefined("battery.level");
testIfBatteryStatusIsUpToDate(batteryManager);
shouldBeTrue("typeof battery.onchargingchange == 'object'");
shouldBeTrue("typeof battery.onchargingtimechange == 'object'");
shouldBeTrue("typeof battery.ondischargingtimechange == 'object'");
shouldBeTrue("typeof battery.onlevelchange == 'object'");
shouldBeTrue("'onchargingchange' in battery");
shouldBeTrue("'onchargingtimechange' in battery");
shouldBeTrue("'ondischargingtimechange' in battery");
shouldBeTrue("'onlevelchange' in battery");
setTimeout(finishJSTest, 0);
}
var promise = navigator.getBattery();
shouldBeDefined("promise");
shouldBeDefined("promise.then");
promise.then(batteryStatusSuccess, batteryStatusFailure);
setAndFireMockBatteryInfo(false, 10, 20, 0.5);
promise_test(async _ => {
setAndFireMockBatteryInfo(false, 10, 20, 0.5);
const battery = await navigator.getBattery();
assert_not_equals(battery, undefined);
assert_equals(typeof battery.charging, 'boolean');
assert_equals(typeof battery.chargingTime, 'number');
assert_equals(typeof battery.dischargingTime, 'number');
assert_equals(typeof battery.level, 'number');
testIfBatteryStatusIsUpToDate(battery);
assert_equals(typeof battery.onchargingchange, 'object');
assert_equals(typeof battery.onchargingtimechange, 'object');
assert_equals(typeof battery.ondischargingtimechange, 'object');
assert_equals(typeof battery.onlevelchange, 'object');
}, 'verify basic getBattery API support');
</script>
</body>
</html>
Accessing navigator.getBattery() on a closed window.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
PASS typeof(nav.getBattery()) == 'object' is true
PASS nav.getBattery() is undefined.
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE HTML>
<body>
<head>
<script src="../resources/js-test.js"></script>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
</head>
<script>
description("Accessing navigator.getBattery() on a closed window.");
window.jsTestIsAsync = true;
function waitForMessage(data) {
return new Promise(
resolve => {
window.addEventListener('message', e => {
if (e.data == data) {
resolve();
}
}, {once: true});
});
}
var w;
var nav;
promise_test(async _ => {
const w = window.open('../resources/window-postmessage-open-close.html');
await waitForMessage('opened');
function processMessage(event) {
if (event.data == "opened") {
nav = w.navigator;
shouldBeTrue("typeof(nav.getBattery()) == 'object'");
w.close();
w = null;
} else if (event.data == "closed") {
shouldBeUndefined("nav.getBattery()");
finishJSTest();
}
}
const nav = w.navigator;
let result = nav.getBattery();
assert_equals(typeof result, 'object');
assert_equals(result.constructor, w.Promise);
if (window.testRunner) {
testRunner.dumpAsText();
testRunner.setCanOpenWindows();
}
w = window.open('../resources/window-postmessage-open-close.html');
window.addEventListener("message", processMessage, false);
w.close();
await waitForMessage('closed');
result = nav.getBattery();
assert_equals(result, undefined);
}, 'navigator.getBattery() does not operate on a closed window');
</script>
</body>
Test battery status API with multiple promises after resolve.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
resolution number 1
PASS batteryInfo is defined.
PASS lastSetMockBatteryInfo is defined.
PASS batteryInfo.charging is lastSetMockBatteryInfo.charging
PASS batteryInfo.chargingTime is lastSetMockBatteryInfo.chargingTime
PASS batteryInfo.dischargingTime is lastSetMockBatteryInfo.dischargingTime
PASS batteryInfo.level is lastSetMockBatteryInfo.level
PASS promise1 === promise2 is true
resolution number 2
PASS batteryInfo is defined.
PASS lastSetMockBatteryInfo is defined.
PASS batteryInfo.charging is lastSetMockBatteryInfo.charging
PASS batteryInfo.chargingTime is lastSetMockBatteryInfo.chargingTime
PASS batteryInfo.dischargingTime is lastSetMockBatteryInfo.dischargingTime
PASS batteryInfo.level is lastSetMockBatteryInfo.level
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE html>
<html>
<body>
<script src="../resources/js-test.js"></script>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<script src="file:///gen/services/device/public/mojom/battery_monitor.mojom.js"></script>
<script src="resources/mock-battery-monitor.js"></script>
<script>
description("Test battery status API with multiple promises after resolve.");
if (!window.testRunner)
debug('This test cannot be run without the TestRunner');
// Clean-up any unused battery manager objects from previous tests.
gc();
jsTestIsAsync = true;
testRunner.waitUntilDone();
var promise1;
function batteryStatusSuccess(battery) {
debug('resolution number 1');
testIfBatteryStatusIsUpToDate(battery);
promise2 = navigator.getBattery();
promise2.then(
function(battery) {
debug('resolution number 2');
testIfBatteryStatusIsUpToDate(battery);
setTimeout(finishJSTest, 0);
}, batteryStatusFailure);
shouldBeTrue('promise1 === promise2');
}
promise1 = navigator.getBattery();
promise1.then(batteryStatusSuccess, batteryStatusFailure);
setAndFireMockBatteryInfo(false, 10, 20, 0.5);
promise_test(async _ => {
const promise1 = navigator.getBattery();
setAndFireMockBatteryInfo(false, 10, 20, 0.5);
const result1 = await promise1;
testIfBatteryStatusIsUpToDate(result1);
const promise2 = navigator.getBattery();
assert_equals(promise1, promise2);
const result2 = await promise2;
testIfBatteryStatusIsUpToDate(result2);
}, 'multiple consecutive invocations of navigator.getBattery()');
</script>
</body>
</html>
Test multiple promise resolution.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
PASS promise1 === promise2 is true
first resolution
PASS batteryInfo is defined.
PASS lastSetMockBatteryInfo is defined.
PASS batteryInfo.charging is lastSetMockBatteryInfo.charging
PASS batteryInfo.chargingTime is lastSetMockBatteryInfo.chargingTime
PASS batteryInfo.dischargingTime is lastSetMockBatteryInfo.dischargingTime
PASS batteryInfo.level is lastSetMockBatteryInfo.level
second resolution
PASS batteryInfo is defined.
PASS lastSetMockBatteryInfo is defined.
PASS batteryInfo.charging is lastSetMockBatteryInfo.charging
PASS batteryInfo.chargingTime is lastSetMockBatteryInfo.chargingTime
PASS batteryInfo.dischargingTime is lastSetMockBatteryInfo.dischargingTime
PASS batteryInfo.level is lastSetMockBatteryInfo.level
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE html>
<html>
<body>
<script src="../resources/js-test.js"></script>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<script src="file:///gen/services/device/public/mojom/battery_monitor.mojom.js"></script>
<script src="resources/mock-battery-monitor.js"></script>
<script>
description("Test multiple promise resolution.");
promise_test(async _ => {
const promise1 = navigator.getBattery();
const promise2 = navigator.getBattery();
assert_equals(promise1, promise2);
if (!window.testRunner)
debug('This test cannot be run without the TestRunner');
setAndFireMockBatteryInfo(false, 10, 20, 0.5);
testIfBatteryStatusIsUpToDate(await promise1);
testIfBatteryStatusIsUpToDate(await promise2);
// Clean-up any unused battery manager objects from previous tests.
gc();
jsTestIsAsync = true;
testRunner.waitUntilDone();
var promise1Count = 0;
var promise2Count = 0;
function finishIfReady() {
if (promise1Count == 1 && promise2Count == 1)
setTimeout(finishJSTest, 0);
}
var promise1;
var promise2;
promise1 = navigator.getBattery();
promise1.then(
function(battery) {
debug('first resolution');
testIfBatteryStatusIsUpToDate(battery);
promise1Count++;
finishIfReady();
}, batteryStatusFailure);
promise2 = navigator.getBattery();
promise2.then(
function(battery) {
debug('second resolution');
testIfBatteryStatusIsUpToDate(battery);
promise2Count++;
finishIfReady();
}, batteryStatusFailure);
shouldBeTrue('promise1 === promise2');
setAndFireMockBatteryInfo(false, 10, 20, 0.5);
}, 'multiple parallel invocations of navigator.getBattery()');
</script>
</body>
</html>
Test no garbage collection of battery manager object when listeners are attached.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
PASS batteryInfo is defined.
PASS lastSetMockBatteryInfo is defined.
PASS batteryInfo.charging is lastSetMockBatteryInfo.charging
PASS batteryInfo.chargingTime is lastSetMockBatteryInfo.chargingTime
PASS batteryInfo.dischargingTime is lastSetMockBatteryInfo.dischargingTime
PASS batteryInfo.level is lastSetMockBatteryInfo.level
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE html>
<html>
<body>
<script src="../resources/js-test.js"></script>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<script src="file:///gen/services/device/public/mojom/battery_monitor.mojom.js"></script>
<script src="resources/mock-battery-monitor.js"></script>
<script>
description("Test no garbage collection of battery manager object when listeners are attached.");
promise_test(async _ => {
setAndFireMockBatteryInfo(false, 10, 20, 0.5);
if (!window.testRunner)
debug('This test cannot be run without the TestRunner');
let battery = await navigator.getBattery();
const newLevel = 0.6;
const levelChange =
new Promise(resolve => battery.addEventListener('levelchange', resolve));
battery = null;
gc();
// Clean-up any unused battery manager objects from previous tests.
gc();
jsTestIsAsync = true;
testRunner.waitUntilDone();
function fireLevelChange() {
gc();
setAndFireMockBatteryInfo(false, 10, 20, 0.6);
}
navigator.getBattery().then(function(battery) {
battery.addEventListener('levelchange', function() {
testIfBatteryStatusIsUpToDate(battery);
battery.removeEventListener('levelchange', arguments.callee);
setTimeout(finishJSTest, 0);
});
setTimeout(fireLevelChange, 0);
});
setAndFireMockBatteryInfo(false, 10, 20, 0.5);
setAndFireMockBatteryInfo(false, 10, 20, newLevel);
await levelChange;
}, 'BatteryManager object is not garbage-collected with listeners attached');
</script>
</body>
</html>
<!DOCTYPE html>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../resources/gc.js"></script>
<script>
promise_test(async () => {
let batteryManagerPromise = navigator.getBattery();
......
Check if calling navigator.getBattery() on a detached frame doesn't leak.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
PASS DONE
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE html>
<html>
<head>
<script src="../resources/js-test.js"></script>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
</head>
<body onload="test()">
<body>
<script>
description("Check if calling navigator.getBattery() on a detached frame doesn't leak.");
promise_test(async _ => {
const iframe = document.createElement('iframe');
iframe.srcdoc = '<html></html>';
document.body.appendChild(iframe);
window.jsTestIsAsync = true;
const frameNav = iframe.contentWindow.navigator;
document.body.removeChild(iframe);
function test() {
old_nav = window.frames[0].navigator;
var p = document.getElementById('subframe');
p.parentNode.removeChild(p);
old_nav.getBattery();
testPassed('DONE');
finishJSTest();
}
const battery = await frameNav.getBattery();
assert_equals(battery, undefined);
}, 'navigator.getBattery() on a detached frame does not leak or crash');
</script>
<!-- iframe with some contents -->
<iframe srcdoc="<html></html>" id="subframe"></iframe>
</body>
</html>
Test with page visibility.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
page is visible
PASS batteryInfo is defined.
PASS lastSetMockBatteryInfo is defined.
PASS batteryInfo.charging is lastSetMockBatteryInfo.charging
PASS batteryInfo.chargingTime is lastSetMockBatteryInfo.chargingTime
PASS batteryInfo.dischargingTime is lastSetMockBatteryInfo.dischargingTime
PASS batteryInfo.level is lastSetMockBatteryInfo.level
page is hidden
page is visible
PASS batteryInfo is defined.
PASS lastSetMockBatteryInfo is defined.
PASS batteryInfo.charging is lastSetMockBatteryInfo.charging
PASS batteryInfo.chargingTime is lastSetMockBatteryInfo.chargingTime
PASS batteryInfo.dischargingTime is lastSetMockBatteryInfo.dischargingTime
PASS batteryInfo.level is lastSetMockBatteryInfo.level
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE html>
<html>
<body>
<script src="../resources/js-test.js"></script>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../resources/visibility.js"></script>
<script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<script src="file:///gen/services/device/public/mojom/battery_monitor.mojom.js"></script>
<script src="resources/mock-battery-monitor.js"></script>
<script>
description("Test with page visibility.");
if (!window.testRunner)
debug('This test cannot be run without the TestRunner');
// Clean-up any unused battery manager objects from previous tests.
gc();
jsTestIsAsync = true;
testRunner.waitUntilDone();
document.addEventListener("visibilitychange", function() {
debug("page is " + document.visibilityState);
});
var battery;
function batteryStatusSuccess(batteryManager) {
battery = batteryManager;
testIfBatteryStatusIsUpToDate(battery);
battery.addEventListener('levelchange', failAndFinish);
setMainWindowHidden(true).then(() => {
setAndFireMockBatteryInfo(false, 10, 20, 0.55);
setTimeout(testWithVisiblePage, 0);
});
}
function testWithVisiblePage() {
battery.removeEventListener('levelchange', failAndFinish);
battery.addEventListener('levelchange', onLevelChange);
setMainWindowHidden(false).then(() => {
setAndFireMockBatteryInfo(false, 10, 20, 0.6);
});
}
function onLevelChange() {
testIfBatteryStatusIsUpToDate(battery);
battery.removeEventListener('levelchange', onLevelChange);
setTimeout(finishJSTest, 0);
}
function failAndFinish() {
testFailed('received event while the page was hidden');
setTimeout(finishJSTest, 0);
}
debug("page is visible");
navigator.getBattery().then(batteryStatusSuccess, batteryStatusFailure);
setAndFireMockBatteryInfo(false, 10, 20, 0.5);
promise_test(async _ => {
setAndFireMockBatteryInfo(false, 10, 20, 0.5);
const battery = await navigator.getBattery();
testIfBatteryStatusIsUpToDate(battery);
const fail = () => { throw new Error('unexpected levelchange event'); };
battery.addEventListener('levelchange', fail);
await setMainWindowHidden(true);
setAndFireMockBatteryInfo(false, 10, 20, 0.55);
battery.removeEventListener('levelchange', fail);
const change =
new Promise(resolve => battery.addEventListener('levelchange', resolve));
await setMainWindowHidden(false);
setAndFireMockBatteryInfo(false, 10, 20, 0.6);
await change;
testIfBatteryStatusIsUpToDate(battery);
}, 'battery status events only fire on visible pages');
</script>
</body>
</html>
Test promise resolution and event listeners callbacks.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
batteryStatusSuccess invoked
PASS batteryInfo is defined.
PASS lastSetMockBatteryInfo is defined.
PASS batteryInfo.charging is lastSetMockBatteryInfo.charging
PASS batteryInfo.chargingTime is lastSetMockBatteryInfo.chargingTime
PASS batteryInfo.dischargingTime is lastSetMockBatteryInfo.dischargingTime
PASS batteryInfo.level is lastSetMockBatteryInfo.level
chargingchange invoked
PASS batteryInfo is defined.
PASS lastSetMockBatteryInfo is defined.
PASS batteryInfo.charging is lastSetMockBatteryInfo.charging
PASS batteryInfo.chargingTime is lastSetMockBatteryInfo.chargingTime
PASS batteryInfo.dischargingTime is lastSetMockBatteryInfo.dischargingTime
PASS batteryInfo.level is lastSetMockBatteryInfo.level
chargingtimechange invoked
PASS batteryInfo is defined.
PASS lastSetMockBatteryInfo is defined.
PASS batteryInfo.charging is lastSetMockBatteryInfo.charging
PASS batteryInfo.chargingTime is lastSetMockBatteryInfo.chargingTime
PASS batteryInfo.dischargingTime is lastSetMockBatteryInfo.dischargingTime
PASS batteryInfo.level is lastSetMockBatteryInfo.level
dischargingtimechange invoked
PASS batteryInfo is defined.
PASS lastSetMockBatteryInfo is defined.
PASS batteryInfo.charging is lastSetMockBatteryInfo.charging
PASS batteryInfo.chargingTime is lastSetMockBatteryInfo.chargingTime
PASS batteryInfo.dischargingTime is lastSetMockBatteryInfo.dischargingTime
PASS batteryInfo.level is lastSetMockBatteryInfo.level
levelchange invoked
PASS batteryInfo is defined.
PASS lastSetMockBatteryInfo is defined.
PASS batteryInfo.charging is lastSetMockBatteryInfo.charging
PASS batteryInfo.chargingTime is lastSetMockBatteryInfo.chargingTime
PASS batteryInfo.dischargingTime is lastSetMockBatteryInfo.dischargingTime
PASS batteryInfo.level is lastSetMockBatteryInfo.level
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE html>
<html>
<body>
<script src="../resources/js-test.js"></script>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<script src="file:///gen/services/device/public/mojom/battery_monitor.mojom.js"></script>
<script src="resources/mock-battery-monitor.js"></script>
<script>
description("Test promise resolution and event listeners callbacks.");
if (!window.testRunner)
debug('This test cannot be run without the TestRunner');
// Clean-up any unused battery manager objects from previous tests.
gc();
jsTestIsAsync = true;
testRunner.waitUntilDone();
var battery;
function batteryStatusSuccess(batteryManager) {
debug('batteryStatusSuccess invoked');
battery = batteryManager;
testIfBatteryStatusIsUpToDate(battery);
battery.addEventListener('chargingchange', onChargingChange);
battery.addEventListener('dischargingtimechange', onDischargingTimeChange);
battery.addEventListener('chargingtimechange', onChargingTimeChange);
battery.addEventListener('levelchange', onLevelChange);
setTimeout(fireNextMockBatteryInfo, 0);
}
function fireNextMockBatteryInfo() {
setAndFireMockBatteryInfo(true, 11, 22, 0.6);
function nextEvent(target, name) {
return new Promise(
resolve => target.addEventListener(name, resolve, {once: true}));
}
var chargingChanged = 0;
var chargingTimeChanged = 0;
var dischargingTimeChanged = 0;
var levelChanged = 0;
promise_test(async _ => {
setAndFireMockBatteryInfo(false, 10, 20, 0.5);
const battery = await navigator.getBattery();
function onChargingChange() {
debug('chargingchange invoked');
if (this !== battery) { testFailed('this !== battery'); }
testIfBatteryStatusIsUpToDate(this);
chargingChanged++;
finishIfReady();
}
function onChargingTimeChange() {
debug('chargingtimechange invoked');
if (this !== battery) { testFailed('this !== battery'); }
testIfBatteryStatusIsUpToDate(this);
chargingTimeChanged++;
finishIfReady();
}
testIfBatteryStatusIsUpToDate(battery);
function onDischargingTimeChange() {
debug('dischargingtimechange invoked');
if (this !== battery) { testFailed('this !== battery'); }
testIfBatteryStatusIsUpToDate(this);
dischargingTimeChanged++;
finishIfReady();
}
const events = Promise.all([
nextEvent(battery, 'chargingchange'),
nextEvent(battery, 'chargingtimechange'),
nextEvent(battery, 'dischargingtimechange'),
nextEvent(battery, 'levelchange'),
]);
function onLevelChange() {
debug('levelchange invoked');
if (this !== battery) { testFailed('this !== battery'); }
testIfBatteryStatusIsUpToDate(this);
levelChanged++;
finishIfReady();
}
function finishIfReady() {
if (chargingChanged == 1 && chargingTimeChanged == 1 && dischargingTimeChanged == 1 && levelChanged == 1) {
battery.removeEventListener('chargingchange', onChargingChange);
battery.removeEventListener('dischargingtimechange', onDischargingTimeChange);
battery.removeEventListener('chargingtimechange', onChargingTimeChange);
battery.removeEventListener('levelchange', onLevelChange);
setTimeout(finishJSTest, 0);
}
}
setTimeout(() => setAndFireMockBatteryInfo(true, 11, 22, 0.6));
navigator.getBattery().then(batteryStatusSuccess, batteryStatusFailure);
setAndFireMockBatteryInfo(false, 10, 20, 0.5);
await events;
testIfBatteryStatusIsUpToDate(battery);
}, 'event listeners fire as specified');
</script>
</body>
</html>
......@@ -69,17 +69,12 @@ function setAndFireMockBatteryInfo(charging, chargingTime, dischargingTime,
// compare obtained battery values with the mock values
function testIfBatteryStatusIsUpToDate(batteryManager) {
batteryInfo = batteryManager;
shouldBeDefined("batteryInfo");
shouldBeDefined("lastSetMockBatteryInfo");
shouldBe('batteryInfo.charging', 'lastSetMockBatteryInfo.charging');
shouldBe('batteryInfo.chargingTime', 'lastSetMockBatteryInfo.chargingTime');
shouldBe('batteryInfo.dischargingTime',
'lastSetMockBatteryInfo.dischargingTime');
shouldBe('batteryInfo.level', 'lastSetMockBatteryInfo.level');
}
function batteryStatusFailure() {
testFailed('failed to successfully resolve the promise');
setTimeout(finishJSTest, 0);
assert_not_equals(batteryManager, undefined);
assert_not_equals(lastSetMockBatteryInfo, undefined);
assert_equals(batteryManager.charging, lastSetMockBatteryInfo.charging);
assert_equals(
batteryManager.chargingTime, lastSetMockBatteryInfo.chargingTime);
assert_equals(
batteryManager.dischargingTime, lastSetMockBatteryInfo.dischargingTime);
assert_equals(batteryManager.level, lastSetMockBatteryInfo.level);
}
Test to ensure level is reported with restricted precision.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
batteryStatusSuccess invoked
PASS battery.level is levelRounded
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE html>
<html>
<body>
<script src="../resources/js-test.js"></script>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<script src="file:///gen/services/device/public/mojom/battery_monitor.mojom.js"></script>
<script src="resources/mock-battery-monitor.js"></script>
<script>
description("Test to ensure level is reported with restricted precision.");
promise_test(async _ => {
const levelFullPrecision = 0.556789;
const levelRounded = 0.56;
if (!window.testRunner)
debug('This test cannot be run without the TestRunner');
setAndFireMockBatteryInfo(false, 10, 20, levelFullPrecision);
// Clean-up any unused battery manager objects from previous tests.
gc();
jsTestIsAsync = true;
testRunner.waitUntilDone();
var levelFullPrecision = 0.556789;
var levelRounded = 0.56;
var battery;
function batteryStatusSuccess(batteryManager) {
debug('batteryStatusSuccess invoked');
battery = batteryManager;
shouldBe('battery.level', 'levelRounded');
setTimeout(finishJSTest, 0);
}
navigator.getBattery().then(batteryStatusSuccess, batteryStatusFailure);
setAndFireMockBatteryInfo(false, 10, 20, levelFullPrecision);
const battery = await navigator.getBattery();
assert_equals(battery.level, levelRounded);
}, 'battery level is reported with restricted precision');
</script>
</body>
</html>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment