Skip to content

Commit

Permalink
Remove unnecessary null checks in flutter/test (#118905)
Browse files Browse the repository at this point in the history
  • Loading branch information
goderbauer committed Jan 20, 2023
1 parent 288a773 commit bb73121
Show file tree
Hide file tree
Showing 41 changed files with 48 additions and 133 deletions.
1 change: 0 additions & 1 deletion packages/flutter/test/cupertino/route_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1830,7 +1830,6 @@ void main() {
child: const Text('Home'),
onPressed: () {
navigator = Navigator.of(context);
assert(navigator != null);
navigator.push<void>(r);
},
);
Expand Down
2 changes: 0 additions & 2 deletions packages/flutter/test/gestures/locking_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class TestGestureFlutterBinding extends BindingBase with GestureBinding {
);

Future<void> test(VoidCallback callback) {
assert(callback != null);
return _binding.lockEvents(() async {
GestureBinding.instance.platformDispatcher.onPointerDataPacket?.call(packet);
callback();
Expand All @@ -41,7 +40,6 @@ late TestGestureFlutterBinding _binding;

void main() {
_binding = TestGestureFlutterBinding();
assert(GestureBinding.instance != null);

test('Pointer events are locked during reassemble', () async {
final List<PointerEvent> events = <PointerEvent>[];
Expand Down
3 changes: 1 addition & 2 deletions packages/flutter/test/gestures/velocity_tracker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ bool _withinTolerance(double actual, double expected) {
}

bool _checkVelocity(Velocity actual, Offset expected) {
return (actual != null)
&& _withinTolerance(actual.pixelsPerSecond.dx, expected.dx)
return _withinTolerance(actual.pixelsPerSecond.dx, expected.dx)
&& _withinTolerance(actual.pixelsPerSecond.dy, expected.dy);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2491,7 +2491,6 @@ void main() {
}

Widget boilerplate({ Widget? bottomNavigationBar, required TextDirection textDirection }) {
assert(textDirection != null);
return MaterialApp(
home: Localizations(
locale: const Locale('en', 'US'),
Expand Down
6 changes: 3 additions & 3 deletions packages/flutter/test/material/chip_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1506,7 +1506,7 @@ void main() {
StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
return RawChip(
avatar: avatar,
onSelected: selectable != null
onSelected: selectable
? (bool value) {
setState(() {
selected = value;
Expand Down Expand Up @@ -1585,7 +1585,7 @@ void main() {
children: <Widget>[
StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
return RawChip(
onSelected: selectable != null
onSelected: selectable
? (bool value) {
setState(() {
selected = value;
Expand Down Expand Up @@ -1659,7 +1659,7 @@ void main() {
StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
return RawChip(
avatar: avatar,
onSelected: selectable != null
onSelected: selectable
? (bool value) {
setState(() {
selected = value;
Expand Down
11 changes: 0 additions & 11 deletions packages/flutter/test/material/dropdown_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,6 @@ void main() {
expect(itemBoxes.length, equals(2));
for (final RenderBox itemBox in itemBoxes) {
assert(itemBox.attached);
assert(textDirection != null);
switch (textDirection) {
case TextDirection.rtl:
expect(
Expand Down Expand Up @@ -1145,13 +1144,11 @@ void main() {
tester.element(find.byType(ListView)).visitAncestorElements((Element element) {
if (element.toString().startsWith('_DropdownMenu')) {
final RenderBox box = element.findRenderObject()! as RenderBox;
assert(box != null);
menuRect = box.localToGlobal(Offset.zero) & box.size;
return false;
}
return true;
});
assert(menuRect != null);
return menuRect;
}

Expand Down Expand Up @@ -1859,9 +1856,7 @@ void main() {
double getMenuScroll() {
double scrollPosition;
final ScrollController scrollController = PrimaryScrollController.of(tester.element(find.byType(ListView)));
assert(scrollController != null);
scrollPosition = scrollController.position.pixels;
assert(scrollPosition != null);
return scrollPosition;
}

Expand Down Expand Up @@ -1895,9 +1890,7 @@ void main() {
double getMenuScroll() {
double scrollPosition;
final ScrollController scrollController = PrimaryScrollController.of(tester.element(find.byType(ListView)));
assert(scrollController != null);
scrollPosition = scrollController.position.pixels;
assert(scrollPosition != null);
return scrollPosition;
}

Expand Down Expand Up @@ -1932,9 +1925,7 @@ void main() {
double getMenuScroll() {
double scrollPosition;
final ScrollController scrollController = PrimaryScrollController.of(tester.element(find.byType(ListView)));
assert(scrollController != null);
scrollPosition = scrollController.position.pixels;
assert(scrollPosition != null);
return scrollPosition;
}

Expand Down Expand Up @@ -1969,9 +1960,7 @@ void main() {
double getMenuScroll() {
double scrollPosition;
final ScrollController scrollController = PrimaryScrollController.of(tester.element(find.byType(ListView)));
assert(scrollController != null);
scrollPosition = scrollController.position.pixels;
assert(scrollPosition != null);
return scrollPosition;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ void main() {
);
final Iterable<double> currentRotations = rotationTransitions.map((RotationTransition t) => t.turns.value);

if (previousRotations != null && previousRotations!.isNotEmpty
&& currentRotations != null && currentRotations.isNotEmpty
if (previousRotations != null && previousRotations!.isNotEmpty && currentRotations.isNotEmpty
&& previousRect != null && currentRect != null) {
final List<double> deltas = <double>[];
for (final double currentRotation in currentRotations) {
Expand Down Expand Up @@ -1739,7 +1738,6 @@ class _StartTopFloatingActionButtonLocation extends FloatingActionButtonLocation
@override
Offset getOffset(ScaffoldPrelayoutGeometry scaffoldGeometry) {
double fabX;
assert(scaffoldGeometry.textDirection != null);
switch (scaffoldGeometry.textDirection) {
case TextDirection.rtl:
final double startPadding = kFloatingActionButtonMargin + scaffoldGeometry.minInsets.right;
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/test/material/scaffold_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2774,7 +2774,7 @@ class _CustomPageRoute<T> extends PageRoute<T> {
RouteSettings super.settings = const RouteSettings(),
this.maintainState = true,
super.fullscreenDialog,
}) : assert(builder != null);
});

final WidgetBuilder builder;

Expand Down
4 changes: 1 addition & 3 deletions packages/flutter/test/material/tooltip_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1340,9 +1340,7 @@ void main() {
const Duration waitDuration = Duration(seconds: 1);
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
addTearDown(() async {
if (gesture != null) {
return gesture.removePointer();
}
return gesture.removePointer();
});
await gesture.addPointer();
await gesture.moveTo(const Offset(1.0, 1.0));
Expand Down
8 changes: 2 additions & 6 deletions packages/flutter/test/material/tooltip_visibility_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ void main() {
testWidgets('Tooltip does not build MouseRegion when mouse is detected and in TooltipVisibility with visibility = false', (WidgetTester tester) async {
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
addTearDown(() async {
if (gesture != null) {
return gesture.removePointer();
}
return gesture.removePointer();
});
await gesture.addPointer();
await gesture.moveTo(const Offset(1.0, 1.0));
Expand Down Expand Up @@ -45,9 +43,7 @@ void main() {
const Duration waitDuration = Duration.zero;
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
addTearDown(() async {
if (gesture != null) {
return gesture.removePointer();
}
return gesture.removePointer();
});
await gesture.addPointer();
await gesture.moveTo(const Offset(1.0, 1.0));
Expand Down
1 change: 0 additions & 1 deletion packages/flutter/test/painting/decoration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ void main() {
test('BoxDecoration backgroundImage clip', () async {
final ui.Image image = await createTestImage(width: 100, height: 100);
void testDecoration({ BoxShape shape = BoxShape.rectangle, BorderRadius? borderRadius, required bool expectClip }) {
assert(shape != null);
FakeAsync().run((FakeAsync async) async {
final DelayedImageProvider imageProvider = DelayedImageProvider(image);
final DecorationImage backgroundImage = DecorationImage(image: imageProvider);
Expand Down
3 changes: 1 addition & 2 deletions packages/flutter/test/painting/mocks_for_image_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ class TestImageInfo extends ImageInfo {
}

class TestImageProvider extends ImageProvider<int> {
const TestImageProvider(this.key, this.imageValue, { required this.image })
: assert(image != null);
const TestImageProvider(this.key, this.imageValue, { required this.image });

final int key;
final int imageValue;
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/test/rendering/mock_canvas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ class _PathMatcher extends Matcher {
}

class _MismatchedCall {
const _MismatchedCall(this.message, this.callIntroduction, this.call) : assert(call != null);
const _MismatchedCall(this.message, this.callIntroduction, this.call);
final String message;
final String callIntroduction;
final RecordedInvocation call;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,7 @@ ui.PointerData _pointerData(
}

class _CursorUpdateDetails extends MethodCall {
const _CursorUpdateDetails(super.method, Map<String, dynamic> super.arguments)
: assert(arguments != null);
const _CursorUpdateDetails(super.method, Map<String, dynamic> super.arguments);

_CursorUpdateDetails.wrap(MethodCall call)
: super(call.method, Map<String, dynamic>.from(call.arguments as Map<dynamic, dynamic>));
Expand Down
15 changes: 4 additions & 11 deletions packages/flutter/test/rendering/mouse_tracker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,13 @@ void main() {
}) {
final TestAnnotationTarget oneAnnotation = TestAnnotationTarget(
onEnter: (PointerEnterEvent event) {
if (logEvents != null) {
logEvents.add(event);
}
logEvents.add(event);
},
onHover: (PointerHoverEvent event) {
if (logEvents != null) {
logEvents.add(event);
}
logEvents.add(event);
},
onExit: (PointerExitEvent event) {
if (logEvents != null) {
logEvents.add(event);
}
logEvents.add(event);
},
);
setUpMouseAnnotationFinder(
Expand Down Expand Up @@ -608,8 +602,7 @@ ui.PointerData _pointerData(
}

class BaseEventMatcher extends Matcher {
BaseEventMatcher(this.expected)
: assert(expected != null);
BaseEventMatcher(this.expected);

final PointerEvent expected;

Expand Down
2 changes: 0 additions & 2 deletions packages/flutter/test/rendering/recording_canvas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class RecordedInvocation {

/// Converts [stack] to a string using the [FlutterError.defaultStackFilter] logic.
String stackToString({ String indent = '' }) {
assert(indent != null);
return indent + FlutterError.defaultStackFilter(
stack.toString().trimRight().split('\n'),
).join('\n$indent');
Expand Down Expand Up @@ -124,7 +123,6 @@ class TestRecordingPaintingContext extends ClipContext implements PaintingContex
Clip clipBehavior = Clip.antiAlias,
ClipRRectLayer? oldLayer,
}) {
assert(clipBehavior != null);
clipRRectAndPaint(clipRRect.shift(offset), clipBehavior, bounds.shift(offset), () => painter(this, offset));
return null;
}
Expand Down
5 changes: 1 addition & 4 deletions packages/flutter/test/rendering/rendering_tester.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,12 @@ class TestRenderingFlutterBinding extends BindingBase with SchedulerBinding, Ser
///
/// If `onErrors` is not null, it is set as [TestRenderingFlutterBinding.onError].
void layout(
RenderBox box, {
RenderBox box, { // If you want to just repump the last box, call pumpFrame().
BoxConstraints? constraints,
Alignment alignment = Alignment.center,
EnginePhase phase = EnginePhase.layout,
VoidCallback? onErrors,
}) {
assert(box != null); // If you want to just repump the last box, call pumpFrame().
assert(box.parent == null); // We stick the box in another, so you can't reuse it easily, sorry.

TestRenderingFlutterBinding.instance.renderView.child = null;
Expand All @@ -225,8 +224,6 @@ void layout(
///
/// If `onErrors` is not null, it is set as [TestRenderingFlutterBinding.onError].
void pumpFrame({ EnginePhase phase = EnginePhase.layout, VoidCallback? onErrors }) {
assert(TestRenderingFlutterBinding.instance != null);
assert(TestRenderingFlutterBinding.instance.renderView != null);
assert(TestRenderingFlutterBinding.instance.renderView.child != null); // call layout() first!

if (onErrors != null) {
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/test/services/text_input_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

class FakeTextChannel implements MethodChannel {
FakeTextChannel(this.outgoing) : assert(outgoing != null);
FakeTextChannel(this.outgoing);

Future<dynamic> Function(MethodCall) outgoing;
Future<void> Function(MethodCall)? incoming;
Expand Down
3 changes: 1 addition & 2 deletions packages/flutter/test/widgets/actions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1869,8 +1869,7 @@ class ThirdTestIntent extends SecondTestIntent {
class TestAction extends CallbackAction<TestIntent> {
TestAction({
required OnInvokeCallback onInvoke,
}) : assert(onInvoke != null),
super(onInvoke: onInvoke);
}) : super(onInvoke: onInvoke);

@override
bool isEnabled(TestIntent intent) => enabled;
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/test/widgets/autocomplete_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ void main() {
home: Scaffold(
body: RawAutocomplete<String>(
optionsBuilder: (TextEditingValue textEditingValue) {
if (textEditingValue.text == null || textEditingValue.text == '') {
if (textEditingValue.text == '') {
return const Iterable<String>.empty();
}
return kOptions.where((String option) {
Expand Down
4 changes: 1 addition & 3 deletions packages/flutter/test/widgets/baseline_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ class RenderBaselineDetector extends RenderBox {

@override
double computeDistanceToActualBaseline(TextBaseline baseline) {
if (callback != null) {
callback();
}
callback();
return 20.0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class LayoutWithMissingId extends ParentDataWidget<MultiChildLayoutParentData> {
const LayoutWithMissingId({
super.key,
required super.child,
}) : assert(child != null);
});

@override
void applyParentData(RenderObject renderObject) {}
Expand Down
4 changes: 0 additions & 4 deletions packages/flutter/test/widgets/dismissible_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ Future<void> dismissItem(
required AxisDirection gestureDirection,
DismissMethod mechanism = dismissElement,
}) async {
assert(gestureDirection != null);
final Finder itemFinder = find.text(item.toString());
expect(itemFinder, findsOneWidget);

Expand All @@ -188,7 +187,6 @@ Future<void> dragItem(
required AxisDirection gestureDirection,
required double amount,
}) async {
assert(gestureDirection != null);
final Finder itemFinder = find.text(item.toString());
expect(itemFinder, findsOneWidget);

Expand All @@ -202,7 +200,6 @@ Future<void> checkFlingItemBeforeMovementEnd(
required AxisDirection gestureDirection,
DismissMethod mechanism = rollbackElement,
}) async {
assert(gestureDirection != null);
final Finder itemFinder = find.text(item.toString());
expect(itemFinder, findsOneWidget);

Expand All @@ -218,7 +215,6 @@ Future<void> checkFlingItemAfterMovement(
required AxisDirection gestureDirection,
DismissMethod mechanism = rollbackElement,
}) async {
assert(gestureDirection != null);
final Finder itemFinder = find.text(item.toString());
expect(itemFinder, findsOneWidget);

Expand Down
4 changes: 1 addition & 3 deletions packages/flutter/test/widgets/fade_in_image_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import '../painting/image_test_utils.dart';
const Duration animationDuration = Duration(milliseconds: 50);

class FadeInImageParts {
const FadeInImageParts(this.fadeInImageElement, this.placeholder, this.target)
: assert(fadeInImageElement != null),
assert(target != null);
const FadeInImageParts(this.fadeInImageElement, this.placeholder, this.target);

final ComponentElement fadeInImageElement;
final FadeInImageElements? placeholder;
Expand Down

0 comments on commit bb73121

Please sign in to comment.