Skip to content

Commit 609b490

Browse files
committed
dialog_checks: Make checkNoDialog describe the dialog it finds
Example output on failure: 00:05 +66: message action sheet ReactionButtons +1 request has an error; useLegacy: false ══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════ The following TestFailure was thrown running a test: Found dialog(s) when none were expected: Dialog: title: Adding reaction failed content: Invalid message(s) When the exception was thrown, this was the stack: <asynchronous suspension> <asynchronous suspension> <asynchronous suspension> <asynchronous suspension> (elided one frame from package:stack_trace) The test description was: +1 request has an error; useLegacy: false ════════════════════════════════════════════════════════════════════════════════════════════════════ 00:05 +66 -1: message action sheet ReactionButtons +1 request has an error; useLegacy: false [E] Test failed. See exception logs above. The test description was: +1 request has an error; useLegacy: false
1 parent 7e98ff1 commit 609b490

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

test/widgets/dialog_checks.dart

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ import 'package:zulip/widgets/dialog.dart';
1313
///
1414
/// On success, returns the widget's "OK" button.
1515
/// Dismiss the dialog by calling `tester.tap(find.byWidget(okButton))`.
16+
///
17+
/// See also:
18+
/// - [checkNoDialog]
1619
Widget checkErrorDialog(WidgetTester tester, {
1720
required String expectedTitle,
1821
String? expectedMessage,
1922
}) {
23+
// TODO if a dialog was found but it doesn't match expectations,
24+
// show its details; see checkNoDialog for how to do that
2025
switch (defaultTargetPlatform) {
2126
case TargetPlatform.android:
2227
case TargetPlatform.fuchsia:
@@ -46,12 +51,47 @@ Widget checkErrorDialog(WidgetTester tester, {
4651
}
4752
}
4853

49-
/// Checks that there is no dialog.
50-
/// Fails if one is found.
54+
55+
/// In a widget test, check that there aren't any alert dialogs.
56+
///
57+
/// See also:
58+
/// - [checkErrorDialog]
5159
void checkNoDialog(WidgetTester tester) {
60+
final List<Widget> alertDialogs = [
61+
...tester.widgetList(find.bySubtype<AlertDialog>()),
62+
...tester.widgetList(find.byType(CupertinoAlertDialog)),
63+
];
64+
65+
if (alertDialogs.isNotEmpty) {
66+
final message = StringBuffer()..write('Found dialog(s) when none were expected:\n');
67+
for (final alertDialog in alertDialogs) {
68+
final (title, content) = switch (alertDialog) {
69+
AlertDialog() => (alertDialog.title, alertDialog.content),
70+
CupertinoAlertDialog() => (alertDialog.title, alertDialog.content),
71+
_ => throw UnimplementedError(),
72+
};
73+
74+
message.write('Dialog:\n'
75+
' title: ${title is Text ? title.data : title.toString()}\n');
76+
77+
if (content != null) {
78+
final contentTexts = tester.widgetList<Text>(find.descendant(
79+
matchRoot: true,
80+
of: find.byWidget(content),
81+
matching: find.byType(Text)));
82+
message.write(' content: ');
83+
if (contentTexts.isNotEmpty) {
84+
message.write(contentTexts.map((t) => t.data).join('\n '));
85+
} else {
86+
// (Could show more detail here as necessary.)
87+
message.write(content.toString());
88+
}
89+
}
90+
}
91+
throw TestFailure(message.toString());
92+
}
93+
5294
check(find.byType(Dialog)).findsNothing();
53-
check(find.bySubtype<AlertDialog>()).findsNothing();
54-
check(find.byType(CupertinoAlertDialog)).findsNothing();
5595
}
5696

5797
/// In a widget test, check that [showSuggestedActionDialog] was called

0 commit comments

Comments
 (0)