[web] Remove old unused golden diffing (flutter/engine#36132)

This commit is contained in:
Mouad Debbar
2022-09-14 14:17:46 -04:00
committed by GitHub
parent 3ab4a2a15d
commit ab1baa4927
26 changed files with 47 additions and 393 deletions

View File

@@ -17,7 +17,6 @@ import 'safari_macos.dart';
const int kDevtoolsPort = 12345;
const int kMaxScreenshotWidth = 1024;
const int kMaxScreenshotHeight = 1024;
const double kMaxDiffRateFailure = 0.28 / 100; // 0.28%
abstract class PlatformBinding {
static PlatformBinding get instance {

View File

@@ -34,11 +34,9 @@ import 'package:test_core/src/util/io.dart';
import 'package:test_core/src/util/stack_trace_mapper.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:web_test_utils/goldens.dart';
import 'package:web_test_utils/image_compare.dart';
import 'browser.dart';
import 'common.dart';
import 'environment.dart' as env;
/// Custom test platform that serves web engine unit tests.
@@ -329,26 +327,16 @@ class BrowserPlatform extends PlatformPlugin {
return shelf.Response.ok(json.encode('OK'));
}
final double maxDiffRate = requestData.containsKey('maxdiffrate')
? (requestData['maxdiffrate'] as num)
.toDouble() // can be parsed as either int or double
: kMaxDiffRateFailure;
final Map<String, dynamic> region =
requestData['region'] as Map<String, dynamic>;
final PixelComparison pixelComparison = PixelComparison.values.firstWhere(
(PixelComparison value) =>
value.toString() == requestData['pixelComparison']);
final bool isCanvaskitTest = requestData['isCanvaskitTest'] as bool;
final String result = await _diffScreenshot(
filename, maxDiffRate, region, pixelComparison, isCanvaskitTest);
final String result = await _diffScreenshot(filename, region, isCanvaskitTest);
return shelf.Response.ok(json.encode(result));
}
Future<String> _diffScreenshot(
String filename,
double maxDiffRateFailure,
Map<String, dynamic> region,
PixelComparison pixelComparison,
bool isCanvaskitTest,
) async {
final Rectangle<num> regionAsRectange = Rectangle<num>(
@@ -366,8 +354,6 @@ class BrowserPlatform extends PlatformPlugin {
screenshot,
doUpdateScreenshotGoldens,
filename,
pixelComparison,
maxDiffRateFailure,
skiaClient,
isCanvaskitTest: isCanvaskitTest,
);

View File

@@ -1570,7 +1570,6 @@ void _paragraphTests() {
await matchGoldenFile(
'paragraph_kitchen_sink.png',
region: const ui.Rect.fromLTRB(0, 0, 400, 160),
maxDiffRatePercent: 0.0,
);
}

View File

@@ -192,7 +192,7 @@ Future<void> matchPictureGolden(String goldenFile, CkPicture picture,
sb.pushOffset(0, 0);
sb.addPicture(ui.Offset.zero, picture);
CanvasKitRenderer.instance.rasterizer.draw(sb.build().layerTree);
await matchGoldenFile(goldenFile, region: region, maxDiffRatePercent: 0.0);
await matchGoldenFile(goldenFile, region: region);
}
/// Sends a platform message to create a Platform View with the given id and viewType.

View File

@@ -514,7 +514,6 @@ void _testForImageCodecs({required bool useBrowserImageDecoder}) {
await matchGoldenFile(
'canvaskit_read_back_decoded_image_$mode.png',
region: const ui.Rect.fromLTRB(0, 0, 150, 150),
maxDiffRatePercent: 0,
);
}
@@ -566,7 +565,6 @@ void _testForImageCodecs({required bool useBrowserImageDecoder}) {
await matchGoldenFile(
'canvaskit_cross_gl_context_image_$mode.png',
region: const ui.Rect.fromLTRB(0, 0, 100, 100),
maxDiffRatePercent: 0,
);
await disposePlatformView(0);
@@ -611,7 +609,6 @@ void _testForImageCodecs({required bool useBrowserImageDecoder}) {
await matchGoldenFile(
'canvaskit_picture_texture_toimage',
region: const ui.Rect.fromLTRB(0, 0, 128, 128),
maxDiffRatePercent: 0,
);
mandrill.dispose();
codec.dispose();

View File

@@ -103,8 +103,7 @@ Future<void> testMain() async {
appendToScene();
await matchGoldenFile('misaligned_canvas_test.png', region: region,
maxDiffRatePercent: 1.0);
await matchGoldenFile('misaligned_canvas_test.png', region: region);
});
test('fill the whole canvas with color even when transformed', () async {
@@ -117,8 +116,7 @@ Future<void> testMain() async {
appendToScene();
await matchGoldenFile('bitmap_canvas_fills_color_when_transformed.png',
region: region,
maxDiffRatePercent: 5.0);
region: region);
});
test('fill the whole canvas with paint even when transformed', () async {
@@ -133,8 +131,7 @@ Future<void> testMain() async {
appendToScene();
await matchGoldenFile('bitmap_canvas_fills_paint_when_transformed.png',
region: region,
maxDiffRatePercent: 5.0);
region: region);
});
// This test reproduces text blurriness when two pieces of text appear inside
@@ -186,8 +183,6 @@ Future<void> testMain() async {
await matchGoldenFile(
'bitmap_canvas_draws_high_quality_text.png',
region: canvasSize,
maxDiffRatePercent: 0.0,
pixelComparison: PixelComparison.precise,
);
}, testOn: 'chrome');
@@ -264,8 +259,6 @@ Future<void> testMain() async {
await matchGoldenFile(
'bitmap_canvas_draws_text_on_top_of_canvas.png',
region: canvasSize,
maxDiffRatePercent: 1.0,
pixelComparison: PixelComparison.precise,
);
});

View File

@@ -57,9 +57,7 @@ Future<void> testMain() async {
..color = const Color.fromARGB(128, 255, 0, 0));
rc.restore();
await canvasScreenshot(rc, 'canvas_blend_circle_diff_color',
maxDiffRatePercent: operatingSystem == OperatingSystem.macOs ? 2.95 :
operatingSystem == OperatingSystem.iOs ? 1.0 : 0);
await canvasScreenshot(rc, 'canvas_blend_circle_diff_color');
});
test('Blend circle and text with multiply', () async {
@@ -95,9 +93,7 @@ Future<void> testMain() async {
rc.drawImage(createTestImage(), const Offset(135.0, 130.0),
SurfacePaint()..blendMode = BlendMode.multiply);
rc.restore();
await canvasScreenshot(rc, 'canvas_blend_image_multiply',
maxDiffRatePercent: operatingSystem == OperatingSystem.macOs ? 2.95 :
operatingSystem == OperatingSystem.iOs ? 2.0 : 0);
await canvasScreenshot(rc, 'canvas_blend_image_multiply');
});
}

View File

@@ -85,8 +85,7 @@ Future<void> testMain() async {
..colorFilter = EngineColorFilter.mode(black, blendMode));
}
rc.restore();
await canvasScreenshot(rc, 'canvas_image_blend_group$blendGroup',
maxDiffRatePercent: 8.0);
await canvasScreenshot(rc, 'canvas_image_blend_group$blendGroup');
},
skip: isSafari);
}
@@ -110,8 +109,7 @@ Future<void> testMain() async {
rc.drawParagraph(paragraph, const Offset(textLeft, textTop));
rc.restore();
await canvasScreenshot(rc, 'canvas_image_blend_and_text',
maxDiffRatePercent: 8.0);
await canvasScreenshot(rc, 'canvas_image_blend_and_text');
});
}

View File

@@ -45,8 +45,7 @@ Future<void> testMain() async {
// TODO(ferhat): update golden for this test after canvas sandwich detection is
// added to RecordingCanvas.
await matchGoldenFile('color_filter_blendMode_color.png', region: region,
maxDiffRatePercent: 12.0);
await matchGoldenFile('color_filter_blendMode_color.png', region: region);
});
test('Should apply matrix color filter to image', () async {
@@ -65,8 +64,7 @@ Future<void> testMain() async {
builder.addPicture(Offset.zero, circles1);
builder.pop();
domDocument.body!.append(builder.build().webOnlyRootElement!);
await matchGoldenFile('color_filter_matrix.png', region: region,
maxDiffRatePercent: 12.0);
await matchGoldenFile('color_filter_matrix.png', region: region);
});
/// Regression test for https://github.com/flutter/flutter/issues/85733
@@ -83,8 +81,7 @@ Future<void> testMain() async {
builder.addPicture(Offset.zero, circles1);
builder.pop();
domDocument.body!.append(builder.build().webOnlyRootElement!);
await matchGoldenFile('color_filter_mode.png', region: region,
maxDiffRatePercent: 12.0);
await matchGoldenFile('color_filter_mode.png', region: region);
});
/// Regression test for https://github.com/flutter/flutter/issues/59451.
@@ -113,8 +110,7 @@ Future<void> testMain() async {
domDocument.body!.append(builder2.build().webOnlyRootElement!);
await matchGoldenFile('color_filter_blendMode_overlay.png',
region: region,
maxDiffRatePercent: 12.0);
region: region);
});
}

View File

@@ -302,7 +302,7 @@ Future<void> testMain() async {
final DomElement viewElement3 = builder3.build().webOnlyRootElement!;
domDocument.body!.append(viewElement3);
await matchGoldenFile('compositing_physical_update_3.png',
region: region, maxDiffRatePercent: 0.8);
region: region);
viewElement3.remove();
/// Update shape from arbitrary path to rect.
@@ -337,8 +337,7 @@ Future<void> testMain() async {
final DomElement viewElement5 = builder5.build().webOnlyRootElement!;
domDocument.body!.append(viewElement5);
await matchGoldenFile('compositing_physical_update_3.png',
region: region,
maxDiffRatePercent: browserEngine == BrowserEngine.webkit ? 0.6 : 0.4);
region: region);
viewElement5.remove();
/// Update shadow color.
@@ -883,8 +882,6 @@ void _testCullRectComputation() {
await matchGoldenFile(
'compositing_draw_high_quality_text.png',
region: canvasSize,
maxDiffRatePercent: 0.0,
pixelComparison: PixelComparison.precise,
);
},
testOn: 'chrome',

View File

@@ -29,7 +29,6 @@ Future<void> testMain() async {
..maskFilter = MaskFilter.blur(BlurStyle.normal, blurSigma.toDouble());
rc.drawRect(Rect.fromLTWH(15.0, 15.0 + blurSigma * 40, 200, 20), paint);
}
await canvasScreenshot(rc, 'dom_mask_filter_blur',
maxDiffRatePercent: 0.01);
await canvasScreenshot(rc, 'dom_mask_filter_blur');
});
}

View File

@@ -350,7 +350,6 @@ Future<void> testMain() async {
await canvasScreenshot(
rc,
'draw_text_composite_order_below',
maxDiffRatePercent: 1.1,
region: const Rect.fromLTWH(0, 0, 350, 300),
);
});
@@ -381,7 +380,7 @@ Future<void> testMain() async {
sceneElement.append(builder.build().webOnlyRootElement!);
domDocument.body!.append(sceneElement);
await matchGoldenFile('draw_nine_slice.png',
region: region, maxDiffRatePercent: 0);
region: region);
} finally {
// The page is reused across tests, so remove the element after taking the
// Scuba screenshot.
@@ -420,7 +419,7 @@ Future<void> testMain() async {
sceneElement.append(builder.build().webOnlyRootElement!);
domDocument.body!.append(sceneElement);
await matchGoldenFile('draw_nine_slice_empty_center.png',
region: region, maxDiffRatePercent: 0);
region: region);
} finally {
// The page is reused across tests, so remove the element after taking the
// Scuba screenshot.
@@ -446,7 +445,7 @@ Future<void> testMain() async {
..close());
canvas.drawImage(createNineSliceImage(), Offset.zero, SurfacePaint());
await canvasScreenshot(canvas, 'draw_clipped_and_transformed_image',
region: region, maxDiffRatePercent: 1.0);
region: region);
});
/// Regression test for https://github.com/flutter/flutter/issues/61245
@@ -462,7 +461,6 @@ Future<void> testMain() async {
canvas.drawImage(createTestImage(), const Offset(0, 100), SurfacePaint());
await canvasScreenshot(canvas, 'draw_3d_image',
region: region,
maxDiffRatePercent: 6.0,
setupPerspective: true);
});
@@ -483,7 +481,6 @@ Future<void> testMain() async {
canvas.drawRect(const Rect.fromLTWH(50, 150, 50, 20), SurfacePaint()..color = const Color(0x80000000));
await canvasScreenshot(canvas, 'draw_3d_image_clipped',
region: region,
maxDiffRatePercent: 5.0,
setupPerspective: true);
});
@@ -507,7 +504,6 @@ Future<void> testMain() async {
SurfacePaint()..color = const Color(0x80E010E0));
await canvasScreenshot(canvas, 'draw_3d_rect_clipped',
region: region,
maxDiffRatePercent: 1.0,
setupPerspective: true);
});
@@ -532,7 +528,6 @@ Future<void> testMain() async {
canvas.drawCircle(const Offset(360, 370), 30, SurfacePaint()..color = const Color(0x80E010E0));
await canvasScreenshot(canvas, 'draw_3d_oval_clipped',
region: region,
maxDiffRatePercent: 1.0,
setupPerspective: true);
});
@@ -564,7 +559,6 @@ Future<void> testMain() async {
canvas.drawCircle(const Offset(100, 50), 4, SurfacePaint()..color = const Color(0xFF000000));
await canvasScreenshot(canvas, 'draw_3d_path',
region: region,
maxDiffRatePercent: 1.0,
setupPerspective: true);
});
@@ -597,7 +591,6 @@ Future<void> testMain() async {
canvas.drawCircle(const Offset(100, 50), 4, SurfacePaint()..color = const Color(0xFF000000));
await canvasScreenshot(canvas, 'draw_3d_path_clipped',
region: region,
maxDiffRatePercent: 1.0,
setupPerspective: true);
});
}

View File

@@ -150,7 +150,7 @@ Future<void> testMain() async {
canvas.drawParagraph(paragraph, offset);
offset = offset.translate(0, paragraph.height + 10);
return takeScreenshot(canvas, bounds, 'canvas_paragraph_align_dom', maxDiffRatePercent: 0.3);
return takeScreenshot(canvas, bounds, 'canvas_paragraph_align_dom');
});
void testAlignAndTransform(EngineCanvas canvas) {

View File

@@ -45,9 +45,8 @@ CanvasParagraph rich(
Future<void> takeScreenshot(
EngineCanvas canvas,
Rect region,
String fileName, {
double? maxDiffRatePercent,
}) async {
String fileName,
) async {
final DomElement sceneElement = createDomElement('flt-scene');
if (isIosSafari) {
// Shrink to fit on the iPhone screen.
@@ -61,7 +60,6 @@ Future<void> takeScreenshot(
await matchGoldenFile(
'$fileName.png',
region: region,
maxDiffRatePercent: maxDiffRatePercent,
);
} finally {
// The page is reused across tests, so remove the element after taking the

View File

@@ -42,12 +42,10 @@ class EngineScubaTester {
Future<void> diffScreenshot(
String fileName, {
ui.Rect? region,
double? maxDiffRatePercent,
}) async {
await matchGoldenFile(
'$fileName.png',
region: region ?? viewportRegion,
maxDiffRatePercent: maxDiffRatePercent,
);
}
@@ -59,7 +57,6 @@ class EngineScubaTester {
EngineCanvas canvas,
String fileName, {
ui.Rect? region,
double? maxDiffRatePercent,
}) async {
// Wrap in <flt-scene> so that our CSS selectors kick in.
final DomElement sceneElement = createDomElement('flt-scene');
@@ -79,7 +76,6 @@ class EngineScubaTester {
await diffScreenshot(
screenshotName,
region: region,
maxDiffRatePercent: maxDiffRatePercent,
);
} finally {
// The page is reused across tests, so remove the element after taking the
@@ -92,8 +88,7 @@ class EngineScubaTester {
typedef CanvasTest = FutureOr<void> Function(EngineCanvas canvas);
/// Runs the given test [body] with each type of canvas.
void testEachCanvas(String description, CanvasTest body,
{double? maxDiffRate}) {
void testEachCanvas(String description, CanvasTest body) {
const ui.Rect bounds = ui.Rect.fromLTWH(0, 0, 600, 800);
test('$description (bitmap + canvas measurement)', () async {
return body(BitmapCanvas(bounds, RenderStrategy()));

View File

@@ -26,7 +26,6 @@ Future<void> testMain() async {
Future<void> testPath(Path path, String scubaFileName,
{SurfacePaint? paint,
double? maxDiffRatePercent,
PaintMode mode = PaintMode.kStrokeAndFill}) async {
const Rect canvasBounds = Rect.fromLTWH(0, 0, 600, 400);
final BitmapCanvas bitmapCanvas =
@@ -74,7 +73,7 @@ Future<void> testMain() async {
sceneElement.append(svgElement);
await matchGoldenFile('$scubaFileName.png',
region: region, maxDiffRatePercent: maxDiffRatePercent);
region: region);
bitmapCanvas.rootElement.remove();
svgElement.remove();
@@ -136,7 +135,7 @@ Future<void> testMain() async {
final Path path = Path();
path.addRect(const Rect.fromLTRB(15, 15, 60, 20));
path.addRect(const Rect.fromLTRB(35, 160, 15, 100));
await testPath(path, 'svg_rect', maxDiffRatePercent: 1.0);
await testPath(path, 'svg_rect');
});
test('render notch', () async {

View File

@@ -701,8 +701,6 @@ Future<void> testMain() async {
await matchGoldenFile(
'paint_spread_bounds.png',
region: const Rect.fromLTRB(0, 0, 250, 600),
maxDiffRatePercent: 0.21,
pixelComparison: PixelComparison.precise,
);
} finally {
sceneElement.remove();

View File

@@ -19,7 +19,6 @@ Future<void> canvasScreenshot(
String fileName, {
ui.Rect region = const ui.Rect.fromLTWH(0, 0, 500, 500),
ui.Rect? canvasRect,
double maxDiffRatePercent = 0.0,
bool setupPerspective = false,
}) async {
canvasRect ??= region;
@@ -47,7 +46,7 @@ Future<void> canvasScreenshot(
sceneElement.append(engineCanvas.rootElement);
domDocument.body!.append(sceneElement);
await matchGoldenFile('$fileName.png',
region: region, maxDiffRatePercent: maxDiffRatePercent);
region: region);
} finally {
// The page is reused across tests, so remove the element after taking the
// Scuba screenshot.
@@ -56,8 +55,7 @@ Future<void> canvasScreenshot(
}
Future<void> sceneScreenshot(SurfaceSceneBuilder sceneBuilder, String fileName,
{ui.Rect region = const ui.Rect.fromLTWH(0, 0, 600, 800),
double maxDiffRatePercent = 0.0}) async {
{ui.Rect region = const ui.Rect.fromLTWH(0, 0, 600, 800)}) async {
DomElement? sceneElement;
try {
sceneElement = sceneBuilder
@@ -65,7 +63,7 @@ Future<void> sceneScreenshot(SurfaceSceneBuilder sceneBuilder, String fileName,
.webOnlyRootElement;
domDocument.body!.append(sceneElement!);
await matchGoldenFile('$fileName.png',
region: region, maxDiffRatePercent: maxDiffRatePercent);
region: region);
} finally {
// The page is reused across tests, so remove the element after taking the
// Scuba screenshot.

View File

@@ -65,8 +65,7 @@ Future<void> testMain() async {
}
Future<void> testImageShader(
TileMode tmx, TileMode tmy, String fileName,
{double maxDiffRatePercent = 0.0}) async {
TileMode tmx, TileMode tmy, String fileName) async {
final RecordingCanvas rc =
RecordingCanvas(const Rect.fromLTRB(0, 0, screenWidth, screenHeight));
//Rect shaderRect = const Rect.fromLTRB(20, 20, 100, 100);
@@ -80,49 +79,42 @@ Future<void> testMain() async {
expect(rc.renderStrategy.hasArbitraryPaint, isTrue);
await canvasScreenshot(rc, fileName,
region: screenRect, maxDiffRatePercent: maxDiffRatePercent);
region: screenRect);
}
test('Should draw with tiled imageshader.', () async {
await testImageShader(
TileMode.repeated, TileMode.repeated, 'image_shader_tiled',
maxDiffRatePercent: 5.0);
TileMode.repeated, TileMode.repeated, 'image_shader_tiled');
});
test('Should draw with horizontally mirrored imageshader.', () async {
await testImageShader(
TileMode.mirror, TileMode.repeated, 'image_shader_horiz_mirror',
maxDiffRatePercent: 6.0);
TileMode.mirror, TileMode.repeated, 'image_shader_horiz_mirror');
});
test('Should draw with vertically mirrored imageshader.', () async {
await testImageShader(
TileMode.repeated, TileMode.mirror, 'image_shader_vert_mirror',
maxDiffRatePercent: 5.0);
TileMode.repeated, TileMode.mirror, 'image_shader_vert_mirror');
});
test('Should draw with mirrored imageshader.', () async {
await testImageShader(
TileMode.mirror, TileMode.mirror, 'image_shader_mirror',
maxDiffRatePercent: 6.0);
TileMode.mirror, TileMode.mirror, 'image_shader_mirror');
});
test('Should draw with horizontal clamp imageshader.', () async {
await testImageShader(
TileMode.clamp, TileMode.repeated, 'image_shader_clamp_horiz',
maxDiffRatePercent: 13.0);
TileMode.clamp, TileMode.repeated, 'image_shader_clamp_horiz');
}, skip: isFirefox);
test('Should draw with vertical clamp imageshader.', () async {
await testImageShader(
TileMode.repeated, TileMode.clamp, 'image_shader_clamp_vertical',
maxDiffRatePercent: 1.0);
TileMode.repeated, TileMode.clamp, 'image_shader_clamp_vertical');
}, skip: isFirefox);
test('Should draw with clamp imageshader.', () async {
await testImageShader(
TileMode.clamp, TileMode.clamp, 'image_shader_clamp',
maxDiffRatePercent: 1.0);
TileMode.clamp, TileMode.clamp, 'image_shader_clamp');
}, skip: isFirefox);
}

View File

@@ -35,8 +35,7 @@ Future<void> testMain() async {
const <Color>[Color(0xFFcfdfd2), Color(0xFF042a85)]);
rc.drawRect(shaderRect, paint);
expect(rc.renderStrategy.hasArbitraryPaint, isTrue);
await canvasScreenshot(rc, 'linear_gradient_rect',
maxDiffRatePercent: 0.01);
await canvasScreenshot(rc, 'linear_gradient_rect');
});
test('Should blend linear gradient with alpha channel correctly.', () async {
@@ -55,8 +54,7 @@ Future<void> testMain() async {
const <Color>[Color(0x00000000), Color(0xFF0000FF)]);
rc.drawRect(shaderRect, paint);
expect(rc.renderStrategy.hasArbitraryPaint, isTrue);
await canvasScreenshot(rc, 'linear_gradient_rect_alpha',
maxDiffRatePercent: 0.01);
await canvasScreenshot(rc, 'linear_gradient_rect_alpha');
});
test('Should draw linear gradient with transform.', () async {
@@ -87,8 +85,7 @@ Future<void> testMain() async {
yOffset += 120;
}
expect(rc.renderStrategy.hasArbitraryPaint, isTrue);
await canvasScreenshot(rc, 'linear_gradient_oval_matrix',
maxDiffRatePercent: 0.2);
await canvasScreenshot(rc, 'linear_gradient_oval_matrix');
});
// Regression test for https://github.com/flutter/flutter/issues/50010
@@ -102,8 +99,7 @@ Future<void> testMain() async {
const <Color>[Color(0xFFcfdfd2), Color(0xFF042a85)]);
rc.drawRRect(RRect.fromRectAndRadius(shaderRect, const Radius.circular(16)), paint);
expect(rc.renderStrategy.hasArbitraryPaint, isTrue);
await canvasScreenshot(rc, 'linear_gradient_rounded_rect',
maxDiffRatePercent: 0.1);
await canvasScreenshot(rc, 'linear_gradient_rounded_rect');
});
test('Should draw tiled repeated linear gradient with transform.', () async {

View File

@@ -24,15 +24,13 @@ Future<void> testMain() async {
Future<void> testGradient(String fileName, Shader shader,
{Rect paintRect = const Rect.fromLTRB(50, 50, 300, 300),
Rect shaderRect = const Rect.fromLTRB(50, 50, 300, 300),
double maxDiffRatePercent = 0,
Rect region = const Rect.fromLTWH(0, 0, 500, 500)}) async {
final RecordingCanvas rc = RecordingCanvas(region);
final SurfacePaint paint = SurfacePaint()..shader = shader;
final Path path = Path();
path.addRect(paintRect);
rc.drawPath(path, paint);
await canvasScreenshot(rc, fileName, region: region,
maxDiffRatePercent: maxDiffRatePercent);
await canvasScreenshot(rc, fileName, region: region);
}
test('Should draw centered radial gradient.', () async {
@@ -46,8 +44,7 @@ Future<void> testMain() async {
<Color>[
const Color.fromARGB(255, 0, 0, 0),
const Color.fromARGB(255, 0, 0, 255)
]),
maxDiffRatePercent: 0.2);
]));
});
test('Should draw right bottom centered radial gradient.', () async {
@@ -62,7 +59,6 @@ Future<void> testMain() async {
const Color.fromARGB(255, 0, 0, 255)
],
),
maxDiffRatePercent: 0.3,
);
});
@@ -81,7 +77,6 @@ Future<void> testMain() async {
<double>[0.0, 1.0],
),
shaderRect: shaderRect,
maxDiffRatePercent: 0.2,
);
});

View File

@@ -100,7 +100,7 @@ Future<void> testMain() async {
test('Renders text with linear gradient shader mask', () async {
_renderTextScene(BlendMode.srcIn);
await matchGoldenFile('shadermask_linear_text.png',
region: const Rect.fromLTWH(0, 0, 360, 200), maxDiffRatePercent: 2.0);
region: const Rect.fromLTWH(0, 0, 360, 200));
}, skip: isSafari || isFirefox);
}

View File

@@ -160,8 +160,6 @@ Future<void> testMain() async {
await matchGoldenFile(
'shadows.png',
region: region,
maxDiffRatePercent: 0.23,
pixelComparison: PixelComparison.precise,
);
},
testOn: 'chrome',

View File

@@ -7,7 +7,7 @@ import 'dart:convert';
import 'package:test/test.dart';
// ignore: implementation_imports
import 'package:ui/src/engine.dart' show OperatingSystem, operatingSystem, renderer;
import 'package:ui/src/engine.dart' show renderer;
// ignore: implementation_imports
import 'package:ui/src/engine/dom.dart';
import 'package:ui/ui.dart';
@@ -47,8 +47,7 @@ enum PixelComparison {
///
/// [pixelComparison] determines the algorithm used to compare pixels. Uses
/// fuzzy comparison by default.
Future<void> matchGoldenFile(String filename,
{Rect? region, double? maxDiffRatePercent, PixelComparison pixelComparison = PixelComparison.fuzzy}) async {
Future<void> matchGoldenFile(String filename, {Rect? region}) async {
final Map<String, dynamic> serverParams = <String, dynamic>{
'filename': filename,
'region': region == null
@@ -59,7 +58,6 @@ Future<void> matchGoldenFile(String filename,
'width': region.width,
'height': region.height
},
'pixelComparison': pixelComparison.toString(),
// We use the renderer tag here rather than `renderer is CanvasKitRenderer`
// because these unit tests operate on the post-transformed (sdk_rewriter)
// sdk where the internal classes like `CanvasKitRenderer` are no longer
@@ -67,16 +65,6 @@ Future<void> matchGoldenFile(String filename,
'isCanvaskitTest': renderer.rendererTag == 'canvaskit',
};
// Chrome on macOS renders slightly differently from Linux, so allow it an
// extra 1% to deviate from the golden files.
if (maxDiffRatePercent != null) {
if (operatingSystem == OperatingSystem.macOs) {
maxDiffRatePercent += 1.0;
}
serverParams['maxdiffrate'] = maxDiffRatePercent / 100;
} else if (operatingSystem == OperatingSystem.macOs) {
serverParams['maxdiffrate'] = 0.01;
}
final String response = await _callScreenshotServer(serverParams) as String;
if (response == 'OK') {
// Pass

View File

@@ -1,186 +0,0 @@
// Copyright 2013 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:io' as io;
import 'package:image/image.dart';
/// How to compares pixels within the image.
///
/// Keep this enum in sync with the one defined in `golden_tester.dart`.
enum PixelComparison {
/// Allows minor blur and anti-aliasing differences by comparing a 3x3 grid
/// surrounding the pixel rather than direct 1:1 comparison.
fuzzy,
/// Compares one pixel at a time.
///
/// Anti-aliasing or blur will result in higher diff rate.
precise,
}
void main(List<String> args) {
final io.File fileA = io.File(args[0]);
final io.File fileB = io.File(args[1]);
final Image imageA = decodeNamedImage(fileA.readAsBytesSync(), 'a.png')!;
final Image imageB = decodeNamedImage(fileB.readAsBytesSync(), 'b.png')!;
final ImageDiff diff = ImageDiff(
golden: imageA, other: imageB, pixelComparison: PixelComparison.fuzzy);
print('Diff: ${(diff.rate * 100).toStringAsFixed(4)}%'); // ignore: avoid_print
}
/// This class encapsulates visually diffing an Image with any other.
/// Both images need to be the exact same size.
class ImageDiff {
/// Image diff constructor which requires two [Image]s to compare and
/// [PixelComparison] algorithm.
ImageDiff({
required this.golden,
required this.other,
required this.pixelComparison,
}) {
_computeDiff();
}
/// The image to match
final Image golden;
/// The image being compared
final Image other;
/// Algorithm used for comparing pixels.
final PixelComparison pixelComparison;
/// The output of the comparison
/// Pixels in the output image can have 3 different colors depending on the comparison
/// between golden pixels and other pixels:
/// * white: when both pixels are the same
/// * red: when a pixel is found in other, but not in golden
/// * green: when a pixel is found in golden, but not in other
late Image diff;
/// The ratio of wrong pixels to all pixels in golden (between 0 and 1)
/// This gets set to 1 (100% difference) when golden and other aren't the same size.
double get rate => _wrongPixels / _pixelCount;
int _pixelCount = 0;
int _wrongPixels = 0;
/// That would be the distance between black and white.
static final double _maxTheoreticalColorDistance = Color.distance(
<num>[255, 255, 255], // white
<num>[0, 0, 0], // black
false,
).toDouble();
// If the normalized color difference of a pixel is greater than this number,
// we consider it a wrong pixel.
static const double _kColorDistanceThreshold = 0.1;
final int _colorOk = Color.fromRgb(255, 255, 255);
final int _colorBadPixel = Color.fromRgb(255, 0, 0);
final int _colorExpectedPixel = Color.fromRgb(0, 255, 0);
/// Reads a pixel value out of [image] at [x] and [y].
///
/// If the pixel is out of bounds, reflects the [x] and [y] coordinates off
/// the border back into the image treating the border like a mirror.
static int _reflectedPixel(Image image, int x, int y) {
x = x.abs();
if (x == image.width) {
x = image.width - 2;
}
y = y.abs();
if (y == image.height) {
y = image.height - 2;
}
return image.getPixel(x, y);
}
static int _average(Iterable<int> values) {
return values.reduce((int a, int b) => a + b) ~/ values.length;
}
/// The value of the pixel at [x] and [y] coordinates.
///
/// If [pixelComparison] is [PixelComparison.precise], reads the RGB value of
/// the pixel.
///
/// If [pixelComparison] is [PixelComparison.fuzzy], reads the RGB values of
/// the average of the 3x3 box of pixels centered at [x] and [y].
List<int> _getPixelRgbForComparison(Image image, int x, int y) {
switch (pixelComparison) {
case PixelComparison.fuzzy:
final List<int> pixels = <int>[
_reflectedPixel(image, x - 1, y - 1),
_reflectedPixel(image, x - 1, y),
_reflectedPixel(image, x - 1, y + 1),
_reflectedPixel(image, x, y - 1),
_reflectedPixel(image, x, y),
_reflectedPixel(image, x, y + 1),
_reflectedPixel(image, x + 1, y - 1),
_reflectedPixel(image, x + 1, y),
_reflectedPixel(image, x + 1, y + 1),
];
return <int>[
_average(pixels.map((int p) => getRed(p))),
_average(pixels.map((int p) => getGreen(p))),
_average(pixels.map((int p) => getBlue(p))),
];
case PixelComparison.precise:
final int pixel = image.getPixel(x, y);
return <int>[
getRed(pixel),
getGreen(pixel),
getBlue(pixel),
];
}
}
void _computeDiff() {
final int goldenWidth = golden.width;
final int goldenHeight = golden.height;
_pixelCount = goldenWidth * goldenHeight;
diff = Image(goldenWidth, goldenHeight);
if (goldenWidth == other.width && goldenHeight == other.height) {
for (int y = 0; y < goldenHeight; y++) {
for (int x = 0; x < goldenWidth; x++) {
final bool isExactlySame =
golden.getPixel(x, y) == other.getPixel(x, y);
final List<int> goldenPixel = _getPixelRgbForComparison(golden, x, y);
final List<int> otherPixel = _getPixelRgbForComparison(other, x, y);
final double colorDistance =
Color.distance(goldenPixel, otherPixel, false) /
_maxTheoreticalColorDistance;
final bool isFuzzySame = colorDistance < _kColorDistanceThreshold;
if (isExactlySame || isFuzzySame) {
diff.setPixel(x, y, _colorOk);
} else {
final int goldenLuminance =
getLuminanceRgb(goldenPixel[0], goldenPixel[1], goldenPixel[2]);
final int otherLuminance =
getLuminanceRgb(otherPixel[0], otherPixel[1], otherPixel[2]);
if (goldenLuminance < otherLuminance) {
diff.setPixel(x, y, _colorExpectedPixel);
} else {
diff.setPixel(x, y, _colorBadPixel);
}
_wrongPixels++;
}
}
}
} else {
// Images are completely different resolutions. Bail out big time.
_wrongPixels = _pixelCount;
}
}
}
/// Returns text explaining pixel difference rate.
String getPrintableDiffFilesInfo(double diffRate, double maxRate) =>
'(${(diffRate * 100).toStringAsFixed(4)}% of pixels were different. '
'Maximum allowed rate is: ${(maxRate * 100).toStringAsFixed(4)}%).';

View File

@@ -9,7 +9,6 @@ import 'package:path/path.dart' as p;
import 'package:skia_gold_client/skia_gold_client.dart';
import 'environment.dart';
import 'goldens.dart';
/// Compares a screenshot taken through a test with its golden.
///
@@ -23,8 +22,6 @@ Future<String> compareImage(
Image screenshot,
bool doUpdateScreenshotGoldens,
String filename,
PixelComparison pixelComparison,
double maxDiffRateFailure,
SkiaGoldClient? skiaClient, {
required bool isCanvaskitTest,
}) async {
@@ -76,75 +73,8 @@ Future<String> compareImage(
return 'OK';
}
// Compare screenshots.
final ImageDiff diff = ImageDiff(
golden: golden,
other: screenshot,
pixelComparison: pixelComparison,
);
// TODO(mdebbar): Use the Gold tool to locally diff the golden.
if (diff.rate > 0) {
final String testResultsPath = environment.webUiTestResultsDirectory.path;
Directory(testResultsPath).createSync(recursive: true);
final String basename = p.basenameWithoutExtension(filename);
final File actualFile =
File(p.join(testResultsPath, '$basename.actual.png'));
actualFile.writeAsBytesSync(encodePng(screenshot), flush: true);
final File diffFile = File(p.join(testResultsPath, '$basename.diff.png'));
diffFile.writeAsBytesSync(encodePng(diff.diff), flush: true);
final File expectedFile =
File(p.join(testResultsPath, '$basename.expected.png'));
screenshotFile.copySync(expectedFile.path);
final File reportFile =
File(p.join(testResultsPath, '$basename.report.html'));
reportFile.writeAsStringSync('''
Golden file $filename did not match the image generated by the test.
<table>
<tr>
<th>Expected</th>
<th>Diff</th>
<th>Actual</th>
</tr>
<tr>
<td>
<img src="$basename.expected.png">
</td>
<td>
<img src="$basename.diff.png">
</td>
<td>
<img src="$basename.actual.png">
</td>
</tr>
</table>
''');
final StringBuffer message = StringBuffer();
message.writeln(
'Golden file $filename did not match the image generated by the test.');
message.writeln(getPrintableDiffFilesInfo(diff.rate, maxDiffRateFailure));
message.writeln('You can view the test report in your browser by opening:');
final String localReportPath = '$testResultsPath/$basename.report.html';
message.writeln(localReportPath);
message.writeln('Golden file: ${expectedFile.path}');
message.writeln('Actual file: ${actualFile.path}');
if (diff.rate < maxDiffRateFailure) {
// Issue a warning but do not fail the test.
print('WARNING:'); // ignore: avoid_print
print(message); // ignore: avoid_print
return 'OK';
} else {
// Fail test
return '$message';
}
}
return 'OK';
}