summaryrefslogtreecommitdiffstats
path: root/dom/media/autoplay/test/browser/head.js
blob: c84850900a86cdd1dc185ab57ca76b426cadc124 (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
/**
 * Return a web-based URL for a given file based on the testing directory.
 * @param {String} fileName
 *        file that caller wants its web-based url
 * @param {Boolean} crossOrigin [optional]
 *        if set, then return a url with different origin. The default value is
 *        false.
 */
function GetTestWebBasedURL(fileName, { crossOrigin = false } = {}) {
  const origin = crossOrigin ? "http://example.org" : "http://example.com";
  return (
    getRootDirectory(gTestPath).replace("chrome://mochitests/content", origin) +
    fileName
  );
}

/**
 * Runs a content script that creates an autoplay video.
 * @param {browserElement} browser
 *        the browser to run the script in
 * @param {object} args
 *        test case definition, required members
 *        {
 *          mode: String, "autoplay attribute" or "call play".
 *        }
 */
function loadAutoplayVideo(browser, args) {
  return SpecialPowers.spawn(browser, [args], async args => {
    info("- create a new autoplay video -");
    let video = content.document.createElement("video");
    video.id = "v1";
    video.didPlayPromise = new Promise((resolve, reject) => {
      video.addEventListener(
        "playing",
        e => {
          video.didPlay = true;
          resolve();
        },
        { once: true }
      );
      video.addEventListener(
        "blocked",
        e => {
          video.didPlay = false;
          resolve();
        },
        { once: true }
      );
    });
    if (args.mode == "autoplay attribute") {
      info("autoplay attribute set to true");
      video.autoplay = true;
    } else if (args.mode == "call play") {
      info("will call play() when reached loadedmetadata");
      video.addEventListener(
        "loadedmetadata",
        e => {
          video.play().then(
            () => {
              info("video play() resolved");
            },
            () => {
              info("video play() rejected");
            }
          );
        },
        { once: true }
      );
    } else {
      ok(false, "Invalid 'mode' arg");
    }
    video.src = "gizmo.mp4";
    content.document.body.appendChild(video);
  });
}

/**
 * Runs a content script that checks whether the video created by
 * loadAutoplayVideo() started playing.
 * @param {browserElement} browser
 *        the browser to run the script in
 * @param {object} args
 *        test case definition, required members
 *        {
 *          name: String, description of test.
 *          mode: String, "autoplay attribute" or "call play".
 *          shouldPlay: boolean, whether video should play.
 *        }
 */
function checkVideoDidPlay(browser, args) {
  return SpecialPowers.spawn(browser, [args], async args => {
    let video = content.document.getElementById("v1");
    await video.didPlayPromise;
    is(
      video.didPlay,
      args.shouldPlay,
      args.name +
        " should " +
        (!args.shouldPlay ? "not " : "") +
        "be able to autoplay"
    );
    video.src = "";
    content.document.body.remove(video);
  });
}

/**
 * Create a tab that will load the given url, and define an autoplay policy
 * check function inside the content window in that tab. This function should
 * only be used when `dom.media.autoplay-policy-detection.enabled` is true.
 * @param {url} url
 *        the url which the created tab should load
 */
async function createTabAndSetupPolicyAssertFunc(url) {
  let tab = await BrowserTestUtils.openNewForegroundTab(window.gBrowser, url);
  await SpecialPowers.spawn(tab.linkedBrowser, [], _ => {
    content.video = content.document.createElement("video");
    content.ac = new content.AudioContext();
    content.assertAutoplayPolicy = ({
      resultForElementType,
      resultForElement,
      resultForContextType,
      resultForContext,
    }) => {
      is(
        content.navigator.getAutoplayPolicy("mediaelement"),
        resultForElementType,
        "getAutoplayPolicy('mediaelement') returns correct value"
      );
      is(
        content.navigator.getAutoplayPolicy(content.video),
        resultForElement,
        "getAutoplayPolicy(content.video) returns correct value"
      );
      // note, per spec "allowed-muted" won't be used for audio context.
      is(
        content.navigator.getAutoplayPolicy("audiocontext"),
        resultForContextType,
        "getAutoplayPolicy('audiocontext') returns correct value"
      );
      is(
        content.navigator.getAutoplayPolicy(content.ac),
        resultForContext,
        "getAutoplayPolicy(content.ac) returns correct value"
      );
    };
  });
  return tab;
}