Skip to content

Commit a5824e4

Browse files
PIG208gnprice
authored andcommitted
content [nfc]: Follow browser preference setting when opening links
This preserves the previous behavior (external application on iOS, and in-app browser on Android) when browserPreference is `null`. There is no way for the client to update the setting for now, hence the NFC. The helper could have been a static method instead of an extension, similar to ThemeSetting.displayName, but that will be a lot more verbose for the caller. Signed-off-by: Zixuan James Li <[email protected]>
1 parent 5325377 commit a5824e4

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

lib/model/settings.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ enum BrowserPreference {
5050

5151
extension GlobalSettingsHelpers on GlobalSettingsData {
5252
BrowserPreference get effectiveBrowserPreference {
53+
if (browserPreference != null) return browserPreference!;
5354
return switch (defaultTargetPlatform) {
5455
// On iOS we prefer UrlLaunchMode.externalApplication because (for
5556
// HTTP URLs) UrlLaunchMode.platformDefault uses SFSafariViewController,

test/model/settings_test.dart

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:checks/checks.dart';
22
import 'package:flutter/foundation.dart';
33
import 'package:flutter_test/flutter_test.dart';
44
import 'package:zulip/model/binding.dart';
5+
import 'package:zulip/model/settings.dart';
56

67
import '../example_data.dart' as eg;
78
import 'store_checks.dart';
@@ -12,20 +13,41 @@ void main() {
1213
final nonHttpLink = Uri.parse('mailto:[email protected]');
1314

1415
group('getUrlLaunchMode', () {
15-
testAndroidIos('use our per-platform defaults for HTTP links', () {
16+
testAndroidIos('globalSettings.browserPreference is null; use our per-platform defaults for HTTP links', () {
1617
final globalStore = eg.globalStore(globalSettings: eg.globalSettings(
1718
browserPreference: null));
1819
check(globalStore).globalSettings.getUrlLaunchMode(httpLink).equals(
1920
defaultTargetPlatform == TargetPlatform.iOS
2021
? UrlLaunchMode.externalApplication : UrlLaunchMode.inAppBrowserView);
2122
});
2223

23-
testAndroidIos('use our per-platform defaults for non-HTTP links', () {
24+
testAndroidIos('globalSettings.browserPreference is null; use our per-platform defaults for non-HTTP links', () {
2425
final globalStore = eg.globalStore(globalSettings: eg.globalSettings(
2526
browserPreference: null));
2627
check(globalStore).globalSettings.getUrlLaunchMode(nonHttpLink).equals(
2728
defaultTargetPlatform == TargetPlatform.android
2829
? UrlLaunchMode.platformDefault : UrlLaunchMode.externalApplication);
2930
});
31+
32+
testAndroidIos('globalSettings.browserPreference is inApp; follow the preference for http links', () {
33+
final globalStore = eg.globalStore(globalSettings: eg.globalSettings(
34+
browserPreference: BrowserPreference.inApp));
35+
check(globalStore).globalSettings.getUrlLaunchMode(httpLink).equals(
36+
UrlLaunchMode.inAppBrowserView);
37+
});
38+
39+
testAndroidIos('globalSettings.browserPreference is inApp; use platform default for non-http links', () {
40+
final globalStore = eg.globalStore(globalSettings: eg.globalSettings(
41+
browserPreference: BrowserPreference.inApp));
42+
check(globalStore).globalSettings.getUrlLaunchMode(nonHttpLink).equals(
43+
UrlLaunchMode.platformDefault);
44+
});
45+
46+
testAndroidIos('globalSettings.browserPreference is external; follow the preference', () {
47+
final globalStore = eg.globalStore(globalSettings: eg.globalSettings(
48+
browserPreference: BrowserPreference.external));
49+
check(globalStore).globalSettings.getUrlLaunchMode(httpLink).equals(
50+
UrlLaunchMode.externalApplication);
51+
});
3052
});
3153
}

test/widgets/content_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:url_launcher/url_launcher.dart';
99
import 'package:zulip/api/core.dart';
1010
import 'package:zulip/model/content.dart';
1111
import 'package:zulip/model/narrow.dart';
12+
import 'package:zulip/model/settings.dart';
1213
import 'package:zulip/model/store.dart';
1314
import 'package:zulip/widgets/content.dart';
1415
import 'package:zulip/widgets/icons.dart';
@@ -798,6 +799,18 @@ void main() {
798799
.single.equals((url: Uri.parse('https://example/'), mode: expectedLaunchMode));
799800
}, variant: const TargetPlatformVariant({TargetPlatform.android, TargetPlatform.iOS}));
800801

802+
testWidgets('follow browser preference setting to open URL', (tester) async {
803+
await testBinding.globalStore.updateGlobalSettings(
804+
eg.globalSettings(
805+
browserPreference: BrowserPreference.inApp).toCompanion(false));
806+
await prepare(tester,
807+
'<p><a href="https://example/">hello</a></p>');
808+
809+
await tapText(tester, find.text('hello'));
810+
check(testBinding.takeLaunchUrlCalls()).single.equals((
811+
url: Uri.parse('https://example/'), mode: LaunchMode.inAppBrowserView));
812+
}, variant: const TargetPlatformVariant({TargetPlatform.android, TargetPlatform.iOS}));
813+
801814
testWidgets('multiple links in paragraph', (tester) async {
802815
const fontSize = kBaseFontSize;
803816

0 commit comments

Comments
 (0)