|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:html' as html; |
| 6 | +import 'dart:math' as math; |
| 7 | +import 'dart:js_util' as js_util; |
| 8 | + |
| 9 | +import 'package:ui/ui.dart' hide TextStyle; |
| 10 | +import 'package:ui/src/engine.dart'; |
| 11 | +import 'package:test/test.dart'; |
| 12 | + |
| 13 | +import 'package:web_engine_tester/golden_tester.dart'; |
| 14 | + |
| 15 | +void main() async { |
| 16 | + const double screenWidth = 600.0; |
| 17 | + const double screenHeight = 800.0; |
| 18 | + const Rect screenRect = Rect.fromLTWH(0, 0, screenWidth, screenHeight); |
| 19 | + final Paint testPaint = Paint()..color = const Color(0xFFFF0000); |
| 20 | + |
| 21 | + // Commit a recording canvas to a bitmap, and compare with the expected |
| 22 | + Future<void> _checkScreenshot(RecordingCanvas rc, String fileName, |
| 23 | + { Rect region = const Rect.fromLTWH(0, 0, 500, 500) }) async { |
| 24 | + |
| 25 | + final EngineCanvas engineCanvas = BitmapCanvas(screenRect); |
| 26 | + |
| 27 | + rc.apply(engineCanvas); |
| 28 | + |
| 29 | + // Wrap in <flt-scene> so that our CSS selectors kick in. |
| 30 | + final html.Element sceneElement = html.Element.tag('flt-scene'); |
| 31 | + try { |
| 32 | + sceneElement.append(engineCanvas.rootElement); |
| 33 | + html.document.body.append(sceneElement); |
| 34 | + await matchGoldenFile('$fileName.png', region: region, maxDiffRate: 0.02); |
| 35 | + } finally { |
| 36 | + // The page is reused across tests, so remove the element after taking the |
| 37 | + // Scuba screenshot. |
| 38 | + sceneElement.remove(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + setUp(() async { |
| 43 | + debugEmulateFlutterTesterEnvironment = true; |
| 44 | + await webOnlyInitializePlatform(); |
| 45 | + webOnlyFontCollection.debugRegisterTestFonts(); |
| 46 | + await webOnlyFontCollection.ensureFontsLoaded(); |
| 47 | + }); |
| 48 | + |
| 49 | + test('Paints image', () async { |
| 50 | + final RecordingCanvas rc = |
| 51 | + RecordingCanvas(const Rect.fromLTRB(0, 0, 400, 300)); |
| 52 | + rc.save(); |
| 53 | + rc.drawImage(createTestImage(), Offset(0, 0), new Paint()); |
| 54 | + await _checkScreenshot(rc, 'draw_image'); |
| 55 | + }); |
| 56 | + |
| 57 | + test('Paints image with transform', () async { |
| 58 | + final RecordingCanvas rc = |
| 59 | + RecordingCanvas(const Rect.fromLTRB(0, 0, 400, 300)); |
| 60 | + rc.save(); |
| 61 | + rc.translate(50.0, 100.0); |
| 62 | + rc.rotate(math.pi / 4.0); |
| 63 | + rc.drawImage(createTestImage(), Offset(0, 0), new Paint()); |
| 64 | + await _checkScreenshot(rc, 'draw_image_with_transform'); |
| 65 | + }); |
| 66 | + |
| 67 | + test('Paints image with transform and offset', () async { |
| 68 | + final RecordingCanvas rc = |
| 69 | + RecordingCanvas(const Rect.fromLTRB(0, 0, 400, 300)); |
| 70 | + rc.save(); |
| 71 | + rc.translate(50.0, 100.0); |
| 72 | + rc.rotate(math.pi / 4.0); |
| 73 | + rc.drawImage(createTestImage(), Offset(30, 20), new Paint()); |
| 74 | + await _checkScreenshot(rc, 'draw_image_with_transform_and_offset'); |
| 75 | + }); |
| 76 | + |
| 77 | + test('Paints image with transform using destination', () async { |
| 78 | + final RecordingCanvas rc = |
| 79 | + RecordingCanvas(const Rect.fromLTRB(0, 0, 400, 300)); |
| 80 | + rc.save(); |
| 81 | + rc.translate(50.0, 100.0); |
| 82 | + rc.rotate(math.pi / 4.0); |
| 83 | + Image testImage = createTestImage(); |
| 84 | + double testWidth = testImage.width.toDouble(); |
| 85 | + double testHeight = testImage.height.toDouble(); |
| 86 | + rc.drawImageRect(testImage, Rect.fromLTRB(0, 0, testWidth, testHeight), |
| 87 | + Rect.fromLTRB(100, 30, 2 * testWidth, 2 * testHeight), new Paint()); |
| 88 | + await _checkScreenshot(rc, 'draw_image_rect_with_transform'); |
| 89 | + }); |
| 90 | + |
| 91 | + test('Paints image with source and destination', () async { |
| 92 | + final RecordingCanvas rc = |
| 93 | + RecordingCanvas(const Rect.fromLTRB(0, 0, 400, 300)); |
| 94 | + rc.save(); |
| 95 | + Image testImage = createTestImage(); |
| 96 | + double testWidth = testImage.width.toDouble(); |
| 97 | + double testHeight = testImage.height.toDouble(); |
| 98 | + rc.drawImageRect(testImage, Rect.fromLTRB(testWidth / 2, 0, testWidth, testHeight), |
| 99 | + Rect.fromLTRB(100, 30, 2 * testWidth, 2 * testHeight), new Paint()); |
| 100 | + await _checkScreenshot(rc, 'draw_image_rect_with_source'); |
| 101 | + }); |
| 102 | + |
| 103 | + test('Paints image with transform using source and destination', () async { |
| 104 | + final RecordingCanvas rc = |
| 105 | + RecordingCanvas(const Rect.fromLTRB(0, 0, 400, 300)); |
| 106 | + rc.save(); |
| 107 | + rc.translate(50.0, 100.0); |
| 108 | + rc.rotate(math.pi / 6.0); |
| 109 | + Image testImage = createTestImage(); |
| 110 | + double testWidth = testImage.width.toDouble(); |
| 111 | + double testHeight = testImage.height.toDouble(); |
| 112 | + rc.drawImageRect(testImage, Rect.fromLTRB(testWidth / 2, 0, testWidth, testHeight), |
| 113 | + Rect.fromLTRB(100, 30, 2 * testWidth, 2 * testHeight), new Paint()); |
| 114 | + await _checkScreenshot(rc, 'draw_image_rect_with_transform_source'); |
| 115 | + }); |
| 116 | +} |
| 117 | + |
| 118 | +HtmlImage createTestImage() { |
| 119 | + const int width = 100; |
| 120 | + const int height = 50; |
| 121 | + html.CanvasElement canvas = new html.CanvasElement(width: width, height: height); |
| 122 | + html.CanvasRenderingContext2D ctx = canvas.context2D; |
| 123 | + ctx.fillStyle = '#E04040'; |
| 124 | + ctx.fillRect(0, 0, 33, 50); |
| 125 | + ctx.fill(); |
| 126 | + ctx.fillStyle = '#40E080'; |
| 127 | + ctx.fillRect(33, 0, 33, 50); |
| 128 | + ctx.fill(); |
| 129 | + ctx.fillStyle = '#2040E0'; |
| 130 | + ctx.fillRect(66, 0, 33, 50); |
| 131 | + ctx.fill(); |
| 132 | + html.ImageElement imageElement = html.ImageElement(); |
| 133 | + imageElement.src = js_util.callMethod(canvas, 'toDataURL', []); |
| 134 | + return HtmlImage(imageElement, width, height); |
| 135 | +} |
| 136 | + |
0 commit comments