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

Commit cfbdd22

Browse files
committed
add leak test
1 parent 375b134 commit cfbdd22

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

lib/web_ui/test/canvaskit/canvas_golden_test.dart

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,11 +492,102 @@ void testMain() {
492492
fontStyle: ui.FontStyle.normal,
493493
);
494494
});
495+
496+
test('text style - foreground/background/color do not leak across paragraphs', () async {
497+
const double testWidth = 440;
498+
const double middle = testWidth / 2;
499+
CkParagraph createTestParagraph({
500+
ui.Color? color,
501+
CkPaint? foreground,
502+
CkPaint? background
503+
}) {
504+
final CkParagraphBuilder builder = CkParagraphBuilder(CkParagraphStyle());
505+
builder.pushStyle(CkTextStyle(
506+
fontSize: 16,
507+
color: color,
508+
foreground: foreground,
509+
background: background,
510+
));
511+
final StringBuffer text = StringBuffer();
512+
if (color == null && foreground == null && background == null) {
513+
text.write('Default');
514+
} else {
515+
if (color != null) {
516+
text.write('Color');
517+
}
518+
if (foreground != null) {
519+
if (text.isNotEmpty) {
520+
text.write('+');
521+
}
522+
text.write('Foreground');
523+
}
524+
if (background != null) {
525+
if (text.isNotEmpty) {
526+
text.write('+');
527+
}
528+
text.write('Background');
529+
}
530+
}
531+
builder.addText(text.toString());
532+
final CkParagraph paragraph = builder.build();
533+
paragraph.layout(ui.ParagraphConstraints(width: testWidth));
534+
return paragraph;
535+
}
536+
537+
final List<ParagraphFactory> variations = <ParagraphFactory>[
538+
() => createTestParagraph(),
539+
() => createTestParagraph(color: ui.Color(0xFF009900)),
540+
() => createTestParagraph(foreground: CkPaint()..color = ui.Color(0xFF990000)),
541+
() => createTestParagraph(background: CkPaint()..color = ui.Color(0xFF7777FF)),
542+
() => createTestParagraph(
543+
color: ui.Color(0xFFFF00FF),
544+
background: CkPaint()..color = ui.Color(0xFF0000FF),
545+
),
546+
() => createTestParagraph(
547+
foreground: CkPaint()..color = ui.Color(0xFF00FFFF),
548+
background: CkPaint()..color = ui.Color(0xFF0000FF),
549+
),
550+
];
551+
552+
final CkPictureRecorder recorder = CkPictureRecorder();
553+
final CkCanvas canvas = recorder.beginRecording(ui.Rect.largest);
554+
canvas.translate(10, 10);
555+
556+
for (ParagraphFactory from in variations) {
557+
for (ParagraphFactory to in variations) {
558+
canvas.save();
559+
final CkParagraph fromParagraph = from();
560+
canvas.drawParagraph(fromParagraph, ui.Offset.zero);
561+
562+
final ui.Offset leftEnd = ui.Offset(fromParagraph.maxIntrinsicWidth + 10, fromParagraph.height / 2);
563+
final ui.Offset rightEnd = ui.Offset(middle - 10, leftEnd.dy);
564+
final ui.Offset tipOffset = ui.Offset(-5, -5);
565+
canvas.drawLine(leftEnd, rightEnd, CkPaint());
566+
canvas.drawLine(rightEnd, rightEnd + tipOffset, CkPaint());
567+
canvas.drawLine(rightEnd, rightEnd + tipOffset.scale(1, -1), CkPaint());
568+
569+
canvas.translate(middle, 0);
570+
canvas.drawParagraph(to(), ui.Offset.zero);
571+
canvas.restore();
572+
canvas.translate(0, 22);
573+
}
574+
}
575+
576+
final CkPicture picture = recorder.endRecording();
577+
await matchPictureGolden(
578+
'canvaskit_text_styles_do_not_leak.png',
579+
picture,
580+
region: ui.Rect.fromLTRB(0, 0, testWidth, 850),
581+
write: true,
582+
);
583+
}, solo: true);
495584
// TODO: https://github.com/flutter/flutter/issues/60040
496585
// TODO: https://github.com/flutter/flutter/issues/71520
497586
}, skip: isIosSafari || isFirefox);
498587
}
499588

589+
typedef ParagraphFactory = CkParagraph Function();
590+
500591
void drawTestPicture(CkCanvas canvas) {
501592
canvas.clear(ui.Color(0xFFFFFFF));
502593

0 commit comments

Comments
 (0)