2021-04-01 00:00:13 +00:00
|
|
|
// This is a basic Flutter widget test.
|
|
|
|
//
|
|
|
|
// To perform an interaction with a widget in your test, use the WidgetTester
|
|
|
|
// utility that Flutter provides. 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';
|
2021-05-19 21:39:52 +00:00
|
|
|
import 'package:cwtch/opaque.dart';
|
|
|
|
import 'package:cwtch/settings.dart';
|
|
|
|
import 'package:cwtch/widgets/textfield.dart';
|
2021-04-01 00:00:13 +00:00
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
|
|
|
|
var settingsEnglishDark = Settings(Locale("en", ''), Opaque.dark);
|
|
|
|
var settingsEnglishLight = Settings(Locale("en", ''), Opaque.light);
|
|
|
|
ChangeNotifierProvider<Settings> getSettingsEnglishDark() => ChangeNotifierProvider.value(value: settingsEnglishDark);
|
|
|
|
|
|
|
|
String file(String slug) {
|
|
|
|
return "textfield_" + slug + ".png";
|
|
|
|
}
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
testWidgets('Textfield widget test', (WidgetTester tester) async {
|
2021-04-01 00:14:25 +00:00
|
|
|
tester.binding.window.physicalSizeTestValue = Size(800, 300);
|
2021-04-01 00:00:13 +00:00
|
|
|
final TextEditingController ctrlr1 = TextEditingController();
|
|
|
|
|
2021-05-25 00:16:57 +00:00
|
|
|
Widget testWidget = CwtchTextField(controller: ctrlr1, validator: (value) { }, labelText: '',);
|
2021-04-01 00:00:13 +00:00
|
|
|
|
|
|
|
Widget testHarness = MultiProvider(
|
|
|
|
providers:[getSettingsEnglishDark()],
|
|
|
|
builder: (context, child) { return MaterialApp(
|
|
|
|
locale: Provider.of<Settings>(context).locale,
|
|
|
|
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
|
|
|
supportedLocales: AppLocalizations.supportedLocales,
|
|
|
|
title: 'Test',
|
|
|
|
theme: mkThemeData(Provider.of<Settings>(context)),
|
|
|
|
home: Card(child: testWidget),
|
|
|
|
);}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check base state appearance
|
|
|
|
await tester.pumpWidget(testHarness);
|
|
|
|
await expectLater(find.byWidget(testHarness), matchesGoldenFile(file('init')));
|
|
|
|
|
|
|
|
// Type "hello there"
|
|
|
|
await tester.tap(find.byWidget(testWidget));
|
|
|
|
await tester.pump();
|
|
|
|
await tester.enterText(find.byWidget(testWidget), "hello there");
|
|
|
|
await tester.pumpAndSettle();
|
|
|
|
await expectLater(find.byWidget(testHarness), matchesGoldenFile(file('basic')));
|
|
|
|
|
|
|
|
// Verify that text is displayed normally
|
|
|
|
expect(find.text("hello there"), findsOneWidget);
|
|
|
|
expect(find.text('nada'), findsNothing);
|
|
|
|
});
|
|
|
|
|
|
|
|
//=\\
|
|
|
|
|
|
|
|
testWidgets('Textfield form validation test', (WidgetTester tester) async {
|
2021-04-01 00:14:25 +00:00
|
|
|
tester.binding.window.physicalSizeTestValue = Size(800, 300);
|
2021-04-01 00:00:13 +00:00
|
|
|
final formKey = GlobalKey<FormState>();
|
|
|
|
final TextEditingController ctrlr1 = TextEditingController();
|
|
|
|
final String strLabel1 = "Age (*Required)";
|
|
|
|
final String strFail1 = "Required field";
|
|
|
|
final String strFail2 = "Please enter an integer";
|
|
|
|
|
|
|
|
Widget testWidget = CwtchTextField(
|
|
|
|
controller: ctrlr1,
|
|
|
|
labelText: strLabel1,
|
|
|
|
validator: (value) {
|
|
|
|
if (value == null || value == "") return strFail1;
|
|
|
|
final number = num.tryParse(value);
|
|
|
|
if (number == null) return strFail2;
|
|
|
|
return null;
|
|
|
|
},
|
2021-05-25 00:16:57 +00:00
|
|
|
onChanged: (value) => formKey.currentState!.validate(),
|
2021-04-01 00:00:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
Widget testHarness = MultiProvider(
|
|
|
|
providers: [getSettingsEnglishDark()],
|
|
|
|
builder: (context, child) {
|
|
|
|
return MaterialApp(
|
|
|
|
locale: Provider
|
|
|
|
.of<Settings>(context)
|
|
|
|
.locale,
|
|
|
|
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
|
|
|
supportedLocales: AppLocalizations.supportedLocales,
|
|
|
|
title: 'Test',
|
|
|
|
theme: mkThemeData(Provider.of<Settings>(context)),
|
|
|
|
home: Card(child: Form(key: formKey, child: testWidget)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check base state appearance
|
|
|
|
await tester.pumpWidget(testHarness);
|
|
|
|
await expectLater(find.byWidget(testHarness), matchesGoldenFile(file('form_init')));
|
|
|
|
expect(find.text(strLabel1), findsOneWidget);
|
|
|
|
|
|
|
|
// 42
|
|
|
|
await tester.tap(find.byWidget(testWidget));
|
|
|
|
await tester.pump();
|
|
|
|
await tester.enterText(find.byWidget(testWidget), "42");
|
|
|
|
await tester.pumpAndSettle();
|
|
|
|
await expectLater(find.byWidget(testHarness), matchesGoldenFile(file('form_42')));
|
|
|
|
expect(find.text("42"), findsOneWidget);
|
|
|
|
expect(find.text(strFail1), findsNothing);
|
|
|
|
expect(find.text(strFail2), findsNothing);
|
|
|
|
ctrlr1.clear();
|
|
|
|
await tester.pumpAndSettle();
|
|
|
|
|
|
|
|
// alpha quadrant
|
|
|
|
await tester.tap(find.byWidget(testWidget));
|
|
|
|
await tester.pump();
|
|
|
|
await tester.enterText(find.byWidget(testWidget), "alpha quadrant");
|
|
|
|
await tester.pumpAndSettle();
|
|
|
|
await expectLater(find.byWidget(testHarness), matchesGoldenFile(file('form_alpha')));
|
|
|
|
expect(find.text("alpha quadrant"), findsOneWidget);
|
|
|
|
expect(find.text(strFail1), findsNothing);
|
|
|
|
expect(find.text(strFail2), findsOneWidget);
|
|
|
|
ctrlr1.clear();
|
|
|
|
await tester.pumpAndSettle();
|
|
|
|
|
|
|
|
// empty string
|
2021-05-25 00:16:57 +00:00
|
|
|
formKey.currentState!.validate(); //(ctrlr1.clear() doesn't trigger validate like keypress does)
|
2021-04-01 00:00:13 +00:00
|
|
|
await tester.pumpAndSettle();
|
|
|
|
await expectLater(find.byWidget(testHarness), matchesGoldenFile(file('form_final')));
|
2021-04-21 00:24:28 +00:00
|
|
|
expect(find.text(strFail1), findsOneWidget);
|
|
|
|
expect(find.text(strFail2), findsNothing);
|
2021-04-01 00:00:13 +00:00
|
|
|
});
|
|
|
|
}
|