8a7943d55cd6c1cbd054bb08c1efbd62f69330c1
[WebKit-https.git] / LayoutTests / inspector / canvas / setShaderProgramHighlighted.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../../http/tests/inspector/resources/inspector-test.js"></script>
5 <script src="resources/shaderProgram-utilities-webgl.js"></script>
6 <script id="vertex-shader" type="x-shader/x-vertex">
7     attribute vec3 position;
8     void main(void) {
9         gl_Position = vec4(position, 1.0);
10     }
11 </script>
12 <script id="fragment-shader" type="x-shader/x-fragment">
13     precision mediump float;
14
15     void main(void) {
16         gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
17     }
18 </script>
19 <script>
20 function load() {
21     createProgram("webgl");
22     linkProgram("vertex-shader", "fragment-shader");
23     context.useProgram(program);
24
25     clearContext();
26
27     runTest();
28 }
29
30 function clearContext() {
31     context.clearColor(0.0, 0.0, 0.0, 1.0);
32     context.clear(context.COLOR_BUFFER_BIT);
33 }
34
35 function drawArrays() {
36     clearContext();
37
38     let vertexes = [
39         -0.5,  0.5,  0.0,
40         -0.5, -0.5,  0.0,
41          0.5, -0.5,  0.0,
42     ];
43     let vertexBuffer = context.createBuffer();
44     context.bindBuffer(context.ARRAY_BUFFER, vertexBuffer);
45     context.bufferData(context.ARRAY_BUFFER, new Float32Array(vertexes), context.STATIC_DRAW);
46
47     let position = context.getAttribLocation(program, "position");
48     context.vertexAttribPointer(position, 3, context.FLOAT, false, 0, 0);
49     context.enableVertexAttribArray(position);
50
51     context.drawArrays(context.TRIANGLES, 0, 3);
52 }
53
54 function drawElements() {
55     clearContext();
56
57     let vertexes = [
58          0.5,  0.5,  0.0,
59         -0.5, -0.5,  0.0,
60          0.5, -0.5,  0.0,
61     ];
62     let vertexBuffer = context.createBuffer();
63     context.bindBuffer(context.ARRAY_BUFFER, vertexBuffer);
64     context.bufferData(context.ARRAY_BUFFER, new Float32Array(vertexes), context.STATIC_DRAW);
65
66     let indexes = [0, 1, 2];
67     let indexBuffer = context.createBuffer();
68     context.bindBuffer(context.ELEMENT_ARRAY_BUFFER, indexBuffer);
69     context.bufferData(context.ELEMENT_ARRAY_BUFFER, new Uint16Array(indexes), context.STATIC_DRAW);
70
71     let position = context.getAttribLocation(program, "position");
72     context.vertexAttribPointer(position, 3, context.FLOAT, false, 0, 0);
73     context.enableVertexAttribArray(position);
74
75     context.drawElements(context.TRIANGLES, indexes.length, context.UNSIGNED_SHORT, 0);
76 }
77
78 function test() {
79     let suite = InspectorTest.createAsyncSuite("Canvas.setShaderProgramHighlighted");
80
81     let shaderProgram = WI.canvasManager.shaderPrograms[0];
82     let originalContent = null;
83     let contentForEval = {};
84
85     function validTest({name, highlighted, evaluateString}) {
86         suite.addTestCase({
87             name,
88             test(resolve, reject) {
89                 CanvasAgent.setShaderProgramHighlighted(shaderProgram.identifier, highlighted, (error) => {
90                     if (error) {
91                         reject(error);
92                         return;
93                     }
94
95                     InspectorTest.evaluateInPage(evaluateString, (error) => {
96                         if (error) {
97                             reject(error);
98                             return;
99                         }
100
101                         CanvasAgent.requestContent(shaderProgram.canvas.identifier)
102                         .then(({content}) => {
103                             if (highlighted) {
104                                 contentForEval[evaluateString] = content;
105                                 InspectorTest.expectNotEqual(content, originalContent, "Highlighting a shader program should change the content.");
106                             } else
107                                 InspectorTest.expectNotEqual(content, contentForEval[evaluateString], "Unhighlighting a shader program should change the content.");
108                         })
109                         .then(resolve, reject);
110                     });
111                 });
112             }
113         });
114     }
115
116     validTest({
117         name: "Canvas.setShaderProgramHighlighted.highlightedDrawArrays",
118         highlighted: true,
119         evaluateString: `drawArrays()`,
120     });
121
122     validTest({
123         name: "Canvas.setShaderProgramHighlighted.highlightedDrawElements",
124         highlighted: true,
125         evaluateString: `drawElements()`,
126     });
127
128     validTest({
129         name: "Canvas.setShaderProgramHighlighted.unhighlightedDrawArrays",
130         highlighted: false,
131         evaluateString: `drawArrays()`,
132     });
133
134     validTest({
135         name: "Canvas.setShaderProgramHighlighted.unhighlightedDrawElements",
136         highlighted: false,
137         evaluateString: `drawElements()`,
138     });
139
140     suite.addTestCase({
141         name: "Canvas.setShaderProgramHighlighted.invalidProgramId",
142         description: "Invalid program identifiers should cause an error.",
143         test(resolve, reject) {
144             const programId = "INVALID_PROGRAM_ID";
145             const highlighted = false;
146             CanvasAgent.setShaderProgramHighlighted(programId, highlighted, (error) => {
147                 InspectorTest.expectThat(error, "Should produce an error.");
148                 InspectorTest.log("Error: " + error);
149                 resolve();
150             });
151         }
152     });
153
154     CanvasAgent.requestContent(shaderProgram.canvas.identifier, (error, content) => {
155         originalContent = content;
156
157         suite.runTestCasesAndFinish();
158     });
159 }
160 </script>
161 </head>
162 <body onload="load()">
163     <p>Test highlighting of shader programs.</p>
164 </body>
165 </html>