tizen beta release
[profile/ivi/webkit-efl.git] / LayoutTests / fast / canvas / webgl / canvas-test.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
2   "http://www.w3.org/TR/html4/loose.dtd">
3 <html>
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6 <title>WebGL Canvas Conformance Tests</title>
7 <script src="resources/desktop-gl-constants.js" type="text/javascript"></script>
8 <script src="../../js/resources/js-test-pre.js"></script>
9 <script src="resources/webgl-test.js"></script>
10 </head>
11 <body>
12 <div id="description"></div>
13 <div id="console"></div>
14 <canvas id="canvas" style="width: 50px; height: 50px;"> </canvas>
15 <canvas id="canvas2d" width="40" height="40"> </canvas>
16 <script>
17 if (window.initNonKhronosFramework) {
18   window.initNonKhronosFramework(true);
19 }
20
21 description("This test ensures WebGL implementations interact correctly with the canvas tag.");
22
23 debug("");
24 debug("Canvas.getContext");
25
26 var canvas = document.getElementById("canvas");
27 var canvas2d = document.getElementById("canvas2d");
28 var ctx2d = canvas2d.getContext("2d");
29 var gl = create3DContext(canvas);
30 if (!gl) {
31   testFailed("context does not exist");
32 } else {
33   testPassed("context exists");
34
35   debug("");
36   debug("Checking canvas and WebGL interaction");
37
38   // Check that a canvas with no width or height is 300x150 pixels
39   shouldBe('canvas.width', '300');
40   shouldBe('canvas.height', '150');
41
42   // Check get a 4 value gl parameter as a csv string.
43   function getValue4v(name) {
44     var v = gl.getParameter(name);
45     var result = '' +
46         v[0] + ',' +
47         v[1] + ',' +
48         v[2] + ',' +
49         v[3];
50     return result;
51   }
52
53   function getViewport() {
54     return getValue4v(gl.VIEWPORT);
55   }
56
57   function getClearColor() {
58     return getValue4v(gl.COLOR_CLEAR_VALUE);
59   }
60
61   function isAboutEqual(a, b) {
62     return Math.abs(a - b) < 0.01;
63   }
64
65   function isAboutEqualInt(a, b) {
66     return Math.abs(a - b) < 3;
67   }
68
69   function checkCanvasContentIs(r3d,g3d,b3d,a3d) {
70     var r2d;
71     var g2d;
72     var b2d;
73     var a2d;
74
75     function checkPixel(x, y, r3d,g3d,b3d,a3d) {
76       var offset = (y * 40 + x) * 4;
77       r2d = imgData.data[offset];
78       g2d = imgData.data[offset + 1];
79       b2d = imgData.data[offset + 2];
80       a2d = imgData.data[offset + 3];
81       //debug('' + x + ', ' + y + "(" + offset + ") = " + r2d + ", " + g2d + ", " + b2d + ", " + a2d);
82       return isAboutEqualInt(r2d, r3d) &&
83              isAboutEqualInt(g2d, g3d) &&
84              isAboutEqualInt(b2d, b3d) &&
85              isAboutEqualInt(a2d, a3d);
86     }
87
88     function checkPixels(r3d,g3d,b3d,a3d) {
89       return checkPixel(0, 0, r3d, g3d, b3d, a3d) &&
90              checkPixel(0, 39, r3d, g3d, b3d, a3d) &&
91              checkPixel(39, 0, r3d, g3d, b3d, a3d) &&
92              checkPixel(39, 39, r3d, g3d, b3d, a3d) &&
93              checkPixel(0, 0, r3d, g3d, b3d, a3d);
94     };
95
96     // Set to just take the color from the 3d canvas
97     ctx2d.globalCompositeOperation = 'copy';
98
99     // fill 2d canvas with orange
100     ctx2d.fillStyle = "rgb(255,192,128)";
101     ctx2d.fillRect (0, 0, 40, 40);
102
103     // get the image data
104     var imgData = ctx2d.getImageData(0, 0, 40, 40);
105
106     // check it got cleared.
107     if (!checkPixels(255, 192, 128, 255)) {
108       testFailed("unable to fill 2d context.");
109       return;
110     }
111
112     // draw 3d canvas on top.
113     ctx2d.drawImage(canvas, 0,0, 40, 40);
114
115     // get the image data
116     var imgData = ctx2d.getImageData(0, 0, 40, 40);
117
118     // Check it's the expected color.
119     if (!checkPixels(r3d, g3d, b3d, a3d)) {
120      testFailed("pixels are " + r2d + "," + g2d + "," + b2d + "," + a2d +
121                 " expected " + r3d + "," + g3d + "," + b3d + "," + a3d);
122     } else {
123       testPassed("pixels are " + r3d + "," + g3d + "," + b3d + "," + a3d);
124     }
125   }
126
127   checkCanvasContentIs(0, 0, 0, 0);
128   shouldBe('getViewport()', '"0,0,300,150"');
129
130   // Change the display size of the canvas and check
131   // the viewport size does not change.
132   debug("");
133   debug("change display size of canvas and see that viewport does not change");
134   canvas.style.width = "100px";
135   canvas.style.height = "25px";
136   var intervalId;
137   intervalId = window.setInterval(function() {
138     if (canvas.clientWidth == 100 &&
139         canvas.clientHeight == 25) {
140       window.clearInterval(intervalId);
141       shouldBe('getViewport()', '"0,0,300,150"');
142       shouldBe('canvas.width', '300');
143       shouldBe('canvas.height', '150');
144
145       // Change the actual size of the canvas
146       // Check that the viewport does not change.
147       // Check that the clear color does not change.
148       // Check that the color mask does not change.
149       debug("");
150       debug("change the actual size of the canvas and see that the viewport does not change");
151       gl.clearColor(0.25, 0.5, 0.75, 1);
152       gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
153       checkCanvasContentIs(64, 128, 192, 255);
154       gl.colorMask(0,0,0,0);
155       canvas.width = 400;
156       canvas.height = 10;
157
158       var v = gl.getParameter(gl.COLOR_CLEAR_VALUE);
159       assertMsg(isAboutEqual(v[0], 0.25) &&
160                 isAboutEqual(v[1], 0.5) &&
161                 isAboutEqual(v[2], 0.75) &&
162                 isAboutEqual(v[3], 1),
163                 "gl.clearColor should not change after canvas resize");
164       v = gl.getParameter(gl.COLOR_WRITEMASK);
165       assertMsg(isAboutEqual(v[0], 0) &&
166                 isAboutEqual(v[1], 0) &&
167                 isAboutEqual(v[2], 0) &&
168                 isAboutEqual(v[3], 0),
169                 "gl.colorMask should not change after canvas resize");
170       shouldBe('getViewport()', '"0,0,300,150"');
171       checkCanvasContentIs(0, 0, 0, 0);
172
173       debug("");
174       var epilogue = document.createElement("script");
175       epilogue.onload = finish;
176       epilogue.src = "../../js/resources/js-test-post.js";
177       document.body.appendChild(epilogue);
178     }
179    }, 1000/30);
180 }
181
182 function finish() {
183   if (window.nonKhronosFrameworkNotifyDone) {
184     window.nonKhronosFrameworkNotifyDone();
185   }
186 }
187
188 </script>
189 <script>
190 </script>
191
192 </body>
193 </html>