1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/battery/test/marionette/test_battery_status_full.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,87 @@ 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 = "full"; 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 + log("Changing power status to " + fromStatus); 1.31 + runEmulatorCmd("power status " + fromStatus, toCharging); 1.32 +} 1.33 + 1.34 +function resetStatus(charging, nextFunction) { 1.35 + log("Resetting power status to " + fromStatus); 1.36 + if (charging !== fromCharging) { 1.37 + battery.onchargingchange = function () { 1.38 + battery.onchargingchange = unexpectedEvent; 1.39 + nextFunction(); 1.40 + }; 1.41 + runEmulatorCmd("power status " + fromStatus); 1.42 + } 1.43 + else { 1.44 + runEmulatorCmd("power status " + fromStatus, nextFunction); 1.45 + } 1.46 +} 1.47 + 1.48 +function changeStatus(toStatus, toCharging, nextFunction) { 1.49 + log("Changing power status to " + toStatus); 1.50 + if (fromCharging !== toCharging) { 1.51 + battery.onchargingchange = function (event) { 1.52 + battery.onchargingchange = unexpectedEvent; 1.53 + is(event.type, "chargingchange", "event type"); 1.54 + is(battery.charging, toCharging, "battery.charging"); 1.55 + resetStatus(toCharging, nextFunction); 1.56 + }; 1.57 + runEmulatorCmd("power status " + toStatus); 1.58 + } 1.59 + else { 1.60 + runEmulatorCmd("power status " + toStatus, function () { 1.61 + is(battery.charging, toCharging, "battery.charging"); 1.62 + resetStatus(toCharging, nextFunction); 1.63 + }); 1.64 + } 1.65 +} 1.66 + 1.67 +function toCharging() { 1.68 + changeStatus("charging", true, toDischarging); 1.69 +} 1.70 + 1.71 +function toDischarging() { 1.72 + changeStatus("discharging", false, toNotCharging); 1.73 +} 1.74 + 1.75 +function toNotCharging() { 1.76 + changeStatus("not-charging", false, toUnknown); 1.77 +} 1.78 + 1.79 +function toUnknown() { 1.80 + changeStatus("unknown", false, cleanUp); 1.81 +} 1.82 + 1.83 +function cleanUp() { 1.84 + battery.onchargingchange = null; 1.85 + battery.onlevelchange = null; 1.86 + log("Resetting power status to charging"); 1.87 + runEmulatorCmd("power status charging", finish); 1.88 +} 1.89 + 1.90 +verifyInitialState();