|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | import {isBrowser, isMix, zoneSymbol} from '../../lib/common/utils'; |
10 | | -import {ifEnvSupports} from '../test-util'; |
| 10 | +import {ifEnvSupports, ifEnvSupportsWithDone} from '../test-util'; |
11 | 11 |
|
12 | 12 | import Spy = jasmine.Spy; |
13 | 13 | declare const global: any; |
@@ -54,6 +54,17 @@ function supportEventListenerOptions() { |
54 | 54 |
|
55 | 55 | (supportEventListenerOptions as any).message = 'supportsEventListenerOptions'; |
56 | 56 |
|
| 57 | +function supportCanvasTest() { |
| 58 | + const HTMLCanvasElement = (window as any)['HTMLCanvasElement']; |
| 59 | + const supportCanvas = typeof HTMLCanvasElement !== 'undefined' && HTMLCanvasElement.prototype && |
| 60 | + HTMLCanvasElement.prototype.toBlob; |
| 61 | + const FileReader = (window as any)['FileReader']; |
| 62 | + const supportFileReader = typeof FileReader !== 'undefined'; |
| 63 | + return supportCanvas && supportFileReader; |
| 64 | +} |
| 65 | + |
| 66 | +(supportCanvasTest as any).message = 'supportCanvasTest'; |
| 67 | + |
57 | 68 | describe('Zone', function() { |
58 | 69 | const rootZone = Zone.current; |
59 | 70 | (Zone as any)[zoneSymbol('ignoreConsoleErrorUncaughtError')] = true; |
@@ -666,5 +677,45 @@ describe('Zone', function() { |
666 | 677 | }); |
667 | 678 | })); |
668 | 679 | }); |
| 680 | + |
| 681 | + it('HTMLCanvasElement.toBlob should be a ZoneAware MacroTask', |
| 682 | + ifEnvSupportsWithDone(supportCanvasTest, (done: Function) => { |
| 683 | + const canvas = document.createElement('canvas'); |
| 684 | + const d = canvas.width; |
| 685 | + const ctx = canvas.getContext('2d'); |
| 686 | + ctx.beginPath(); |
| 687 | + ctx.moveTo(d / 2, 0); |
| 688 | + ctx.lineTo(d, d); |
| 689 | + ctx.lineTo(0, d); |
| 690 | + ctx.closePath(); |
| 691 | + ctx.fillStyle = 'yellow'; |
| 692 | + ctx.fill(); |
| 693 | + |
| 694 | + const scheduleSpy = jasmine.createSpy('scheduleSpy'); |
| 695 | + const zone: Zone = Zone.current.fork({ |
| 696 | + name: 'canvas', |
| 697 | + onScheduleTask: |
| 698 | + (delegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task) => { |
| 699 | + scheduleSpy(); |
| 700 | + return delegate.scheduleTask(targetZone, task); |
| 701 | + } |
| 702 | + }); |
| 703 | + |
| 704 | + zone.run(() => { |
| 705 | + const canvasData = canvas.toDataURL(); |
| 706 | + canvas.toBlob(function(blob) { |
| 707 | + expect(Zone.current.name).toEqual('canvas'); |
| 708 | + expect(scheduleSpy).toHaveBeenCalled(); |
| 709 | + |
| 710 | + const reader = new FileReader(); |
| 711 | + reader.readAsDataURL(blob); |
| 712 | + reader.onloadend = function() { |
| 713 | + const base64data = reader.result; |
| 714 | + expect(base64data).toEqual(canvasData); |
| 715 | + done(); |
| 716 | + }; |
| 717 | + }); |
| 718 | + }); |
| 719 | + })); |
669 | 720 | }); |
670 | 721 | }); |
0 commit comments