<!DOCTYPE html>
<html>
<head>
<title>Texture upload with color profile</title>
<script src="../../../resources/js-test.js"></script>
<script src="resources/webgl-test.js"></script>
<script src="resources/webgl-test-utils.js"></script>
</head>
<body>
<canvas id="example" width="16" height="16" style="width: 16px; height: 16px;"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
description("Test that applying a color profile to a texture with separate alpha doesn't destroy the texture.");
debug("")
debug("This test isn't appropriate to include in the WebGL conformance suite, since the browser's behavior when applying a color profile can't be guaranteed.")
var wtu = WebGLTestUtils;
var canvas = document.getElementById("example");
var gl = wtu.create3DContext(canvas);
var program = wtu.setupTexturedQuad(gl);

glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");

var imgURLs = [
  'resources/tan-1x1-with-alpha.png'
];

wtu.loadImagesAsync(imgURLs, runTests);

function runTests(imgs) {
  // pixelStorei defaults are UNPACK_PREMULTIPLY_ALPHA_WEBGL=false, UNPACK_COLORSPACE_CONVERSION_WEBGL=BROWSER_DEFAULT_WEBGL.
  var loc = gl.getUniformLocation(program, "tex");
  gl.uniform1i(loc, 0);

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

  var width = canvas.width;
  var height = canvas.height;

  function checkPixelRange(buf, x, y, color, allowedRange) {
    var off = (y * width + x) * 4;
    var msg = "pixel " + x + ", " + y + " should be within " +
              allowedRange + " units of " +
              color[0] + ", " +
              color[1] + ", " +
              color[2] + ", " +
              color[3];
    var subMsg = " was " +
              buf[off + 0] + ", " +
              buf[off + 1] + ", " +
              buf[off + 2] + ", " +
              buf[off + 3];
    // When running in WebKit's test harness, we don't want to print the
    // pixel value when the test passes, because different machines might
    // have different results and we record the text output.
    var inDumpRenderTree = window.testRunner;
    for (var ii = 0; ii < 4; ++ii) {
      if (Math.abs(buf[off + ii] - color[ii]) > allowedRange) {
        testFailed(msg + subMsg);
        return;
      }
    }
    testPassed(msg + (inDumpRenderTree ? "" : subMsg));
  }
  
  var tex = gl.createTexture();
  gl.bindTexture(gl.TEXTURE_2D, tex);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);

  var buf = new Uint8Array(width * height * 4);

  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE,
                imgs['resources/tan-1x1-with-alpha.png']);
  glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup");
  wtu.drawQuad(gl);
  gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf);
  // This generous range is still enough to catch the regression.
  checkPixelRange(buf, 0, 0, [163, 126, 94, 129], 20);

  debug("");
}
</script>
</body>
</html>
