Created
May 1, 2019 21:47
-
-
Save gamebox/8eedba4995ced91609bc07b2a3cd0250 to your computer and use it in GitHub Desktop.
A possible implementation of TestAssetBundle for Flutter tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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