Unverified Commit 2c59a3a7 authored by Darren Austin's avatar Darren Austin Committed by GitHub

Migrate Switch tests to null safety. (#68133)

parent f2d5f1f6
...@@ -149,7 +149,7 @@ class Switch extends StatefulWidget { ...@@ -149,7 +149,7 @@ class Switch extends StatefulWidget {
/// }, /// },
/// ) /// )
/// ``` /// ```
final ValueChanged<bool?>? onChanged; final ValueChanged<bool>? onChanged;
/// The color to use when this switch is on. /// The color to use when this switch is on.
/// ///
...@@ -440,7 +440,7 @@ class _SwitchRenderObjectWidget extends LeafRenderObjectWidget { ...@@ -440,7 +440,7 @@ class _SwitchRenderObjectWidget extends LeafRenderObjectWidget {
final Color activeTrackColor; final Color activeTrackColor;
final Color inactiveTrackColor; final Color inactiveTrackColor;
final ImageConfiguration configuration; final ImageConfiguration configuration;
final ValueChanged<bool?>? onChanged; final ValueChanged<bool>? onChanged;
final BoxConstraints additionalConstraints; final BoxConstraints additionalConstraints;
final DragStartBehavior dragStartBehavior; final DragStartBehavior dragStartBehavior;
final bool hasFocus; final bool hasFocus;
...@@ -463,7 +463,7 @@ class _SwitchRenderObjectWidget extends LeafRenderObjectWidget { ...@@ -463,7 +463,7 @@ class _SwitchRenderObjectWidget extends LeafRenderObjectWidget {
activeTrackColor: activeTrackColor, activeTrackColor: activeTrackColor,
inactiveTrackColor: inactiveTrackColor, inactiveTrackColor: inactiveTrackColor,
configuration: configuration, configuration: configuration,
onChanged: onChanged, onChanged: onChanged != null ? _handleValueChanged : null,
textDirection: Directionality.of(context)!, textDirection: Directionality.of(context)!,
additionalConstraints: additionalConstraints, additionalConstraints: additionalConstraints,
hasFocus: hasFocus, hasFocus: hasFocus,
...@@ -487,7 +487,7 @@ class _SwitchRenderObjectWidget extends LeafRenderObjectWidget { ...@@ -487,7 +487,7 @@ class _SwitchRenderObjectWidget extends LeafRenderObjectWidget {
..activeTrackColor = activeTrackColor ..activeTrackColor = activeTrackColor
..inactiveTrackColor = inactiveTrackColor ..inactiveTrackColor = inactiveTrackColor
..configuration = configuration ..configuration = configuration
..onChanged = onChanged ..onChanged = onChanged != null ? _handleValueChanged : null
..textDirection = Directionality.of(context)! ..textDirection = Directionality.of(context)!
..additionalConstraints = additionalConstraints ..additionalConstraints = additionalConstraints
..dragStartBehavior = dragStartBehavior ..dragStartBehavior = dragStartBehavior
...@@ -495,6 +495,17 @@ class _SwitchRenderObjectWidget extends LeafRenderObjectWidget { ...@@ -495,6 +495,17 @@ class _SwitchRenderObjectWidget extends LeafRenderObjectWidget {
..hovering = hovering ..hovering = hovering
..vsync = state; ..vsync = state;
} }
void _handleValueChanged(bool? value) {
// Wrap the onChanged callback because the RenderToggleable supports tri-state
// values (i.e. value can be null), but the Switch doesn't. We pass false
// for the tristate param to RenderToggleable, so value should never
// be null.
assert(value != null);
if (onChanged != null) {
onChanged!(value!);
}
}
} }
class _RenderSwitch extends RenderToggleable { class _RenderSwitch extends RenderToggleable {
...@@ -650,7 +661,6 @@ class _RenderSwitch extends RenderToggleable { ...@@ -650,7 +661,6 @@ class _RenderSwitch extends RenderToggleable {
} }
} }
@override @override
void detach() { void detach() {
_cachedThumbPainter?.dispose(); _cachedThumbPainter?.dispose();
......
...@@ -346,7 +346,7 @@ class SwitchListTile extends StatelessWidget { ...@@ -346,7 +346,7 @@ class SwitchListTile extends StatelessWidget {
/// title: Text('Selection'), /// title: Text('Selection'),
/// ) /// )
/// ``` /// ```
final ValueChanged<bool?>? onChanged; final ValueChanged<bool>? onChanged;
/// The color to use when this switch is on. /// The color to use when this switch is on.
/// ///
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
...@@ -12,7 +10,7 @@ import '../rendering/mock_canvas.dart'; ...@@ -12,7 +10,7 @@ import '../rendering/mock_canvas.dart';
import '../widgets/semantics_tester.dart'; import '../widgets/semantics_tester.dart';
Widget wrap({ Widget child }) { Widget wrap({ required Widget child }) {
return MediaQuery( return MediaQuery(
data: const MediaQueryData(), data: const MediaQueryData(),
child: Directionality( child: Directionality(
...@@ -51,14 +49,14 @@ void main() { ...@@ -51,14 +49,14 @@ void main() {
), ),
CheckboxListTile( CheckboxListTile(
value: true, value: true,
onChanged: (bool value) { }, onChanged: (bool? value) { },
title: const Text('BBB'), title: const Text('BBB'),
secondary: const Text('bbb'), secondary: const Text('bbb'),
), ),
RadioListTile<bool>( RadioListTile<bool>(
value: true, value: true,
groupValue: false, groupValue: false,
onChanged: (bool value) { }, onChanged: (bool? value) { },
title: const Text('CCC'), title: const Text('CCC'),
secondary: const Text('ccc'), secondary: const Text('ccc'),
), ),
...@@ -278,7 +276,7 @@ void main() { ...@@ -278,7 +276,7 @@ void main() {
); );
await tester.pump(); await tester.pump();
expect(Focus.of(childKey.currentContext, nullOk: true).hasPrimaryFocus, isTrue); expect(Focus.of(childKey.currentContext!)!.hasPrimaryFocus, isTrue);
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( MaterialApp(
...@@ -298,7 +296,7 @@ void main() { ...@@ -298,7 +296,7 @@ void main() {
); );
await tester.pump(); await tester.pump();
expect(Focus.of(childKey.currentContext, nullOk: true).hasPrimaryFocus, isFalse); expect(Focus.of(childKey.currentContext!)!.hasPrimaryFocus, isFalse);
}); });
testWidgets('SwitchListTile controlAffinity test', (WidgetTester tester) async { testWidgets('SwitchListTile controlAffinity test', (WidgetTester tester) async {
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart'; import 'package:flutter/gestures.dart';
...@@ -318,7 +316,7 @@ void main() { ...@@ -318,7 +316,7 @@ void main() {
Material.of(tester.element(find.byType(Switch))), Material.of(tester.element(find.byType(Switch))),
paints paints
..rrect( ..rrect(
color: Colors.blue[600].withAlpha(0x80), color: Colors.blue[600]!.withAlpha(0x80),
rrect: RRect.fromLTRBR( rrect: RRect.fromLTRBR(
383.5, 293.0, 416.5, 307.0, const Radius.circular(7.0))) 383.5, 293.0, 416.5, 307.0, const Radius.circular(7.0)))
..circle(color: const Color(0x33000000)) ..circle(color: const Color(0x33000000))
...@@ -608,10 +606,10 @@ void main() { ...@@ -608,10 +606,10 @@ void main() {
expect(value, true); expect(value, true);
expect(semanticEvent, <String, dynamic>{ expect(semanticEvent, <String, dynamic>{
'type': 'tap', 'type': 'tap',
'nodeId': object.debugSemantics.id, 'nodeId': object.debugSemantics!.id,
'data': <String, dynamic>{}, 'data': <String, dynamic>{},
}); });
expect(object.debugSemantics.getSemanticsData().hasAction(SemanticsAction.tap), true); expect(object.debugSemantics!.getSemanticsData().hasAction(SemanticsAction.tap), true);
semanticsTester.dispose(); semanticsTester.dispose();
SystemChannels.accessibility.setMockMessageHandler(null); SystemChannels.accessibility.setMockMessageHandler(null);
...@@ -658,10 +656,10 @@ void main() { ...@@ -658,10 +656,10 @@ void main() {
expect(value, true); expect(value, true);
expect(semanticEvent, <String, dynamic>{ expect(semanticEvent, <String, dynamic>{
'type': 'tap', 'type': 'tap',
'nodeId': object.debugSemantics.id, 'nodeId': object.debugSemantics!.id,
'data': <String, dynamic>{}, 'data': <String, dynamic>{},
}); });
expect(object.debugSemantics.getSemanticsData().hasAction(SemanticsAction.tap), true); expect(object.debugSemantics!.getSemanticsData().hasAction(SemanticsAction.tap), true);
semanticsTester.dispose(); semanticsTester.dispose();
SystemChannels.accessibility.setMockMessageHandler(null); SystemChannels.accessibility.setMockMessageHandler(null);
...@@ -943,7 +941,7 @@ void main() { ...@@ -943,7 +941,7 @@ void main() {
await tester.pump(); await tester.pump();
expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.text); expect(RendererBinding.instance!.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.text);
// Test Switch() constructor // Test Switch() constructor
await tester.pumpWidget( await tester.pumpWidget(
...@@ -967,7 +965,7 @@ void main() { ...@@ -967,7 +965,7 @@ void main() {
); );
await gesture.moveTo(tester.getCenter(find.byType(Switch))); await gesture.moveTo(tester.getCenter(find.byType(Switch)));
expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.text); expect(RendererBinding.instance!.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.text);
// Test default cursor // Test default cursor
await tester.pumpWidget( await tester.pumpWidget(
...@@ -989,7 +987,7 @@ void main() { ...@@ -989,7 +987,7 @@ void main() {
), ),
); );
expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.click); expect(RendererBinding.instance!.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.click);
// Test default cursor when disabled // Test default cursor when disabled
await tester.pumpWidget( await tester.pumpWidget(
...@@ -1011,7 +1009,7 @@ void main() { ...@@ -1011,7 +1009,7 @@ void main() {
), ),
); );
expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic); expect(RendererBinding.instance!.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic);
await tester.pumpAndSettle(); await tester.pumpAndSettle();
}); });
...@@ -1020,7 +1018,7 @@ void main() { ...@@ -1020,7 +1018,7 @@ void main() {
// Regression test for https://github.com/flutter/flutter/issues/61247. // Regression test for https://github.com/flutter/flutter/issues/61247.
bool value = true; bool value = true;
bool enabled = true; bool enabled = true;
StateSetter stateSetter; late StateSetter stateSetter;
await tester.pumpWidget( await tester.pumpWidget(
Directionality( Directionality(
textDirection: TextDirection.ltr, textDirection: TextDirection.ltr,
......
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