Unverified Commit c3552191 authored by Andrew Kolos's avatar Andrew Kolos Committed by GitHub

make asset_test.dart tests not dependent on context (#141331)

Part of work on https://github.com/flutter/flutter/issues/141330, which is a part of work on https://github.com/flutter/flutter/issues/140092

This is a refactoring; there should be no behavioral changes in these tests.
parent e281c391
name: font
description: A test project that contains a font.
environment:
sdk: '>=3.2.0-0 <4.0.0'
flutter:
uses-material-design: true
fonts:
- family: test_font
fonts:
- asset: test_font_file
name: main
description: A test project that has a package with a font as a dependency.
environment:
sdk: '>=3.2.0-0 <4.0.0'
dependencies:
font:
path: ../font
# PUBSPEC CHECKSUM: 0000
......@@ -5,86 +5,189 @@
import 'package:file/memory.dart';
import 'package:flutter_tools/src/asset.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/base/user_messages.dart';
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
import 'package:flutter_tools/src/project.dart';
// We aren't using this to construct paths—only to expose a type.
import 'package:path/path.dart' show Style; // flutter_ignore: package_path_import
import '../src/common.dart';
import '../src/context.dart';
void main() {
group('Assets', () {
final String dataPath = globals.fs.path.join(
getFlutterRoot(),
'packages',
'flutter_tools',
'test',
'data',
'asset_test',
);
final Style posix = Style.posix;
final Style windows = Style.windows;
final List<Style> styles = <Style>[posix, windows];
for (final Style style in styles) {
group('Assets (${style.name} file system)', () {
late FileSystem fileSystem;
late BufferLogger logger;
late String? previousCacheFlutterRootValue;
late Platform platform;
setUpAll(() {
Cache.disableLocking();
previousCacheFlutterRootValue = Cache.flutterRoot;
});
// This test intentionally does not use a memory file system to ensure
// that AssetBundle with fonts also works on Windows.
testUsingContext('app font uses local font file', () async {
final AssetBundle asset = AssetBundleFactory.instance.createBundle();
tearDownAll(() {
Cache.flutterRoot = previousCacheFlutterRootValue;
});
setUp(() {
fileSystem = MemoryFileSystem(
style: style == Style.posix ? FileSystemStyle.posix : FileSystemStyle.windows,
);
logger = BufferLogger.test();
platform = FakePlatform(
operatingSystem: style == Style.posix ? 'linux' : 'windows');
Cache.flutterRoot = Cache.defaultFlutterRoot(
platform: platform,
fileSystem: fileSystem,
userMessages: UserMessages(),
);
});
testWithoutContext('app font uses local font file', () async {
final String packagesPath = fileSystem.path.join('main', '.packages');
final String manifestPath =
globals.fs.path.join(dataPath, 'main', 'pubspec.yaml');
final String packagesPath =
globals.fs.path.join(dataPath, 'main', '.packages');
await asset.build(
manifestPath: manifestPath,
fileSystem.path.join('main', 'pubspec.yaml');
final ManifestAssetBundle assetBundle = ManifestAssetBundle(
logger: logger,
fileSystem: fileSystem,
platform: platform,
splitDeferredAssets: true,
);
fileSystem.file(fileSystem.path.join('font', 'pubspec.yaml'))
..createSync(recursive: true)
..writeAsStringSync(r'''
name: font
description: A test project that contains a font.
environment:
sdk: '>=3.2.0-0 <4.0.0'
flutter:
uses-material-design: true
fonts:
- family: test_font
fonts:
- asset: test_font_file
''');
fileSystem.file(fileSystem.path.join('font', 'test_font_file'))
..createSync(recursive: true)
..writeAsStringSync('This is a fake font.');
fileSystem.file(
fileSystem.path.join('main', '.dart_tool', 'package_config.json'))
..createSync(recursive: true)
..writeAsStringSync(r'''
{
"configVersion": 2,
"packages": [
{
"name": "font",
"rootUri": "../../font",
"packageUri": "lib/",
"languageVersion": "3.2"
},
{
"name": "main",
"rootUri": "../",
"packageUri": "lib/",
"languageVersion": "3.2"
}
],
"generated": "2024-01-08T19:39:02.396620Z",
"generator": "pub",
"generatorVersion": "3.3.0-276.0.dev"
}
''');
fileSystem.file(manifestPath)
..createSync(recursive: true)
..writeAsStringSync(r'''
name: main
description: A test project that has a package with a font as a dependency.
environment:
sdk: '>=3.2.0-0 <4.0.0'
dependencies:
font:
path: ../font
''');
await assetBundle.build(
packagesPath: packagesPath,
manifestPath: manifestPath,
flutterProject: FlutterProject.fromDirectoryTest(fileSystem.directory('main')),
);
expect(asset.entries.containsKey('FontManifest.json'), isTrue);
expect(assetBundle.entries, contains('FontManifest.json'));
expect(
await getValueAsString('FontManifest.json', asset),
await _getValueAsString('FontManifest.json', assetBundle),
'[{"family":"packages/font/test_font","fonts":[{"asset":"packages/font/test_font_file"}]}]',
);
expect(asset.wasBuiltOnce(), true);
expect(assetBundle.wasBuiltOnce(), true);
expect(
asset.inputFiles.map((File f) {
return f.path;
}),
<String>[
assetBundle.inputFiles.map((File f) => f.path),
equals(<String>[
packagesPath,
globals.fs.path.join(dataPath, 'font', 'pubspec.yaml'),
manifestPath,
globals.fs.path.join(dataPath, 'font', 'test_font_file'),
],
fileSystem.path.join(fileSystem.currentDirectory.path, 'font', 'pubspec.yaml'),
fileSystem.path.join(fileSystem.currentDirectory.path, manifestPath),
fileSystem.path.join(fileSystem.currentDirectory.path,'font', 'test_font_file'),
]),
);
});
testUsingContext('handles empty pubspec with .packages', () async {
final String dataPath = globals.fs.path.join(
getFlutterRoot(),
'packages',
'flutter_tools',
'test',
'data',
'fuchsia_test',
testWithoutContext('handles empty pubspec with .packages', () async {
final String packagesPath = fileSystem.path.join('fuchsia_test', 'main', '.packages');
final String manifestPath =
fileSystem.path.join('fuchsia_test', 'main', 'pubspec.yaml');
fileSystem.directory(fileSystem.file(manifestPath)).parent.createSync(recursive: true);
fileSystem.directory(fileSystem.file(packagesPath)).parent.createSync(recursive: true);
final ManifestAssetBundle assetBundle = ManifestAssetBundle(
logger: logger,
fileSystem: fileSystem,
platform: platform,
splitDeferredAssets: true,
);
final AssetBundle asset = AssetBundleFactory.instance.createBundle();
await asset.build(
manifestPath: globals.fs.path
.join(dataPath, 'main', 'pubspec.yaml'), // file doesn't exist
packagesPath: globals.fs.path.join(dataPath, 'main', '.packages'),
await assetBundle.build(
manifestPath: manifestPath, // file doesn't exist
packagesPath: packagesPath,
flutterProject: FlutterProject.fromDirectoryTest(fileSystem.file(manifestPath).parent),
);
expect(asset.wasBuiltOnce(), true);
expect(assetBundle.wasBuiltOnce(), true);
expect(
asset.inputFiles.map((File f) {
return f.path;
}),
assetBundle.inputFiles.map((File f) => f.path),
<String>[],
);
});
});
const String packageConfig = '''
testWithoutContext('bundles material shaders on non-web platforms',
() async {
final String shaderPath = fileSystem.path.join(
Cache.flutterRoot!,
'packages',
'flutter',
'lib',
'src',
'material',
'shaders',
'ink_sparkle.frag',
);
fileSystem.file(shaderPath).createSync(recursive: true);
fileSystem.file(fileSystem.path.join('.dart_tool', 'package_config.json'))
..createSync(recursive: true)
..writeAsStringSync(r'''
{
"configVersion": 2,
"packages":[
......@@ -96,53 +199,70 @@ const String packageConfig = '''
}
]
}
''';
const String pubspecDotYaml = '''
name: my_package
''';
testUsingContext('Bundles material shaders on non-web platforms', () async {
final String shaderPath = globals.fs.path.join(
Cache.flutterRoot!,
'packages', 'flutter', 'lib', 'src', 'material', 'shaders', 'ink_sparkle.frag'
''');
fileSystem.file('pubspec.yaml').writeAsStringSync('name: my_package');
final ManifestAssetBundle assetBundle = ManifestAssetBundle(
logger: logger,
fileSystem: fileSystem,
platform: platform,
);
globals.fs.file(shaderPath).createSync(recursive: true);
globals.fs.file('.dart_tool/package_config.json')
..createSync(recursive: true)
..writeAsStringSync(packageConfig);
globals.fs.file('pubspec.yaml').writeAsStringSync(pubspecDotYaml);
final AssetBundle asset = AssetBundleFactory.instance.createBundle();
await asset.build(packagesPath: '.packages', targetPlatform: TargetPlatform.android_arm);
await assetBundle.build(
packagesPath: '.packages',
targetPlatform: TargetPlatform.android_arm,
flutterProject: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
);
expect(asset.entries.keys, contains('shaders/ink_sparkle.frag'));
}, overrides: <Type, Generator>{
FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => FakeProcessManager.empty(),
expect(assetBundle.entries.keys, contains('shaders/ink_sparkle.frag'));
});
testUsingContext('Does bundle material shaders on web platforms', () async {
final String shaderPath = globals.fs.path.join(
testWithoutContext('bundles material shaders on web platforms',
() async {
final String shaderPath = fileSystem.path.join(
Cache.flutterRoot!,
'packages', 'flutter', 'lib', 'src', 'material', 'shaders', 'ink_sparkle.frag'
'packages',
'flutter',
'lib',
'src',
'material',
'shaders',
'ink_sparkle.frag',
);
globals.fs.file(shaderPath).createSync(recursive: true);
globals.fs.file('.dart_tool/package_config.json')
fileSystem.file(shaderPath).createSync(recursive: true);
fileSystem.file(fileSystem.path.join('.dart_tool', 'package_config.json'))
..createSync(recursive: true)
..writeAsStringSync(packageConfig);
globals.fs.file('pubspec.yaml').writeAsStringSync(pubspecDotYaml);
final AssetBundle asset = AssetBundleFactory.instance.createBundle();
..writeAsStringSync(r'''
{
"configVersion": 2,
"packages":[
{
"name": "my_package",
"rootUri": "file:///",
"packageUri": "lib/",
"languageVersion": "2.17"
}
]
}
''');
fileSystem.file('pubspec.yaml').writeAsStringSync('name: my_package');
final ManifestAssetBundle assetBundle = ManifestAssetBundle(
logger: logger,
fileSystem: fileSystem,
platform: platform,
);
await asset.build(packagesPath: '.packages', targetPlatform: TargetPlatform.web_javascript);
await assetBundle.build(
packagesPath: '.packages',
targetPlatform: TargetPlatform.web_javascript,
flutterProject: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
);
expect(asset.entries.keys, contains('shaders/ink_sparkle.frag'));
}, overrides: <Type, Generator>{
FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => FakeProcessManager.empty(),
expect(assetBundle.entries.keys, contains('shaders/ink_sparkle.frag'));
});
});
}
}
Future<String> getValueAsString(String key, AssetBundle asset) async {
Future<String> _getValueAsString(String key, AssetBundle asset) async {
return String.fromCharCodes(await asset.entries[key]!.contentsAsBytes());
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment