Skip to content

Commit

Permalink
fix issue 12999: Make assets available during tests (flutter#31207)
Browse files Browse the repository at this point in the history
  • Loading branch information
chunhtai authored Apr 18, 2019
1 parent e69a8a1 commit b275e11
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
37 changes: 37 additions & 0 deletions packages/flutter_test/lib/src/binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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>(() {
Expand Down
26 changes: 24 additions & 2 deletions packages/flutter_tools/lib/src/commands/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import 'dart:async';
import 'dart:math' as math;

import '../asset.dart';
import '../base/common.dart';
import '../base/file_system.dart';
import '../base/platform.dart';
import '../bundle.dart';
import '../cache.dart';
import '../codegen.dart';
import '../dart/pub.dart';
Expand All @@ -18,7 +20,6 @@ import '../test/coverage_collector.dart';
import '../test/event_printer.dart';
import '../test/runner.dart';
import '../test/watcher.dart';

class TestCommand extends FastFlutterCommand {
TestCommand({ bool verboseHelp = false }) {
requiresPubspecYaml();
Expand Down Expand Up @@ -83,7 +84,14 @@ class TestCommand extends FastFlutterCommand {
abbr: 'j',
defaultsTo: math.max<int>(1, platform.numberOfProcessors - 2).toString(),
help: 'The number of concurrent test processes to run.',
valueHelp: 'jobs');
valueHelp: 'jobs'
)
..addFlag('test-assets',
defaultsTo: true,
negatable: true,
help: 'Whether to build the assets bundle for testing.\n'
'Consider using --no-test-assets if assets are not required.',
);
}

@override
Expand All @@ -110,6 +118,10 @@ class TestCommand extends FastFlutterCommand {
if (shouldRunPub) {
await pubGet(context: PubContext.getVerifyContext(name), skipPubspecYamlCheck: true);
}
final bool buildTestAssets = argResults['test-assets'];
if (buildTestAssets) {
await _buildTestAsset();
}
final List<String> names = argResults['name'];
final List<String> plainNames = argResults['plain-name'];
final FlutterProject flutterProject = await FlutterProject.current();
Expand Down Expand Up @@ -195,6 +207,7 @@ class TestCommand extends FastFlutterCommand {
trackWidgetCreation: argResults['track-widget-creation'],
updateGoldens: argResults['update-goldens'],
concurrency: jobs,
buildTestAssets: buildTestAssets,
flutterProject: flutterProject,
);

Expand All @@ -208,6 +221,15 @@ class TestCommand extends FastFlutterCommand {
throwToolExit(null);
return const FlutterCommandResult(ExitStatus.success);
}

Future<void> _buildTestAsset() async {
final AssetBundle assetBundle = AssetBundleFactory.instance.createBundle();
final int build = await assetBundle.build();
if (build != 0) {
throwToolExit('Error: Failed to build asset bundle');
}
await writeBundle(fs.directory(fs.path.join('build', 'unit_test_assets')), assetBundle.entries);
}
}

Iterable<String> _findTests(Directory directory) {
Expand Down
8 changes: 8 additions & 0 deletions packages/flutter_tools/lib/src/test/flutter_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ void installHook({
Map<String, String> precompiledDillFiles,
bool trackWidgetCreation = false,
bool updateGoldens = false,
bool buildTestAssets = false,
int observatoryPort,
InternetAddressType serverType = InternetAddressType.IPv4,
Uri projectRootDirectory,
Expand All @@ -110,6 +111,7 @@ void installHook({
precompiledDillFiles: precompiledDillFiles,
trackWidgetCreation: trackWidgetCreation,
updateGoldens: updateGoldens,
buildTestAssets: buildTestAssets,
projectRootDirectory: projectRootDirectory,
flutterProject: flutterProject,
icudtlPath: icudtlPath,
Expand Down Expand Up @@ -390,6 +392,7 @@ class _FlutterPlatform extends PlatformPlugin {
this.precompiledDillFiles,
this.trackWidgetCreation,
this.updateGoldens,
this.buildTestAssets,
this.projectRootDirectory,
this.flutterProject,
this.icudtlPath,
Expand All @@ -407,6 +410,7 @@ class _FlutterPlatform extends PlatformPlugin {
final Map<String, String> precompiledDillFiles;
final bool trackWidgetCreation;
final bool updateGoldens;
final bool buildTestAssets;
final Uri projectRootDirectory;
final FlutterProject flutterProject;
final String icudtlPath;
Expand Down Expand Up @@ -977,6 +981,10 @@ class _FlutterPlatform extends PlatformPlugin {
'FONTCONFIG_FILE': _fontConfigFile.path,
'SERVER_PORT': serverPort.toString(),
};
if (buildTestAssets) {
environment['UNIT_TEST_ASSETS'] = fs.path.join(
flutterProject.directory.path, 'build', 'unit_test_assets');
}
return processManager.start(command, environment: environment);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/flutter_tools/lib/src/test/runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Future<int> runTests(
bool updateGoldens = false,
TestWatcher watcher,
@required int concurrency,
bool buildTestAssets = false,
FlutterProject flutterProject,
String icudtlPath,
Directory coverageDirectory,
Expand Down Expand Up @@ -83,6 +84,7 @@ Future<int> runTests(
precompiledDillFiles: precompiledDillFiles,
trackWidgetCreation: trackWidgetCreation,
updateGoldens: updateGoldens,
buildTestAssets: buildTestAssets,
projectRootDirectory: fs.currentDirectory.uri,
flutterProject: flutterProject,
icudtlPath: icudtlPath,
Expand Down

0 comments on commit b275e11

Please sign in to comment.