Unverified Commit 17a27358 authored by Nate's avatar Nate Committed by GitHub

Implementing switch expressions in `flutter/test/` (#144580)

Migrates test code to new `switch` syntax.
parent dff03432
...@@ -2662,21 +2662,14 @@ class _TestPostRouteCancelState extends State<_TestPostRouteCancel> { ...@@ -2662,21 +2662,14 @@ class _TestPostRouteCancelState extends State<_TestPostRouteCancel> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return CupertinoApp( return CupertinoApp(
initialRoute: 'home', initialRoute: 'home',
onGenerateRoute: (RouteSettings settings) { onGenerateRoute: (RouteSettings settings) => CupertinoPageRoute<void>(
return CupertinoPageRoute<void>(
settings: settings, settings: settings,
builder: (BuildContext context) { builder: (BuildContext context) => switch (settings.name) {
switch (settings.name) { 'home' => _buildHome(context),
case 'home': 'sub' => _buildSub(context),
return _buildHome(context); _ => throw UnimplementedError(),
case 'sub':
return _buildSub(context);
default:
throw UnimplementedError();
}
},
);
}, },
),
); );
} }
} }
......
...@@ -1119,17 +1119,10 @@ void main() { ...@@ -1119,17 +1119,10 @@ void main() {
await tester.tap(find.byType(DropdownMenu<TestMenu>)); await tester.tap(find.byType(DropdownMenu<TestMenu>));
await tester.pump(); await tester.pump();
late final bool isMobile; final bool isMobile = switch (themeData.platform) {
switch (themeData.platform) { TargetPlatform.android || TargetPlatform.iOS || TargetPlatform.fuchsia => true,
case TargetPlatform.android: TargetPlatform.macOS || TargetPlatform.linux || TargetPlatform.windows => false,
case TargetPlatform.iOS: };
case TargetPlatform.fuchsia:
isMobile = true;
case TargetPlatform.macOS:
case TargetPlatform.linux:
case TargetPlatform.windows:
isMobile = false;
}
int expectedCount = isMobile ? 0 : 1; int expectedCount = isMobile ? 0 : 1;
// Test onSelected on key press // Test onSelected on key press
......
...@@ -1259,12 +1259,10 @@ void main() { ...@@ -1259,12 +1259,10 @@ void main() {
} }
final RenderBox renderBox = renderObject as RenderBox; final RenderBox renderBox = renderObject as RenderBox;
switch (axis) { return switch (axis) {
case Axis.horizontal: Axis.horizontal => renderBox.size.width,
return renderBox.size.width; Axis.vertical => renderBox.size.height,
case Axis.vertical: };
return renderBox.size.height;
}
} }
group('animated: $animated, scrollDirection: $axis', () { group('animated: $animated, scrollDirection: $axis', () {
......
...@@ -188,23 +188,16 @@ class FakeAndroidPlatformViewsController { ...@@ -188,23 +188,16 @@ class FakeAndroidPlatformViewsController {
} }
Future<dynamic> _onMethodCall(MethodCall call) { Future<dynamic> _onMethodCall(MethodCall call) {
switch (call.method) { return switch (call.method) {
case 'create': 'create' => _create(call),
return _create(call); 'dispose' => _dispose(call),
case 'dispose': 'resize' => _resize(call),
return _dispose(call); 'touch' => _touch(call),
case 'resize': 'setDirection' => _setDirection(call),
return _resize(call); 'clearFocus' => _clearFocus(call),
case 'touch': 'offset' => _offset(call),
return _touch(call); _ => Future<dynamic>.sync(() => null),
case 'setDirection': };
return _setDirection(call);
case 'clearFocus':
return _clearFocus(call);
case 'offset':
return _offset(call);
}
return Future<dynamic>.sync(() => null);
} }
Future<dynamic> _create(MethodCall call) async { Future<dynamic> _create(MethodCall call) async {
...@@ -400,17 +393,13 @@ class FakeIosPlatformViewsController { ...@@ -400,17 +393,13 @@ class FakeIosPlatformViewsController {
} }
Future<dynamic> _onMethodCall(MethodCall call) { Future<dynamic> _onMethodCall(MethodCall call) {
switch (call.method) { return switch (call.method) {
case 'create': 'create' => _create(call),
return _create(call); 'dispose' => _dispose(call),
case 'dispose': 'acceptGesture' => _acceptGesture(call),
return _dispose(call); 'rejectGesture' => _rejectGesture(call),
case 'acceptGesture': _ => Future<dynamic>.sync(() => null),
return _acceptGesture(call); };
case 'rejectGesture':
return _rejectGesture(call);
}
return Future<dynamic>.sync(() => null);
} }
Future<dynamic> _create(MethodCall call) async { Future<dynamic> _create(MethodCall call) async {
...@@ -503,17 +492,13 @@ class FakeMacosPlatformViewsController { ...@@ -503,17 +492,13 @@ class FakeMacosPlatformViewsController {
} }
Future<dynamic> _onMethodCall(MethodCall call) { Future<dynamic> _onMethodCall(MethodCall call) {
switch (call.method) { return switch (call.method) {
case 'create': 'create' => _create(call),
return _create(call); 'dispose' => _dispose(call),
case 'dispose': 'acceptGesture' => _acceptGesture(call),
return _dispose(call); 'rejectGesture' => _rejectGesture(call),
case 'acceptGesture': _ => Future<dynamic>.sync(() => null),
return _acceptGesture(call); };
case 'rejectGesture':
return _rejectGesture(call);
}
return Future<dynamic>.sync(() => null);
} }
Future<dynamic> _create(MethodCall call) async { Future<dynamic> _create(MethodCall call) async {
......
...@@ -2241,22 +2241,15 @@ void main() { ...@@ -2241,22 +2241,15 @@ void main() {
// How modifiers are interpreted depends upon the keyCode for GLFW. // How modifiers are interpreted depends upon the keyCode for GLFW.
int keyCodeForModifier(int modifier, {required bool isLeft}) { int keyCodeForModifier(int modifier, {required bool isLeft}) {
switch (modifier) { return switch (modifier) {
case GLFWKeyHelper.modifierAlt: GLFWKeyHelper.modifierAlt => isLeft ? 342 : 346,
return isLeft ? 342 : 346; GLFWKeyHelper.modifierShift => isLeft ? 340 : 344,
case GLFWKeyHelper.modifierShift: GLFWKeyHelper.modifierControl => isLeft ? 341 : 345,
return isLeft ? 340 : 344; GLFWKeyHelper.modifierMeta => isLeft ? 343 : 347,
case GLFWKeyHelper.modifierControl: GLFWKeyHelper.modifierNumericPad => 282,
return isLeft ? 341 : 345; GLFWKeyHelper.modifierCapsLock => 280,
case GLFWKeyHelper.modifierMeta: _ => 65, // keyA
return isLeft ? 343 : 347; };
case GLFWKeyHelper.modifierNumericPad:
return 282;
case GLFWKeyHelper.modifierCapsLock:
return 280;
default:
return 65; // keyA
}
} }
test('modifier keys are recognized individually', () { test('modifier keys are recognized individually', () {
...@@ -2473,22 +2466,15 @@ void main() { ...@@ -2473,22 +2466,15 @@ void main() {
// How modifiers are interpreted depends upon the keyCode for GTK. // How modifiers are interpreted depends upon the keyCode for GTK.
int keyCodeForModifier(int modifier, {required bool isLeft}) { int keyCodeForModifier(int modifier, {required bool isLeft}) {
switch (modifier) { return switch (modifier) {
case GtkKeyHelper.modifierMod1: GtkKeyHelper.modifierShift => isLeft ? 65505 : 65506,
return 65513; GtkKeyHelper.modifierControl => isLeft ? 65507 : 65508,
case GtkKeyHelper.modifierShift: GtkKeyHelper.modifierMeta => isLeft ? 65515 : 65516,
return isLeft ? 65505 : 65506; GtkKeyHelper.modifierMod1 => 65513,
case GtkKeyHelper.modifierControl: GtkKeyHelper.modifierMod2 => 65407,
return isLeft ? 65507 : 65508; GtkKeyHelper.modifierCapsLock => 65509,
case GtkKeyHelper.modifierMeta: _ => 65, // keyA
return isLeft ? 65515 : 65516; };
case GtkKeyHelper.modifierMod2:
return 65407;
case GtkKeyHelper.modifierCapsLock:
return 65509;
default:
return 65; // keyA
}
} }
test('modifier keys are recognized individually', () { test('modifier keys are recognized individually', () {
......
...@@ -119,32 +119,22 @@ Future<void> dismissElement(WidgetTester tester, Finder finder, { required AxisD ...@@ -119,32 +119,22 @@ Future<void> dismissElement(WidgetTester tester, Finder finder, { required AxisD
} }
Future<void> dragElement(WidgetTester tester, Finder finder, { required AxisDirection gestureDirection, required double amount }) async { Future<void> dragElement(WidgetTester tester, Finder finder, { required AxisDirection gestureDirection, required double amount }) async {
Offset delta; final Offset delta = switch (gestureDirection) {
switch (gestureDirection) { AxisDirection.left => Offset(-amount, 0.0),
case AxisDirection.left: AxisDirection.right => Offset(amount, 0.0),
delta = Offset(-amount, 0.0); AxisDirection.up => Offset(0.0, -amount),
case AxisDirection.right: AxisDirection.down => Offset(0.0, amount),
delta = Offset(amount, 0.0); };
case AxisDirection.up:
delta = Offset(0.0, -amount);
case AxisDirection.down:
delta = Offset(0.0, amount);
}
await tester.drag(finder, delta); await tester.drag(finder, delta);
} }
Future<void> flingElement(WidgetTester tester, Finder finder, { required AxisDirection gestureDirection, double initialOffsetFactor = 0.0 }) async { Future<void> flingElement(WidgetTester tester, Finder finder, { required AxisDirection gestureDirection, double initialOffsetFactor = 0.0 }) async {
Offset delta; final Offset delta = switch (gestureDirection) {
switch (gestureDirection) { AxisDirection.left => const Offset(-300, 0.0),
case AxisDirection.left: AxisDirection.right => const Offset(300, 0.0),
delta = const Offset(-300.0, 0.0); AxisDirection.up => const Offset(0.0, -300),
case AxisDirection.right: AxisDirection.down => const Offset(0.0, 300),
delta = const Offset(300.0, 0.0); };
case AxisDirection.up:
delta = const Offset(0.0, -300.0);
case AxisDirection.down:
delta = const Offset(0.0, 300.0);
}
await tester.fling(finder, delta, 1000.0, initialOffset: delta * initialOffsetFactor); await tester.fling(finder, delta, 1000.0, initialOffset: delta * initialOffsetFactor);
} }
...@@ -213,17 +203,12 @@ Future<void> checkFlingItemAfterMovement( ...@@ -213,17 +203,12 @@ Future<void> checkFlingItemAfterMovement(
} }
Future<void> rollbackElement(WidgetTester tester, Finder finder, { required AxisDirection gestureDirection, double initialOffsetFactor = 0.0 }) async { Future<void> rollbackElement(WidgetTester tester, Finder finder, { required AxisDirection gestureDirection, double initialOffsetFactor = 0.0 }) async {
Offset delta; final Offset delta = switch (gestureDirection) {
switch (gestureDirection) { AxisDirection.left => const Offset(-30.0, 0.0),
case AxisDirection.left: AxisDirection.right => const Offset(30.0, 0.0),
delta = const Offset(-30.0, 0.0); AxisDirection.up => const Offset(0.0, -30.0),
case AxisDirection.right: AxisDirection.down => const Offset(0.0, 30.0),
delta = const Offset(30.0, 0.0); };
case AxisDirection.up:
delta = const Offset(0.0, -30.0);
case AxisDirection.down:
delta = const Offset(0.0, 30.0);
}
await tester.fling(finder, delta, 1000.0, initialOffset: delta * initialOffsetFactor); await tester.fling(finder, delta, 1000.0, initialOffset: delta * initialOffsetFactor);
} }
......
...@@ -442,13 +442,11 @@ class FakePlatformViewRegistry implements ui_web.PlatformViewRegistry { ...@@ -442,13 +442,11 @@ class FakePlatformViewRegistry implements ui_web.PlatformViewRegistry {
} }
Future<dynamic> _onMethodCall(MethodCall call) { Future<dynamic> _onMethodCall(MethodCall call) {
switch (call.method) { return switch (call.method) {
case 'create': 'create' => _create(call),
return _create(call); 'dispose' => _dispose(call),
case 'dispose': _ => Future<dynamic>.sync(() => null),
return _dispose(call); };
}
return Future<dynamic>.sync(() => null);
} }
Future<dynamic> _create(MethodCall call) async { Future<dynamic> _create(MethodCall call) async {
......
...@@ -38,23 +38,16 @@ class TestAssetBundle extends CachingAssetBundle { ...@@ -38,23 +38,16 @@ class TestAssetBundle extends CachingAssetBundle {
@override @override
Future<ByteData> load(String key) { Future<ByteData> load(String key) {
late ByteData data; final ByteData data = switch (key) {
switch (key) { 'AssetManifest.bin' => manifest,
case 'AssetManifest.bin': 'assets/image.png' => testByteData(1.0),
data = manifest; 'assets/1.0x/image.png' => testByteData(10.0), // see "...with a main asset and a 1.0x asset"
case 'assets/image.png': 'assets/1.5x/image.png' => testByteData(1.5),
data = testByteData(1.0); 'assets/2.0x/image.png' => testByteData(2.0),
case 'assets/1.0x/image.png': 'assets/3.0x/image.png' => testByteData(3.0),
data = testByteData(10.0); // see "...with a main asset and a 1.0x asset" 'assets/4.0x/image.png' => testByteData(4.0),
case 'assets/1.5x/image.png': _ => throw ArgumentError('Unexpected key: $key'),
data = testByteData(1.5); };
case 'assets/2.0x/image.png':
data = testByteData(2.0);
case 'assets/3.0x/image.png':
data = testByteData(3.0);
case 'assets/4.0x/image.png':
data = testByteData(4.0);
}
return SynchronousFuture<ByteData>(data); return SynchronousFuture<ByteData>(data);
} }
......
...@@ -199,12 +199,10 @@ void main() { ...@@ -199,12 +199,10 @@ void main() {
testWidgets('Check onstage/offstage handling of barriers around transitions', (WidgetTester tester) async { testWidgets('Check onstage/offstage handling of barriers around transitions', (WidgetTester tester) async {
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( MaterialApp(
onGenerateRoute: (RouteSettings settings) { onGenerateRoute: (RouteSettings settings) => switch (settings.name) {
switch (settings.name) { '/' => TestRoute<void>(settings: settings, child: const Text('A')),
case '/': return TestRoute<void>(settings: settings, child: const Text('A')); '/1' => TestRoute<void>(settings: settings, barrierColor: const Color(0xFFFFFF00), child: const Text('B')),
case '/1': return TestRoute<void>(settings: settings, barrierColor: const Color(0xFFFFFF00), child: const Text('B')); _ => null,
}
return null;
}, },
), ),
); );
......
...@@ -26,13 +26,10 @@ class TestOrientedBox extends SingleChildRenderObjectWidget { ...@@ -26,13 +26,10 @@ class TestOrientedBox extends SingleChildRenderObjectWidget {
const TestOrientedBox({ super.key, super.child }); const TestOrientedBox({ super.key, super.child });
Decoration _getDecoration(BuildContext context) { Decoration _getDecoration(BuildContext context) {
final Orientation orientation = MediaQuery.orientationOf(context); return switch (MediaQuery.orientationOf(context)) {
switch (orientation) { Orientation.landscape => const BoxDecoration(color: Color(0xFF00FF00)),
case Orientation.landscape: Orientation.portrait => const BoxDecoration(color: Color(0xFF0000FF)),
return const BoxDecoration(color: Color(0xFF00FF00)); };
case Orientation.portrait:
return const BoxDecoration(color: Color(0xFF0000FF));
}
} }
@override @override
......
...@@ -286,14 +286,11 @@ class _Diagonal extends RenderObjectWidget with SlottedMultiChildRenderObjectWid ...@@ -286,14 +286,11 @@ class _Diagonal extends RenderObjectWidget with SlottedMultiChildRenderObjectWid
@override @override
Widget? childForSlot(_DiagonalSlot? slot) { Widget? childForSlot(_DiagonalSlot? slot) {
switch (slot) { return switch (slot) {
case null: null => nullSlot,
return nullSlot; _DiagonalSlot.topLeft => topLeft,
case _DiagonalSlot.topLeft: _DiagonalSlot.bottomRight => bottomRight,
return topLeft; };
case _DiagonalSlot.bottomRight:
return bottomRight;
}
} }
@override @override
......
...@@ -2008,14 +2008,17 @@ class TextSelectionControlsSpy extends TextSelectionControls { ...@@ -2008,14 +2008,17 @@ class TextSelectionControlsSpy extends TextSelectionControls {
@override @override
Widget buildHandle(BuildContext context, TextSelectionHandleType type, double textLineHeight, [VoidCallback? onTap]) { Widget buildHandle(BuildContext context, TextSelectionHandleType type, double textLineHeight, [VoidCallback? onTap]) {
switch (type) { return ElevatedButton(
case TextSelectionHandleType.left: onPressed: onTap,
return ElevatedButton(onPressed: onTap, child: Text('height ${textLineHeight.toInt()}', key: leftHandleKey)); child: Text(
case TextSelectionHandleType.right: key: switch (type) {
return ElevatedButton(onPressed: onTap, child: Text('height ${textLineHeight.toInt()}', key: rightHandleKey)); TextSelectionHandleType.left => leftHandleKey,
case TextSelectionHandleType.collapsed: TextSelectionHandleType.right => rightHandleKey,
return ElevatedButton(onPressed: onTap, child: Text('height ${textLineHeight.toInt()}', key: collapsedHandleKey)); TextSelectionHandleType.collapsed => collapsedHandleKey,
} },
'height ${textLineHeight.toInt()}',
),
);
} }
@override @override
......
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