Skip to content

Commit f2e59e7

Browse files
authored
Proposal to deprecate webGoldenComparator. (#161196)
Towards flutter/flutter#160261. This is a PR and proposal all-in-one to deprecate, and remove, pending the removal of the HTML backend, `WebGoldenComparator` (and related symbols, `webGoldenComparator` and `DefaultWebGoldenComparator`). The concept of `WebGoldenComparator` was added because the HTML-based renderer could not support the `GoldenFileComparator` contract: https://github.com/flutter/flutter/blob/7141c2a136e21330e385f4151fdb644e63c2740b/packages/flutter_test/lib/src/_matchers_web.dart#L105-L108 Once the Skia renderer (and now, SkWasm renderer) were added, it was now possible to support the same API used by the native engine(s). This PR conditionally, if _not_ using the HTML renderer, uses `goldenFileComparator` instead, which re-uses the same code that previously backed `DefaultWebGoldenFileComparator`. No _new_ logic has been introduced in this change. This might need an iteration or two to get right. Feedback welcome!
1 parent 3d433ad commit f2e59e7

File tree

5 files changed

+80
-35
lines changed

5 files changed

+80
-35
lines changed

packages/flutter_test/lib/src/_goldens_io.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,16 @@ ByteData _invert(ByteData imageBytes) {
284284
}
285285

286286
/// An unsupported [WebGoldenComparator] that exists for API compatibility.
287+
@Deprecated(
288+
'Use GoldenFileComparator instead. '
289+
'This feature was deprecated after v3.28.0-0.1.pre.',
290+
)
287291
class DefaultWebGoldenComparator extends WebGoldenComparator {
288292
/// This is provided to prevent warnings from the analyzer.
293+
@Deprecated(
294+
'Use a GoldenFileComparator implementation instead. '
295+
'This feature was deprecated after v3.28.0-0.1.pre.',
296+
)
289297
DefaultWebGoldenComparator(Uri _) {
290298
throw UnsupportedError('DefaultWebGoldenComparator is only supported on the web.');
291299
}

packages/flutter_test/lib/src/_goldens_web.dart

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,19 @@ Future<ComparisonResult> compareLists(List<int> test, List<int> master) async {
3535
throw UnsupportedError('Golden testing is not supported on the web.');
3636
}
3737

38-
/// The default [WebGoldenComparator] implementation for `flutter test`.
39-
///
40-
/// This comparator will send a request to the test server for golden comparison
41-
/// which will then defer the comparison to [goldenFileComparator].
42-
///
43-
/// See also:
44-
///
45-
/// * [matchesGoldenFile], the function that invokes the comparator.
46-
class DefaultWebGoldenComparator extends WebGoldenComparator {
47-
/// Creates a new [DefaultWebGoldenComparator] for the specified [testUri].
48-
///
49-
/// Golden file keys will be interpreted as file paths relative to the
50-
/// directory in which [testUri] resides.
51-
///
52-
/// The [testUri] must represent a file.
53-
DefaultWebGoldenComparator(this.testUri);
54-
55-
/// The test file currently being executed.
38+
/// Implements [GoldenFileComparator] by proxying calls to an HTTP service `/flutter_goldens`.
39+
final class HttpProxyGoldenComparator extends GoldenFileComparator {
40+
/// Creates a comparator with the given test file being executed.
5641
///
5742
/// Golden file keys will be interpreted as file paths relative to the
5843
/// directory in which this file resides.
59-
Uri testUri;
44+
HttpProxyGoldenComparator(this._testUri);
45+
final Uri _testUri;
6046

6147
@override
62-
Future<bool> compare(double width, double height, Uri golden) async {
48+
Future<bool> compare(Uint8List bytes, Uri golden) async {
6349
final String key = golden.toString();
50+
final String bytesEncoded = base64.encode(bytes);
6451
final web.Response response =
6552
await web.window
6653
.fetch(
@@ -69,10 +56,9 @@ class DefaultWebGoldenComparator extends WebGoldenComparator {
6956
method: 'POST',
7057
body:
7158
json.encode(<String, Object>{
72-
'testUri': testUri.toString(),
59+
'testUri': _testUri.toString(),
7360
'key': key,
74-
'width': width.round(),
75-
'height': height.round(),
61+
'bytes': bytesEncoded,
7662
}).toJS,
7763
),
7864
)
@@ -85,15 +71,43 @@ class DefaultWebGoldenComparator extends WebGoldenComparator {
8571
}
8672

8773
@override
88-
Future<void> update(double width, double height, Uri golden) async {
74+
Future<void> update(Uri golden, Uint8List bytes) async {
8975
// Update is handled on the server side, just use the same logic here
90-
await compare(width, height, golden);
76+
await compare(bytes, golden);
9177
}
78+
}
79+
80+
/// The default [WebGoldenComparator] implementation for `flutter test`.
81+
///
82+
/// This comparator will send a request to the test server for golden comparison
83+
/// which will then defer the comparison to [goldenFileComparator].
84+
///
85+
/// See also:
86+
///
87+
/// * [matchesGoldenFile], the function that invokes the comparator.
88+
@Deprecated(
89+
'Use goldenFileComparator instead. '
90+
'This feature was deprecated after v3.28.0-0.1.pre.',
91+
)
92+
class DefaultWebGoldenComparator extends WebGoldenComparator {
93+
/// Creates a new [DefaultWebGoldenComparator] for the specified [testUri].
94+
///
95+
/// Golden file keys will be interpreted as file paths relative to the
96+
/// directory in which [testUri] resides.
97+
///
98+
/// The [testUri] must represent a file.
99+
@Deprecated(
100+
'Use an implementation of GoldenFileComparator instead. '
101+
'This feature was deprecated after v3.28.0-0.1.pre.',
102+
)
103+
DefaultWebGoldenComparator(Uri testUri) : _comparatorImpl = HttpProxyGoldenComparator(testUri);
104+
105+
// TODO(matanlurey): Refactor as part of https://github.com/flutter/flutter/issues/160261.
106+
final HttpProxyGoldenComparator _comparatorImpl;
92107

93108
@override
94-
Future<bool> compareBytes(Uint8List bytes, Uri golden) async {
109+
Future<bool> compare(double width, double height, Uri golden) async {
95110
final String key = golden.toString();
96-
final String bytesEncoded = base64.encode(bytes);
97111
final web.Response response =
98112
await web.window
99113
.fetch(
@@ -102,9 +116,10 @@ class DefaultWebGoldenComparator extends WebGoldenComparator {
102116
method: 'POST',
103117
body:
104118
json.encode(<String, Object>{
105-
'testUri': testUri.toString(),
119+
'testUri': _comparatorImpl._testUri.toString(),
106120
'key': key,
107-
'bytes': bytesEncoded,
121+
'width': width.round(),
122+
'height': height.round(),
108123
}).toJS,
109124
),
110125
)
@@ -117,8 +132,18 @@ class DefaultWebGoldenComparator extends WebGoldenComparator {
117132
}
118133

119134
@override
120-
Future<void> updateBytes(Uint8List bytes, Uri golden) async {
135+
Future<void> update(double width, double height, Uri golden) async {
121136
// Update is handled on the server side, just use the same logic here
122-
await compareBytes(bytes, golden);
137+
await compare(width, height, golden);
138+
}
139+
140+
@override
141+
Future<bool> compareBytes(Uint8List bytes, Uri golden) async {
142+
return _comparatorImpl.compare(bytes, golden);
143+
}
144+
145+
@override
146+
Future<void> updateBytes(Uint8List bytes, Uri golden) async {
147+
await _comparatorImpl.update(golden, bytes);
123148
}
124149
}

packages/flutter_test/lib/src/_matchers_web.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ class MatchesGoldenFile extends AsyncMatcher {
8585
return 'could not encode screenshot.';
8686
}
8787
if (autoUpdateGoldenFiles) {
88-
await webGoldenComparator.updateBytes(bytes.buffer.asUint8List(), key);
88+
await goldenFileComparator.update(key, bytes.buffer.asUint8List());
8989
return null;
9090
}
9191
try {
92-
final bool success = await webGoldenComparator.compareBytes(
92+
final bool success = await goldenFileComparator.compare(
9393
bytes.buffer.asUint8List(),
9494
key,
9595
);
@@ -126,7 +126,7 @@ class MatchesGoldenFile extends AsyncMatcher {
126126

127127
@override
128128
Description describe(Description description) {
129-
final Uri testNameUri = webGoldenComparator.getTestUri(key, version);
129+
final Uri testNameUri = goldenFileComparator.getTestUri(key, version);
130130
return description.add('one widget whose rasterized image matches golden image "$testNameUri"');
131131
}
132132
}

packages/flutter_test/lib/src/_test_selector_web.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:js_interop';
77
import 'dart:ui' as ui;
88
import 'dart:ui_web' as ui_web;
99

10+
import 'package:flutter/foundation.dart' show isSkiaWeb;
1011
import 'package:stream_channel/stream_channel.dart';
1112
import 'package:test_api/backend.dart';
1213

@@ -41,7 +42,14 @@ Future<void> runWebTest(WebTest test) async {
4142
final Completer<void> completer = Completer<void>();
4243
await ui_web.bootstrapEngine(runApp: () => completer.complete());
4344
await completer.future;
44-
webGoldenComparator = DefaultWebGoldenComparator(test.goldensUri);
45+
46+
// TODO(matanlurey): Remove webGoldenComparator when dart:html is deprecated.
47+
// See https://github.com/flutter/flutter/issues/145954.
48+
if (isSkiaWeb) {
49+
goldenFileComparator = HttpProxyGoldenComparator(test.goldensUri);
50+
} else {
51+
webGoldenComparator = DefaultWebGoldenComparator(test.goldensUri);
52+
}
4553

4654
/// This hard-codes the device pixel ratio to 3.0 and a 2400 x 1800 window
4755
/// size for the purposes of testing.

packages/flutter_test/lib/src/goldens.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ GoldenFileComparator goldenFileComparator = const TrivialComparator._();
216216
/// * [DefaultWebGoldenComparator] for the default [WebGoldenComparator]
217217
/// implementation for `flutter test`.
218218
/// * [matchesGoldenFile], the function that invokes the comparator.
219+
@Deprecated(
220+
'Use GoldenFileComparator instead. '
221+
'This feature was deprecated after v3.28.0-0.1.pre.',
222+
)
219223
abstract class WebGoldenComparator {
220224
/// Compares the rendered pixels of size [width]x[height] that is being
221225
/// rendered on the top left of the screen against the golden file identified

0 commit comments

Comments
 (0)