add battery-plugged-in and battery-unplugged tests; add prime computation to the battery-discharging test
authorAnssi Kostiainen <anssi.kostiainen@nokia.com>
Wed, 31 Oct 2012 00:37:06 +0100
changeset 262 e9611e6102bd
parent 261 14b5699e5ac3
child 263 0c19463d8e6a
add battery-plugged-in and battery-unplugged tests; add prime computation to the battery-discharging test
battery/tests/submissions/anssik/battery-charging.html
battery/tests/submissions/anssik/battery-chargingchange.html
battery/tests/submissions/anssik/battery-discharging.html
battery/tests/submissions/anssik/battery-plugged-in.html
battery/tests/submissions/anssik/manifest.txt
battery/tests/submissions/anssik/prime.js
--- a/battery/tests/submissions/anssik/battery-charging.html	Tue Oct 30 16:13:22 2012 +0100
+++ b/battery/tests/submissions/anssik/battery-charging.html	Wed Oct 31 00:37:06 2012 +0100
@@ -23,7 +23,6 @@
     <script>
     (function() {
 
-      // timeout in 5 mins
       setup({ timeout: 5*60*1000 });
 
       test(function() {
@@ -32,8 +31,7 @@
       
       test(function() {
         assert_true(navigator.battery.chargingTime < Infinity);
-      }, 'The chargingTime attribute must be set to the time remaining in ' +
-      'seconds until the system\'s battery is completely charged.');
+      }, 'The chargingTime attribute must be set to the time remaining in seconds until the system\'s battery is completely charged.');
       
       test(function() {
         assert_true(navigator.battery.dischargingTime === Infinity);
--- a/battery/tests/submissions/anssik/battery-chargingchange.html	Tue Oct 30 16:13:22 2012 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,57 +0,0 @@
-<!DOCTYPE html>
-<html>
-  <head>
-  <title>Battery Status API Test Suite</title>
-  <script src="http://www.w3c-test.org/resources/testharness.js"></script>
-  <link rel='stylesheet' href='http://w3c-test.org/resources/testharness.css' media='all'/>
-  </head>
-  <body>
-    <h1>Description</h1>
-    <p>
-       The battery charging state is updated.
-    </p>
-    <h2>Preconditions</h2>
-    <ol>
-      <li>
-        The device is unplugged from the charger before this test case is run.
-      </li>
-      <li>
-        The battery must not be full or reach full capacity during the time the test is run.
-      </li>
-    </ol>
-    <div id="log"></div>
-    <script>
-    (function() {
-
-      // this is the pattern used for testing event handlers
-      
-      /*
-      var onclick_test = async_test('onclick on body');
-      document.querySelector('body').onclick = onclick_test.step_func(function (e) {
-        // assert_xxxx();
-        onclick_test.done();
-      });
-      
-      alert('click on body!');
-      */
-
-      // the tests below must be validated on a real implementation, to set proper timeouts etc.
-      
-      var onchargingchange_test = async_test('When the battery charging state is updated, must fire a chargingchange event.');
-      navigator.battery.onchargingchange = onchargingchange_test.step_func(function (e) {
-        assert_true(navigator.battery.charging, 'The charging attribute must be set to true, if the battery is charging.')
-        onchargingchange_test.done();
-      });
-      
-      var onchargingtimechange_test = async_test('When the battery charging time is updated, must fire a chargingtimechange event.');
-      navigator.battery.onchargingtimechange = onchargingtimechange_test.step_func(function (e) {
-        assert_false(navigator.battery.chargingTime !== Infinity);
-        onchargingchange_test.done();
-      });
-      
-      alert('Plug in the charger, and click OK.');
-
-      })();
-    </script>
-  </body>
-</html>
--- a/battery/tests/submissions/anssik/battery-discharging.html	Tue Oct 30 16:13:22 2012 +0100
+++ b/battery/tests/submissions/anssik/battery-discharging.html	Wed Oct 31 00:37:06 2012 +0100
@@ -19,11 +19,14 @@
         The battery must neither be empty or full, nor reach empty or full capacity during the test.
       </li>
     </ol>
+    <p>
+      The highest prime number discovered so far is:  <output id="prime"></output>
+    </p>
     <div id="log"></div>
     <script>
+
     (function() {
 
-      // timeout in 5 mins
       setup({ timeout: 5*60*1000 });
       
       test(function() {
@@ -36,8 +39,7 @@
       
       test(function() {
         assert_true(navigator.battery.dischargingTime < Infinity);
-      }, 'The dischargingTime attribute must be set to the time remaining in ' +
-      'seconds until the system\'s battery is completely discharged.');
+      }, 'The dischargingTime attribute must be set to the time remaining in seconds until the system\'s battery is completely discharged.');
       
       test(function() {
         assert_true(0 <= navigator.battery.level && navigator.battery.level <= 1.0);
@@ -45,10 +47,18 @@
       
       var onlevelchange_test = async_test('When the battery\'s level changes, must decrease the level attribute\'s value and fire a levelchange event.');
       var battery_level = navigator.battery.level;
-      // TODO: run some computationally heavy stuff e.g. in a worker to make the battery deplete faster?
+      
+      // 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 < battery_level, 'The value of the level attribute must decrease, if the battery is discharging.');
         onlevelchange_test.done();
+        w.terminate();
       });
       
     })();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/battery/tests/submissions/anssik/battery-plugged-in.html	Wed Oct 31 00:37:06 2012 +0100
@@ -0,0 +1,62 @@
+<!DOCTYPE html>
+<html>
+  <head>
+  <title>Battery Status API Test Suite</title>
+  <script src="http://www.w3c-test.org/resources/testharness.js"></script>
+  <link rel='stylesheet' href='http://w3c-test.org/resources/testharness.css' media='all'/>
+  </head>
+  <body>
+    <h1>Description</h1>
+    <p>
+       The battery charging state, charging time, discharging time and level are updated, and 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 case is run.
+      </li>
+      <li>
+        The battery must not be full or reach full capacity during the time the test is run.
+      </li>
+    </ol>
+    <div id="log"></div>
+    <script>
+    (function() {
+
+      setup({ timeout: 5*60*1000 });
+      
+      alert('Unplug the device from its charger (if not already unplugged), and click OK.');
+      
+      var onchargingchange_test = async_test('When the battery charging state is updated, must fire a chargingchange event.');
+      navigator.battery.onchargingchange = onchargingchange_test.step_func(function (e) {
+        assert_true(navigator.battery.charging, 'The charging attribute must be set to true, if the battery is charging.')
+        onchargingchange_test.done();
+      });
+      
+      var onchargingtimechange_test = async_test('When the battery charging time is updated, must fire a chargingtimechange event.');
+      var battery_chargingtime = navigator.battery.chargingTime;
+      navigator.battery.onchargingtimechange = onchargingtimechange_test.step_func(function (e) {
+        assert_true(navigator.battery.chargingTime < battery_chargingtime, 'The value of the chargingTime attribute must decrease, if the battery is charging.');
+        onchargingtimechange_test.done();
+      });
+      
+      var ondischargingtimechange_test = async_test('When the battery discharging time is updated, must fire a dischargingtimechange event.');
+      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 Infinity, if the battery is charging.');
+        ondischargingtimechange_test.done();
+      });
+      
+      var onlevelchange_test = async_test('When the battery\'s level changes, must fire a levelchange event.');
+      var battery_level = navigator.battery.level;
+      navigator.battery.onlevelchange = onlevelchange_test.step_func(function (e) {
+        assert_true(navigator.battery.level > 0 && navigator.battery.level < 1.0, 'The value of the level attribute must >0 and <1.0');
+        onlevelchange_test.done();
+      });
+
+      alert('Plug in the charger, and click OK.');
+
+      })();
+    </script>
+  </body>
+</html>
--- a/battery/tests/submissions/anssik/manifest.txt	Tue Oct 30 16:13:22 2012 +0100
+++ b/battery/tests/submissions/anssik/manifest.txt	Wed Oct 31 00:37:06 2012 +0100
@@ -1,6 +1,7 @@
 battery-interface.html		Battery Status API Test Suite					
 battery-charging.html		Battery Status API Test Suite					
-battery-chargingchange.html		Battery Status API Test Suite					
 battery-created.html		Battery Status API Test Suite					
 battery-discharging.html		Battery Status API Test Suite					
 battery-full.html		Battery Status API Test Suite					
+battery-plugged-in.html		Battery Status API Test Suite					
+battery-unplugged.html		Battery Status API Test Suite					
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/battery/tests/submissions/anssik/prime.js	Wed Oct 31 00:37:06 2012 +0100
@@ -0,0 +1,25 @@
+// http://html5demos.com/worker
+
+var running = false;
+
+onmessage = function (event) {
+  // doesn't matter what the message is, just toggle the worker
+  if (running == false) {
+    running = true;
+    run();
+  } else {
+    running = false;
+  }
+};
+
+function run() {
+  var n = 1;
+  search: while (running) {
+    n += 1;
+    for (var i = 2; i <= Math.sqrt(n); i += 1)
+      if (n % i == 0)
+       continue search;
+    // found a prime!
+    postMessage(n);
+  }
+}
\ No newline at end of file