Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: tests/html/canvasrenderingcontext2d_test.dart

Issue 12775010: Added the CanvasImageSource interface, which all types that a canvas can draw (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library url_test; 5 library canvas_rendering_context_2d_test;
6 import '../../pkg/unittest/lib/unittest.dart'; 6 import '../../pkg/unittest/lib/unittest.dart';
7 import '../../pkg/unittest/lib/html_config.dart'; 7 import '../../pkg/unittest/lib/html_config.dart';
8 import 'dart:html'; 8 import 'dart:html';
9 import 'dart:math'; 9 import 'dart:math';
10 10
11 // Some rounding errors in the browsers. 11 // Some rounding errors in the browsers.
12 checkPixel(List<int> pixel, List<int> expected) { 12 checkPixel(List<int> pixel, List<int> expected) {
13 expect(pixel[0], closeTo(expected[0], 2)); 13 expect(pixel[0], closeTo(expected[0], 2));
14 expect(pixel[1], closeTo(expected[1], 2)); 14 expect(pixel[1], closeTo(expected[1], 2));
15 expect(pixel[2], closeTo(expected[2], 2)); 15 expect(pixel[2], closeTo(expected[2], 2));
16 expect(pixel[3], closeTo(expected[3], 2)); 16 expect(pixel[3], closeTo(expected[3], 2));
17 } 17 }
18 18
19 main() { 19 main() {
20 useHtmlConfiguration(); 20 useHtmlConfiguration();
21 21
22 group('canvasRenderingContext2d', () { 22 group('canvasRenderingContext2d', () {
23 var canvas; 23 var canvas;
24 var context; 24 var context;
25 var otherCanvas;
26 var otherContext;
27
28 void createOtherCanvas() {
29 otherCanvas = new CanvasElement();
30 otherCanvas.width = 10;
31 otherCanvas.height = 10;
32 otherContext = otherCanvas.context2d;
33 otherContext.fillStyle = "red";
34 otherContext.fillRect(0, 0, otherCanvas.width, otherCanvas.height);
35 }
25 36
26 setUp(() { 37 setUp(() {
27 canvas = new CanvasElement(); 38 canvas = new CanvasElement();
28 canvas.width = 100; 39 canvas.width = 100;
29 canvas.height = 100; 40 canvas.height = 100;
30 41
31 context = canvas.context2d; 42 context = canvas.context2d;
43
44 createOtherCanvas();
32 }); 45 });
33 46
34 tearDown(() { 47 tearDown(() {
35 canvas = null; 48 canvas = null;
36 context = null; 49 context = null;
50 otherCanvas = null;
51 otherContext = null;
37 }); 52 });
38 53
39 List<int> readPixel(int x, int y) { 54 List<int> readPixel(int x, int y) {
40 var imageData = context.getImageData(x, y, 1, 1); 55 var imageData = context.getImageData(x, y, 1, 1);
41 return imageData.data; 56 return imageData.data;
42 } 57 }
43 58
44 /// Returns true if the pixel has some data in it, false otherwise. 59 /// Returns true if the pixel has some data in it, false otherwise.
45 bool isPixelFilled(int x, int y) => readPixel(x,y).any((p) => p != 0); 60 bool isPixelFilled(int x, int y) => readPixel(x,y).any((p) => p != 0);
46 61
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 245
231 // (cx - r/SQRT2, cy - r/SQRT2) should be filled. 246 // (cx - r/SQRT2, cy - r/SQRT2) should be filled.
232 expectPixelFilled((cx - r/SQRT2).toInt(), (cy + r/SQRT2).toInt(), true); 247 expectPixelFilled((cx - r/SQRT2).toInt(), (cy + r/SQRT2).toInt(), true);
233 248
234 // (cx + r/SQRT2, cy + r/SQRT2) should be filled. 249 // (cx + r/SQRT2, cy + r/SQRT2) should be filled.
235 expectPixelFilled((cx - r/SQRT2).toInt(), (cy - r/SQRT2).toInt(), true); 250 expectPixelFilled((cx - r/SQRT2).toInt(), (cy - r/SQRT2).toInt(), true);
236 251
237 // (cx - r/SQRT2, cy - r/SQRT2) should be filled. 252 // (cx - r/SQRT2, cy - r/SQRT2) should be filled.
238 expectPixelFilled((cx + r/SQRT2).toInt(), (cy - r/SQRT2).toInt(), true); 253 expectPixelFilled((cx + r/SQRT2).toInt(), (cy - r/SQRT2).toInt(), true);
239 }); 254 });
255
256 // Draw an image to the canvas from an image element.
257 test('drawImage from img element with 3 params', () {
258 var dataUrl = otherCanvas.toDataUrl('image/gif');
259 var img = new ImageElement();
260
261 img.onLoad.listen(expectAsync1((_) {
262 context.drawImage(img, 50, 50);
263
264 expectPixelFilled(50, 50);
265 expectPixelFilled(55, 55);
266 expectPixelFilled(59, 59);
267 expectPixelUnfilled(60, 60);
268 expectPixelUnfilled(0, 0);
269 expectPixelUnfilled(70, 70);
270 }));
271 img.onError.listen((_) {
272 guardAsync(() {
273 fail('URL failed to load.');
274 });
275 });
276 img.src = dataUrl;
277 });
278
279 // Draw an image to the canvas from an image element and scale it.
280 test('drawImage from img element with 5 params', () {
281 var dataUrl = otherCanvas.toDataUrl('image/gif');
282 var img = new ImageElement();
283
284 img.onLoad.listen(expectAsync1((_) {
285 context.drawImageAndScale(img, 50, 50, 20, 20);
286
287 expectPixelFilled(50, 50);
288 expectPixelFilled(55, 55);
289 expectPixelFilled(59, 59);
290 expectPixelFilled(60, 60);
291 expectPixelFilled(69, 69);
292 expectPixelUnfilled(70, 70);
293 expectPixelUnfilled(0, 0);
294 expectPixelUnfilled(80, 80);
295 }));
296 img.onError.listen((_) {
297 guardAsync(() {
298 fail('URL failed to load.');
299 });
300 });
301 img.src = dataUrl;
302 });
303
304 // Draw an image to the canvas from an image element and scale it.
305 test('drawImage from img element with 9 params', () {
306 otherContext.fillStyle = "blue";
307 otherContext.fillRect(5, 5, 5, 5);
308 var dataUrl = otherCanvas.toDataUrl('image/gif');
309 var img = new ImageElement();
310
311 img.onLoad.listen(expectAsync1((_) {
312 // This will take a 6x6 square from the first canvas from position 2,2
313 // and then scale it to a 20x20 square and place it to the second canvas
314 // at 50,50.
315 context.drawImageAndTransform(img, 2, 2, 6, 6, 50, 50, 20, 20);
316
317 checkPixel(readPixel(50, 50), [255, 0, 0, 255]);
318 checkPixel(readPixel(55, 55), [255, 0, 0, 255]);
319 checkPixel(readPixel(60, 50), [255, 0, 0, 255]);
320 checkPixel(readPixel(65, 65), [0, 0, 255, 255]);
321 checkPixel(readPixel(69, 69), [0, 0, 255, 255]);
322
323 expectPixelFilled(50, 50);
324 expectPixelFilled(55, 55);
325 expectPixelFilled(59, 59);
326 expectPixelFilled(60, 60);
327 expectPixelFilled(69, 69);
328 expectPixelUnfilled(70, 70);
329 expectPixelUnfilled(0, 0);
330 expectPixelUnfilled(80, 80);
331 }));
332 img.onError.listen((_) {
333 guardAsync(() {
334 fail('URL failed to load.');
335 });
336 });
337 img.src = dataUrl;
338 });
339
340 // Represents 2 frames of 8x8 red pixels.
341 // Created with:
342 // convert -size 8x8 xc:red blank1.jpg
343 // convert -size 8x8 xc:red blank2.jpg
344 // avconv -f image2 -i "blank%d.jpg" -c:v libx264 small.mp4
345 // python -m base64 -e small.mp4
346 // avconv -i small.mp4 small.webm
347 // python -m base64 -e small.webm
348 var mp4VideoUrl =
blois 2013/03/14 16:24:10 What is the video and where did it come from? (jus
Andrei Mouravski 2013/03/15 00:08:52 I have the instructions of how to make the video y
349 'data:video/mp4;base64,AAAAIGZ0eXBpc29tAAACAGlzb21pc28yYXZjMW1wNDEAAAAIZn'
350 'JlZQAAAsdtZGF0AAACmwYF//+X3EXpvebZSLeWLNgg2SPu73gyNjQgLSBjb3JlIDEyMCByMj'
351 'E1MSBhM2Y0NDA3IC0gSC4yNjQvTVBFRy00IEFWQyBjb2RlYyAtIENvcHlsZWZ0IDIwMDMtMj'
352 'AxMSAtIGh0dHA6Ly93d3cudmlkZW9sYW4ub3JnL3gyNjQuaHRtbCAtIG9wdGlvbnM6IGNhYm'
353 'FjPTEgcmVmPTMgZGVibG9jaz0xOjA6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AACkAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
383 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
384 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
385 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAVSalmAQAAAAAAAEEq17GDD0JATYCLTGF2ZjUzL'
386 'jIxLjFXQYtMYXZmNTMuMjEuMXOkkJatuHwTJ7cvFLSzBSmxbp5EiYhAVAAAAAAAABZUrmsBA'
387 'AAAAAAAR64BAAAAAAAAPteBAXPFgQGcgQAitZyDdW5khoVWX1ZQOIOBASPjg4QCYloA4AEAA'
388 'AAAAAASsIEIuoEIVLCBCFS6gQhUsoEDH0O2dQEAAAAAAABZ54EAo72BAACA8AIAnQEqCAAIA'
389 'ABHCIWFiIWEiAICAnWqA/gD+gINTRgA/v0hRf/kb+PnRv/I4//8WE8DijI//FRAo5WBACgAs'
390 'QEAARAQABgAGFgv9AAIAAAcU7trAQAAAAAAAA67jLOBALeH94EB8YIBfw==';
391
392 test('drawImage from video element with 3 params', () {
393 var video = new VideoElement();
394
395 video.onLoadedData.listen(expectAsync1((_) {
396 context.drawImage(video, 50, 50);
397
398 expectPixelFilled(50, 50);
399 expectPixelFilled(54, 54);
400 expectPixelFilled(57, 57);
401 expectPixelUnfilled(58, 58);
402 expectPixelUnfilled(0, 0);
403 expectPixelUnfilled(70, 70);
404 }));
405 video.onError.listen((_) {
406 guardAsync(() {
407 fail('URL failed to load.');
408 });
409 });
410
411 if(video.canPlayType('video/webm; codecs="vp8.0, vorbis"', '') != '') {
412 video.src = webmVideoUrl;
413 } else if(video.canPlayType('video/mp4; codecs="avc1.4D401E, mp4a.40.2"', '') != '') {
blois 2013/03/14 16:24:10 line length
Andrei Mouravski 2013/03/15 00:08:52 Done.
414 video.src = mp4VideoUrl;
415 } else {
416 // TODO(amouravski): Better fallback?
417 fail('Video is not supported on this system.');
blois 2013/03/14 16:24:10 Don't fail the test! Just skip it.
Andrei Mouravski 2013/03/15 00:08:52 Done.
418 }
419 });
420
421 test('drawImage from video element with 5 params', () {
422 var video = new VideoElement();
423
424 video.onLoadedData.listen(expectAsync1((_) {
425 context.drawImageAndScale(video, 50, 50, 20, 20);
426
427 expectPixelFilled(50, 50);
428 expectPixelFilled(55, 55);
429 expectPixelFilled(59, 59);
430 expectPixelFilled(60, 60);
431 expectPixelFilled(69, 69);
432 expectPixelUnfilled(70, 70);
433 expectPixelUnfilled(0, 0);
434 expectPixelUnfilled(80, 80);
435 }));
436 video.onError.listen((_) {
437 guardAsync(() {
438 fail('URL failed to load.');
439 });
440 });
441
442 if(video.canPlayType('video/webm; codecs="vp8.0, vorbis"', '') != '') {
443 video.src = webmVideoUrl;
444 } else if(video.canPlayType('video/mp4; codecs="avc1.4D401E, mp4a.40.2"', '') != '') {
blois 2013/03/14 16:24:10 length
Andrei Mouravski 2013/03/15 00:08:52 Done.
445 video.src = mp4VideoUrl;
446 } else {
447 // TODO(amouravski): Better fallback?
448 fail('Video is not supported on this system.');
449 }
450 });
451
452 test('drawImage from video element with 9 params', () {
453 var video = new VideoElement();
454
455 video.onLoadedData.listen(expectAsync1((_) {
456 context.drawImageAndTransform(video, 2, 2, 6, 6, 50, 50, 20, 20);
457
458 expectPixelFilled(50, 50);
459 expectPixelFilled(55, 55);
460 expectPixelFilled(59, 59);
461 expectPixelFilled(60, 60);
462 expectPixelFilled(69, 69);
463 expectPixelUnfilled(70, 70);
464 expectPixelUnfilled(0, 0);
465 expectPixelUnfilled(80, 80);
466 }));
467 video.onError.listen((_) {
468 guardAsync(() {
469 fail('URL failed to load.');
470 });
471 });
472
473 if(video.canPlayType('video/webm; codecs="vp8.0, vorbis"', '') != '') {
474 video.src = webmVideoUrl;
475 } else if(video.canPlayType('video/mp4; codecs="avc1.4D401E, mp4a.40.2"', '') != '') {
blois 2013/03/14 16:24:10 length
Andrei Mouravski 2013/03/15 00:08:52 Done.
476 video.src = mp4VideoUrl;
477 } else {
478 // TODO(amouravski): Better fallback?
479 fail('Video is not supported on this system.');
480 }
481 });
482
483 test('drawImage from canvas element with 3 params', () {
484 // Draw an image to the canvas from a canvas element.
485 context.drawImage(otherCanvas, 50, 50);
486
487 expectPixelFilled(50, 50);
488 expectPixelFilled(55, 55);
489 expectPixelFilled(59, 59);
490 expectPixelUnfilled(60, 60);
491 expectPixelUnfilled(0, 0);
492 expectPixelUnfilled(70, 70);
493 });
494 test('drawImage from canvas element with 5 params', () {
495 // Draw an image to the canvas from a canvas element.
496 context.drawImageAndScale(otherCanvas, 50, 50, 20, 20);
497
498 expectPixelFilled(50, 50);
499 expectPixelFilled(55, 55);
500 expectPixelFilled(59, 59);
501 expectPixelFilled(60, 60);
502 expectPixelFilled(69, 69);
503 expectPixelUnfilled(70, 70);
504 expectPixelUnfilled(0, 0);
505 expectPixelUnfilled(80, 80);
506 });
507 test('drawImage from canvas element with 9 params', () {
508 // Draw an image to the canvas from a canvas element.
509 otherContext.fillStyle = "blue";
510 otherContext.fillRect(5, 5, 5, 5);
511 context.drawImageAndTransform(otherCanvas, 2, 2, 6, 6, 50, 50, 20, 20);
512
513 checkPixel(readPixel(50, 50), [255, 0, 0, 255]);
514 checkPixel(readPixel(55, 55), [255, 0, 0, 255]);
515 checkPixel(readPixel(60, 50), [255, 0, 0, 255]);
516 checkPixel(readPixel(65, 65), [0, 0, 255, 255]);
517 checkPixel(readPixel(69, 69), [0, 0, 255, 255]);
518 expectPixelFilled(50, 50);
519 expectPixelFilled(55, 55);
520 expectPixelFilled(59, 59);
521 expectPixelFilled(60, 60);
522 expectPixelFilled(69, 69);
523 expectPixelUnfilled(70, 70);
524 expectPixelUnfilled(0, 0);
525 expectPixelUnfilled(80, 80);
526 });
240 }); 527 });
241 } 528 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698