<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test-pre.js"></script>
<script src="resources/webgl-test.js"> </script>
</head>
<body>
<canvas id="example" width="40" height="40" style="width: 40px; height: 40px;"></canvas>
<div id="description"></div>
<div id="console"></div>
<script id="vshader" type="x-shader/x-vertex">
#ifdef GL_ES
precision highp float;
#endif
attribute vec4 vPosition;
void main()
{
    gl_Position = vPosition;
}
</script>

<script id="fshader" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif
void main()
{
    gl_FragColor = vec4(0, 1, 0, 1);
}
</script>

<script>
function init()
{
    if (window.initNonKhronosFramework)
        window.initNonKhronosFramework(false);

    debug("Checks the last two arguments to bufferData() are respected.");
    debug("");

    gl = initWebGL("example", "vshader", "fshader", [ "vPosition", "index" ],
                   [ 1, 1, 1, 1 ], 1, null, true);

    gl.disable(gl.DEPTH_TEST);
    gl.disable(gl.BLEND);

    var coordinates = new Float32Array([ -1,1,0, 0,1,0, -1,-1,0, -1,-1,0, 0,1,0, 0,-1,0,
                                         0,1,0, 1,1,0, 0,-1,0, 0,-1,0, 1,1,0, 1,-1,0 ]);

    var vertexObject = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
    gl.bufferData(gl.ARRAY_BUFFER, coordinates, gl.STATIC_DRAW, 0, 4 * 3 * 6);
    shouldBe("gl.getError()", "gl.NO_ERROR");
    gl.enableVertexAttribArray(0);
    gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
    shouldBe("gl.getError()", "gl.NO_ERROR");

    checkRedValue(10, 10, 0, "Left half of canvas should be filled");
    checkRedValue(30, 10, 255, "Right half of canvas should be empty");

    gl.bufferData(gl.ARRAY_BUFFER, coordinates, gl.STATIC_DRAW, 4 * 3 * 6, 4 * 3 * 6);
    shouldBe("gl.getError()", "gl.NO_ERROR");

    checkRedValue(10, 10, 255, "Left half of canvas should be empty");
    checkRedValue(30, 10, 0, "Right half of canvas should be filled");

    gl.bufferData(gl.ARRAY_BUFFER, coordinates, gl.STATIC_DRAW, 0, 2 * 4 * 3 * 6);
    gl.bufferSubData(gl.ARRAY_BUFFER, 0, coordinates, 4 * 3 * 6, 4 * 3 * 6);
    shouldBe("gl.getError()", "gl.NO_ERROR");

    checkRedValue(10, 10, 255, "Left half of canvas should be empty");
    checkRedValue(30, 10, 0, "Right half of canvas should be filled");

    gl.bufferData(gl.ARRAY_BUFFER, coordinates, gl.STATIC_DRAW, 0, 2 * 4 * 3 * 6);
    gl.bufferSubData(gl.ARRAY_BUFFER, 4 * 3 * 6, coordinates, 0, 4 * 3 * 6);
    shouldBe("gl.getError()", "gl.NO_ERROR");

    checkRedValue(10, 10, 0, "Left half of canvas should be filled");
    checkRedValue(30, 10, 255, "Right half of canvas should be empty");

    gl.deleteBuffer(vertexObject);

    function checkRedValue(x, y, value, msg) {
        gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
        gl.drawArrays(gl.TRIANGLES, 0, 6);
        gl.flush();
        var buf = new Uint8Array(4);
        gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf);
        if (buf[0] != value || buf[3] != 255) {
            debug('expected: red channel should = ' + value + ' but was = ' + buf[0] + '.');
            testFailed(msg);
            return;
        }
        testPassed(msg);
    }
}

init();
</script>
<script src="../../../resources/js-test-post.js"></script>
</body>
</html>