summaryrefslogtreecommitdiffstats
path: root/dom/media/mediacontrol/tests/browser/browser_default_action_handler.js
blob: c33c08b0c26a7512e770d698e5ff50aaf68d3785 (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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
const PAGE_URL =
  "https://example.com/browser/dom/media/mediacontrol/tests/browser/file_non_autoplay.html";
const PAGE2_URL =
  "https://example.com/browser/dom/media/mediacontrol/tests/browser/file_main_frame_with_multiple_child_session_frames.html";
const IFRAME_URL =
  "https://example.com/browser/dom/media/mediacontrol/tests/browser/file_iframe_media.html";
const CORS_IFRAME_URL =
  "https://example.org/browser/dom/media/mediacontrol/tests/browser/file_iframe_media.html";
const CORS_IFRAME2_URL =
  "https://test1.example.org/browser/dom/media/mediacontrol/tests/browser/file_iframe_media.html";
const videoId = "video";

/**
 * This test is used to check the scenario when we should use the customized
 * action handler and the the default action handler (play/pause/stop).
 * If a frame (DOM Window, it could be main frame or an iframe) has active media
 * session, then it should use the customized action handler it it has one.
 * Otherwise, the default action handler should be used.
 */
add_task(async function setupTestingPref() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["dom.media.mediasession.enabled", true],
      ["media.mediacontrol.testingevents.enabled", true],
    ],
  });
});

add_task(async function triggerDefaultActionHandler() {
  // Default handler should be triggered no matter if media session exists or not.
  const kCreateMediaSession = [true, false];
  for (const shouldCreateSession of kCreateMediaSession) {
    info(`open page and start media`);
    const tab = await createLoadedTabWrapper(PAGE_URL);
    await playMedia(tab, videoId);

    if (shouldCreateSession) {
      info(
        `media has started, so created session should become active session`
      );
      await Promise.all([
        waitUntilActiveMediaSessionChanged(),
        createMediaSession(tab),
      ]);
    }

    info(`test 'pause' action`);
    await simulateMediaAction(tab, "pause");

    info(`default action handler should pause media`);
    await checkOrWaitUntilMediaPauses(tab, { videoId });

    info(`test 'play' action`);
    await simulateMediaAction(tab, "play");

    info(`default action handler should resume media`);
    await checkOrWaitUntilMediaPlays(tab, { videoId });

    info(`test 'stop' action`);
    await simulateMediaAction(tab, "stop");

    info(`default action handler should pause media`);
    await checkOrWaitUntilMediaPauses(tab, { videoId });

    const controller = tab.linkedBrowser.browsingContext.mediaController;
    ok(
      !controller.isActive,
      `controller should be deactivated after receiving stop`
    );

    info(`remove tab`);
    await tab.close();
  }
});

add_task(async function triggerNonDefaultHandlerWhenSetCustomizedHandler() {
  info(`open page and start media`);
  const tab = await createLoadedTabWrapper(PAGE_URL);
  await Promise.all([
    new Promise(r => (tab.controller.onactivated = r)),
    startMedia(tab, { videoId }),
  ]);

  const kActions = ["play", "pause", "stop"];
  for (const action of kActions) {
    info(`set action handler for '${action}'`);
    await setActionHandler(tab, action);

    info(`press '${action}' should trigger action handler (not a default one)`);
    await simulateMediaAction(tab, action);
    await waitUntilActionHandlerIsTriggered(tab, action);

    info(`action handler doesn't pause media, media should keep playing`);
    await checkOrWaitUntilMediaPlays(tab, { videoId });
  }

  info(`remove tab`);
  await tab.close();
});

add_task(
  async function triggerDefaultHandlerToPausePlaybackOnInactiveSession() {
    const kIframeUrls = [IFRAME_URL, CORS_IFRAME_URL];
    for (const url of kIframeUrls) {
      const kActions = ["play", "pause", "stop"];
      for (const action of kActions) {
        info(`open page and load iframe`);
        const tab = await createLoadedTabWrapper(PAGE_URL);
        const frameId = "iframe";
        await loadIframe(tab, frameId, url);

        info(`start media from iframe would make it become active session`);
        await Promise.all([
          new Promise(r => (tab.controller.onactivated = r)),
          startMedia(tab, { frameId }),
        ]);

        info(`press '${action}' should trigger iframe's action handler`);
        await setActionHandler(tab, action, frameId);
        await simulateMediaAction(tab, action);
        await waitUntilActionHandlerIsTriggered(tab, action, frameId);

        info(`start media from main frame so iframe would become inactive`);
        // When action is `play`, controller is already playing, because above
        // code won't pause media. So we need to wait for the active session
        // changed to ensure the following tests can be executed on the right
        // browsing context.
        let waitForControllerStatusChanged =
          action == "play"
            ? waitUntilActiveMediaSessionChanged()
            : ensureControllerIsPlaying(tab.controller);
        await Promise.all([
          waitForControllerStatusChanged,
          startMedia(tab, { videoId }),
        ]);

        if (action == "play") {
          info(`pause media first in order to test 'play'`);
          await pauseAllMedia(tab);

          info(
            `press '${action}' would trigger default andler on main frame because it doesn't set action handler`
          );
          await simulateMediaAction(tab, action);
          await checkOrWaitUntilMediaPlays(tab, { videoId });

          info(
            `default handler should also be triggered on inactive iframe, which would resume media`
          );
          await checkOrWaitUntilMediaPlays(tab, { frameId });
        } else {
          info(
            `press '${action}' would trigger default andler on main frame because it doesn't set action handler`
          );
          await simulateMediaAction(tab, action);
          await checkOrWaitUntilMediaPauses(tab, { videoId });

          info(
            `default handler should also be triggered on inactive iframe, which would pause media`
          );
          await checkOrWaitUntilMediaPauses(tab, { frameId });
        }

        info(`remove tab`);
        await tab.close();
      }
    }
  }
);

add_task(async function onlyResumeActiveMediaSession() {
  info(`open page and load iframes`);
  const tab = await createLoadedTabWrapper(PAGE2_URL);
  const frame1Id = "frame1";
  const frame2Id = "frame2";
  await loadIframe(tab, frame1Id, CORS_IFRAME_URL);
  await loadIframe(tab, frame2Id, CORS_IFRAME2_URL);

  info(`start media from iframe1 would make it become active session`);
  await createMediaSession(tab, frame1Id);
  await Promise.all([
    waitUntilActiveMediaSessionChanged(),
    startMedia(tab, { frameId: frame1Id }),
  ]);

  info(`start media from iframe2 would make it become active session`);
  await createMediaSession(tab, frame2Id);
  await Promise.all([
    waitUntilActiveMediaSessionChanged(),
    startMedia(tab, { frameId: frame2Id }),
  ]);

  info(`press 'pause' should pause both iframes`);
  await simulateMediaAction(tab, "pause");
  await checkOrWaitUntilMediaPauses(tab, { frameId: frame1Id });
  await checkOrWaitUntilMediaPauses(tab, { frameId: frame2Id });

  info(
    `press 'play' should only resume iframe2 which has active media session`
  );
  await simulateMediaAction(tab, "play");
  await checkOrWaitUntilMediaPauses(tab, { frameId: frame1Id });
  await checkOrWaitUntilMediaPlays(tab, { frameId: frame2Id });

  info(`remove tab`);
  await tab.close();
});

/**
 * The following are helper functions.
 */
function startMedia(tab, { videoId, frameId }) {
  return SpecialPowers.spawn(
    tab.linkedBrowser,
    [videoId, frameId],
    (videoId, frameId) => {
      if (frameId) {
        return content.messageHelper(
          content.document.getElementById(frameId),
          "play",
          "played"
        );
      }
      return content.document.getElementById(videoId).play();
    }
  );
}

function pauseAllMedia(tab) {
  return SpecialPowers.spawn(tab.linkedBrowser, [], async () => {
    await content.messageHelper(
      content.document.getElementById("iframe"),
      "pause",
      "paused"
    );
    const videos = content.document.getElementsByTagName("video");
    for (let video of videos) {
      video.pause();
    }
  });
}

function createMediaSession(tab, frameId = null) {
  info(`create media session`);
  return SpecialPowers.spawn(tab.linkedBrowser, [frameId], async frameId => {
    if (frameId) {
      await content.messageHelper(
        content.document.getElementById(frameId),
        "create-media-session",
        "created-media-session"
      );
      return;
    }
    // simply calling a media session would create an instance.
    content.navigator.mediaSession;
  });
}

function checkOrWaitUntilMediaPauses(tab, { videoId, frameId }) {
  return SpecialPowers.spawn(
    tab.linkedBrowser,
    [videoId, frameId],
    (videoId, frameId) => {
      if (frameId) {
        return content.messageHelper(
          content.document.getElementById(frameId),
          "check-pause",
          "checked-pause"
        );
      }
      return new Promise(r => {
        const video = content.document.getElementById(videoId);
        if (video.paused) {
          ok(true, `media stopped playing`);
          r();
        } else {
          info(`wait until media stops playing`);
          video.onpause = () => {
            video.onpause = null;
            ok(true, `media stopped playing`);
            r();
          };
        }
      });
    }
  );
}

function checkOrWaitUntilMediaPlays(tab, { videoId, frameId }) {
  return SpecialPowers.spawn(
    tab.linkedBrowser,
    [videoId, frameId],
    (videoId, frameId) => {
      if (frameId) {
        return content.messageHelper(
          content.document.getElementById(frameId),
          "check-playing",
          "checked-playing"
        );
      }
      return new Promise(r => {
        const video = content.document.getElementById(videoId);
        if (!video.paused) {
          ok(true, `media is playing`);
          r();
        } else {
          info(`wait until media starts playing`);
          video.onplay = () => {
            video.onplay = null;
            ok(true, `media starts playing`);
            r();
          };
        }
      });
    }
  );
}

function setActionHandler(tab, action, frameId = null) {
  return SpecialPowers.spawn(
    tab.linkedBrowser,
    [action, frameId],
    async (action, frameId) => {
      if (frameId) {
        await content.messageHelper(
          content.document.getElementById(frameId),
          {
            cmd: "setActionHandler",
            action,
          },
          "setActionHandler-done"
        );
        return;
      }
      // Create this on the first function call
      if (content.actionHandlerPromises === undefined) {
        content.actionHandlerPromises = {};
      }
      content.actionHandlerPromises[action] = new Promise(r => {
        content.navigator.mediaSession.setActionHandler(action, () => {
          info(`receive ${action}`);
          r();
        });
      });
    }
  );
}

async function waitUntilActionHandlerIsTriggered(tab, action, frameId = null) {
  info(`wait until '${action}' action handler is triggered`);
  return SpecialPowers.spawn(
    tab.linkedBrowser,
    [action, frameId],
    (action, frameId) => {
      if (frameId) {
        return content.messageHelper(
          content.document.getElementById(frameId),
          {
            cmd: "checkActionHandler",
            action,
          },
          "checkActionHandler-done"
        );
      }
      const actionTriggerPromise = content.actionHandlerPromises[action];
      ok(actionTriggerPromise, `Has created promise for ${action}`);
      return actionTriggerPromise;
    }
  );
}

async function simulateMediaAction(tab, action) {
  const controller = tab.linkedBrowser.browsingContext.mediaController;
  if (!controller.isActive) {
    await new Promise(r => (controller.onactivated = r));
  }
  MediaControlService.generateMediaControlKey(action);
}

function loadIframe(tab, iframeId, url) {
  return SpecialPowers.spawn(
    tab.linkedBrowser,
    [iframeId, url],
    async (iframeId, url) => {
      const iframe = content.document.getElementById(iframeId);
      info(`load iframe with url '${url}'`);
      iframe.src = url;
      await new Promise(r => (iframe.onload = r));
      // create a helper to simplify communication process with iframe
      content.messageHelper = (target, sentMessage, expectedResponse) => {
        target.contentWindow.postMessage(sentMessage, "*");
        return new Promise(r => {
          content.onmessage = event => {
            if (event.data == expectedResponse) {
              ok(true, `Received response ${expectedResponse}`);
              content.onmessage = null;
              r();
            }
          };
        });
      };
    }
  );
}

function waitUntilActiveMediaSessionChanged() {
  return BrowserUtils.promiseObserved("active-media-session-changed");
}

function ensureControllerIsPlaying(controller) {
  return new Promise(r => {
    if (controller.isPlaying) {
      r();
      return;
    }
    controller.onplaybackstatechange = () => {
      if (controller.isPlaying) {
        r();
      }
    };
  });
}