summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/canvas/offscreen/manual/the-offscreen-canvas/offscreencanvas.resize.html
blob: 3ff0eea153f6c3d9ed393fea24d0947b1f8fb53d (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<!DOCTYPE html>
<title>Test resizing an OffscreenCanvas with a 2d context</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/html/canvas/resources/canvas-tests.js"></script>
<body></body>
<script>
test(function() {
  var canvas = new OffscreenCanvas(10, 20);
  canvas.width = 30;
  canvas.height = 40;
  assert_equals(canvas.width, 30);
  assert_equals(canvas.height, 40);
}, "Verify that writing to the width and height attributes of an OffscreenCanvas works when there is no context attached.");

test(function() {
  var canvas = new OffscreenCanvas(10, 20);
  canvas.getContext('2d');
  canvas.width = 30;
  canvas.height = 40;
  assert_equals(canvas.width, 30);
  assert_equals(canvas.height, 40);
  var image = canvas.transferToImageBitmap();
  assert_equals(image.width, 30);
  assert_equals(image.height, 40);
}, "Verify that writing to the width and height attributes of an OffscreenCanvas works when there is a 2d context attached.");

test(function() {
  var canvas = new OffscreenCanvas(10, 20);
  canvas.getContext('webgl');
  canvas.width = 30;
  canvas.height = 40;
  assert_equals(canvas.width, 30);
  assert_equals(canvas.height, 40);
  var image = canvas.transferToImageBitmap();
  assert_equals(image.width, 30);
  assert_equals(image.height, 40);
}, "Verify that writing to the width and height attributes of an OffscreenCanvas works when there is a webgl context attached.");

test(function() {
  var placeholder = document.createElement('canvas');
  placeholder.width = 2;
  placeholder.height = 2;
  var offscreen = placeholder.transferControlToOffscreen();
  assert_throws_dom("InvalidStateError", () => { placeholder.width = 1; });
  assert_throws_dom("InvalidStateError", () => { placeholder.height = 1; });
}, "Verify that writing to the width or height attribute of a placeholder canvas throws an exception");

test(function() {
  var placeholder = document.createElement('canvas');
  placeholder.width = 1;
  placeholder.height = 1;
  var offscreen = placeholder.transferControlToOffscreen();
  assert_throws_dom("InvalidStateError", () => { placeholder.width = 1; });
  assert_throws_dom("InvalidStateError", () => { placeholder.height = 1; });
}, "Verify that writing to the width or height attribute of a placeholder canvas throws an exception even when not changing the value of the attribute.");

test(function() {
  var canvas = new OffscreenCanvas(10, 20);
  var ctx = canvas.getContext('2d');
  ctx.lineWidth = 5;
  canvas.width = 30;
  assert_equals(ctx.lineWidth, 1);
  ctx.lineWidth = 5;
  canvas.height = 40;
  assert_equals(ctx.lineWidth, 1);
}, "Verify that resizing a 2d context resets its state.");

test(function() {
  var canvas = new OffscreenCanvas(10, 20);
  var ctx = canvas.getContext('2d');
  ctx.lineWidth = 5;
  canvas.width = canvas.width;
  assert_equals(ctx.lineWidth, 1);
  ctx.lineWidth = 5;
  canvas.height = canvas.height;
  assert_equals(ctx.lineWidth, 1);
}, "Verify that setting the size of a 2d context to the same size it already had resets its state.");

async_test(function(t) {
  var placeholder = document.createElement('canvas');
  document.body.appendChild(placeholder); // So that we can check computed style/
  placeholder.width = 10;
  placeholder.height = 20;
  var offscreen = placeholder.transferControlToOffscreen();
  var ctx = offscreen.getContext('2d');
  t.step(function() {
    // Verify that setting the size of an OffscreenCanvas that has a placeholder works.
    offscreen.width = 30;
    offscreen.height = 40;
    assert_equals(offscreen.width, 30);
    assert_equals(offscreen.height, 40);
    var image = offscreen.transferToImageBitmap();
    assert_equals(image.width, 30);
    assert_equals(image.height, 40);
  });
  t.step(function() {
    // Verify that setting the size of an OffscreenCanvas does not directly update the size of its placeholder canvas.
    assert_equals(placeholder.width, 10);
    assert_equals(placeholder.height, 20);
  });
  var asyncStepsCompleted = 0;
  createImageBitmap(placeholder).then(image => {
    t.step(function() {
      // Verify that the placeholder was not updated synchronously.
      assert_equals(image.width, 10);
      assert_equals(image.height, 20);
    });
    asyncStepsCompleted = asyncStepsCompleted + 1;
    if (asyncStepsCompleted == 2) {
      t.done();
    }
  });

  function testImage() {
    if (placeholder.width != 30) {
      requestAnimationFrame(testImage);
    } else {
      t.step(function() {
        assert_equals(placeholder.width, 30);
        assert_equals(placeholder.height, 40);
        var computedStyle = window.getComputedStyle(placeholder);
        assert_equals(computedStyle.getPropertyValue('width'), "30px");
        assert_equals(computedStyle.getPropertyValue('height'), "40px");
      });
      createImageBitmap(placeholder).then(image => {
        t.step(function() {
          // Verify that an image grabbed from the placeholder has the correct dimensions
          assert_equals(image.width, 30);
          assert_equals(image.height, 40);
        });
        asyncStepsCompleted = asyncStepsCompleted + 1;
        if (asyncStepsCompleted == 2) {
          t.done();
        }
      });
    }
  }

  requestAnimationFrame(testImage);
}, "Verify that resizing an OffscreenCanvas with a 2d context propagates the new size to its placeholder canvas asynchronously.");

async_test(function(t) {
  var placeholder = document.createElement('canvas');
  document.body.appendChild(placeholder); // So that we can check computed style/
  placeholder.width = 10;
  placeholder.height = 20;
  var offscreen = placeholder.transferControlToOffscreen();
  var ctx = offscreen.getContext('webgl');
  t.step(function() {
    // Verify that setting the size of an OffscreenCanvas that has a placeholder works.
    offscreen.width = 30;
    offscreen.height = 40;
    assert_equals(offscreen.width, 30);
    assert_equals(offscreen.height, 40);
    var image = offscreen.transferToImageBitmap();
    assert_equals(image.width, 30);
    assert_equals(image.height, 40);
  });
  t.step(function() {
    // Verify that setting the size of an OffscreenCanvas does not directly update the size of its placeholder canvas.
    assert_equals(placeholder.width, 10);
    assert_equals(placeholder.height, 20);
  });
  var asyncStepsCompleted = 0;
  createImageBitmap(placeholder).then(image => {
    t.step(function() {
      // Verify that the placeholder was not updated synchronously.
      assert_equals(image.width, 10);
      assert_equals(image.height, 20);
    });
    asyncStepsCompleted = asyncStepsCompleted + 1;
    if (asyncStepsCompleted == 2) {
      t.done();
    }
  });

  function testImage() {
    if (placeholder.width != 30) {
      requestAnimationFrame(testImage);
    } else {
      t.step(function() {
        assert_equals(placeholder.width, 30);
        assert_equals(placeholder.height, 40);
        var computedStyle = window.getComputedStyle(placeholder);
        assert_equals(computedStyle.getPropertyValue('width'), "30px");
        assert_equals(computedStyle.getPropertyValue('height'), "40px");
      });
      createImageBitmap(placeholder).then(image => {
        t.step(function() {
          // Verify that an image grabbed from the placeholder has the correct dimensions
          assert_equals(image.width, 30);
          assert_equals(image.height, 40);
        });
        asyncStepsCompleted = asyncStepsCompleted + 1;
        if (asyncStepsCompleted == 2) {
          t.done();
        }
      });
    }
  }
  requestAnimationFrame(testImage);
}, "Verify that resizing an OffscreenCanvas with a webgl context propagates the new size to its placeholder canvas asynchronously.");

async_test(function(t){
  var placeholder = document.createElement('canvas');
  placeholder.width = 1;
  placeholder.height = 1;
  var offscreen = placeholder.transferControlToOffscreen();
  var ctx = offscreen.getContext('2d');
  offscreen.width = offscreen.height = 10;
  ctx.fillStyle = '#0f0';
  ctx.fillRect(0, 0, 10, 10);

  var testCanvas = document.createElement('canvas');
  testCanvas.width = testCanvas.height = 20;
  var testCtx = testCanvas.getContext('2d');

  function testImage() {
    if (placeholder.width != 10) {
      requestAnimationFrame(testImage);
    } else {
      testCtx.drawImage(placeholder, 0, 0);
      var pixel1 = testCtx.getImageData(9, 9, 1, 1).data;
      var pixel2 = testCtx.getImageData(9, 10, 1, 1).data;
      var pixel3 = testCtx.getImageData(10, 9, 1, 1).data;
      t.step(function() {
        assert_equals(placeholder.width, 10);
        assert_equals(placeholder.height, 10);
        assert_array_equals(pixel1, [0, 255, 0, 255]);
        assert_array_equals(pixel2, [0, 0, 0, 0]);
        assert_array_equals(pixel3, [0, 0, 0, 0]);
      });
      t.done();
    }
  }

  requestAnimationFrame(testImage);
}, "Verify that drawImage uses the size of the frame as the intinsic size of a placeholder canvas.");
</script>