summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/canvas/element/manual/drawing-images-to-the-canvas/drawimage_crossorigin.sub.html
blob: 3d57d9f064bec7755a4f735e3fd12850109fcc15 (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
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
  function draw_and_read_image(img, should_throw) {
      let c = document.createElement('canvas');
      document.body.appendChild(c);
      let ctx = c.getContext('2d');
      ctx.drawImage(img, 0, 0);

      function get_image_data() {
          ctx.getImageData(0, 0, 4, 4);
      }

      if (should_throw) {
          assert_throws_dom('SecurityError', get_image_data);
      } else {
          get_image_data();
      }

      document.body.removeChild(c);
  }

  async_test(t => {
      let img = new Image();
      img.src = "/images/green.png";
      img.crossOrigin = "anonymous";
      img.onload = t.step_func_done(() => {
          draw_and_read_image(img, false);
      });
      img.onerror = t.unreached_func();
  }, "Can get pixels of canvas with same origin image drawn");

  async_test(t => {
      let img = new Image();
      img.src = "http://{{hosts[][www]}}:{{ports[http][0]}}/images/green.png?pipe=header(Access-Control-Allow-Origin,*)";
      img.crossOrigin = "anonymous";
      img.onload = t.step_func_done(() => {
          draw_and_read_image(img, false);
      });
      img.onerror = t.unreached_func();
  }, "Can get pixels of canvas with CORS enabled cross origin image drawn");

  async_test(t => {
      let img = new Image();
      img.src = "http://{{hosts[][www]}}:{{ports[http][0]}}/images/green.png?pipe=header(Access-Control-Allow-Origin,*)";
      img.onload = t.step_func_done(() => {
          draw_and_read_image(img, true);
      });
      img.onerror = t.unreached_func();
  }, "Can't get pixels of canvas with CORS enabled cross origin image drawn from non-CORS element");

  async_test(t => {
      let img = new Image();
      img.src = "http://{{hosts[][www]}}:{{ports[http][0]}}/images/green.png";

      img.onload = t.step_func_done(() => {
          draw_and_read_image(img, true);
      });
      img.onerror = t.unreached_func();
  }, "Can't get pixels of canvas with non-CORS enabled cross origin image drawn");
</script>