|
| 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 | +// @dart = 2.6 |
| 6 | +import 'dart:async'; |
| 7 | + |
| 8 | +import 'package:test/bootstrap/browser.dart'; |
| 9 | +import 'package:test/test.dart'; |
| 10 | +import 'package:ui/ui.dart' hide window; |
| 11 | +import 'package:ui/src/engine.dart'; |
| 12 | + |
| 13 | +import '../scuba.dart'; |
| 14 | +import 'helper.dart'; |
| 15 | + |
| 16 | +typedef CanvasTest = FutureOr<void> Function(EngineCanvas canvas); |
| 17 | + |
| 18 | +const Rect bounds = Rect.fromLTWH(0, 0, 800, 600); |
| 19 | + |
| 20 | +const Color white = Color(0xFFFFFFFF); |
| 21 | +const Color black = Color(0xFF000000); |
| 22 | +const Color red = Color(0xFFFF0000); |
| 23 | +const Color green = Color(0xFF00FF00); |
| 24 | +const Color blue = Color(0xFF0000FF); |
| 25 | + |
| 26 | +ParagraphConstraints constrain(double width) { |
| 27 | + return ParagraphConstraints(width: width); |
| 28 | +} |
| 29 | + |
| 30 | +CanvasParagraph rich( |
| 31 | + EngineParagraphStyle style, |
| 32 | + void Function(CanvasParagraphBuilder) callback, |
| 33 | +) { |
| 34 | + final CanvasParagraphBuilder builder = CanvasParagraphBuilder(style); |
| 35 | + callback(builder); |
| 36 | + return builder.build(); |
| 37 | +} |
| 38 | + |
| 39 | +void main() { |
| 40 | + internalBootstrapBrowserTest(() => testMain); |
| 41 | +} |
| 42 | + |
| 43 | +void testMain() async { |
| 44 | + setUpStableTestFonts(); |
| 45 | + |
| 46 | + test('paints spans and lines correctly', () { |
| 47 | + final canvas = BitmapCanvas(bounds, RenderStrategy()); |
| 48 | + |
| 49 | + Offset offset = Offset.zero; |
| 50 | + CanvasParagraph paragraph; |
| 51 | + |
| 52 | + // Single-line multi-span. |
| 53 | + paragraph = rich(ParagraphStyle(fontFamily: 'Roboto'), (builder) { |
| 54 | + builder.pushStyle(EngineTextStyle.only(color: blue)); |
| 55 | + builder.addText('Lorem '); |
| 56 | + builder.pushStyle(EngineTextStyle.only( |
| 57 | + color: green, |
| 58 | + background: Paint()..color = red, |
| 59 | + )); |
| 60 | + builder.addText('ipsum '); |
| 61 | + builder.pop(); |
| 62 | + builder.addText('.'); |
| 63 | + }) |
| 64 | + ..layout(constrain(double.infinity)); |
| 65 | + canvas.drawParagraph(paragraph, offset); |
| 66 | + offset = offset.translate(0, paragraph.height + 10); |
| 67 | + |
| 68 | + // Multi-line single-span. |
| 69 | + paragraph = rich(ParagraphStyle(fontFamily: 'Roboto'), (builder) { |
| 70 | + builder.addText('Lorem ipsum dolor sit'); |
| 71 | + }) |
| 72 | + ..layout(constrain(90.0)); |
| 73 | + canvas.drawParagraph(paragraph, offset); |
| 74 | + offset = offset.translate(0, paragraph.height + 10); |
| 75 | + |
| 76 | + // Multi-line multi-span. |
| 77 | + paragraph = rich(ParagraphStyle(fontFamily: 'Roboto'), (builder) { |
| 78 | + builder.pushStyle(EngineTextStyle.only(color: blue)); |
| 79 | + builder.addText('Lorem ipsum '); |
| 80 | + builder.pushStyle(EngineTextStyle.only(background: Paint()..color = red)); |
| 81 | + builder.pushStyle(EngineTextStyle.only(color: green)); |
| 82 | + builder.addText('dolor '); |
| 83 | + builder.pop(); |
| 84 | + builder.addText('sit'); |
| 85 | + }) |
| 86 | + ..layout(constrain(90.0)); |
| 87 | + canvas.drawParagraph(paragraph, offset); |
| 88 | + offset = offset.translate(0, paragraph.height + 10); |
| 89 | + |
| 90 | + return takeScreenshot(canvas, bounds, 'canvas_paragraph_general'); |
| 91 | + }); |
| 92 | + |
| 93 | + test('respects alignment', () { |
| 94 | + final canvas = BitmapCanvas(bounds, RenderStrategy()); |
| 95 | + |
| 96 | + Offset offset = Offset.zero; |
| 97 | + CanvasParagraph paragraph; |
| 98 | + |
| 99 | + void build(CanvasParagraphBuilder builder) { |
| 100 | + builder.pushStyle(EngineTextStyle.only(color: black)); |
| 101 | + builder.addText('Lorem '); |
| 102 | + builder.pushStyle(EngineTextStyle.only(color: blue)); |
| 103 | + builder.addText('ipsum '); |
| 104 | + builder.pushStyle(EngineTextStyle.only(color: green)); |
| 105 | + builder.addText('dolor '); |
| 106 | + builder.pushStyle(EngineTextStyle.only(color: red)); |
| 107 | + builder.addText('sit'); |
| 108 | + } |
| 109 | + |
| 110 | + paragraph = rich( |
| 111 | + ParagraphStyle(fontFamily: 'Roboto', textAlign: TextAlign.left), |
| 112 | + build, |
| 113 | + )..layout(constrain(100.0)); |
| 114 | + canvas.drawParagraph(paragraph, offset); |
| 115 | + offset = offset.translate(0, paragraph.height + 10); |
| 116 | + |
| 117 | + paragraph = rich( |
| 118 | + ParagraphStyle(fontFamily: 'Roboto', textAlign: TextAlign.center), |
| 119 | + build, |
| 120 | + )..layout(constrain(100.0)); |
| 121 | + canvas.drawParagraph(paragraph, offset); |
| 122 | + offset = offset.translate(0, paragraph.height + 10); |
| 123 | + |
| 124 | + paragraph = rich( |
| 125 | + ParagraphStyle(fontFamily: 'Roboto', textAlign: TextAlign.right), |
| 126 | + build, |
| 127 | + )..layout(constrain(100.0)); |
| 128 | + canvas.drawParagraph(paragraph, offset); |
| 129 | + offset = offset.translate(0, paragraph.height + 10); |
| 130 | + |
| 131 | + return takeScreenshot(canvas, bounds, 'canvas_paragraph_align'); |
| 132 | + }); |
| 133 | + |
| 134 | + test('paints spans with varying heights/baselines', () { |
| 135 | + final canvas = BitmapCanvas(bounds, RenderStrategy()); |
| 136 | + |
| 137 | + final CanvasParagraph paragraph = rich( |
| 138 | + ParagraphStyle(fontFamily: 'Roboto'), |
| 139 | + (builder) { |
| 140 | + builder.pushStyle(EngineTextStyle.only(fontSize: 20.0)); |
| 141 | + builder.addText('Lorem '); |
| 142 | + builder.pushStyle(EngineTextStyle.only( |
| 143 | + fontSize: 40.0, |
| 144 | + background: Paint()..color = green, |
| 145 | + )); |
| 146 | + builder.addText('ipsum '); |
| 147 | + builder.pushStyle(EngineTextStyle.only( |
| 148 | + fontSize: 10.0, |
| 149 | + color: white, |
| 150 | + background: Paint()..color = black, |
| 151 | + )); |
| 152 | + builder.addText('dolor '); |
| 153 | + builder.pushStyle(EngineTextStyle.only(fontSize: 30.0)); |
| 154 | + builder.addText('sit '); |
| 155 | + builder.pop(); |
| 156 | + builder.pop(); |
| 157 | + builder.pushStyle(EngineTextStyle.only( |
| 158 | + fontSize: 20.0, |
| 159 | + background: Paint()..color = blue, |
| 160 | + )); |
| 161 | + builder.addText('amet'); |
| 162 | + }, |
| 163 | + )..layout(constrain(220.0)); |
| 164 | + canvas.drawParagraph(paragraph, Offset.zero); |
| 165 | + |
| 166 | + return takeScreenshot(canvas, bounds, 'canvas_paragraph_varying_heights'); |
| 167 | + }); |
| 168 | +} |
0 commit comments