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