<!--

/*
** 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">
<title>WebGL toDataURL test</title>
<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>
</head>
<body>
<canvas width="20" height="20" style="border: 1px solid black; width: 16px; height: 16px" id="c3d"></canvas>
<canvas width="20" height="20" style="border: 1px solid black; width: 16px; height: 16px" id="c2d"></canvas>
<div id="description"></div>
<div id="console"></div>
<script type="text/javascript">
var wtu = WebGLTestUtils;
var numTests = 10;
var gl;
var ctx;

var main = function() {
  description();
  ctx = document.getElementById("c2d").getContext("2d");
  gl = wtu.create3DContext("c3d");

  if (!gl) {
    testFailed("can't create 3d context");
    return;
  }

  var clearRect = function(gl, x, y, width, height, color) {
    gl.clearColor(color[0] / 255, color[1] / 255, color[2] / 255, color[3] / 255);
    gl.scissor(x, y, width, height);
    gl.clear(gl.COLOR_BUFFER_BIT);
  };

  var testSize = function(gl, width, height, callback) {
    debug("testing " + width + " by " + height);
    gl.canvas.width = width;
    gl.canvas.height = height;
    gl.viewport(0, 0, width, height);
    gl.enable(gl.SCISSOR_TEST);

    var bottomColor = [255, 0, 0, 255];
    var topColor = [0, 255, 0, 255];
    var rightColor = [0, 0, 255, 255];
    var halfHeight = Math.floor(height / 2);
    var topHeight = height - halfHeight;
    var canvasTopHeight = height - topHeight;
    clearRect(gl, 0, 0, width, halfHeight, bottomColor);
    clearRect(gl, 0, halfHeight, width, topHeight, topColor);
    clearRect(gl, width - 1, 0, 1, height, rightColor);

    // Performs gl.canvas.toDataURL() internally
    var img = wtu.makeImageFromCanvas(gl.canvas, function() {
      ctx.canvas.width = width;
      ctx.canvas.height = height;
      ctx.drawImage(img, 0, 0);
      wtu.checkCanvasRect(ctx, 0, 0, width - 1, topHeight, topColor);
      wtu.checkCanvasRect(ctx, 0, topHeight, width - 1, halfHeight, bottomColor);
      wtu.checkCanvasRect(ctx, width - 1, 0, 1, height, rightColor);
      debug("");
      callback();
    });
  };

  var tests = [
    { width:  16    , height:  16    , },
    { width:  16 - 1, height:  16    , },
    { width:  16 - 1, height:  16 - 1, },
    { width:  16 + 1, height:  16 - 1, },
    { width:  16 - 1, height:  16 + 1, },
    { width: 256    , height: 256    , },
    { width: 256 - 1, height: 256    , },
    { width: 256 - 1, height: 256 - 1, },
    { width: 256 + 1, height: 256 - 1, },
    { width: 256 - 1, height: 256 + 1, },
    { width: 512    , height: 512    , },
    { width: 512 - 1, height: 512    , },
    { width: 512 - 1, height: 512 - 1, },
    { width: 512 + 1, height: 512 - 1, },
    { width: 512 - 1, height: 512 + 1, },
  ];
  var testIndex = 0;
  var runNextTest = function() {
    if (testIndex == tests.length) {
      finishTest();
      return;
    }
    var test = tests[testIndex++];
    testSize(gl, test.width, test.height, function() {
      setTimeout(runNextTest, 0);
    })
  };
  runNextTest();
};
main();
var successfullyParsed = true;
</script>
</body>
</html>

