Improve some WebRTC flaky tests
[WebKit-https.git] / LayoutTests / webrtc / video-replace-track-to-null.html
1 <!doctype html>
2 <html>
3     <head>
4         <meta charset="utf-8">
5         <title>Testing basic video exchange from offerer to receiver</title>
6         <script src="../resources/testharness.js"></script>
7         <script src="../resources/testharnessreport.js"></script>
8     </head>
9     <body>
10 <div id="log"></div>
11         <video id="video" autoplay=""></video>
12         <canvas id="canvas" width="640" height="480"></canvas>
13         <script src ="routines.js"></script>
14         <script>
15 video = document.getElementById("video");
16 canvas = document.getElementById("canvas");
17
18 function grabImagePixels()
19 {
20     canvas.width = video.videoWidth;
21     canvas.height = video.videoHeight;
22     canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
23
24     imageData = canvas.getContext('2d').getImageData(20, 20, 100, 100);
25     return imageData.data;
26  }
27
28 var firstFrameData;
29
30 function storeFrame()
31 {
32     firstFrameData = grabImagePixels();
33 }
34
35 function testCameraImage()
36 {
37     data = grabImagePixels();
38
39     assert_true(data[0] < 20);
40     assert_true(data[1] < 20);
41     assert_true(data[2] < 20);
42
43     var same = true;
44     for (var cptr = 0; cptr < data.length; ++cptr) {
45         if (data[cptr] != firstFrameData[cptr]) {
46             same = false;
47             break;
48         }
49     }
50     assert_false(same);
51 }
52
53 function testStoppedImage()
54 {
55     assert_array_equals(grabImagePixels(), firstFrameData);
56 }
57
58 promise_test((test) => {
59     if (window.testRunner)
60         testRunner.setUserMediaPermission(true);
61
62     var sender;
63     var frontStream;
64     return navigator.mediaDevices.getUserMedia({ video: true }).then((stream) => {
65         frontStream = stream;
66     }).then(() => {
67         return new Promise((resolve, reject) => {
68             if (window.internals)
69                 internals.useMockRTCPeerConnectionFactory("TwoRealPeerConnections");
70
71             createConnections((firstConnection) => {
72                 sender = firstConnection.addTrack(frontStream.getVideoTracks()[0], frontStream);
73             }, (secondConnection) => {
74                 secondConnection.ontrack = (trackEvent) => {
75                     resolve(trackEvent.streams[0]);
76                 };
77             });
78             setTimeout(() => reject("Test timed out"), 5000);
79         });
80     }).then((remoteStream) => {
81         video.srcObject = remoteStream;
82         return video.play();
83     }).then(() => {
84         return waitFor(100);
85     }).then(() => {
86         storeFrame();
87         return waitFor(100);
88     }).then(() => {
89         testCameraImage();
90     }).then(() => {
91         promise = sender.replaceTrack(null);
92         assert_true(!!sender.track);
93         return promise;
94     }).then(() => {
95         return waitFor(100);
96     }).then(() => {
97         storeFrame();
98         return waitFor(100);
99     }).then(() => {
100         testStoppedImage();
101     });
102 }, "Stopping sending video using replaceTrack");
103         </script>
104     </body>
105 </html>