File size: 5,277 Bytes
88b9d23 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | // 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 ui.Paint paint = ui.Paint()
..style = ui.PaintingStyle.stroke
..strokeWidth = 1.0
..color = color;
final ui.PictureRecorder recorder = ui.PictureRecorder();
final ui.Canvas 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 ui.Color red = ui.Color.fromARGB(255, 255, 0, 0);
const ui.Color green = ui.Color.fromARGB(255, 0, 255, 0);
const ui.Color 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 ValueKey<String> 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 ValueKey<String> 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'),
);
});
});
}
|