Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test #2

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
);
6 changes: 6 additions & 0 deletions example/macos/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
PODS:
- appkit_ui_element_colors (1.0.0):
- FlutterMacOS
- dynamic_color (0.0.2):
- FlutterMacOS
- file_selector_macos (0.0.1):
Expand All @@ -12,6 +14,7 @@ PODS:
- FlutterMacOS

DEPENDENCIES:
- appkit_ui_element_colors (from `Flutter/ephemeral/.symlinks/plugins/appkit_ui_element_colors/macos`)
- dynamic_color (from `Flutter/ephemeral/.symlinks/plugins/dynamic_color/macos`)
- file_selector_macos (from `Flutter/ephemeral/.symlinks/plugins/file_selector_macos/macos`)
- FlutterMacOS (from `Flutter/ephemeral`)
Expand All @@ -20,6 +23,8 @@ DEPENDENCIES:
- url_launcher_macos (from `Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos`)

EXTERNAL SOURCES:
appkit_ui_element_colors:
:path: Flutter/ephemeral/.symlinks/plugins/appkit_ui_element_colors/macos
dynamic_color:
:path: Flutter/ephemeral/.symlinks/plugins/dynamic_color/macos
file_selector_macos:
Expand All @@ -34,6 +39,7 @@ EXTERNAL SOURCES:
:path: Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos

SPEC CHECKSUMS:
appkit_ui_element_colors: 39bb2d80be3f19b152ccf4c70d5bbe6cba43d74a
dynamic_color: 2eaa27267de1ca20d879fbd6e01259773fb1670f
file_selector_macos: 468fb6b81fac7c0e88d71317f3eec34c3b008ff9
FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24
Expand Down
98 changes: 98 additions & 0 deletions example/test/pages/alert_page_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
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> openOkCanPopFalseDialog() => open('OK Dialog (canPop: 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 (canPop: false)', () {
testWidgets('OK', (tester) async {
final container = await tester.setUpAlert();
await tester.openOkCanPopFalseDialog();

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.openOkCanPopFalseDialog();

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();
}
}