Fix default test font on web (flutter/engine#40479)

Fix default test font on web
This commit is contained in:
LongCatIsLooong 2023-03-23 15:24:36 -07:00 committed by GitHub
parent 0a18ae544e
commit 42c2dc35ad
10 changed files with 29 additions and 26 deletions

View File

@ -16,7 +16,7 @@ import 'skia_object_cache.dart';
import 'text_fragmenter.dart';
import 'util.dart';
final List<String> _testFonts = <String>['Ahem', 'FlutterTest'];
final List<String> _testFonts = <String>['FlutterTest', 'Ahem'];
String? _effectiveFontFamily(String? fontFamily) {
return ui.debugEmulateFlutterTesterEnvironment && !_testFonts.contains(fontFamily)
? _testFonts.first

View File

@ -464,7 +464,7 @@ class EngineTextStyle implements ui.TextStyle {
final ui.Paint? foreground;
final List<ui.Shadow>? shadows;
static final List<String> _testFonts = <String>['Ahem', 'FlutterTest'];
static final List<String> _testFonts = <String>['FlutterTest', 'Ahem'];
String get effectiveFontFamily {
final String fontFamily = this.fontFamily.isEmpty ? FlutterViewEmbedder.defaultFontFamily : this.fontFamily;
// In the flutter tester environment, we use predictable-size test fonts.

View File

@ -119,8 +119,11 @@ class TextDimensions {
..fontFamily = canonicalizeFontFamily(fontFamily)!;
final double? height = textHeightStyle.height;
if (height != null) {
style.lineHeight = height.toString();
// Workaround the rounding introduced by https://github.com/flutter/flutter/issues/122066
// in tests.
final double? effectiveLineHeight = height ?? (fontFamily == 'FlutterTest' ? 1.0 : null);
if (effectiveLineHeight != null) {
style.lineHeight = effectiveLineHeight.toString();
}
_invalidateBoundsCache();
}

View File

@ -21,7 +21,7 @@ void testMain() {
group('flutter_tester emulation', () {
setUpCanvasKitTest();
test('defaults to Ahem font family',
test('defaults to FlutterTest font family',
() async {
final CkPictureRecorder recorder = CkPictureRecorder();
final CkCanvas canvas = recorder.beginRecording(kDefaultRegion);

View File

@ -97,7 +97,7 @@ void testMain() {
final bool resetValue = ui.debugEmulateFlutterTesterEnvironment;
ui.debugEmulateFlutterTesterEnvironment = true;
tearDownAll(() => ui.debugEmulateFlutterTesterEnvironment = resetValue);
const List<String> testFonts = <String>['Ahem', 'FlutterTest'];
const List<String> testFonts = <String>['FlutterTest', 'Ahem'];
test('The default test font is used when a non-test fontFamily is specified', () {
final String defaultTestFontFamily = testFonts.first;

View File

@ -37,7 +37,7 @@ const Color yellow = Color(0xFFFFEB3B);
const Color lightPurple = Color(0xFFE1BEE7);
final EngineParagraphStyle ahemStyle = EngineParagraphStyle(
fontFamily: 'ahem',
fontFamily: 'Ahem',
fontSize: 10,
);

View File

@ -410,9 +410,9 @@ Future<void> testMain() async {
});
test('various font sizes', () {
// Paragraphs and spans force the Ahem font in test mode. We need to trick
// them into thinking they are not in test mode, so they use the provided
// font family.
// Paragraphs and spans force the FlutterTest font in test mode. We need to
// trick them into thinking they are not in test mode, so they use the
// provided font family.
debugEmulateFlutterTesterEnvironment = false;
final EngineParagraphStyle style = EngineParagraphStyle(fontSize: 12.0, fontFamily: 'first');
final CanvasParagraphBuilder builder = CanvasParagraphBuilder(style);
@ -491,7 +491,7 @@ Future<void> testMain() async {
});
}
const String defaultFontFamily = 'Ahem';
const String defaultFontFamily = 'FlutterTest';
const num defaultFontSize = 14;
String paragraphStyle() {

View File

@ -388,7 +388,7 @@ Future<void> testMain() async {
test('respects text overflow', () {
final EngineParagraphStyle overflowStyle = EngineParagraphStyle(
fontFamily: 'ahem',
fontFamily: 'Ahem',
fontSize: 10,
ellipsis: '...',
);
@ -458,7 +458,7 @@ Future<void> testMain() async {
test('respects max lines', () {
final EngineParagraphStyle maxlinesStyle = EngineParagraphStyle(
fontFamily: 'ahem',
fontFamily: 'Ahem',
fontSize: 10,
maxLines: 2,
);
@ -506,13 +506,13 @@ Future<void> testMain() async {
test('respects text overflow and max lines combined', () {
final EngineParagraphStyle onelineStyle = EngineParagraphStyle(
fontFamily: 'ahem',
fontFamily: 'Ahem',
fontSize: 10,
maxLines: 1,
ellipsis: '...',
);
final EngineParagraphStyle multilineStyle = EngineParagraphStyle(
fontFamily: 'ahem',
fontFamily: 'Ahem',
fontSize: 10,
maxLines: 2,
ellipsis: '...',
@ -591,7 +591,7 @@ Future<void> testMain() async {
EngineParagraphStyle createStyle(ui.TextAlign textAlign) {
return EngineParagraphStyle(
fontFamily: 'ahem',
fontFamily: 'Ahem',
fontSize: 10,
textAlign: textAlign,
textDirection: ui.TextDirection.ltr,
@ -644,7 +644,7 @@ Future<void> testMain() async {
EngineParagraphStyle createStyle(ui.TextAlign textAlign) {
return EngineParagraphStyle(
fontFamily: 'ahem',
fontFamily: 'Ahem',
fontSize: 10,
textAlign: textAlign,
textDirection: ui.TextDirection.rtl,

View File

@ -143,7 +143,7 @@ Future<void> testMain() async {
test('should handle placeholder-only paragraphs', () {
final EngineParagraphStyle paragraphStyle = EngineParagraphStyle(
fontFamily: 'ahem',
fontFamily: 'Ahem',
fontSize: 10,
textAlign: ui.TextAlign.center,
);
@ -161,7 +161,7 @@ Future<void> testMain() async {
test('correct maxIntrinsicWidth when paragraph ends with placeholder', () {
final EngineParagraphStyle paragraphStyle = EngineParagraphStyle(
fontFamily: 'ahem',
fontFamily: 'Ahem',
fontSize: 10,
textAlign: ui.TextAlign.center,
);
@ -180,7 +180,7 @@ Future<void> testMain() async {
test('handles new line followed by a placeholder', () {
final EngineParagraphStyle paragraphStyle = EngineParagraphStyle(
fontFamily: 'ahem',
fontFamily: 'Ahem',
fontSize: 10,
textAlign: ui.TextAlign.center,
);

View File

@ -227,15 +227,15 @@ Future<void> testMain() async {
expect(paragraph.plainText, 'abcdef');
final List<DomElement> spans =
paragraph.toDomElement().querySelectorAll('flt-span').toList();
expect(spans[0].style.fontFamily, 'Ahem, $fallback, sans-serif');
expect(spans[0].style.fontFamily, 'FlutterTest, $fallback, sans-serif');
// The nested span here should not set it's family to default sans-serif.
expect(spans[1].style.fontFamily, 'Ahem, $fallback, sans-serif');
expect(spans[1].style.fontFamily, 'FlutterTest, $fallback, sans-serif');
},
// TODO(mdebbar): https://github.com/flutter/flutter/issues/46638
skip: browserEngine == BrowserEngine.firefox);
test('adds Arial and sans-serif as fallback fonts', () {
// Set this to false so it doesn't default to 'Ahem' font.
// Set this to false so it doesn't default to the test font.
debugEmulateFlutterTesterEnvironment = false;
final CanvasParagraph paragraph = plain(EngineParagraphStyle(
@ -253,7 +253,7 @@ Future<void> testMain() async {
skip: browserEngine == BrowserEngine.firefox);
test('does not add fallback fonts to generic families', () {
// Set this to false so it doesn't default to 'Ahem' font.
// Set this to false so it doesn't default to the default test font.
debugEmulateFlutterTesterEnvironment = false;
final CanvasParagraph paragraph = plain(EngineParagraphStyle(
@ -268,7 +268,7 @@ Future<void> testMain() async {
});
test('can set font families that need to be quoted', () {
// Set this to false so it doesn't default to 'Ahem' font.
// Set this to false so it doesn't default to the default test font.
debugEmulateFlutterTesterEnvironment = false;
final CanvasParagraph paragraph = plain(EngineParagraphStyle(
@ -363,7 +363,7 @@ Future<void> testMain() async {
final bool resetValue = debugEmulateFlutterTesterEnvironment;
debugEmulateFlutterTesterEnvironment = true;
tearDownAll(() => debugEmulateFlutterTesterEnvironment = resetValue);
const List<String> testFonts = <String>['Ahem', 'FlutterTest'];
const List<String> testFonts = <String>['FlutterTest', 'Ahem'];
test('The default test font is used when a non-test fontFamily is specified, or fontFamily is not specified', () {
final String defaultTestFontFamily = testFonts.first;