Implement golden-file matching for integration_test on Android and iOS devices (#160484)

Work towards https://github.com/flutter/flutter/issues/143299.
Work towards https://github.com/flutter/flutter/issues/160043.

---

This PR implements, end-to-end, support for `matchesGoldenFile` when (a)
running with `package:integration_test` (b) on a device, such as an
Android emulator, Android device, iOS simulator, or iOS device, where
the _runner_ of a test file does not have process and local-file system
access.

There are multiple parts to this PR; I could make it smaller than 1K
lines, but the bulk of that is tests, and it would mean landing PRs that
are incomplete and unused, which does not seem useful - so instead here
is a quick overview of the PR's contents - questions/feedback welcome,
and I am willing to break code out or land incremental refactors if
requested.

1. Augmented `flutter_platform.dart` (used for iOS and Android), similar
to
[`flutter_web_platform.dart`](1398dc7eec/packages/flutter_tools/lib/src/test/flutter_web_platform.dart (L117-L128)),
now creates and uses
[`test_golden_comparator.dart`](https://github.com/flutter/flutter/blob/master/packages/flutter_tools/lib/src/test/test_golden_comparator.dart)
to proxy calls (coming from the VM service protocol) for golden-file
updates and comparisons to a `flutter_tester` process. A full
explanation of how (or why) it works this way is too hard to include
here, but see https://github.com/flutter/flutter/pull/160215 for more
details.
1. Added `VmServiceProxyGoldenFileComparator`, which is a currently
unused (outside of a single e2e test) comparator that forwards calls to
`compare` and `update` to the VM service protocol (of which, the other
side of this is implemented above, in `flutter_platform.dart`. The idea
is that this comparator would be used automatically when running in an
integration test on a device that requires it (similar to how web works
today), but that is **not** wired up yet and requires additional work in
`flutter_tools`.
1. Added two unit tests (of both the client and server), and a full
e2e-test using it to run `matchesGoldenFile`.
This commit is contained in:
Matan Lurey
2024-12-28 11:48:20 -08:00
committed by GitHub
parent b15625ca92
commit 4cd0e33013
9 changed files with 958 additions and 22 deletions

View File

@@ -151,19 +151,19 @@ Future<void> run(List<String> args) async {
}
// TODO(dnfield): This should be injected.
final BuildInfo buildInfo = BuildInfo(
BuildMode.debug,
'',
treeShakeIcons: false,
packageConfigPath: globals.fs.path.normalize(
globals.fs.path.absolute(argResults[_kOptionPackages] as String),
),
);
exitCode = await const FlutterTestRunner().runTests(
const TestWrapper(),
tests.keys.map(Uri.file).toList(),
debuggingOptions: DebuggingOptions.enabled(
BuildInfo(
BuildMode.debug,
'',
treeShakeIcons: false,
packageConfigPath: globals.fs.path.normalize(
globals.fs.path.absolute(argResults[_kOptionPackages] as String),
),
),
),
debuggingOptions: DebuggingOptions.enabled(buildInfo),
buildInfo: buildInfo,
watcher: collector,
enableVmService: collector != null,
precompiledDillFiles: tests,

View File

@@ -1008,7 +1008,7 @@ class DebuggingOptions {
startPaused = false,
dartFlags = '',
disableServiceAuthCodes = false,
enableDds = true,
enableDds = false,
cacheStartupProfile = false,
enableSoftwareRendering = false,
skiaDeterministicRendering = false,

View File

@@ -3,16 +3,20 @@
// found in the LICENSE file.
import 'dart:async';
import 'dart:typed_data';
import 'package:meta/meta.dart';
import 'package:package_config/package_config.dart';
import 'package:process/process.dart';
import 'package:stream_channel/stream_channel.dart';
import 'package:test_core/src/platform.dart'; // ignore: implementation_imports
import 'package:vm_service/vm_service.dart';
import '../base/async_guard.dart';
import '../base/common.dart';
import '../base/file_system.dart';
import '../base/io.dart';
import '../base/logger.dart';
import '../base/process.dart';
import '../build_info.dart';
import '../cache.dart';
@@ -32,6 +36,7 @@ import 'integration_test_device.dart';
import 'test_compiler.dart';
import 'test_config.dart';
import 'test_device.dart';
import 'test_golden_comparator.dart';
import 'test_time_recorder.dart';
import 'watcher.dart';
@@ -53,6 +58,10 @@ FlutterPlatform installHook({
TestWrapper testWrapper = const TestWrapper(),
required String shellPath,
required DebuggingOptions debuggingOptions,
required BuildInfo buildInfo,
required FileSystem fileSystem,
required Logger logger,
required ProcessManager processManager,
TestWatcher? watcher,
bool enableVmService = false,
bool machine = false,
@@ -69,7 +78,6 @@ FlutterPlatform installHook({
String? integrationTestUserIdentifier,
TestTimeRecorder? testTimeRecorder,
TestCompilerNativeAssetsBuilder? nativeAssetsBuilder,
BuildInfo? buildInfo,
}) {
assert(
enableVmService ||
@@ -101,6 +109,9 @@ FlutterPlatform installHook({
testTimeRecorder: testTimeRecorder,
nativeAssetsBuilder: nativeAssetsBuilder,
buildInfo: buildInfo,
fileSystem: fileSystem,
logger: logger,
processManager: processManager,
);
platformPluginRegistration(platform);
return platform;
@@ -289,6 +300,10 @@ class FlutterPlatform extends PlatformPlugin {
FlutterPlatform({
required this.shellPath,
required this.debuggingOptions,
required this.buildInfo,
required this.logger,
required FileSystem fileSystem,
required ProcessManager processManager,
this.watcher,
this.enableVmService,
this.machine,
@@ -304,9 +319,19 @@ class FlutterPlatform extends PlatformPlugin {
this.integrationTestUserIdentifier,
this.testTimeRecorder,
this.nativeAssetsBuilder,
this.buildInfo,
this.shutdownHooks,
});
}) {
_testGoldenComparator = TestGoldenComparator(
flutterTesterBinPath: shellPath,
compilerFactory:
() =>
compiler ??
TestCompiler(buildInfo, flutterProject, testTimeRecorder: testTimeRecorder),
fileSystem: fileSystem,
logger: logger,
processManager: processManager,
);
}
final String shellPath;
final DebuggingOptions debuggingOptions;
@@ -323,7 +348,8 @@ class FlutterPlatform extends PlatformPlugin {
final String? icudtlPath;
final TestTimeRecorder? testTimeRecorder;
final TestCompilerNativeAssetsBuilder? nativeAssetsBuilder;
final BuildInfo? buildInfo;
final BuildInfo buildInfo;
final Logger logger;
final ShutdownHooks? shutdownHooks;
/// The device to run the test on for Integration Tests.
@@ -332,6 +358,7 @@ class FlutterPlatform extends PlatformPlugin {
/// Tester; otherwise it will run as a Integration Test on this device.
final Device? integrationTestDevice;
bool get _isIntegrationTest => integrationTestDevice != null;
late final TestGoldenComparator _testGoldenComparator;
final String? integrationTestUserIdentifier;
@@ -375,7 +402,9 @@ class FlutterPlatform extends PlatformPlugin {
return controller.suite;
}
StreamChannel<dynamic> loadChannel(String path, SuitePlatform platform) {
/// Used as an implementation detail for [load]ing a test suite.
@visibleForTesting
StreamChannel<Object?> loadChannel(String path, SuitePlatform platform) {
if (_testCount > 0) {
// Fail if there will be a port conflict.
if (debuggingOptions.hostVmServicePort != null) {
@@ -481,15 +510,64 @@ class FlutterPlatform extends PlatformPlugin {
);
}
void _handleStartedDevice(Uri? uri, int testCount) {
void _handleStartedDevice({required Uri? uri, required int testCount, required String testPath}) {
if (uri != null) {
globals.printTrace('test $testCount: VM Service uri is available at $uri');
if (_isIntegrationTest) {
_listenToVmServiceForGoldens(uri: uri, testPath: testPath);
}
} else {
globals.printTrace('test $testCount: VM Service uri is not available');
}
watcher?.handleStartedDevice(uri);
}
static const String _kEventName = 'integration_test.VmServiceProxyGoldenFileComparator';
static const String _kExtension = 'ext.$_kEventName';
Future<void> _listenToVmServiceForGoldens({required Uri uri, required String testPath}) async {
final Uri goldensBaseUri = Uri.parse(testPath);
final FlutterVmService vmService = await connectToVmService(uri, logger: logger);
final IsolateRef testAppIsolate = await vmService.findExtensionIsolate(_kExtension);
await vmService.service.streamListen(_kEventName);
vmService.service.onEvent(_kEventName).listen((Event e) async {
if (!const <String>['compare', 'update'].contains(e.extensionKind)) {
throw StateError('Unexpected command: "${e.extensionKind}".');
}
final Map<String, Object?>? data = e.extensionData?.data;
if (data == null) {
throw StateError('Expected VM service data, but got null.');
}
final int id = data['id']! as int;
final Uri relativePath = Uri.parse(data['path']! as String);
final Uint8List bytes = base64.decode(data['bytes']! as String);
final Map<String, Object?> args;
if (e.extensionKind == 'update') {
switch (await _testGoldenComparator.update(goldensBaseUri, bytes, relativePath)) {
case TestGoldenUpdateDone():
args = <String, Object?>{'result': true};
case TestGoldenUpdateError(error: final String error):
args = <String, Object?>{'error': error};
}
} else {
switch (await _testGoldenComparator.compare(goldensBaseUri, bytes, relativePath)) {
case TestGoldenComparisonDone(matched: final bool matched):
args = <String, Object?>{'result': matched};
case TestGoldenComparisonError(error: final String error):
args = <String, Object?>{'error': error};
}
}
await vmService.callMethodWrapper(
_kExtension,
isolateId: testAppIsolate.id,
args: <String, Object?>{'id': id, ...args},
);
});
}
Future<_AsyncError?> _startTest(
String testPath,
StreamChannel<dynamic> testHarnessChannel,
@@ -625,7 +703,11 @@ class FlutterPlatform extends PlatformPlugin {
// This future may depend on [_handleStartedDevice] having been called
remoteChannelCompleter.future,
testDevice.vmServiceUri.then<void>((Uri? processVmServiceUri) {
_handleStartedDevice(processVmServiceUri, ourTestCount);
_handleStartedDevice(
uri: processVmServiceUri,
testCount: ourTestCount,
testPath: testPath,
);
}),
],
// If [remoteChannelCompleter.future] errors, we may never get the
@@ -727,8 +809,7 @@ class FlutterPlatform extends PlatformPlugin {
testUrl: testUrl,
testConfigFile: findTestConfigFile(globals.fs.file(testUrl), globals.logger),
// This MUST be a file URI.
packageConfigUri:
buildInfo != null ? globals.fs.path.toUri(buildInfo!.packageConfigPath) : null,
packageConfigUri: globals.fs.path.toUri(buildInfo.packageConfigPath),
host: host!,
updateGoldens: updateGoldens!,
flutterTestDep: packageConfig['flutter_test'] != null,

View File

@@ -63,7 +63,7 @@ abstract class FlutterTestRunner {
String? integrationTestUserIdentifier,
TestTimeRecorder? testTimeRecorder,
TestCompilerNativeAssetsBuilder? nativeAssetsBuilder,
BuildInfo? buildInfo,
required BuildInfo buildInfo,
});
/// Runs tests using the experimental strategy of spawning each test in a
@@ -130,7 +130,7 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner {
String? integrationTestUserIdentifier,
TestTimeRecorder? testTimeRecorder,
TestCompilerNativeAssetsBuilder? nativeAssetsBuilder,
BuildInfo? buildInfo,
required BuildInfo buildInfo,
}) async {
// Configure package:test to use the Flutter engine for child processes.
final String flutterTesterBinPath = globals.artifacts!.getArtifactPath(Artifact.flutterTester);
@@ -236,6 +236,9 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner {
testTimeRecorder: testTimeRecorder,
nativeAssetsBuilder: nativeAssetsBuilder,
buildInfo: buildInfo,
fileSystem: globals.fs,
logger: globals.logger,
processManager: globals.processManager,
);
try {

View File

@@ -2,8 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:io' as io;
import 'package:file/memory.dart';
import 'package:flutter_tools/src/application_package.dart';
import 'package:flutter_tools/src/artifacts.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
@@ -14,8 +18,12 @@ import 'package:flutter_tools/src/flutter_manifest.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
import 'package:flutter_tools/src/project.dart';
import 'package:flutter_tools/src/test/flutter_platform.dart';
import 'package:flutter_tools/src/test/test_compiler.dart';
import 'package:flutter_tools/src/vmservice.dart';
import 'package:stream_channel/stream_channel.dart';
import 'package:test/fake.dart';
import 'package:test_core/backend.dart';
import 'package:vm_service/src/vm_service.dart';
import '../src/common.dart';
import '../src/context.dart';
@@ -44,6 +52,10 @@ void main() {
shellPath: '/',
debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug, hostVmServicePort: 1234),
enableVmService: false,
buildInfo: BuildInfo.debug,
fileSystem: fileSystem,
processManager: FakeProcessManager.empty(),
logger: BufferLogger.test(),
);
flutterPlatform.loadChannel('test1.dart', fakeSuitePlatform);
@@ -67,6 +79,10 @@ void main() {
shellPath: '/',
precompiledDillPath: 'example.dill',
enableVmService: false,
buildInfo: BuildInfo.debug,
fileSystem: fileSystem,
processManager: FakeProcessManager.empty(),
logger: BufferLogger.test(),
);
flutterPlatform.loadChannel('test1.dart', fakeSuitePlatform);
@@ -93,6 +109,10 @@ void main() {
flutterProject: _FakeFlutterProject(),
host: InternetAddress.anyIPv4,
updateGoldens: false,
buildInfo: BuildInfo.debug,
fileSystem: fileSystem,
processManager: FakeProcessManager.empty(),
logger: BufferLogger.test(),
);
await expectLater(
@@ -133,6 +153,10 @@ void main() {
host: InternetAddress.anyIPv4,
updateGoldens: false,
shutdownHooks: shutdownHooks,
buildInfo: BuildInfo.debug,
fileSystem: fileSystem,
processManager: FakeProcessManager.empty(),
logger: BufferLogger.test(),
);
await expectLater(
@@ -156,6 +180,10 @@ void main() {
() => installHook(
shellPath: 'abc',
debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug, startPaused: true),
buildInfo: BuildInfo.debug,
fileSystem: fileSystem,
processManager: FakeProcessManager.empty(),
logger: BufferLogger.test(),
),
throwsAssertionError,
);
@@ -168,6 +196,10 @@ void main() {
startPaused: true,
hostVmServicePort: 123,
),
buildInfo: BuildInfo.debug,
fileSystem: fileSystem,
processManager: FakeProcessManager.empty(),
logger: BufferLogger.test(),
),
throwsAssertionError,
);
@@ -193,6 +225,10 @@ void main() {
platformPluginRegistration: (FlutterPlatform platform) {
capturedPlatform = platform;
},
buildInfo: BuildInfo.debug,
fileSystem: fileSystem,
processManager: FakeProcessManager.empty(),
logger: BufferLogger.test(),
);
expect(identical(capturedPlatform, flutterPlatform), equals(true));
@@ -247,6 +283,254 @@ void main() {
});
});
});
group('proxies goldenFileComparator using the VM service driver', () {
late SuitePlatform fakeSuitePlatform;
late MemoryFileSystem fileSystem;
late Artifacts artifacts;
late FlutterProject flutterProject;
late FakeProcessManager processManager;
late BufferLogger logger;
late TestCompiler testCompiler;
late _FakeFlutterVmService flutterVmService;
late Completer<void> testCompleter;
setUp(() {
fakeSuitePlatform = SuitePlatform(Runtime.vm);
fileSystem = MemoryFileSystem.test();
fileSystem.file('.dart_tool/package_config.json')
..createSync(recursive: true)
..writeAsStringSync('{"configVersion":2,"packages":[]}');
artifacts = Artifacts.test(fileSystem: fileSystem);
flutterProject = FlutterProject.fromDirectoryTest(fileSystem.systemTempDirectory);
testCompleter = Completer<void>();
processManager = FakeProcessManager.empty();
logger = BufferLogger.test();
testCompiler = _FakeTestCompiler();
flutterVmService = _FakeFlutterVmService();
});
tearDown(() {
printOnFailure(logger.errorText);
});
void addFlutterTesterDeviceExpectation() {
processManager.addCommand(
FakeCommand(
command: const <String>[
'flutter_tester',
'--disable-vm-service',
'--enable-checked-mode',
'--verify-entry-points',
'--enable-software-rendering',
'--skia-deterministic-rendering',
'--enable-dart-profiling',
'--non-interactive',
'--use-test-fonts',
'--disable-asset-fonts',
'--packages=.dart_tool/package_config.json',
'',
],
exitCode: -9,
completer: testCompleter,
),
);
}
testUsingContext(
'should not listen in a non-integration test',
() async {
addFlutterTesterDeviceExpectation();
const Device? notAnIntegrationTest = null;
final FlutterPlatform flutterPlatform = FlutterPlatform(
debuggingOptions: DebuggingOptions.disabled(BuildInfo.debug),
shellPath: 'flutter_tester',
enableVmService: false,
// ignore: avoid_redundant_argument_values
integrationTestDevice: notAnIntegrationTest,
flutterProject: flutterProject,
host: InternetAddress.anyIPv4,
updateGoldens: false,
buildInfo: BuildInfo.debug,
fileSystem: fileSystem,
processManager: processManager,
logger: BufferLogger.test(),
);
flutterPlatform.compiler = testCompiler;
// Simulate the test immediately completing.
testCompleter.complete();
final StreamChannel<Object?> channel = flutterPlatform.loadChannel(
'test1.dart',
fakeSuitePlatform,
);
// Without draining, the sink will never complete.
unawaited(channel.stream.drain<void>());
await expectLater(channel.sink.done, completes);
},
overrides: <Type, Generator>{
FileSystem: () => fileSystem,
ProcessManager: () => processManager,
Logger: () => logger,
VMServiceConnector: () => (_) => throw UnimplementedError(),
},
);
// This is not a complete test of all the possible cases supported by the
// golden-file integration, which is a complex multi-process implementation
// that lives across multiple packages and files.
//
// Instead, this is a unit-test based smoke test that the overall flow works
// as expected to give a quicker turn-around signal that either the test
// should be updated, or the process was broken; run an integration_test on
// an Android or iOS device or emulator/simulator that takes screenshots
// and compares them with matchesGoldenFile for a full e2e-test of the
// entire workflow.
testUsingContext(
'should listen in an integration test',
() async {
processManager.addCommand(
const FakeCommand(
command: <String>[
'flutter_tester',
'--disable-vm-service',
'--non-interactive',
'--packages=.dart_tool/package_config.json',
'',
],
stdout: '{"success": true}\n',
),
);
addFlutterTesterDeviceExpectation();
final FlutterPlatform flutterPlatform = FlutterPlatform(
debuggingOptions: DebuggingOptions.disabled(BuildInfo.debug),
shellPath: 'flutter_tester',
enableVmService: false,
flutterProject: flutterProject,
integrationTestDevice: _WorkingDevice(),
host: InternetAddress.anyIPv4,
updateGoldens: false,
buildInfo: BuildInfo.debug,
fileSystem: fileSystem,
processManager: processManager,
logger: BufferLogger.test(),
);
flutterPlatform.compiler = testCompiler;
final StreamChannel<Object?> channel = flutterPlatform.loadChannel(
'test1.dart',
fakeSuitePlatform,
);
// Responds to update events.
flutterVmService.service.onExtensionEventController.add(
Event(
extensionData: ExtensionData.parse(<String, Object?>{
'id': 1,
'path': 'foo',
'bytes': '',
}),
extensionKind: 'update',
),
);
// Wait for tiny async tasks to complete.
await pumpEventQueue();
await flutterVmService.service.onExtensionEventController.close();
final (String event, String? isolateId, Map<String, Object?>? data) =
flutterVmService.callMethodWrapperInvocation!;
expect(event, 'ext.integration_test.VmServiceProxyGoldenFileComparator');
expect(isolateId, null);
expect(data, <String, Object?>{'id': 1, 'result': true});
// Without draining, the sink will never complete.
unawaited(channel.stream.drain<void>());
// Allow the test to finish.
testCompleter.complete();
await expectLater(channel.sink.done, completes);
},
overrides: <Type, Generator>{
FileSystem: () => fileSystem,
ProcessManager: () => processManager,
Logger: () => logger,
VMServiceConnector:
() =>
(
Uri httpUri, {
ReloadSources? reloadSources,
Restart? restart,
CompileExpression? compileExpression,
GetSkSLMethod? getSkSLMethod,
FlutterProject? flutterProject,
PrintStructuredErrorLogMethod? printStructuredErrorLogMethod,
io.CompressionOptions? compression,
Device? device,
Logger? logger,
}) async => flutterVmService,
ApplicationPackageFactory: _FakeApplicationPackageFactory.new,
Artifacts: () => artifacts,
},
);
});
}
class _FakeFlutterVmService extends Fake implements FlutterVmService {
@override
Future<IsolateRef> findExtensionIsolate(String extensionName) async {
return IsolateRef();
}
@override
final _FakeVmService service = _FakeVmService();
(String, String?, Map<String, Object?>?)? callMethodWrapperInvocation;
@override
Future<Response?> callMethodWrapper(
String method, {
String? isolateId,
Map<String, Object?>? args,
}) async {
callMethodWrapperInvocation = (method, isolateId, args);
return Response();
}
}
class _FakeVmService extends Fake implements VmService {
String? lastStreamListenId;
@override
Future<Success> streamListen(String streamId) async {
lastStreamListenId = streamId;
return Success();
}
final StreamController<Event> onExtensionEventController = StreamController<Event>();
@override
Stream<Event> get onExtensionEvent => const Stream<Event>.empty();
@override
Stream<Event> onEvent(String streamId) {
return onExtensionEventController.stream;
}
@override
Future<void> get onDone => onExtensionEventController.done;
}
class _FakeTestCompiler extends Fake implements TestCompiler {
@override
Future<String?> compile(Uri mainDart) async => '';
}
class _UnstartableDevice extends Fake implements Device {