Skip to content

Commit

Permalink
action_sheet tests: Cover "Share" button
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbobbe committed Oct 27, 2023
1 parent 4a99e3c commit f2a847a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
21 changes: 21 additions & 0 deletions test/test_share_plus.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:flutter/services.dart';

class MockSharePlus {
MockSharePlus();

/// The last string that `shareWithResult` was called with.
String? sharedString;

Future<Object?> handleMethodCall(MethodCall methodCall) async {
switch (methodCall.method) {
case 'shareWithResult':
// The method channel doesn't preserve Map<String, dynamic> as
// `arguments`'s type; logging runtimeType gives _Map<Object?, Object?>.
final arguments = methodCall.arguments as Map;
sharedString = arguments['text'] as String;
return 'some-success-response';
default:
throw UnimplementedError();
}
}
}
59 changes: 59 additions & 0 deletions test/widgets/action_sheet_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import 'package:zulip/widgets/compose_box.dart';
import 'package:zulip/widgets/content.dart';
import 'package:zulip/widgets/message_list.dart';
import 'package:zulip/widgets/store.dart';
import 'package:share_plus_platform_interface/method_channel/method_channel_share.dart';
import '../api/fake_api.dart';

import '../example_data.dart' as eg;
import '../flutter_checks.dart';
import '../model/binding.dart';
import '../model/test_store.dart';
import '../test_clipboard.dart';
import '../test_share_plus.dart';
import 'compose_box_checks.dart';
import 'dialog_checks.dart';

Expand Down Expand Up @@ -88,6 +90,63 @@ void main() {
(store.connection as FakeApiConnection).prepare(httpStatus: 400, json: fakeResponseJson);
}

group('ShareButton', () {
// Tests should call setupMockSharePlus.
setUp(() async {
TestZulipBinding.ensureInitialized();
TestWidgetsFlutterBinding.ensureInitialized();
});

MockSharePlus setupMockSharePlus() {
final mock = MockSharePlus();
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
MethodChannelShare.channel,
mock.handleMethodCall,
);
return mock;
}

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

Future<void> tapShareButton(WidgetTester tester) async {
await tester.ensureVisible(find.byIcon(Icons.adaptive.share, skipOffstage: false));
await tester.tap(find.byIcon(Icons.adaptive.share));
await tester.pump(); // [MenuItemButton.onPressed] called in a post-frame callback: flutter/flutter@e4a39fa2e
}

testWidgets('success', (WidgetTester tester) async {
final mockSharePlus = setupMockSharePlus();
final message = eg.streamMessage();
await setupToMessageActionSheet(tester, message: message, narrow: TopicNarrow.ofMessage(message));
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);

prepareRawContentResponseSuccess(store, message: message, rawContent: 'Hello world');
await tapShareButton(tester);
await tester.pump(Duration.zero);
check(mockSharePlus.sharedString).equals('Hello world');
});

testWidgets('request has an error', (WidgetTester tester) async {
final mockSharePlus = setupMockSharePlus();
final message = eg.streamMessage();
await setupToMessageActionSheet(tester, message: message, narrow: TopicNarrow.ofMessage(message));
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);

prepareRawContentResponseError(store);
await tapShareButton(tester);
await tester.pump(Duration.zero); // error arrives; error dialog shows

await tester.tap(find.byWidget(checkErrorDialog(tester,
expectedTitle: 'Sharing failed',
expectedMessage: 'That message does not seem to exist.',
)));

check(mockSharePlus.sharedString).isNull();
});
});

group('QuoteAndReplyButton', () {
ComposeBoxController? findComposeBoxController(WidgetTester tester) {
return tester.widget<ComposeBox>(find.byType(ComposeBox))
Expand Down

0 comments on commit f2a847a

Please sign in to comment.