Skip to content

Commit d4202c7

Browse files
content [test]: Add golden tests for KaTeX content
1 parent 054d4fe commit d4202c7

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

test/widgets/content_test.dart

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import 'package:checks/checks.dart';
2+
import 'package:file/file.dart';
3+
import 'package:file/local.dart';
24
import 'package:flutter/cupertino.dart';
35
import 'package:flutter/foundation.dart';
46
import 'package:flutter/material.dart';
57
import 'package:flutter/rendering.dart';
8+
import 'package:flutter/services.dart';
69
import 'package:flutter_checks/flutter_checks.dart';
710
import 'package:flutter_test/flutter_test.dart';
11+
import 'package:path/path.dart' as p;
812
import 'package:url_launcher/url_launcher.dart';
913
import 'package:zulip/api/core.dart';
1014
import 'package:zulip/model/content.dart';
@@ -723,6 +727,43 @@ void main() {
723727
});
724728
}
725729
});
730+
731+
testWidgets('golden tests', (tester) async {
732+
await _loadKatexFonts();
733+
734+
addTearDown(testBinding.reset);
735+
final globalSettings = testBinding.globalStore.settings;
736+
await globalSettings.setBool(BoolGlobalSetting.renderKatex, true);
737+
check(globalSettings).getBool(BoolGlobalSetting.renderKatex).isTrue();
738+
739+
final contentHtml =
740+
ContentExample.mathBlockKatexSizing.html +
741+
ContentExample.mathBlockKatexNestedSizing.html +
742+
ContentExample.mathBlockKatexDelimSizing.html +
743+
ContentExample.mathBlockKatexVertical1.html +
744+
ContentExample.mathBlockKatexVertical2.html +
745+
ContentExample.mathBlockKatexVertical3.html +
746+
ContentExample.mathBlockKatexVertical4.html +
747+
ContentExample.mathBlockKatexVertical5.html;
748+
749+
await prepareContent(tester, Builder(builder: (context) =>
750+
DefaultTextStyle(
751+
style: ContentTheme.of(context).textStylePlainParagraph,
752+
child: BlockContentList(
753+
nodes: parseContent(contentHtml).nodes.map((node) {
754+
final mathBlockNode = node as MathBlockNode;
755+
return ParagraphNode(
756+
links: null,
757+
nodes: [
758+
MathInlineNode(
759+
texSource: mathBlockNode.texSource,
760+
nodes: mathBlockNode.nodes),
761+
]);
762+
}).toList(growable: false)))));
763+
764+
await check(find.byType(BlockContentList))
765+
.matchesGoldenFile(Uri.file('goldens/katex.png'));
766+
});
726767
});
727768

728769
/// Make a [TargetFontSizeFinder] to pass to [checkFontSizeRatio],
@@ -1469,3 +1510,21 @@ void main() {
14691510
});
14701511
});
14711512
}
1513+
1514+
Future<void> _loadKatexFonts() async {
1515+
const fs = LocalFileSystem();
1516+
final katexFontsDir = fs.currentDirectory.childDirectory(
1517+
fs.path.join('assets', 'KaTeX'));
1518+
assert(katexFontsDir.existsSync());
1519+
1520+
for (final fontFile in katexFontsDir.listSync()) {
1521+
if (fontFile is! File) continue;
1522+
if (p.extension(fontFile.path) != '.ttf') continue;
1523+
1524+
final [fontName, ...] = p.withoutExtension(fontFile.basename).split('-');
1525+
1526+
final bytes =
1527+
Future.value(fontFile.readAsBytesSync().buffer.asByteData());
1528+
await (FontLoader(fontName)..addFont(bytes)).load();
1529+
}
1530+
}

test/widgets/goldens/katex.png

11.3 KB
Loading

0 commit comments

Comments
 (0)