<!--
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>
</head>
<body>
<div id="description"></div>
<div id="console"></div>

<script>
"use strict";
description("Test of getActiveAttrib and getActiveUniform");

var wtu = WebGLTestUtils;
var context = wtu.create3DContext();
var context2 = wtu.create3DContext();
var program = wtu.loadStandardProgram(context);
var program2 = wtu.loadProgramFromFile(context2,
                           "../../resources/intArrayUniformShader.vert",
                           "../../resources/noopUniformShader.frag");

wtu.glErrorShouldBe(context, context.NO_ERROR);
shouldBe("context.getActiveUniform(program, 0).name", "'u_modelViewProjMatrix'");
shouldBe("context.getActiveUniform(program, 0).type", "context.FLOAT_MAT4");
shouldBe("context.getActiveUniform(program, 0).size", "1");
shouldBeNull("context.getActiveUniform(program, 1)");
wtu.glErrorShouldBe(context, context.INVALID_VALUE);
shouldBeNull("context.getActiveUniform(program, -1)");
wtu.glErrorShouldBe(context, context.INVALID_VALUE);
shouldThrow("context.getActiveUniform(null, 0)");
wtu.glErrorShouldBe(context, context.NO_ERROR);

// we don't know the order the attribs will appear.
var info = [
  context.getActiveAttrib(program, 0),
  context.getActiveAttrib(program, 1)
];
for (var ii = 0; ii < info.length; ++ii)
    shouldBeNonNull("info[ii]");

var expected = [
  { name: 'a_normal', type: context.FLOAT_VEC3, size: 1 },
  { name: 'a_vertex', type: context.FLOAT_VEC4, size: 1 }
];

if (info[0].name != expected[0].name) {
  var t = info[0];
  info[0] = info[1];
  info[1] = t;
}

for (var ii = 0; ii < info.length; ++ii) {
  shouldBe("info[ii].name", "expected[ii].name");
  shouldBe("info[ii].type", "expected[ii].type");
  shouldBe("info[ii].size", "expected[ii].size");
}

// we don't know the order the uniforms will appear.
var info2 = [
    context2.getActiveUniform(program2, 0),
    context2.getActiveUniform(program2, 1)
];
for (var ii = 0; ii < info2.length; ++ii)
    shouldBeNonNull("info2[ii]");

var expected2 = [
    { name: 'ival', type: context2.INT, size: 1 },
    { name: 'ival2[0]', type: context2.INT, size: 2 }
];

if (info2[0].name != expected2[0].name) {
    t = info2[0];
    info2[0] = info2[1];
    info2[1] = t;
}

for (var ii = 0; ii < info2.length; ++ii) {
    shouldBe("info2[ii].name", "expected2[ii].name");
    shouldBe("info2[ii].type", "expected2[ii].type");
    shouldBe("info2[ii].size", "expected2[ii].size");
}

shouldBeNull("context.getActiveAttrib(program, 2)");
wtu.glErrorShouldBe(context, context.INVALID_VALUE);
shouldBeNull("context.getActiveAttrib(program, -1)");
wtu.glErrorShouldBe(context, context.INVALID_VALUE);
shouldThrow("context.getActiveAttrib(null, 0)");
wtu.glErrorShouldBe(context, context.NO_ERROR);

wtu.glErrorShouldBe(context2, context.NO_ERROR);

debug("Check trying to get attribs from different context");
shouldBeNull("context2.getActiveAttrib(program, 0)");
wtu.glErrorShouldBe(context2, context2.INVALID_OPERATION);
shouldBeNull("context2.getActiveUniform(program, 0)");
wtu.glErrorShouldBe(context2, context2.INVALID_OPERATION);

debug("Check trying to get attribs from deleted program");
context.deleteProgram(program);
shouldBeNull("context.getActiveUniform(program, 0)");
wtu.glErrorShouldBe(context, context.INVALID_VALUE);
shouldBeNull("context.getActiveAttrib(program, 0)");
wtu.glErrorShouldBe(context, context.INVALID_VALUE);

var successfullyParsed = true;
</script>

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