From ded320c67aa091fc31c862ea53b004e8d7a0d194 Mon Sep 17 00:00:00 2001 From: stuartmorgan Date: Thu, 20 May 2021 18:19:39 -0700 Subject: [PATCH] [url_launcher] Fix Link test (#3954) One of the Link tests relied on internal implementation details of url_launcher_platform_interface that have changed, so started failing when the new version of that plugin was published. This replaces that test with a test that the relevant url_launcher_platform_interface function is called, without asserting anything about how it works internally. --- .../url_launcher/lib/src/link.dart | 9 +++- .../url_launcher/url_launcher/pubspec.yaml | 1 + .../url_launcher/test/link_test.dart | 54 +++++-------------- 3 files changed, 23 insertions(+), 41 deletions(-) diff --git a/packages/url_launcher/url_launcher/lib/src/link.dart b/packages/url_launcher/url_launcher/lib/src/link.dart index 259ac8dd1c66..72d6e247c970 100644 --- a/packages/url_launcher/url_launcher/lib/src/link.dart +++ b/packages/url_launcher/url_launcher/lib/src/link.dart @@ -4,11 +4,18 @@ import 'dart:async'; +import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; +import 'package:meta/meta.dart'; import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher_platform_interface/link.dart'; import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart'; +/// The function used to push routes to the Flutter framework. +@visibleForTesting +Future Function(Object?, String) pushRouteToFrameworkFunction = + pushRouteNameToFramework; + /// A widget that renders a real link on the web, and uses WebViews in native /// platforms to open links. /// @@ -98,7 +105,7 @@ class DefaultLinkDelegate extends StatelessWidget { // case, we push it via Flutter's navigation system instead of letting the // browser handle it. final String routeName = link.uri.toString(); - await pushRouteNameToFramework(context, routeName); + await pushRouteToFrameworkFunction(context, routeName); return; } diff --git a/packages/url_launcher/url_launcher/pubspec.yaml b/packages/url_launcher/url_launcher/pubspec.yaml index b72a643a19fb..064240731b03 100644 --- a/packages/url_launcher/url_launcher/pubspec.yaml +++ b/packages/url_launcher/url_launcher/pubspec.yaml @@ -24,6 +24,7 @@ flutter: dependencies: flutter: sdk: flutter + meta: ^1.3.0 url_launcher_platform_interface: ^2.0.0 # The design on https://flutter.dev/go/federated-plugins was to leave # this constraint as "any". We cannot do it right now as it fails pub publish diff --git a/packages/url_launcher/url_launcher/test/link_test.dart b/packages/url_launcher/url_launcher/test/link_test.dart index 833797409c8a..6a598298bdf0 100644 --- a/packages/url_launcher/url_launcher/test/link_test.dart +++ b/packages/url_launcher/url_launcher/test/link_test.dart @@ -9,6 +9,7 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/mockito.dart'; import 'package:flutter/services.dart'; import 'package:url_launcher/link.dart'; +import 'package:url_launcher/src/link.dart'; import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart'; import 'mock_url_launcher_platform.dart'; @@ -108,26 +109,8 @@ void main() { expect(mock.launchCalled, isTrue); }); - testWidgets('sends navigation platform messages for internal route names', + testWidgets('pushes to framework for internal route names', (WidgetTester tester) async { - // Intercept messages sent to the engine. - final List engineCalls = []; - SystemChannels.navigation.setMockMethodCallHandler((MethodCall call) { - engineCalls.add(call); - return Future.value(); - }); - - // Intercept messages sent to the framework. - final List frameworkCalls = []; - window.onPlatformMessage = ( - String name, - ByteData? data, - PlatformMessageResponseCallback? callback, - ) { - frameworkCalls.add(_codec.decodeMethodCall(data)); - realOnPlatformMessage!(name, data, callback); - }; - final Uri uri = Uri.parse('/foo/bar'); FollowLink? followLink; @@ -144,34 +127,25 @@ void main() { }, )); - engineCalls.clear(); - frameworkCalls.clear(); + bool frameworkCalled = false; + Future Function(Object?, String) originalPushFunction = + pushRouteToFrameworkFunction; + pushRouteToFrameworkFunction = (Object? _, String __) { + frameworkCalled = true; + return Future.value(ByteData(0)); + }; + await followLink!(); // Shouldn't use url_launcher when uri is an internal route name. expect(mock.canLaunchCalled, isFalse); expect(mock.launchCalled, isFalse); - // A message should've been sent to the engine (by the Navigator, not by - // the Link widget). - // - // Even though this message isn't being sent by Link, we still want to - // have a test for it because we rely on it for Link to work correctly. - expect(engineCalls, hasLength(1)); - expect( - engineCalls.single, - isMethodCall('routeUpdated', arguments: { - 'previousRouteName': '/', - 'routeName': '/foo/bar', - }), - ); + // A route should have been pushed to the framework. + expect(frameworkCalled, true); - // Pushes route to the framework. - expect(frameworkCalls, hasLength(1)); - expect( - frameworkCalls.single, - isMethodCall('pushRoute', arguments: '/foo/bar'), - ); + // Restore the original function. + pushRouteToFrameworkFunction = originalPushFunction; }); testWidgets('sends router platform messages for internal route names',