1<!DOCTYPE html>
2<html>
3<head>
4 <script src="../../resources/js-test-pre.js"></script>
5 <script src="./resources/getUserMedia-helper.js"></script>
6</head>
7<body onload="start()">
8<p id="description"></p>
9<div id="console"></div>
10<video controls width="680" height="360"></video>
11<canvas width="680" height="360"></canvas>
12<script>
13 let canvas;
14 let context;
15 let mediaStream;
16 let video;
17
18 let buffer;
19
20 function isPixelBlack(pixel)
21 {
22 return pixel[0] === 0 && pixel[1] === 0 && pixel[2] === 0 && pixel[3] === 255;
23 }
24
25 function isPixelTransparent(pixel)
26 {
27 return pixel[0] === 0 && pixel[1] === 0 && pixel[2] === 0 && pixel[3] === 0;
28 }
29
30 function isPixelWhite(pixel)
31 {
32 return pixel[0] === 255 && pixel[1] === 255 && pixel[2] === 255 && pixel[3] === 255;
33 }
34
35 function attempt(numberOfTries, call, callback, successMessage)
36 {
37 if (numberOfTries <= 0) {
38 testFailed('Pixel check did not succeed after multiple tries.');
39 return;
40 }
41
42 let attemptSucceeded = call();
43 if (attemptSucceeded) {
44 testPassed(successMessage);
45 callback();
46
47 return;
48 }
49
50 setTimeout(function() {
51 attempt(--numberOfTries, call, callback, successMessage);
52 }, 50);
53 }
54
55 function repeatWithVideoPlayingAndFinishTest()
56 {
57 if (video.paused) {
58 debug('<br> ===== play video =====');
59 evalAndLog('video.play()');
60 beginTestRound();
61 } else
62 finishJSTest();
63 }
64
65 function reenableTrack()
66 {
67 mediaStream.getVideoTracks()[0].enabled = true;
68 debug('<br> === video track reenabled ===');
69
70 // The video is not guaranteed to render non-black frames before the canvas is drawn to and the pixels are checked.
71 // A timeout is used to ensure that the pixel check is done after the video renders non-black frames.
72 attempt(10, checkPixels, repeatWithVideoPlayingAndFinishTest, 'pixel was white.');
73 }
74
75 function checkPixels()
76 {
77 context.clearRect(0, 0, canvas.width, canvas.height);
78 buffer = context.getImageData(30, 242, 1, 1).data;
79 if(!isPixelTransparent(buffer)) {
80 testFailed('pixel was not transparent after clearing canvas.');
81 }
82
83 context.drawImage(video, 0, 0, canvas.width, canvas.height);
84 buffer = context.getImageData(30, 242, 1, 1).data;
85
86 if (mediaStream.getVideoTracks()[0].enabled)
87 return isPixelWhite(buffer);
88 else
89 return isPixelBlack(buffer);
90 }
91
92 function disableAllTracks()
93 {
94 mediaStream.getVideoTracks()[0].enabled = false;
95 debug('<br> === all video tracks disabled ===');
96
97 // The video is not guaranteed to render black frames before the canvas is drawn to and the pixels are checked.
98 // A timeout is used to ensure that the pixel check is done after the video renders black frames.
99 attempt(10, checkPixels, reenableTrack, 'pixel was black.');
100 }
101
102 function beginTestRound()
103 {
104 debug('<br> === beginning round of pixel tests ===');
105 attempt(1, checkPixels, disableAllTracks, 'pixel was white');
106 }
107
108 function canplay()
109 {
110 canvas = document.querySelector('canvas');
111 context = canvas.getContext('2d');
112
113 beginTestRound();
114 }
115
116 function start()
117 {
118 description("Tests that re-enabling a video MediaStreamTrack when all tracks were previously disabled causes captured media to display.");
119
120 video = document.querySelector('video');
121 video.addEventListener('canplay', canplay);
122
123 getUserMedia("allow", {video:true}, setupVideoElementWithStream);
124 }
125
126 window.jsTestIsAsync = true;
127</script>
128<script src="../../resources/js-test-post.js"></script>
129</body>
130</html>