<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL Context Release Test</title>
<script src="../../../resources/js-test.js"></script>
<script src="resources/webgl-test.js"></script>
</head>
<body>
<iframe id="host" style="width: 256px; height: 256px; border: 0;"></iframe>
<div id="description"></div>
<div id="console"></div>
<script>
description("This test ensures that WebGL contexts are released properly upon page reload");

window.jsTestIsAsync = true;

var host = document.getElementById("host");
var testIterations = 8;
var currentIteration = 0;

function refreshFrame() {
  if(currentIteration < testIterations) {
    currentIteration++;
    host.src = "resources/context-release-upon-reload-child.html";
  } else {
    testPassed("All drawing buffers were allocated at the correct size");
    finishTest();
  }
}

function testContext() {
  var bail = false;
  var gl = host.contentWindow.glContext;
  if (gl == null) {
    bail = true;
    testFailed("context was not created properly");
  }

  if (gl) {
    var err = gl.getError();
    if (err != gl.NO_ERROR) {
      bail = true;
      testFailed("Should be no GL error; got " + getGLErrorAsString(gl, err));
    }
  }

  if (bail) {
    finishTest();
    return;
  }

  if (gl.canvas.width != gl.drawingBufferWidth ||
      gl.canvas.height != gl.drawingBufferHeight) {
    testFailed("At iteration " + currentIteration + " of " + testIterations +
        ": Buffer was the wrong size: " +
        gl.drawingBufferWidth + "x" + gl.drawingBufferHeight);
    finishTest();
    return;
  } else {
    refreshFrame();
  }

  gl = null;
}

window.addEventListener("message", function(event) {
  if(event.data == "Ready") {
    testContext();
  }
});

refreshFrame();
</script>

</body>
</html>
