Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / printing / webgl-repeated-printing.html
1 <!-- This is a regression test for crbug.com/351956, in which printing
2      of Google Maps was broken. In order to catch the regression it
3      must be run with the threaded compositor enabled. -->
4 <head>
5 <script src="../resources/js-test.js"></script>
6 <script>
7 var gl;
8
9 function main()
10 {
11   if (!window.testRunner) {
12     testFailed("Requires window.testRunner");
13   } else {
14     testRunner.waitUntilDone();
15     testRunner.setPrinting();
16     testRunner.dumpAsText();
17     window.requestAnimationFrame(initTest);
18   }
19 }
20
21 var testIndex = 0;
22 var testsAndExpectations = [
23   { 'description': 'green', 'clearColor': [0, 1, 0, 1], 'expected': [  0, 255,   0] },
24   { 'description': 'red',   'clearColor': [1, 0, 0, 1], 'expected': [255,   0,   0] },
25   { 'description': 'blue',  'clearColor': [0, 0, 1, 1], 'expected': [  0,   0, 255] },
26 ];
27 var tolerance = 1;
28
29 function initTest() {
30   var canvas = document.getElementById("c");
31   gl = initGL(canvas);
32   if (!gl) {
33     testFailed("Test requires WebGL");
34     testRunner.notifyDone();
35     return;
36   }
37
38    window.requestAnimationFrame(nextTest);
39 }
40
41 function nextTest() {
42   if (testIndex >= testsAndExpectations.length) {
43     // Without clearing this bit, the output comes out as a render
44     // tree, which is difficult to read.
45     testRunner.clearPrinting();
46     testRunner.notifyDone();
47     return;
48   }
49
50   var test = testsAndExpectations[testIndex];
51   var color = test['clearColor'];
52   try {
53     draw(color[0], color[1], color[2], color[3]);
54     testRunner.capturePixelsAsyncThen(completionCallback);
55   } catch (e) {
56     debug('error in nextTest');
57     debug(e);
58     testRunner.notifyDone();
59   }
60 }
61
62 var pixel;
63 function fetchPixelAt(x, y, width, height, snapshot) {
64   var data = new Uint8Array(snapshot);
65   pixel = [
66     data[4 * (width * y + x) + 0],
67     data[4 * (width * y + x) + 1],
68     data[4 * (width * y + x) + 2],
69     data[4 * (width * y + x) + 3]
70   ];
71 }
72
73 function completionCallback(width, height, snapshot) {
74   var test = testsAndExpectations[testIndex];
75   debug('Test ' + testIndex + ': canvas should be ' + test['description']);
76   try {
77     var expectation = test['expected'];
78     fetchPixelAt(50, 50, width, height, snapshot);
79     shouldBeCloseTo('pixel[0]', expectation[0], tolerance);
80     shouldBeCloseTo('pixel[1]', expectation[1], tolerance);
81     shouldBeCloseTo('pixel[2]', expectation[2], tolerance);
82   } catch (e) {
83     debug('error in completionCallback');
84     debug(e);
85     testRunner.notifyDone();
86     return;
87   }
88
89   ++testIndex;
90   window.requestAnimationFrame(nextTest);
91 }
92
93 function initGL(canvas)
94 {
95   try {
96     gl = canvas.getContext("webgl");
97   } catch (e) { }
98   return gl;
99 }
100
101 function draw(r, g, b, a)
102 {
103   gl.clearColor(r, g, b, a);
104   gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
105 }
106
107 main();
108 </script>
109 </head>
110 <body>
111 <canvas id="c" width="200" height="200" class="nomargin"></canvas>
112 <div id="console"></div>
113 </body>