summaryrefslogtreecommitdiffstats
path: root/dom/media/mediacontrol/tests/browser/browser_media_control_seekto.js
blob: 70b75841b61d1e8c79d1229c8eb87c8cc44801b1 (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
const PAGE_URL =
  "https://example.com/browser/dom/media/mediacontrol/tests/browser/file_non_autoplay.html";

const testVideoId = "video";

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 if the `seekto` action can be sent when calling
 * media controller's `seekTo()`. In addition, the seeking related properties
 * which would be sent to the action handler should also be correct as what we
 * set in `seekTo()`.
 */
add_task(async function testSetPositionState() {
  info(`open media page`);
  const tab = await createLoadedTabWrapper(PAGE_URL);

  info(`start media`);
  await playMedia(tab, testVideoId);

  const seektime = 0;
  info(`seek to ${seektime} seconds.`);
  await PerformSeekTo(tab, {
    seekTime: seektime,
  });

  info(`seek to ${seektime} seconds and set fastseek to boolean`);
  await PerformSeekTo(tab, {
    seekTime: seektime,
    fastSeek: true,
  });

  info(`seek to ${seektime} seconds and set fastseek to false`);
  await PerformSeekTo(tab, {
    seekTime: seektime,
    fastSeek: false,
  });

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

/**
 * The following is helper function.
 */
async function PerformSeekTo(tab, seekDetails) {
  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [seekDetails, testVideoId],
    (seekDetails, Id) => {
      const { seekTime, fastSeek } = seekDetails;
      content.navigator.mediaSession.setActionHandler("seekto", details => {
        ok(details.seekTime != undefined, "Seektime must be presented");
        is(seekTime, details.seekTime, "Get correct seektime");
        if (fastSeek) {
          is(fastSeek, details.fastSeek, "Get correct fastSeek");
        } else {
          ok(
            details.fastSeek === undefined,
            "Details should not contain fastSeek"
          );
        }
        // We use `onseek` as a hint to know if the `seekto` has been received
        // or not. The reason we don't return a resolved promise instead is
        // because if we do so, it can't guarantees that the `seekto` action
        // handler has been set before calling `mediaController.seekTo`.
        content.document.getElementById(Id).currentTime = seekTime;
      });
    }
  );
  const seekPromise = SpecialPowers.spawn(
    tab.linkedBrowser,
    [testVideoId],
    Id => {
      const video = content.document.getElementById(Id);
      return new Promise(r => (video.onseeking = r()));
    }
  );
  const { seekTime, fastSeek } = seekDetails;
  tab.linkedBrowser.browsingContext.mediaController.seekTo(seekTime, fastSeek);
  await seekPromise;
}