|
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 = "full"; |
|
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 log("Changing power status to " + fromStatus); |
|
28 runEmulatorCmd("power status " + fromStatus, toCharging); |
|
29 } |
|
30 |
|
31 function resetStatus(charging, nextFunction) { |
|
32 log("Resetting power status to " + fromStatus); |
|
33 if (charging !== fromCharging) { |
|
34 battery.onchargingchange = function () { |
|
35 battery.onchargingchange = unexpectedEvent; |
|
36 nextFunction(); |
|
37 }; |
|
38 runEmulatorCmd("power status " + fromStatus); |
|
39 } |
|
40 else { |
|
41 runEmulatorCmd("power status " + fromStatus, nextFunction); |
|
42 } |
|
43 } |
|
44 |
|
45 function changeStatus(toStatus, toCharging, nextFunction) { |
|
46 log("Changing power status to " + toStatus); |
|
47 if (fromCharging !== toCharging) { |
|
48 battery.onchargingchange = function (event) { |
|
49 battery.onchargingchange = unexpectedEvent; |
|
50 is(event.type, "chargingchange", "event type"); |
|
51 is(battery.charging, toCharging, "battery.charging"); |
|
52 resetStatus(toCharging, nextFunction); |
|
53 }; |
|
54 runEmulatorCmd("power status " + toStatus); |
|
55 } |
|
56 else { |
|
57 runEmulatorCmd("power status " + toStatus, function () { |
|
58 is(battery.charging, toCharging, "battery.charging"); |
|
59 resetStatus(toCharging, nextFunction); |
|
60 }); |
|
61 } |
|
62 } |
|
63 |
|
64 function toCharging() { |
|
65 changeStatus("charging", true, toDischarging); |
|
66 } |
|
67 |
|
68 function toDischarging() { |
|
69 changeStatus("discharging", false, toNotCharging); |
|
70 } |
|
71 |
|
72 function toNotCharging() { |
|
73 changeStatus("not-charging", false, toUnknown); |
|
74 } |
|
75 |
|
76 function toUnknown() { |
|
77 changeStatus("unknown", false, cleanUp); |
|
78 } |
|
79 |
|
80 function cleanUp() { |
|
81 battery.onchargingchange = null; |
|
82 battery.onlevelchange = null; |
|
83 log("Resetting power status to charging"); |
|
84 runEmulatorCmd("power status charging", finish); |
|
85 } |
|
86 |
|
87 verifyInitialState(); |