dom/battery/test/marionette/test_battery_status_discharging.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/battery/test/marionette/test_battery_status_discharging.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,93 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +MARIONETTE_TIMEOUT = 10000;
     1.8 +
     1.9 +let battery = window.navigator.battery;
    1.10 +let fromStatus = "discharging";
    1.11 +let fromCharging = false;
    1.12 +
    1.13 +function verifyInitialState() {
    1.14 +  ok(battery, "battery");
    1.15 +  ok(battery.charging, "battery.charging");
    1.16 +  runEmulatorCmd("power display", function (result) {
    1.17 +    is(result.pop(), "OK", "power display successful");
    1.18 +    ok(result.indexOf("status: Charging") !== -1, "power status charging");
    1.19 +    setUp();
    1.20 +  });
    1.21 +}
    1.22 +
    1.23 +function unexpectedEvent(event) {
    1.24 +  ok(false, "Unexpected " + event.type + " event");
    1.25 +}
    1.26 +
    1.27 +function setUp() {
    1.28 +  battery.onchargingchange = function () {
    1.29 +    battery.onchargingchange = unexpectedEvent;
    1.30 +    toCharging();
    1.31 +  };
    1.32 +  battery.onlevelchange = unexpectedEvent;
    1.33 +  log("Changing power status to " + fromStatus);
    1.34 +  runEmulatorCmd("power status " + fromStatus);
    1.35 +}
    1.36 +
    1.37 +function resetStatus(charging, nextFunction) {
    1.38 +  log("Resetting power status to " + fromStatus);
    1.39 +  if (charging !== fromCharging) {
    1.40 +    battery.onchargingchange = function () {
    1.41 +      battery.onchargingchange = unexpectedEvent;
    1.42 +      nextFunction();
    1.43 +    };
    1.44 +    runEmulatorCmd("power status " + fromStatus);
    1.45 +  }
    1.46 +  else {
    1.47 +    runEmulatorCmd("power status " + fromStatus, nextFunction);
    1.48 +  }
    1.49 +}
    1.50 +
    1.51 +function changeStatus(toStatus, toCharging, nextFunction) {
    1.52 +  log("Changing power status to " + toStatus);
    1.53 +  if (fromCharging !== toCharging) {
    1.54 +    battery.onchargingchange = function (event) {
    1.55 +      battery.onchargingchange = unexpectedEvent;
    1.56 +      is(event.type, "chargingchange", "event type");
    1.57 +      is(battery.charging, toCharging, "battery.charging");
    1.58 +      resetStatus(toCharging, nextFunction);
    1.59 +    };
    1.60 +    runEmulatorCmd("power status " + toStatus);
    1.61 +  }
    1.62 +  else {
    1.63 +    runEmulatorCmd("power status " + toStatus, function () {
    1.64 +      is(battery.charging, toCharging, "battery.charging");
    1.65 +      resetStatus(toCharging, nextFunction);
    1.66 +    });
    1.67 +  }
    1.68 +}
    1.69 +
    1.70 +function toCharging() {
    1.71 +  changeStatus("charging", true, toFull);
    1.72 +}
    1.73 +
    1.74 +function toFull() {
    1.75 +  changeStatus("full", true, toNotCharging);
    1.76 +}
    1.77 +
    1.78 +function toNotCharging() {
    1.79 +  changeStatus("not-charging", false, toUnknown);
    1.80 +}
    1.81 +
    1.82 +function toUnknown() {
    1.83 +  changeStatus("unknown", false, cleanUp);
    1.84 +}
    1.85 +
    1.86 +function cleanUp() {
    1.87 +  battery.onchargingchange = function () {
    1.88 +    battery.onchargingchange = null;
    1.89 +    finish();
    1.90 +  };
    1.91 +  battery.onlevelchange = null;
    1.92 +  log("Resetting power status to charging");
    1.93 +  runEmulatorCmd("power status charging");
    1.94 +}
    1.95 +
    1.96 +verifyInitialState();

mercurial