Unverified Commit ca065f64 authored by Darren Austin's avatar Darren Austin Committed by GitHub

Migrate more Material framework tests to null safety. (#67790)

* Migrate more Material framework tests to null safety.
parent ef119187
......@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.8
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
......@@ -12,7 +10,7 @@ import 'package:flutter_test/flutter_test.dart';
import '../widgets/semantics_tester.dart';
class MockClipboard {
Object _clipboardData = <String, dynamic>{
dynamic _clipboardData = <String, dynamic>{
'text': null,
};
......@@ -66,7 +64,7 @@ void main() {
expect(selectedResults, hasLength(0));
final TextField textField = tester.widget(find.byType(TextField));
expect(textField.focusNode.hasFocus, isTrue);
expect(textField.focusNode!.hasFocus, isTrue);
// Close search
await tester.tap(find.byTooltip('Back'));
......@@ -104,7 +102,7 @@ void main() {
// Simulate system back button
final ByteData message = const JSONMethodCodec().encodeMethodCall(const MethodCall('popRoute'));
await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage('flutter/navigation', message, (_) { });
await ServicesBinding.instance!.defaultBinaryMessenger.handlePlatformMessage('flutter/navigation', message, (_) { });
await tester.pumpAndSettle();
expect(selectedResults, <void>[null]);
......@@ -133,7 +131,7 @@ void main() {
await tester.pumpAndSettle();
final TextField textField = tester.widget<TextField>(find.byType(TextField));
final Color hintColor = textField.decoration.hintStyle.color;
final Color hintColor = textField.decoration!.hintStyle!.color!;
expect(hintColor, delegate.hintTextColor);
});
......@@ -188,7 +186,7 @@ void main() {
expect(find.text('Results'), findsOneWidget);
final TextField textField = tester.widget(find.byType(TextField));
expect(textField.focusNode.hasFocus, isFalse);
expect(textField.focusNode!.hasFocus, isFalse);
expect(delegate.queriesForResults, <String>['Wow']);
// Close search
......@@ -236,14 +234,14 @@ void main() {
expect(delegate.queriesForResults, <String>['Wow']);
TextField textField = tester.widget(find.byType(TextField));
expect(textField.focusNode.hasFocus, isFalse);
expect(textField.focusNode!.hasFocus, isFalse);
// Tapping search field to go back to suggestions
await tester.tap(find.byType(TextField));
await tester.pumpAndSettle();
textField = tester.widget(find.byType(TextField));
expect(textField.focusNode.hasFocus, isTrue);
expect(textField.focusNode!.hasFocus, isTrue);
expect(find.text('Suggestions'), findsOneWidget);
expect(find.text('Results'), findsNothing);
......@@ -269,7 +267,7 @@ void main() {
expect(delegate.queriesForResults, <String>['Wow', 'Foo']);
textField = tester.widget(find.byType(TextField));
expect(textField.focusNode.hasFocus, isFalse);
expect(textField.focusNode!.hasFocus, isFalse);
});
testWidgets('Fresh search always starts with empty query', (WidgetTester tester) async {
......@@ -440,8 +438,8 @@ void main() {
});
testWidgets('Closing search with nested search shown goes back to underlying route', (WidgetTester tester) async {
_TestSearchDelegate delegate;
final List<String> nestedSearchResults = <String>[];
late _TestSearchDelegate delegate;
final List<String?> nestedSearchResults = <String?>[];
final _TestSearchDelegate nestedSearchDelegate = _TestSearchDelegate(
suggestions: 'Nested Suggestions',
result: 'Nested Result',
......@@ -507,7 +505,7 @@ void main() {
expect(find.text('HomeBody'), findsOneWidget);
expect(find.text('Suggestions'), findsNothing);
expect(find.text('Nested Suggestions'), findsNothing);
expect(nestedSearchResults, <String>[null]);
expect(nestedSearchResults, <String?>[null]);
expect(selectedResults, <String>['Result Foo']);
});
......@@ -554,7 +552,7 @@ void main() {
await tester.pumpAndSettle();
final TextField textField = tester.widget<TextField>(find.byType(TextField));
final TextStyle hintStyle = textField.decoration.hintStyle;
final TextStyle hintStyle = textField.decoration!.hintStyle!;
expect(hintStyle, delegate.searchFieldStyle);
});
......@@ -569,7 +567,7 @@ void main() {
await tester.showKeyboard(find.byType(TextField));
expect(tester.testTextInput.setClientArgs['inputAction'], TextInputAction.search.toString());
expect(tester.testTextInput.setClientArgs!['inputAction'], TextInputAction.search.toString());
});
testWidgets('Custom textInputAction results in keyboard with corresponding button', (WidgetTester tester) async {
......@@ -581,11 +579,11 @@ void main() {
await tester.tap(find.byTooltip('Search'));
await tester.pumpAndSettle();
await tester.showKeyboard(find.byType(TextField));
expect(tester.testTextInput.setClientArgs['inputAction'], TextInputAction.done.toString());
expect(tester.testTextInput.setClientArgs!['inputAction'], TextInputAction.done.toString());
});
group('contributes semantics', () {
TestSemantics buildExpected({ String routeName }) {
TestSemantics buildExpected({ required String routeName }) {
return TestSemantics.root(
children: <TestSemantics>[
TestSemantics(
......@@ -697,17 +695,17 @@ void main() {
class TestHomePage extends StatelessWidget {
const TestHomePage({
Key key,
Key? key,
this.results,
this.delegate,
required this.delegate,
this.passInInitialQuery = false,
this.initialQuery,
}) : super(key: key);
final List<String> results;
final List<String>? results;
final SearchDelegate<String> delegate;
final bool passInInitialQuery;
final String initialQuery;
final String? initialQuery;
@override
Widget build(BuildContext context) {
......@@ -751,8 +749,8 @@ class _TestSearchDelegate extends SearchDelegate<String> {
this.suggestions = 'Suggestions',
this.result = 'Result',
this.actions = const <Widget>[],
TextStyle searchFieldStyle,
String searchHint,
TextStyle? searchFieldStyle,
String? searchHint,
TextInputAction textInputAction = TextInputAction.search,
}) : super(searchFieldLabel: searchHint, textInputAction: textInputAction, searchFieldStyle: searchFieldStyle);
......@@ -763,7 +761,7 @@ class _TestSearchDelegate extends SearchDelegate<String> {
@override
ThemeData appBarTheme(BuildContext context) {
final ThemeData theme = Theme.of(context);
final ThemeData theme = Theme.of(context)!;
return theme.copyWith(
inputDecorationTheme: InputDecorationTheme(hintStyle: TextStyle(color: hintTextColor)),
);
......
......@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.8
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
......@@ -73,7 +71,7 @@ void main() {
builder: (BuildContext context) {
return GestureDetector(
onTap: () {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
ScaffoldMessenger.of(context)!.showSnackBar(SnackBar(
content: const Text(text),
duration: const Duration(seconds: 2),
action: SnackBarAction(label: 'ACTION', onPressed: () {}),
......@@ -111,7 +109,7 @@ void main() {
builder: (BuildContext context) {
return GestureDetector(
onTap: () {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
ScaffoldMessenger.of(context)!.showSnackBar(SnackBar(
content: const Text(text),
duration: const Duration(seconds: 2),
action: SnackBarAction(label: action, onPressed: () {}),
......@@ -136,7 +134,7 @@ void main() {
expect(material.color, snackBarTheme.backgroundColor);
expect(material.elevation, snackBarTheme.elevation);
expect(material.shape, snackBarTheme.shape);
expect(button.text.style.color, snackBarTheme.actionTextColor);
expect(button.text.style!.color, snackBarTheme.actionTextColor);
});
testWidgets('SnackBar widget properties take priority over theme', (WidgetTester tester) async {
......@@ -155,7 +153,7 @@ void main() {
builder: (BuildContext context) {
return GestureDetector(
onTap: () {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
ScaffoldMessenger.of(context)!.showSnackBar(SnackBar(
backgroundColor: backgroundColor,
elevation: elevation,
shape: shape,
......@@ -185,7 +183,7 @@ void main() {
expect(material.color, backgroundColor);
expect(material.elevation, elevation);
expect(material.shape, shape);
expect(button.text.style.color, textColor);
expect(button.text.style!.color, textColor);
});
testWidgets('SnackBar theme behavior is correct for floating', (WidgetTester tester) async {
......@@ -202,7 +200,7 @@ void main() {
builder: (BuildContext context) {
return GestureDetector(
onTap: () {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
ScaffoldMessenger.of(context)!.showSnackBar(SnackBar(
content: const Text('I am a snack bar.'),
duration: const Duration(seconds: 2),
action: SnackBarAction(label: 'ACTION', onPressed: () {}),
......@@ -244,7 +242,7 @@ void main() {
builder: (BuildContext context) {
return GestureDetector(
onTap: () {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
ScaffoldMessenger.of(context)!.showSnackBar(SnackBar(
content: const Text('I am a snack bar.'),
duration: const Duration(seconds: 2),
action: SnackBarAction(label: 'ACTION', onPressed: () {}),
......
......@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.8
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
......@@ -383,7 +381,7 @@ void main() {
}
final ControlsWidgetBuilder builder =
(BuildContext context, { VoidCallback onStepContinue, VoidCallback onStepCancel }) {
(BuildContext context, { VoidCallback? onStepContinue, VoidCallback? onStepCancel }) {
return Container(
margin: const EdgeInsets.only(top: 16.0),
child: ConstrainedBox(
......@@ -477,8 +475,8 @@ void main() {
});
testWidgets('Nested stepper error test', (WidgetTester tester) async {
FlutterErrorDetails errorDetails;
final FlutterExceptionHandler oldHandler = FlutterError.onError;
late FlutterErrorDetails errorDetails;
final FlutterExceptionHandler? oldHandler = FlutterError.onError;
FlutterError.onError = (FlutterErrorDetails details) {
errorDetails = details;
};
......@@ -518,7 +516,6 @@ void main() {
FlutterError.onError = oldHandler;
}
expect(errorDetails, isNotNull);
expect(errorDetails.stack, isNotNull);
// Check the ErrorDetails without the stack trace
final String fullErrorMessage = errorDetails.toString();
......@@ -627,7 +624,7 @@ void main() {
);
await tester.pump();
final FocusNode disabledNode = Focus.of(tester.element(find.text('Step 0')), nullOk: true, scopeOk: true);
final FocusNode disabledNode = Focus.of(tester.element(find.text('Step 0')), nullOk: true, scopeOk: true)!;
disabledNode.requestFocus();
await tester.pump();
expect(disabledNode.hasPrimaryFocus, isFalse);
......@@ -653,7 +650,7 @@ void main() {
);
await tester.pump();
final FocusNode disabledNode = Focus.of(tester.element(find.text('Step 0')), nullOk: true, scopeOk: true);
final FocusNode disabledNode = Focus.of(tester.element(find.text('Step 0')), nullOk: true, scopeOk: true)!;
disabledNode.requestFocus();
await tester.pump();
expect(disabledNode.hasPrimaryFocus, isFalse);
......@@ -748,26 +745,26 @@ void main() {
await tester.pumpWidget(buildFrame(ThemeData.light()));
expect(buttonMaterial('CONTINUE').color.value, 0xff2196f3);
expect(buttonMaterial('CONTINUE').textStyle.color.value, 0xffffffff);
expect(buttonMaterial('CONTINUE').color!.value, 0xff2196f3);
expect(buttonMaterial('CONTINUE').textStyle!.color!.value, 0xffffffff);
expect(buttonMaterial('CONTINUE').shape, buttonShape);
expect(tester.getRect(find.widgetWithText(TextButton, 'CONTINUE')), continueButtonRect);
expect(buttonMaterial('CANCEL').color.value, 0);
expect(buttonMaterial('CANCEL').textStyle.color.value, 0x8a000000);
expect(buttonMaterial('CANCEL').color!.value, 0);
expect(buttonMaterial('CANCEL').textStyle!.color!.value, 0x8a000000);
expect(buttonMaterial('CANCEL').shape, buttonShape);
expect(tester.getRect(find.widgetWithText(TextButton, 'CANCEL')), cancelButtonRect);
await tester.pumpWidget(buildFrame(ThemeData.dark()));
await tester.pumpAndSettle(); // Complete the theme animation.
expect(buttonMaterial('CONTINUE').color.value, 0);
expect(buttonMaterial('CONTINUE').textStyle.color.value, 0xffffffff);
expect(buttonMaterial('CONTINUE').color!.value, 0);
expect(buttonMaterial('CONTINUE').textStyle!.color!.value, 0xffffffff);
expect(buttonMaterial('CONTINUE').shape, buttonShape);
expect(tester.getRect(find.widgetWithText(TextButton, 'CONTINUE')), continueButtonRect);
expect(buttonMaterial('CANCEL').color.value, 0);
expect(buttonMaterial('CANCEL').textStyle.color.value, 0xb3ffffff);
expect(buttonMaterial('CANCEL').color!.value, 0);
expect(buttonMaterial('CANCEL').textStyle!.color!.value, 0xb3ffffff);
expect(buttonMaterial('CANCEL').shape, buttonShape);
expect(tester.getRect(find.widgetWithText(TextButton, 'CANCEL')), cancelButtonRect);
});
......@@ -802,20 +799,20 @@ void main() {
await tester.pumpWidget(buildFrame(ThemeData.light()));
expect(buttonMaterial('CONTINUE').color.value, 0);
expect(buttonMaterial('CONTINUE').textStyle.color.value, 0x61000000);
expect(buttonMaterial('CONTINUE').color!.value, 0);
expect(buttonMaterial('CONTINUE').textStyle!.color!.value, 0x61000000);
expect(buttonMaterial('CANCEL').color.value, 0);
expect(buttonMaterial('CANCEL').textStyle.color.value, 0x61000000);
expect(buttonMaterial('CANCEL').color!.value, 0);
expect(buttonMaterial('CANCEL').textStyle!.color!.value, 0x61000000);
await tester.pumpWidget(buildFrame(ThemeData.dark()));
await tester.pumpAndSettle(); // Complete the theme animation.
expect(buttonMaterial('CONTINUE').color.value, 0);
expect(buttonMaterial('CONTINUE').textStyle.color.value, 0x61ffffff);
expect(buttonMaterial('CONTINUE').color!.value, 0);
expect(buttonMaterial('CONTINUE').textStyle!.color!.value, 0x61ffffff);
expect(buttonMaterial('CANCEL').color.value, 0);
expect(buttonMaterial('CANCEL').textStyle.color.value, 0x61ffffff);
expect(buttonMaterial('CANCEL').color!.value, 0);
expect(buttonMaterial('CANCEL').textStyle!.color!.value, 0x61ffffff);
});
testWidgets('Vertical and Horizontal Stepper physics test', (WidgetTester tester) async {
......
......@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.8
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
......@@ -26,7 +24,7 @@ final List<SizedBox> _sizedTabs = <SizedBox>[
];
Widget _withTheme(
TabBarTheme theme, {
TabBarTheme? theme, {
List<Widget> tabs = _tabs,
bool isScrollable = false,
}) {
......@@ -56,21 +54,21 @@ void main() {
await tester.pumpWidget(_withTheme(null));
final RenderParagraph selectedRenderObject = tester.renderObject<RenderParagraph>(find.text(_tab1Text));
expect(selectedRenderObject.text.style.fontFamily, equals('Roboto'));
expect(selectedRenderObject.text.style.fontSize, equals(14.0));
expect(selectedRenderObject.text.style.color, equals(Colors.white));
expect(selectedRenderObject.text.style!.fontFamily, equals('Roboto'));
expect(selectedRenderObject.text.style!.fontSize, equals(14.0));
expect(selectedRenderObject.text.style!.color, equals(Colors.white));
final RenderParagraph unselectedRenderObject = tester.renderObject<RenderParagraph>(find.text(_tab2Text));
expect(unselectedRenderObject.text.style.fontFamily, equals('Roboto'));
expect(unselectedRenderObject.text.style.fontSize, equals(14.0));
expect(unselectedRenderObject.text.style.color, equals(Colors.white.withAlpha(0xB2)));
expect(unselectedRenderObject.text.style!.fontFamily, equals('Roboto'));
expect(unselectedRenderObject.text.style!.fontSize, equals(14.0));
expect(unselectedRenderObject.text.style!.color, equals(Colors.white.withAlpha(0xB2)));
// tests for the default value of labelPadding when tabBarTheme and tabBar do not provide one
await tester.pumpWidget(_withTheme(null, tabs: _sizedTabs, isScrollable: true));
const double indicatorWeight = 2.0;
final Rect tabBar = tester.getRect(find.byType(TabBar));
final Rect tabOneRect = tester.getRect(find.byKey(_sizedTabs[0].key));
final Rect tabTwoRect = tester.getRect(find.byKey(_sizedTabs[1].key));
final Rect tabOneRect = tester.getRect(find.byKey(_sizedTabs[0].key!));
final Rect tabTwoRect = tester.getRect(find.byKey(_sizedTabs[1].key!));
// verify coordinates of tabOne
expect(tabOneRect.left, equals(kTabLabelPadding.left));
......@@ -92,9 +90,9 @@ void main() {
await tester.pumpWidget(_withTheme(tabBarTheme));
final RenderParagraph textRenderObject = tester.renderObject<RenderParagraph>(find.text(_tab1Text));
expect(textRenderObject.text.style.color, equals(labelColor));
expect(textRenderObject.text.style!.color, equals(labelColor));
final RenderParagraph iconRenderObject = _iconRenderObject(tester, Icons.looks_one);
expect(iconRenderObject.text.style.color, equals(labelColor));
expect(iconRenderObject.text.style!.color, equals(labelColor));
});
testWidgets('Tab bar theme overrides label padding', (WidgetTester tester) async {
......@@ -117,8 +115,8 @@ void main() {
));
final Rect tabBar = tester.getRect(find.byType(TabBar));
final Rect tabOneRect = tester.getRect(find.byKey(_sizedTabs[0].key));
final Rect tabTwoRect = tester.getRect(find.byKey(_sizedTabs[1].key));
final Rect tabOneRect = tester.getRect(find.byKey(_sizedTabs[0].key!));
final Rect tabTwoRect = tester.getRect(find.byKey(_sizedTabs[1].key!));
// verify coordinates of tabOne
expect(tabOneRect.left, equals(leftPadding));
......@@ -145,9 +143,9 @@ void main() {
await tester.pumpWidget(_withTheme(tabBarTheme));
final RenderParagraph selectedRenderObject = tester.renderObject<RenderParagraph>(find.text(_tab1Text));
expect(selectedRenderObject.text.style.fontFamily, equals(labelStyle.fontFamily));
expect(selectedRenderObject.text.style!.fontFamily, equals(labelStyle.fontFamily));
final RenderParagraph unselectedRenderObject = tester.renderObject<RenderParagraph>(find.text(_tab2Text));
expect(unselectedRenderObject.text.style.fontFamily, equals(unselectedLabelStyle.fontFamily));
expect(unselectedRenderObject.text.style!.fontFamily, equals(unselectedLabelStyle.fontFamily));
});
testWidgets('Tab bar theme with just label style specified', (WidgetTester tester) async {
......@@ -160,11 +158,11 @@ void main() {
await tester.pumpWidget(_withTheme(tabBarTheme));
final RenderParagraph selectedRenderObject = tester.renderObject<RenderParagraph>(find.text(_tab1Text));
expect(selectedRenderObject.text.style.fontFamily, equals(labelStyle.fontFamily));
expect(selectedRenderObject.text.style!.fontFamily, equals(labelStyle.fontFamily));
final RenderParagraph unselectedRenderObject = tester.renderObject<RenderParagraph>(find.text(_tab2Text));
expect(unselectedRenderObject.text.style.fontFamily, equals('Roboto'));
expect(unselectedRenderObject.text.style.fontSize, equals(14.0));
expect(unselectedRenderObject.text.style.color, equals(Colors.white.withAlpha(0xB2)));
expect(unselectedRenderObject.text.style!.fontFamily, equals('Roboto'));
expect(unselectedRenderObject.text.style!.fontSize, equals(14.0));
expect(unselectedRenderObject.text.style!.color, equals(Colors.white.withAlpha(0xB2)));
});
testWidgets('Tab bar label styles override theme label styles', (WidgetTester tester) async {
......@@ -191,9 +189,9 @@ void main() {
);
final RenderParagraph selectedRenderObject = tester.renderObject<RenderParagraph>(find.text(_tab1Text));
expect(selectedRenderObject.text.style.fontFamily, equals(labelStyle.fontFamily));
expect(selectedRenderObject.text.style!.fontFamily, equals(labelStyle.fontFamily));
final RenderParagraph unselectedRenderObject = tester.renderObject<RenderParagraph>(find.text(_tab2Text));
expect(unselectedRenderObject.text.style.fontFamily, equals(unselectedLabelStyle.fontFamily));
expect(unselectedRenderObject.text.style!.fontFamily, equals(unselectedLabelStyle.fontFamily));
});
testWidgets('Tab bar label padding overrides theme label padding', (WidgetTester tester) async {
......@@ -233,8 +231,8 @@ void main() {
);
final Rect tabBar = tester.getRect(find.byType(TabBar));
final Rect tabOneRect = tester.getRect(find.byKey(_sizedTabs[0].key));
final Rect tabTwoRect = tester.getRect(find.byKey(_sizedTabs[1].key));
final Rect tabOneRect = tester.getRect(find.byKey(_sizedTabs[0].key!));
final Rect tabTwoRect = tester.getRect(find.byKey(_sizedTabs[1].key!));
// verify coordinates of tabOne
expect(tabOneRect.left, equals(horizontalPadding));
......@@ -257,9 +255,9 @@ void main() {
await tester.pumpWidget(_withTheme(tabBarTheme));
final RenderParagraph textRenderObject = tester.renderObject<RenderParagraph>(find.text(_tab2Text));
expect(textRenderObject.text.style.color, equals(unselectedLabelColor));
expect(textRenderObject.text.style!.color, equals(unselectedLabelColor));
final RenderParagraph iconRenderObject = _iconRenderObject(tester, Icons.looks_two);
expect(iconRenderObject.text.style.color, equals(unselectedLabelColor));
expect(iconRenderObject.text.style!.color, equals(unselectedLabelColor));
});
testWidgets('Tab bar theme overrides tab indicator size (tab)', (WidgetTester tester) async {
......
......@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.8
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
......@@ -26,7 +24,7 @@ class MySliverPersistentHeaderDelegate extends SliverPersistentHeaderDelegate {
}
class MyHomePage extends StatefulWidget {
const MyHomePage({ Key key }) : super(key: key);
const MyHomePage({ Key? key }) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
......@@ -34,7 +32,7 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
static const int tabCount = 3;
TabController tabController;
late TabController tabController;
@override
void initState() {
......
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