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

This commit is contained in:
Lau Ching Jun 2019-07-19 14:54:24 -07:00 committed by GitHub
parent 76cbbeb627
commit 677b7c15c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 {
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.
// In this case, we do a best-effort look up.
if (!key.startsWith(prefix))
return null;
key = key.replaceFirst(prefix, '');
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) { if (!asset.existsSync()) {
if (_allowedAssetKeys != null) { // For tests in package, it will load assets with its own package prefix.
return; // In this case, we do a best-effort look up.
} if (!key.startsWith(prefix)) {
final File manifestFile = File( return null;
path.join(assetFolderPath, 'AssetManifest.json')); }
// If the file does not exist, it means there is no asset declared in
// the project. key = key.replaceFirst(prefix, '');
if (!manifestFile.existsSync()) { asset = File(path.join(assetFolderPath, key));
_allowedAssetKeys = <String>{}; if (!asset.existsSync()) {
return; return null;
} }
final Map<String, dynamic> manifest = json.decode(manifestFile.readAsStringSync()); }
_allowedAssetKeys = <String>{
'AssetManifest.json', final Uint8List encoded = Uint8List.fromList(asset.readAsBytesSync());
...manifest.values.cast<List<dynamic>>().expand<dynamic>((List<dynamic> e) => e).cast<String>(), return Future<ByteData>.value(encoded.buffer.asByteData());
}; });
} }
@override @override