11import 'package:checks/checks.dart' ;
2+ import 'package:flutter/cupertino.dart' ;
3+ import 'package:flutter/foundation.dart' ;
24import 'package:flutter/material.dart' ;
35import 'package:flutter_checks/flutter_checks.dart' ;
46import 'package:flutter_test/flutter_test.dart' ;
7+ import 'package:url_launcher/url_launcher.dart' ;
58import 'package:zulip/widgets/dialog.dart' ;
69
7- /// In a widget test, check that showErrorDialog was called with the right text.
10+ import '../model/binding.dart' ;
11+
12+ /// In a widget test, check that [showErrorDialog] was called with the right text.
813///
914/// Checks for an error dialog matching an expected title
1015/// and, optionally, matching an expected message. Fails if none is found.
@@ -14,27 +19,55 @@ import 'package:zulip/widgets/dialog.dart';
1419Widget checkErrorDialog (WidgetTester tester, {
1520 required String expectedTitle,
1621 String ? expectedMessage,
22+ Uri ? expectedLearnMoreButtonUrl,
1723}) {
18- final dialog = tester.widget <AlertDialog >(find.byType (AlertDialog ));
19- tester.widget (find.descendant (matchRoot: true ,
20- of: find.byWidget (dialog.title! ), matching: find.text (expectedTitle)));
21- if (expectedMessage != null ) {
22- tester.widget (find.descendant (matchRoot: true ,
23- of: find.byWidget (dialog.content! ), matching: find.text (expectedMessage)));
24- }
24+ switch (defaultTargetPlatform) {
25+ case TargetPlatform .android:
26+ case TargetPlatform .fuchsia:
27+ case TargetPlatform .linux:
28+ case TargetPlatform .windows:
29+ final dialog = tester.widget <AlertDialog >(find.bySubtype <AlertDialog >());
30+ tester.widget (find.descendant (matchRoot: true ,
31+ of: find.byWidget (dialog.title! ), matching: find.text (expectedTitle)));
32+ if (expectedMessage != null ) {
33+ tester.widget (find.descendant (matchRoot: true ,
34+ of: find.byWidget (dialog.content! ), matching: find.text (expectedMessage)));
35+ }
36+ if (expectedLearnMoreButtonUrl != null ) {
37+ check (testBinding.takeLaunchUrlCalls ()).single.equals ((
38+ url: expectedLearnMoreButtonUrl,
39+ mode: LaunchMode .inAppBrowserView));
40+ }
41+
42+ return tester.widget (find.descendant (of: find.byWidget (dialog),
43+ matching: find.widgetWithText (TextButton , 'OK' )));
2544
26- // TODO check "Learn more" button?
45+ case TargetPlatform .iOS:
46+ case TargetPlatform .macOS:
47+ final dialog = tester.widget <CupertinoAlertDialog >(find.byType (CupertinoAlertDialog ));
48+ tester.widget (find.descendant (matchRoot: true ,
49+ of: find.byWidget (dialog.title! ), matching: find.text (expectedTitle)));
50+ if (expectedMessage != null ) {
51+ tester.widget (find.descendant (matchRoot: true ,
52+ of: find.byWidget (dialog.content! ), matching: find.text (expectedMessage)));
53+ }
54+ if (expectedLearnMoreButtonUrl != null ) {
55+ check (testBinding.takeLaunchUrlCalls ()).single.equals ((
56+ url: expectedLearnMoreButtonUrl,
57+ mode: LaunchMode .externalApplication));
58+ }
2759
28- return tester.widget (
29- find. descendant (of : find.byWidget (dialog),
30- matching : find. widgetWithText ( TextButton , 'OK' )));
60+ return tester.widget (find. descendant (of : find. byWidget (dialog),
61+ matching : find.widgetWithText ( CupertinoDialogAction , 'OK' )));
62+ }
3163}
3264
33- // TODO(#996) update this to check for per-platform flavors of alert dialog
3465/// Checks that there is no dialog.
3566/// Fails if one is found.
3667void checkNoDialog (WidgetTester tester) {
37- check (find.byType (AlertDialog )).findsNothing ();
68+ check (find.byType (Dialog )).findsNothing ();
69+ check (find.bySubtype <AlertDialog >()).findsNothing ();
70+ check (find.byType (CupertinoAlertDialog )).findsNothing ();
3871}
3972
4073/// In a widget test, check that [showSuggestedActionDialog] was called
@@ -51,19 +84,35 @@ void checkNoDialog(WidgetTester tester) {
5184 required String expectedMessage,
5285 String ? expectedActionButtonText,
5386}) {
54- final dialog = tester.widget <AlertDialog >(find.byType (AlertDialog ));
55- tester.widget (find.descendant (matchRoot: true ,
56- of: find.byWidget (dialog.title! ), matching: find.text (expectedTitle)));
57- tester.widget (find.descendant (matchRoot: true ,
58- of: find.byWidget (dialog.content! ), matching: find.text (expectedMessage)));
87+ switch (defaultTargetPlatform) {
88+ case TargetPlatform .android:
89+ case TargetPlatform .fuchsia:
90+ case TargetPlatform .linux:
91+ case TargetPlatform .windows:
92+ final dialog = tester.widget <AlertDialog >(find.bySubtype <AlertDialog >());
93+ tester.widget (find.descendant (matchRoot: true ,
94+ of: find.byWidget (dialog.title! ), matching: find.text (expectedTitle)));
95+ tester.widget (find.descendant (matchRoot: true ,
96+ of: find.byWidget (dialog.content! ), matching: find.text (expectedMessage)));
5997
60- final actionButton = tester.widget (
61- find.descendant (of: find.byWidget (dialog),
62- matching: find.widgetWithText (TextButton , expectedActionButtonText ?? 'Continue' )));
98+ final actionButton = tester.widget (find.descendant (of: find.byWidget (dialog),
99+ matching: find.widgetWithText (TextButton , expectedActionButtonText ?? 'Continue' )));
100+ final cancelButton = tester.widget (find.descendant (of: find.byWidget (dialog),
101+ matching: find.widgetWithText (TextButton , 'Cancel' )));
102+ return (actionButton, cancelButton);
63103
64- final cancelButton = tester.widget (
65- find.descendant (of: find.byWidget (dialog),
66- matching: find.widgetWithText (TextButton , 'Cancel' )));
104+ case TargetPlatform .iOS:
105+ case TargetPlatform .macOS:
106+ final dialog = tester.widget <CupertinoAlertDialog >(find.byType (CupertinoAlertDialog ));
107+ tester.widget (find.descendant (matchRoot: true ,
108+ of: find.byWidget (dialog.title! ), matching: find.text (expectedTitle)));
109+ tester.widget (find.descendant (matchRoot: true ,
110+ of: find.byWidget (dialog.content! ), matching: find.text (expectedMessage)));
67111
68- return (actionButton, cancelButton);
112+ final actionButton = tester.widget (find.descendant (of: find.byWidget (dialog),
113+ matching: find.widgetWithText (CupertinoDialogAction , expectedActionButtonText ?? 'Continue' )));
114+ final cancelButton = tester.widget (find.descendant (of: find.byWidget (dialog),
115+ matching: find.widgetWithText (CupertinoDialogAction , 'Cancel' )));
116+ return (actionButton, cancelButton);
117+ }
69118}
0 commit comments