Improve test coverage again (#7458)
This patch adds tests for radio buttons, the date picker, as well as a number of other classes.
This commit is contained in:
@@ -672,10 +672,10 @@ class _DatePickerDialogState extends State<_DatePickerDialog> {
|
||||
/// * [showTimePicker]
|
||||
/// * <https://material.google.com/components/pickers.html#pickers-date-pickers>
|
||||
Future<DateTime> showDatePicker({
|
||||
BuildContext context,
|
||||
DateTime initialDate,
|
||||
DateTime firstDate,
|
||||
DateTime lastDate
|
||||
@required BuildContext context,
|
||||
@required DateTime initialDate,
|
||||
@required DateTime firstDate,
|
||||
@required DateTime lastDate
|
||||
}) async {
|
||||
return await showDialog(
|
||||
context: context,
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
import 'button.dart';
|
||||
import 'button_bar.dart';
|
||||
@@ -315,7 +316,10 @@ class _DialogRoute<T> extends PopupRoute<T> {
|
||||
/// * [AlertDialog], for dialogs that have a row of buttons below the body.
|
||||
/// * [Dialog], on which [SimpleDialog] and [AlertDialog] are based.
|
||||
/// * <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*/>(
|
||||
child: child,
|
||||
theme: Theme.of(context, shadowThemeOnly: true),
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('tap-select a day', (WidgetTester tester) async {
|
||||
@@ -112,4 +113,94 @@ void main() {
|
||||
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';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Floating Action Button control test', (WidgetTester tester) async {
|
||||
testWidgets('Floating Action Button control test',
|
||||
(WidgetTester tester) async {
|
||||
bool didPressButton = false;
|
||||
await tester.pumpWidget(
|
||||
new Center(
|
||||
@@ -14,13 +15,30 @@ void main() {
|
||||
onPressed: () {
|
||||
didPressButton = true;
|
||||
},
|
||||
child: new Icon(Icons.add)
|
||||
)
|
||||
)
|
||||
child: new Icon(Icons.add),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
expect(didPressButton, isFalse);
|
||||
await tester.tap(find.byType(Icon));
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
43
packages/flutter/test/material/grid_title_test.dart
Normal file
43
packages/flutter/test/material/grid_title_test.dart
Normal file
@@ -0,0 +1,43 @@
|
||||
// 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);
|
||||
});
|
||||
}
|
||||
64
packages/flutter/test/material/radio_test.dart
Normal file
64
packages/flutter/test/material/radio_test.dart
Normal file
@@ -0,0 +1,64 @@
|
||||
// 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() {
|
||||
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
|
||||
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"]]}']));
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user