Unverified Commit 677b7c15 authored by Lau Ching Jun's avatar Lau Ching Jun Committed by GitHub

Load assets during test from file system instead of manifest. (#36553)

parent 76cbbeb6
...@@ -798,9 +798,6 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding { ...@@ -798,9 +798,6 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
@override @override
int get microtaskCount => _currentFakeAsync.microtaskCount; int get microtaskCount => _currentFakeAsync.microtaskCount;
/// A whitelist [Set] that is used in mocking the asset message channel.
static Set<String> _allowedAssetKeys;
void _mockFlutterAssets() { void _mockFlutterAssets() {
if (isBrowser) { if (isBrowser) {
return; return;
...@@ -809,46 +806,29 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding { ...@@ -809,46 +806,29 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
return; return;
} }
final String assetFolderPath = Platform.environment['UNIT_TEST_ASSETS']; final String assetFolderPath = Platform.environment['UNIT_TEST_ASSETS'];
_ensureInitialized(assetFolderPath);
final String prefix = 'packages/${Platform.environment['APP_NAME']}/'; final String prefix = 'packages/${Platform.environment['APP_NAME']}/';
if (_allowedAssetKeys.isNotEmpty) { defaultBinaryMessenger.setMockMessageHandler('flutter/assets', (ByteData message) {
defaultBinaryMessenger.setMockMessageHandler('flutter/assets', (ByteData message) { String key = utf8.decode(message.buffer.asUint8List());
String key = utf8.decode(message.buffer.asUint8List()); File asset = File(path.join(assetFolderPath, key));
if (!_allowedAssetKeys.contains(key)) {
// For tests in package, it will load assets with its own package prefix. if (!asset.existsSync()) {
// In this case, we do a best-effort look up. // For tests in package, it will load assets with its own package prefix.
if (!key.startsWith(prefix)) // In this case, we do a best-effort look up.
return null; if (!key.startsWith(prefix)) {
key = key.replaceFirst(prefix, ''); return null;
if (!_allowedAssetKeys.contains(key))
return null;
} }
final File asset = File(path.join(assetFolderPath, key));
final Uint8List encoded = Uint8List.fromList(asset.readAsBytesSync());
return Future<ByteData>.value(encoded.buffer.asByteData());
});
}
}
void _ensureInitialized(String assetFolderPath) { key = key.replaceFirst(prefix, '');
if (_allowedAssetKeys != null) { asset = File(path.join(assetFolderPath, key));
return; if (!asset.existsSync()) {
} return null;
final File manifestFile = File( }
path.join(assetFolderPath, 'AssetManifest.json')); }
// If the file does not exist, it means there is no asset declared in
// the project. final Uint8List encoded = Uint8List.fromList(asset.readAsBytesSync());
if (!manifestFile.existsSync()) { return Future<ByteData>.value(encoded.buffer.asByteData());
_allowedAssetKeys = <String>{}; });
return;
}
final Map<String, dynamic> manifest = json.decode(manifestFile.readAsStringSync());
_allowedAssetKeys = <String>{
'AssetManifest.json',
...manifest.values.cast<List<dynamic>>().expand<dynamic>((List<dynamic> e) => e).cast<String>(),
};
} }
@override @override
......
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