Fix test cross-imports for ListTile (#180572)
- A part of https://github.com/flutter/flutter/issues/177415
- In this PR:
- Build a minimal TestListTile widget in `list_tile_test_utils.dart`.
It's basically a Container with necessary properties like min height,
padding, onTap callback with `HitTestBehavior.opaque` to illustrate
InkWell used in ListTile.
- Replace material ListTile widget with TestListTile for all appearances
in `packages/flutter/test/widgets`
## 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 `///`).
- [x] 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.
- [x] All existing and new tests are passing.
If you need help, consider asking for advice on the #hackers-new channel
on [Discord].
**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.
<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
Signed-off-by: huycozy <huy@nevercode.io>
diff --git a/packages/flutter/test/widgets/autocomplete_test.dart b/packages/flutter/test/widgets/autocomplete_test.dart
index d5381b2..c7b8b50 100644
--- a/packages/flutter/test/widgets/autocomplete_test.dart
+++ b/packages/flutter/test/widgets/autocomplete_test.dart
@@ -8,6 +8,7 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
+import 'list_tile_test_utils.dart';
import 'multi_view_testing.dart';
@@ -198,7 +199,7 @@
onTap: () {
onSelected(option);
},
- child: ListTile(title: Text(option)),
+ child: TestListTile(title: Text(option)),
),
)
.toList(),
@@ -315,7 +316,7 @@
onTap: () {
onSelected(option);
},
- child: ListTile(title: Text(option)),
+ child: TestListTile(title: Text(option)),
);
},
),
diff --git a/packages/flutter/test/widgets/baseline_test.dart b/packages/flutter/test/widgets/baseline_test.dart
index 390bfec..3c7efe7 100644
--- a/packages/flutter/test/widgets/baseline_test.dart
+++ b/packages/flutter/test/widgets/baseline_test.dart
@@ -5,6 +5,7 @@
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
+import 'list_tile_test_utils.dart';
void main() {
testWidgets('Baseline - control test', (WidgetTester tester) async {
@@ -75,7 +76,7 @@
child: Baseline(
baseline: 100.0,
baselineType: TextBaseline.alphabetic,
- child: ListTile(
+ child: TestListTile(
title: BaselineDetector(() {
assert(!debugCheckIntrinsicSizes);
calls += 1;
diff --git a/packages/flutter/test/widgets/focus_traversal_test.dart b/packages/flutter/test/widgets/focus_traversal_test.dart
index ceecf42..92e0a9e 100644
--- a/packages/flutter/test/widgets/focus_traversal_test.dart
+++ b/packages/flutter/test/widgets/focus_traversal_test.dart
@@ -10,6 +10,7 @@
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
+import 'list_tile_test_utils.dart';
import 'semantics_tester.dart';
void main() {
@@ -3186,7 +3187,7 @@
) async {
await tester.pumpWidget(
const MaterialApp(
- home: Scaffold(body: ListTile(title: Text('title'))),
+ home: Scaffold(body: TestListTile(title: Text('title'))),
),
);
final FocusNode? initialFocus = primaryFocus;
diff --git a/packages/flutter/test/widgets/list_tile_test_utils.dart b/packages/flutter/test/widgets/list_tile_test_utils.dart
new file mode 100644
index 0000000..13af94a
--- /dev/null
+++ b/packages/flutter/test/widgets/list_tile_test_utils.dart
@@ -0,0 +1,30 @@
+// Copyright 2014 The Flutter Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+import 'package:flutter/widgets.dart';
+
+/// A minimal ListTile widget for testing purposes to avoid relying on
+/// widgets from material.dart.
+/// See https://github.com/flutter/flutter/issues/177415.
+class TestListTile extends StatelessWidget {
+ const TestListTile({super.key, this.title, this.onTap});
+
+ final Widget? title;
+ final VoidCallback? onTap;
+
+ @override
+ Widget build(BuildContext context) {
+ final Widget content = Container(
+ constraints: const BoxConstraints(minHeight: 56.0),
+ padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 4.0),
+ child: title,
+ );
+
+ if (onTap != null) {
+ return GestureDetector(onTap: onTap, behavior: HitTestBehavior.opaque, child: content);
+ }
+
+ return content;
+ }
+}
diff --git a/packages/flutter/test/widgets/navigator_test.dart b/packages/flutter/test/widgets/navigator_test.dart
index 28e35dd..0e81baf 100644
--- a/packages/flutter/test/widgets/navigator_test.dart
+++ b/packages/flutter/test/widgets/navigator_test.dart
@@ -12,6 +12,7 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
+import 'list_tile_test_utils.dart';
import 'navigator_utils.dart';
import 'observer_tester.dart';
import 'semantics_tester.dart';
@@ -5085,9 +5086,9 @@
'/one': (BuildContext context) => Scaffold(
body: Column(
children: <Widget>[
- const ListTile(title: Text('Title 1')),
- const ListTile(title: Text('Title 2')),
- const ListTile(title: Text('Title 3')),
+ const TestListTile(title: Text('Title 1')),
+ const TestListTile(title: Text('Title 2')),
+ const TestListTile(title: Text('Title 3')),
ElevatedButton(
key: openSheetKey,
onPressed: () {
diff --git a/packages/flutter/test/widgets/nested_scroll_view_test.dart b/packages/flutter/test/widgets/nested_scroll_view_test.dart
index c445630..16f51c8 100644
--- a/packages/flutter/test/widgets/nested_scroll_view_test.dart
+++ b/packages/flutter/test/widgets/nested_scroll_view_test.dart
@@ -12,6 +12,7 @@
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
import '../rendering/rendering_tester.dart' show TestClipPaintingContext;
+import 'list_tile_test_utils.dart';
class _CustomPhysics extends ClampingScrollPhysics {
const _CustomPhysics({super.parent});
@@ -340,7 +341,7 @@
physics: const BouncingScrollPhysics(),
itemCount: 15,
itemBuilder: (BuildContext context, int index) {
- return ListTile(
+ return TestListTile(
title: Text('Item $index'),
onTap: () {
tapped = true;
@@ -423,7 +424,7 @@
itemExtent: 56,
itemCount: 15,
itemBuilder: (BuildContext context, int index) {
- return ListTile(
+ return TestListTile(
title: Text('Item $index'),
onTap: () {
tapped = true;
@@ -537,7 +538,7 @@
itemExtent: 56,
itemCount: 15,
itemBuilder: (BuildContext context, int index) {
- return ListTile(
+ return TestListTile(
title: Text('Item $index'),
onTap: () {
tapped = true;
@@ -1007,7 +1008,7 @@
// This builder is called for each child.
// In this example, we just number each list
// item.
- return ListTile(title: Text('Item $index'));
+ return TestListTile(title: Text('Item $index'));
},
),
),
@@ -1101,7 +1102,9 @@
expect(find.text('Item 2'), findsOneWidget);
expect(find.text('Item 0'), findsOneWidget);
expect(
- tester.getTopLeft(find.ancestor(of: find.text('Item 0'), matching: find.byType(ListTile))).dy,
+ tester
+ .getTopLeft(find.ancestor(of: find.text('Item 0'), matching: find.byType(TestListTile)))
+ .dy,
tester.getBottomLeft(find.byType(AppBar)).dy + 8.0,
);
checkPhysicalLayer(elevation: 4);
@@ -1666,7 +1669,7 @@
itemExtent: 50.0,
itemCount: 30,
itemBuilder: (BuildContext context, int index) =>
- ListTile(title: Text('Item $index')),
+ TestListTile(title: Text('Item $index')),
),
],
);
@@ -2711,7 +2714,7 @@
itemExtent: 48.0,
itemCount: 30,
itemBuilder: (BuildContext context, int index) {
- return ListTile(title: Text('Item $index'));
+ return TestListTile(title: Text('Item $index'));
},
),
),
@@ -2767,7 +2770,7 @@
itemExtent: 48.0,
itemCount: 30,
itemBuilder: (BuildContext context, int index) {
- return ListTile(title: Text('Item $index'));
+ return TestListTile(title: Text('Item $index'));
},
),
),
@@ -2821,7 +2824,7 @@
itemExtent: 50.0,
itemCount: 30,
itemBuilder: (BuildContext context, int index) =>
- ListTile(title: Text('Item $index')),
+ TestListTile(title: Text('Item $index')),
),
],
);
@@ -2909,7 +2912,7 @@
itemExtent: 50.0,
itemCount: 30,
itemBuilder: (BuildContext context, int index) =>
- ListTile(title: Text('Item $index')),
+ TestListTile(title: Text('Item $index')),
),
],
);
@@ -3072,13 +3075,13 @@
),
PinnedHeaderSliver(
key: pinnedHeaderSliverKey,
- child: const ListTile(title: Text('Pinned Header')),
+ child: const TestListTile(title: Text('Pinned Header')),
),
SliverFixedExtentList.builder(
itemExtent: 50.0,
itemCount: 30,
itemBuilder: (BuildContext context, int index) =>
- ListTile(title: Text('Item $index')),
+ TestListTile(title: Text('Item $index')),
),
],
);
@@ -3380,7 +3383,7 @@
sliver: SliverList.builder(
itemCount: 30,
itemBuilder: (BuildContext context, int index) {
- return ListTile(title: Text('Item $index'));
+ return TestListTile(title: Text('Item $index'));
},
),
),
@@ -3421,7 +3424,7 @@
final Finder finder = find.text('Item 14', skipOffstage: false);
final Finder findAny = find
- .descendant(of: find.byType(SliverList), matching: find.byType(ListTile))
+ .descendant(of: find.byType(SliverList), matching: find.byType(TestListTile))
.first;
Future<void> scroll(VerticalDirection direction) async {
diff --git a/packages/flutter/test/widgets/reorderable_list_test.dart b/packages/flutter/test/widgets/reorderable_list_test.dart
index e08bbb1..b001e9e 100644
--- a/packages/flutter/test/widgets/reorderable_list_test.dart
+++ b/packages/flutter/test/widgets/reorderable_list_test.dart
@@ -8,6 +8,7 @@
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
+import 'list_tile_test_utils.dart';
import 'semantics_tester.dart';
void main() {
@@ -1448,7 +1449,7 @@
key: ValueKey<String>('item-$index'),
child: ReorderableDragStartListener(
index: index,
- child: ListTile(title: Text('item ${items[index]}')),
+ child: TestListTile(title: Text('item ${items[index]}')),
),
);
},
@@ -1604,7 +1605,7 @@
return ReorderableDragStartListener(
key: Key('$index'),
index: index,
- child: Material(child: ListTile(title: Text(items[index]))),
+ child: Material(child: TestListTile(title: Text(items[index]))),
);
},
),
diff --git a/packages/flutter/test/widgets/scrollable_helpers_test.dart b/packages/flutter/test/widgets/scrollable_helpers_test.dart
index 8643a52..2ca43d8 100644
--- a/packages/flutter/test/widgets/scrollable_helpers_test.dart
+++ b/packages/flutter/test/widgets/scrollable_helpers_test.dart
@@ -7,6 +7,7 @@
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
+import 'list_tile_test_utils.dart';
final LogicalKeyboardKey modifierKey = defaultTargetPlatform == TargetPlatform.macOS
? LogicalKeyboardKey.metaLeft
@@ -736,7 +737,10 @@
});
},
children: <Widget>[
- ListTile(key: const ValueKey<String>('Item 1'), title: Text(items.first)),
+ TestListTile(
+ key: const ValueKey<String>('Item 1'),
+ title: Text(items.first),
+ ),
],
);
},
diff --git a/packages/flutter/test/widgets/sliver_main_axis_group_test.dart b/packages/flutter/test/widgets/sliver_main_axis_group_test.dart
index ecdb21f..da9f6ee 100644
--- a/packages/flutter/test/widgets/sliver_main_axis_group_test.dart
+++ b/packages/flutter/test/widgets/sliver_main_axis_group_test.dart
@@ -7,6 +7,7 @@
import 'package:flutter_test/flutter_test.dart';
import '../rendering/sliver_utils.dart';
+import 'list_tile_test_utils.dart';
import 'semantics_tester.dart';
const double VIEWPORT_HEIGHT = 600;
@@ -1371,7 +1372,9 @@
Widget buildItem(BuildContext context, int index) {
return !skip || index.isEven
? Card(
- child: ListTile(title: Text('item$index', style: const TextStyle(fontSize: 80))),
+ child: TestListTile(
+ title: Text('item$index', style: const TextStyle(fontSize: 80)),
+ ),
)
: Container();
}
diff --git a/packages/flutter/test/widgets/slivers_test.dart b/packages/flutter/test/widgets/slivers_test.dart
index 2c74dee..633a748 100644
--- a/packages/flutter/test/widgets/slivers_test.dart
+++ b/packages/flutter/test/widgets/slivers_test.dart
@@ -7,6 +7,7 @@
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
+import 'list_tile_test_utils.dart';
import 'semantics_tester.dart';
Future<void> test(WidgetTester tester, double offset, {double anchor = 0.0}) {
@@ -481,7 +482,7 @@
Widget buildItem(BuildContext context, int index) {
return !skip || index.isEven
? Card(
- child: ListTile(title: Text('item$index', style: const TextStyle(fontSize: 80))),
+ child: TestListTile(title: Text('item$index', style: const TextStyle(fontSize: 80))),
)
: Container();
}