-
Notifications
You must be signed in to change notification settings - Fork 10
Feat/warn missing fonts #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import 'package:flutter/rendering.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
|
|
||
| /// What to do when a widget tree asks for a font family that no font has been | ||
| /// registered for. Such text renders as placeholder blocks in goldens, which is | ||
| /// easy to miss when reviewing them. | ||
| enum MissingFontsBehavior { | ||
| /// Do not check for missing font families. | ||
| ignore, | ||
|
|
||
| /// Print a warning listing the missing families. This is the default. | ||
| warn, | ||
|
|
||
| /// Fail the test. Recommended on CI once the suite is clean. | ||
| fail, | ||
| } | ||
|
|
||
| /// The font families the rendered text asks for while [loadedFamilies], as | ||
| /// returned by [loadFonts], does not cover them. | ||
| /// | ||
| /// A family is only reported when none of the fallbacks of the style is covered | ||
| /// either, since the engine would then have a real font to use. | ||
| Set<String> findUnregisteredFontFamilies( | ||
| RenderObject root, { | ||
| required Set<String> loadedFamilies, | ||
| }) { | ||
| final unregisteredFamilies = <String>{}; | ||
|
|
||
| void visitStyle(TextStyle? style) { | ||
| final family = style?.fontFamily; | ||
| if (style == null || family == null) return; | ||
|
|
||
| final candidates = [family, ...?style.fontFamilyFallback]; | ||
| if (candidates.any(loadedFamilies.contains)) return; | ||
|
|
||
| unregisteredFamilies.add(family); | ||
| } | ||
|
|
||
| void visitSpan(InlineSpan span) { | ||
| if (span is TextSpan) visitStyle(span.style); | ||
| span.visitChildren((child) { | ||
| if (child != span) visitSpan(child); | ||
|
|
||
| return true; | ||
| }); | ||
| } | ||
|
|
||
| void visitRenderObject(RenderObject renderObject) { | ||
| switch (renderObject) { | ||
| case final RenderParagraph paragraph: | ||
| visitSpan(paragraph.text); | ||
| case final RenderEditable editable: | ||
| if (editable.text case final text?) visitSpan(text); | ||
| default: | ||
| break; | ||
| } | ||
| renderObject.visitChildren(visitRenderObject); | ||
| } | ||
|
|
||
| visitRenderObject(root); | ||
|
|
||
| return unregisteredFamilies; | ||
| } | ||
|
|
||
| /// Reports the [families] no font is registered for, according to [behavior], | ||
| /// and returns the ones it warned about. | ||
| /// | ||
| /// The families of [alreadyWarned] are skipped, so that the output stays | ||
| /// readable when many goldens share the same gap. | ||
| Set<String> reportUnregisteredFontFamilies( | ||
| Set<String> families, | ||
| MissingFontsBehavior behavior, { | ||
| Set<String> alreadyWarned = const {}, | ||
| }) { | ||
| if (behavior == MissingFontsBehavior.ignore || families.isEmpty) { | ||
| return const {}; | ||
| } | ||
|
|
||
| if (behavior == MissingFontsBehavior.fail) { | ||
| throw TestFailure(_message(families)); | ||
| } | ||
|
|
||
| final familiesToWarnAbout = families.difference(alreadyWarned); | ||
| if (familiesToWarnAbout.isEmpty) return const {}; | ||
|
|
||
| debugPrint(_message(familiesToWarnAbout)); | ||
|
|
||
| return familiesToWarnAbout; | ||
| } | ||
|
|
||
| String _message(Set<String> families) { | ||
| final sortedFamilies = families.toList()..sort(); | ||
|
|
||
| return ''' | ||
| adaptive_test: no font is registered for ${sortedFamilies.map((family) => "'$family'").join(', ')}. | ||
| The text using ${sortedFamilies.length > 1 ? 'those families' : 'that family'} renders as placeholder blocks in the golden. | ||
| Declare the font in your pubspec.yaml so that loadFonts() picks it up, or, if it | ||
| is provided by the host OS, register a stand-in with FontLoader before the test.'''; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import 'package:adaptive_test/adaptive_test.dart'; | ||
| import 'package:adaptive_test/src/configuration.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
|
|
||
| void main() { | ||
| late Set<String> loadedFamilies; | ||
|
|
||
| setUpAll(() async => loadedFamilies = await loadFonts()); | ||
|
|
||
| group('loadFonts', () { | ||
| test('returns the families it registered', () { | ||
| expect(loadedFamilies, isNotEmpty); | ||
| }); | ||
|
|
||
| test('records the loaded families in the configuration', () { | ||
| expect( | ||
| FontLoadingRegistry.loadedFontFamilies, | ||
| containsAll(loadedFamilies), | ||
| ); | ||
| }); | ||
| }); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All how this should stay private. or at least not being exposed to the public API of adaptive_test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed I think I missed this part when reviewing, sorry i'll see what i can change