Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mono0926 committed Mar 27, 2020
1 parent 2365fa5 commit 78f03aa
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
14 changes: 12 additions & 2 deletions example/lib/pages/alert_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:example/result_banner.dart';
import 'package:example/router.dart';
import 'package:example/util/logger.dart';
import 'package:flutter/material.dart';
Expand All @@ -10,12 +11,21 @@ class AlertPage extends StatelessWidget {

@override
Widget build(BuildContext context) {
final resultKey = GlobalObjectKey<ResultBannerState>(context);
void updateResult(dynamic result) {
resultKey.currentState.updateText(
result.toString(),
);
logger.info(result);
}

return Scaffold(
appBar: AppBar(
title: Text(pascalCaseFromRouteName(AlertPage.routeName)),
),
body: ListView(
children: <Widget>[
ResultBanner(key: resultKey),
ListTile(
title: const Text('OK Dialog'),
onTap: () async {
Expand All @@ -24,7 +34,7 @@ class AlertPage extends StatelessWidget {
title: 'Title',
message: 'This is message.',
);
logger.info(result);
updateResult(result);
},
),
ListTile(
Expand All @@ -37,7 +47,7 @@ class AlertPage extends StatelessWidget {
barrierDismissible: false,
);
assert(result == OkCancelResult.ok);
logger.info(result);
updateResult(result);
},
),
ListTile(
Expand Down
30 changes: 30 additions & 0 deletions example/lib/result_banner.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:flutter/material.dart';

class ResultBanner extends StatefulWidget {
const ResultBanner({Key key}) : super(key: key);
@override
ResultBannerState createState() => ResultBannerState();
}

class ResultBannerState extends State<ResultBanner> {
var _text = '';

void updateText(String text) {
setState(() {
_text = text;
});
}

@override
Widget build(BuildContext context) {
return MaterialBanner(
content: Text('Result: $_text'),
actions: <Widget>[
FlatButton(
child: Text('Clear'),
onPressed: () => updateText(''),
)
],
);
}
}
55 changes: 55 additions & 0 deletions example/test/alert_dialog_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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:example/app.dart';
import 'package:example/router.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';

void main() {
testWidgets('OK Dialog', (tester) async {
await _setupTester(tester);

await tester.tap(find.widgetWithText(ListTile, 'OK Dialog'));
await tester.pumpAndSettle();

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

expect(find.text('Result: OkCancelResult.ok'), findsOneWidget);
});

testWidgets('OK Dialog (barrierDismissible: false)', (tester) async {
await _setupTester(tester);

await tester.tap(
find.widgetWithText(ListTile, 'OK Dialog (barrierDismissible: false)'));
await tester.pumpAndSettle();

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

expect(find.text('Result: OkCancelResult.ok'), findsOneWidget);
});
}

Future<void> _setupTester(WidgetTester tester) async {
await tester.pumpWidget(MultiProvider(
providers: [
Provider(create: (context) => Router()),
],
child: const App(),
));

await tester.tap(find.widgetWithText(ListTile, 'Alert'));
await tester.pumpAndSettle();
}

0 comments on commit 78f03aa

Please sign in to comment.