allow flutter tester to disable font loading from asset bundle (flutter/engine#33323)

This commit is contained in:
Jonah Williams
2022-05-13 15:04:06 -07:00
committed by GitHub
parent 17df57007d
commit 815195114e
9 changed files with 74 additions and 6 deletions

View File

@@ -810,6 +810,7 @@ FILE: ../../../flutter/lib/ui/dart_ui.cc
FILE: ../../../flutter/lib/ui/dart_ui.h
FILE: ../../../flutter/lib/ui/dart_wrapper.h
FILE: ../../../flutter/lib/ui/fixtures/DashInNooglerHat.jpg
FILE: ../../../flutter/lib/ui/fixtures/FontManifest.json
FILE: ../../../flutter/lib/ui/fixtures/Horizontal.jpg
FILE: ../../../flutter/lib/ui/fixtures/Horizontal.png
FILE: ../../../flutter/lib/ui/fixtures/hello_loop_2.gif

View File

@@ -190,6 +190,8 @@ struct Settings {
// Font settings
bool use_test_fonts = false;
bool use_asset_fonts = true;
// Indicates whether the embedding started a prefetch of the default font
// manager before creating the engine.
bool prefetched_default_font_manager = false;

View File

@@ -187,6 +187,8 @@ if (enable_unittests) {
"fixtures/Horizontal.png",
"fixtures/hello_loop_2.gif",
"fixtures/hello_loop_2.webp",
"fixtures/FontManifest.json",
"//flutter/third_party/txt/third_party/fonts/Roboto-Medium.ttf",
]
}

View File

@@ -0,0 +1 @@
[{"family":"Roboto","fonts":[{"asset":"Roboto-Medium.ttf"}]}]

View File

@@ -172,7 +172,9 @@ bool Engine::UpdateAssetManager(
}
// Using libTXT as the text engine.
font_collection_->RegisterFonts(asset_manager_);
if (settings_.use_asset_fonts) {
font_collection_->RegisterFonts(asset_manager_);
}
if (settings_.use_test_fonts) {
font_collection_->RegisterTestFonts();

View File

@@ -421,6 +421,8 @@ Settings SettingsFromCommandLine(const fml::CommandLine& command_line) {
settings.use_test_fonts =
command_line.HasOption(FlagForSwitch(Switch::UseTestFonts));
settings.use_asset_fonts =
!command_line.HasOption(FlagForSwitch(Switch::DisableAssetFonts));
std::string enable_skparagraph = command_line.GetOptionValueWithDefault(
FlagForSwitch(Switch::EnableSkParagraph), "");

View File

@@ -176,6 +176,11 @@ DEF_SWITCH(UseTestFonts,
"will make font resolution default to the Ahem test font on all "
"platforms (See https://www.w3.org/Style/CSS/Test/Fonts/Ahem/). "
"This option is only available on the desktop test shells.")
DEF_SWITCH(DisableAssetFonts,
"disable-asset-fonts",
"Prevents usage of any non-test fonts unless they were explicitly "
"Loaded via dart:ui font APIs. This option is only available on the "
"desktop test shells.")
DEF_SWITCH(PrefetchedDefaultFontManager,
"prefetched-default-font-manager",
"Indicates whether the embedding started a prefetch of the "

View File

@@ -2,7 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:ui' as ui;
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui';
import 'package:litetest/litetest.dart';
@@ -10,7 +14,7 @@ void main() {
test('Loading an asset that does not exist returns null', () async {
Object? error;
try {
await ui.ImmutableBuffer.fromAsset('ThisDoesNotExist');
await ImmutableBuffer.fromAsset('ThisDoesNotExist');
} catch (err) {
error = err;
}
@@ -19,14 +23,62 @@ void main() {
});
test('returns the bytes of a bundled asset', () async {
final ui.ImmutableBuffer buffer = await ui.ImmutableBuffer.fromAsset('assets/DashInNooglerHat.jpg');
final ImmutableBuffer buffer = await ImmutableBuffer.fromAsset('DashInNooglerHat.jpg');
expect(buffer.length == 354679, true);
});
test('can dispose immutable buffer', () async {
final ui.ImmutableBuffer buffer = await ui.ImmutableBuffer.fromAsset('assets/DashInNooglerHat.jpg');
final ImmutableBuffer buffer = await ImmutableBuffer.fromAsset('DashInNooglerHat.jpg');
buffer.dispose();
});
test('Tester can disable loading fonts from an asset bundle', () async {
final List<int> ahemImage = await _createPictureFromFont('Ahem');
// Font that is bundled in the asset directory of the test runner.
final List<int> bundledFontImage = await _createPictureFromFont('Roboto');
// Bundling fonts is disabled, so the font selected in both cases should be ahem.
// Therefore each buffer will contain identical contents.
expect(ahemImage, equals(bundledFontImage));
});
test('Tester can still load through dart:ui', () async {
/// Manually load font asset through dart.
final Uint8List encoded = utf8.encoder.convert(Uri(path: Uri.encodeFull('Roboto-Medium.ttf')).path);
final Completer<Uint8List> result = Completer<Uint8List>();
window.sendPlatformMessage('flutter/assets', encoded.buffer.asByteData(), (ByteData? data) {
result.complete(data!.buffer.asUint8List());
});
await loadFontFromList(await result.future, fontFamily: 'Roboto2');
final List<int> ahemImage = await _createPictureFromFont('Ahem');
// Font that is bundled in the asset directory of the test runner.
final List<int> bundledFontImage = await _createPictureFromFont('Roboto2');
// Bundling fonts is disabled, so the font selected in both cases should be ahem.
// Therefore each buffer will contain identical contents.
expect(ahemImage, notEquals(bundledFontImage));
});
}
Future<List<int>> _createPictureFromFont(String fontFamily) async {
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle(
fontFamily: fontFamily,
fontStyle: FontStyle.normal,
fontWeight: FontWeight.normal,
fontSize: 20,
));
builder.addText('Test');
final Paragraph paragraph = builder.build();
paragraph.layout(const ParagraphConstraints(width: 20 * 5.0));
final PictureRecorder recorder = PictureRecorder();
final Canvas canvas = Canvas(recorder);
canvas.drawParagraph(paragraph, Offset.zero);
final Picture picture = recorder.endRecording();
final Image image = await picture.toImage(100, 100);
final ByteData? data = await image.toByteData();
return data!.buffer.asUint8List().toList();
}

View File

@@ -286,7 +286,8 @@ def RunDartTest(build_dir, test_packages, dart_file, verbose_dart_snapshot, mult
command_args += [
'--use-test-fonts',
'--icu-data-file-path=%s' % os.path.join(build_dir, 'icudtl.dat'),
'--flutter-assets-dir=%s' % os.path.join(build_dir, 'gen', 'flutter', 'lib', 'ui'),
'--flutter-assets-dir=%s' % os.path.join(build_dir, 'gen', 'flutter', 'lib', 'ui', 'assets'),
'--disable-asset-fonts',
kernel_file_output,
]