Skip to content

Commit be9b204

Browse files
authored
Android_hardware_smoke_test: clean up golden copy in CI (#189948)
In PR #189390 (part of issue #187538) we configured some new CI test shards which run the instrumented tests subsequently after the driver tests. As part of that, we had to copy the goldens to the asset directory so they would be bundled into the instrumented test APK. This PR proposes to skip the copy by configuring the skia gold comparator to write directly to the desired directory, via a new optional `localOutputDir` parameter. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 9988960 commit be9b204

3 files changed

Lines changed: 86 additions & 52 deletions

File tree

‎dev/bots/suite_runners/run_android_hardware_smoke_tests.dart‎

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:async';
66
import 'dart:io' show Platform;
7+
78
import 'package:file/file.dart';
89
import 'package:file/local.dart';
910
import 'package:path/path.dart' as path;
@@ -15,18 +16,6 @@ import 'run_android_engine_tests.dart';
1516
String _impellerBackendMetadata({required String value}) =>
1617
'<meta-data android:name="io.flutter.embedding.android.ImpellerBackend" android:value="$value" />';
1718

18-
void _copyDirectory(Directory source, Directory destination) {
19-
destination.createSync(recursive: true);
20-
for (final FileSystemEntity entity in source.listSync(recursive: true)) {
21-
if (entity is File) {
22-
final String relativePath = path.relative(entity.path, from: source.path);
23-
final String destPath = path.join(destination.path, relativePath);
24-
entity.fileSystem.file(destPath).parent.createSync(recursive: true);
25-
entity.copySync(destPath);
26-
}
27-
}
28-
}
29-
3019
void _cleanGoldensDirectory(Directory directory) {
3120
if (!directory.existsSync()) {
3221
return;
@@ -64,9 +53,6 @@ Future<void> runAndroidHardwareSmokeTests({
6453
final Directory destinationDir = const LocalFileSystem().directory(
6554
path.join(testDir, 'test_driver', 'goldens'),
6655
);
67-
final Directory sourceDir = const LocalFileSystem().directory(
68-
path.join(testDir, 'android_hardware_smoke_test.${backend.name}.goldens'),
69-
);
7056

7157
try {
7258
// Replace whatever the current backend is with the specified backend.
@@ -93,13 +79,6 @@ Future<void> runAndroidHardwareSmokeTests({
9379
], workingDirectory: testDir);
9480

9581
if (runInstrumented) {
96-
// 2. Copy the generated goldens to the assets directory so they get packaged with the APK.
97-
// In CI, the Skia Gold comparator downloads the baseline images into a temporary prefixed
98-
// directory (sourceDir) instead of the default assets directory (destinationDir).
99-
if (sourceDir.existsSync()) {
100-
_copyDirectory(sourceDir, destinationDir);
101-
}
102-
10382
final String gradle = path.absolute(
10483
path.join(androidDir, Platform.isWindows ? 'gradlew.bat' : 'gradlew'),
10584
);
@@ -117,10 +96,5 @@ Future<void> runAndroidHardwareSmokeTests({
11796

11897
// Clean up copied goldens to keep Git worktree completely clean
11998
_cleanGoldensDirectory(destinationDir);
120-
121-
// Clean up the temporary prefixed goldens directory
122-
if (sourceDir.existsSync()) {
123-
sourceDir.deleteSync(recursive: true);
124-
}
12599
}
126100
}

‎dev/integration_tests/android_hardware_smoke_test/test_driver/driver_test.dart‎

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,20 @@ void main() async {
2929
final String response = await flutterDriver.requestData(
3030
json.encode(<String, Object?>{keyCommand: commandGetGoldenVariant}),
3131
);
32-
final Map<String, Object?> reply = (json.decode(response) as Map<Object?, Object?>)
33-
.cast<String, Object?>();
32+
final Map<String, Object?> reply =
33+
(json.decode(response) as Map<Object?, Object?>)
34+
.cast<String, Object?>();
3435
final replyVariant = reply[keyGoldenVariant] as String?;
35-
activeGoldenVariant = (replyVariant != null && replyVariant.isNotEmpty) ? '.$replyVariant' : '';
36+
activeGoldenVariant = switch (replyVariant) {
37+
final String s when s.isNotEmpty => '.$s',
38+
_ => '',
39+
};
3640

3741
if (isLuci) {
38-
await enableSkiaGoldComparator(namePrefix: 'android_hardware_smoke_test$activeGoldenVariant');
42+
await enableSkiaGoldComparator(
43+
namePrefix: 'android_hardware_smoke_test$activeGoldenVariant',
44+
localOutputDir: 'goldens',
45+
);
3946
}
4047
});
4148

@@ -47,12 +54,16 @@ void main() async {
4754
Future<void> templateTest(String testName) async {
4855
// Ask the app to render the test and return the rendered image bytes
4956
final String response = await flutterDriver.requestData(
50-
json.encode(<String, Object?>{keyTestName: testName, keyPerformAppSideGoldenCompare: false}),
57+
json.encode(<String, Object?>{
58+
keyTestName: testName,
59+
keyPerformAppSideGoldenCompare: false,
60+
}),
5161
);
5262

5363
// Expect a successful reply or skip status
54-
final Map<String, Object?> reply = (json.decode(response) as Map<Object?, Object?>)
55-
.cast<String, Object?>();
64+
final Map<String, Object?> reply =
65+
(json.decode(response) as Map<Object?, Object?>)
66+
.cast<String, Object?>();
5667

5768
if (reply[keyMessage] == 'Skipped') {
5869
markTestSkipped('Skipping $testName: ${reply[keyReason]}');
@@ -74,22 +85,38 @@ void main() async {
7485

7586
final img.Image? decoded = img.decodePng(fullBytes);
7687
if (decoded == null) {
77-
throw StateError('Failed to decode full screen screenshot for $testName');
88+
throw StateError(
89+
'Failed to decode full screen screenshot for $testName',
90+
);
7891
}
79-
if (x < 0 || y < 0 || w <= 0 || h <= 0 || x + w > decoded.width || y + h > decoded.height) {
92+
if (x < 0 ||
93+
y < 0 ||
94+
w <= 0 ||
95+
h <= 0 ||
96+
x + w > decoded.width ||
97+
y + h > decoded.height) {
8098
throw StateError(
8199
'Crop bounds out of range for $testName: x=$x, y=$y, w=$w, h=$h, image.width=${decoded.width}, image.height=${decoded.height}',
82100
);
83101
}
84-
final img.Image cropped = img.copyCrop(decoded, x: x, y: y, width: w, height: h);
102+
final img.Image cropped = img.copyCrop(
103+
decoded,
104+
x: x,
105+
y: y,
106+
width: w,
107+
height: h,
108+
);
85109
imageBytes = Uint8List.fromList(img.encodePng(cropped));
86110
} else {
87111
final imageBase64 = reply[keyImageBytes]! as String;
88112
imageBytes = base64.decode(imageBase64);
89113
}
90114

91115
// Compare the bytes to a golden file on the host filesystem using the cached variant
92-
await expectLater(imageBytes, matchesGoldenFile('goldens/$testName$activeGoldenVariant.png'));
116+
await expectLater(
117+
imageBytes,
118+
matchesGoldenFile('goldens/$testName$activeGoldenVariant.png'),
119+
);
93120
}
94121

95122
test('should render and match blueRectangleTest golden', () async {
@@ -116,15 +143,27 @@ void main() async {
116143
await templateTest('backdropFilterBlurTest');
117144
}, timeout: Timeout.none);
118145

119-
test('should render and match $kPlatformViewTextureLayerTest golden', () async {
120-
await templateTest(kPlatformViewTextureLayerTest);
121-
}, timeout: Timeout.none);
122-
123-
test('should render and match $kPlatformViewHybridCompositionTest golden', () async {
124-
await templateTest(kPlatformViewHybridCompositionTest);
125-
}, timeout: Timeout.none);
126-
127-
test('should render and match $kPlatformViewHybridCompositionPlusPlusTest golden', () async {
128-
await templateTest(kPlatformViewHybridCompositionPlusPlusTest);
129-
}, timeout: Timeout.none);
146+
test(
147+
'should render and match $kPlatformViewTextureLayerTest golden',
148+
() async {
149+
await templateTest(kPlatformViewTextureLayerTest);
150+
},
151+
timeout: Timeout.none,
152+
);
153+
154+
test(
155+
'should render and match $kPlatformViewHybridCompositionTest golden',
156+
() async {
157+
await templateTest(kPlatformViewHybridCompositionTest);
158+
},
159+
timeout: Timeout.none,
160+
);
161+
162+
test(
163+
'should render and match $kPlatformViewHybridCompositionPlusPlusTest golden',
164+
() async {
165+
await templateTest(kPlatformViewHybridCompositionPlusPlusTest);
166+
},
167+
timeout: Timeout.none,
168+
);
130169
}

‎dev/tools/android_driver_extensions/lib/skia_gold.dart‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ const String _kGoldctlPresubmitKey = 'GOLD_TRYJOB';
3535
/// for more information.
3636
///
3737
/// May optionally provide a [namePrefix] to be used when uploading images.
38-
Future<void> enableSkiaGoldComparator({String? namePrefix}) async {
38+
///
39+
/// If [localOutputDir] is provided, local screenshots downloaded from Skia Gold
40+
/// will be stored flat in this directory relative to the script execution path
41+
/// (nested subdirectories in golden file URIs are not currently supported).
42+
Future<void> enableSkiaGoldComparator({String? namePrefix, String? localOutputDir}) async {
3943
assert(
4044
goldenFileComparator is NaiveLocalFileComparator,
4145
'The flutter_goldens_fork library should be used from a *_test.dart file '
@@ -75,31 +79,44 @@ Future<void> enableSkiaGoldComparator({String? namePrefix}) async {
7579
skiaGoldClient,
7680
namePrefix: namePrefix,
7781
presubmit: isPresubmit,
82+
localOutputDir: localOutputDir,
7883
);
7984
}
8085

8186
/// Configures [goldenFileComparator] to use Skia Gold (for unit testing).
87+
///
88+
/// If [localOutputDir] is provided, local screenshots downloaded from Skia Gold
89+
/// will be stored flat in this directory relative to the script execution path
90+
/// (nested subdirectories in golden file URIs are not currently supported).
8291
@visibleForTesting
8392
Future<void> enableSkiaGoldComparatorForTesting(
8493
SkiaGoldClient skiaGoldClient, {
8594
required bool presubmit,
8695
String? namePrefix,
96+
String? localOutputDir,
8797
}) async {
8898
await skiaGoldClient.auth();
8999
goldenFileComparator = _SkiaGoldComparator(
90100
skiaGoldClient,
91101
namePrefix: namePrefix,
92102
isPresubmit: presubmit,
103+
localOutputDir: localOutputDir,
93104
);
94105
}
95106

96107
final class _SkiaGoldComparator extends GoldenFileComparator {
97-
_SkiaGoldComparator(this.skiaClient, {required this.isPresubmit, this.namePrefix, Uri? baseDir})
98-
: baseDir = baseDir ?? Uri.parse(path.dirname(io.Platform.script.path));
108+
_SkiaGoldComparator(
109+
this.skiaClient, {
110+
required this.isPresubmit,
111+
this.namePrefix,
112+
this.localOutputDir,
113+
Uri? baseDir,
114+
}) : baseDir = baseDir ?? Uri.directory(path.dirname(io.Platform.script.toFilePath()));
99115

100116
final Uri baseDir;
101117
final SkiaGoldClient skiaClient;
102118
final String? namePrefix;
119+
final String? localOutputDir;
103120
final bool isPresubmit;
104121

105122
@override
@@ -149,6 +166,10 @@ final class _SkiaGoldComparator extends GoldenFileComparator {
149166
}
150167

151168
io.File _getGoldenFile(Uri uri) {
169+
if (localOutputDir != null) {
170+
final String fileName = uri.pathSegments.last;
171+
return io.File(path.join(baseDir.toFilePath(), localOutputDir, fileName));
172+
}
152173
return io.File.fromUri(baseDir.resolveUri(uri));
153174
}
154175

0 commit comments

Comments
 (0)