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

test [nfc]: Add shorter name "testBinding" for TestZulipBinding.instance #271

Merged
merged 3 commits into from
Aug 15, 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
4 changes: 3 additions & 1 deletion lib/model/binding.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'package:device_info_plus/device_info_plus.dart' as device_info_plus;
import 'package:flutter/foundation.dart';
import 'package:url_launcher/url_launcher.dart' as url_launcher;
export 'package:url_launcher/url_launcher.dart' show LaunchMode;

import '../widgets/store.dart';
import 'store.dart';

/// Alias for [url_launcher.LaunchMode].
typedef UrlLaunchMode = url_launcher.LaunchMode;

/// A singleton service providing the app's data and use of Flutter plugins.
///
/// Only one instance will be constructed in the lifetime of the app,
Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,8 @@ void _launchUrl(BuildContext context, String urlString) async {
// opening a URL in a browser, where the system gives the user
// a bit of UI to choose which browser to use:
// https://github.com/zulip/zulip-flutter/issues/74#issuecomment-1514040730
TargetPlatform.android => LaunchMode.externalApplication,
_ => LaunchMode.platformDefault,
TargetPlatform.android => UrlLaunchMode.externalApplication,
_ => UrlLaunchMode.platformDefault,
},
);
} on PlatformException catch (e) {
Expand Down
9 changes: 7 additions & 2 deletions test/model/binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import 'package:zulip/model/store.dart';

import 'test_store.dart';

/// The binding instance used in tests.
///
/// This is the Zulip-specific analogue of [WidgetTester.binding].
TestZulipBinding get testBinding => TestZulipBinding.instance;

/// A concrete binding for use in the `flutter test` environment.
///
/// Tests that will mount a [GlobalStoreWidget], or invoke a Flutter plugin,
Expand Down Expand Up @@ -48,7 +53,7 @@ class TestZulipBinding extends ZulipBinding {
/// Tests that mount a [GlobalStoreWidget], or invoke a Flutter plugin,
/// or access [globalStore] or other methods on this class,
/// should clean up by calling this method. Typically this is done using
/// [addTearDown], like `addTearDown(TestZulipBinding.instance.reset);`.
/// [addTearDown], like `addTearDown(testBinding.reset);`.
void reset() {
_globalStore?.dispose();
_globalStore = null;
Expand Down Expand Up @@ -90,7 +95,7 @@ class TestZulipBinding extends ZulipBinding {
),
ErrorHint(
'Typically this is accomplished using [addTearDown], like '
'`addTearDown(TestZulipBinding.instance.reset);`.',
'`addTearDown(testBinding.reset);`.',
),
]);
}
Expand Down
20 changes: 10 additions & 10 deletions test/widgets/action_sheet_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ Future<void> setupToMessageActionSheet(WidgetTester tester, {
required Message message,
required Narrow narrow,
}) async {
addTearDown(TestZulipBinding.instance.reset);
addTearDown(testBinding.reset);

await TestZulipBinding.instance.globalStore.add(eg.selfAccount, eg.initialSnapshot());
final store = await TestZulipBinding.instance.globalStore.perAccount(eg.selfAccount.id);
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);
store.addUser(eg.user(userId: message.senderId));
if (message is StreamMessage) {
store.addStream(eg.stream(streamId: message.streamId));
Expand Down Expand Up @@ -136,7 +136,7 @@ void main() {
testWidgets('in stream narrow', (WidgetTester tester) async {
final message = eg.streamMessage();
await setupToMessageActionSheet(tester, message: message, narrow: StreamNarrow(message.streamId));
final store = await TestZulipBinding.instance.globalStore.perAccount(eg.selfAccount.id);
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);

final composeBoxController = findComposeBoxController(tester)!;
final contentController = composeBoxController.contentController;
Expand All @@ -154,7 +154,7 @@ void main() {
testWidgets('in topic narrow', (WidgetTester tester) async {
final message = eg.streamMessage();
await setupToMessageActionSheet(tester, message: message, narrow: TopicNarrow.ofMessage(message));
final store = await TestZulipBinding.instance.globalStore.perAccount(eg.selfAccount.id);
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);

final composeBoxController = findComposeBoxController(tester)!;
final contentController = composeBoxController.contentController;
Expand All @@ -173,7 +173,7 @@ void main() {
final message = eg.dmMessage(from: eg.selfUser, to: [eg.otherUser]);
await setupToMessageActionSheet(tester,
message: message, narrow: DmNarrow.ofMessage(message, selfUserId: eg.selfUser.userId));
final store = await TestZulipBinding.instance.globalStore.perAccount(eg.selfAccount.id);
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);

final composeBoxController = findComposeBoxController(tester)!;
final contentController = composeBoxController.contentController;
Expand All @@ -191,7 +191,7 @@ void main() {
testWidgets('request has an error', (WidgetTester tester) async {
final message = eg.streamMessage();
await setupToMessageActionSheet(tester, message: message, narrow: TopicNarrow.ofMessage(message));
final store = await TestZulipBinding.instance.globalStore.perAccount(eg.selfAccount.id);
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);

final composeBoxController = findComposeBoxController(tester)!;
final contentController = composeBoxController.contentController;
Expand Down Expand Up @@ -235,7 +235,7 @@ void main() {
});

tearDown(() async {
TestZulipBinding.instance.reset();
testBinding.reset();
});

Future<void> tapCopyButton(WidgetTester tester) async {
Expand All @@ -247,7 +247,7 @@ void main() {
testWidgets('success', (WidgetTester tester) async {
final message = eg.streamMessage();
await setupToMessageActionSheet(tester, message: message, narrow: TopicNarrow.ofMessage(message));
final store = await TestZulipBinding.instance.globalStore.perAccount(eg.selfAccount.id);
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);

prepareRawContentResponseSuccess(store, message: message, rawContent: 'Hello world');
await tapCopyButton(tester);
Expand All @@ -258,7 +258,7 @@ void main() {
testWidgets('request has an error', (WidgetTester tester) async {
final message = eg.streamMessage();
await setupToMessageActionSheet(tester, message: message, narrow: TopicNarrow.ofMessage(message));
final store = await TestZulipBinding.instance.globalStore.perAccount(eg.selfAccount.id);
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);

prepareRawContentResponseError(store);
await tapCopyButton(tester);
Expand Down
8 changes: 4 additions & 4 deletions test/widgets/autocomplete_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import '../model/test_store.dart';
Future<Finder> setupToComposeInput(WidgetTester tester, {
required List<User> users,
}) async {
addTearDown(TestZulipBinding.instance.reset);
await TestZulipBinding.instance.globalStore.add(eg.selfAccount, eg.initialSnapshot());
final store = await TestZulipBinding.instance.globalStore.perAccount(eg.selfAccount.id);
addTearDown(testBinding.reset);
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);
store.addUsers([eg.selfUser, eg.otherUser]);
store.addUsers(users);
final connection = store.connection as FakeApiConnection;
Expand Down Expand Up @@ -67,7 +67,7 @@ void main() {
final user2 = eg.user(userId: 2, fullName: 'User Two');
final user3 = eg.user(userId: 3, fullName: 'User Three');
final composeInputFinder = await setupToComposeInput(tester, users: [user1, user2, user3]);
final store = await TestZulipBinding.instance.globalStore.perAccount(eg.selfAccount.id);
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);

// Options are filtered correctly for query
// TODO(#226): Remove this extra edit when this bug is fixed.
Expand Down
8 changes: 4 additions & 4 deletions test/widgets/clipboard_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void main() {
});

tearDown(() async {
TestZulipBinding.instance.reset();
testBinding.reset();
});

group('copyWithPopup', () {
Expand Down Expand Up @@ -59,21 +59,21 @@ void main() {
}

testWidgets('iOS', (WidgetTester tester) async {
TestZulipBinding.instance.deviceInfoResult = IosDeviceInfo(systemVersion: '16.0');
testBinding.deviceInfoResult = IosDeviceInfo(systemVersion: '16.0');
await call(tester, text: 'asdf');
await checkClipboardText('asdf');
await checkSnackBar(tester, expected: true);
});

testWidgets('Android', (WidgetTester tester) async {
TestZulipBinding.instance.deviceInfoResult = AndroidDeviceInfo(sdkInt: 33);
testBinding.deviceInfoResult = AndroidDeviceInfo(sdkInt: 33);
await call(tester, text: 'asdf');
await checkClipboardText('asdf');
await checkSnackBar(tester, expected: false);
});

testWidgets('Android <13', (WidgetTester tester) async {
TestZulipBinding.instance.deviceInfoResult = AndroidDeviceInfo(sdkInt: 32);
testBinding.deviceInfoResult = AndroidDeviceInfo(sdkInt: 32);
await call(tester, text: 'asdf');
await checkClipboardText('asdf');
await checkSnackBar(tester, expected: true);
Expand Down
32 changes: 15 additions & 17 deletions test/widgets/content_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ void main() {
const fontSize = 48.0;

Future<void> prepareContent(WidgetTester tester, String html) async {
final globalStore = TestZulipBinding.instance.globalStore;
addTearDown(TestZulipBinding.instance.reset);
await globalStore.add(eg.selfAccount, eg.initialSnapshot());
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
addTearDown(testBinding.reset);

await tester.pumpWidget(GlobalStoreWidget(child: MaterialApp(
home: PerAccountStoreWidget(accountId: eg.selfAccount.id,
Expand All @@ -45,7 +44,7 @@ void main() {
await tester.tap(find.text('hello'));
final expectedMode = defaultTargetPlatform == TargetPlatform.android ?
LaunchMode.externalApplication : LaunchMode.platformDefault;
check(TestZulipBinding.instance.takeLaunchUrlCalls())
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: Uri.parse('https://example/'), mode: expectedMode));
}, variant: const TargetPlatformVariant({TargetPlatform.android, TargetPlatform.iOS}));

Expand All @@ -56,22 +55,22 @@ void main() {
.translate(fontSize/2, fontSize/2); // middle of first letter

await tester.tapAt(base.translate(5*fontSize, 0)); // "foo bXr baz"
check(TestZulipBinding.instance.takeLaunchUrlCalls()).isEmpty();
check(testBinding.takeLaunchUrlCalls()).isEmpty();

await tester.tapAt(base.translate(1*fontSize, 0)); // "fXo bar baz"
check(TestZulipBinding.instance.takeLaunchUrlCalls())
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: Uri.parse('https://a/'), mode: expectedModeAndroid));

await tester.tapAt(base.translate(9*fontSize, 0)); // "foo bar bXz"
check(TestZulipBinding.instance.takeLaunchUrlCalls())
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: Uri.parse('https://b/'), mode: expectedModeAndroid));
});

testWidgets('link nested in other spans', (tester) async {
await prepareContent(tester,
'<p><strong><em><a href="https://a/">word</a></em></strong></p>');
await tester.tap(find.text('word'));
check(TestZulipBinding.instance.takeLaunchUrlCalls())
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: Uri.parse('https://a/'), mode: expectedModeAndroid));
});

Expand All @@ -82,37 +81,37 @@ void main() {
.translate(fontSize/2, fontSize/2); // middle of first letter

await tester.tapAt(base.translate(1*fontSize, 0)); // "tXo words"
check(TestZulipBinding.instance.takeLaunchUrlCalls())
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: Uri.parse('https://a/'), mode: expectedModeAndroid));

await tester.tapAt(base.translate(6*fontSize, 0)); // "two woXds"
check(TestZulipBinding.instance.takeLaunchUrlCalls())
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: Uri.parse('https://a/'), mode: expectedModeAndroid));
});

testWidgets('relative links are resolved', (tester) async {
await prepareContent(tester,
'<p><a href="/a/b?c#d">word</a></p>');
await tester.tap(find.text('word'));
check(TestZulipBinding.instance.takeLaunchUrlCalls())
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: Uri.parse('${eg.realmUrl}a/b?c#d'), mode: expectedModeAndroid));
});

testWidgets('link inside HeadingNode', (tester) async {
await prepareContent(tester,
'<h6><a href="https://a/">word</a></h6>');
await tester.tap(find.text('word'));
check(TestZulipBinding.instance.takeLaunchUrlCalls())
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: Uri.parse('https://a/'), mode: expectedModeAndroid));
});

testWidgets('error dialog if invalid link', (tester) async {
await prepareContent(tester,
'<p><a href="file:///etc/bad">word</a></p>');
TestZulipBinding.instance.launchUrlResult = false;
testBinding.launchUrlResult = false;
await tester.tap(find.text('word'));
await tester.pump();
check(TestZulipBinding.instance.takeLaunchUrlCalls())
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: Uri.parse('file:///etc/bad'), mode: expectedModeAndroid));
checkErrorDialog(tester, expectedTitle: 'Unable to open link');
});
Expand All @@ -122,9 +121,8 @@ void main() {
final authHeaders = authHeader(email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey);

Future<String?> actualAuthHeader(WidgetTester tester, Uri src) async {
final globalStore = TestZulipBinding.instance.globalStore;
addTearDown(TestZulipBinding.instance.reset);
await globalStore.add(eg.selfAccount, eg.initialSnapshot());
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
addTearDown(testBinding.reset);

final httpClient = FakeImageHttpClient();
debugNetworkImageHttpClientProvider = () => httpClient;
Expand Down
10 changes: 5 additions & 5 deletions test/widgets/message_list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ void main() {
int? messageCount,
List<Message>? messages,
}) async {
addTearDown(TestZulipBinding.instance.reset);
addTearDown(testBinding.reset);
addTearDown(tester.view.resetPhysicalSize);

tester.view.physicalSize = const Size(600, 800);

await TestZulipBinding.instance.globalStore.add(eg.selfAccount, eg.initialSnapshot());
store = await TestZulipBinding.instance.globalStore.perAccount(eg.selfAccount.id);
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
store = await testBinding.globalStore.perAccount(eg.selfAccount.id);
connection = store.connection as FakeApiConnection;

// prepare message list data
Expand Down Expand Up @@ -181,7 +181,7 @@ void main() {

group('MessageWithSender', () {
testWidgets('Updates avatar on RealmUserUpdateEvent', (tester) async {
addTearDown(TestZulipBinding.instance.reset);
addTearDown(testBinding.reset);

// TODO recognize avatar more reliably:
// https://github.com/zulip/zulip-flutter/pull/246#discussion_r1282516308
Expand All @@ -202,7 +202,7 @@ void main() {
}

Future<void> handleNewAvatarEventAndPump(WidgetTester tester, String avatarUrl) async {
final store = await TestZulipBinding.instance.globalStore.perAccount(eg.selfAccount.id);
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);
store.handleEvent(RealmUserUpdateEvent(id: 1, userId: eg.selfUser.userId, avatarUrl: avatarUrl));
await tester.pump();
}
Expand Down
6 changes: 3 additions & 3 deletions test/widgets/recent_dm_conversations_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ Future<void> setupPage(WidgetTester tester, {
NavigatorObserver? navigatorObserver,
String? newNameForSelfUser,
}) async {
addTearDown(TestZulipBinding.instance.reset);
addTearDown(testBinding.reset);

await TestZulipBinding.instance.globalStore.add(eg.selfAccount, eg.initialSnapshot());
final store = await TestZulipBinding.instance.globalStore.perAccount(eg.selfAccount.id);
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);

store.addUser(eg.selfUser);
for (final user in users) {
Expand Down
Loading