Web Inspector: tint all pixels drawn by shader program when hovering ShaderProgramTre...
[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.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
84     function validTest({name, highlighted, evaluateString}) {
85         suite.addTestCase({
86             name,
87             test(resolve, reject) {
88                 CanvasAgent.setShaderProgramHighlighted(shaderProgram.identifier, highlighted, (error) => {
89                     if (error) {
90                         reject(error);
91                         return;
92                     }
93
94                     InspectorTest.evaluateInPage(evaluateString, (error) => {
95                         if (error) {
96                             reject(error);
97                             return;
98                         }
99
100                         CanvasAgent.requestContent(shaderProgram.canvas.identifier)
101                         .then(({content}) => InspectorTest.log(content))
102                         .then(resolve, reject);
103                     });
104                 });
105             }
106         });
107     }
108
109     validTest({
110         name: "Canvas.setShaderProgramHighlighted.highlightedDrawArrays",
111         highlighted: true,
112         evaluateString: `drawArrays()`,
113     });
114
115     validTest({
116         name: "Canvas.setShaderProgramHighlighted.highlightedDrawElements",
117         highlighted: true,
118         evaluateString: `drawElements()`,
119     });
120
121     validTest({
122         name: "Canvas.setShaderProgramHighlighted.unhighlightedDrawArrays",
123         highlighted: false,
124         evaluateString: `drawArrays()`,
125     });
126
127     validTest({
128         name: "Canvas.setShaderProgramHighlighted.unhighlightedDrawElements",
129         highlighted: false,
130         evaluateString: `drawElements()`,
131     });
132
133     suite.addTestCase({
134         name: "Canvas.setShaderProgramHighlighted.invalidProgramId",
135         description: "Invalid program identifiers should cause an error.",
136         test(resolve, reject) {
137             const programId = "INVALID_PROGRAM_ID";
138             const highlighted = false;
139             CanvasAgent.setShaderProgramHighlighted(programId, highlighted, (error) => {
140                 InspectorTest.expectThat(error, "Should produce an error.");
141                 InspectorTest.log("Error: " + error);
142                 resolve();
143             });
144         }
145     });
146
147     CanvasAgent.requestContent(shaderProgram.canvas.identifier, (error, content) => {
148         originalContent = content;
149
150         suite.runTestCasesAndFinish();
151     });
152 }
153 </script>
154 </head>
155 <body onload="load()">
156     <p>Test highlighting of shader programs.</p>
157 </body>
158 </html>