Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
[url_launcher] Fix Link test (#3954)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stuartmorgan authored May 21, 2021
1 parent 6e245d2 commit ded320c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 41 deletions.
9 changes: 8 additions & 1 deletion packages/url_launcher/url_launcher/lib/src/link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<ByteData> Function(Object?, String) pushRouteToFrameworkFunction =
pushRouteNameToFramework;

/// A widget that renders a real link on the web, and uses WebViews in native
/// platforms to open links.
///
Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions packages/url_launcher/url_launcher/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 14 additions & 40 deletions packages/url_launcher/url_launcher/test/link_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<MethodCall> engineCalls = <MethodCall>[];
SystemChannels.navigation.setMockMethodCallHandler((MethodCall call) {
engineCalls.add(call);
return Future<void>.value();
});

// Intercept messages sent to the framework.
final List<MethodCall> frameworkCalls = <MethodCall>[];
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;

Expand All @@ -144,34 +127,25 @@ void main() {
},
));

engineCalls.clear();
frameworkCalls.clear();
bool frameworkCalled = false;
Future<ByteData> 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: <dynamic, dynamic>{
'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',
Expand Down

0 comments on commit ded320c

Please sign in to comment.