summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/canvas/resources/canvas-tests.js
blob: c6e28732838119e71f920dc2eacf43af41dafb4a (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
function _valToString(val)
{
    if (val === undefined || val === null)
        return '[' + typeof(val) + ']';
    return val.toString() + '[' + typeof(val) + ']';
}

function _assert(cond, text)
{
    assert_true(!!cond, text);
}

function _assertSame(a, b, text_a, text_b)
{
    var msg = text_a + ' === ' + text_b + ' (got ' + _valToString(a) +
              ', expected ' + _valToString(b) + ')';
    assert_equals(a, b, msg);
}

function _assertDifferent(a, b, text_a, text_b)
{
    var msg = text_a + ' !== ' + text_b + ' (got ' + _valToString(a) +
              ', expected not ' + _valToString(b) + ')';
    assert_not_equals(a, b, msg);
}


function _getPixel(canvas, x,y)
{
    var ctx = canvas.getContext('2d');
    var imgdata = ctx.getImageData(x, y, 1, 1);
    return [ imgdata.data[0], imgdata.data[1], imgdata.data[2], imgdata.data[3] ];
}

function _assertPixel(canvas, x, y, r, g, b, a)
{
    var c = _getPixel(canvas, x,y);
    assert_equals(c[0], r, 'Red channel of the pixel at (' + x + ', ' + y + ')');
    assert_equals(c[1], g, 'Green channel of the pixel at (' + x + ', ' + y + ')');
    assert_equals(c[2], b, 'Blue channel of the pixel at (' + x + ', ' + y + ')');
    assert_equals(c[3], a, 'Alpha channel of the pixel at (' + x + ', ' + y + ')');
}

function _assertPixelApprox(canvas, x, y, r, g, b, a, tolerance)
{
    var c = _getPixel(canvas, x,y);
    assert_approx_equals(c[0], r, tolerance, 'Red channel of the pixel at (' + x + ', ' + y + ')');
    assert_approx_equals(c[1], g, tolerance, 'Green channel of the pixel at (' + x + ', ' + y + ')');
    assert_approx_equals(c[2], b, tolerance, 'Blue channel of the pixel at (' + x + ', ' + y + ')');
    assert_approx_equals(c[3], a, tolerance, 'Alpha channel of the pixel at (' + x + ', ' + y + ')');
}

function _assertMatricesApproxEqual(matA, matB)
{
  A = matA.toFloat32Array();
  B = matB.toFloat32Array();
  assert_equals(A.length, B.length);
  for (var i = 0; i < A.length; i++) {
    assert_approx_equals(A[i], B[i], 10e-6);
  }
}

function rad2deg(angle_in_radians) {
  return angle_in_radians / Math.PI * 180;
}

function deg2rad(angle_in_degrees) {
  return angle_in_degrees / 180 * Math.PI;
}

let _deferred = false;

function deferTest() {
  _deferred = true;
}

function _addTest(testFn, attributes={})
{
    on_event(window, "load", function()
    {
        t.step(function() {
            var canvas = document.getElementById('c');
            var ctx = canvas.getContext('2d', attributes);
            t.step(testFn, window, canvas, ctx);
        });

        if (!_deferred) {
            t.done();
        }
    });
}

function _assertGreen(ctx, canvasWidth, canvasHeight)
{
    var testColor = function(d, idx, expected) {
        assert_equals(d[idx], expected, "d[" + idx + "]", String(expected));
    };
    var imagedata = ctx.getImageData(0, 0, canvasWidth, canvasHeight);
    var w = imagedata.width, h = imagedata.height, d = imagedata.data;
    for (var i = 0; i < h; ++i) {
        for (var j = 0; j < w; ++j) {
            testColor(d, 4 * (w * i + j) + 0, 0);
            testColor(d, 4 * (w * i + j) + 1, 255);
            testColor(d, 4 * (w * i + j) + 2, 0);
            testColor(d, 4 * (w * i + j) + 3, 255);
        }
    }
}

function addCrossOriginYellowImage()
{
    var img = new Image();
    img.id = "yellow.png";
    img.className = "resource";
    img.src = get_host_info().HTTP_REMOTE_ORIGIN + "/images/yellow.png";
    document.body.appendChild(img);
}

function addCrossOriginRedirectYellowImage()
{
    var img = new Image();
    img.id = "yellow.png";
    img.className = "resource";
    img.src = get_host_info().HTTP_ORIGIN + "/common/redirect.py?location=" +
        get_host_info().HTTP_REMOTE_ORIGIN + "/images/yellow.png";
    document.body.appendChild(img);
}

function forEachCanvasSource(crossOriginUrl, sameOriginUrl, callback) {
  function makeImage() {
    return new Promise((resolve, reject) => {
      const image = new Image();
      image.onload = () => resolve(image);
      image.onerror = reject;
      image.src = crossOriginUrl + "/images/red.png";
    });
  }

  const arguments = [
    {
      name: "cross-origin HTMLImageElement",
      factory: makeImage,
    },

    {
      name: "cross-origin SVGImageElement",
      factory: () => {
        return new Promise((resolve, reject) => {
          const image = document.createElementNS("http://www.w3.org/2000/svg", "image");
          image.onload = () => resolve(image);
          image.onerror = reject;
          image.setAttribute("externalResourcesRequired", "true");
          image.setAttributeNS("http://www.w3.org/1999/xlink", 'xlink:href', crossOriginUrl + "/images/red.png");
          document.body.appendChild(image);
        });
      },
    },

    {
      name: "cross-origin HTMLVideoElement",
      factory: () => {
        return new Promise((resolve, reject) => {
          const video = document.createElement("video");
          video.oncanplaythrough = () => resolve(video);
          video.preload = "auto";
          video.onerror = reject;
          video.src = getVideoURI(crossOriginUrl + "/media/movie_300");
        });
      },
    },

    {
      name: "redirected to cross-origin HTMLVideoElement",
      factory: () => {
        return new Promise((resolve, reject) => {
          const video = document.createElement("video");
          video.oncanplaythrough = () => resolve(video);
          video.preload = "auto";
          video.onerror = reject;
          video.src = "/common/redirect.py?location=" + getVideoURI(crossOriginUrl + "/media/movie_300");
        });
      },
    },

    {
      name: "redirected to same-origin HTMLVideoElement",
      factory: () => {
        return new Promise((resolve, reject) => {
          const video = document.createElement("video");
          video.oncanplaythrough = () => resolve(video);
          video.preload = "auto";
          video.onerror = reject;
          video.src = crossOriginUrl + "/common/redirect.py?location=" + getVideoURI(sameOriginUrl + "/media/movie_300");
        });
      },
    },

    {
      name: "unclean HTMLCanvasElement",
      factory: () => {
        return makeImage().then(image => {
          const canvas = document.createElement("canvas");
          const context = canvas.getContext("2d");
          context.drawImage(image, 0, 0);
          return canvas;
        });
      },
    },

    {
      name: "unclean ImageBitmap",
      factory: () => {
        return makeImage().then(createImageBitmap);
      },
    },
  ];

  for (let { name, factory } of arguments) {
    callback(name, factory);
  }
}