<html>
  <head>
    <title>Device Motion/Orientation all-null event test with alert</title>
  </head>
  <body>
    <div id="status">FAIL</div>
  </body>
  <script type="text/javascript">
    passedOrientation = false;
    passedMotion = false;

    function checkOrientationEvent(event) {
      return event.alpha == null &&
             event.beta == null &&
             event.gamma == null;
    }

    function checkMotionEvent(event) {
      return event.acceleration.x == null &&
             event.acceleration.y == null &&
             event.acceleration.z == null &&
             event.accelerationIncludingGravity.x == null &&
             event.accelerationIncludingGravity.y == null &&
             event.accelerationIncludingGravity.z == null &&
             event.rotationRate.alpha == null &&
             event.rotationRate.beta == null &&
             event.rotationRate.gamma == null;
    }

    function onOrientation(event) {
      window.removeEventListener('deviceorientation', onOrientation);
      checkOrientationEvent(event) ?  passOrientation() : fail();
    }

    function onMotion(event) {
      window.removeEventListener('devicemotion', onMotion);
      checkMotionEvent(event) ?  passMotion() : fail();
    }

    function passOrientation() {
      passedOrientation = true;
      if (passedMotion) pass();
    }

    function passMotion() {
      passedMotion = true;
      if (passedOrientation) pass();
    }

    function pass() {
      document.getElementById('status').innerHTML = 'PASS';
      document.location = '#pass';
    }

    function fail() {
      document.location = '#fail';
    }

    window.addEventListener('deviceorientation', onOrientation);
    window.addEventListener('devicemotion', onMotion);

    // Raise a modal alert dialog to test that the one-off null-events
    // still propagate to window after the alert is dismissed. 
    alert("suspend active DOM objects");
  </script>
</html>
