<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"></script>
<script id="vshader" type="x-shader/x-vertex">
attribute vec3 g_Position;

void main()
{
    gl_Position = vec4(g_Position.x, g_Position.y, g_Position.z, 1.0);
}
</script>

<script id="fshader" type="x-shader/x-fragment">
void main()
{
    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
</script>

</head>
<body>
<canvas id="example" width="4" height="4"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
description('Verifies that GL viewport does not change when canvas is resized');

var err;
var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("example");
var program = wtu.setupProgram(gl, ["vshader", "fshader"], ["g_Position"]);

var vertices = new Float32Array([
     1.0,  1.0, 0.0,
    -1.0,  1.0, 0.0,
    -1.0, -1.0, 0.0,
     1.0,  1.0, 0.0,
    -1.0, -1.0, 0.0,
     1.0, -1.0, 0.0]);
var vbo = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);

gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);

// Clear and set up
gl.clearColor(0, 0, 1, 1);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.useProgram(program);
// Draw the triangle pair to the frame buffer
gl.drawArrays(gl.TRIANGLES, 0, 6);

// Ensure that the frame buffer is red at the sampled pixel
wtu.checkCanvasRect(gl, 2, 2, 1, 1, [255, 0, 0, 255]);

// Now resize the canvas
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "No GL errors before resizing the canvas");
var canvas = gl.canvas;
canvas.width = 8;
canvas.height = 8;
err = gl.getError();
// Some implementations might lost the context when resizing
if (err == gl.CONTEXT_LOST_WEBGL) {
  testPassed("canvas lost context on resize");
} else {
  shouldBe("err", "gl.NO_ERROR");
  // Do another render
  gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  gl.drawArrays(gl.TRIANGLES, 0, 6);
  // This time, because we did not change the viewport, it should
  // still be (0, 0, 4, 4), so only the lower-left quadrant should
  // have been filled.
  wtu.checkCanvasRect(gl, 6, 6, 1, 1, [0, 0, 255, 255]);
}

var successfullyParsed = true;
</script>
<script src="../../js/js-test-post.js"></script>
</body>
</html>

