cpd_2022_zi/test/widget_tests/widget_slider_test.dart

118 lines
3.0 KiB
Dart

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/widgets/slider.dart';
void main() {
testWidgets('Slider starts at 50', (WidgetTester tester) async {
// Create an instance of the InputProvider and add it to the widget tree
final inputProvider = InputProvider();
await tester.pumpWidget(
MultiProvider(
providers: [
ChangeNotifierProvider.value(value: inputProvider),
],
child: const MaterialApp(
home: Scaffold(
body: MySlider(),
),
),
),
);
// Verify that the Slider displays 50
expect(find.text('50'), findsOneWidget);
});
testWidgets('Slider puts 51 after Tap on plus in InputProvider',
(WidgetTester tester) async {
final inputProvider = InputProvider();
await tester.pumpWidget(
MultiProvider(
providers: [
ChangeNotifierProvider.value(value: inputProvider),
],
child: const MaterialApp(
home: Scaffold(
body: MySlider(),
),
),
),
);
await tester.tap(find.byIcon(Icons.add_outlined));
await tester.pump();
expect(inputProvider.sliderValue, equals(51));
});
testWidgets('Slider puts 49 after Tap on subtract in InputProvider',
(WidgetTester tester) async {
final inputProvider = InputProvider();
await tester.pumpWidget(
MultiProvider(
providers: [
ChangeNotifierProvider.value(value: inputProvider),
],
child: const MaterialApp(
home: Scaffold(
body: MySlider(),
),
),
),
);
await tester.tap(find.byIcon(Icons.remove_outlined));
await tester.pump();
expect(inputProvider.sliderValue, equals(49));
});
testWidgets('Slider doesnt go higher than 100', (WidgetTester tester) async {
final inputProvider = InputProvider();
await tester.pumpWidget(
MultiProvider(
providers: [
ChangeNotifierProvider.value(value: inputProvider),
],
child: const MaterialApp(
home: Scaffold(
body: MySlider(),
),
),
),
);
for (int i = 0; i < 100; i++) {
await tester.tap(find.byIcon(Icons.add_outlined));
await tester.pump();
}
expect(inputProvider.sliderValue, equals(100));
});
testWidgets('Slider doesnt go lower than 0', (WidgetTester tester) async {
final inputProvider = InputProvider();
await tester.pumpWidget(
MultiProvider(
providers: [
ChangeNotifierProvider.value(value: inputProvider),
],
child: const MaterialApp(
home: Scaffold(
body: MySlider(),
),
),
),
);
for (int i = 0; i < 100; i++) {
await tester.tap(find.byIcon(Icons.remove_outlined));
await tester.pump();
}
expect(inputProvider.sliderValue, equals(0));
});
}