summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/crossorigin/test_webgl_crossorigin_textures.html
blob: 545048a340d9c43c9d54bcbab745fcfdaeecd1f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<!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>