Wrote Widget test

main
Lachfrosch 2024-06-19 22:08:49 +02:00
parent f0e08bba26
commit ddb7b0002a
3 changed files with 31 additions and 26 deletions

View File

@ -39,17 +39,19 @@ class Game {
} }
} }
void endGame() { void endGame(bool gameIsCanceled) {
running = false; running = false;
counterTimer.cancel(); counterTimer.cancel();
if (!gameIsCanceled) {
gameConsumer.gameStopped(); gameConsumer.gameStopped();
} }
}
void countDown() { void countDown() {
timeInSeconds--; timeInSeconds--;
gameConsumer.updateTime(); gameConsumer.updateTime();
if (timeInSeconds == 0 && running) { if (timeInSeconds == 0 && running) {
endGame(); endGame(false);
} }
} }
@ -67,7 +69,8 @@ class Game {
points = 0; points = 0;
board = Board(this); board = Board(this);
gameConsumer.updateStones(); gameConsumer.updateStones();
counterTimer = Timer.periodic(const Duration(seconds: 1), (_) => countDown()); counterTimer =
Timer.periodic(const Duration(seconds: 1), (_) => countDown());
gameConsumer.updatePoints(); gameConsumer.updatePoints();
running = true; running = true;
} }

View File

@ -72,6 +72,14 @@ class _MyHomePageState extends State<MyHomePage> implements IGameConsumer {
}); });
} }
@override
void dispose() {
if (game.isRunning()) {
game.endGame(true);
}
super.dispose();
}
void _saveScore(String name, int score) async { void _saveScore(String name, int score) async {
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
_scoreboard.add({'name': name, 'score': score}); _scoreboard.add({'name': name, 'score': score});

View File

@ -1,30 +1,24 @@
// This is a basic Flutter widget test. // In the my_app_test.dart file
// import 'package:bubbletwist/main.dart';
// 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.
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:bubbletwist/main.dart';
void main() { void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async { group('Bubble-Twist Widget Tests', () {
// Build our app and trigger a frame. testWidgets('Initial startup shows start button',
(WidgetTester tester) async {
await tester.pumpWidget(const MyApp()); await tester.pumpWidget(const MyApp());
expect(find.text('Start'), findsOneWidget);
});
// Verify that our counter starts at 0. testWidgets('Game starts and displays grid on start button tap',
expect(find.text('0'), findsOneWidget); (WidgetTester tester) async {
expect(find.text('1'), findsNothing); await tester.pumpWidget(const MyApp());
await tester.tap(find.text('Start'));
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump(); await tester.pump();
// Verify that our counter has incremented. // Ensure the GridView is now visible
expect(find.text('0'), findsNothing); expect(find.byType(GridView), findsOneWidget);
expect(find.text('1'), findsOneWidget); });
}); });
} }