Add and use an integration test with native(ADB) screenshots (#152326)

Closes https://github.com/flutter/flutter/issues/152325.

This PR is large due to generate `flutter create --platforms android`. A quick summary:

- Moves the integration test from `packages/flutter_driver/test` to `dev/integration_tests`
- Created a sample Flutter app that draws a blue rectangle
- Forked a subset of `package:flutter_goldens` that will work on the standalone Dart VM
- Forked a subset of `goldens.dart` (from `flutter_test`) to `src/native/goldens.dart` (i.e. `matchesGoldenFile`)

This ... works locally, but as usual I have no idea if it will work on Skia Gold so let's roll some dice.
This commit is contained in:
Matan Lurey
2024-07-26 09:09:29 -07:00
committed by GitHub
parent 4b5d24e6a8
commit 7f8ad8a4cb
31 changed files with 932 additions and 20 deletions

View File

@@ -0,0 +1,4 @@
# In flutter/flutter, all screenshot tests are run on Skia Gold.
#
# However, local development might require running screenshot tests locally.
*.png

View File

@@ -0,0 +1,105 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/// A fork of `package:flutter_goldens/flutter_goldens.dart` without the
/// dependency on `package:flutter_test` or `package:flutter`; this allows
/// the library to be used in a standalone Dart VM context.
library;
import 'dart:async';
import 'dart:io' as io;
import 'dart:typed_data';
import 'package:file/local.dart';
import 'package:flutter_driver/src/native_driver.dart';
import 'package:flutter_goldens/skia_client.dart';
import 'package:path/path.dart' as path;
import 'package:platform/platform.dart';
import 'package:process/process.dart';
const LocalFileSystem _localFs = LocalFileSystem();
// TODO(matanlurey): Refactor flutter_goldens to just re-use that code instead.
Future<void> testExecutable(
FutureOr<void> Function() testMain, {
String? namePrefix,
}) async {
assert(
goldenFileComparator is NaiveLocalFileComparator,
'The flutter_goldens_fork library should be used from a *_test.dart file '
'where the "goldenFileComparator" has not yet been set. This is to ensure '
'that the correct comparator is used for the current test environment.',
);
final io.Directory tmpDir = io.Directory.systemTemp.createTempSync('android_driver_test');
goldenFileComparator = _GoldenFileComparator(
SkiaGoldClient(
_localFs.directory(tmpDir.path),
fs: _localFs,
process: const LocalProcessManager(),
platform: const LocalPlatform(),
httpClient: io.HttpClient(),
log: io.stderr.writeln,
),
namePrefix: namePrefix,
isPresubmit: false,
);
}
final class _GoldenFileComparator extends GoldenFileComparator {
_GoldenFileComparator(
this.skiaClient, {
required this.isPresubmit,
this.namePrefix,
Uri? baseDir,
}) : baseDir = baseDir ?? Uri.parse(path.dirname(io.Platform.script.path));
final Uri baseDir;
final SkiaGoldClient skiaClient;
final String? namePrefix;
final bool isPresubmit;
@override
Future<bool> compare(Uint8List imageBytes, Uri golden) async {
if (isPresubmit) {
await skiaClient.tryjobInit();
} else {
await skiaClient.imgtestInit();
}
golden = _addPrefix(golden);
await update(golden, imageBytes);
final io.File goldenFile = _getGoldenFile(golden);
if (isPresubmit) {
await skiaClient.tryjobAdd(golden.path, _localFs.file(goldenFile.path));
return true;
} else {
return skiaClient.imgtestAdd(golden.path, _localFs.file(goldenFile.path));
}
}
@override
Future<void> update(Uri golden, Uint8List imageBytes) async {
final io.File goldenFile = _getGoldenFile(golden);
await goldenFile.parent.create(recursive: true);
await goldenFile.writeAsBytes(imageBytes, flush: true);
}
io.File _getGoldenFile(Uri uri) {
return io.File.fromUri(baseDir.resolveUri(uri));
}
Uri _addPrefix(Uri golden) {
assert(
golden.toString().split('.').last == 'png',
'Golden files in the Flutter framework must end with the file extension '
'.png.',
);
return Uri.parse(<String>[
if (namePrefix != null) namePrefix!,
baseDir.pathSegments[baseDir.pathSegments.length - 2],
golden.toString(),
].join('.'));
}
}

View File

@@ -0,0 +1,43 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_driver/flutter_driver.dart';
import 'package:flutter_driver/src/native_driver.dart';
import 'package:test/test.dart';
import '_flutter_goldens_fork.dart';
// TODO(matanlurey): This is done automatically by 'flutter test' but not by
// 'flutter drive'. If we get closer to shipping the native 'flutter drive'
// command, we should look into if 'flutter_test_config.dart', or a similar
// mechanism, can be used to configure this automatically.
void main() async {
await testExecutable(_main);
}
Future<void> _main() async {
// To generate golden files locally, uncomment the following line.
// autoUpdateGoldenFiles = true;
late FlutterDriver flutterDriver;
late NativeDriver nativeDriver;
setUpAll(() async {
flutterDriver = await FlutterDriver.connect();
nativeDriver = await AndroidNativeDriver.connect();
});
tearDownAll(() async {
await nativeDriver.close();
await flutterDriver.close();
});
test('should screenshot and match a full-screen blue rectangle', () async {
await flutterDriver.waitFor(find.byType('DecoratedBox'));
await expectLater(
nativeDriver.screenshot(),
matchesGoldenFile('android_driver_test.BlueRectangle.png'),
);
}, timeout: Timeout.none);
}