summaryrefslogtreecommitdiffstats
path: root/dom/media/autoplay/test/mochitest/file_autoplay_policy_eventdown_activation.html
blob: e25b6401d1e335f72c5226e8da3c3def40e800f5 (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
<!DOCTYPE HTML>
<html>

<head>
  <title>Autoplay policy window</title>
  <style>
    video {
      width: 50%;
      height: 50%;
    }
  </style>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <script type="text/javascript" src="manifest.js"></script>
  <script type="text/javascript" src="AutoplayTestUtils.js"></script>
</head>

<body>
  <pre id="test">
      <script>

        window.ok = window.opener.ok;
        window.is = window.opener.is;
        window.info = window.opener.info;

        async function testEventDownActivates(eventNames, activator) {
          let element = document.createElement("video");
          element.preload = "auto";
          element.src = "short.mp4";
          document.body.appendChild(element);

          await once(element, "loadedmetadata");

          let played = await element.play().then(() => true, () => false);
          ok(!played, "Document should start out not activated, with playback blocked.");

          let x = eventNames.map(
            (eventName) => {
              return new Promise(function (resolve, reject) {
                window.addEventListener(eventName, async function (event) {
                  let p = await element.play().then(() => true, () => false);
                  ok(p, "Expect to be activated already in " + eventName);
                  resolve();
                });
              });
            });

          activator();

          await Promise.all(x);

          removeNodeAndSource(element);
        }

        nextWindowMessage().then(
          async (event) => {
            try {
              if (event.data == "run keydown test") {
                await testEventDownActivates(["keydown", "keypress", "keyup"], () => {
                  document.body.focus();
                  synthesizeKey(" ");
                });
              } else if (event.data == "run mousedown test") {
                let events = ["mousedown", "mouseup", "click"];
                if (getAndroidVersion() < 0) {
                  // Non-Android, also listen on pointer events.
                  events.push("pointerdown", "pointerup");
                }
                await testEventDownActivates(events, () => {
                  synthesizeMouseAtCenter(document.body, {});
                });
              } else {
                ok(false, "unexpected message");
              }
            } catch (e) {
              ok(false, "Caught exception " + e + " " + e.message + " " + e.stackTrace);
            }
            event.source.postMessage("done", "*");
          });

      </script>
    </pre>
</body>

</html>