summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/speculation-rules/prerender/resources/media-play.html
blob: 48cd3beb3b2611cd524d7bc03eb8d1063373797a (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
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/speculation-rules/prerender/resources/utils.js"></script>
<script src="/speculation-rules/prerender/resources/deferred-promise-utils.js"></script>
<audio id="audio" loop></audio>
<video id="video" type="video/mp4"></video>
<script>
const params = new URLSearchParams(location.search);
// The main test page (restriction-media-play.html) loads the initiator page,
// then the initiator page will prerender itself with the `prerendering`
// parameter.
const isPrerendering = params.has('prerendering');

if (!isPrerendering) {
  loadInitiatorPage();
} else {
  const mediaType = params.get('type');
  const media = document.getElementById(mediaType);
  media.src = "./bear-av1-opus.mp4";

  const prerenderEventCollector = new PrerenderEventCollector();
  const promise = new Promise((resolve, reject) => {
    media.play();

    media.onloadedmetadata = () => {
      prerenderEventCollector.addEvent(
          'fired loadedmetadata event after prerendering is activated');
      resolve();
    };
    media.onloadstart = () => {
      // Wait some time to give the test a chance to load the data and fail the
      // test.
      setTimeout(() => {
        prerenderEventCollector.start(promise, 'Media.Play');
      }, 100);
    };
    media.onerror = reject;
  });
}
</script>