<!--

/*
** Copyright (c) 2012 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Materials.
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/

-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../resources/js-test-pre.js"></script>
<script src="../resources/webgl-test-utils.js"></script>
<script src="../../resources/test-eval.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>

<script>
"use strict";
var wtu = WebGLTestUtils;
description("Test that conversion of boolean arguments of WebGL functions follows EcmaScript 9.2. ToBoolean");
debug("");
debug("When an object is converted to a boolean, it should always evaluate as true. Any valueOf() method should not even get called. See Mozilla bug 727590 where Gecko incorrectly converted such an argument to a Number instead of a Boolean, giving the wrong behavior. See 9.2 and 9.3 in the EcmaScript specification.");
debug("");
var gl = wtu.create3DContext();
var program = wtu.loadStandardProgram(gl);
var shader = wtu.loadStandardVertexShader(gl);
var shouldGenerateGLError = wtu.shouldGenerateGLError;

assertMsg(program != null, "Program Compiled");
assertMsg(shader != null, "Shader Compiled");

var uloc = gl.getUniformLocation(program, "u_modelViewProjMatrix");
var aloc = gl.getAttribLocation(program, "a_vertex");

gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());

wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from initialization.");
assertMsg(uloc, "Uniform not found");
assertMsg(aloc >= 0, "Attribute not found");

var boolArg = { valueOf: function() { throw "Converting an Object to a Boolean should just give 'true' without further evaluation"; } }

function shouldNotThrowWithBoolArgs(code) {
  try {
    TestEval(code);
  } catch(e) {
    testFailed(code + " threw exception: " + e);
    return;
  }
  testPassed(code + " converted its boolean arguments correctly");
}

shouldNotThrowWithBoolArgs(
  "gl.colorMask(boolArg, boolArg, boolArg, boolArg)"
);

shouldNotThrowWithBoolArgs(
  "gl.depthMask(boolArg)"
);

shouldNotThrowWithBoolArgs(
  "gl.sampleCoverage(1, boolArg)"
);

function zeroArray(length) {
  var a = new Array(length);
  for (var i = 0; i < length; i++)
    a[i] = 0;
  return a;
}

function zeroFloat32Array(length) {
  var a = new Float32Array(length);
  for (var i = 0; i < length; i++)
    a[i] = 0;
  return a;
}

shouldNotThrowWithBoolArgs(
  "gl.uniformMatrix2fv(uloc, boolArg, zeroFloat32Array(4))"
);

shouldNotThrowWithBoolArgs(
  "gl.uniformMatrix2fv(uloc, boolArg, zeroArray(4))"
);

shouldNotThrowWithBoolArgs(
  "gl.uniformMatrix3fv(uloc, boolArg, zeroFloat32Array(9))"
);

shouldNotThrowWithBoolArgs(
  "gl.uniformMatrix3fv(uloc, boolArg, zeroArray(9))"
);

shouldNotThrowWithBoolArgs(
  "gl.uniformMatrix4fv(uloc, boolArg, zeroFloat32Array(16))"
);

shouldNotThrowWithBoolArgs(
  "gl.uniformMatrix4fv(uloc, boolArg, zeroArray(16))"
);

shouldNotThrowWithBoolArgs(
  "gl.vertexAttribPointer(aloc, 4, gl.FLOAT, boolArg, 4, 0)"
);

var successfullyParsed = true;
</script>

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