1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/battery/test/marionette/test_battery_status_charging.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,85 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +MARIONETTE_TIMEOUT = 10000; 1.8 + 1.9 +let battery = window.navigator.battery; 1.10 +let fromStatus = "charging"; 1.11 +let fromCharging = true; 1.12 + 1.13 +function verifyInitialState() { 1.14 + ok(battery, "battery"); 1.15 + ok(battery.charging, "battery.charging"); 1.16 + runEmulatorCmd("power display", function (result) { 1.17 + is(result.pop(), "OK", "power display successful"); 1.18 + ok(result.indexOf("status: Charging") !== -1, "power status charging"); 1.19 + setUp(); 1.20 + }); 1.21 +} 1.22 + 1.23 +function unexpectedEvent(event) { 1.24 + ok(false, "Unexpected " + event.type + " event"); 1.25 +} 1.26 + 1.27 +function setUp() { 1.28 + battery.onchargingchange = unexpectedEvent; 1.29 + battery.onlevelchange = unexpectedEvent; 1.30 + toDischarging(); 1.31 +} 1.32 + 1.33 +function resetStatus(charging, nextFunction) { 1.34 + log("Resetting power status to " + fromStatus); 1.35 + if (charging !== fromCharging) { 1.36 + battery.onchargingchange = function () { 1.37 + battery.onchargingchange = unexpectedEvent; 1.38 + nextFunction(); 1.39 + }; 1.40 + runEmulatorCmd("power status " + fromStatus); 1.41 + } 1.42 + else { 1.43 + runEmulatorCmd("power status " + fromStatus, nextFunction); 1.44 + } 1.45 +} 1.46 + 1.47 +function changeStatus(toStatus, toCharging, nextFunction) { 1.48 + log("Changing power status to " + toStatus); 1.49 + if (fromCharging !== toCharging) { 1.50 + battery.onchargingchange = function (event) { 1.51 + battery.onchargingchange = unexpectedEvent; 1.52 + is(event.type, "chargingchange", "event type"); 1.53 + is(battery.charging, toCharging, "battery.charging"); 1.54 + resetStatus(toCharging, nextFunction); 1.55 + }; 1.56 + runEmulatorCmd("power status " + toStatus); 1.57 + } 1.58 + else { 1.59 + runEmulatorCmd("power status " + toStatus, function () { 1.60 + is(battery.charging, toCharging, "battery.charging"); 1.61 + resetStatus(toCharging, nextFunction); 1.62 + }); 1.63 + } 1.64 +} 1.65 + 1.66 +function toDischarging() { 1.67 + changeStatus("discharging", false, toFull); 1.68 +} 1.69 + 1.70 +function toFull() { 1.71 + changeStatus("full", true, toNotCharging); 1.72 +} 1.73 + 1.74 +function toNotCharging() { 1.75 + changeStatus("not-charging", false, toUnknown); 1.76 +} 1.77 + 1.78 +function toUnknown() { 1.79 + changeStatus("unknown", false, cleanUp); 1.80 +} 1.81 + 1.82 +function cleanUp() { 1.83 + battery.onchargingchange = null; 1.84 + battery.onlevelchange = null; 1.85 + finish(); 1.86 +} 1.87 + 1.88 +verifyInitialState();