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

Integration test #380

Merged
merged 2 commits into from
Dec 18, 2021
Merged
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
10 changes: 6 additions & 4 deletions example/lib/presentation/pages/calendar_add.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class _CalendarAddPageState extends State<CalendarAddPage> {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
late DeviceCalendarPlugin _deviceCalendarPlugin;

bool _autovalidate = false;
AutovalidateMode _autovalidate = AutovalidateMode.disabled;
String _calendarName = '';
ColorChoice? _colorChoice;
String _localAccountName = '';
Expand All @@ -32,7 +32,7 @@ class _CalendarAddPageState extends State<CalendarAddPage> {
title: Text('Create Calendar'),
),
body: Form(
autovalidate: _autovalidate,
autovalidateMode: _autovalidate,
key: _formKey,
child: Container(
padding: EdgeInsets.all(10),
Expand Down Expand Up @@ -81,7 +81,8 @@ class _CalendarAddPageState extends State<CalendarAddPage> {
onPressed: () async {
final form = _formKey.currentState;
if (form?.validate() == false) {
_autovalidate = true; // Start validating on every change.
_autovalidate =
AutovalidateMode.always; // Start validating on every change.
showInSnackBar('Please fix the errors in red before submitting.');
} else {
form?.save();
Expand Down Expand Up @@ -115,7 +116,8 @@ class _CalendarAddPageState extends State<CalendarAddPage> {
}

void showInSnackBar(String value) {
_scaffoldKey.currentState?.showSnackBar(SnackBar(content: Text(value)));
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(value)));
// _scaffoldKey.currentState?.showSnackBar(SnackBar(content: Text(value)));
}
}

Expand Down
9 changes: 5 additions & 4 deletions example/lib/presentation/pages/calendar_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class _CalendarEventPageState extends State<CalendarEventPage> {
TZDateTime? _endDate;
late TimeOfDay _endTime;

bool _autovalidate = false;
AutovalidateMode _autovalidate = AutovalidateMode.disabled;
DayOfWeekGroup? _dayOfWeekGroup = DayOfWeekGroup.None;

bool _isRecurringEvent = false;
Expand Down Expand Up @@ -181,7 +181,7 @@ class _CalendarEventPageState extends State<CalendarEventPage> {
child: Column(
children: [
Form(
autovalidate: _autovalidate,
autovalidateMode: _autovalidate,
key: _formKey,
child: Column(
children: [
Expand Down Expand Up @@ -808,7 +808,8 @@ class _CalendarEventPageState extends State<CalendarEventPage> {
onPressed: () async {
final form = _formKey.currentState;
if (form?.validate() == false) {
_autovalidate = true; // Start validating on every change.
_autovalidate =
AutovalidateMode.always; // Start validating on every change.
showInSnackBar('Please fix the errors in red before submitting.');
} else {
form?.save();
Expand Down Expand Up @@ -1012,6 +1013,6 @@ class _CalendarEventPageState extends State<CalendarEventPage> {
}

void showInSnackBar(String value) {
_scaffoldKey.currentState!.showSnackBar(SnackBar(content: Text(value)));
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(value)));
}
}
2 changes: 1 addition & 1 deletion example/lib/presentation/pages/calendar_events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class _CalendarEventsPageState extends State<CalendarEventsPage> {
if (deleteSucceeded) {
await _retrieveCalendarEvents();
} else {
_scaffoldstate.currentState!.showSnackBar(SnackBar(
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Oops, we ran into an issue deleting the event'),
backgroundColor: Colors.red,
duration: Duration(seconds: 5),
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies:
cupertino_icons:

dev_dependencies:
flutter_driver:
integration_test:
sdk: flutter
flutter_test:
sdk: flutter
Expand Down
11 changes: 0 additions & 11 deletions example/test_driver/app.dart

This file was deleted.

147 changes: 72 additions & 75 deletions example/test_driver/app_test.dart
Original file line number Diff line number Diff line change
@@ -1,93 +1,90 @@
import 'dart:io';

import 'package:flutter_driver/flutter_driver.dart';
import 'package:test/test.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:uuid/uuid.dart';

// import 'package:flutter_driver/driver_extension.dart';
import 'package:device_calendar_example/main.dart' as app;

/// NOTE: These integration tests are currently made to be run on a physical Android device where there is at least a calendar that can be written to.
/// They will currently need to be run on a Mac as well
/// Calendar permissions are needed. See example/test_driver/integration_test.dart for how to run this
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
group('Calendar plugin example', () {
FlutterDriver? driver;
final eventTitle = Uuid().v1();
final saveEventButtonFinder = find.byValueKey('saveEventButton');
final saveEventButtonFinder = find.byKey(const Key('saveEventButton'));
final eventTitleFinder = find.text(eventTitle);
setUpAll(() async {
// workaround for handling permissions based on info taken from https://github.com/flutter/flutter/issues/12561
// this is to be run in a Mac environment
final envVars = Platform.environment;
final adbPath = envVars['ANDROID_HOME'] != null
? envVars['ANDROID_HOME']! + '/platform-tools/adb'
: '';
await Process.run(adbPath, [
'shell',
'pm',
'grant',
'com.builttoroam.devicecalendarexample',
'android.permission.INTERNET'
]);
await Process.run(adbPath, [
'shell',
'pm',
'grant',
'com.builttoroam.devicecalendarexample',
'android.permission.READ_CALENDAR'
]);
await Process.run(adbPath, [
'shell',
'pm',
'grant',
'com.builttoroam.devicecalendarexample',
'android.permission.WRITE_CALENDAR'
]);

driver = await FlutterDriver.connect();
final firstWritableCalendarFinder =
find.byKey(const Key('writableCalendar0'));
final addEventButtonFinder = find.byKey(const Key('addEventButton'));
final titleFieldFinder = find.byKey(const Key('titleField'));
final deleteButtonFinder = find.byKey(const Key('deleteEventButton'));
//TODO: remove redundant restarts. Currently needed because the first screen is always "test starting..."
testWidgets('starts on calendars page', (WidgetTester tester) async {
app.main();
await tester.pumpAndSettle();
expect(find.byKey(const Key('calendarsPage')), findsOneWidget);
});
testWidgets('select first writable calendar', (WidgetTester tester) async {
app.main();

tearDownAll(() async {
await driver?.close();
await tester.pumpAndSettle(Duration(milliseconds: 500));
expect(firstWritableCalendarFinder, findsOneWidget);
});
testWidgets('go to add event page', (WidgetTester tester) async {
app.main();
await tester.pumpAndSettle();
await tester.tap(firstWritableCalendarFinder);

test('check flutter driver health', () async {
final health = await driver?.checkHealth();
print('flutter driver status: ${health?.status}');
});

test('starts on calendars page', () async {
await driver?.waitFor(find.byValueKey('calendarsPage'));
});
test('select first writable calendar', () async {
final writableCalendarFinder = find.byValueKey('writableCalendar0');
await driver?.waitFor(writableCalendarFinder,
timeout: Duration(milliseconds: 500));
await driver?.tap(writableCalendarFinder);
});
test('go to add event page', () async {
final addEventButtonFinder = find.byValueKey('addEventButton');
await driver?.waitFor(addEventButtonFinder);
await tester.pumpAndSettle();
expect(addEventButtonFinder, findsOneWidget);
print('found add event button');
await driver?.tap(addEventButtonFinder);
await driver?.waitFor(saveEventButtonFinder);
await tester.tap(addEventButtonFinder);
await tester.pumpAndSettle();
expect(saveEventButtonFinder, findsOneWidget);
});
test('try to save event without entering mandatory fields', () async {
await driver?.tap(saveEventButtonFinder);
await driver?.waitFor(
find.text('Please fix the errors in red before submitting.'));
testWidgets('try to save event without entering mandatory fields',
(WidgetTester tester) async {
app.main();
await tester.pumpAndSettle();
await tester.tap(firstWritableCalendarFinder);
await tester.pumpAndSettle();
await tester.tap(addEventButtonFinder);

await tester.pumpAndSettle();
await tester.tap(saveEventButtonFinder);
await tester.pumpAndSettle();
expect(find.text('Please fix the errors in red before submitting.'),
findsOneWidget);
});
test('save event with title $eventTitle', () async {
final titleFieldFinder = find.byValueKey('titleField');
await driver?.waitFor(titleFieldFinder);
await driver?.tap(titleFieldFinder);
await driver?.enterText(eventTitle);
await driver?.tap(saveEventButtonFinder);
await driver?.waitFor(eventTitleFinder);
testWidgets('save event with title $eventTitle',
(WidgetTester tester) async {
app.main();
await tester.pumpAndSettle();
await tester.tap(firstWritableCalendarFinder);
await tester.pumpAndSettle();
await tester.tap(addEventButtonFinder);

await tester.pumpAndSettle();
await tester.tap(titleFieldFinder);

await tester.enterText(titleFieldFinder, eventTitle);
await tester.tap(saveEventButtonFinder);
await tester.pumpAndSettle();
expect(eventTitleFinder, findsOneWidget);
});
test('delete event with title $eventTitle', () async {
await driver?.tap(eventTitleFinder);
final deleteButtonFinder = find.byValueKey('deleteEventButton');
await driver?.scrollIntoView(deleteButtonFinder);
await driver?.tap(deleteButtonFinder);
await driver?.waitForAbsent(eventTitleFinder);
testWidgets('delete event with title $eventTitle',
(WidgetTester tester) async {
app.main();
await tester.pumpAndSettle();
await tester.tap(firstWritableCalendarFinder);
await tester.pumpAndSettle();
await tester.tap(eventTitleFinder);

await tester.scrollUntilVisible(deleteButtonFinder, 5);
await tester.tap(deleteButtonFinder);
await tester.pumpAndSettle();
expect(eventTitleFinder, findsNothing);
});
});
}
26 changes: 26 additions & 0 deletions example/test_driver/integration_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'dart:io';

import 'package:integration_test/integration_test_driver.dart';

// make sure 'adb devices' works on your local machine, then run the following:
/*
flutter drive \ --driver=test_driver/integration_test.dart \ --target=integration_test/app_test.dart
*/

Future<void> main() async {
await Process.run('adb', [
'shell',
'pm',
'grant',
'com.builttoroam.devicecalendarexample',
'android.permission.READ_CALENDAR'
]);
await Process.run('adb', [
'shell',
'pm',
'grant',
'com.builttoroam.devicecalendarexample',
'android.permission.WRITE_CALENDAR'
]);
await integrationDriver();
}
1 change: 0 additions & 1 deletion test_driver/app.dart

This file was deleted.