summaryrefslogtreecommitdiff
path: root/dom/html/test/test_video_wakelock.html
blob: 52b05cda4f359d29002e8748be71ddfb39d1949e (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!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>