|
1 | 1 | import 'package:checks/checks.dart';
|
| 2 | +import 'package:file/file.dart'; |
| 3 | +import 'package:file/local.dart'; |
2 | 4 | import 'package:flutter/cupertino.dart';
|
3 | 5 | import 'package:flutter/foundation.dart';
|
4 | 6 | import 'package:flutter/material.dart';
|
5 | 7 | import 'package:flutter/rendering.dart';
|
| 8 | +import 'package:flutter/services.dart'; |
6 | 9 | import 'package:flutter_checks/flutter_checks.dart';
|
7 | 10 | import 'package:flutter_test/flutter_test.dart';
|
| 11 | +import 'package:path/path.dart' as p; |
8 | 12 | import 'package:url_launcher/url_launcher.dart';
|
9 | 13 | import 'package:zulip/api/core.dart';
|
10 | 14 | import 'package:zulip/model/content.dart';
|
@@ -723,6 +727,43 @@ void main() {
|
723 | 727 | });
|
724 | 728 | }
|
725 | 729 | });
|
| 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 | + }); |
726 | 767 | });
|
727 | 768 |
|
728 | 769 | /// Make a [TargetFontSizeFinder] to pass to [checkFontSizeRatio],
|
@@ -1469,3 +1510,21 @@ void main() {
|
1469 | 1510 | });
|
1470 | 1511 | });
|
1471 | 1512 | }
|
| 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 | +} |
0 commit comments