|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:gpt_markdown/gpt_markdown.dart'; |
| 4 | +import 'package:buzz/shared/theme/app_markdown_theme.dart'; |
| 5 | +import 'package:buzz/shared/theme/app_theme.dart'; |
| 6 | +import 'package:buzz/shared/theme/code_style.dart'; |
| 7 | + |
| 8 | +/// The Markdown theme a widget reads under [theme], with [wrapped] deciding |
| 9 | +/// whether the app states its code style above it. |
| 10 | +Future<GptMarkdownThemeData> _markdownTheme( |
| 11 | + WidgetTester tester, |
| 12 | + ThemeData theme, { |
| 13 | + required bool wrapped, |
| 14 | +}) async { |
| 15 | + late GptMarkdownThemeData read; |
| 16 | + final reader = Builder( |
| 17 | + builder: (context) { |
| 18 | + read = GptMarkdownTheme.of(context); |
| 19 | + return const SizedBox.shrink(); |
| 20 | + }, |
| 21 | + ); |
| 22 | + |
| 23 | + await tester.pumpWidget( |
| 24 | + MaterialApp( |
| 25 | + theme: theme, |
| 26 | + home: wrapped ? AppMarkdownTheme(child: reader) : reader, |
| 27 | + ), |
| 28 | + ); |
| 29 | + // MaterialApp animates a theme change, so the first frame after a pump can |
| 30 | + // still carry the previous one. |
| 31 | + await tester.pumpAndSettle(); |
| 32 | + return read; |
| 33 | +} |
| 34 | + |
| 35 | +void main() { |
| 36 | + testWidgets('states inline code and leaves the rest of Markdown alone', ( |
| 37 | + tester, |
| 38 | + ) async { |
| 39 | + for (final theme in [AppTheme.light(), AppTheme.dark()]) { |
| 40 | + final ambient = await _markdownTheme(tester, theme, wrapped: false); |
| 41 | + final stated = await _markdownTheme(tester, theme, wrapped: true); |
| 42 | + |
| 43 | + // Headings, rules and the rest keep the values gpt_markdown derives from |
| 44 | + // the app theme: a code-only change must not restyle anything else. |
| 45 | + expect(stated.h1, ambient.h1); |
| 46 | + expect(stated.h2, ambient.h2); |
| 47 | + expect(stated.h3, ambient.h3); |
| 48 | + expect(stated.h4, ambient.h4); |
| 49 | + expect(stated.h5, ambient.h5); |
| 50 | + expect(stated.h6, ambient.h6); |
| 51 | + expect(stated.hrLineColor, ambient.hrLineColor); |
| 52 | + expect(stated.hrLineThickness, ambient.hrLineThickness); |
| 53 | + expect(stated.hrLinePadding, ambient.hrLinePadding); |
| 54 | + expect(stated.highlightColor, ambient.highlightColor); |
| 55 | + expect(stated.linkColor, ambient.linkColor); |
| 56 | + expect(stated.linkHoverColor, ambient.linkHoverColor); |
| 57 | + expect( |
| 58 | + stated.autoAddDividerLineAfterH1, |
| 59 | + ambient.autoAddDividerLineAfterH1, |
| 60 | + ); |
| 61 | + expect(stated.styleSheet, ambient.styleSheet); |
| 62 | + |
| 63 | + // Inline code is the whole of the difference. |
| 64 | + final scheme = theme.colorScheme; |
| 65 | + expect(stated.inlineCode.fontFamily, CodeStyle.fontFamily); |
| 66 | + expect(stated.inlineCode.color, scheme.onSurface); |
| 67 | + expect(stated.inlineCode.backgroundColor, CodeStyle.background(scheme)); |
| 68 | + expect(stated.inlineCode.borderColor, CodeStyle.border(scheme)); |
| 69 | + expect( |
| 70 | + ambient.inlineCode.fontFamily, |
| 71 | + isNot(CodeStyle.fontFamily), |
| 72 | + reason: 'the package default is what this widget exists to replace', |
| 73 | + ); |
| 74 | + } |
| 75 | + }); |
| 76 | +} |
0 commit comments