Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | MARIONETTE_TIMEOUT = 10000; |
michael@0 | 5 | |
michael@0 | 6 | let battery = window.navigator.battery; |
michael@0 | 7 | let fromStatus = "charging"; |
michael@0 | 8 | let fromCharging = true; |
michael@0 | 9 | |
michael@0 | 10 | function verifyInitialState() { |
michael@0 | 11 | ok(battery, "battery"); |
michael@0 | 12 | ok(battery.charging, "battery.charging"); |
michael@0 | 13 | runEmulatorCmd("power display", function (result) { |
michael@0 | 14 | is(result.pop(), "OK", "power display successful"); |
michael@0 | 15 | ok(result.indexOf("status: Charging") !== -1, "power status charging"); |
michael@0 | 16 | setUp(); |
michael@0 | 17 | }); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | function unexpectedEvent(event) { |
michael@0 | 21 | ok(false, "Unexpected " + event.type + " event"); |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | function setUp() { |
michael@0 | 25 | battery.onchargingchange = unexpectedEvent; |
michael@0 | 26 | battery.onlevelchange = unexpectedEvent; |
michael@0 | 27 | toDischarging(); |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | function resetStatus(charging, nextFunction) { |
michael@0 | 31 | log("Resetting power status to " + fromStatus); |
michael@0 | 32 | if (charging !== fromCharging) { |
michael@0 | 33 | battery.onchargingchange = function () { |
michael@0 | 34 | battery.onchargingchange = unexpectedEvent; |
michael@0 | 35 | nextFunction(); |
michael@0 | 36 | }; |
michael@0 | 37 | runEmulatorCmd("power status " + fromStatus); |
michael@0 | 38 | } |
michael@0 | 39 | else { |
michael@0 | 40 | runEmulatorCmd("power status " + fromStatus, nextFunction); |
michael@0 | 41 | } |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | function changeStatus(toStatus, toCharging, nextFunction) { |
michael@0 | 45 | log("Changing power status to " + toStatus); |
michael@0 | 46 | if (fromCharging !== toCharging) { |
michael@0 | 47 | battery.onchargingchange = function (event) { |
michael@0 | 48 | battery.onchargingchange = unexpectedEvent; |
michael@0 | 49 | is(event.type, "chargingchange", "event type"); |
michael@0 | 50 | is(battery.charging, toCharging, "battery.charging"); |
michael@0 | 51 | resetStatus(toCharging, nextFunction); |
michael@0 | 52 | }; |
michael@0 | 53 | runEmulatorCmd("power status " + toStatus); |
michael@0 | 54 | } |
michael@0 | 55 | else { |
michael@0 | 56 | runEmulatorCmd("power status " + toStatus, function () { |
michael@0 | 57 | is(battery.charging, toCharging, "battery.charging"); |
michael@0 | 58 | resetStatus(toCharging, nextFunction); |
michael@0 | 59 | }); |
michael@0 | 60 | } |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | function toDischarging() { |
michael@0 | 64 | changeStatus("discharging", false, toFull); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | function toFull() { |
michael@0 | 68 | changeStatus("full", true, toNotCharging); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | function toNotCharging() { |
michael@0 | 72 | changeStatus("not-charging", false, toUnknown); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | function toUnknown() { |
michael@0 | 76 | changeStatus("unknown", false, cleanUp); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | function cleanUp() { |
michael@0 | 80 | battery.onchargingchange = null; |
michael@0 | 81 | battery.onlevelchange = null; |
michael@0 | 82 | finish(); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | verifyInitialState(); |