<!DOCTYPE HTML>
<title>WebGL cross-origin textures test</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css">
<body>
<canvas id="canvas" style="border: none;" width="100" height="100">
  <p class="fallback"> FAIL (fallback content) </p>
</canvas>
<script>

  SimpleTest.waitForExplicitFinish();

  const OK = "";

  var gl;
  var number_of_tests_live = 0;
  var all_tests_started = false;

  function verifyError(actual_error, expected_error, message) {
    ok(actual_error == expected_error,
       message + ": expected " + expected_error + ", got " + actual_error);
  }

  function testTexture(url, crossOriginAttribute, expected_error) {
    number_of_tests_live++;
    var image = new Image();
    if (crossOriginAttribute == "just-crossOrigin-without-value") {
      var div = document.createElement('div');
      div.innerHTML="<img crossOrigin>";
      image = div.children[0];
    }
    else if (crossOriginAttribute != "missing-value-default")
      image.crossOrigin = crossOriginAttribute;
    

    function testDone() {
      number_of_tests_live--;
        
      if (number_of_tests_live == 0 && all_tests_started)
        SimpleTest.finish();
    }

    image.onload = function() {
      var tex = gl.createTexture();
      gl.bindTexture(gl.TEXTURE_2D, tex);
      var actual_error = OK;
      try {
        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
      } catch(e) {
        actual_error = e.name;
      }
      verifyError(actual_error, expected_error, "texImage2D on " + url + " with crossOrigin=" + image.crossOrigin);

      testDone();
    };

    image.onerror = function(event) {
      ok(expected_error != OK, "Got an error but expected OK!");

      testDone();
    }

    image.src = url;
  }

  addLoadEvent(function () {
    var canvas = document.getElementById("canvas");
    gl = canvas.getContext("experimental-webgl");
    if (!gl) {
      todo(false, "Canvas WebGL not supported");
      SimpleTest.finish();
      return;
    }


    testTexture("http://mochi.test:8888/tests/dom/canvas/test/crossorigin/image.png",
                "missing-value-default",
                OK);
    testTexture("http://mochi.test:8888/tests/dom/canvas/test/crossorigin/image.png",
                "",
                OK);
    testTexture("http://mochi.test:8888/tests/dom/canvas/test/crossorigin/image.png",
                "just-crossOrigin-without-value",
                OK);
    testTexture("http://example.com/tests/dom/canvas/test/crossorigin/image.png",
                "missing-value-default",
                "SecurityError");
    testTexture("http://example.com/tests/dom/canvas/test/crossorigin/image.png",
                "",
                "SecurityError");
    testTexture("http://example.com/tests/dom/canvas/test/crossorigin/image.png",
                "just-crossOrigin-without-value",
                "SecurityError");

    testTexture("http://example.com/tests/dom/canvas/test/crossorigin/image-allow-star.png",
                "missing-value-default",
                "SecurityError");
    testTexture("http://example.com/tests/dom/canvas/test/crossorigin/image-allow-star.png",
                "",
                OK);
    testTexture("http://example.com/tests/dom/canvas/test/crossorigin/image-allow-star.png",
                "just-crossOrigin-without-value",
                OK);
    testTexture("http://example.com/tests/dom/canvas/test/crossorigin/image-allow-star.png",
                "anonymous",
                OK);
    testTexture("http://example.com/tests/dom/canvas/test/crossorigin/image-allow-star.png",
                "use-credentials",
                "SecurityError");

    testTexture("http://example.com/tests/dom/canvas/test/crossorigin/image-allow-credentials.png",
                "missing-value-default",
                "SecurityError");
    testTexture("http://example.com/tests/dom/canvas/test/crossorigin/image-allow-credentials.png",
                "",
                OK);
    testTexture("http://example.com/tests/dom/canvas/test/crossorigin/image-allow-credentials.png",
                "just-crossOrigin-without-value",
                OK);
    testTexture("http://example.com/tests/dom/canvas/test/crossorigin/image-allow-credentials.png",
                "anonymous",
                OK);
    testTexture("http://example.com/tests/dom/canvas/test/crossorigin/image-allow-credentials.png",
                "use-credentials",
                OK);

    // Test that bad values for crossorigin="..." are interpreted as invalid-value-default which is "anonymous".
    testTexture("http://mochi.test:8888/tests/dom/canvas/test/crossorigin/image.png",
                "foobar",
                OK);
    testTexture("http://example.com/tests/dom/canvas/test/crossorigin/image.png",
                "foobar",
                "SecurityError");
    testTexture("http://example.com/tests/dom/canvas/test/crossorigin/image-allow-star.png",
                "foobar",
                OK);
    testTexture("http://example.com/tests/dom/canvas/test/crossorigin/image-allow-credentials.png",
                "foobar",
                OK);

    all_tests_started = true;
  });
</script>
