summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/mediacapture-streams/MediaStream-MediaElement-firstframe.https.html
blob: 36641b58c91f74808783d1078185240326a06295 (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
<!doctype html>
<html>
<head>
<title>Assigning a MediaStream to a media element and not playing it results in rendering a first frame</title>
</head>
<body>
<p class="instructions">When prompted, accept to share your video stream.</p>
<h1 class="instructions">Description</h1>
<p class="instructions">This test checks that a HTMLMediaElement with an
assigned MediaStream with a video track fires the appropriate events to reach
the "canplay" event and readyState HAVE_ENOUGH_DATA even when not playing or
autoplaying.</p>
<video id="vid"></video>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/resources/testdriver.js></script>
<script src=/resources/testdriver-vendor.js></script>
<script src=permission-helper.js></script>
<script>
'use strict';
const vid = document.getElementById("vid");

promise_test(async t => {
  const wait = ms => new Promise(r => t.step_timeout(r, ms));
  const timeout = (promise, time, msg) => Promise.race([
    promise,
    wait(time).then(() => Promise.reject(new Error(msg)))
  ]);
  await setMediaPermission("granted", ["camera"]);
  const stream = await navigator.mediaDevices.getUserMedia({video: true});
  t.add_cleanup(() => stream.getTracks().forEach(track => track.stop()));
  vid.srcObject = stream;

  await timeout(new Promise(r => vid.oncanplay = r), 8000, "canplay timeout");
  assert_equals(vid.readyState, vid.HAVE_ENOUGH_DATA,
    "readyState is HAVE_ENOUGH_DATA after \"canplay\"");
}, "Tests that loading a MediaStream in a media element eventually results in \"canplay\" even when not playing or autoplaying");

promise_test(async t => {
  const wait = ms => new Promise(r => t.step_timeout(r, ms));
  const timeout = (promise, time, msg) => Promise.race([
    promise,
    wait(time).then(() => Promise.reject(new Error(msg)))
  ]);
  const unexpected = e => assert_unreached(`Got unexpected event ${e.type}`);
  const stream = await navigator.mediaDevices.getUserMedia({video: true});
  t.add_cleanup(() => {
    vid.ondurationchange = null;
    stream.getTracks().forEach(track => track.stop())
  });
  vid.srcObject = stream;

  vid.onloadstart = unexpected;
  vid.ondurationchange = unexpected;
  vid.onresize = unexpected;
  vid.onloadedmetadata = unexpected;
  vid.onloadeddata = unexpected;
  vid.oncanplay = unexpected;
  vid.oncanplaythrough = unexpected;

  await timeout(new Promise(r => vid.onloadstart = r), 8000,
    "loadstart timeout");
  vid.onloadstart = unexpected;

  await timeout(new Promise(r => vid.ondurationchange = r), 8000,
    "durationchange timeout");
  vid.ondurationchange = unexpected;
  assert_equals(vid.duration, Infinity, "duration changes to Infinity");

  await timeout(new Promise(r => vid.onresize = r), 8000,
    "resize timeout");
  vid.onresize = unexpected;
  assert_not_equals(vid.videoWidth, 0,
    "videoWidth is something after \"resize\"");
  assert_not_equals(vid.videoHeight, 0,
    "videoHeight is something after \"resize\"");

  await timeout(new Promise(r => vid.onloadedmetadata = r), 8000,
    "loadedmetadata timeout");
  vid.onloadedmetadata = unexpected;
  assert_greater_than_equal(vid.readyState, vid.HAVE_METADATA,
    "readyState is at least HAVE_METADATA after \"loadedmetadata\"");

  await timeout(new Promise(r => vid.onloadeddata = r), 8000,
    "loadeddata timeout");
  vid.onloadeddata = unexpected;
  assert_equals(vid.readyState, vid.HAVE_ENOUGH_DATA,
    "readyState is HAVE_ENOUGH_DATA after \"loadeddata\" since there's no buffering");

  await timeout(new Promise(r => vid.oncanplay = r), 8000, "canplay timeout");
  vid.oncanplay = unexpected;
  assert_equals(vid.readyState, vid.HAVE_ENOUGH_DATA,
    "readyState is HAVE_ENOUGH_DATA after \"canplay\" since there's no buffering");

  await timeout(new Promise(r => vid.oncanplaythrough = r), 8000,
    "canplaythrough timeout");
  vid.oncanplaythrough = unexpected;
  assert_equals(vid.readyState, vid.HAVE_ENOUGH_DATA,
    "readyState is HAVE_ENOUGH_DATA after \"canplaythrough\"");

  // Crank the event loop to see whether any more events are fired.
  await wait(100);
}, "Tests that loading a MediaStream in a media element sees all the expected (deterministic) events even when not playing or autoplaying");
</script>
</body>
</html>