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