<!--
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 get error conformance test.</title>
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/desktop-gl-constants.js"></script>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"> </script>
</head>
<body>
<canvas id="example" width="1" height="1" style="width: 256px; height: 48px;"></canvas>
<div id="description"></div><div id="console"></div>
<script>
"use strict";
description("Test getError.");
var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("example");
var tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);

gl.enable(desktopGL.ALPHA_TEST);
wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "should generate INVALID_ENUM");
gl.viewport(-1, -1, -1, -1);
wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "should generate INVALID_VALUE");
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "should generate INVALID_OPERATION");

// Generate 2 errors of each type for 6 total possible errors.
// The OpenGL ES 2.0 spec section 2.5 says the implementation is allowed to
// either return the first error or many errors in an unspecied order.
gl.viewport(-1, -1, -1, -1);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.enable(desktopGL.ALPHA_TEST);
gl.viewport(-1, -1, -1, -1);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
// Note: This error is specifically last because we know it will be synthasized
// by WebGL at least when implemented on top of Desktop OpenGL
gl.enable(desktopGL.ALPHA_TEST);

var err1 = gl.getError();
var err2 = gl.getError();
var err3 = gl.getError();
var err4 = gl.getError();
var err5 = gl.getError();
var err6 = gl.getError();

debug("");
if (err2 == gl.NO_ERROR) {
  debug("This WebGL implementation looks like it uses the 'first error' method");
  debug("There should be 1 error, the first one generated");
  shouldBeTrue('err1 == gl.INVALID_VALUE && err2 == gl.NO_ERROR && err3 == gl.NO_ERROR');
} else {
  debug("This WebGL implementation looks like it uses the many error method");
  debug("Check is that at least one of the errors is the first error");
  shouldBeTrue('err1 == gl.INVALID_VALUE || ' +
               'err2 == gl.INVALID_VALUE || ' +
               'err3 == gl.INVALID_VALUE || ' +
               'err4 == gl.INVALID_VALUE || ' +
               'err5 == gl.INVALID_VALUE || ' +
               'err6 == gl.INVALID_VALUE');
  shouldBeTrue('gl.getError() == gl.NO_ERROR');
}

debug("");
var successfullyParsed = true;
</script>
<script src="../../js/js-test-post.js"></script>

</body>
</html>

