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 {
@override
int get microtaskCount => _currentFakeAsync.microtaskCount;
/// A whitelist [Set] that is used in mocking the asset message channel.
static Set<String> _allowedAssetKeys;
void _mockFlutterAssets() {
if (isBrowser) {
return;
......@@ -809,47 +806,30 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
return;
}
final String assetFolderPath = Platform.environment['UNIT_TEST_ASSETS'];
_ensureInitialized(assetFolderPath);
final String prefix = 'packages/${Platform.environment['APP_NAME']}/';
if (_allowedAssetKeys.isNotEmpty) {
defaultBinaryMessenger.setMockMessageHandler('flutter/assets', (ByteData message) {
String key = utf8.decode(message.buffer.asUint8List());
if (!_allowedAssetKeys.contains(key)) {
File asset = File(path.join(assetFolderPath, key));
if (!asset.existsSync()) {
// For tests in package, it will load assets with its own package prefix.
// In this case, we do a best-effort look up.
if (!key.startsWith(prefix))
if (!key.startsWith(prefix)) {
return null;
}
key = key.replaceFirst(prefix, '');
if (!_allowedAssetKeys.contains(key))
asset = File(path.join(assetFolderPath, key));
if (!asset.existsSync()) {
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) {
if (_allowedAssetKeys != null) {
return;
}
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.
if (!manifestFile.existsSync()) {
_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
Future<void> pump([ Duration duration, EnginePhase newPhase = EnginePhase.sendSemanticsUpdate ]) {
......
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