flutter_application_1/test/widget_test.dart

349 lines
13 KiB
Dart
Raw Normal View History

2024-06-12 12:48:30 +02:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_application_1/pages/chart_page.dart';
import 'package:flutter_application_1/pages/milestone_page.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_application_1/enums.dart';
2024-06-12 12:48:30 +02:00
import 'package:flutter_application_1/widgets/error_widget.dart';
import 'package:flutter_application_1/widgets/input_widget.dart';
import 'package:flutter_application_1/widgets/interval_widget.dart';
import 'package:flutter_application_1/widgets/milestone_timeline_widget.dart';
import 'package:flutter_application_1/widgets/result_widget.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
void main() {
group('InputWidget Tests', () {
testWidgets('Displays label, tooltip, and input field', (WidgetTester tester) async {
const label = 'Test Label';
const tooltipText = 'This is a tooltip';
const suffixText = 'suffix';
final controller = TextEditingController();
final focusNode = FocusNode();
await tester.pumpWidget(
CupertinoApp(
home: CupertinoPageScaffold(
navigationBar: const CupertinoNavigationBar(
middle: Text('Test InputWidget'),
),
child: InputWidget(
label: label,
controller: controller,
focusNode: focusNode,
isValid: false,
suffixText: suffixText,
tooltipText: tooltipText,
),
),
),
);
expect(find.text(label), findsOneWidget);
expect(find.text(suffixText), findsOneWidget);
expect(find.byType(CupertinoTextField), findsOneWidget);
expect(find.byType(Tooltip), findsOneWidget);
});
testWidgets('Displays validation icon correctly', (WidgetTester tester) async {
final controller = TextEditingController();
final focusNode = FocusNode();
await tester.pumpWidget(
CupertinoApp(
home: InputWidget(
label: 'Test Label',
controller: controller,
focusNode: focusNode,
isValid: false,
suffixText: 'suffix',
tooltipText: 'This is a tooltip',
),
),
);
expect(find.byIcon(CupertinoIcons.clear_circled_solid), findsOneWidget);
expect(find.byIcon(CupertinoIcons.check_mark_circled_solid), findsNothing);
await tester.pumpWidget(
CupertinoApp(
home: InputWidget(
label: 'Test Label',
controller: controller,
focusNode: focusNode,
isValid: true,
suffixText: 'suffix',
tooltipText: 'This is a tooltip',
),
),
);
expect(find.byIcon(CupertinoIcons.check_mark_circled_solid), findsOneWidget);
expect(find.byIcon(CupertinoIcons.clear_circled_solid), findsNothing);
});
testWidgets('Text input updates controller', (WidgetTester tester) async {
final controller = TextEditingController();
final focusNode = FocusNode();
await tester.pumpWidget(
CupertinoApp(
home: InputWidget(
label: 'Test Label',
controller: controller,
focusNode: focusNode,
isValid: false,
suffixText: 'suffix',
tooltipText: 'This is a tooltip',
),
),
);
await tester.enterText(find.byType(CupertinoTextField), '12345');
expect(controller.text, '12345');
});
});
group('Interval Widget Tests', () {
testWidgets('Shows correct localized texts and handles dropdown selection', (WidgetTester tester) async {
const Locale testLocale = Locale('en');
final localizations = await AppLocalizations.delegate.load(testLocale);
2024-06-12 12:48:30 +02:00
String selectedInterval = localizations.yearly;
2024-06-12 12:48:30 +02:00
await tester.pumpWidget(
MaterialApp(
locale: testLocale,
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
2024-06-12 12:48:30 +02:00
home: Scaffold(
body: IntervalWidget(
selectedInterval: selectedInterval,
onChanged: (String newValue) {
selectedInterval = newValue;
},
2024-06-12 12:48:30 +02:00
),
),
),
);
expect(find.text(localizations.payout_interval), findsOneWidget);
expect(find.text(localizations.payout_interval_tooltiptext), findsNothing);
2024-06-12 12:48:30 +02:00
expect(find.text(localizations.yearly), findsOneWidget);
2024-06-12 12:48:30 +02:00
await tester.tap(find.byType(ElevatedButton));
await tester.pumpAndSettle();
2024-06-12 12:48:30 +02:00
expect(find.text(localizations.yearly), findsNWidgets(2)); // Eins im Button und eins im Dropdown-Menü
expect(find.text(localizations.monthly), findsOneWidget);
2024-06-12 12:48:30 +02:00
await tester.tap(find.text(localizations.monthly).last);
expect(selectedInterval, localizations.monthly);
2024-06-12 12:48:30 +02:00
});
});
group('Milestone Timeline Widget Tests', () {
testWidgets('Milestone timeline displays correctly', (WidgetTester tester) async {
final List<Map<String, dynamic>> milestones = [
{'value': 100.0, 'emoji': '😊', 'text': 'First milestone'},
{'value': 200.0, 'emoji': '😃', 'text': 'Second milestone'},
{'value': 300.0, 'emoji': '😁', 'text': 'Third milestone'},
];
const double totalInterest = 250.0;
await tester.pumpWidget(
CupertinoApp(
home: Material(
child: MilestoneTimeline(
milestones: milestones,
totalInterest: totalInterest,
),
),
),
);
expect(find.text('First milestone'), findsOneWidget);
expect(find.text('Second milestone'), findsOneWidget);
expect(find.text('Third milestone'), findsOneWidget);
expect(find.byIcon(CupertinoIcons.check_mark_circled_solid), findsNWidgets(2));
expect(find.byIcon(CupertinoIcons.circle_fill), findsOneWidget);
});
});
group('Result Widget Tests', () {
testWidgets('Shows correct localized texts', (WidgetTester tester) async {
const Locale testLocale = Locale('en');
final localizations = await AppLocalizations.delegate.load(testLocale);
const compoundInterest = '12000';
const investedMoney = '10000';
const time = '10';
const monthlySavingsRate = '100';
const interestRate = '5';
const payoutInterval = PayoutInterval.yearly;
final List<double> investedMoneyList = [1000, 2000, 3000, 4000, 5000];
final List<double> compoundInterestList = [1100, 2300, 3600, 5000, 6600];
2024-06-12 12:48:30 +02:00
await tester.pumpWidget(
MaterialApp(
locale: testLocale,
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
home: Scaffold(
body: ResultWidget(
compoundInterest: compoundInterest,
investedMoney: investedMoney,
time: time,
monthlySavingsRate: monthlySavingsRate,
interestRate: interestRate,
payoutInterval: payoutInterval,
investedMoneyList: investedMoneyList,
compoundInterestList: compoundInterestList,
),
2024-06-12 12:48:30 +02:00
),
),
);
2024-06-12 12:48:30 +02:00
expect(find.text(localizations.final_capital), findsOneWidget);
expect(find.text(localizations.amount(compoundInterest)), findsOneWidget);
expect(find.text(localizations.deposits), findsOneWidget);
expect(find.text(localizations.amount(investedMoney)), findsOneWidget);
expect(find.text(localizations.interest_received), findsOneWidget);
expect(find.text(localizations.amount(double.parse(compoundInterest) - double.parse(investedMoney))), findsOneWidget);
final resultText = localizations.result_text(
double.parse(compoundInterest) - double.parse(investedMoney),
compoundInterest,
interestRate,
investedMoney,
time,
monthlySavingsRate,
);
expect(find.text(resultText), findsOneWidget);
2024-06-12 12:48:30 +02:00
expect(find.text(localizations.graphic), findsOneWidget);
expect(find.text(localizations.milestones), findsOneWidget);
});
2024-06-12 12:48:30 +02:00
});
group('Error Widget Tests', () {
testWidgets('Displays error message correctly', (WidgetTester tester) async {
const String errorMessage = 'This is an error message';
await tester.pumpWidget(
const CupertinoApp(
home: Material(
child: ErrWidget(
errorMessage: errorMessage,
),
),
),
);
expect(find.byIcon(CupertinoIcons.exclamationmark_circle_fill), findsOneWidget);
expect(find.text(errorMessage), findsOneWidget);
});
});
group('Chart Page Tests', () {
testWidgets('Shows correct localized texts and table data', (WidgetTester tester) async {
const Locale testLocale = Locale('en');
final localizations = await AppLocalizations.delegate.load(testLocale);
final investedMoneyList = [1000.0, 2000.0, 3000.0, 4000.0, 5000.0];
final compoundInterestList = [1100.0, 2200.0, 3300.0, 4400.0, 5500.0];
await tester.pumpWidget(
MaterialApp(
locale: testLocale,
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
home: ChartPage(
investedMoneyList: investedMoneyList,
compoundInterestList: compoundInterestList,
),
),
);
expect(find.text(localizations.graphic), findsOneWidget);
expect(find.byType(SfCartesianChart), findsOneWidget);
expect(find.text(localizations.year), findsOneWidget);
expect(find.text(localizations.deposits), findsNWidgets(2));
expect(find.text(localizations.interest_received), findsNWidgets(2));
expect(find.text(localizations.final_capital), findsOneWidget);
for (int i = 0; i < investedMoneyList.length; i++) {
expect(find.text('${i + 1}'), findsOneWidget);
expect(find.text(localizations.amount(investedMoneyList[i])), findsOneWidget);
expect(find.text(localizations.amount(compoundInterestList[i])), findsOneWidget);
expect(find.text(localizations.amount(compoundInterestList[i] + investedMoneyList[i])), findsOneWidget);
}
await tester.tap(find.byIcon(CupertinoIcons.chevron_left));
await tester.pumpAndSettle();
expect(find.byIcon(CupertinoIcons.chevron_left), findsNothing);
});
});
group('Milestone Page Tests', () {
testWidgets('Shows correct localized texts and milestone data', (WidgetTester tester) async {
const Locale testLocale = Locale('en');
final localizations = await AppLocalizations.delegate.load(testLocale);
const compoundInterest = '50000';
const investedMoney = '30000';
List<Map<String, dynamic>> milestoneList = [
{'value': 700.0, 'emoji': '📱', 'text': localizations.milestone_text1 + localizations.amount(700)},
{'value': 3250.0, 'emoji': '🚲', 'text': localizations.milestone_text2 + localizations.amount(3250)},
{'value': 20000.0, 'emoji': '🌎', 'text': localizations.milestone_text3 + localizations.amount(20000)},
{'value': 100000.0, 'emoji': '🏎️', 'text': localizations.milestone_text4 + localizations.amount(100000)},
{'value': 350000.0, 'emoji': '🏡', 'text': localizations.milestone_text5 + localizations.amount(350000)},
];
await tester.pumpWidget(
MaterialApp(
locale: testLocale,
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
home: MilestonePage(
compoundInterest: compoundInterest,
investedMoney: investedMoney,
milestoneList: milestoneList,
),
),
);
expect(find.text(localizations.milestones), findsOneWidget);
expect(find.byType(MilestoneTimeline), findsOneWidget);
for (var milestone in milestoneList) {
expect(find.text(milestone['emoji']), findsOneWidget);
expect(find.text(milestone['text']), findsOneWidget);
}
await tester.tap(find.byIcon(CupertinoIcons.chevron_left));
await tester.pumpAndSettle();
expect(find.byIcon(CupertinoIcons.chevron_left), findsNothing);
});
});
2024-06-12 12:48:30 +02:00
}