Update testharness.js from upstream
[WebKit-https.git] / LayoutTests / webrtc / video-mute.html
1 <!doctype html>
2 <html>
3     <head>
4         <meta charset="utf-8">
5         <title>Testing muting video</title>
6         <script src="../resources/testharness.js"></script>
7         <script src="../resources/testharnessreport.js"></script>
8     </head>
9     <body>
10         <div>Video should be running, go to black and running.</div>
11         <div>Following, should be a snapshot of the video, a black frame and a snapshot of the video.</div>
12         <video id="video" autoplay playsInline width="320" height="240"></video>
13         <canvas id="canvas1" width="320" height="240"></canvas>
14         <canvas id="canvas2" width="320" height="240"></canvas>
15         <canvas id="canvas3" width="320" height="240"></canvas>
16         <script src ="routines.js"></script>
17         <script>
18 var track;
19 var remoteTrack;
20 var receivingConnection;
21 promise_test((test) => {
22     if (window.testRunner)
23         testRunner.setUserMediaPermission(true);
24
25     return navigator.mediaDevices.getUserMedia({video: {width: 320, height: 240, facingMode: "environment"}}).then((localStream) => {
26         return new Promise((resolve, reject) => {
27             track = localStream.getVideoTracks()[0];
28
29             createConnections((firstConnection) => {
30                 firstConnection.addTrack(track, localStream);
31             }, (secondConnection) => {
32                 receivingConnection = secondConnection;
33                 secondConnection.ontrack = (trackEvent) => {
34                     remoteTrack = trackEvent.track;
35                     resolve(trackEvent.streams[0]);
36                 };
37             });
38             setTimeout(() => reject("Test timed out"), 5000);
39         });
40     }).then((remoteStream) => {
41         video.srcObject = remoteStream;
42         return video.play();
43     });
44 }, "Setting video exchange");
45
46 promise_test(() => {
47     if (receivingConnection.connectionState === "connected")
48         return Promise.resolve();
49     return new Promise((resolve, reject) => {
50         receivingConnection.onconnectionstatechange = () => {
51             if (receivingConnection.connectionState === "connected")
52                 resolve();
53         };
54         setTimeout(() => reject("Test timed out"), 5000);
55     });
56 }, "Ensuring connection state is connected");
57
58 promise_test((test) => {
59     return checkVideoBlack(false, canvas1, video);
60 }, "Track is enabled, video should not be black");
61
62 promise_test((test) => {
63     track.enabled = false;
64     return checkVideoBlack(true, canvas2, video);
65 }, "Track is disabled, video should be black");
66
67 async function getInboundRTPStatsNumberOfDecodedFrames(connection)
68 {
69     var report = await connection.getStats();
70     var framesDecoded;
71     report.forEach((statItem) => {
72         if (statItem.type === "inbound-rtp")
73             framesDecoded = statItem.framesDecoded;
74     });
75     return framesDecoded;
76 }
77
78 async function testFrameDecodedIncreased(connection, count, previousFramesNumber)
79 {
80     if (previousFramesNumber === undefined) {
81         let number = await getInboundRTPStatsNumberOfDecodedFrames(connection);
82         await waitFor(1000);
83         return testFrameDecodedIncreased(connection, 0, number);
84     }
85
86     var number = await getInboundRTPStatsNumberOfDecodedFrames(connection);
87     if (previousFramesNumber && number > previousFramesNumber)
88         return;
89
90     if (count >= 20)
91         return Promise.reject("test increasing frame encoded timed out");
92
93     await waitFor(1000);
94     return testFrameDecodedIncreased(connection, ++count, previousFramesNumber);
95 }
96
97 promise_test((test) => {
98     return testFrameDecodedIncreased(receivingConnection);
99 }, "If disabled, black frames should still be coming");
100
101 promise_test((test) => {
102     track.enabled = true;
103     return checkVideoBlack(false, canvas2, video);
104 }, "Track is enabled, video should not be black 2");
105
106         </script>
107     </body>
108 </html>