24c39419c2f0b226468043d881cfc236ed16bb84
[platform/framework/web/crosswalk.git] / src / third_party / webgl / src / sdk / tests / conformance / resources / iterable-test.js
1 /*
2 ** Copyright (c) 2013 The Khronos Group Inc.
3 **
4 ** Permission is hereby granted, free of charge, to any person obtaining a
5 ** copy of this software and/or associated documentation files (the
6 ** "Materials"), to deal in the Materials without restriction, including
7 ** without limitation the rights to use, copy, modify, merge, publish,
8 ** distribute, sublicense, and/or sell copies of the Materials, and to
9 ** permit persons to whom the Materials are furnished to do so, subject to
10 ** the following conditions:
11 **
12 ** The above copyright notice and this permission notice shall be included
13 ** in all copies or substantial portions of the Materials.
14 **
15 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
22 */
23 IterableTest = (function() {
24
25   var wtu = WebGLTestUtils;
26
27   function run(test, iterations) {
28     var target = iterations || 10;
29     var count = 0;
30
31     function doNextTest() {
32       ++count;
33       debug("Test " + count + " of " + target);
34       var success = test();
35       if (count < target && success !== false) {
36         wtu.waitForComposite(doNextTest);
37         //setTimeout(doNextTest, 100);
38       } else {
39         finishTest();
40       }
41     }
42
43     doNextTest();
44   }
45
46   // Creates a canvas and a texture then exits. There are
47   // no references to either so both should be garbage collected.
48   function createContextCreationAndDestructionTest() {
49     var textureSize = null;
50
51     return function() {
52       var canvas = document.createElement("canvas");
53       // This is safe for any device. See drawingBufferWidth in spec.
54       canvas.width = 2048;
55       canvas.height = 2048;
56       var gl = wtu.create3DContext(canvas);
57       if (textureSize === null) {
58         var maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
59         textureSize = Math.min(1024, maxTextureSize);
60       }
61       var tex = gl.createTexture();
62       gl.bindTexture(gl.TEXTURE_2D, tex);
63       gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, textureSize, textureSize, 0, gl.RGBA, gl.UNSIGNED_BYTE,
64                     null);
65       gl.clear(gl.COLOR_BUFFER_BIT);
66       wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors");
67
68       return true;
69     };
70   }
71
72   // Creates many small canvases and attaches them to the DOM.
73   // This tests an edge case discovered in Chrome where the creation of multiple
74   // WebGL contexts would eventually lead to context creation failure.
75   // (crbug.com/319265) The test does not require that old contexts remain
76   // valid, only that new ones can be created.
77   function createContextCreationTest() {
78     return function() {
79       var canvas = document.createElement("canvas");
80       canvas.width = 1;
81       canvas.height = 1;
82
83       document.body.appendChild(canvas);
84
85       var gl = wtu.create3DContext(canvas);
86       if (!gl) {
87         return false;
88       }
89
90       gl.clear(gl.COLOR_BUFFER_BIT);
91       wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors");
92
93       return true;
94     };
95   }
96
97   // Creates a canvas and a texture then exits. There are
98   // no references to either so both should be garbage collected.
99   function createMultisampleCorruptionTest(gl) {
100     var lastContext = null;
101
102     var program = wtu.loadStandardProgram(gl);
103     var uniforms = wtu.getUniformMap(gl, program);
104     gl.useProgram(program);
105
106     gl.clearColor(1.0, 0.0, 0.0, 1.0);
107     gl.clear(gl.COLOR_BUFFER_BIT);
108
109     var vertexObject = gl.createBuffer();
110     gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
111     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 0,2.5,0, 1.5,1.5,0, 2.5,1.5,0 ]), gl.STATIC_DRAW);
112     gl.enableVertexAttribArray(0);
113     gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
114     gl.vertexAttrib3f(1, 0.0, 0.0, 1.0);
115
116     var identityMat = new Float32Array([
117       1, 0, 0, 0,
118       0, 1, 0, 0,
119       0, 0, 1, 0,
120       0, 0, 0, 1
121     ]);
122
123     gl.uniformMatrix4fv(uniforms.u_modelViewProjMatrix.location, false, identityMat);
124
125     function test() {
126       var gl2 = wtu.create3DContext(null, {antialias: true});
127
128       gl2.canvas.width = gl2.canvas.height = 1024;
129       gl2.canvas.style.width = gl2.canvas.style.height = "1px";
130       document.body.appendChild(gl2.canvas);
131
132       gl2.clearColor(1.0, 0.0, 0.0, 1.0);
133       gl2.clear(gl2.COLOR_BUFFER_BIT);
134
135       if(lastContext) {
136           gl.drawArrays(gl.TRIANGLES, 0, 3);
137           var msg = "Canvas should be red";
138           wtu.checkCanvasRectColor(gl,
139               0, 0, gl.canvas.width, gl.canvas.height,
140               [255, 0, 0, 255], null,
141               function() {
142                   testPassed(msg);
143               },
144               function() {
145                   testFailed(msg);
146                   return false;
147               },
148           debug);
149           document.body.removeChild(lastContext.canvas);
150       }
151
152       lastContext = gl2;
153       return true;
154     };
155
156     // First pass does initialization
157     test();
158
159     return test;
160   }
161
162   return {
163     run: run,
164
165     createContextCreationAndDestructionTest: createContextCreationAndDestructionTest,
166     createContextCreationTest: createContextCreationTest,
167     createMultisampleCorruptionTest: createMultisampleCorruptionTest
168   };
169
170 })();