Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 = "unknown"; |
michael@0 | 8 | let fromCharging = false; |
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 = function () { |
michael@0 | 26 | battery.onchargingchange = unexpectedEvent; |
michael@0 | 27 | toCharging(); |
michael@0 | 28 | }; |
michael@0 | 29 | battery.onlevelchange = unexpectedEvent; |
michael@0 | 30 | log("Changing power status to " + fromStatus); |
michael@0 | 31 | runEmulatorCmd("power status " + fromStatus); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | function resetStatus(charging, nextFunction) { |
michael@0 | 35 | log("Resetting power status to " + fromStatus); |
michael@0 | 36 | if (charging !== fromCharging) { |
michael@0 | 37 | battery.onchargingchange = function () { |
michael@0 | 38 | battery.onchargingchange = unexpectedEvent; |
michael@0 | 39 | nextFunction(); |
michael@0 | 40 | }; |
michael@0 | 41 | runEmulatorCmd("power status " + fromStatus); |
michael@0 | 42 | } |
michael@0 | 43 | else { |
michael@0 | 44 | runEmulatorCmd("power status " + fromStatus, nextFunction); |
michael@0 | 45 | } |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | function changeStatus(toStatus, toCharging, nextFunction) { |
michael@0 | 49 | log("Changing power status to " + toStatus); |
michael@0 | 50 | if (fromCharging !== toCharging) { |
michael@0 | 51 | battery.onchargingchange = function (event) { |
michael@0 | 52 | battery.onchargingchange = unexpectedEvent; |
michael@0 | 53 | is(event.type, "chargingchange", "event type"); |
michael@0 | 54 | is(battery.charging, toCharging, "battery.charging"); |
michael@0 | 55 | resetStatus(toCharging, nextFunction); |
michael@0 | 56 | }; |
michael@0 | 57 | runEmulatorCmd("power status " + toStatus); |
michael@0 | 58 | } |
michael@0 | 59 | else { |
michael@0 | 60 | runEmulatorCmd("power status " + toStatus, function () { |
michael@0 | 61 | is(battery.charging, toCharging, "battery.charging"); |
michael@0 | 62 | resetStatus(toCharging, nextFunction); |
michael@0 | 63 | }); |
michael@0 | 64 | } |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | function toCharging() { |
michael@0 | 68 | changeStatus("charging", true, toDischarging); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | function toDischarging() { |
michael@0 | 72 | changeStatus("discharging", false, toFull); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | function toFull() { |
michael@0 | 76 | changeStatus("full", true, toNotCharging); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | function toNotCharging() { |
michael@0 | 80 | changeStatus("not-charging", false, cleanUp); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | function cleanUp() { |
michael@0 | 84 | battery.onchargingchange = function () { |
michael@0 | 85 | battery.onchargingchange = null; |
michael@0 | 86 | finish(); |
michael@0 | 87 | }; |
michael@0 | 88 | battery.onlevelchange = null; |
michael@0 | 89 | log("Resetting power status to charging"); |
michael@0 | 90 | runEmulatorCmd("power status charging"); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | verifyInitialState(); |