Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mono0926 committed Dec 23, 2022
1 parent e03d828 commit 0def227
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 5 deletions.
12 changes: 7 additions & 5 deletions example/lib/pages/alert_page.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:example/result_provider.dart';
import 'package:example/router/router.dart';
import 'package:example/util/logger.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';

class AlertRoute extends GoRouteData {
Expand All @@ -10,11 +12,11 @@ class AlertRoute extends GoRouteData {
Widget build(BuildContext context, GoRouterState state) => const AlertPage();
}

class AlertPage extends StatelessWidget {
class AlertPage extends ConsumerWidget {
const AlertPage({super.key});

@override
Widget build(BuildContext context) {
Widget build(BuildContext context, WidgetRef ref) {
return Scaffold(
appBar: AppBar(
title: Text(pascalCaseFromRouteName(GoRouter.of(context).location)),
Expand All @@ -29,7 +31,7 @@ class AlertPage extends StatelessWidget {
title: 'Title',
message: 'This is message.',
);
logger.info(result);
ref.read(resultProvider.notifier).set(result);
},
),
ListTile(
Expand All @@ -42,7 +44,7 @@ class AlertPage extends StatelessWidget {
onWillPop: () => Future.value(false),
);
assert(result == OkCancelResult.ok);
logger.info(result);
ref.read(resultProvider.notifier).set(result);
},
),
ListTile(
Expand All @@ -54,7 +56,7 @@ class AlertPage extends StatelessWidget {
message: 'This is message.',
barrierDismissible: false,
);
logger.info(result);
ref.read(resultProvider.notifier).set(result);
},
),
ListTile(
Expand Down
21 changes: 21 additions & 0 deletions example/lib/result_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:example/util/util.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

final resultProvider =
StateNotifierProvider<ResultNotifier, dynamic>((ref) => ResultNotifier());

class ResultNotifier extends StateNotifier<dynamic> {
ResultNotifier() : super(null);
// ignore: use_setters_to_change_properties
void set(dynamic text) {
state = text;
}

void clear() => set('');

@override
set state(dynamic value) {
super.state = value;
logger.info(state);
}
}
1 change: 1 addition & 0 deletions example/lib/util/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ final SimpleLogger logger = SimpleLogger()
..setLevel(
Level.FINEST,
includeCallerInfo: true,
callerInfoFrameLevelOffset: 2,
);
99 changes: 99 additions & 0 deletions example/test/pages/alert_page_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:example/result_provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';

import '../util.dart';

extension on WidgetTester {
Future<ProviderContainer> setUpAlert() => setup('Alert');
Future<void> openOkDialog() => open('OK Dialog');
Future<void> openOkOnWillPopFalseDialog() =>
open('OK Dialog (onWillPop: false)');
Future<void> openOkBarrierDismissibleFalseDialog() =>
open('OK Dialog (barrierDismissible: false)');
}

void main() {
group('OK Dialog', () {
testWidgets('OK', (tester) async {
final container = await tester.setUpAlert();
await tester.openOkDialog();

expect(find.text('Title'), findsOneWidget);
expect(find.text('This is message.'), findsOneWidget);
await tester.tap(find.widgetWithText(TextButton, 'OK'));
await tester.pumpAndSettle();

expect(container.read(resultProvider), OkCancelResult.ok);
});
testWidgets('barrier', (tester) async {
final container = await tester.setUpAlert();
await tester.openOkDialog();

await tester.tapBarrier();

expect(container.read(resultProvider), OkCancelResult.cancel);
});

testWidgets('maybePop', (tester) async {
final container = await tester.setUpAlert();
await tester.openOkDialog();

final context = tester.element(find.text('Title'));
await Navigator.of(context).maybePop();
await tester.pumpAndSettle();

expect(container.read(resultProvider), OkCancelResult.cancel);
});
});

group('OK Dialog (onWillPop: false)', () {
testWidgets('OK', (tester) async {
final container = await tester.setUpAlert();
await tester.openOkOnWillPopFalseDialog();

expect(find.text('Title'), findsOneWidget);
expect(find.text('This is message.'), findsOneWidget);
await tester.tap(find.widgetWithText(TextButton, 'OK'));
await tester.pumpAndSettle();

expect(container.read(resultProvider), OkCancelResult.ok);
});
testWidgets('maybePop', (tester) async {
final container = await tester.setUpAlert();
await tester.openOkOnWillPopFalseDialog();

final context = tester.element(find.text('Title'));
await Navigator.of(context).maybePop();
await tester.pumpAndSettle();

expect(container.read(resultProvider), isNull);
expect(find.text('Title'), findsOneWidget);
});
});

group('OK Dialog (barrierDismissible: false)', () {
testWidgets('OK', (tester) async {
final container = await tester.setUpAlert();
await tester.openOkBarrierDismissibleFalseDialog();

expect(find.text('Title'), findsOneWidget);
expect(find.text('This is message.'), findsOneWidget);
await tester.tap(find.widgetWithText(TextButton, 'OK'));
await tester.pumpAndSettle();

expect(container.read(resultProvider), OkCancelResult.ok);
});
testWidgets('barrier', (tester) async {
final container = await tester.setUpAlert();
await tester.openOkBarrierDismissibleFalseDialog();

await tester.tapBarrier();

expect(container.read(resultProvider), isNull);
expect(find.text('Title'), findsOneWidget);
});
});
}
31 changes: 31 additions & 0 deletions example/test/util.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:example/app.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';

extension WidgetTesterX on WidgetTester {
Future<ProviderContainer> setup(String pageName) async {
final container = ProviderContainer();
addTearDown(container.dispose);
await pumpWidget(
ProviderScope(
parent: container,
child: const App(),
),
);

await tap(find.widgetWithText(ListTile, pageName));
await pumpAndSettle();
return container;
}

Future<void> open(String label) async {
await tap(find.widgetWithText(ListTile, label));
await pumpAndSettle();
}

Future<void> tapBarrier() async {
await tapAt(Offset.zero);
await pumpAndSettle();
}
}

0 comments on commit 0def227

Please sign in to comment.