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;
counterTimer.cancel();
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;
}

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 {
final prefs = await SharedPreferences.getInstance();
_scoreboard.add({'name': name, 'score': score});

View File

@ -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.
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);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
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();
// 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);
});
});
}