Skip to content

Support golden file image diffing in testWidgets #16859

@tvolkert

Description

@tvolkert

After #16758 lands, we'll ostensibly have the ability to capture a screenshot in a widget test running via flutter_tester and compare it to a golden file, as follows:

testWidgets('test', (WidgetTester tester) async {
  await tester.pumpWidget(new MyApp());

  // Get the bytes of the screenshot
  RenderRepaintBoundary repaintRenderBoundary = tester
      .allRenderObjects
      .firstWhere((RenderObject object) => object is RenderRepaintBoundary);
  ui.Image image = await repaintRenderBoundary.toImage();
  ByteData bytes = await image.toByteData(format: ui.ImageByteFormat.png);

  // Get the bytes of the golden file
  File golden = new ui.File('/path/to/golden.png');
  List<int> goldenBytes = await golden.readAsBytes();

  // Fail the test if the screenshot isn't within tolerance of golden
  ...
});

But doing so causes the following exception due to flutter_test's use of FakeAsync:

  TimeoutException after 0:00:05.000000: Test timed out after 5 seconds.
  dart:async  _startMicrotaskLoop

A hacked up attempt to call _fakeAsync.flushTimers() ends up yielding the same exception when we await that call.

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: tests"flutter test", flutter_test, or one of our tests

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions