Skip to content

Instantly share code, notes, and snippets.

@gamebox
Created May 1, 2019 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gamebox/8eedba4995ced91609bc07b2a3cd0250 to your computer and use it in GitHub Desktop.
Save gamebox/8eedba4995ced91609bc07b2a3cd0250 to your computer and use it in GitHub Desktop.
A possible implementation of TestAssetBundle for Flutter tests
class TestAssetBundle extends CachingAssetBundle {
TestAssetBundle(Map<String, List<String>> assets) : _assets = assets {
for (String assetList in assets.keys) {
for (String asset in assets[assetList]) {
_assetMap[asset] = bytesForFile(asset);
}
}
}
final Map<String, ByteData> _assetMap = <String, ByteData>{};
final Map<String, List<String>> _assets;
@override
Future<ByteData> load(String key) {
if (key == 'AssetManifest.json') {
return Future<ByteData>.value(bytesForJsonLike(_assets));
}
return Future<ByteData>.value(_assetMap[key]);
}
}
ByteData bytesForJsonLike(Map<String, dynamic> jsonLike) => ByteData.view(
Uint8List.fromList(const Utf8Encoder().convert(json.encode(jsonLike)))
.buffer);
ByteData bytesForFile(String path) => ByteData.view(
Uint8List.fromList(File('../' + path).readAsBytesSync()).buffer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment