<!--
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">
<title>WebGL gl calls Conformance Tests</title>
<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>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<canvas id="canvas" width="2" height="2"> </canvas>
<script>
"use strict";
description("This test ensures basic functionality of the underlying graphics library");

debug("");
debug("Canvas.getContext");

var minimumRequiredStencilMask = 0;
var wtu = WebGLTestUtils;
var context = wtu.create3DContext("canvas");
if (!context)
    testFailed("context does not exist");
else {
    testPassed("context exists");

    debug("");
    debug("Context contains getError");
    if ("getError" in context)
      testPassed("context contains getError");
    else
      testFailed("context does not contains getError");

    debug("");
    debug("Check default values");
    shouldBe('context.getParameter(context.ACTIVE_TEXTURE)', 'context.TEXTURE0');
    shouldBe('(context.getParameter(context.ALIASED_LINE_WIDTH_RANGE)[0] <= 1) && (context.getParameter(context.ALIASED_LINE_WIDTH_RANGE)[0] > 0) && (context.getParameter(context.ALIASED_LINE_WIDTH_RANGE)[1] >= 1)', 'true');
    shouldBe('(context.getParameter(context.ALIASED_POINT_SIZE_RANGE)[0] <= 1) && (context.getParameter(context.ALIASED_POINT_SIZE_RANGE)[0] > 0) && (context.getParameter(context.ALIASED_POINT_SIZE_RANGE)[1] >= 1)', 'true');
    shouldBeType('context.getParameter(context.ALIASED_LINE_WIDTH_RANGE)', 'Float32Array');
    shouldBeType('context.getParameter(context.ALIASED_POINT_SIZE_RANGE)', 'Float32Array');
    shouldBe('context.getParameter(context.ARRAY_BUFFER_BINDING)', 'null');
    shouldBe('context.getParameter(context.BLEND)', 'false');
    shouldBe('context.getParameter(context.BLEND_COLOR)', '[0, 0, 0, 0]');
    shouldBeType('context.getParameter(context.BLEND_COLOR)', 'Float32Array');
    shouldBe('context.getParameter(context.BLEND_DST_ALPHA)', '0');
    shouldBe('context.getParameter(context.BLEND_DST_RGB)', '0');
    shouldBe('context.getParameter(context.BLEND_EQUATION_ALPHA)', 'context.FUNC_ADD');
    shouldBe('context.getParameter(context.BLEND_EQUATION_RGB)', 'context.FUNC_ADD');
    shouldBe('context.getParameter(context.BLEND_SRC_ALPHA)', '1');
    shouldBe('context.getParameter(context.BLEND_SRC_RGB)', '1');
    shouldBe('context.getParameter(context.COLOR_CLEAR_VALUE)', '[0, 0, 0, 0]');
    shouldBeType('context.getParameter(context.COLOR_CLEAR_VALUE)', 'Float32Array');
    shouldBe('context.getParameter(context.COLOR_WRITEMASK)', '[true, true, true, true]');
    shouldBeType('context.getParameter(context.COMPRESSED_TEXTURE_FORMATS)', 'Uint32Array');
    shouldBe('context.getParameter(context.CULL_FACE)', 'false');
    shouldBe('context.getParameter(context.CULL_FACE_MODE)', 'context.BACK');
    shouldBe('context.getParameter(context.CURRENT_PROGRAM)', 'null');
    shouldBe('context.getParameter(context.DEPTH_CLEAR_VALUE)', '1');
    shouldBe('context.getParameter(context.DEPTH_FUNC)', 'context.LESS');
    shouldBe('context.getParameter(context.DEPTH_RANGE)', '[0, 1]');
    shouldBeType('context.getParameter(context.DEPTH_RANGE)', 'Float32Array');
    shouldBe('context.getParameter(context.DEPTH_TEST)', 'false');
    shouldBe('context.getParameter(context.DEPTH_WRITEMASK)', 'true');
    shouldBe('context.getParameter(context.DITHER)', 'true');
    shouldBe('context.getParameter(context.ELEMENT_ARRAY_BUFFER_BINDING)', 'null');
    shouldBe('context.getParameter(context.FRONT_FACE)', 'context.CCW');
    shouldBe('context.getParameter(context.GENERATE_MIPMAP_HINT)', 'context.DONT_CARE');
    shouldBe('context.getParameter(context.LINE_WIDTH)', '1');
    shouldBe('context.getParameter(context.PACK_ALIGNMENT)', '4');
    shouldBe('context.getParameter(context.POLYGON_OFFSET_FACTOR)', '0');
    shouldBe('context.getParameter(context.POLYGON_OFFSET_FILL)', 'false');
    shouldBe('context.getParameter(context.POLYGON_OFFSET_UNITS)', '0');
    shouldBe('context.getParameter(context.RENDERBUFFER_BINDING)', 'null');
    shouldBe('context.getParameter(context.SAMPLE_COVERAGE_INVERT)', 'false');
    shouldBe('context.getParameter(context.SAMPLE_COVERAGE_VALUE)', '1');
    shouldBe('context.getParameter(context.SCISSOR_BOX)[0]', '0');
    shouldBe('context.getParameter(context.SCISSOR_BOX)[1]', '0');
    shouldBe('context.getParameter(context.SCISSOR_BOX)[2]', 'context.getParameter(context.VIEWPORT)[2]');
    shouldBe('context.getParameter(context.SCISSOR_BOX)[3]', 'context.getParameter(context.VIEWPORT)[3]');
    shouldBeType('context.getParameter(context.SCISSOR_BOX)', Int32Array);
    shouldBe('context.getParameter(context.SCISSOR_TEST)', 'false');
    shouldBe('context.getParameter(context.STENCIL_BACK_FAIL)', 'context.KEEP');
    shouldBe('context.getParameter(context.STENCIL_BACK_FUNC)', 'context.ALWAYS');
    shouldBe('context.getParameter(context.STENCIL_BACK_PASS_DEPTH_FAIL)', 'context.KEEP');
    shouldBe('context.getParameter(context.STENCIL_BACK_PASS_DEPTH_PASS)', 'context.KEEP');
    shouldBe('context.getParameter(context.STENCIL_BACK_REF)', '0');

    // WebGL 1.0.2 - 5.14.3 types / ES 2.0.25 - 6.2 State tables - 6.18 page 152
    shouldBeType('context.getParameter(context.SUBPIXEL_BITS)', 'Number');
    shouldBeGreaterThanOrEqual('context.getParameter(context.SUBPIXEL_BITS)', '4');

    shouldBeType('context.getParameter(context.SAMPLE_BUFFERS)', 'Number');
    shouldBeGreaterThanOrEqual('context.getParameter(context.SAMPLE_BUFFERS)', '0');

    shouldBeType('context.getParameter(context.SAMPLES)', 'Number');
    shouldBeGreaterThanOrEqual('context.getParameter(context.SAMPLES)', '0');

    shouldBeType('context.getParameter(context.DEPTH_BITS)', 'Number');
    shouldBeGreaterThanOrEqual('context.getParameter(context.DEPTH_BITS)', '0');
    shouldBeType('context.getParameter(context.RED_BITS)', 'Number');
    shouldBeGreaterThanOrEqual('context.getParameter(context.RED_BITS)', '0');
    shouldBeType('context.getParameter(context.GREEN_BITS)', 'Number');
    shouldBeGreaterThanOrEqual('context.getParameter(context.GREEN_BITS)', '0');
    shouldBeType('context.getParameter(context.BLUE_BITS)', 'Number');
    shouldBeGreaterThanOrEqual('context.getParameter(context.BLUE_BITS)', '0');
    shouldBeType('context.getParameter(context.ALPHA_BITS)', 'Number');
    shouldBeGreaterThanOrEqual('context.getParameter(context.ALPHA_BITS)', '0');
    shouldBeType('context.getParameter(context.STENCIL_BITS)', 'Number');

    var stencilBits = context.getParameter(context.STENCIL_BITS);
    minimumRequiredStencilMask = (1 << stencilBits) - 1;

    shouldBe('context.getParameter(context.STENCIL_BACK_VALUE_MASK) & minimumRequiredStencilMask', 'minimumRequiredStencilMask');
    shouldBe('context.getParameter(context.STENCIL_BACK_WRITEMASK) & minimumRequiredStencilMask', 'minimumRequiredStencilMask');

    // If EXT_packed_depth_stencil is supported, STENCIL_BITS > 0; otherwise, STENCIL_BITS == 0.
    shouldBe('context.getParameter(context.STENCIL_BITS) >= 0', 'true');
    shouldBe('context.getParameter(context.STENCIL_CLEAR_VALUE)', '0');
    shouldBe('context.getParameter(context.STENCIL_FAIL)', 'context.KEEP');
    shouldBe('context.getParameter(context.STENCIL_FUNC)', 'context.ALWAYS');
    shouldBe('context.getParameter(context.STENCIL_PASS_DEPTH_FAIL)', 'context.KEEP');
    shouldBe('context.getParameter(context.STENCIL_PASS_DEPTH_PASS)', 'context.KEEP');
    shouldBe('context.getParameter(context.STENCIL_REF)', '0');
    shouldBe('context.getParameter(context.STENCIL_TEST)', 'false');

    shouldBe('context.getParameter(context.STENCIL_VALUE_MASK) & minimumRequiredStencilMask', 'minimumRequiredStencilMask');
    shouldBe('context.getParameter(context.STENCIL_WRITEMASK) & minimumRequiredStencilMask', 'minimumRequiredStencilMask');

    shouldBe('context.getParameter(context.TEXTURE_BINDING_2D)', 'null');
    shouldBe('context.getParameter(context.TEXTURE_BINDING_CUBE_MAP)', 'null');
    shouldBe('context.getParameter(context.UNPACK_ALIGNMENT)', '4');
    shouldBe('context.getParameter(context.UNPACK_FLIP_Y_WEBGL)', 'false');
    shouldBe('context.getParameter(context.UNPACK_PREMULTIPLY_ALPHA_WEBGL)', 'false');
    shouldBe('context.getParameter(context.VIEWPORT)', '[0, 0, 2, 2]');
    shouldBeType('context.getParameter(context.VIEWPORT)', 'Int32Array');

    shouldBeGreaterThanOrEqual('context.getParameter(context.MAX_COMBINED_TEXTURE_IMAGE_UNITS)', '8');
    shouldBeGreaterThanOrEqual('context.getParameter(context.MAX_CUBE_MAP_TEXTURE_SIZE)', '16');
    shouldBeGreaterThanOrEqual('context.getParameter(context.MAX_FRAGMENT_UNIFORM_VECTORS)', '16');
    shouldBeGreaterThanOrEqual('context.getParameter(context.MAX_RENDERBUFFER_SIZE)', '1');
    shouldBeGreaterThanOrEqual('context.getParameter(context.MAX_TEXTURE_IMAGE_UNITS)', '8');
    shouldBeGreaterThanOrEqual('context.getParameter(context.MAX_TEXTURE_SIZE)', '64');
    shouldBeGreaterThanOrEqual('context.getParameter(context.MAX_VARYING_VECTORS)', '8');
    shouldBeGreaterThanOrEqual('context.getParameter(context.MAX_VERTEX_ATTRIBS)', '8');
    shouldBeGreaterThanOrEqual('context.getParameter(context.MAX_VERTEX_TEXTURE_IMAGE_UNITS)', '0');
    shouldBeGreaterThanOrEqual('context.getParameter(context.MAX_VERTEX_UNIFORM_VECTORS)', '128');
    // Note: This requirement should be removed from the spec IMO. Many impelementations
    // will be based on FBOs and FBOs might have a restriction smaller than the current screen size.
    // especially if there are multiple screens.
    shouldBeTrue('context.getParameter(context.MAX_VIEWPORT_DIMS)[0] >= window.screen.width');
    shouldBeTrue('context.getParameter(context.MAX_VIEWPORT_DIMS)[1] >= window.screen.height');
    shouldBeType('context.getParameter(context.MAX_VIEWPORT_DIMS)', 'Int32Array');

    debug("");
    debug("check texture values");
    var maxTextures = context.getParameter(context.MAX_TEXTURE_IMAGE_UNITS);
    for (var ii = 0; ii < maxTextures; ++ii) {
      context.activeTexture(context.TEXTURE0 + ii);
      debug("check texture unit: " + ii);
      shouldBe('context.getParameter(context.TEXTURE_BINDING_2D)', 'null');
      shouldBe('context.getParameter(context.TEXTURE_BINDING_CUBE_MAP)', 'null');
    }

    debug("");
    debug("check attrib values");
    var maxAttribs = context.getParameter(context.MAX_VERTEX_ATTRIBS);
    for (var ii = 0; ii < maxAttribs; ++ii) {
      debug("check attrib: " + ii);
      shouldBe('context.getVertexAttrib(ii, context.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING)', 'null');
      shouldBe('context.getVertexAttrib(ii, context.VERTEX_ATTRIB_ARRAY_ENABLED)', 'false');
      shouldBe('context.getVertexAttrib(ii, context.VERTEX_ATTRIB_ARRAY_SIZE)', '4');
      shouldBe('context.getVertexAttrib(ii, context.VERTEX_ATTRIB_ARRAY_STRIDE)', '0');
      shouldBe('context.getVertexAttrib(ii, context.VERTEX_ATTRIB_ARRAY_TYPE)', 'context.FLOAT');
      shouldBe('context.getVertexAttrib(ii, context.VERTEX_ATTRIB_ARRAY_NORMALIZED)', 'false');
      shouldBe('context.getVertexAttrib(ii, context.CURRENT_VERTEX_ATTRIB)', '[0, 0, 0, 1]');
      shouldBeType('context.getVertexAttrib(ii, context.CURRENT_VERTEX_ATTRIB)', 'Float32Array');
      shouldBe('context.getVertexAttribOffset(ii, context.VERTEX_ATTRIB_ARRAY_POINTER)', '0');
    }

    shouldBe('context.getError()', 'context.NO_ERROR');
}

debug("");
var successfullyParsed = true;

</script>
<script src="../../js/js-test-post.js"></script>

</body>
</html>
