Skip to content

Commit 130808c

Browse files
authored
Add TestWidgetsApp utility and refactor widget tests to use WidgetsApp (flutter#180456)
This PR introduces a TestWidgetsApp utility class that provides a minimal WidgetsApp wrapper for widget tests, replacing direct usage of Directionality widgets. part of: flutter#177415 ## Changes - test_widgets_app.dart - A reusable TestWidgetsApp widget that wraps WidgetsApp with a builder pattern, providing Directionality, MediaQuery, and other app-level widgets that WidgetsApp provides. - gesture_detector_test.dart - Replaced 9 Directionality usages - mouse_region_test.dart - Replaced 13 Directionality usages - opacity_test.dart - Replaced 2 Directionality usages - slivers_evil_test.dart - Replaced 3 Directionality usages and removed redundant MediaQuery wrappers ## Note - This PR updates golden test baselines for opacity_test.dart. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing.
1 parent 4783676 commit 130808c

7 files changed

Lines changed: 574 additions & 292 deletions

File tree

dev/bots/check_tests_cross_imports.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ class TestsCrossImportChecker {
166166
'packages/flutter/test/widgets/route_notification_messages_test.dart',
167167
'packages/flutter/test/widgets/animated_cross_fade_test.dart',
168168
'packages/flutter/test/widgets/editable_text_shortcuts_test.dart',
169-
'packages/flutter/test/widgets/gesture_detector_test.dart',
170169
'packages/flutter/test/widgets/router_test.dart',
171170
'packages/flutter/test/widgets/scroll_notification_test.dart',
172171
'packages/flutter/test/widgets/magnifier_test.dart',
@@ -187,7 +186,6 @@ class TestsCrossImportChecker {
187186
'packages/flutter/test/widgets/platform_menu_bar_test.dart',
188187
'packages/flutter/test/widgets/inherited_test.dart',
189188
'packages/flutter/test/widgets/heroes_test.dart',
190-
'packages/flutter/test/widgets/slivers_evil_test.dart',
191189
'packages/flutter/test/widgets/container_test.dart',
192190
'packages/flutter/test/widgets/drawer_test.dart',
193191
'packages/flutter/test/widgets/framework_test.dart',
@@ -216,7 +214,6 @@ class TestsCrossImportChecker {
216214
'packages/flutter/test/widgets/spell_check_test.dart',
217215
'packages/flutter/test/widgets/slivers_appbar_floating_test.dart',
218216
'packages/flutter/test/widgets/toggleable_test.dart',
219-
'packages/flutter/test/widgets/mouse_region_test.dart',
220217
'packages/flutter/test/widgets/draggable_test.dart',
221218
'packages/flutter/test/widgets/page_transitions_builder_test.dart',
222219
'packages/flutter/test/widgets/selectable_region_context_menu_test.dart',
@@ -242,7 +239,6 @@ class TestsCrossImportChecker {
242239
'packages/flutter/test/widgets/simple_semantics_test.dart',
243240
'packages/flutter/test/widgets/image_filter_test.dart',
244241
'packages/flutter/test/widgets/navigator_on_did_remove_page_test.dart',
245-
'packages/flutter/test/widgets/opacity_test.dart',
246242
'packages/flutter/test/widgets/baseline_test.dart',
247243
'packages/flutter/test/widgets/selection_container_test.dart',
248244
'packages/flutter/test/widgets/scrollable_semantics_test.dart',

packages/flutter/test/widgets/gesture_detector_test.dart

Lines changed: 126 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
// found in the LICENSE file.
44

55
import 'package:flutter/gestures.dart';
6-
import 'package:flutter/material.dart';
76
import 'package:flutter/rendering.dart';
7+
import 'package:flutter/widgets.dart';
88
import 'package:flutter_test/flutter_test.dart';
99

10+
import 'widgets_app_tester.dart';
11+
1012
void main() {
1113
const forcePressOffset = Offset(400.0, 50.0);
1214

@@ -806,29 +808,27 @@ void main() {
806808
final SemanticsOwner semanticsOwner = tester.binding.pipelineOwner.semanticsOwner!;
807809
final key = UniqueKey();
808810
await tester.pumpWidget(
809-
MaterialApp(
810-
home: Scaffold(
811-
body: Center(
812-
child: RawGestureDetector(
813-
key: key,
814-
gestures: <Type, GestureRecognizerFactory>{
815-
TapGestureRecognizer: GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(
816-
() => TapGestureRecognizer(postAcceptSlopTolerance: null),
817-
(TapGestureRecognizer instance) {
818-
instance.onTapDown = (TapDownDetails details) {
819-
receivedTapDownDetails = details;
820-
};
821-
instance.onTapUp = (TapUpDetails details) {
822-
receivedTapUpDetails = details;
823-
};
824-
instance.onTap = () {
825-
tapped = true;
826-
};
827-
},
828-
),
829-
},
830-
child: const SizedBox(width: 20, height: 20),
831-
),
811+
TestWidgetsApp(
812+
home: Center(
813+
child: RawGestureDetector(
814+
key: key,
815+
gestures: <Type, GestureRecognizerFactory>{
816+
TapGestureRecognizer: GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(
817+
() => TapGestureRecognizer(postAcceptSlopTolerance: null),
818+
(TapGestureRecognizer instance) {
819+
instance.onTapDown = (TapDownDetails details) {
820+
receivedTapDownDetails = details;
821+
};
822+
instance.onTapUp = (TapUpDetails details) {
823+
receivedTapUpDetails = details;
824+
};
825+
instance.onTap = () {
826+
tapped = true;
827+
};
828+
},
829+
),
830+
},
831+
child: const SizedBox(width: 20, height: 20),
832832
),
833833
),
834834
),
@@ -854,36 +854,34 @@ void main() {
854854
final SemanticsOwner semanticsOwner = tester.binding.pipelineOwner.semanticsOwner!;
855855
final key = UniqueKey();
856856
await tester.pumpWidget(
857-
MaterialApp(
858-
home: Scaffold(
859-
body: Center(
860-
child: RawGestureDetector(
861-
key: key,
862-
gestures: <Type, GestureRecognizerFactory>{
863-
LongPressGestureRecognizer:
864-
GestureRecognizerFactoryWithHandlers<LongPressGestureRecognizer>(
865-
() => LongPressGestureRecognizer(),
866-
(LongPressGestureRecognizer instance) {
867-
instance.onLongPressDown = (LongPressDownDetails details) {
868-
receivedLongPressDownDetails = details;
869-
};
870-
instance.onLongPressStart = (LongPressStartDetails details) {
871-
receivedLongPressStartDetails = details;
872-
};
873-
instance.onLongPressEnd = (LongPressEndDetails details) {
874-
receivedLongPressEndDetails = details;
875-
};
876-
instance.onLongPressUp = () {
877-
upped = true;
878-
};
879-
instance.onLongPress = () {
880-
pressed = true;
881-
};
882-
},
883-
),
884-
},
885-
child: const SizedBox(width: 20, height: 20),
886-
),
857+
TestWidgetsApp(
858+
home: Center(
859+
child: RawGestureDetector(
860+
key: key,
861+
gestures: <Type, GestureRecognizerFactory>{
862+
LongPressGestureRecognizer:
863+
GestureRecognizerFactoryWithHandlers<LongPressGestureRecognizer>(
864+
() => LongPressGestureRecognizer(),
865+
(LongPressGestureRecognizer instance) {
866+
instance.onLongPressDown = (LongPressDownDetails details) {
867+
receivedLongPressDownDetails = details;
868+
};
869+
instance.onLongPressStart = (LongPressStartDetails details) {
870+
receivedLongPressStartDetails = details;
871+
};
872+
instance.onLongPressEnd = (LongPressEndDetails details) {
873+
receivedLongPressEndDetails = details;
874+
};
875+
instance.onLongPressUp = () {
876+
upped = true;
877+
};
878+
instance.onLongPress = () {
879+
pressed = true;
880+
};
881+
},
882+
),
883+
},
884+
child: const SizedBox(width: 20, height: 20),
887885
),
888886
),
889887
),
@@ -911,33 +909,31 @@ void main() {
911909
final SemanticsOwner semanticsOwner = tester.binding.pipelineOwner.semanticsOwner!;
912910
final key = UniqueKey();
913911
await tester.pumpWidget(
914-
MaterialApp(
915-
home: Scaffold(
916-
body: Center(
917-
child: RawGestureDetector(
918-
key: key,
919-
gestures: <Type, GestureRecognizerFactory>{
920-
HorizontalDragGestureRecognizer:
921-
GestureRecognizerFactoryWithHandlers<HorizontalDragGestureRecognizer>(
922-
() => HorizontalDragGestureRecognizer(),
923-
(HorizontalDragGestureRecognizer instance) {
924-
instance.onDown = (DragDownDetails details) {
925-
receivedDragDownDetails = details;
926-
};
927-
instance.onStart = (DragStartDetails details) {
928-
receivedDragStartDetails = details;
929-
};
930-
instance.onUpdate = (DragUpdateDetails details) {
931-
receivedDragUpdateDetails = details;
932-
};
933-
instance.onEnd = (DragEndDetails details) {
934-
receivedDragEndDetails = details;
935-
};
936-
},
937-
),
938-
},
939-
child: const SizedBox(width: 20, height: 20),
940-
),
912+
TestWidgetsApp(
913+
home: Center(
914+
child: RawGestureDetector(
915+
key: key,
916+
gestures: <Type, GestureRecognizerFactory>{
917+
HorizontalDragGestureRecognizer:
918+
GestureRecognizerFactoryWithHandlers<HorizontalDragGestureRecognizer>(
919+
() => HorizontalDragGestureRecognizer(),
920+
(HorizontalDragGestureRecognizer instance) {
921+
instance.onDown = (DragDownDetails details) {
922+
receivedDragDownDetails = details;
923+
};
924+
instance.onStart = (DragStartDetails details) {
925+
receivedDragStartDetails = details;
926+
};
927+
instance.onUpdate = (DragUpdateDetails details) {
928+
receivedDragUpdateDetails = details;
929+
};
930+
instance.onEnd = (DragEndDetails details) {
931+
receivedDragEndDetails = details;
932+
};
933+
},
934+
),
935+
},
936+
child: const SizedBox(width: 20, height: 20),
941937
),
942938
),
943939
),
@@ -968,33 +964,31 @@ void main() {
968964
final SemanticsOwner semanticsOwner = tester.binding.pipelineOwner.semanticsOwner!;
969965
final key = UniqueKey();
970966
await tester.pumpWidget(
971-
MaterialApp(
972-
home: Scaffold(
973-
body: Center(
974-
child: RawGestureDetector(
975-
key: key,
976-
gestures: <Type, GestureRecognizerFactory>{
977-
VerticalDragGestureRecognizer:
978-
GestureRecognizerFactoryWithHandlers<VerticalDragGestureRecognizer>(
979-
() => VerticalDragGestureRecognizer(),
980-
(VerticalDragGestureRecognizer instance) {
981-
instance.onDown = (DragDownDetails details) {
982-
receivedDragDownDetails = details;
983-
};
984-
instance.onStart = (DragStartDetails details) {
985-
receivedDragStartDetails = details;
986-
};
987-
instance.onUpdate = (DragUpdateDetails details) {
988-
receivedDragUpdateDetails = details;
989-
};
990-
instance.onEnd = (DragEndDetails details) {
991-
receivedDragEndDetails = details;
992-
};
993-
},
994-
),
995-
},
996-
child: const SizedBox(width: 20, height: 20),
997-
),
967+
TestWidgetsApp(
968+
home: Center(
969+
child: RawGestureDetector(
970+
key: key,
971+
gestures: <Type, GestureRecognizerFactory>{
972+
VerticalDragGestureRecognizer:
973+
GestureRecognizerFactoryWithHandlers<VerticalDragGestureRecognizer>(
974+
() => VerticalDragGestureRecognizer(),
975+
(VerticalDragGestureRecognizer instance) {
976+
instance.onDown = (DragDownDetails details) {
977+
receivedDragDownDetails = details;
978+
};
979+
instance.onStart = (DragStartDetails details) {
980+
receivedDragStartDetails = details;
981+
};
982+
instance.onUpdate = (DragUpdateDetails details) {
983+
receivedDragUpdateDetails = details;
984+
};
985+
instance.onEnd = (DragEndDetails details) {
986+
receivedDragEndDetails = details;
987+
};
988+
},
989+
),
990+
},
991+
child: const SizedBox(width: 20, height: 20),
998992
),
999993
),
1000994
),
@@ -1025,32 +1019,30 @@ void main() {
10251019
final SemanticsOwner semanticsOwner = tester.binding.pipelineOwner.semanticsOwner!;
10261020
final key = UniqueKey();
10271021
await tester.pumpWidget(
1028-
MaterialApp(
1029-
home: Scaffold(
1030-
body: Center(
1031-
child: RawGestureDetector(
1032-
key: key,
1033-
gestures: <Type, GestureRecognizerFactory>{
1034-
PanGestureRecognizer: GestureRecognizerFactoryWithHandlers<PanGestureRecognizer>(
1035-
() => PanGestureRecognizer(),
1036-
(PanGestureRecognizer instance) {
1037-
instance.onDown = (DragDownDetails details) {
1038-
receivedDragDownDetails = details;
1039-
};
1040-
instance.onStart = (DragStartDetails details) {
1041-
receivedDragStartDetails = details;
1042-
};
1043-
instance.onUpdate = (DragUpdateDetails details) {
1044-
receivedDragUpdateDetails = details;
1045-
};
1046-
instance.onEnd = (DragEndDetails details) {
1047-
receivedDragEndDetails = details;
1048-
};
1049-
},
1050-
),
1051-
},
1052-
child: const SizedBox(width: 20, height: 20),
1053-
),
1022+
TestWidgetsApp(
1023+
home: Center(
1024+
child: RawGestureDetector(
1025+
key: key,
1026+
gestures: <Type, GestureRecognizerFactory>{
1027+
PanGestureRecognizer: GestureRecognizerFactoryWithHandlers<PanGestureRecognizer>(
1028+
() => PanGestureRecognizer(),
1029+
(PanGestureRecognizer instance) {
1030+
instance.onDown = (DragDownDetails details) {
1031+
receivedDragDownDetails = details;
1032+
};
1033+
instance.onStart = (DragStartDetails details) {
1034+
receivedDragStartDetails = details;
1035+
};
1036+
instance.onUpdate = (DragUpdateDetails details) {
1037+
receivedDragUpdateDetails = details;
1038+
};
1039+
instance.onEnd = (DragEndDetails details) {
1040+
receivedDragEndDetails = details;
1041+
};
1042+
},
1043+
),
1044+
},
1045+
child: const SizedBox(width: 20, height: 20),
10541046
),
10551047
),
10561048
),

0 commit comments

Comments
 (0)