androidflutterflutter-integration-test

Simulate Android in integration tests


I'm coding automatic screenshots for my app using Flutter's integration_test + golden tests.

When I run flutter run I usually press o a couple of times. It changes look of my app. With this I can select Android UI emulation, and my app looks like Android app (with fonts and spacing like on Android app) even though I run my app on Linux without any emulator.

The problem I'm facing is: I want my golden files to look like on they have been taken on Android device. Is there a way to do that like in example below?

This is the difference between not simulating (1 picture), and simulating Android (2 picture):

Without simulating With simulating

I did not found any documentation on this problem, sadly.


Solution

  • I found solution.

    You just need to insert this line at the start of each of your tests:

    debugDefaultTargetPlatformOverride = TargetPlatform.android;

    And, at the end of the tests:

    debugDefaultTargetPlatformOverride = null;

    Last line needed in order to avoid this error: The value of a foundation debug variable was changed by the test.

    My screenshot function now looks like this:

    void makeScreenshot(
        Widget w, String testName, String fileName, Function(WidgetTester) later) {
      testWidgets(testName, (WidgetTester tester) async {
        tester.view.physicalSize = const Size(1080 / 2, 1920 / 2);
        debugDefaultTargetPlatformOverride = TargetPlatform.android;
    
        await tester.pumpWidget(w);
    
        // Without this line the whole method does not work.
        // main.png is just a blank screenshot.
        await tester.pumpAndSettle();
    
        await later(tester);
    
        await expectLater(
          find.byType(SudokuApp),
          matchesGoldenFile('screenshots/$fileName.png'),
        );
        debugDefaultTargetPlatformOverride = null;
      });
    }