diff --git a/lib/game/game.dart b/lib/game/game.dart index d225da2..3008c37 100644 --- a/lib/game/game.dart +++ b/lib/game/game.dart @@ -39,17 +39,19 @@ class Game { } } - void endGame() { + void endGame(bool gameIsCanceled) { running = false; counterTimer.cancel(); - gameConsumer.gameStopped(); + if (!gameIsCanceled) { + gameConsumer.gameStopped(); + } } void countDown() { timeInSeconds--; gameConsumer.updateTime(); if (timeInSeconds == 0 && running) { - endGame(); + endGame(false); } } @@ -67,7 +69,8 @@ class Game { points = 0; board = Board(this); gameConsumer.updateStones(); - counterTimer = Timer.periodic(const Duration(seconds: 1), (_) => countDown()); + counterTimer = + Timer.periodic(const Duration(seconds: 1), (_) => countDown()); gameConsumer.updatePoints(); running = true; } diff --git a/lib/main.dart b/lib/main.dart index 72501d5..2269a5e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -72,6 +72,14 @@ class _MyHomePageState extends State implements IGameConsumer { }); } + @override + void dispose() { + if (game.isRunning()) { + game.endGame(true); + } + super.dispose(); + } + void _saveScore(String name, int score) async { final prefs = await SharedPreferences.getInstance(); _scoreboard.add({'name': name, 'score': score}); diff --git a/test/widget_test.dart b/test/widget_test.dart index 1024f07..282b9b1 100644 --- a/test/widget_test.dart +++ b/test/widget_test.dart @@ -1,30 +1,24 @@ -// This is a basic Flutter widget test. -// -// To perform an interaction with a widget in your test, use the WidgetTester -// utility in the flutter_test package. For example, you can send tap and scroll -// gestures. You can also use WidgetTester to find child widgets in the widget -// tree, read text, and verify that the values of widget properties are correct. - +// In the my_app_test.dart file +import 'package:bubbletwist/main.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:bubbletwist/main.dart'; - void main() { - testWidgets('Counter increments smoke test', (WidgetTester tester) async { - // Build our app and trigger a frame. - await tester.pumpWidget(const MyApp()); + group('Bubble-Twist Widget Tests', () { + testWidgets('Initial startup shows start button', + (WidgetTester tester) async { + await tester.pumpWidget(const MyApp()); + expect(find.text('Start'), findsOneWidget); + }); - // Verify that our counter starts at 0. - expect(find.text('0'), findsOneWidget); - expect(find.text('1'), findsNothing); + testWidgets('Game starts and displays grid on start button tap', + (WidgetTester tester) async { + await tester.pumpWidget(const MyApp()); + await tester.tap(find.text('Start')); + await tester.pump(); - // Tap the '+' icon and trigger a frame. - await tester.tap(find.byIcon(Icons.add)); - await tester.pump(); - - // Verify that our counter has incremented. - expect(find.text('0'), findsNothing); - expect(find.text('1'), findsOneWidget); + // Ensure the GridView is now visible + expect(find.byType(GridView), findsOneWidget); + }); }); }