Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Unified Diff: LayoutTests/battery-status/page-visibility.html

Issue 329723005: Battery Status API: blink promise implementation and layout tests. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: revert to a single resolver at a time Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: LayoutTests/battery-status/page-visibility.html
diff --git a/LayoutTests/battery-status/page-visibility.html b/LayoutTests/battery-status/page-visibility.html
new file mode 100644
index 0000000000000000000000000000000000000000..57ad89b713e424b39a3942e2e6eeeac79d3ff049
--- /dev/null
+++ b/LayoutTests/battery-status/page-visibility.html
@@ -0,0 +1,77 @@
+<!DOCTYPE html>
+<html>
+<body>
+<script src="../resources/js-test.js"></script>
+<script>
+description("Test with page visibility.");
+
+window.jsTestIsAsync = true;
+if (!window.testRunner)
+ debug('This test cannot be run without the TestRunner');
+
+var mockBatteryInfo;
+function setAndFireMockBatteryInfo(charging, chargingTime, dischargingTime, level) {
+ mockBatteryInfo = { charging: charging,
+ chargingTime: chargingTime,
+ dischargingTime: dischargingTime,
+ level: level };
+ testRunner.didChangeBatteryStatus(charging, chargingTime, dischargingTime, level);
+}
+
+// compare obtained battery values with the mock values
+function checkBatteryInfo(batteryManager) {
+ batteryInfo = batteryManager;
+ shouldBeDefined("batteryInfo");
+ shouldBeDefined("mockBatteryInfo");
+ shouldBe('batteryInfo.charging', 'mockBatteryInfo.charging');
+ shouldBe('batteryInfo.chargingTime', 'mockBatteryInfo.chargingTime');
+ shouldBe('batteryInfo.dischargingTime', 'mockBatteryInfo.dischargingTime');
+ shouldBe('batteryInfo.level', 'mockBatteryInfo.level');
+}
+
+function batteryStatusFailure() {
+ testFailed('failed to successfully resolve the promise');
+ setTimeout(cleanupAndFinish, 0);
+}
+
+var battery;
+function batteryStatusSuccess(batteryManager) {
+ battery = batteryManager;
+ checkBatteryInfo(battery);
+ battery.addEventListener('levelchange', failAndFinish);
+ testRunner.setPageVisibility("hidden");
+ debug("page is hidden");
+ setAndFireMockBatteryInfo(false, 10, 20, 0.55);
+ setTimeout(testWithVisiblePage, 100);
+}
+
+function testWithVisiblePage() {
+ battery.removeEventListener('levelchange', failAndFinish);
+ battery.addEventListener('levelchange', onLevelChange);
+ testRunner.setPageVisibility("visible");
+ debug("page is visible");
+ setAndFireMockBatteryInfo(false, 10, 20, 0.6);
+}
+
+function onLevelChange() {
+ checkBatteryInfo(battery);
+ setTimeout(cleanupAndFinish, 0);
+}
+
+function cleanupAndFinish() {
+ // stops updating battery and clears current battery status.
+ window.testRunner.setPageVisibility("hidden");
+ finishJSTest();
+}
+
+function failAndFinish() {
+ testFailed('received event while the page was hidden');
+ setTimeout(cleanupAndFinish, 0);
+}
+
+debug("page is visible");
+navigator.getBattery().then(batteryStatusSuccess, batteryStatusFailure);
+setAndFireMockBatteryInfo(false, 10, 20, 0.5);
+</script>
+</body>
+</html>

Powered by Google App Engine
This is Rietveld 408576698