mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Remove WebGoldenComparator, which is no longer used by the tool. (#169898)
I could see the argument in keeping this around longer for API compatibility, but we no longer use it, it's no longer tested, and only really existed for the HTML-backend, which was removed, so I'm saying remove it. Closes https://github.com/flutter/flutter/issues/160261.
This commit is contained in:
parent
d20f8b080b
commit
d810bad840
@ -285,42 +285,6 @@ ByteData _invert(ByteData imageBytes) {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/// An unsupported [WebGoldenComparator] that exists for API compatibility.
|
||||
@Deprecated(
|
||||
'Use GoldenFileComparator instead. '
|
||||
'This feature was deprecated after v3.28.0-0.1.pre.',
|
||||
)
|
||||
class DefaultWebGoldenComparator extends WebGoldenComparator {
|
||||
/// This is provided to prevent warnings from the analyzer.
|
||||
@Deprecated(
|
||||
'Use a GoldenFileComparator implementation instead. '
|
||||
'This feature was deprecated after v3.28.0-0.1.pre.',
|
||||
)
|
||||
DefaultWebGoldenComparator(Uri _) {
|
||||
throw UnsupportedError('DefaultWebGoldenComparator is only supported on the web.');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> compare(double width, double height, Uri golden) {
|
||||
throw UnsupportedError('DefaultWebGoldenComparator is only supported on the web.');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> update(double width, double height, Uri golden) {
|
||||
throw UnsupportedError('DefaultWebGoldenComparator is only supported on the web.');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> compareBytes(Uint8List bytes, Uri golden) {
|
||||
throw UnsupportedError('DefaultWebGoldenComparator is only supported on the web.');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateBytes(Uint8List bytes, Uri golden) {
|
||||
throw UnsupportedError('DefaultWebGoldenComparator is only supported on the web.');
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads the red value out of a 32 bit rgba pixel.
|
||||
int _readRed(int pixel) => (pixel >> 24) & 0xff;
|
||||
|
||||
|
||||
@ -76,74 +76,3 @@ final class HttpProxyGoldenComparator extends GoldenFileComparator {
|
||||
await compare(bytes, golden);
|
||||
}
|
||||
}
|
||||
|
||||
/// The default [WebGoldenComparator] implementation for `flutter test`.
|
||||
///
|
||||
/// This comparator will send a request to the test server for golden comparison
|
||||
/// which will then defer the comparison to [goldenFileComparator].
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [matchesGoldenFile], the function that invokes the comparator.
|
||||
@Deprecated(
|
||||
'Use goldenFileComparator instead. '
|
||||
'This feature was deprecated after v3.28.0-0.1.pre.',
|
||||
)
|
||||
class DefaultWebGoldenComparator extends WebGoldenComparator {
|
||||
/// Creates a new [DefaultWebGoldenComparator] for the specified [testUri].
|
||||
///
|
||||
/// Golden file keys will be interpreted as file paths relative to the
|
||||
/// directory in which [testUri] resides.
|
||||
///
|
||||
/// The [testUri] must represent a file.
|
||||
@Deprecated(
|
||||
'Use an implementation of GoldenFileComparator instead. '
|
||||
'This feature was deprecated after v3.28.0-0.1.pre.',
|
||||
)
|
||||
DefaultWebGoldenComparator(Uri testUri) : _comparatorImpl = HttpProxyGoldenComparator(testUri);
|
||||
|
||||
// TODO(matanlurey): Refactor as part of https://github.com/flutter/flutter/issues/160261.
|
||||
final HttpProxyGoldenComparator _comparatorImpl;
|
||||
|
||||
@override
|
||||
Future<bool> compare(double width, double height, Uri golden) async {
|
||||
final String key = golden.toString();
|
||||
final web.Response response =
|
||||
await web.window
|
||||
.fetch(
|
||||
'flutter_goldens'.toJS,
|
||||
web.RequestInit(
|
||||
method: 'POST',
|
||||
body:
|
||||
json.encode(<String, Object>{
|
||||
'testUri': _comparatorImpl._testUri.toString(),
|
||||
'key': key,
|
||||
'width': width.round(),
|
||||
'height': height.round(),
|
||||
}).toJS,
|
||||
),
|
||||
)
|
||||
.toDart;
|
||||
final String responseText = (await response.text().toDart).toDart;
|
||||
if (responseText == 'true') {
|
||||
return true;
|
||||
}
|
||||
fail(responseText);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> update(double width, double height, Uri golden) async {
|
||||
// Update is handled on the server side, just use the same logic here
|
||||
await compare(width, height, golden);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> compareBytes(Uint8List bytes, Uri golden) async {
|
||||
return _comparatorImpl.compare(bytes, golden);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateBytes(Uint8List bytes, Uri golden) async {
|
||||
await _comparatorImpl.update(golden, bytes);
|
||||
}
|
||||
}
|
||||
|
||||
@ -195,142 +195,6 @@ abstract class GoldenFileComparator {
|
||||
/// directory-level.
|
||||
GoldenFileComparator goldenFileComparator = const TrivialComparator._();
|
||||
|
||||
/// Compares image pixels against a golden image file.
|
||||
///
|
||||
/// Instances of this comparator will be used as the backend for
|
||||
/// [matchesGoldenFile] when tests are running on Flutter Web, and will usually
|
||||
/// implemented by deferring the screenshot taking and image comparison to a
|
||||
/// test server.
|
||||
///
|
||||
/// Instances of this comparator will be invoked by the test framework in the
|
||||
/// [TestWidgetsFlutterBinding.runAsync] zone and are thus not subject to the
|
||||
/// fake async constraints that are normally imposed on widget tests (i.e. the
|
||||
/// need or the ability to call [WidgetTester.pump] to advance the microtask
|
||||
/// queue). Prior to the invocation, the test framework will render only the
|
||||
/// [Element] to be compared on the screen.
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [GoldenFileComparator] for the comparator to be used when the test is
|
||||
/// not running in a web browser.
|
||||
/// * [DefaultWebGoldenComparator] for the default [WebGoldenComparator]
|
||||
/// implementation for `flutter test`.
|
||||
/// * [matchesGoldenFile], the function that invokes the comparator.
|
||||
@Deprecated(
|
||||
'Use GoldenFileComparator instead. '
|
||||
'This feature was deprecated after v3.28.0-0.1.pre.',
|
||||
)
|
||||
abstract class WebGoldenComparator {
|
||||
/// Compares the rendered pixels of size [width]x[height] that is being
|
||||
/// rendered on the top left of the screen against the golden file identified
|
||||
/// by [golden].
|
||||
///
|
||||
/// The returned future completes with a boolean value that indicates whether
|
||||
/// the pixels rendered on screen match the golden file's pixels.
|
||||
///
|
||||
/// In the case of comparison mismatch, the comparator may choose to throw a
|
||||
/// [TestFailure] if it wants to control the failure message, often in the
|
||||
/// form of a [ComparisonResult] that provides detailed information about the
|
||||
/// mismatch.
|
||||
///
|
||||
/// The method by which [golden] is located and by which its bytes are loaded
|
||||
/// is left up to the implementation class. For instance, some implementations
|
||||
/// may load files from the local file system, whereas others may load files
|
||||
/// over the network or from a remote repository.
|
||||
Future<bool> compare(double width, double height, Uri golden);
|
||||
|
||||
/// Updates the golden file identified by [golden] with rendered pixels of
|
||||
/// [width]x[height].
|
||||
///
|
||||
/// This will be invoked in lieu of [compare] when [autoUpdateGoldenFiles]
|
||||
/// is `true` (which gets set automatically by the test framework when the
|
||||
/// user runs `flutter test --update-goldens --platform=chrome`).
|
||||
///
|
||||
/// The method by which [golden] is located and by which its bytes are written
|
||||
/// is left up to the implementation class.
|
||||
Future<void> update(double width, double height, Uri golden);
|
||||
|
||||
/// Compares the pixels of decoded png [bytes] against the golden file
|
||||
/// identified by [golden].
|
||||
///
|
||||
/// The returned future completes with a boolean value that indicates whether
|
||||
/// the pixels rendered on screen match the golden file's pixels.
|
||||
///
|
||||
/// In the case of comparison mismatch, the comparator may choose to throw a
|
||||
/// [TestFailure] if it wants to control the failure message, often in the
|
||||
/// form of a [ComparisonResult] that provides detailed information about the
|
||||
/// mismatch.
|
||||
///
|
||||
/// The method by which [golden] is located and by which its bytes are loaded
|
||||
/// is left up to the implementation class. For instance, some implementations
|
||||
/// may load files from the local file system, whereas others may load files
|
||||
/// over the network or from a remote repository.
|
||||
Future<bool> compareBytes(Uint8List bytes, Uri golden);
|
||||
|
||||
/// Compares the pixels of decoded png [bytes] against the golden file
|
||||
/// identified by [golden].
|
||||
///
|
||||
/// This will be invoked in lieu of [compareBytes] when [autoUpdateGoldenFiles]
|
||||
/// is `true` (which gets set automatically by the test framework when the
|
||||
/// user runs `flutter test --update-goldens --platform=chrome`).
|
||||
///
|
||||
/// The method by which [golden] is located and by which its bytes are written
|
||||
/// is left up to the implementation class.
|
||||
Future<void> updateBytes(Uint8List bytes, Uri golden);
|
||||
|
||||
/// Returns a new golden file [Uri] to incorporate any [version] number with
|
||||
/// the [key].
|
||||
///
|
||||
/// The [version] is an optional int that can be used to differentiate
|
||||
/// historical golden files.
|
||||
///
|
||||
/// Version numbers are used in golden file tests for package:flutter. You can
|
||||
/// learn more about these tests [here](https://github.com/flutter/flutter/blob/main/docs/contributing/testing/Writing-a-golden-file-test-for-package-flutter.md).
|
||||
Uri getTestUri(Uri key, int? version) {
|
||||
if (version == null) {
|
||||
return key;
|
||||
}
|
||||
final String keyString = key.toString();
|
||||
final String extension = path.extension(keyString);
|
||||
return Uri.parse('${keyString.split(extension).join()}.$version$extension');
|
||||
}
|
||||
}
|
||||
|
||||
/// Compares pixels against those of a golden image file.
|
||||
///
|
||||
/// This comparator is used as the backend for [matchesGoldenFile] when tests
|
||||
/// are running in a web browser.
|
||||
///
|
||||
/// When using `flutter test --platform=chrome`, a comparator implemented by
|
||||
/// [DefaultWebGoldenComparator] is used if no other comparator is specified. It
|
||||
/// will send a request to the test server, which uses [goldenFileComparator]
|
||||
/// for golden file comparison.
|
||||
///
|
||||
/// When using `flutter test --update-goldens`, the [DefaultWebGoldenComparator]
|
||||
/// updates the files on disk to match the rendering.
|
||||
///
|
||||
/// When using `flutter run`, the default comparator
|
||||
/// (`_TrivialWebGoldenComparator`) is used. It prints a message to the console
|
||||
/// but otherwise does nothing. This allows tests to be developed visually on a
|
||||
/// web browser.
|
||||
///
|
||||
/// Callers may choose to override the default comparator by setting this to a
|
||||
/// custom comparator during test set-up (or using directory-level test
|
||||
/// configuration). For example, some projects may wish to install a comparator
|
||||
/// with tolerance levels for allowable differences.
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [flutter_test] for more information about how to configure tests at the
|
||||
/// directory-level.
|
||||
/// * [goldenFileComparator], the comparator used when tests are not running on
|
||||
/// a web browser.
|
||||
WebGoldenComparator get webGoldenComparator => _webGoldenComparator;
|
||||
WebGoldenComparator _webGoldenComparator = const _TrivialWebGoldenComparator._();
|
||||
set webGoldenComparator(WebGoldenComparator value) {
|
||||
_webGoldenComparator = value;
|
||||
}
|
||||
|
||||
/// Whether golden files should be automatically updated during tests rather
|
||||
/// than compared to the image bytes recorded by the tests.
|
||||
///
|
||||
@ -386,44 +250,6 @@ class TrivialComparator implements GoldenFileComparator {
|
||||
}
|
||||
}
|
||||
|
||||
class _TrivialWebGoldenComparator implements WebGoldenComparator {
|
||||
const _TrivialWebGoldenComparator._();
|
||||
|
||||
@override
|
||||
Future<bool> compare(double width, double height, Uri golden) {
|
||||
return _warnAboutSkipping(golden);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> update(double width, double height, Uri golden) {
|
||||
throw StateError('webGoldenComparator has not been initialized');
|
||||
}
|
||||
|
||||
@override
|
||||
Uri getTestUri(Uri key, int? version) {
|
||||
return key;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> compareBytes(Uint8List bytes, Uri golden) {
|
||||
return _warnAboutSkipping(golden);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateBytes(Uint8List bytes, Uri golden) {
|
||||
throw StateError('webGoldenComparator has not been initialized');
|
||||
}
|
||||
|
||||
Future<bool> _warnAboutSkipping(Uri golden) {
|
||||
// Ideally we would use markTestSkipped here but in some situations,
|
||||
// comparators are called outside of tests.
|
||||
// See also: https://github.com/flutter/flutter/issues/91285
|
||||
// ignore: avoid_print
|
||||
print('Golden comparison requested for "$golden"; skipping...');
|
||||
return Future<bool>.value(true);
|
||||
}
|
||||
}
|
||||
|
||||
/// The result of a pixel comparison test.
|
||||
///
|
||||
/// The [ComparisonResult] will always indicate if a test has [passed]. The
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user