summaryrefslogtreecommitdiffstats
path: root/dom/html/test/test_audio_wakelock.html
blob: a45f2405bd90a59b6105e0d7278107e3dd74678b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!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 testAudioPlayPause() {
  var lockState = true;
  var count = 0;

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

  var audio = document.createElement('audio');
  audio.src = "wakelock.ogg";
  content.appendChild(audio);

  var startDate;
  function testAudioPlayListener(topic, state) {
    is(topic, "cpu", "#1 Audio element locked the target == cpu");
    var locked = state == "locked-foreground" ||
                 state == "locked-background";

    var s = locked ? "locked" : "unlocked";
    is(locked, lockState, "#1 Audio element " + s + " the cpu");
    count++;

    // count == 1 is when the cpu wakelock is created
    // count == 2 is when the cpu wakelock is released

    if (count == 1) {
      // The next step is to unlock the resource.
      lockState = false;
      audio.pause();
      startDate = new Date();
      return;
    }

    is(count, 2, "The count should be 2 which indicates wakelock release");

    if (count == 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(audio);
      navigator.mozPower.removeWakeLockListener(testAudioPlayListener);
      runTests();
    }
  };

  navigator.mozPower.addWakeLockListener(testAudioPlayListener);
  audio.play();
}

function testAudioPlay() {
  var lockState = true;
  var count = 0;

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

  var audio = document.createElement('audio');
  audio.src = "wakelock.ogg";
  content.appendChild(audio);

  function testAudioPlayListener(topic, state) {
    is(topic, "cpu", "#2 Audio element locked the target == cpu");
    var locked = state == "locked-foreground" ||
                 state == "locked-background";

    var s = locked ? "locked" : "unlocked";
    is(locked, lockState, "#2 Audio element " + s + " the cpu");
    count++;

    // count == 1 is when the cpu wakelock is created: the wakelock must be
    // created when the media element starts playing.
    // count == 2 is when the cpu wakelock is released.

    if (count == 1) {
      // The next step is to unlock the resource.
      lockState = false;
    } else if (count == 2) {
      content.removeChild(audio);
      navigator.mozPower.removeWakeLockListener(testAudioPlayListener);
      runTests();
    }
  };

  navigator.mozPower.addWakeLockListener(testAudioPlayListener);
  audio.play();
}

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

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

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

SimpleTest.waitForExplicitFinish();

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