From 52ec92b5660fd5ce37b11b699ae8317fa794ff8c Mon Sep 17 00:00:00 2001 From: "k.mannweiler" <2012491@stud.hs-mannheim.de> Date: Mon, 6 Mar 2023 23:46:49 +0100 Subject: [PATCH] Added Integrationtests --- lib/providers/tasks_provider.dart | 2 +- lib/widgets/view_form/mood_form.dart | 4 +- lib/widgets/view_form/sleep_view.dart | 2 +- test/integration_tests/mood_form_test.dart | 41 +++++++++++++ test/integration_tests/mood_view_test.dart | 49 ++++++++++++++++ test/integration_tests/relapse_form_test.dart | 57 +++++++++++++++++++ test/integration_tests/relapse_view_test.dart | 52 +++++++++++++++++ test/integration_tests/sleep_form_test.dart | 42 ++++++++++++++ test/integration_tests/sleep_view_test.dart | 53 +++++++++++++++++ 9 files changed, 298 insertions(+), 4 deletions(-) create mode 100644 test/integration_tests/mood_form_test.dart create mode 100644 test/integration_tests/mood_view_test.dart create mode 100644 test/integration_tests/relapse_form_test.dart create mode 100644 test/integration_tests/relapse_view_test.dart create mode 100644 test/integration_tests/sleep_form_test.dart create mode 100644 test/integration_tests/sleep_view_test.dart diff --git a/lib/providers/tasks_provider.dart b/lib/providers/tasks_provider.dart index 2fed9b8..17b2aa5 100644 --- a/lib/providers/tasks_provider.dart +++ b/lib/providers/tasks_provider.dart @@ -31,7 +31,7 @@ class TasksProvider extends ChangeNotifier { notifyListeners(); } - void initHistories() async { + Future initHistories() async { moodHistory = await globals.databaseService.getMoodRecords(); sleepHistory = await globals.databaseService.getSleepRecords(); workoutHistory = await globals.databaseService.getWorkoutRecords(); diff --git a/lib/widgets/view_form/mood_form.dart b/lib/widgets/view_form/mood_form.dart index 588d542..d287767 100644 --- a/lib/widgets/view_form/mood_form.dart +++ b/lib/widgets/view_form/mood_form.dart @@ -13,8 +13,8 @@ class MoodForm extends StatelessWidget { @override Widget build(BuildContext context) { - var inputModel = context.watch(); - var tasksModel = context.watch(); + InputProvider inputModel = context.watch(); + TasksProvider tasksModel = context.watch(); return ListView( padding: const EdgeInsets.fromLTRB(10, 10, 10, 10), children: [ diff --git a/lib/widgets/view_form/sleep_view.dart b/lib/widgets/view_form/sleep_view.dart index e53837c..add4e8f 100644 --- a/lib/widgets/view_form/sleep_view.dart +++ b/lib/widgets/view_form/sleep_view.dart @@ -27,7 +27,7 @@ class SleepView extends StatelessWidget { history: tasksModel.sleepHistory, dateSelector: (Sleep sleep) => sleep.date, entryDataSelector: (Sleep sleep) => - '${sleep.sleepDuration.hour}:${sleep.sleepDuration.minute}', + '${sleep.sleepDuration.hour}:${sleep.sleepDuration.minute.toString().padLeft(2, "0")}', entryCommentSelector: (Sleep sleep) => 'Kommentar: ${sleep.comment}', icon: Icons.bedtime_outlined, ) diff --git a/test/integration_tests/mood_form_test.dart b/test/integration_tests/mood_form_test.dart new file mode 100644 index 0000000..c723e0a --- /dev/null +++ b/test/integration_tests/mood_form_test.dart @@ -0,0 +1,41 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:provider/provider.dart'; +import 'package:smoke_cess_app/providers/input_provider.dart'; +import 'package:smoke_cess_app/providers/page_provider.dart'; +import 'package:smoke_cess_app/providers/tasks_provider.dart'; +import 'package:smoke_cess_app/widgets/view_form/mood_form.dart'; +import 'package:smoke_cess_app/globals.dart' as globals; +import '../mock/db_mock.dart'; + +void main() { + globals.databaseService = DatabaseMock(); + testWidgets('Mood Form saves correctly', (WidgetTester tester) async { + const String testText = 'Its a test'; + // Create an instance of the Providers and add it to the widget tree + final inputProvider = InputProvider(); + final tasksProvider = TasksProvider(null); + final pageProvider = PageProvider(); + await tester.pumpWidget( + MultiProvider( + providers: [ + ChangeNotifierProvider.value(value: inputProvider), + ChangeNotifierProvider.value(value: tasksProvider), + ChangeNotifierProvider.value(value: pageProvider), + ], + child: const MaterialApp( + home: Scaffold( + body: MoodForm(), + ), + ), + ), + ); + + await tester.enterText(find.byType(TextField), testText); + await tester.tap(find.byType(ElevatedButton)); + await tester.pump(); + final result = await globals.databaseService.getMoodRecords(); + expect(result.last.comment, testText); + expect(result.last.moodValue, 50); + }); +} diff --git a/test/integration_tests/mood_view_test.dart b/test/integration_tests/mood_view_test.dart new file mode 100644 index 0000000..00bb61b --- /dev/null +++ b/test/integration_tests/mood_view_test.dart @@ -0,0 +1,49 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:intl/date_symbol_data_local.dart'; +import 'package:provider/provider.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:smoke_cess_app/models/mood.dart'; +import 'package:smoke_cess_app/providers/settings_provider.dart'; +import 'package:smoke_cess_app/providers/tasks_provider.dart'; +import 'package:smoke_cess_app/globals.dart' as globals; +import 'package:smoke_cess_app/widgets/view_form/mood_view.dart'; +import '../mock/db_mock.dart'; +import '../mock/settings_mock.dart'; + +void main() { + globals.databaseService = DatabaseMock(); + TestWidgetsFlutterBinding.ensureInitialized(); + SharedPreferences.setMockInitialValues(mockSettings); + initializeDateFormatting('de'); + testWidgets('Mood View displays correctly', (WidgetTester tester) async { + const String testText = 'Its a test'; + const int testValue = 30; + // Create an instance of the Providers and add it to the widget tree + final tasksProvider = TasksProvider(null); + final settingsProvider = SettingsProvider(); + + await tester.pumpWidget( + MultiProvider( + providers: [ + ChangeNotifierProvider.value(value: tasksProvider), + ChangeNotifierProvider.value(value: settingsProvider), + ], + child: const MaterialApp( + home: Scaffold( + body: MoodView(), + ), + ), + ), + ); + await settingsProvider.initSettings(); + await globals.databaseService + .addMood(Mood(testValue, testText, DateTime.now())); + await tasksProvider.initHistories(); + await tester.pump(); + expect(find.text('Stimmung: $testValue'), findsOneWidget); + await tester.tap(find.byIcon(Icons.expand_more)); + await tester.pump(); + expect(find.text('Kommentar: $testText'), findsOneWidget); + }); +} diff --git a/test/integration_tests/relapse_form_test.dart b/test/integration_tests/relapse_form_test.dart new file mode 100644 index 0000000..cdf7321 --- /dev/null +++ b/test/integration_tests/relapse_form_test.dart @@ -0,0 +1,57 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:provider/provider.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:smoke_cess_app/providers/input_provider.dart'; +import 'package:smoke_cess_app/providers/page_provider.dart'; +import 'package:smoke_cess_app/providers/settings_provider.dart'; +import 'package:smoke_cess_app/providers/tasks_provider.dart'; +import 'package:smoke_cess_app/globals.dart' as globals; +import 'package:smoke_cess_app/widgets/drop_down.dart'; +import 'package:smoke_cess_app/widgets/view_form/relapse_form.dart'; +import '../mock/db_mock.dart'; +import '../mock/settings_mock.dart'; + +void main() { + globals.databaseService = DatabaseMock(); + TestWidgetsFlutterBinding.ensureInitialized(); + SharedPreferences.setMockInitialValues(mockSettings); + testWidgets('Relapse Form saves correctly', (WidgetTester tester) async { + const String testText = 'Its a test'; + // Create an instance of the Providers and add it to the widget tree + final inputProvider = InputProvider(); + final tasksProvider = TasksProvider(null); + final pageProvider = PageProvider(); + final settingsProvider = SettingsProvider(); + await tester.pumpWidget( + MultiProvider( + providers: [ + ChangeNotifierProvider.value(value: inputProvider), + ChangeNotifierProvider.value(value: tasksProvider), + ChangeNotifierProvider.value(value: pageProvider), + ChangeNotifierProvider.value(value: settingsProvider), + ], + child: const MaterialApp( + home: Scaffold( + body: RelapseForm(), + ), + ), + ), + ); + + await settingsProvider.initSettings(); + + await tester.enterText(find.byType(TextField), testText); + await tester.tap(find.byType(DropDown)); + await tester.pump(); + await tester + .tap(find.text(settingsProvider.settings!.relapseCategories![1]).last); + await tester.pump(); + await tester.tap(find.byType(ElevatedButton).last); + await tester.pump(); + final result = await globals.databaseService.getRelapseRecords(); + expect(result.last.comment, testText); + expect( + result.last.category, settingsProvider.settings!.relapseCategories![1]); + }); +} diff --git a/test/integration_tests/relapse_view_test.dart b/test/integration_tests/relapse_view_test.dart new file mode 100644 index 0000000..96f3ee9 --- /dev/null +++ b/test/integration_tests/relapse_view_test.dart @@ -0,0 +1,52 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:intl/date_symbol_data_local.dart'; +import 'package:provider/provider.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:smoke_cess_app/models/relapse.dart'; +import 'package:smoke_cess_app/providers/settings_provider.dart'; +import 'package:smoke_cess_app/providers/tasks_provider.dart'; +import 'package:smoke_cess_app/globals.dart' as globals; +import 'package:smoke_cess_app/widgets/view_form/relapse_view.dart'; +import '../mock/db_mock.dart'; +import '../mock/settings_mock.dart'; + +void main() { + globals.databaseService = DatabaseMock(); + TestWidgetsFlutterBinding.ensureInitialized(); + SharedPreferences.setMockInitialValues(mockSettings); + initializeDateFormatting('de'); + testWidgets('Relapse View displays correctly', (WidgetTester tester) async { + const String testText = 'Its a test'; + // Create an instance of the Providers and add it to the widget tree + final tasksProvider = TasksProvider(null); + final settingsProvider = SettingsProvider(); + + await tester.pumpWidget( + MultiProvider( + providers: [ + ChangeNotifierProvider.value(value: tasksProvider), + ChangeNotifierProvider.value(value: settingsProvider), + ], + child: const MaterialApp( + home: Scaffold( + body: RelapseView(), + ), + ), + ), + ); + await settingsProvider.initSettings(); + await globals.databaseService.addRelapse(Relapse( + settingsProvider.settings!.relapseCategories![0], + testText, + DateTime.now(), + )); + await tasksProvider.initHistories(); + await tester.pump(); + expect(find.text(settingsProvider.settings!.relapseCategories![0]), + findsOneWidget); + await tester.tap(find.byIcon(Icons.expand_more)); + await tester.pump(); + expect(find.text('Kommentar: $testText'), findsOneWidget); + }); +} diff --git a/test/integration_tests/sleep_form_test.dart b/test/integration_tests/sleep_form_test.dart new file mode 100644 index 0000000..b833f08 --- /dev/null +++ b/test/integration_tests/sleep_form_test.dart @@ -0,0 +1,42 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:provider/provider.dart'; +import 'package:smoke_cess_app/providers/input_provider.dart'; +import 'package:smoke_cess_app/providers/page_provider.dart'; +import 'package:smoke_cess_app/providers/tasks_provider.dart'; +import 'package:smoke_cess_app/globals.dart' as globals; +import 'package:smoke_cess_app/widgets/view_form/sleep_form.dart'; +import '../mock/db_mock.dart'; + +void main() { + globals.databaseService = DatabaseMock(); + testWidgets('Sleep Form saves correctly', (WidgetTester tester) async { + const String testText = 'Its a test'; + // Create an instance of the Providers and add it to the widget tree + final inputProvider = InputProvider(); + final tasksProvider = TasksProvider(null); + final pageProvider = PageProvider(); + await tester.pumpWidget( + MultiProvider( + providers: [ + ChangeNotifierProvider.value(value: inputProvider), + ChangeNotifierProvider.value(value: tasksProvider), + ChangeNotifierProvider.value(value: pageProvider), + ], + child: const MaterialApp( + home: Scaffold( + body: SleepForm(), + ), + ), + ), + ); + + await tester.enterText(find.byType(TextField), testText); + await tester.tap(find.byType(ElevatedButton).last); + await tester.pump(); + final result = await globals.databaseService.getSleepRecords(); + expect(result.last.comment, testText); + expect(result.last.sleepQualitiyValue, 50); + expect(result.last.sleepDuration, const TimeOfDay(hour: 10, minute: 0)); + }); +} diff --git a/test/integration_tests/sleep_view_test.dart b/test/integration_tests/sleep_view_test.dart new file mode 100644 index 0000000..b8f2e1e --- /dev/null +++ b/test/integration_tests/sleep_view_test.dart @@ -0,0 +1,53 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:intl/date_symbol_data_local.dart'; +import 'package:provider/provider.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:smoke_cess_app/models/sleep.dart'; +import 'package:smoke_cess_app/providers/settings_provider.dart'; +import 'package:smoke_cess_app/providers/tasks_provider.dart'; +import 'package:smoke_cess_app/globals.dart' as globals; +import 'package:smoke_cess_app/widgets/view_form/sleep_view.dart'; +import '../mock/db_mock.dart'; +import '../mock/settings_mock.dart'; + +void main() { + globals.databaseService = DatabaseMock(); + TestWidgetsFlutterBinding.ensureInitialized(); + SharedPreferences.setMockInitialValues(mockSettings); + initializeDateFormatting('de'); + testWidgets('Sleep View displays correctly', (WidgetTester tester) async { + const String testText = 'Its a test'; + const int testValue = 30; + // Create an instance of the Providers and add it to the widget tree + final tasksProvider = TasksProvider(null); + final settingsProvider = SettingsProvider(); + + await tester.pumpWidget( + MultiProvider( + providers: [ + ChangeNotifierProvider.value(value: tasksProvider), + ChangeNotifierProvider.value(value: settingsProvider), + ], + child: const MaterialApp( + home: Scaffold( + body: SleepView(), + ), + ), + ), + ); + await settingsProvider.initSettings(); + await globals.databaseService.addSleep(Sleep( + testValue, + testText, + DateTime.now(), + const TimeOfDay(hour: 22, minute: 0), + const TimeOfDay(hour: 8, minute: 0))); + await tasksProvider.initHistories(); + await tester.pump(); + expect(find.text('10:00'), findsOneWidget); + await tester.tap(find.byIcon(Icons.expand_more)); + await tester.pump(); + expect(find.text('Kommentar: $testText'), findsOneWidget); + }); +}