fix issue 12999: Make assets available during tests (#31207)
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
@@ -16,6 +17,7 @@ import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_test/flutter_test.dart' show TestWindow;
|
||||
import 'package:quiver/testing/async.dart';
|
||||
import 'package:quiver/time.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:test_api/test_api.dart' as test_package;
|
||||
import 'package:stack_trace/stack_trace.dart' as stack_trace;
|
||||
import 'package:vector_math/vector_math_64.dart';
|
||||
@@ -707,6 +709,7 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
|
||||
super.initInstances();
|
||||
window.onBeginFrame = null;
|
||||
window.onDrawFrame = null;
|
||||
_mockFlutterAssets();
|
||||
}
|
||||
|
||||
FakeAsync _currentFakeAsync; // set in runTest; cleared in postTest
|
||||
@@ -736,6 +739,40 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
|
||||
@override
|
||||
int get microtaskCount => _currentFakeAsync.microtaskCount;
|
||||
|
||||
static Set<String> _allowedKeys;
|
||||
|
||||
void _mockFlutterAssets() {
|
||||
if (!Platform.environment.containsKey('UNIT_TEST_ASSETS')) {
|
||||
return;
|
||||
}
|
||||
final String assetFolderPath = Platform.environment['UNIT_TEST_ASSETS'];
|
||||
_ensureInitialized(assetFolderPath);
|
||||
BinaryMessages.setMockMessageHandler('flutter/assets', (ByteData message) {
|
||||
final String key = utf8.decode(message.buffer.asUint8List());
|
||||
if (_allowedKeys.contains(key)) {
|
||||
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 (_allowedKeys == null) {
|
||||
final File manifestFile = File(
|
||||
path.join(assetFolderPath, 'AssetManifest.json'));
|
||||
final Map<String, dynamic> manifest = json.decode(
|
||||
manifestFile.readAsStringSync());
|
||||
_allowedKeys = <String>{
|
||||
'AssetManifest.json',
|
||||
};
|
||||
for (List<dynamic> value in manifest.values) {
|
||||
final List<String> strList = List<String>.from(value);
|
||||
_allowedKeys.addAll(strList);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> pump([ Duration duration, EnginePhase newPhase = EnginePhase.sendSemanticsUpdate ]) {
|
||||
return TestAsyncUtils.guard<void>(() {
|
||||
|
||||
Reference in New Issue
Block a user