Commit 709cc003 authored by Adam Barth's avatar Adam Barth Committed by GitHub

Improve test coverage again (#7458)

This patch adds tests for radio buttons, the date picker, as well as a
number of other classes.
parent d0cd5458
...@@ -672,10 +672,10 @@ class _DatePickerDialogState extends State<_DatePickerDialog> { ...@@ -672,10 +672,10 @@ class _DatePickerDialogState extends State<_DatePickerDialog> {
/// * [showTimePicker] /// * [showTimePicker]
/// * <https://material.google.com/components/pickers.html#pickers-date-pickers> /// * <https://material.google.com/components/pickers.html#pickers-date-pickers>
Future<DateTime> showDatePicker({ Future<DateTime> showDatePicker({
BuildContext context, @required BuildContext context,
DateTime initialDate, @required DateTime initialDate,
DateTime firstDate, @required DateTime firstDate,
DateTime lastDate @required DateTime lastDate
}) async { }) async {
return await showDialog( return await showDialog(
context: context, context: context,
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
import 'dart:async'; import 'dart:async';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:meta/meta.dart';
import 'button.dart'; import 'button.dart';
import 'button_bar.dart'; import 'button_bar.dart';
...@@ -315,7 +316,10 @@ class _DialogRoute<T> extends PopupRoute<T> { ...@@ -315,7 +316,10 @@ class _DialogRoute<T> extends PopupRoute<T> {
/// * [AlertDialog], for dialogs that have a row of buttons below the body. /// * [AlertDialog], for dialogs that have a row of buttons below the body.
/// * [Dialog], on which [SimpleDialog] and [AlertDialog] are based. /// * [Dialog], on which [SimpleDialog] and [AlertDialog] are based.
/// * <https://material.google.com/components/dialogs.html> /// * <https://material.google.com/components/dialogs.html>
Future<dynamic/*=T*/> showDialog/*<T>*/({ BuildContext context, Widget child }) { Future<dynamic/*=T*/> showDialog/*<T>*/({
@required BuildContext context,
@required Widget child
}) {
return Navigator.push(context, new _DialogRoute<dynamic/*=T*/>( return Navigator.push(context, new _DialogRoute<dynamic/*=T*/>(
child: child, child: child,
theme: Theme.of(context, shadowThemeOnly: true), theme: Theme.of(context, shadowThemeOnly: true),
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:intl/intl.dart';
void main() { void main() {
testWidgets('tap-select a day', (WidgetTester tester) async { testWidgets('tap-select a day', (WidgetTester tester) async {
...@@ -112,4 +113,94 @@ void main() { ...@@ -112,4 +113,94 @@ void main() {
await tester.pump(const Duration(seconds: 5)); await tester.pump(const Duration(seconds: 5));
}); });
Future<Null> preparePicker(WidgetTester tester, Future<Null> callback(Future<DateTime> date)) async {
BuildContext buttonContext;
await tester.pumpWidget(new MaterialApp(
home: new Material(
child: new Builder(
builder: (BuildContext context) {
return new RaisedButton(
onPressed: () {
buttonContext = context;
},
child: new Text('Go'),
);
},
),
),
));
await tester.tap(find.text('Go'));
expect(buttonContext, isNotNull);
Future<DateTime> date = showDatePicker(
context: buttonContext,
initialDate: new DateTime(2016, DateTime.JANUARY, 15),
firstDate: new DateTime(2001, DateTime.JANUARY, 1),
lastDate: new DateTime(2031, DateTime.DECEMBER, 31),
);
await tester.pumpUntilNoTransientCallbacks(const Duration(seconds: 1));
await callback(date);
// TODO(abarth): Remove this call once https://github.com/flutter/flutter/issues/7457 is fixed.
await tester.pumpUntilNoTransientCallbacks(const Duration(seconds: 1));
}
testWidgets('Initial date is the default', (WidgetTester tester) async {
await preparePicker(tester, (Future<DateTime> date) async {
await tester.tap(find.text('OK'));
expect(await date, equals(new DateTime(2016, DateTime.JANUARY, 15)));
});
});
testWidgets('Can cancel', (WidgetTester tester) async {
await preparePicker(tester, (Future<DateTime> date) async {
await tester.tap(find.text('CANCEL'));
expect(await date, isNull);
});
});
testWidgets('Can select a day', (WidgetTester tester) async {
await preparePicker(tester, (Future<DateTime> date) async {
await tester.tap(find.text('12'));
await tester.tap(find.text('OK'));
expect(await date, equals(new DateTime(2016, DateTime.JANUARY, 12)));
});
});
testWidgets('Can select a month', (WidgetTester tester) async {
await preparePicker(tester, (Future<DateTime> date) async {
await tester.tap(find.byTooltip('Previous month'));
await tester.pumpUntilNoTransientCallbacks(const Duration(seconds: 1));
await tester.tap(find.text('25'));
await tester.tap(find.text('OK'));
expect(await date, equals(new DateTime(2015, DateTime.DECEMBER, 25)));
});
});
testWidgets('Can select a year', (WidgetTester tester) async {
await preparePicker(tester, (Future<DateTime> date) async {
await tester.tap(find.text('2016'));
await tester.pump();
await tester.tap(find.text('2006'));
await tester.tap(find.text('OK'));
expect(await date, equals(new DateTime(2006, DateTime.JANUARY, 15)));
});
});
testWidgets('Can select a year and then a day', (WidgetTester tester) async {
await preparePicker(tester, (Future<DateTime> date) async {
await tester.tap(find.text('2016'));
await tester.pump();
await tester.tap(find.text('2005'));
await tester.pump();
String dayLabel = new DateFormat('E, MMM\u00a0d').format(new DateTime(2005, DateTime.JANUARY, 15));
await tester.tap(find.text(dayLabel));
await tester.pump();
await tester.tap(find.text('19'));
await tester.tap(find.text('OK'));
expect(await date, equals(new DateTime(2005, DateTime.JANUARY, 19)));
});
});
} }
...@@ -6,7 +6,8 @@ import 'package:flutter/material.dart'; ...@@ -6,7 +6,8 @@ import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
void main() { void main() {
testWidgets('Floating Action Button control test', (WidgetTester tester) async { testWidgets('Floating Action Button control test',
(WidgetTester tester) async {
bool didPressButton = false; bool didPressButton = false;
await tester.pumpWidget( await tester.pumpWidget(
new Center( new Center(
...@@ -14,13 +15,30 @@ void main() { ...@@ -14,13 +15,30 @@ void main() {
onPressed: () { onPressed: () {
didPressButton = true; didPressButton = true;
}, },
child: new Icon(Icons.add) child: new Icon(Icons.add),
) ),
) ),
); );
expect(didPressButton, isFalse); expect(didPressButton, isFalse);
await tester.tap(find.byType(Icon)); await tester.tap(find.byType(Icon));
expect(didPressButton, isTrue); expect(didPressButton, isTrue);
}); });
testWidgets('Floating Action Button tooltip', (WidgetTester tester) async {
await tester.pumpWidget(
new MaterialApp(
home: new Scaffold(
floatingActionButton: new FloatingActionButton(
onPressed: null,
tooltip: 'Add',
child: new Icon(Icons.add),
),
),
),
);
await tester.tap(find.byType(Icon));
expect(find.byTooltip('Add'), findsOneWidget);
});
} }
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
void main() {
testWidgets('GridTile control test', (WidgetTester tester) async {
Key headerKey = new UniqueKey();
Key footerKey = new UniqueKey();
await tester.pumpWidget(new GridTile(
header: new GridTileBar(
key: headerKey,
leading: new Icon(Icons.thumb_up),
title: new Text('Header'),
subtitle: new Text('Subtitle'),
trailing: new Icon(Icons.thumb_up),
),
child: new DecoratedBox(
decoration: new BoxDecoration(
backgroundColor: Colors.green[500],
),
),
footer: new GridTileBar(
key: footerKey,
title: new Text('Footer'),
backgroundColor: Colors.black38,
),
));
expect(find.text('Header'), findsOneWidget);
expect(find.text('Footer'), findsOneWidget);
expect(tester.getBottomLeft(find.byKey(headerKey)).y,
lessThan(tester.getTopLeft(find.byKey(footerKey)).y));
await tester.pumpWidget(new GridTile(child: new Text('Simple')));
expect(find.text('Simple'), findsOneWidget);
});
}
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
void main() {
testWidgets('Radio control test', (WidgetTester tester) async {
Key key = new UniqueKey();
List<int> log = <int>[];
await tester.pumpWidget(new Material(
child: new Center(
child: new Radio<int>(
key: key,
value: 1,
groupValue: 2,
onChanged: (int value) {
log.add(value);
},
),
),
));
await tester.tap(find.byKey(key));
expect(log, equals(<int>[1]));
log.clear();
await tester.pumpWidget(new Material(
child: new Center(
child: new Radio<int>(
key: key,
value: 1,
groupValue: 1,
onChanged: (int value) {
log.add(value);
},
activeColor: Colors.green[500],
),
),
));
await tester.tap(find.byKey(key));
expect(log, isEmpty);
await tester.pumpWidget(new Material(
child: new Center(
child: new Radio<int>(
key: key,
value: 1,
groupValue: 2,
onChanged: null,
),
),
));
await tester.tap(find.byKey(key));
expect(log, isEmpty);
});
}
...@@ -19,4 +19,44 @@ void main() { ...@@ -19,4 +19,44 @@ void main() {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light); SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
expect(tester.binding.microtaskCount, equals(0)); expect(tester.binding.microtaskCount, equals(0));
}); });
test('setPreferredOrientations control test', () async {
List<String> log = <String>[];
PlatformMessages.setMockStringMessageHandler('flutter/platform', (String message) async {
log.add(message);
});
await SystemChrome.setPreferredOrientations(<DeviceOrientation>[
DeviceOrientation.portraitUp,
]);
expect(log, equals(<String>['{"method":"SystemChrome.setPreferredOrientations","args":[["DeviceOrientation.portraitUp"]]}']));
});
test('setApplicationSwitcherDescription control test', () async {
List<String> log = <String>[];
PlatformMessages.setMockStringMessageHandler('flutter/platform', (String message) async {
log.add(message);
});
await SystemChrome.setApplicationSwitcherDescription(
new ApplicationSwitcherDescription(label: 'Example label', primaryColor: 0xFF00FF00)
);
expect(log, equals(<String>['{"method":"SystemChrome.setApplicationSwitcherDescription","args":[{"label":"Example label","primaryColor":4278255360}]}']));
});
test('setEnabledSystemUIOverlays control test', () async {
List<String> log = <String>[];
PlatformMessages.setMockStringMessageHandler('flutter/platform', (String message) async {
log.add(message);
});
await SystemChrome.setEnabledSystemUIOverlays(<SystemUiOverlay>[SystemUiOverlay.top]);
expect(log, equals(<String>['{"method":"SystemChrome.setEnabledSystemUIOverlays","args":[["SystemUiOverlay.top"]]}']));
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment