michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: MARIONETTE_TIMEOUT = 10000; michael@0: michael@0: let battery = window.navigator.battery; michael@0: let fromStatus = "discharging"; michael@0: let fromCharging = false; michael@0: michael@0: function verifyInitialState() { michael@0: ok(battery, "battery"); michael@0: ok(battery.charging, "battery.charging"); michael@0: runEmulatorCmd("power display", function (result) { michael@0: is(result.pop(), "OK", "power display successful"); michael@0: ok(result.indexOf("status: Charging") !== -1, "power status charging"); michael@0: setUp(); michael@0: }); michael@0: } michael@0: michael@0: function unexpectedEvent(event) { michael@0: ok(false, "Unexpected " + event.type + " event"); michael@0: } michael@0: michael@0: function setUp() { michael@0: battery.onchargingchange = function () { michael@0: battery.onchargingchange = unexpectedEvent; michael@0: toCharging(); michael@0: }; michael@0: battery.onlevelchange = unexpectedEvent; michael@0: log("Changing power status to " + fromStatus); michael@0: runEmulatorCmd("power status " + fromStatus); michael@0: } michael@0: michael@0: function resetStatus(charging, nextFunction) { michael@0: log("Resetting power status to " + fromStatus); michael@0: if (charging !== fromCharging) { michael@0: battery.onchargingchange = function () { michael@0: battery.onchargingchange = unexpectedEvent; michael@0: nextFunction(); michael@0: }; michael@0: runEmulatorCmd("power status " + fromStatus); michael@0: } michael@0: else { michael@0: runEmulatorCmd("power status " + fromStatus, nextFunction); michael@0: } michael@0: } michael@0: michael@0: function changeStatus(toStatus, toCharging, nextFunction) { michael@0: log("Changing power status to " + toStatus); michael@0: if (fromCharging !== toCharging) { michael@0: battery.onchargingchange = function (event) { michael@0: battery.onchargingchange = unexpectedEvent; michael@0: is(event.type, "chargingchange", "event type"); michael@0: is(battery.charging, toCharging, "battery.charging"); michael@0: resetStatus(toCharging, nextFunction); michael@0: }; michael@0: runEmulatorCmd("power status " + toStatus); michael@0: } michael@0: else { michael@0: runEmulatorCmd("power status " + toStatus, function () { michael@0: is(battery.charging, toCharging, "battery.charging"); michael@0: resetStatus(toCharging, nextFunction); michael@0: }); michael@0: } michael@0: } michael@0: michael@0: function toCharging() { michael@0: changeStatus("charging", true, toFull); michael@0: } michael@0: michael@0: function toFull() { michael@0: changeStatus("full", true, toNotCharging); michael@0: } michael@0: michael@0: function toNotCharging() { michael@0: changeStatus("not-charging", false, toUnknown); michael@0: } michael@0: michael@0: function toUnknown() { michael@0: changeStatus("unknown", false, cleanUp); michael@0: } michael@0: michael@0: function cleanUp() { michael@0: battery.onchargingchange = function () { michael@0: battery.onchargingchange = null; michael@0: finish(); michael@0: }; michael@0: battery.onlevelchange = null; michael@0: log("Resetting power status to charging"); michael@0: runEmulatorCmd("power status charging"); michael@0: } michael@0: michael@0: verifyInitialState();