Improve debugging ability of some webrtc tests
[WebKit-https.git] / LayoutTests / webrtc / video-addTrack.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         <video id="video" autoplay=""></video>
11         <canvas id="canvas" width="640" height="480"></canvas>
12         <script src ="routines.js"></script>
13         <script>
14 video = document.getElementById("video");
15 canvas = document.getElementById("canvas");
16
17 function testImage(wait)
18 {
19   test(() => {
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(10, 325, 250, 1);
25     data = imageData.data;
26
27     var index = 20;
28     assert_true(data[index] < 100);
29     assert_true(data[index + 1] < 100);
30     assert_true(data[index + 2] < 100);
31
32     index = 80;
33     assert_true(data[index] > 200);
34     assert_true(data[index + 1] > 200);
35     assert_true(data[index + 2] > 200);
36
37     index += 80;
38     assert_true(data[index] > 200);
39     assert_true(data[index + 1] > 200);
40     assert_true(data[index + 2] < 100);
41   }, "Testing image result, wait = " + wait);
42 }
43
44 function testBasicVideoExchangeWithAddTrack(waitForSecondTrack)
45 {
46   promise_test((test) => {
47     if (window.testRunner)
48         testRunner.setUserMediaPermission(true);
49
50     return navigator.mediaDevices.getUserMedia({audio: true, video: true}).then((stream) => {
51         return new Promise((resolve, reject) => {
52             createConnections((firstConnection) => {
53                 assert_equals(stream.getTracks().length, 2);
54                 stream.getTracks().forEach(track => firstConnection.addTrack(track));
55             }, (secondConnection) => {
56                 var count = 0;
57                 secondConnection.ontrack = (trackEvent) => {
58                     window.test(function() {
59                         if (trackEvent.track.kind === "video")
60                             assert_equals(trackEvent.track.id, stream.getVideoTracks()[0].id);
61                         else
62                             assert_equals(trackEvent.track.id, stream.getAudioTracks()[0].id);
63                     }, " track " + count + ", wait = " + waitForSecondTrack);
64                     if (count++ === (waitForSecondTrack ? 1 : 0))
65                         resolve(trackEvent.streams[0]);
66                 };
67             });
68             setTimeout(() => reject("Test timed out"), 5000);
69         });
70     }).then((stream) => {
71         video.srcObject = stream;
72         return video.play();
73     }).then(() => {
74         testImage(waitForSecondTrack);
75     });
76   }, "Basic video exchange with addTrack - " + (waitForSecondTrack ? "waiting for second track before playing" : "not waiting for second track to play"));
77 }
78 testBasicVideoExchangeWithAddTrack(true);
79 testBasicVideoExchangeWithAddTrack(false);
80         </script>
81     </body>
82 </html>