openDeepLink function

Future<void> openDeepLink(
  1. WidgetTester tester,
  2. String link
)

Delivers link to the app the way the operating system does when the user taps a deep link, then waits for navigation to settle.

link may be a full URL such as https://example.com/users/42 or myapp://open/users/42, or a plain location such as /users/42. Only the path, query and fragment reach the router, as on a real device.

The app must already be pumped, for example with pumpRouter.

Implementation

Future<void> openDeepLink(WidgetTester tester, String link) async {
  final uri = Uri.parse(link);
  final location = Uri(
    path: uri.path.isEmpty ? '/' : uri.path,
    query: uri.hasQuery ? uri.query : null,
    fragment: uri.hasFragment ? uri.fragment : null,
  ).toString();

  final message = const JSONMethodCodec().encodeMethodCall(
    MethodCall('pushRouteInformation', {'location': location, 'state': null}),
  );
  await tester.binding.defaultBinaryMessenger.handlePlatformMessage(
    SystemChannels.navigation.name,
    message,
    (_) {},
  );
  await tester.pumpAndSettle();
}