Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit d2a0927

Browse files
committed
fix tests in other browsers
1 parent 10cb17c commit d2a0927

File tree

1 file changed

+52
-28
lines changed

1 file changed

+52
-28
lines changed

lib/web_ui/test/text/canvas_paragraph_builder_test.dart

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ bool get isIosSafari =>
1111
browserEngine == BrowserEngine.webkit &&
1212
operatingSystem == OperatingSystem.iOs;
1313

14+
/// Some text measurements are sensitive to browser implementations. Position
15+
/// info in the following tests only pass in Chrome, they are slightly different
16+
/// on each browser. So we need to ignore position info on non-Chrome browsers
17+
/// when comparing expectations with actual output.
18+
bool get isChrome => browserEngine == BrowserEngine.blink;
19+
1420
String fontFamilyToAttribute(String fontFamily) {
1521
fontFamily = canonicalizeFontFamily(fontFamily)!;
1622
if (browserEngine == BrowserEngine.firefox) {
@@ -42,19 +48,20 @@ Future<void> testMain() async {
4248
expect(paragraph.spans, hasLength(1));
4349

4450
paragraph.layout(const ParagraphConstraints(width: double.infinity));
45-
expect(
46-
paragraph.toDomElement().outerHtml,
51+
expectOuterHtml(
52+
paragraph,
4753
'<p style="${paragraphStyle(fontSize: 13)}">'
4854
'<span style="${spanStyle(top: 0, left: 0, fontSize: 13)}">'
4955
'Hello'
5056
'</span>'
5157
'</p>',
58+
ignorePositions: !isChrome,
5259
);
5360

5461
// Should break "Hello" into "Hel" and "lo".
5562
paragraph.layout(const ParagraphConstraints(width: 39.0));
56-
expect(
57-
paragraph.toDomElement().outerHtml,
63+
expectOuterHtml(
64+
paragraph,
5865
'<p style="${paragraphStyle(fontSize: 13)}">'
5966
'<span style="${spanStyle(top: 0, left: 0, fontSize: 13)}">'
6067
'Hel'
@@ -63,6 +70,7 @@ Future<void> testMain() async {
6370
'lo'
6471
'</span>'
6572
'</p>',
73+
ignorePositions: !isChrome,
6674
);
6775

6876
final ParagraphSpan span = paragraph.spans.single;
@@ -195,8 +203,8 @@ Future<void> testMain() async {
195203
expect(paragraph.spans, hasLength(2));
196204

197205
paragraph.layout(const ParagraphConstraints(width: double.infinity));
198-
expect(
199-
paragraph.toDomElement().outerHtml,
206+
expectOuterHtml(
207+
paragraph,
200208
'<p style="${paragraphStyle(fontSize: 13)}">'
201209
'<span style="${spanStyle(top: 0, left: 0, fontSize: 13, fontWeight: 'bold')}">'
202210
'Hello'
@@ -208,12 +216,13 @@ Future<void> testMain() async {
208216
'world'
209217
'</span>'
210218
'</p>',
219+
ignorePositions: !isChrome,
211220
);
212221

213222
// Should break "Hello world" into 2 lines: "Hello" and " world".
214223
paragraph.layout(const ParagraphConstraints(width: 75.0));
215-
expect(
216-
paragraph.toDomElement().outerHtml,
224+
expectOuterHtml(
225+
paragraph,
217226
'<p style="${paragraphStyle(fontSize: 13)}">'
218227
'<span style="${spanStyle(top: 0, left: 0, fontSize: 13, fontWeight: 'bold')}">'
219228
'Hello'
@@ -225,6 +234,7 @@ Future<void> testMain() async {
225234
'world'
226235
'</span>'
227236
'</p>',
237+
ignorePositions: !isChrome,
228238
);
229239

230240
final FlatTextSpan hello = paragraph.spans.first as FlatTextSpan;
@@ -266,8 +276,8 @@ Future<void> testMain() async {
266276
expect(paragraph.spans, hasLength(3));
267277

268278
paragraph.layout(const ParagraphConstraints(width: double.infinity));
269-
expect(
270-
paragraph.toDomElement().outerHtml,
279+
expectOuterHtml(
280+
paragraph,
271281
'<p style="${paragraphStyle(fontSize: 13)}">'
272282
'<span style="${spanStyle(top: 0, left: 0, lineHeight: 2, fontSize: 13, fontWeight: 'bold')}">'
273283
'Hello'
@@ -282,6 +292,7 @@ Future<void> testMain() async {
282292
'!'
283293
'</span>'
284294
'</p>',
295+
ignorePositions: !isChrome,
285296
);
286297

287298
final FlatTextSpan hello = paragraph.spans[0] as FlatTextSpan;
@@ -333,8 +344,8 @@ Future<void> testMain() async {
333344
// There's a new line between "First" and "Second", but "Second" and
334345
// "ThirdLongLine" remain together since constraints are infinite.
335346
paragraph.layout(const ParagraphConstraints(width: double.infinity));
336-
expect(
337-
paragraph.toDomElement().outerHtml,
347+
expectOuterHtml(
348+
paragraph,
338349
'<p style="${paragraphStyle(fontSize: 13)}">'
339350
'<span style="${spanStyle(top: 0, left: 0, fontSize: 13)}">'
340351
'First'
@@ -349,12 +360,13 @@ Future<void> testMain() async {
349360
'ThirdLongLine'
350361
'</span>'
351362
'</p>',
363+
ignorePositions: !isChrome,
352364
);
353365

354366
// Should break the paragraph into "First", "Second" and "ThirdLongLine".
355367
paragraph.layout(const ParagraphConstraints(width: 180.0));
356-
expect(
357-
paragraph.toDomElement().outerHtml,
368+
expectOuterHtml(
369+
paragraph,
358370
'<p style="${paragraphStyle(fontSize: 13)}">'
359371
'<span style="${spanStyle(top: 0, left: 0, fontSize: 13)}">'
360372
'First'
@@ -369,6 +381,7 @@ Future<void> testMain() async {
369381
'ThirdLongLine'
370382
'</span>'
371383
'</p>',
384+
ignorePositions: !isChrome,
372385
);
373386
});
374387

@@ -393,10 +406,8 @@ Future<void> testMain() async {
393406
// The paragraph should take the font size and family from the span with the
394407
// greatest font size.
395408
paragraph.layout(const ParagraphConstraints(width: double.infinity));
396-
expect(
397-
// Since we are using unknown font families, we can't predict the text
398-
// measurements.
399-
removePositionInfo(paragraph.toDomElement().outerHtml!),
409+
expectOuterHtml(
410+
paragraph,
400411
'<p style="${paragraphStyle(fontSize: 18, fontFamily: 'second')}">'
401412
'<span style="${spanStyle(top: null, left: null, fontSize: 12, fontFamily: 'first')}">'
402413
'First'
@@ -414,6 +425,9 @@ Future<void> testMain() async {
414425
'Third'
415426
'</span>'
416427
'</p>',
428+
// Since we are using unknown font families, we can't predict the text
429+
// measurements.
430+
ignorePositions: true,
417431
);
418432
debugEmulateFlutterTesterEnvironment = true;
419433
});
@@ -460,16 +474,6 @@ String spanStyle({
460474
].join(' ');
461475
}
462476

463-
/// Removes "top" and "left" CSS styles from the given html string.
464-
///
465-
/// This is needed when the positioning information in the html output is
466-
/// unknown and could be different depending on browser and environment.
467-
String removePositionInfo(String outerHtml) {
468-
return outerHtml
469-
.replaceAll(RegExp(r'\s*top:\s*[\d\.]+px\s*;\s*'), '')
470-
.replaceAll(RegExp(r'\s*left:\s*[\d\.]+px\s*;\s*'), '');
471-
}
472-
473477
TextStyle styleWithDefaults({
474478
Color color = const Color(0xFFFF0000),
475479
String fontFamily = FlutterViewEmbedder.defaultFontFamily,
@@ -489,3 +493,23 @@ TextStyle styleWithDefaults({
489493
letterSpacing: letterSpacing,
490494
);
491495
}
496+
497+
void expectOuterHtml(CanvasParagraph paragraph, String expected, {required bool ignorePositions}) {
498+
String outerHtml = paragraph.toDomElement().outerHtml!;
499+
if (ignorePositions) {
500+
outerHtml = removePositionInfo(outerHtml);
501+
expected = removePositionInfo(expected);
502+
}
503+
504+
expect(outerHtml, expected);
505+
}
506+
507+
/// Removes "top" and "left" CSS styles from the given html string.
508+
///
509+
/// This is needed when the positioning information in the html output is
510+
/// unknown and could be different depending on browser and environment.
511+
String removePositionInfo(String outerHtml) {
512+
return outerHtml
513+
.replaceAll(RegExp(r'\s*top:\s*[\d\.]+px\s*;\s*'), '')
514+
.replaceAll(RegExp(r'\s*left:\s*[\d\.]+px\s*;\s*'), '');
515+
}

0 commit comments

Comments
 (0)