Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mono0926 committed Jan 23, 2024
1 parent 75aad3a commit f5a4b17
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,8 +1,10 @@
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/cupertino.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 @@ -11,11 +13,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(pascalCaseFromRouteUri(GoRouterState.of(context).uri)),
Expand All @@ -30,7 +32,7 @@ class AlertPage extends StatelessWidget {
title: 'Title',
message: 'This is message.',
);
logger.info(result);
ref.read(resultProvider.notifier).set(result);
},
),
ListTile(
Expand All @@ -43,7 +45,7 @@ class AlertPage extends StatelessWidget {
canPop: false,
);
assert(result == OkCancelResult.ok);
logger.info(result);
ref.read(resultProvider.notifier).set(result);
},
),
ListTile(
Expand All @@ -55,7 +57,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 f5a4b17

Please sign in to comment.