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 @@ ...@@ -5,86 +5,189 @@
import 'package:file/memory.dart'; import 'package:file/memory.dart';
import 'package:flutter_tools/src/asset.dart'; import 'package:flutter_tools/src/asset.dart';
import 'package:flutter_tools/src/base/file_system.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/build_info.dart';
import 'package:flutter_tools/src/cache.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/common.dart';
import '../src/context.dart';
void main() { void main() {
group('Assets', () { final Style posix = Style.posix;
final String dataPath = globals.fs.path.join( final Style windows = Style.windows;
getFlutterRoot(), final List<Style> styles = <Style>[posix, windows];
'packages',
'flutter_tools', for (final Style style in styles) {
'test', group('Assets (${style.name} file system)', () {
'data', late FileSystem fileSystem;
'asset_test', late BufferLogger logger;
); late String? previousCacheFlutterRootValue;
late Platform platform;
setUpAll(() { setUpAll(() {
Cache.disableLocking(); previousCacheFlutterRootValue = Cache.flutterRoot;
}); });
// This test intentionally does not use a memory file system to ensure tearDownAll(() {
// that AssetBundle with fonts also works on Windows. Cache.flutterRoot = previousCacheFlutterRootValue;
testUsingContext('app font uses local font file', () async { });
final AssetBundle asset = AssetBundleFactory.instance.createBundle();
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 = final String manifestPath =
globals.fs.path.join(dataPath, 'main', 'pubspec.yaml'); fileSystem.path.join('main', 'pubspec.yaml');
final String packagesPath = final ManifestAssetBundle assetBundle = ManifestAssetBundle(
globals.fs.path.join(dataPath, 'main', '.packages'); logger: logger,
await asset.build( fileSystem: fileSystem,
manifestPath: manifestPath, 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, packagesPath: packagesPath,
manifestPath: manifestPath,
flutterProject: FlutterProject.fromDirectoryTest(fileSystem.directory('main')),
); );
expect(asset.entries.containsKey('FontManifest.json'), isTrue); expect(assetBundle.entries, contains('FontManifest.json'));
expect( expect(
await getValueAsString('FontManifest.json', asset), await _getValueAsString('FontManifest.json', assetBundle),
'[{"family":"packages/font/test_font","fonts":[{"asset":"packages/font/test_font_file"}]}]', '[{"family":"packages/font/test_font","fonts":[{"asset":"packages/font/test_font_file"}]}]',
); );
expect(asset.wasBuiltOnce(), true); expect(assetBundle.wasBuiltOnce(), true);
expect( expect(
asset.inputFiles.map((File f) { assetBundle.inputFiles.map((File f) => f.path),
return f.path; equals(<String>[
}),
<String>[
packagesPath, packagesPath,
globals.fs.path.join(dataPath, 'font', 'pubspec.yaml'), fileSystem.path.join(fileSystem.currentDirectory.path, 'font', 'pubspec.yaml'),
manifestPath, fileSystem.path.join(fileSystem.currentDirectory.path, manifestPath),
globals.fs.path.join(dataPath, 'font', 'test_font_file'), fileSystem.path.join(fileSystem.currentDirectory.path,'font', 'test_font_file'),
], ]),
); );
}); });
testUsingContext('handles empty pubspec with .packages', () async { testWithoutContext('handles empty pubspec with .packages', () async {
final String dataPath = globals.fs.path.join( final String packagesPath = fileSystem.path.join('fuchsia_test', 'main', '.packages');
getFlutterRoot(), final String manifestPath =
'packages', fileSystem.path.join('fuchsia_test', 'main', 'pubspec.yaml');
'flutter_tools',
'test', fileSystem.directory(fileSystem.file(manifestPath)).parent.createSync(recursive: true);
'data', fileSystem.directory(fileSystem.file(packagesPath)).parent.createSync(recursive: true);
'fuchsia_test',
final ManifestAssetBundle assetBundle = ManifestAssetBundle(
logger: logger,
fileSystem: fileSystem,
platform: platform,
splitDeferredAssets: true,
); );
final AssetBundle asset = AssetBundleFactory.instance.createBundle();
await asset.build( await assetBundle.build(
manifestPath: globals.fs.path manifestPath: manifestPath, // file doesn't exist
.join(dataPath, 'main', 'pubspec.yaml'), // file doesn't exist packagesPath: packagesPath,
packagesPath: globals.fs.path.join(dataPath, 'main', '.packages'), flutterProject: FlutterProject.fromDirectoryTest(fileSystem.file(manifestPath).parent),
); );
expect(asset.wasBuiltOnce(), true);
expect(assetBundle.wasBuiltOnce(), true);
expect( expect(
asset.inputFiles.map((File f) { assetBundle.inputFiles.map((File f) => f.path),
return f.path;
}),
<String>[], <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, "configVersion": 2,
"packages":[ "packages":[
...@@ -96,53 +199,70 @@ const String packageConfig = ''' ...@@ -96,53 +199,70 @@ const String packageConfig = '''
} }
] ]
} }
'''; ''');
fileSystem.file('pubspec.yaml').writeAsStringSync('name: my_package');
const String pubspecDotYaml = ''' final ManifestAssetBundle assetBundle = ManifestAssetBundle(
name: my_package logger: logger,
'''; fileSystem: fileSystem,
platform: platform,
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'
); );
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')); expect(assetBundle.entries.keys, contains('shaders/ink_sparkle.frag'));
}, overrides: <Type, Generator>{
FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => FakeProcessManager.empty(),
}); });
testUsingContext('Does bundle material shaders on web platforms', () async { testWithoutContext('bundles material shaders on web platforms',
final String shaderPath = globals.fs.path.join( () async {
final String shaderPath = fileSystem.path.join(
Cache.flutterRoot!, 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); fileSystem.file(shaderPath).createSync(recursive: true);
globals.fs.file('.dart_tool/package_config.json') fileSystem.file(fileSystem.path.join('.dart_tool', 'package_config.json'))
..createSync(recursive: true) ..createSync(recursive: true)
..writeAsStringSync(packageConfig); ..writeAsStringSync(r'''
globals.fs.file('pubspec.yaml').writeAsStringSync(pubspecDotYaml); {
final AssetBundle asset = AssetBundleFactory.instance.createBundle(); "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')); expect(assetBundle.entries.keys, contains('shaders/ink_sparkle.frag'));
}, overrides: <Type, Generator>{
FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => FakeProcessManager.empty(),
}); });
});
}
} }
Future<String> getValueAsString(String key, AssetBundle asset) async { Future<String> _getValueAsString(String key, AssetBundle asset) async {
return String.fromCharCodes(await asset.entries[key]!.contentsAsBytes()); 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