flutter_flutter/packages/flutter_test/test/reference_image_test.dart
Kate Lovett 9d96df2364
Modernize framework lints (#179089)
WIP

Commits separated as follows:
- Update lints in analysis_options files
- Run `dart fix --apply`
- Clean up leftover analysis issues 
- Run `dart format .` in the right places.

Local analysis and testing passes. Checking CI now.

Part of https://github.com/flutter/flutter/issues/178827
- Adoption of flutter_lints in examples/api coming in a separate change
(cc @loic-sharma)

## Pre-launch Checklist

- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[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
2025-11-26 01:10:39 +00:00

151 lines
5.1 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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 'dart:ui' as ui;
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
Future<ui.Image> createTestImage(int width, int height, ui.Color color) async {
final paint = ui.Paint()
..style = ui.PaintingStyle.stroke
..strokeWidth = 1.0
..color = color;
final recorder = ui.PictureRecorder();
final pictureCanvas = ui.Canvas(recorder);
pictureCanvas.drawCircle(Offset.zero, 20.0, paint);
final ui.Picture picture = recorder.endRecording();
final ui.Image image = await picture.toImage(width, height);
picture.dispose();
return image;
}
void main() {
const red = ui.Color.fromARGB(255, 255, 0, 0);
const green = ui.Color.fromARGB(255, 0, 255, 0);
const transparentRed = ui.Color.fromARGB(128, 255, 0, 0);
group('succeeds', () {
testWidgets('when images have the same content', (WidgetTester tester) async {
final ui.Image image1 = await createTestImage(100, 100, red);
addTearDown(image1.dispose);
final ui.Image referenceImage1 = await createTestImage(100, 100, red);
addTearDown(referenceImage1.dispose);
await expectLater(image1, matchesReferenceImage(referenceImage1));
final ui.Image image2 = await createTestImage(100, 100, green);
addTearDown(image2.dispose);
final ui.Image referenceImage2 = await createTestImage(100, 100, green);
addTearDown(referenceImage2.dispose);
await expectLater(image2, matchesReferenceImage(referenceImage2));
final ui.Image image3 = await createTestImage(100, 100, transparentRed);
addTearDown(image3.dispose);
final ui.Image referenceImage3 = await createTestImage(100, 100, transparentRed);
addTearDown(referenceImage3.dispose);
await expectLater(image3, matchesReferenceImage(referenceImage3));
});
testWidgets('when images are identical', (WidgetTester tester) async {
final ui.Image image = await createTestImage(100, 100, red);
addTearDown(image.dispose);
await expectLater(image, matchesReferenceImage(image));
});
testWidgets('when widget looks the same', (WidgetTester tester) async {
addTearDown(tester.view.reset);
tester.view
..physicalSize = const Size(10, 10)
..devicePixelRatio = 1;
const repaintBoundaryKey = ValueKey<String>('boundary');
await tester.pumpWidget(
const RepaintBoundary(
key: repaintBoundaryKey,
child: ColoredBox(color: red),
),
);
final ui.Image referenceImage =
(tester.renderObject(find.byKey(repaintBoundaryKey)) as RenderRepaintBoundary)
.toImageSync();
addTearDown(referenceImage.dispose);
await expectLater(find.byKey(repaintBoundaryKey), matchesReferenceImage(referenceImage));
});
});
group('fails', () {
testWidgets('when image sizes do not match', (WidgetTester tester) async {
final ui.Image red50 = await createTestImage(50, 50, red);
addTearDown(red50.dispose);
final ui.Image red100 = await createTestImage(100, 100, red);
addTearDown(red100.dispose);
expect(
await matchesReferenceImage(red50).matchAsync(red100),
equals('does not match as width or height do not match. [100×100] != [50×50]'),
);
});
testWidgets('when image pixels do not match', (WidgetTester tester) async {
final ui.Image red100 = await createTestImage(100, 100, red);
addTearDown(red100.dispose);
final ui.Image transparentRed100 = await createTestImage(100, 100, transparentRed);
addTearDown(transparentRed100.dispose);
expect(
await matchesReferenceImage(red100).matchAsync(transparentRed100),
equals('does not match on 57 pixels'),
);
final ui.Image green100 = await createTestImage(100, 100, green);
addTearDown(green100.dispose);
expect(
await matchesReferenceImage(red100).matchAsync(green100),
equals('does not match on 57 pixels'),
);
});
testWidgets('when widget does not look the same', (WidgetTester tester) async {
addTearDown(tester.view.reset);
tester.view
..physicalSize = const Size(10, 10)
..devicePixelRatio = 1;
const repaintBoundaryKey = ValueKey<String>('boundary');
await tester.pumpWidget(
const RepaintBoundary(
key: repaintBoundaryKey,
child: ColoredBox(color: red),
),
);
final ui.Image referenceImage =
(tester.renderObject(find.byKey(repaintBoundaryKey)) as RenderRepaintBoundary)
.toImageSync();
addTearDown(referenceImage.dispose);
await tester.pumpWidget(
const RepaintBoundary(
key: repaintBoundaryKey,
child: ColoredBox(color: green),
),
);
expect(
await matchesReferenceImage(referenceImage).matchAsync(find.byKey(repaintBoundaryKey)),
equals('does not match on 100 pixels'),
);
});
});
}