1
1
import 'package:checks/checks.dart' ;
2
2
import 'package:flutter/material.dart' ;
3
3
import 'package:flutter_test/flutter_test.dart' ;
4
+ import 'package:zulip/log.dart' ;
4
5
import 'package:zulip/model/database.dart' ;
5
6
import 'package:zulip/widgets/app.dart' ;
6
7
import 'package:zulip/widgets/inbox.dart' ;
@@ -10,6 +11,7 @@ import '../example_data.dart' as eg;
10
11
import '../flutter_checks.dart' ;
11
12
import '../model/binding.dart' ;
12
13
import '../test_navigation.dart' ;
14
+ import 'dialog_checks.dart' ;
13
15
import 'page_checks.dart' ;
14
16
import 'test_app.dart' ;
15
17
@@ -169,5 +171,121 @@ void main() {
169
171
check (ZulipApp .scaffoldMessenger).isNotNull ();
170
172
check (ZulipApp .ready).value.isTrue ();
171
173
});
174
+
175
+ Finder findSnackBarByText (String text) => find.descendant (
176
+ of: find.byType (SnackBar ),
177
+ matching: find.text (text));
178
+
179
+ testWidgets ('reportErrorToUserBriefly' , (tester) async {
180
+ addTearDown (testBinding.reset);
181
+ await tester.pumpWidget (const ZulipApp ());
182
+ const message = 'test error message' ;
183
+
184
+ // Prior to app startup, reportErrorToUserBriefly only logs.
185
+ reportErrorToUserBriefly (message);
186
+ check (ZulipApp .ready).value.isFalse ();
187
+ await tester.pump ();
188
+ check (findSnackBarByText (message).evaluate ()).isEmpty ();
189
+
190
+ check (ZulipApp .ready).value.isTrue ();
191
+ // After app startup, reportErrorToUserBriefly displays a SnackBar.
192
+ reportErrorToUserBriefly (message);
193
+ await tester.pump ();
194
+ check (findSnackBarByText (message).evaluate ()).single;
195
+ check (find.text ('Details' ).evaluate ()).isEmpty ();
196
+ });
197
+
198
+ testWidgets ('reportErrorToUserBriefly with details' , (tester) async {
199
+ addTearDown (testBinding.reset);
200
+ await tester.pumpWidget (const ZulipApp ());
201
+ const message = 'test error message' ;
202
+ const details = 'error details' ;
203
+
204
+ // Prior to app startup, reportErrorToUserBriefly only logs.
205
+ reportErrorToUserBriefly (message, details: details);
206
+ check (ZulipApp .ready).value.isFalse ();
207
+ await tester.pump ();
208
+ check (findSnackBarByText (message).evaluate ()).isEmpty ();
209
+ check (find.byType (AlertDialog ).evaluate ()).isEmpty ();
210
+
211
+ check (ZulipApp .ready).value.isTrue ();
212
+ // After app startup, reportErrorToUserBriefly displays a SnackBar.
213
+ reportErrorToUserBriefly (message, details: details);
214
+ await tester.pumpAndSettle ();
215
+ check (findSnackBarByText (message).evaluate ()).single;
216
+ check (find.byType (AlertDialog ).evaluate ()).isEmpty ();
217
+
218
+ // Open the error details dialog.
219
+ await tester.tap (find.text ('Details' ));
220
+ await tester.pumpAndSettle ();
221
+ check (findSnackBarByText (message).evaluate ()).isEmpty ();
222
+ checkErrorDialog (tester, expectedTitle: 'Error' , expectedMessage: details);
223
+ });
224
+
225
+ Future <void > prepareSnackBarWithDetails (WidgetTester tester, String message, String details) async {
226
+ addTearDown (testBinding.reset);
227
+ await tester.pumpWidget (const ZulipApp ());
228
+ await tester.pump ();
229
+ check (ZulipApp .ready).value.isTrue ();
230
+
231
+ reportErrorToUserBriefly (message, details: details);
232
+ await tester.pumpAndSettle ();
233
+ check (findSnackBarByText (message).evaluate ()).single;
234
+ }
235
+
236
+ testWidgets ('reportErrorToUser dismissing SnackBar' , (tester) async {
237
+ const message = 'test error message' ;
238
+ const details = 'error details' ;
239
+ await prepareSnackBarWithDetails (tester, message, details);
240
+
241
+ // Dismissing the SnackBar.
242
+ reportErrorToUserBriefly (null );
243
+ await tester.pumpAndSettle ();
244
+ check (findSnackBarByText (message).evaluate ()).isEmpty ();
245
+
246
+ // Verify that the SnackBar would otherwise stay when not dismissed.
247
+ reportErrorToUserBriefly (message, details: details);
248
+ await tester.pumpAndSettle ();
249
+ check (findSnackBarByText (message).evaluate ()).single;
250
+ await tester.pumpAndSettle ();
251
+ check (findSnackBarByText (message).evaluate ()).single;
252
+ });
253
+
254
+ testWidgets ('reportErrorToUserBriefly(null) does not dismiss dialog' , (tester) async {
255
+ const message = 'test error message' ;
256
+ const details = 'error details' ;
257
+ await prepareSnackBarWithDetails (tester, message, details);
258
+
259
+ // Open the error details dialog.
260
+ await tester.tap (find.text ('Details' ));
261
+ await tester.pumpAndSettle ();
262
+ check (findSnackBarByText (message).evaluate ()).isEmpty ();
263
+ checkErrorDialog (tester, expectedTitle: 'Error' , expectedMessage: details);
264
+
265
+ // The dialog should not get dismissed.
266
+ reportErrorToUserBriefly (null );
267
+ await tester.pumpAndSettle ();
268
+ checkErrorDialog (tester, expectedTitle: 'Error' , expectedMessage: details);
269
+ });
270
+
271
+ testWidgets ('reportErrorToUserBriefly(null) does not dismiss unrelated SnackBar' , (tester) async {
272
+ const message = 'test error message' ;
273
+ const details = 'error details' ;
274
+ await prepareSnackBarWithDetails (tester, message, details);
275
+
276
+ // Dismissing the SnackBar.
277
+ reportErrorToUserBriefly (null );
278
+ await tester.pumpAndSettle ();
279
+ check (findSnackBarByText (message).evaluate ()).isEmpty ();
280
+
281
+ // Unrelated SnackBars should not be dismissed.
282
+ ZulipApp .scaffoldMessenger! .showSnackBar (
283
+ const SnackBar (content: Text ('unrelated' )));
284
+ await tester.pumpAndSettle ();
285
+ check (findSnackBarByText ('unrelated' ).evaluate ()).single;
286
+ reportErrorToUserBriefly (null );
287
+ await tester.pumpAndSettle ();
288
+ check (findSnackBarByText ('unrelated' ).evaluate ()).single;
289
+ });
172
290
});
173
291
}
0 commit comments