<!--
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>
"use strict";

// These declarations need to be global for "shouldBe" to see them
var gl;
var pixel = [0, 0, 0, 1];
var canvas;

function init()
{
    description('Verify that changing the size of an antialiased WebGL context does not cause it to stop working.');

    runTest();
}

function getWebGL(canvasWidth, canvasHeight, contextAttribs)
{
    canvas = document.createElement("canvas");
    if (!canvas)
        return null;
    canvas.width = canvasWidth;
    canvas.height = canvasHeight;

    gl = WebGLTestUtils.create3DContext(canvas, contextAttribs);
    if (!gl)
        return null;

    return gl;
}

function runTest()
{
    shouldBeNonNull("gl = getWebGL(1, 1, { alpha: false, antialias: true })");

    // Clear to black.
    gl.clearColor(0.0, 0.0, 0.0, 1.0);
    gl.clear(gl.COLOR_BUFFER_BIT);

    // Check that the pixel's R channel is 0.
    var buf = new Uint8Array(1 * 1 * 4);
    gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    pixel[0] = buf[0];
    shouldBeTrue("pixel[0] == 0");

    // Change the size of the canvas.
    canvas.width = 3;
    canvas.height = 3;

    // Clear to black.
    gl.clearColor(0.0, 0.0, 0.0, 1.0);
    gl.clear(gl.COLOR_BUFFER_BIT);

    // Clear the top-left pixel to white.
    gl.enable(gl.SCISSOR_TEST);
    gl.scissor(0, 0, 1, 1);
    gl.clearColor(1.0, 1.0, 1.0, 1.0);
    gl.clear(gl.COLOR_BUFFER_BIT);

    // Check that the top-left pixel has R channel 255.
    gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    pixel[0] = buf[0];
    shouldBeTrue("pixel[0] == 255");

    // Check that the bottom-right pixel has R channel 0.
    gl.readPixels(2, 2, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    pixel[0] = buf[0];
    shouldBeTrue("pixel[0] == 0");

    finishTest();
}

</script>
</head>
<body onload="init()">
<div id="description"></div>
<div id="console"></div>
</body>
</html>
