From b5ced0113f071381dcedb38677cb1a0b032fca0a Mon Sep 17 00:00:00 2001 From: jonathandaniels-vgv <102978796+jonathandaniels-vgv@users.noreply.github.com> Date: Thu, 28 Apr 2022 10:57:30 -0700 Subject: [PATCH] feat: how to play dialog is timed and manually dismissible (#251) * chore: autoclose how to play dialog after 3 seconds * test: added test for barrier dismiss on tap --- .../view/character_selection_page.dart | 33 ++++++++--- .../view/character_selection_page_test.dart | 55 ++++++++++++++++--- 2 files changed, 72 insertions(+), 16 deletions(-) diff --git a/lib/select_character/view/character_selection_page.dart b/lib/select_character/view/character_selection_page.dart index 83dc6ee6..8e652f72 100644 --- a/lib/select_character/view/character_selection_page.dart +++ b/lib/select_character/view/character_selection_page.dart @@ -1,5 +1,7 @@ // ignore_for_file: public_member_api_docs +import 'dart:async'; + import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:pinball/l10n/l10n.dart'; @@ -46,19 +48,34 @@ class CharacterSelectionView extends StatelessWidget { const SizedBox(height: 20), TextButton( onPressed: () { - Navigator.of(context).pop(); + late Timer timer; // TODO(arturplaczek): remove after merge StarBlocListener final height = MediaQuery.of(context).size.height * 0.5; + Navigator.of(context).pop(); showDialog( context: context, - builder: (_) => Center( - child: SizedBox( - height: height, - width: height * 1.4, - child: const HowToPlayDialog(), - ), - ), + builder: (context) { + timer = Timer( + const Duration(seconds: 3), + () { + Navigator.of(context).pop(); + }, + ); + return Center( + child: SizedBox( + height: height, + width: height * 1.4, + child: const HowToPlayDialog(), + ), + ); + }, + ).then( + (_) { + if (timer.isActive) { + timer.cancel(); + } + }, ); }, child: Text(l10n.start), diff --git a/test/select_character/view/character_selection_page_test.dart b/test/select_character/view/character_selection_page_test.dart index 0dda92d7..90ac509d 100644 --- a/test/select_character/view/character_selection_page_test.dart +++ b/test/select_character/view/character_selection_page_test.dart @@ -1,5 +1,7 @@ // ignore_for_file: prefer_const_constructors +import 'dart:async'; + import 'package:bloc_test/bloc_test.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -84,17 +86,54 @@ void main() { .called(1); }); - testWidgets('displays how to play dialog when start is tapped', + group('HowToPlayDialog', () { + testWidgets( + 'is displayed for 3 seconds when start is tapped', (tester) async { - await tester.pumpApp( - CharacterSelectionView(), - characterThemeCubit: characterThemeCubit, + await tester.pumpApp( + CharacterSelectionView(), + characterThemeCubit: characterThemeCubit, + ); + await tester.ensureVisible(find.byType(TextButton)); + await tester.tap(find.byType(TextButton)); + await tester.pumpAndSettle(); + expect(find.byType(HowToPlayDialog), findsOneWidget); + await tester.pump(Duration(seconds: 3)); + await tester.pumpAndSettle(); + expect(find.byType(HowToPlayDialog), findsNothing); + }, ); - await tester.ensureVisible(find.byType(TextButton)); - await tester.tap(find.byType(TextButton)); - await tester.pumpAndSettle(); - expect(find.byType(HowToPlayDialog), findsOneWidget); + testWidgets( + 'can be dismissed manually before 3 seconds have passed', + (tester) async { + await tester.pumpApp( + Scaffold( + body: Builder( + builder: (context) { + return ElevatedButton( + onPressed: () { + Navigator.of(context) + .push(CharacterSelectionDialog.route()); + }, + child: Text('Tap me'), + ); + }, + ), + ), + characterThemeCubit: characterThemeCubit, + ); + await tester.tap(find.text('Tap me')); + await tester.pumpAndSettle(); + await tester.ensureVisible(find.byType(TextButton)); + await tester.tap(find.byType(TextButton)); + await tester.pumpAndSettle(); + expect(find.byType(HowToPlayDialog), findsOneWidget); + await tester.tapAt(Offset(1, 1)); + await tester.pumpAndSettle(); + expect(find.byType(HowToPlayDialog), findsNothing); + }, + ); }); });