https://github.com/web-platform-tests/wpt
Raw File
Tip revision: 8e313975c739ba7c32a03927a13664e5595fdc12 authored by Ryosuke Niwa on 26 November 2013, 07:14:14 UTC
Fix tests for parsing template elements inside XHTML documents.
Tip revision: 8e31397
battery-unplugged-manual.html
<!DOCTYPE html>
<html>
  <head>
    <title>Battery Status API Test Suite</title>
    <script src="/resources/testharness.js"></script>
    <script src="countdown.js"></script>
    <link rel="stylesheet" href="/resources/testharness.css" media="all"/>
    <style>
      #note { background-color: #fef1b5; border: solid 1px #cdab2d; padding: 5px; margin: 15px; display: none; }
    </style>
    <meta name="flags" content="interact">
  </head>
  <body>
    <h1>Description</h1>
    <p>
      This test validates that all of the navigator.battery attributes exist and are set to correct values, with corresponding events fired, when the charger is unplugged.
    </p>
    <h2>Preconditions</h2>
    <ol>
      <li>
        The device is plugged in to the charger before this test is run.
      </li>
      <li>
        The battery must not be full or reach full capacity during the time the test is run.
      </li>
    </ol>
    <p>
      The highest prime number discovered so far is:  <output id="prime"></output>
    </p>
    <div id="note">
      Unplug the charger and wait for all the tests to complete.
    </div>
    <div id="log"></div>
    <script>
    (function() {

      setup({ explicit_timeout: true });
      
      var onchargingchange_test = async_test('When the device is unplugged in and its charging state is updated, must set the charging attribute\'s value to false and fire a chargingchange event.');
      navigator.battery.onchargingchange = onchargingchange_test.step_func(function (e) {
        assert_false(navigator.battery.charging, 'The charging attribute must be set to false.')
        onchargingchange_test.done();
      });
      
      var onchargingtimechange_test = async_test('When the device is unplugged in and its charging time is updated, must set the chargingTime attribute\'s value to Infinity and fire a chargingtimechange event.');
      var battery_chargingtime = navigator.battery.chargingTime;
      navigator.battery.onchargingtimechange = onchargingtimechange_test.step_func(function (e) {
        assert_true(navigator.battery.chargingTime === Infinity, 'The value of the chargingTime attribute must be set to Infinity.');
        onchargingtimechange_test.done();
      });
      
      var ondischargingtimechange_test = async_test('When the device is unplugged in and its discharging time is updated, must set the dischargingTime attribute\'s value and fire a dischargingtimechange event.  If the reported discharging time ' + navigator.battery.dischargingTime + ' seconds or ' + Math.round(navigator.battery.dischargingTime/60) + ' minutes is not correct, please indicate that the test has failed.');
      var battery_dischargingtime = navigator.battery.dischargingTime;
      navigator.battery.ondischargingtimechange = ondischargingtimechange_test.step_func(function (e) {
        assert_true(navigator.battery.dischargingTime < Infinity, 'The value of the dischargingTime attribute must be set to the remaining discharging time.');
        ondischargingtimechange_test.done();
      });
      
      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 battery_level = navigator.battery.level;
      
      // compute primes to deplete the battery faster
      var w = new Worker('prime.js');
      w.postMessage('compute');
      w.onmessage = function (e) {
        document.querySelector('#prime').textContent = e.data;
      };
      
      navigator.battery.onlevelchange = onlevelchange_test.step_func(function (e) {
        assert_true(navigator.battery.level > 0 && navigator.battery.level < 1.0, 'The level attribute must be set to the current battery level scaled from 0 to 1.0.');
        onlevelchange_test.done();
        w.terminate();
      });

      add_completion_callback(function () {
        w.terminate();
      });

      setTimeout(function() {
        var note = document.querySelector('#note');
        note.style.display = 'block';
        navigator.battery.onchargingchange = function (e) {
          if (!navigator.battery.charging) {
            note.style.display = 'none';
          }
        };
      }, 4000);
      
      })();
    </script>
  </body>
</html>
back to top