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.
This commit is contained in:
parent
e281c39164
commit
c355219154
@ -1,12 +0,0 @@
|
||||
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
|
@ -1 +0,0 @@
|
||||
This is a fake font.
|
@ -1 +0,0 @@
|
||||
.packages
|
@ -1,11 +0,0 @@
|
||||
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
|
@ -1 +0,0 @@
|
||||
!.packages
|
@ -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];
|
||||
|
||||
setUpAll(() {
|
||||
Cache.disableLocking();
|
||||
});
|
||||
for (final Style style in styles) {
|
||||
group('Assets (${style.name} file system)', () {
|
||||
late FileSystem fileSystem;
|
||||
late BufferLogger logger;
|
||||
late String? previousCacheFlutterRootValue;
|
||||
late Platform platform;
|
||||
|
||||
// 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();
|
||||
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,
|
||||
packagesPath: packagesPath,
|
||||
);
|
||||
setUpAll(() {
|
||||
previousCacheFlutterRootValue = Cache.flutterRoot;
|
||||
});
|
||||
|
||||
expect(asset.entries.containsKey('FontManifest.json'), isTrue);
|
||||
expect(
|
||||
await getValueAsString('FontManifest.json', asset),
|
||||
'[{"family":"packages/font/test_font","fonts":[{"asset":"packages/font/test_font_file"}]}]',
|
||||
);
|
||||
expect(asset.wasBuiltOnce(), true);
|
||||
expect(
|
||||
asset.inputFiles.map((File f) {
|
||||
return f.path;
|
||||
}),
|
||||
<String>[
|
||||
packagesPath,
|
||||
globals.fs.path.join(dataPath, 'font', 'pubspec.yaml'),
|
||||
manifestPath,
|
||||
globals.fs.path.join(dataPath, 'font', 'test_font_file'),
|
||||
],
|
||||
);
|
||||
});
|
||||
tearDownAll(() {
|
||||
Cache.flutterRoot = previousCacheFlutterRootValue;
|
||||
});
|
||||
|
||||
testUsingContext('handles empty pubspec with .packages', () async {
|
||||
final String dataPath = globals.fs.path.join(
|
||||
getFlutterRoot(),
|
||||
'packages',
|
||||
'flutter_tools',
|
||||
'test',
|
||||
'data',
|
||||
'fuchsia_test',
|
||||
);
|
||||
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'),
|
||||
);
|
||||
expect(asset.wasBuiltOnce(), true);
|
||||
expect(
|
||||
asset.inputFiles.map((File f) {
|
||||
return f.path;
|
||||
}),
|
||||
<String>[],
|
||||
);
|
||||
});
|
||||
});
|
||||
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(),
|
||||
);
|
||||
});
|
||||
|
||||
const String packageConfig = '''
|
||||
testWithoutContext('app font uses local font file', () async {
|
||||
final String packagesPath = fileSystem.path.join('main', '.packages');
|
||||
final String 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(assetBundle.entries, contains('FontManifest.json'));
|
||||
expect(
|
||||
await _getValueAsString('FontManifest.json', assetBundle),
|
||||
'[{"family":"packages/font/test_font","fonts":[{"asset":"packages/font/test_font_file"}]}]',
|
||||
);
|
||||
expect(assetBundle.wasBuiltOnce(), true);
|
||||
expect(
|
||||
assetBundle.inputFiles.map((File f) => f.path),
|
||||
equals(<String>[
|
||||
packagesPath,
|
||||
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'),
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
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,
|
||||
);
|
||||
|
||||
await assetBundle.build(
|
||||
manifestPath: manifestPath, // file doesn't exist
|
||||
packagesPath: packagesPath,
|
||||
flutterProject: FlutterProject.fromDirectoryTest(fileSystem.file(manifestPath).parent),
|
||||
);
|
||||
|
||||
expect(assetBundle.wasBuiltOnce(), true);
|
||||
expect(
|
||||
assetBundle.inputFiles.map((File f) => f.path),
|
||||
<String>[],
|
||||
);
|
||||
});
|
||||
|
||||
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 = '''
|
||||
}
|
||||
]
|
||||
}
|
||||
''';
|
||||
''');
|
||||
fileSystem.file('pubspec.yaml').writeAsStringSync('name: my_package');
|
||||
final ManifestAssetBundle assetBundle = ManifestAssetBundle(
|
||||
logger: logger,
|
||||
fileSystem: fileSystem,
|
||||
platform: platform,
|
||||
);
|
||||
|
||||
const String pubspecDotYaml = '''
|
||||
name: my_package
|
||||
''';
|
||||
await assetBundle.build(
|
||||
packagesPath: '.packages',
|
||||
targetPlatform: TargetPlatform.android_arm,
|
||||
flutterProject: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
|
||||
);
|
||||
|
||||
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();
|
||||
expect(assetBundle.entries.keys, contains('shaders/ink_sparkle.frag'));
|
||||
});
|
||||
|
||||
await asset.build(packagesPath: '.packages', targetPlatform: TargetPlatform.android_arm);
|
||||
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',
|
||||
);
|
||||
fileSystem.file(shaderPath).createSync(recursive: true);
|
||||
fileSystem.file(fileSystem.path.join('.dart_tool', 'package_config.json'))
|
||||
..createSync(recursive: true)
|
||||
..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,
|
||||
);
|
||||
|
||||
expect(asset.entries.keys, contains('shaders/ink_sparkle.frag'));
|
||||
}, overrides: <Type, Generator>{
|
||||
FileSystem: () => MemoryFileSystem.test(),
|
||||
ProcessManager: () => FakeProcessManager.empty(),
|
||||
});
|
||||
await assetBundle.build(
|
||||
packagesPath: '.packages',
|
||||
targetPlatform: TargetPlatform.web_javascript,
|
||||
flutterProject: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
|
||||
);
|
||||
|
||||
testUsingContext('Does bundle material shaders on 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.web_javascript);
|
||||
|
||||
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());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user