summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/battery-status/battery-plugging-in-manual.html
blob: 4d16cf1875f2ccf0efb146bd205eed25d6b54f3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!DOCTYPE html>
<meta charset="utf-8">
<title>Battery Test: battery not full, charger plugging in</title>
<meta name="flags" content="interact">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<style>
  #note {
    background-color: #fef1b5;
    border: solid 1px #cdab2d;
    padding: 5px;
    margin: 15px;
    display: block;
  }
</style>

<h2>Description</h2>
<p>
  This test validates that all of the BatteryManager attributes exist and
  are set to correct values, with corresponding events fired,
  when the charger is plugged in.
</p>

<h2>Preconditions</h2>
<ol>
  <li>
    The device is unplugged from the charger before this test is run.
  </li>
  <li>
    The battery must not be full or reach full capacity before the time the test is run.
  </li>
</ol>

<div id="note">
  <ol>
    <li>
      Plug in the charger and wait for all the tests to complete.
    </li>
    <li>
      The tests may take long time since the definition of how
      often the chargingtimechange, dischargingtimechange, and levelchange
      events are fired is left to the implementation.
    </li>
  </ol>
</div>

<div id="log"></div>

<script>

(function() {

  setup({ explicit_timeout: true });

  var onchargingchange_test = async_test(
      'When the device is plugged in and its charging state is updated, ' +
      'must set the charging attribute\'s value to true and ' +
      'fire a chargingchange event.');
  var onchargingtimechange_test = async_test(
      'When the device is plugged in and its charging time is updated, ' +
      'must set the chargingTime attribute\'s value and fire ' +
      'a chargingtimechange event.');
  var ondischargingtimechange_test = async_test(
      'When the device is plugged in and its discharging time is updated, ' +
      'must set the dischargingTime attribute\'s value to Infinity and ' +
      'fire a dischargingtimechange event.');
  var onlevelchange_test = async_test(
      'When the device is plugged in and the battery level is updated, ' +
      'must set the level attribute\'s value and fire a levelchange event.');

  var batterySuccess = function (battery) {
    battery.onchargingchange = onchargingchange_test.step_func(function () {
      assert_true(battery.charging, 'The charging attribute must be set to true.');
      onchargingchange_test.done();
    });

    var battery_chargingtime = battery.chargingTime;
    battery.onchargingtimechange = onchargingtimechange_test.step_func(function () {
      assert_less_than(battery.chargingTime, battery_chargingtime,
          'The value of the chargingTime attribute must decrease.');
      onchargingtimechange_test.done();
    });

    battery.ondischargingtimechange = ondischargingtimechange_test.step_func(function () {
      if (battery.charging) {
        assert_equals(battery.dischargingTime, Infinity,
            'The value of the dischargingTime attribute must be set to Infinity.');
        ondischargingtimechange_test.done();
      }
    });

    battery.onlevelchange = onlevelchange_test.step_func(function () {
      assert_greater_than(battery.level, 0);
      assert_less_than_equal(battery.level, 1.0);
      onlevelchange_test.done();
    });
  };

  var batteryFailure = function (error) {
    onchargingchange_test.step(function () {
      assert_unreached(error.message);
    });
    onchargingchange_test.done();

    onchargingtimechange_test.step(function () {
      assert_unreached(error.message);
    });
    onchargingtimechange_test.done();

    ondischargingtimechange_test.step(function () {
      assert_unreached(error.message);
    });
    ondischargingtimechange_test.done();

    onlevelchange_test.step(function () {
      assert_unreached(error.message);
     });
    onlevelchange_test.done();
  };

  navigator.getBattery().then(batterySuccess, batteryFailure);

})();

</script>