<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="resources/webgl-test.js"></script>
<script src="resources/webgl-test-utils.js"></script>
<script>

var evt;
var canvas;
var context;
var extension;

function createNewCanvas()
{
    canvas = document.createElement("canvas");
    canvas.width = 1;
    canvas.height = 1;

    context = WebGLTestUtils.create3DContext(canvas);
    extension = context.getExtension("WEBGL_lose_context");
    if (!extension) {
        debug("Could not find the WEBGL_lose_context extension.");
        return;
    }
}

function runTest1()
{
    if (window.initNonKhronosFramework)
        window.initNonKhronosFramework(true);
    createNewCanvas();
    canvas.addEventListener("webglcontextlost", function(e) {
        evt = e;
        debug("Test that the event passed to a listener of webglcontextlost is a WebGLContextEvent.")
        shouldBe("evt.toString()", "'[object WebGLContextEvent]'");
        shouldBe("evt.statusMessage", "''");
        // Start the next test when event dispatch has finished.
        setTimeout(function() {
          runTest2();
        }, 0);
    }, false);
    extension.loseContext();
}

function runTest2()
{
    createNewCanvas();
    canvas.addEventListener("webglcontextlost", function(e) {
        e.preventDefault();
        // Restore the context after event dispatch has finished.
        setTimeout(function() {
          // Because context restoration is specified as being asynchronous, we can not verify
          // that the GL error state is empty here.
          extension.restoreContext();
        }, 0);
    }, false);
    canvas.addEventListener("webglcontextrestored", function(e) {
        evt = e;
        shouldBe("context.getError()", "context.NO_ERROR");
        debug("Test that the event passed to a listener of webglcontextrestored is a WebGLContextEvent.")
        shouldBe("evt.toString()", "'[object WebGLContextEvent]'");
        shouldBe("evt.statusMessage", "''");

        setTimeout(finish, 0);
    }, false);
    extension.loseContext();
}

function finish() {
    finishTest();
}

</script>

</head>
<body onload="runTest1()">
<div id="description"></div>
<div id="console"></div>
<canvas id="canvas">
</body>
</html>
