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> <!DOCTYPE html>
<html> <html>
<body> <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/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="file:///gen/services/device/public/mojom/battery_monitor.mojom.js"></script>
<script src="resources/mock-battery-monitor.js"></script> <script src="resources/mock-battery-monitor.js"></script>
<script> <script>
description("Test basic API definitions."); promise_test(async _ => {
setAndFireMockBatteryInfo(false, 10, 20, 0.5);
if (!window.testRunner) const battery = await navigator.getBattery();
debug('This test cannot be run without the TestRunner');
assert_not_equals(battery, undefined);
// Clean-up any unused battery manager objects from previous tests. assert_equals(typeof battery.charging, 'boolean');
gc(); assert_equals(typeof battery.chargingTime, 'number');
jsTestIsAsync = true; assert_equals(typeof battery.dischargingTime, 'number');
testRunner.waitUntilDone(); assert_equals(typeof battery.level, 'number');
var battery; testIfBatteryStatusIsUpToDate(battery);
function batteryStatusSuccess(batteryManager) {
debug('batteryStatusSuccess invoked'); assert_equals(typeof battery.onchargingchange, 'object');
battery = batteryManager; assert_equals(typeof battery.onchargingtimechange, 'object');
assert_equals(typeof battery.ondischargingtimechange, 'object');
shouldBeDefined("battery"); assert_equals(typeof battery.onlevelchange, 'object');
shouldBeNonNull("battery"); }, 'verify basic getBattery API support');
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);
</script> </script>
</body> </body>
</html> </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> <!DOCTYPE HTML>
<body> <body>
<head> <head>
<script src="../resources/js-test.js"></script> <script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
</head> </head>
<script> <script>
description("Accessing navigator.getBattery() on a closed window."); function waitForMessage(data) {
return new Promise(
window.jsTestIsAsync = true; resolve => {
window.addEventListener('message', e => {
if (e.data == data) {
resolve();
}
}, {once: true});
});
}
var w; promise_test(async _ => {
var nav; const w = window.open('../resources/window-postmessage-open-close.html');
await waitForMessage('opened');
function processMessage(event) { const nav = w.navigator;
if (event.data == "opened") { let result = nav.getBattery();
nav = w.navigator; assert_equals(typeof result, 'object');
shouldBeTrue("typeof(nav.getBattery()) == 'object'"); assert_equals(result.constructor, w.Promise);
w.close();
w = null;
} else if (event.data == "closed") {
shouldBeUndefined("nav.getBattery()");
finishJSTest();
}
}
if (window.testRunner) { w.close();
testRunner.dumpAsText(); await waitForMessage('closed');
testRunner.setCanOpenWindows(); result = nav.getBattery();
} assert_equals(result, undefined);
w = window.open('../resources/window-postmessage-open-close.html'); }, 'navigator.getBattery() does not operate on a closed window');
window.addEventListener("message", processMessage, false);
</script> </script>
</body> </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> <!DOCTYPE html>
<html> <html>
<body> <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/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="file:///gen/services/device/public/mojom/battery_monitor.mojom.js"></script>
<script src="resources/mock-battery-monitor.js"></script> <script src="resources/mock-battery-monitor.js"></script>
<script> <script>
description("Test battery status API with multiple promises after resolve."); promise_test(async _ => {
const promise1 = navigator.getBattery();
if (!window.testRunner) setAndFireMockBatteryInfo(false, 10, 20, 0.5);
debug('This test cannot be run without the TestRunner'); const result1 = await promise1;
testIfBatteryStatusIsUpToDate(result1);
// Clean-up any unused battery manager objects from previous tests. const promise2 = navigator.getBattery();
gc(); assert_equals(promise1, promise2);
jsTestIsAsync = true; const result2 = await promise2;
testRunner.waitUntilDone(); testIfBatteryStatusIsUpToDate(result2);
}, 'multiple consecutive invocations of navigator.getBattery()');
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);
</script> </script>
</body> </body>
</html> </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> <!DOCTYPE html>
<html> <html>
<body> <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/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="file:///gen/services/device/public/mojom/battery_monitor.mojom.js"></script>
<script src="resources/mock-battery-monitor.js"></script> <script src="resources/mock-battery-monitor.js"></script>
<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) setAndFireMockBatteryInfo(false, 10, 20, 0.5);
debug('This test cannot be run without the TestRunner'); testIfBatteryStatusIsUpToDate(await promise1);
testIfBatteryStatusIsUpToDate(await promise2);
// Clean-up any unused battery manager objects from previous tests. }, 'multiple parallel invocations of navigator.getBattery()');
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);
</script> </script>
</body> </body>
</html> </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> <!DOCTYPE html>
<html> <html>
<body> <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/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="file:///gen/services/device/public/mojom/battery_monitor.mojom.js"></script>
<script src="resources/mock-battery-monitor.js"></script> <script src="resources/mock-battery-monitor.js"></script>
<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) let battery = await navigator.getBattery();
debug('This test cannot be run without the TestRunner'); 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. setAndFireMockBatteryInfo(false, 10, 20, newLevel);
gc(); await levelChange;
jsTestIsAsync = true; }, 'BatteryManager object is not garbage-collected with listeners attached');
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);
</script> </script>
</body> </body>
</html> </html>
<!DOCTYPE html> <!DOCTYPE html>
<script src="../resources/testharness.js"></script> <script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script> <script src="../resources/testharnessreport.js"></script>
<script src="../resources/gc.js"></script>
<script> <script>
promise_test(async () => { promise_test(async () => {
let batteryManagerPromise = navigator.getBattery(); 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> <!DOCTYPE html>
<html> <html>
<head> <head>
<script src="../resources/js-test.js"></script> <script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
</head> </head>
<body onload="test()"> <body>
<script> <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() { const battery = await frameNav.getBattery();
old_nav = window.frames[0].navigator; assert_equals(battery, undefined);
var p = document.getElementById('subframe'); }, 'navigator.getBattery() on a detached frame does not leak or crash');
p.parentNode.removeChild(p);
old_nav.getBattery();
testPassed('DONE');
finishJSTest();
}
</script> </script>
<!-- iframe with some contents -->
<iframe srcdoc="<html></html>" id="subframe"></iframe>
</body> </body>
</html> </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> <!DOCTYPE html>
<html> <html>
<body> <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="../resources/visibility.js"></script>
<script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.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="file:///gen/services/device/public/mojom/battery_monitor.mojom.js"></script>
<script src="resources/mock-battery-monitor.js"></script> <script src="resources/mock-battery-monitor.js"></script>
<script> <script>
description("Test with page visibility."); promise_test(async _ => {
setAndFireMockBatteryInfo(false, 10, 20, 0.5);
if (!window.testRunner) const battery = await navigator.getBattery();
debug('This test cannot be run without the TestRunner');
testIfBatteryStatusIsUpToDate(battery);
// Clean-up any unused battery manager objects from previous tests.
gc(); const fail = () => { throw new Error('unexpected levelchange event'); };
jsTestIsAsync = true; battery.addEventListener('levelchange', fail);
testRunner.waitUntilDone(); await setMainWindowHidden(true);
setAndFireMockBatteryInfo(false, 10, 20, 0.55);
document.addEventListener("visibilitychange", function() { battery.removeEventListener('levelchange', fail);
debug("page is " + document.visibilityState);
}); const change =
new Promise(resolve => battery.addEventListener('levelchange', resolve));
var battery; await setMainWindowHidden(false);
function batteryStatusSuccess(batteryManager) { setAndFireMockBatteryInfo(false, 10, 20, 0.6);
battery = batteryManager; await change;
testIfBatteryStatusIsUpToDate(battery);
battery.addEventListener('levelchange', failAndFinish); testIfBatteryStatusIsUpToDate(battery);
setMainWindowHidden(true).then(() => { }, 'battery status events only fire on visible pages');
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);
</script> </script>
</body> </body>
</html> </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> <!DOCTYPE html>
<html> <html>
<body> <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/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="file:///gen/services/device/public/mojom/battery_monitor.mojom.js"></script>
<script src="resources/mock-battery-monitor.js"></script> <script src="resources/mock-battery-monitor.js"></script>
<script> <script>
description("Test promise resolution and event listeners callbacks."); function nextEvent(target, name) {
return new Promise(
if (!window.testRunner) resolve => target.addEventListener(name, resolve, {once: true}));
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);
} }
var chargingChanged = 0; promise_test(async _ => {
var chargingTimeChanged = 0; setAndFireMockBatteryInfo(false, 10, 20, 0.5);
var dischargingTimeChanged = 0; const battery = await navigator.getBattery();
var levelChanged = 0;
function onChargingChange() { testIfBatteryStatusIsUpToDate(battery);
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();
}
function onDischargingTimeChange() { const events = Promise.all([
debug('dischargingtimechange invoked'); nextEvent(battery, 'chargingchange'),
if (this !== battery) { testFailed('this !== battery'); } nextEvent(battery, 'chargingtimechange'),
testIfBatteryStatusIsUpToDate(this); nextEvent(battery, 'dischargingtimechange'),
dischargingTimeChanged++; nextEvent(battery, 'levelchange'),
finishIfReady(); ]);
}
function onLevelChange() { setTimeout(() => setAndFireMockBatteryInfo(true, 11, 22, 0.6));
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);
}
}
navigator.getBattery().then(batteryStatusSuccess, batteryStatusFailure); await events;
setAndFireMockBatteryInfo(false, 10, 20, 0.5); testIfBatteryStatusIsUpToDate(battery);
}, 'event listeners fire as specified');
</script> </script>
</body> </body>
</html> </html>
...@@ -69,17 +69,12 @@ function setAndFireMockBatteryInfo(charging, chargingTime, dischargingTime, ...@@ -69,17 +69,12 @@ function setAndFireMockBatteryInfo(charging, chargingTime, dischargingTime,
// compare obtained battery values with the mock values // compare obtained battery values with the mock values
function testIfBatteryStatusIsUpToDate(batteryManager) { function testIfBatteryStatusIsUpToDate(batteryManager) {
batteryInfo = batteryManager; assert_not_equals(batteryManager, undefined);
shouldBeDefined("batteryInfo"); assert_not_equals(lastSetMockBatteryInfo, undefined);
shouldBeDefined("lastSetMockBatteryInfo"); assert_equals(batteryManager.charging, lastSetMockBatteryInfo.charging);
shouldBe('batteryInfo.charging', 'lastSetMockBatteryInfo.charging'); assert_equals(
shouldBe('batteryInfo.chargingTime', 'lastSetMockBatteryInfo.chargingTime'); batteryManager.chargingTime, lastSetMockBatteryInfo.chargingTime);
shouldBe('batteryInfo.dischargingTime', assert_equals(
'lastSetMockBatteryInfo.dischargingTime'); batteryManager.dischargingTime, lastSetMockBatteryInfo.dischargingTime);
shouldBe('batteryInfo.level', 'lastSetMockBatteryInfo.level'); assert_equals(batteryManager.level, lastSetMockBatteryInfo.level);
}
function batteryStatusFailure() {
testFailed('failed to successfully resolve the promise');
setTimeout(finishJSTest, 0);
} }
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> <!DOCTYPE html>
<html> <html>
<body> <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/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="file:///gen/services/device/public/mojom/battery_monitor.mojom.js"></script>
<script src="resources/mock-battery-monitor.js"></script> <script src="resources/mock-battery-monitor.js"></script>
<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) setAndFireMockBatteryInfo(false, 10, 20, levelFullPrecision);
debug('This test cannot be run without the TestRunner');
// Clean-up any unused battery manager objects from previous tests. const battery = await navigator.getBattery();
gc(); assert_equals(battery.level, levelRounded);
jsTestIsAsync = true; }, 'battery level is reported with restricted precision');
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);
</script> </script>
</body> </body>
</html> </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