summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/browser/browser_mediaPlayback_suspended_multipleAudio.js
blob: 12e2ec077401597813d63e9e2aacfc36d6e1e3b4 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
const PAGE = "https://example.com/browser/toolkit/content/tests/browser/file_multipleAudio.html";

var SuspendedType = {
  NONE_SUSPENDED             : 0,
  SUSPENDED_PAUSE            : 1,
  SUSPENDED_BLOCK            : 2,
  SUSPENDED_PAUSE_DISPOSABLE : 3
};

function wait_for_event(browser, event) {
  return BrowserTestUtils.waitForEvent(browser, event, false, (event) => {
    is(event.originalTarget, browser, "Event must be dispatched to correct browser.");
    return true;
  });
}

function check_all_audio_suspended(suspendedType) {
  var autoPlay = content.document.getElementById('autoplay');
  var nonAutoPlay = content.document.getElementById('nonautoplay');
  if (!autoPlay || !nonAutoPlay) {
    ok(false, "Can't get the audio element!");
  }

  is(autoPlay.computedSuspended, suspendedType,
     "The suspeded state of autoplay audio is correct.");
  is(nonAutoPlay.computedSuspended, suspendedType,
     "The suspeded state of non-autoplay audio is correct.");
}

function check_autoplay_audio_suspended(suspendedType) {
  var autoPlay = content.document.getElementById('autoplay');
  if (!autoPlay) {
    ok(false, "Can't get the audio element!");
  }

  is(autoPlay.computedSuspended, suspendedType,
     "The suspeded state of autoplay audio is correct.");
}

function check_nonautoplay_audio_suspended(suspendedType) {
  var nonAutoPlay = content.document.getElementById('nonautoplay');
  if (!nonAutoPlay) {
    ok(false, "Can't get the audio element!");
  }

  is(nonAutoPlay.computedSuspended, suspendedType,
     "The suspeded state of non-autoplay audio is correct.");
}

function check_autoplay_audio_pause_state(expectedPauseState) {
  var autoPlay = content.document.getElementById('autoplay');
  if (!autoPlay) {
    ok(false, "Can't get the audio element!");
  }

  if (autoPlay.paused == expectedPauseState) {
    if (expectedPauseState) {
      ok(true, "Audio is paused correctly.");
    } else {
      ok(true, "Audio is resumed correctly.");
    }
  } else if (expectedPauseState) {
    autoPlay.onpause = function () {
      autoPlay.onpause = null;
      ok(true, "Audio is paused correctly, checking from onpause.");
    }
  } else {
    autoPlay.onplay = function () {
      autoPlay.onplay = null;
      ok(true, "Audio is resumed correctly, checking from onplay.");
    }
  }
}

function play_nonautoplay_audio_should_be_paused() {
  var nonAutoPlay = content.document.getElementById('nonautoplay');
  if (!nonAutoPlay) {
    ok(false, "Can't get the audio element!");
  }

  nonAutoPlay.play();
  return new Promise(resolve => {
    nonAutoPlay.onpause = function () {
      nonAutoPlay.onpause = null;
      is(nonAutoPlay.ended, false, "Audio can't be playback.");
      resolve();
    }
  });
}

function all_audio_onresume() {
  var autoPlay = content.document.getElementById('autoplay');
  var nonAutoPlay = content.document.getElementById('nonautoplay');
  if (!autoPlay || !nonAutoPlay) {
    ok(false, "Can't get the audio element!");
  }

  is(autoPlay.paused, false, "Autoplay audio is resumed.");
  is(nonAutoPlay.paused, false, "Non-AutoPlay audio is resumed.");
}

function all_audio_onpause() {
  var autoPlay = content.document.getElementById('autoplay');
  var nonAutoPlay = content.document.getElementById('nonautoplay');
  if (!autoPlay || !nonAutoPlay) {
    ok(false, "Can't get the audio element!");
  }

  is(autoPlay.paused, true, "Autoplay audio is paused.");
  is(nonAutoPlay.paused, true, "Non-AutoPlay audio is paused.");
}

function play_nonautoplay_audio_should_play_until_ended() {
  var nonAutoPlay = content.document.getElementById('nonautoplay');
  if (!nonAutoPlay) {
    ok(false, "Can't get the audio element!");
  }

  nonAutoPlay.play();
  return new Promise(resolve => {
    nonAutoPlay.onended = function () {
      nonAutoPlay.onended = null;
      ok(true, "Audio can be playback until ended.");
      resolve();
    }
  });
}

function no_audio_resumed() {
  var autoPlay = content.document.getElementById('autoplay');
  var nonAutoPlay = content.document.getElementById('nonautoplay');
  if (!autoPlay || !nonAutoPlay) {
    ok(false, "Can't get the audio element!");
  }

  is(autoPlay.paused && nonAutoPlay.paused, true, "No audio was resumed.");
}

function play_nonautoplay_audio_should_be_blocked(suspendedType) {
  var nonAutoPlay = content.document.getElementById('nonautoplay');
  if (!nonAutoPlay) {
    ok(false, "Can't get the audio element!");
  }

  nonAutoPlay.play();
  ok(nonAutoPlay.paused, "The blocked audio can't be playback.");
}

function* suspended_pause(url, browser) {
  info("### Start test for suspended-pause ###");
  browser.loadURI(url);

  info("- page should have playing audio -");
  yield wait_for_event(browser, "DOMAudioPlaybackStarted");

  info("- the default suspended state of all audio should be non-suspened-");
  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
                                   check_all_audio_suspended);

  info("- pause all audio in the page -");
  browser.pauseMedia(false /* non-disposable */);
  yield ContentTask.spawn(browser, true /* expect for pause */,
                                   check_autoplay_audio_pause_state);
  yield ContentTask.spawn(browser, SuspendedType.SUSPENDED_PAUSE,
                                   check_autoplay_audio_suspended);
  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
                                   check_nonautoplay_audio_suspended);

  info("- no audio can be playback during suspended-paused -");
  yield ContentTask.spawn(browser, null,
                                   play_nonautoplay_audio_should_be_paused);
  yield ContentTask.spawn(browser, SuspendedType.SUSPENDED_PAUSE,
                                   check_nonautoplay_audio_suspended);

  info("- both audio should be resumed at the same time -");
  browser.resumeMedia();
  yield ContentTask.spawn(browser, null,
                                   all_audio_onresume);
  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
                                   check_all_audio_suspended);

  info("- both audio should be paused at the same time -");
  browser.pauseMedia(false /* non-disposable */);
  yield ContentTask.spawn(browser, null, all_audio_onpause);
}

function* suspended_pause_disposable(url, browser) {
  info("### Start test for suspended-pause-disposable ###");
  browser.loadURI(url);

  info("- page should have playing audio -");
  yield wait_for_event(browser, "DOMAudioPlaybackStarted");

  info("- the default suspended state of all audio should be non-suspened -");
  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
                                   check_all_audio_suspended);

  info("- only pause playing audio in the page -");
  browser.pauseMedia(true /* non-disposable */);
  yield ContentTask.spawn(browser, true /* expect for pause */,
                                   check_autoplay_audio_pause_state);
  yield ContentTask.spawn(browser, SuspendedType.SUSPENDED_PAUSE_DISPOSABLE,
                                   check_autoplay_audio_suspended);
  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
                                   check_nonautoplay_audio_suspended);

  info("- new playing audio should be playback correctly -");
  yield ContentTask.spawn(browser, null,
                                   play_nonautoplay_audio_should_play_until_ended);

  info("- should only resume one audio -");
  browser.resumeMedia();
  yield ContentTask.spawn(browser, false /* expect for playing */,
                                   check_autoplay_audio_pause_state);
  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
                                   check_all_audio_suspended);
}

function* suspended_stop_disposable(url, browser) {
  info("### Start test for suspended-stop-disposable ###");
  browser.loadURI(url);

  info("- page should have playing audio -");
  yield wait_for_event(browser, "DOMAudioPlaybackStarted");

  info("- the default suspended state of all audio should be non-suspened -");
  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
                                   check_all_audio_suspended);

  info("- only stop playing audio in the page -");
  browser.stopMedia();
  yield wait_for_event(browser, "DOMAudioPlaybackStopped");
  yield ContentTask.spawn(browser, true /* expect for pause */,
                                   check_autoplay_audio_pause_state);
  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
                                   check_all_audio_suspended);

  info("- new playing audio should be playback correctly -");
  yield ContentTask.spawn(browser, null,
                                   play_nonautoplay_audio_should_play_until_ended);

  info("- no any audio can be resumed by page -");
  browser.resumeMedia();
  yield ContentTask.spawn(browser, null, no_audio_resumed);
  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
                                   check_all_audio_suspended);
}

function* suspended_block(url, browser) {
  info("### Start test for suspended-block ###");
  browser.loadURI(url);

  info("- page should have playing audio -");
  yield wait_for_event(browser, "DOMAudioPlaybackStarted");

  info("- the default suspended state of all audio should be non-suspened-");
  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
                                   check_all_audio_suspended);

  info("- block autoplay audio -");
  browser.blockMedia();
  yield ContentTask.spawn(browser, SuspendedType.SUSPENDED_BLOCK,
                                   check_autoplay_audio_suspended);
  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
                                   check_nonautoplay_audio_suspended);

  info("- no audio can be playback during suspended-block -");
  yield ContentTask.spawn(browser, SuspendedType.SUSPENDED_BLOCK,
                                   play_nonautoplay_audio_should_be_blocked);

  info("- both audio should be resumed at the same time -");
  browser.resumeMedia();
  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
                                   check_all_audio_suspended);
}

add_task(function* setup_test_preference() {
  yield new Promise(resolve => {
    SpecialPowers.pushPrefEnv({"set": [
      ["media.useAudioChannelService.testing", true]
    ]}, resolve);
  });
});

add_task(function* test_suspended_pause() {
  yield BrowserTestUtils.withNewTab({
      gBrowser,
      url: "about:blank"
    }, suspended_pause.bind(this, PAGE));
});

add_task(function* test_suspended_pause_disposable() {
  yield BrowserTestUtils.withNewTab({
      gBrowser,
      url: "about:blank"
    }, suspended_pause_disposable.bind(this, PAGE));
});

add_task(function* test_suspended_stop_disposable() {
  yield BrowserTestUtils.withNewTab({
      gBrowser,
      url: "about:blank"
    }, suspended_stop_disposable.bind(this, PAGE));
});

add_task(function* test_suspended_block() {
  yield BrowserTestUtils.withNewTab({
      gBrowser,
      url: "about:blank"
    }, suspended_block.bind(this, PAGE));
});