test_audio_wakelock.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=868943
  5. -->
  6. <head>
  7. <title>Test for Bug 868943</title>
  8. <script type="application/javascript" src="/MochiKit/packed.js"></script>
  9. <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  10. <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
  11. <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
  12. </head>
  13. <body>
  14. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=868943">Mozilla Bug 868943</a>
  15. <p id="display"></p>
  16. <div id="content">
  17. </div>
  18. <pre id="test">
  19. <script type="application/javascript">
  20. /** Test for Bug 868943 **/
  21. function testAudioPlayPause() {
  22. var lockState = true;
  23. var count = 0;
  24. var content = document.getElementById('content');
  25. var audio = document.createElement('audio');
  26. audio.src = "wakelock.ogg";
  27. content.appendChild(audio);
  28. var startDate;
  29. function testAudioPlayListener(topic, state) {
  30. is(topic, "cpu", "#1 Audio element locked the target == cpu");
  31. var locked = state == "locked-foreground" ||
  32. state == "locked-background";
  33. var s = locked ? "locked" : "unlocked";
  34. is(locked, lockState, "#1 Audio element " + s + " the cpu");
  35. count++;
  36. // count == 1 is when the cpu wakelock is created
  37. // count == 2 is when the cpu wakelock is released
  38. if (count == 1) {
  39. // The next step is to unlock the resource.
  40. lockState = false;
  41. audio.pause();
  42. startDate = new Date();
  43. return;
  44. }
  45. is(count, 2, "The count should be 2 which indicates wakelock release");
  46. if (count == 2) {
  47. var diffDate = (new Date() - startDate);
  48. ok(diffDate > 200, "#1 There was at least 200 milliseconds between the stop and the wakelock release");
  49. content.removeChild(audio);
  50. navigator.mozPower.removeWakeLockListener(testAudioPlayListener);
  51. runTests();
  52. }
  53. };
  54. navigator.mozPower.addWakeLockListener(testAudioPlayListener);
  55. audio.play();
  56. }
  57. function testAudioPlay() {
  58. var lockState = true;
  59. var count = 0;
  60. var content = document.getElementById('content');
  61. var audio = document.createElement('audio');
  62. audio.src = "wakelock.ogg";
  63. content.appendChild(audio);
  64. function testAudioPlayListener(topic, state) {
  65. is(topic, "cpu", "#2 Audio element locked the target == cpu");
  66. var locked = state == "locked-foreground" ||
  67. state == "locked-background";
  68. var s = locked ? "locked" : "unlocked";
  69. is(locked, lockState, "#2 Audio element " + s + " the cpu");
  70. count++;
  71. // count == 1 is when the cpu wakelock is created: the wakelock must be
  72. // created when the media element starts playing.
  73. // count == 2 is when the cpu wakelock is released.
  74. if (count == 1) {
  75. // The next step is to unlock the resource.
  76. lockState = false;
  77. } else if (count == 2) {
  78. content.removeChild(audio);
  79. navigator.mozPower.removeWakeLockListener(testAudioPlayListener);
  80. runTests();
  81. }
  82. };
  83. navigator.mozPower.addWakeLockListener(testAudioPlayListener);
  84. audio.play();
  85. }
  86. var tests = [ testAudioPlayPause, testAudioPlay ];
  87. function runTests() {
  88. if (!tests.length) {
  89. SimpleTest.finish();
  90. return;
  91. }
  92. var test = tests.pop();
  93. test();
  94. };
  95. SpecialPowers.pushPrefEnv({"set": [["media.wakelock_timeout", 500]]}, runTests);
  96. SimpleTest.waitForExplicitFinish();
  97. </script>
  98. </pre>
  99. </body>
  100. </html>