<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=868943
-->
<head>
  <title>Test for Bug 868943</title>
  <script type="application/javascript" src="/MochiKit/packed.js"></script>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=868943">Mozilla Bug 868943</a>
<p id="display"></p>
<div id="content">
</div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 868943 **/

function testVideoPlayPause() {
  info("#1 testVideoPlayPause");

  var lockState_cpu = true;
  var lockState_screen = true;
  var count_cpu = 0;
  var count_screen = 0;

  var content = document.getElementById('content');

  var video = document.createElement('video');
  ok(video.mozUseScreenWakeLock, "#1 Video element uses screen wake lock by default");
  video.src = "wakelock.ogv";
  content.appendChild(video);

  var startDate;
  function testVideoPlayPauseListener(topic, state) {
    info("#1 topic=" + topic + ", state=" + state);

    var locked = state == "locked-foreground" ||
                 state == "locked-background";

    if (topic == "cpu") {
      is(locked, lockState_cpu, "#1 Video element locked the cpu");
      count_cpu++;
    } else if (topic == "screen") {
      is(locked, lockState_screen, "#1 Video element locked the screen");
      count_screen++;
    }

    if (count_cpu == 1 && count_screen == 1) {
      info("#1 Both cpu and screen are locked");
      // The next step is to unlock the resource.
      lockState_cpu = false;
      lockState_screen = false;
      video.pause();
      startDate = new Date();
    }

    if (count_cpu == 2 && count_screen == 2) {
      var diffDate = (new Date() - startDate);
      ok(diffDate > 200, "#1 There was at least 200 milliseconds between the stop and the wakelock release");

      content.removeChild(video);
      navigator.mozPower.removeWakeLockListener(testVideoPlayPauseListener);
      runTests();
    }
  }

  navigator.mozPower.addWakeLockListener(testVideoPlayPauseListener);
  video.play();
}

function testVideoPlay() {
  info("#2 testVideoPlay");

  var lockState_cpu = true;
  var lockState_screen = true;
  var count_cpu = 0;
  var count_screen = 0;

  var content = document.getElementById('content');

  var video = document.createElement('video');
  ok(video.mozUseScreenWakeLock, "#2 Video element uses screen wake lock by default");
  video.src = "wakelock.ogv";
  content.appendChild(video);

  var startDate;
  video.addEventListener('progress', function() {
    startDate = new Date();
  });

  function testVideoPlayListener(topic, state) {
    info("#2 topic=" + topic + ", state=" + state);

    var locked = state == "locked-foreground" ||
                 state == "locked-background";

    if (topic == "cpu") {
      is(locked, lockState_cpu, "#2 Video element locked the cpu");
      count_cpu++;
    } else if (topic == "screen") {
      is(locked, lockState_screen, "#2 Video element locked the screen");
      count_screen++;
    }

    if (count_cpu == 1 && count_screen == 1) {
      info("#2 Both cpu and screen are locked");
      // The next step is to unlock the resource.
      lockState_cpu = false;
      lockState_screen = false;
    } else if (count_cpu == 2 && count_screen == 2) {
      var diffDate = (new Date() - startDate);
      ok(diffDate > 200, "#2 There was at least milliseconds between the stop and the wakelock release");

      content.removeChild(video);
      navigator.mozPower.removeWakeLockListener(testVideoPlayListener);
      runTests();
    }
  }

  navigator.mozPower.addWakeLockListener(testVideoPlayListener);
  video.play();
}

function testVideoNoScreenWakeLock() {
  info("#3 testVideoNoScreenWakeLock");

  var lockState_cpu = true;
  var lockState_screen = false;
  var count_cpu = 0;

  var content = document.getElementById('content');

  var video = document.createElement('video');
  video.mozUseScreenWakeLock = false;
  video.src = "wakelock.ogv";
  content.appendChild(video);

  var startDate;
  function testVideoNoScreenWakeLockListener(topic, state) {
    info("#3 topic=" + topic + ", state=" + state);

    var locked = state == "locked-foreground" ||
                 state == "locked-background";

    if (topic == "cpu") {
      is(locked, lockState_cpu, "#3 Video element locked the cpu");
      count_cpu++;
    } else if (topic == "screen") {
      is(locked, lockState_screen, "#3 Video element locked the screen");
    }

    if (count_cpu == 1) {
      info("#3 Cpu is locked");
      // The next step is to unlock the resource.
      lockState_cpu = false;
      video.pause();
      startDate = new Date();
    }

    if (count_cpu == 2) {
      var diffDate = (new Date() - startDate);
      ok(diffDate > 200, "#3 There was at least 200 milliseconds between the stop and the wakelock release");

      content.removeChild(video);
      navigator.mozPower.removeWakeLockListener(testVideoNoScreenWakeLockListener);
      runTests();
    }
  }

  navigator.mozPower.addWakeLockListener(testVideoNoScreenWakeLockListener);
  video.play();
}

var tests = [ testVideoPlayPause, testVideoPlay, testVideoNoScreenWakeLock ];
function runTests() {
  if (!tests.length) {
    SimpleTest.finish();
    return;
  }

  var test =  tests.pop();
  test();
};

SpecialPowers.pushPrefEnv({"set": [["media.wakelock_timeout", 500],
                                   ["dom.wakelock.enabled", true]]}, runTests);

SimpleTest.waitForExplicitFinish();

</script>
</pre>
</body>
</html>