summaryrefslogtreecommitdiffstats
path: root/dom/media/mediacontrol/tests/browser/browser_media_control_audio_focus_within_a_page.js
blob: 5db37059dd0680b2eaf5f382d6074fb58031814a (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
const mainPageURL =
  "https://example.com/browser/dom/media/mediacontrol/tests/browser/file_main_frame_with_multiple_child_session_frames.html";
const frameURL =
  "https://example.com/browser/dom/media/mediacontrol/tests/browser/file_iframe_media.html";

const frame1 = "frame1";
const frame2 = "frame2";

add_task(async function setupTestingPref() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["media.mediacontrol.testingevents.enabled", true],
      ["dom.media.mediasession.enabled", true],
    ],
  });
});

/**
 * This test is used to check the behaviors when we play media from different
 * frames. When a page contains multiple frames, if those frames are using the
 * media session and set the metadata, then we have to know which frame owns the
 * audio focus that would be the last tab playing media. When the frame owns
 * audio focus, it means its metadata would be displayed on the virtual control
 * interface if it has a media session.
 */
add_task(async function testAudioFocusChangesAmongMultipleFrames() {
  /**
   * Play the media from the main frame, so it would own the audio focus and
   * its metadata should be shown on the virtual control interface. As the main
   * frame doesn't use media session, the current metadata would be the default
   * metadata.
   */
  const tab = await createLoadedTabWrapper(mainPageURL);
  await playAndWaitUntilMetadataChanged(tab);
  await isGivenTabUsingDefaultMetadata(tab);

  /**
   * Play media for frame1, so the audio focus switches to frame1 because it's
   * the last tab playing media and frame1's metadata should be displayed.
   */
  await loadPageForFrame(tab, frame1, frameURL);
  let metadata = await setMetadataAndGetReturnResult(tab, frame1);
  await playAndWaitUntilMetadataChanged(tab, frame1);
  isCurrentMetadataEqualTo(metadata);

  /**
   * Play media for frame2, so the audio focus switches to frame2 because it's
   * the last tab playing media and frame2's metadata should be displayed.
   */
  await loadPageForFrame(tab, frame2, frameURL);
  metadata = await setMetadataAndGetReturnResult(tab, frame2);
  await playAndWaitUntilMetadataChanged(tab, frame2);
  isCurrentMetadataEqualTo(metadata);

  /**
   * Remove tab and end test.
   */
  await tab.close();
});

add_task(async function testAudioFocusChangesAfterPausingAudioFocusOwner() {
  /**
   * Play the media from the main frame, so it would own the audio focus and
   * its metadata should be shown on the virtual control interface. As the main
   * frame doesn't use media session, the current metadata would be the default
   * metadata.
   */
  const tab = await createLoadedTabWrapper(mainPageURL);
  await playAndWaitUntilMetadataChanged(tab);
  await isGivenTabUsingDefaultMetadata(tab);

  /**
   * Play media for frame1, so the audio focus switches to frame1 because it's
   * the last tab playing media and frame1's metadata should be displayed.
   */
  await loadPageForFrame(tab, frame1, frameURL);
  let metadata = await setMetadataAndGetReturnResult(tab, frame1);
  await playAndWaitUntilMetadataChanged(tab, frame1);
  isCurrentMetadataEqualTo(metadata);

  /**
   * Pause media for frame1, so the audio focus switches back to the main frame
   * which is still playing media.
   */
  await pauseAndWaitUntilMetadataChangedFrom(tab, frame1);
  await isGivenTabUsingDefaultMetadata(tab);

  /**
   * Remove tab and end test.
   */
  await tab.close();
});

add_task(async function testAudioFocusUnchangesAfterPausingAudioFocusOwner() {
  /**
   * Play the media from the main frame, so it would own the audio focus and
   * its metadata should be shown on the virtual control interface. As the main
   * frame doesn't use media session, the current metadata would be the default
   * metadata.
   */
  const tab = await createLoadedTabWrapper(mainPageURL);
  await playAndWaitUntilMetadataChanged(tab);
  await isGivenTabUsingDefaultMetadata(tab);

  /**
   * Play media for frame1, so the audio focus switches to frame1 because it's
   * the last tab playing media and frame1's metadata should be displayed.
   */
  await loadPageForFrame(tab, frame1, frameURL);
  let metadata = await setMetadataAndGetReturnResult(tab, frame1);
  await playAndWaitUntilMetadataChanged(tab, frame1);
  isCurrentMetadataEqualTo(metadata);

  /**
   * Pause main frame's media first. When pausing frame1's media, there are not
   * other frames playing media, so frame1 still owns the audio focus and its
   * metadata should be displayed.
   */
  await pauseMediaFrom(tab);
  isCurrentMetadataEqualTo(metadata);

  /**
   * Remove tab and end test.
   */
  await tab.close();
});

add_task(
  async function testSwitchAudioFocusToMainFrameAfterRemovingAudioFocusOwner() {
    /**
     * Play the media from the main frame, so it would own the audio focus and
     * its metadata should be displayed on the virtual control interface. As the
     * main frame doesn't use media session, the current metadata would be the
     * default metadata.
     */
    const tab = await createLoadedTabWrapper(mainPageURL);
    await playAndWaitUntilMetadataChanged(tab);
    await isGivenTabUsingDefaultMetadata(tab);

    /**
     * Play media for frame1, so the audio focus switches to frame1 because it's
     * the last tab playing media and frame1's metadata should be displayed.
     */
    await loadPageForFrame(tab, frame1, frameURL);
    let metadata = await setMetadataAndGetReturnResult(tab, frame1);
    await playAndWaitUntilMetadataChanged(tab, frame1);
    isCurrentMetadataEqualTo(metadata);

    /**
     * Remove frame1, the audio focus would switch to the main frame which
     * metadata should be displayed.
     */
    await Promise.all([
      waitUntilDisplayedMetadataChanged(),
      removeFrame(tab, frame1),
    ]);
    await isGivenTabUsingDefaultMetadata(tab);

    /**
     * Remove tab and end test.
     */
    await tab.close();
  }
);

add_task(
  async function testSwitchAudioFocusToIframeAfterRemovingAudioFocusOwner() {
    /**
     * Play media for frame1, so frame1 owns the audio focus and frame1's metadata
     * should be displayed.
     */
    const tab = await createLoadedTabWrapper(mainPageURL);
    await loadPageForFrame(tab, frame1, frameURL);
    let metadataFrame1 = await setMetadataAndGetReturnResult(tab, frame1);
    await playAndWaitUntilMetadataChanged(tab, frame1);
    isCurrentMetadataEqualTo(metadataFrame1);

    /**
     * Play media for frame2, so the audio focus switches to frame2 because it's
     * the last tab playing media and frame2's metadata should be displayed.
     */
    await loadPageForFrame(tab, frame2, frameURL);
    let metadataFrame2 = await setMetadataAndGetReturnResult(tab, frame2);
    await playAndWaitUntilMetadataChanged(tab, frame2);
    isCurrentMetadataEqualTo(metadataFrame2);

    /**
     * Remove frame2, the audio focus would switch to frame1 which metadata should
     * be displayed.
     */
    await Promise.all([
      waitUntilDisplayedMetadataChanged(),
      removeFrame(tab, frame2),
    ]);
    isCurrentMetadataEqualTo(metadataFrame1);

    /**
     * Remove tab and end test.
     */
    await tab.close();
  }
);

add_task(async function testNoAudioFocusAfterRemovingAudioFocusOwner() {
  /**
   * Play the media from the main frame, so it would own the audio focus and
   * its metadata should be shown on the virtual control interface. As the main
   * frame doesn't use media session, the current metadata would be the default
   * metadata.
   */
  const tab = await createLoadedTabWrapper(mainPageURL);
  await playAndWaitUntilMetadataChanged(tab);
  await isGivenTabUsingDefaultMetadata(tab);

  /**
   * Play media for frame1, so the audio focus switches to frame1 because it's
   * the last tab playing media and frame1's metadata should be displayed.
   */
  await loadPageForFrame(tab, frame1, frameURL);
  let metadata = await setMetadataAndGetReturnResult(tab, frame1);
  await playAndWaitUntilMetadataChanged(tab, frame1);
  isCurrentMetadataEqualTo(metadata);

  /**
   * Pause media in main frame and then remove frame1. As the frame which owns
   * the audio focus is removed and no frame is still playing media, the current
   * metadata would be the default metadata.
   */
  await pauseMediaFrom(tab);
  await Promise.all([
    waitUntilDisplayedMetadataChanged(),
    removeFrame(tab, frame1),
  ]);
  await isGivenTabUsingDefaultMetadata(tab);

  /**
   * Remove tab and end test.
   */
  await tab.close();
});

/**
 * The following are helper functions.
 */
function loadPageForFrame(tab, frameId, pageUrl) {
  info(`start to load page for ${frameId}`);
  return SpecialPowers.spawn(
    tab.linkedBrowser,
    [frameId, pageUrl],
    async (id, url) => {
      const iframe = content.document.getElementById(id);
      if (!iframe) {
        ok(false, `can not get iframe '${id}'`);
      }
      iframe.src = url;
      await new Promise(r => (iframe.onload = r));
      // Set the document title that would be used as the value for properties
      // in frame's medadata.
      iframe.contentDocument.title = id;
    }
  );
}

function playMediaFrom(tab, frameId = undefined) {
  return SpecialPowers.spawn(tab.linkedBrowser, [frameId], id => {
    if (id == undefined) {
      info(`start to play media from main frame`);
      const video = content.document.getElementById("video");
      if (!video) {
        ok(false, `can't get the media element!`);
      }
      return video.play();
    }

    info(`start to play media from ${id}`);
    const iframe = content.document.getElementById(id);
    if (!iframe) {
      ok(false, `can not get ${id}`);
    }
    iframe.contentWindow.postMessage("play", "*");
    return new Promise(r => {
      content.onmessage = event => {
        is(event.data, "played", `media started playing in ${id}`);
        r();
      };
    });
  });
}

function playAndWaitUntilMetadataChanged(tab, frameId = undefined) {
  const metadataChanged = frameId
    ? new Promise(r => (tab.controller.onmetadatachange = r))
    : waitUntilDisplayedMetadataChanged();
  return Promise.all([metadataChanged, playMediaFrom(tab, frameId)]);
}

function pauseMediaFrom(tab, frameId = undefined) {
  return SpecialPowers.spawn(tab.linkedBrowser, [frameId], id => {
    if (id == undefined) {
      info(`start to pause media from in frame`);
      const video = content.document.getElementById("video");
      if (!video) {
        ok(false, `can't get the media element!`);
      }
      return video.pause();
    }

    info(`start to pause media in ${id}`);
    const iframe = content.document.getElementById(id);
    if (!iframe) {
      ok(false, `can not get ${id}`);
    }
    iframe.contentWindow.postMessage("pause", "*");
    return new Promise(r => {
      content.onmessage = event => {
        is(event.data, "paused", `media paused in ${id}`);
        r();
      };
    });
  });
}

function pauseAndWaitUntilMetadataChangedFrom(tab, frameId = undefined) {
  const metadataChanged = waitUntilDisplayedMetadataChanged();
  return Promise.all([metadataChanged, pauseMediaFrom(tab, frameId)]);
}

function setMetadataAndGetReturnResult(tab, frameId) {
  info(`start to set metadata for ${frameId}`);
  return SpecialPowers.spawn(tab.linkedBrowser, [frameId], id => {
    const iframe = content.document.getElementById(id);
    if (!iframe) {
      ok(false, `can not get ${id}`);
    }
    iframe.contentWindow.postMessage("setMetadata", "*");
    info(`wait until we get metadata for ${id}`);
    return new Promise(r => {
      content.onmessage = event => {
        ok(
          event.data.title && event.data.artist && event.data.album,
          "correct return format"
        );
        r(event.data);
      };
    });
  });
}

function removeFrame(tab, frameId) {
  info(`remove ${frameId}`);
  return SpecialPowers.spawn(tab.linkedBrowser, [frameId], id => {
    const iframe = content.document.getElementById(id);
    if (!iframe) {
      ok(false, `can not get ${id}`);
    }
    content.document.body.removeChild(iframe);
  });
}