import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:gpt_markdown/gpt_markdown.dart'; import 'package:buzz/shared/theme/app_markdown_theme.dart'; import 'package:buzz/shared/theme/app_theme.dart'; import 'package:buzz/shared/theme/code_style.dart'; /// The Markdown theme a widget reads under [theme], with [wrapped] deciding /// whether the app states its code style above it. Future _markdownTheme( WidgetTester tester, ThemeData theme, { required bool wrapped, }) async { late GptMarkdownThemeData read; final reader = Builder( builder: (context) { read = GptMarkdownTheme.of(context); return const SizedBox.shrink(); }, ); await tester.pumpWidget( MaterialApp( theme: theme, home: wrapped ? AppMarkdownTheme(child: reader) : reader, ), ); // MaterialApp animates a theme change, so the first frame after a pump can // still carry the previous one. await tester.pumpAndSettle(); return read; } void main() { testWidgets('keeps one theme object until the app theme changes', ( tester, ) async { final theme = ValueNotifier(AppTheme.light()); final revision = ValueNotifier(0); addTearDown(theme.dispose); addTearDown(revision.dispose); final reads = []; await tester.pumpWidget( ValueListenableBuilder( valueListenable: theme, builder: (_, data, _) => MaterialApp( theme: data, home: ValueListenableBuilder( valueListenable: revision, // A fresh subtree each time, as an ordinary ancestor rebuild gives. builder: (_, generation, _) => AppMarkdownTheme( child: Builder( key: ValueKey(generation), builder: (context) { reads.add(GptMarkdownTheme.of(context)); return const SizedBox.shrink(); }, ), ), ), ), ), ); await tester.pumpAndSettle(); final first = reads.last; for (var i = 0; i < 3; i++) { revision.value++; await tester.pump(); // GptMarkdownTheme notifies by identity: a new object would make every // GptMarkdown in the app parse its text again. expect(reads.last, same(first)); } theme.value = AppTheme.dark(); await tester.pumpAndSettle(); expect(reads.last, isNot(same(first))); expect(reads.last.inlineCode.color, AppTheme.dark().colorScheme.onSurface); }); testWidgets('states inline code and leaves the rest of Markdown alone', ( tester, ) async { for (final theme in [AppTheme.light(), AppTheme.dark()]) { final ambient = await _markdownTheme(tester, theme, wrapped: false); final stated = await _markdownTheme(tester, theme, wrapped: true); // Headings, rules and the rest keep the values gpt_markdown derives from // the app theme: a code-only change must not restyle anything else. expect(stated.h1, ambient.h1); expect(stated.h2, ambient.h2); expect(stated.h3, ambient.h3); expect(stated.h4, ambient.h4); expect(stated.h5, ambient.h5); expect(stated.h6, ambient.h6); expect(stated.hrLineColor, ambient.hrLineColor); expect(stated.hrLineThickness, ambient.hrLineThickness); expect(stated.hrLinePadding, ambient.hrLinePadding); expect(stated.highlightColor, ambient.highlightColor); expect(stated.linkColor, ambient.linkColor); expect(stated.linkHoverColor, ambient.linkHoverColor); expect( stated.autoAddDividerLineAfterH1, ambient.autoAddDividerLineAfterH1, ); expect(stated.styleSheet, ambient.styleSheet); // Inline code is the whole of the difference. final scheme = theme.colorScheme; expect(stated.inlineCode.fontFamily, CodeStyle.fontFamily); expect(stated.inlineCode.color, scheme.onSurface); expect(stated.inlineCode.backgroundColor, CodeStyle.background(scheme)); expect(stated.inlineCode.borderColor, CodeStyle.border(scheme)); expect( ambient.inlineCode.fontFamily, isNot(CodeStyle.fontFamily), reason: 'the package default is what this widget exists to replace', ); } }); }