Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / printing / resources / webgl-repeated-printing-common.js
blob339256588e77d4fad61a750fdb843aa7f954cf21
1 var gl;
3 function main()
5 if (!window.testRunner) {
6 testFailed("Requires window.testRunner");
7 } else {
8 testRunner.waitUntilDone();
9 testRunner.setPrinting();
10 testRunner.dumpAsText();
11 window.requestAnimationFrame(initTest);
15 var testIndex = 0;
16 var testsAndExpectations = [
17 { 'description': 'green', 'clearColor': [0, 1, 0, 1], 'expected': [ 0, 255, 0] },
18 { 'description': 'red', 'clearColor': [1, 0, 0, 1], 'expected': [255, 0, 0] },
19 { 'description': 'blue', 'clearColor': [0, 0, 1, 1], 'expected': [ 0, 0, 255] },
21 var tolerance = 1;
23 function initTest() {
24 var canvas = document.getElementById("c");
25 gl = initGL(canvas);
26 if (!gl) {
27 testFailed("Test requires WebGL");
28 testRunner.notifyDone();
29 return;
32 window.requestAnimationFrame(nextTest);
35 function nextTest() {
36 if (testIndex >= testsAndExpectations.length) {
37 // Without clearing this bit, the output comes out as a render
38 // tree, which is difficult to read.
39 testRunner.clearPrinting();
40 testRunner.notifyDone();
41 return;
44 var test = testsAndExpectations[testIndex];
45 var color = test['clearColor'];
46 try {
47 draw(color[0], color[1], color[2], color[3]);
48 testRunner.capturePixelsAsyncThen(completionCallback);
49 } catch (e) {
50 debug('error in nextTest');
51 debug(e);
52 testRunner.notifyDone();
56 var pixel;
57 function fetchPixelAt(x, y, width, height, snapshot) {
58 var data = new Uint8Array(snapshot);
59 pixel = [
60 data[4 * (width * y + x) + 0],
61 data[4 * (width * y + x) + 1],
62 data[4 * (width * y + x) + 2],
63 data[4 * (width * y + x) + 3]
67 function completionCallback(width, height, snapshot) {
68 var test = testsAndExpectations[testIndex];
69 debug('Test ' + testIndex + ': canvas should be ' + test['description']);
70 try {
71 var expectation = test['expected'];
72 fetchPixelAt(50, 50, width, height, snapshot);
73 shouldBeCloseTo('pixel[0]', expectation[0], tolerance);
74 shouldBeCloseTo('pixel[1]', expectation[1], tolerance);
75 shouldBeCloseTo('pixel[2]', expectation[2], tolerance);
76 } catch (e) {
77 debug('error in completionCallback');
78 debug(e);
79 testRunner.notifyDone();
80 return;
83 ++testIndex;
84 window.requestAnimationFrame(nextTest);
87 function draw(r, g, b, a)
89 gl.clearColor(r, g, b, a);
90 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
93 main();