flutterflutter-test

How to test flutter url_launcher that email app opens?


I do have a Snackbar with a SnackbarAction which should open the default email app with a default subject and body on tap. I am wondering if there is somehow the possibility to verify if this really happens with some unit tests.

My Snackbar code looks like this:

SnackBar get snackbar =>
      SnackBar(
          content: Text(message),
          action: SnackBarAction(
              key: const Key('ErrorSnackbarAction'),
              label: AppLocalizations
                  .of(_context)
                  .report,
              onPressed: () async => await launch('mailto:test@test.com?subject=TestSubject&body=TestBody')));

I am already verifying the appearance which works fine:

group('ErrorSnackbar', () {
  testWidgets('appearance test', (WidgetTester tester) async {
    await tester.pumpWidget(_generateSnackbarApp());

    await _showSnackbar(tester);

    expect(find.text(userMessage), findsOneWidget);
    expect(find.byWidgetPredicate((Widget widget) =>
    widget is SnackBarAction && widget.label == 'Report'), findsOneWidget);
  });

  testWidgets('error report test', (WidgetTester tester) async {
    await tester.pumpWidget(_generateSnackbarApp());

    await _showSnackbar(tester);

    tester.tap(find.byKey(errorSnackbarAction));
    await tester.pump();

   // how to verify that the default email app was opened
  // with expected subject and body?

  });
});

Solution

  • Short answer: You can't.

    The launch with mailto is handled by the OS of the device and is out of context of the flutter app. As the flutter test package focuses on the flutter app, what happens on the OS is out of reach.