Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate internal links in message contents #318

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/widgets/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import '../model/store.dart';
import 'code_block.dart';
import 'dialog.dart';
import 'lightbox.dart';
import 'message_list.dart';
import 'store.dart';
import 'text.dart';

Expand Down Expand Up @@ -668,6 +669,14 @@ void _launchUrl(BuildContext context, String urlString) async {
return;
}

final internalNarrow = parseInternalLink(url, store);
if (internalNarrow != null) {
Navigator.push(context,
MessageListPage.buildRoute(context: context,
narrow: internalNarrow));
return;
}

bool launched = false;
String? errorMessage;
try {
Expand Down
53 changes: 53 additions & 0 deletions test/widgets/content_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:zulip/api/core.dart';
import 'package:zulip/model/content.dart';
import 'package:zulip/model/narrow.dart';
import 'package:zulip/widgets/content.dart';
import 'package:zulip/widgets/message_list.dart';
import 'package:zulip/widgets/page.dart';
import 'package:zulip/widgets/store.dart';

import '../example_data.dart' as eg;
import '../model/binding.dart';
import '../test_images.dart';
import '../test_navigation.dart';
import 'dialog_checks.dart';
import 'message_list_checks.dart';
import 'page_checks.dart';

void main() {
TestZulipBinding.ensureInitialized();
Expand Down Expand Up @@ -158,6 +164,53 @@ void main() {
});
});

group('LinkNode on internal links', () {
Future<List<Route<dynamic>>> prepareContent(WidgetTester tester, {
required String html,
}) async {
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot(
streams: [eg.stream(streamId: 1, name: 'check')],
));
addTearDown(testBinding.reset);
final pushedRoutes = <Route<dynamic>>[];
final testNavObserver = TestNavigatorObserver()
..onPushed = (route, prevRoute) => pushedRoutes.add(route);
await tester.pumpWidget(GlobalStoreWidget(child: MaterialApp(
navigatorObservers: [testNavObserver],
home: PerAccountStoreWidget(accountId: eg.selfAccount.id,
child: BlockContentList(nodes: parseContent(html).nodes)))));
await tester.pump(); // global store
await tester.pump(); // per-account store
// `tester.pumpWidget` introduces an initial route, remove so
// consumers only have newly pushed routes.
assert(pushedRoutes.length == 1);
pushedRoutes.removeLast();
return pushedRoutes;
}

testWidgets('valid internal links are navigated to within app', (tester) async {
final pushedRoutes = await prepareContent(tester,
html: '<p><a href="/#narrow/stream/1-check">stream</a></p>');

await tester.tap(find.text('stream'));
check(testBinding.takeLaunchUrlCalls()).isEmpty();
check(pushedRoutes).single.isA<WidgetRoute>()
.page.isA<MessageListPage>().narrow.equals(const StreamNarrow(1));
});

testWidgets('invalid internal links are opened in browser', (tester) async {
// Link is invalid due to `topic` operator missing an operand.
final pushedRoutes = await prepareContent(tester,
html: '<p><a href="/#narrow/stream/1-check/topic">invalid</a></p>');

await tester.tap(find.text('invalid'));
final expectedUrl = eg.realmUrl.resolve('/#narrow/stream/1-check/topic');
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: expectedUrl, mode: LaunchMode.externalApplication));
check(pushedRoutes).isEmpty();
});
});

group('UnicodeEmoji', () {
Future<void> prepareContent(WidgetTester tester, String html) async {
await tester.pumpWidget(MaterialApp(home: BlockContentList(nodes: parseContent(html).nodes)));
Expand Down