dom/battery/test/marionette/test_battery_status_not_charging.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* Any copyright is dedicated to the Public Domain.
     2  * http://creativecommons.org/publicdomain/zero/1.0/ */
     4 MARIONETTE_TIMEOUT = 10000;
     6 let battery = window.navigator.battery;
     7 let fromStatus = "not-charging";
     8 let fromCharging = false;
    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 }
    20 function unexpectedEvent(event) {
    21   ok(false, "Unexpected " + event.type + " event");
    22 }
    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 }
    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 }
    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 }
    67 function toCharging() {
    68   changeStatus("charging", true, toDischarging);
    69 }
    71 function toDischarging() {
    72   changeStatus("discharging", false, toFull);
    73 }
    75 function toFull() {
    76   changeStatus("full", true, toUnknown);
    77 }
    79 function toUnknown() {
    80   changeStatus("unknown", false, cleanUp);
    81 }
    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 }
    93 verifyInitialState();

mercurial