2 <meta http-equiv=
"content-type" content=
"text/html; charset=utf-8" />
4 <title>Canvas2D test: CaptureStream()
</title>
6 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
7 <script src=
"captureStream_common.js"></script>
8 <link rel=
"stylesheet" href=
"/tests/SimpleTest/test.css">
11 SimpleTest
.waitForExplicitFinish();
12 SimpleTest
.requestFlakyTimeout("Ensuring nothing happens until timing out with good margin");
14 // CaptureStreamTestHelper holding utility test functions.
15 const h
= new CaptureStreamTestHelper2D();
16 // Canvas element captured by streams.
17 const c
= h
.createAndAppendElement('canvas', 'c');
18 // Video element with captureStream stream in automatic mode.
19 const vauto
= h
.createAndAppendElement('video', 'vauto');
20 // Video element with captureStream stream in manual (fps 0) mode.
21 const vmanual
= h
.createAndAppendElement('video', 'vmanual');
22 // Video element with captureStream stream with fixed frame rate.
23 const vrate
= h
.createAndAppendElement('video', 'vrate');
25 async
function checkDrawColorInitialRed() {
26 info("Checking that all video elements become red when initiated just after the first drawColor(red).");
28 h
.drawColor(c
, h
.red
);
30 vauto
.srcObject
= c
.captureStream();
31 vmanual
.srcObject
= c
.captureStream(0);
32 vrate
.srcObject
= c
.captureStream(10);
34 ok(h
.isPixel(h
.getPixel(vauto
), h
.blackTransparent
, 0),
35 "vauto should not be drawn to before stable state");
36 ok(h
.isPixel(h
.getPixel(vrate
), h
.blackTransparent
, 0),
37 "vrate should not be drawn to before stable state");
38 ok(h
.isPixel(h
.getPixel(vmanual
), h
.blackTransparent
, 0),
39 "vmanual should not be drawn to before stable state");
41 await h
.pixelMustBecome(vauto
, h
.red
, {
42 infoString
: "should become red automatically",
44 await h
.pixelMustBecome(vrate
, h
.red
, {
45 infoString
: "should become red automatically",
47 await h
.pixelMustBecome(vmanual
, h
.red
, {
48 infoString
: "should become red when we get to stable state (first frame)",
52 async
function checkDrawColorGreen() {
53 info("Checking that drawing green propagates properly to video elements.");
55 const drawing
= h
.startDrawing(() => h
.drawColor(c
, h
.green
));
58 await h
.pixelMustBecome(vauto
, h
.green
, {
59 infoString
: "should become green automatically",
61 await h
.pixelMustBecome(vrate
, h
.green
, {
62 infoString
: "should become green automatically",
64 await h
.pixelMustBecome(vmanual
, h
.red
, {
65 infoString
: "should still be red",
67 h
.requestFrame(vmanual
);
68 await h
.pixelMustBecome(vmanual
, h
.green
, {
69 infoString
: "should become green after requstFrame()",
73 ok(false, "checkDrawColorGreen failed: ", err
);
78 async
function checkRequestFrameOrderGuarantee() {
79 info("Checking that requestFrame() immediately after a drawColor() " +
80 "call results in the expected frame seen in the stream.");
82 await h
.pixelMustBecome(vmanual
, h
.green
, {
83 infoString
: "should still be green",
85 h
.drawColor(c
, h
.red
); // 1. Draw canvas red
86 h
.requestFrame(vmanual
); // 2. Immediately request a frame
87 await h
.pixelMustBecome(vmanual
, h
.red
, {
88 infoString
: "should become red after call order test",
92 async
function checkDrawImageNotCleanRed() {
93 info("Checking that drawImage with not origin-clean image renders streams useless.");
94 const ctx
= c
.getContext('2d');
95 const notCleanRed
= new Image();
97 await
new Promise((resolve
, reject
) => {
98 notCleanRed
.onload
= resolve
;
99 notCleanRed
.onerror
= () => reject(new Error("Failed to load tainted image."));
100 notCleanRed
.src
= "http://example.com/tests/dom/canvas/test/image_red_crossorigin_credentials.png";
101 document
.body
.appendChild(notCleanRed
);
103 const drawing
= h
.startDrawing(
104 () => ctx
.drawImage(notCleanRed
, 0, 0, c
.width
, c
.height
));
107 await h
.pixelMustNotBecome(vauto
, h
.red
, {
109 infoString
: "should not become red",
111 ok(h
.isPixelNot(h
.getPixel(vrate
), h
.red
, 250),
112 "should not have become red");
113 await h
.pixelMustBecome(vmanual
, h
.green
, {
114 infoString
: "should still be green",
116 h
.requestFrame(vmanual
);
117 await h
.pixelMustNotBecome(vmanual
, h
.red
, {
119 infoString
: "should not become red",
122 ok(false, "checkDrawImageNotCleanRed failed: ", err
);
127 async
function checkEndedOnStop() {
128 const promises
= [vauto
, vmanual
, vrate
].map(elem
=> {
129 elem
.srcObject
.getTracks()[0].stop();
130 return new Promise(resolve
=>
131 elem
.addEventListener("ended", function endedListener(event
) {
132 ok(true, "Element " + elem
.id
+ " ended.");
134 elem
.removeEventListener("ended", endedListener
);
137 await Promise
.all(promises
);
141 ok(true, 'Test complete.');
146 await
checkDrawColorInitialRed();
147 await
checkDrawColorGreen();
148 await
checkRequestFrameOrderGuarantee();
149 await
checkDrawColorGreen(); // Restore video elements to green.
150 await
checkDrawImageNotCleanRed();
151 await
checkEndedOnStop();