|
| 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.12 |
| 6 | + |
| 7 | +import 'package:test/bootstrap/browser.dart'; |
| 8 | +import 'package:test/test.dart'; |
| 9 | +import 'package:ui/src/engine.dart'; |
| 10 | +import 'package:ui/ui.dart' as ui; |
| 11 | + |
| 12 | +import 'layout_service_helper.dart'; |
| 13 | + |
| 14 | +const ui.Color white = ui.Color(0xFFFFFFFF); |
| 15 | +const ui.Color black = ui.Color(0xFF000000); |
| 16 | +const ui.Color red = ui.Color(0xFFFF0000); |
| 17 | +const ui.Color green = ui.Color(0xFF00FF00); |
| 18 | +const ui.Color blue = ui.Color(0xFF0000FF); |
| 19 | + |
| 20 | +final EngineParagraphStyle ahemStyle = EngineParagraphStyle( |
| 21 | + fontFamily: 'ahem', |
| 22 | + fontSize: 10, |
| 23 | +); |
| 24 | + |
| 25 | +ui.ParagraphConstraints constrain(double width) { |
| 26 | + return ui.ParagraphConstraints(width: width); |
| 27 | +} |
| 28 | + |
| 29 | +CanvasParagraph rich( |
| 30 | + EngineParagraphStyle style, |
| 31 | + void Function(CanvasParagraphBuilder) callback, |
| 32 | +) { |
| 33 | + final CanvasParagraphBuilder builder = CanvasParagraphBuilder(style); |
| 34 | + callback(builder); |
| 35 | + return builder.build(); |
| 36 | +} |
| 37 | + |
| 38 | +void main() { |
| 39 | + internalBootstrapBrowserTest(() => testMain); |
| 40 | +} |
| 41 | + |
| 42 | +void testMain() async { |
| 43 | + await ui.webOnlyInitializeTestDomRenderer(); |
| 44 | + |
| 45 | + test('measures spans in the same line correctly', () { |
| 46 | + final CanvasParagraph paragraph = rich(ahemStyle, (builder) { |
| 47 | + builder.pushStyle(EngineTextStyle.only(fontSize: 12.0)); |
| 48 | + // 12.0 * 6 = 72.0 (with spaces) |
| 49 | + // 12.0 * 5 = 60.0 (without spaces) |
| 50 | + builder.addText('Lorem '); |
| 51 | + |
| 52 | + builder.pushStyle(EngineTextStyle.only(fontSize: 13.0)); |
| 53 | + // 13.0 * 6 = 78.0 (with spaces) |
| 54 | + // 13.0 * 5 = 65.0 (without spaces) |
| 55 | + builder.addText('ipsum '); |
| 56 | + |
| 57 | + builder.pushStyle(EngineTextStyle.only(fontSize: 11.0)); |
| 58 | + // 11.0 * 5 = 55.0 |
| 59 | + builder.addText('dolor'); |
| 60 | + })..layout(constrain(double.infinity)); |
| 61 | + |
| 62 | + expect(paragraph.maxIntrinsicWidth, 205.0); |
| 63 | + expect(paragraph.minIntrinsicWidth, 65.0); // "ipsum" |
| 64 | + expect(paragraph.width, double.infinity); |
| 65 | + expectLines(paragraph, [ |
| 66 | + l('Lorem ipsum dolor', 0, 17, hardBreak: true, width: 205.0, left: 0.0), |
| 67 | + ]); |
| 68 | + }); |
| 69 | + |
| 70 | + test('breaks lines correctly at the end of spans', () { |
| 71 | + final CanvasParagraph paragraph = rich(ahemStyle, (builder) { |
| 72 | + builder.addText('Lorem '); |
| 73 | + builder.pushStyle(EngineTextStyle.only(fontSize: 15.0)); |
| 74 | + builder.addText('sit '); |
| 75 | + builder.pop(); |
| 76 | + builder.addText('.'); |
| 77 | + })..layout(constrain(60.0)); |
| 78 | + |
| 79 | + expect(paragraph.maxIntrinsicWidth, 130.0); |
| 80 | + expect(paragraph.minIntrinsicWidth, 50.0); // "Lorem" |
| 81 | + expect(paragraph.width, 60.0); |
| 82 | + expectLines(paragraph, [ |
| 83 | + l('Lorem ', 0, 6, hardBreak: false, width: 50.0, left: 0.0), |
| 84 | + l('sit ', 6, 10, hardBreak: false, width: 45.0, left: 0.0), |
| 85 | + l('.', 10, 11, hardBreak: true, width: 10.0, left: 0.0), |
| 86 | + ]); |
| 87 | + }); |
| 88 | + |
| 89 | + test('breaks lines correctly in the middle of spans', () { |
| 90 | + final CanvasParagraph paragraph = rich(ahemStyle, (builder) { |
| 91 | + builder.addText('Lorem ipsum '); |
| 92 | + builder.pushStyle(EngineTextStyle.only(fontSize: 11.0)); |
| 93 | + builder.addText('sit dolor'); |
| 94 | + })..layout(constrain(100.0)); |
| 95 | + |
| 96 | + expect(paragraph.maxIntrinsicWidth, 219.0); |
| 97 | + expect(paragraph.minIntrinsicWidth, 55.0); // "dolor" |
| 98 | + expect(paragraph.width, 100.0); |
| 99 | + expectLines(paragraph, [ |
| 100 | + l('Lorem ', 0, 6, hardBreak: false, width: 50.0, left: 0.0), |
| 101 | + l('ipsum sit ', 6, 16, hardBreak: false, width: 93.0, left: 0.0), |
| 102 | + l('dolor', 16, 21, hardBreak: true, width: 55.0, left: 0.0), |
| 103 | + ]); |
| 104 | + }); |
| 105 | + |
| 106 | + test('handles space-only spans', () { |
| 107 | + final CanvasParagraph paragraph = rich(ahemStyle, (builder) { |
| 108 | + builder.pushStyle(EngineTextStyle.only(color: red)); |
| 109 | + builder.addText('Lorem '); |
| 110 | + builder.pop(); |
| 111 | + builder.pushStyle(EngineTextStyle.only(color: blue)); |
| 112 | + builder.addText(' '); |
| 113 | + builder.pushStyle(EngineTextStyle.only(color: green)); |
| 114 | + builder.addText(' '); |
| 115 | + builder.pushStyle(EngineTextStyle.only(color: black)); |
| 116 | + builder.addText('ipsum'); |
| 117 | + }); |
| 118 | + paragraph.layout(constrain(80.0)); |
| 119 | + |
| 120 | + expect(paragraph.maxIntrinsicWidth, 160.0); |
| 121 | + expect(paragraph.minIntrinsicWidth, 50.0); // "Lorem" or "ipsum" |
| 122 | + expect(paragraph.width, 80.0); |
| 123 | + expectLines(paragraph, [ |
| 124 | + l('Lorem ', 0, 11, hardBreak: false, width: 50.0, widthWithTrailingSpaces: 110.0, left: 0.0), |
| 125 | + l('ipsum', 11, 16, hardBreak: true, width: 50.0, left: 0.0), |
| 126 | + ]); |
| 127 | + }); |
| 128 | + |
| 129 | + test('should not break at span end if it is not a line break', () { |
| 130 | + final CanvasParagraph paragraph = rich(ahemStyle, (builder) { |
| 131 | + builder.pushStyle(EngineTextStyle.only(color: red)); |
| 132 | + builder.addText('Lorem'); |
| 133 | + builder.pop(); |
| 134 | + builder.pushStyle(EngineTextStyle.only(color: blue)); |
| 135 | + builder.addText(' '); |
| 136 | + builder.pushStyle(EngineTextStyle.only(color: black)); |
| 137 | + builder.addText('ip'); |
| 138 | + builder.pushStyle(EngineTextStyle.only(color: green)); |
| 139 | + builder.addText('su'); |
| 140 | + builder.pushStyle(EngineTextStyle.only(color: white)); |
| 141 | + builder.addText('m'); |
| 142 | + })..layout(constrain(50.0)); |
| 143 | + |
| 144 | + expect(paragraph.maxIntrinsicWidth, 110.0); |
| 145 | + expect(paragraph.minIntrinsicWidth, 50.0); // "Lorem" or "ipsum" |
| 146 | + expect(paragraph.width, 50.0); |
| 147 | + expectLines(paragraph, [ |
| 148 | + l('Lorem ', 0, 6, hardBreak: false, width: 50.0, left: 0.0), |
| 149 | + l('ipsum', 6, 11, hardBreak: true, width: 50.0, left: 0.0), |
| 150 | + ]); |
| 151 | + }); |
| 152 | +} |
0 commit comments