flutter_flutter/packages/flutter/test/widgets/scroll_cache_extent_test.dart
chunhtai 2391bdb566
Introduce ScrollCacheExtent and also fixes unbound shrinkwrap cache ex… (#181092)
…tent NaN problem

<!--
Thanks for filing a pull request!
Reviewers are typically assigned within a week of filing a request.
To learn more about code review, see our documentation on Tree Hygiene:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
-->

besides the general piping through parameters and breaking change.
Fixing an issue when the shrinkwrap: true the mainAxisExetent will be
double.infinity, which cause the cacheExtent to be double.Nan after the
calculation and crash the app.

## Pre-launch Checklist

- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] 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].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] 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
2026-02-09 22:10:20 +00:00

282 lines
10 KiB
Dart

// 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/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('CustomScrollView respects scrollCacheExtent', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: CustomScrollView(scrollCacheExtent: ScrollCacheExtent.viewport(1.0)),
),
);
final RenderViewport viewport = tester.renderObject(find.byType(Viewport));
expect(viewport.scrollCacheExtent, const ScrollCacheExtent.viewport(1.0));
});
testWidgets('ListView respects scrollCacheExtent', (WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: ListView(scrollCacheExtent: const ScrollCacheExtent.viewport(1.0)),
),
);
final RenderViewport viewport = tester.renderObject(find.byType(Viewport));
expect(viewport.scrollCacheExtent, const ScrollCacheExtent.viewport(1.0));
});
testWidgets('ListView.builder respects scrollCacheExtent', (WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: ListView.builder(
scrollCacheExtent: const ScrollCacheExtent.viewport(1.0),
itemBuilder: (BuildContext context, int index) => const Text(''),
itemCount: 0,
),
),
);
final RenderViewport viewport = tester.renderObject(find.byType(Viewport));
expect(viewport.scrollCacheExtent, const ScrollCacheExtent.viewport(1.0));
});
testWidgets('ListView.separated respects scrollCacheExtent', (WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: ListView.separated(
scrollCacheExtent: const ScrollCacheExtent.viewport(1.0),
itemBuilder: (BuildContext context, int index) => const Text(''),
separatorBuilder: (BuildContext context, int index) => const Text(''),
itemCount: 0,
),
),
);
final RenderViewport viewport = tester.renderObject(find.byType(Viewport));
expect(viewport.scrollCacheExtent, const ScrollCacheExtent.viewport(1.0));
});
testWidgets('ListView.custom respects scrollCacheExtent', (WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: ListView.custom(
scrollCacheExtent: const ScrollCacheExtent.viewport(1.0),
childrenDelegate: SliverChildListDelegate(const <Widget>[]),
),
),
);
final RenderViewport viewport = tester.renderObject(find.byType(Viewport));
expect(viewport.scrollCacheExtent, const ScrollCacheExtent.viewport(1.0));
});
testWidgets('GridView respects scrollCacheExtent', (WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: GridView(
scrollCacheExtent: const ScrollCacheExtent.viewport(1.0),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 1),
),
),
);
final RenderViewport viewport = tester.renderObject(find.byType(Viewport));
expect(viewport.scrollCacheExtent, const ScrollCacheExtent.viewport(1.0));
});
testWidgets('GridView.builder respects scrollCacheExtent', (WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: GridView.builder(
scrollCacheExtent: const ScrollCacheExtent.viewport(1.0),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 1),
itemBuilder: (BuildContext context, int index) => const Text(''),
itemCount: 0,
),
),
);
final RenderViewport viewport = tester.renderObject(find.byType(Viewport));
expect(viewport.scrollCacheExtent, const ScrollCacheExtent.viewport(1.0));
});
testWidgets('GridView.custom respects scrollCacheExtent', (WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: GridView.custom(
scrollCacheExtent: const ScrollCacheExtent.viewport(1.0),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 1),
childrenDelegate: SliverChildListDelegate(const <Widget>[]),
),
),
);
final RenderViewport viewport = tester.renderObject(find.byType(Viewport));
expect(viewport.scrollCacheExtent, const ScrollCacheExtent.viewport(1.0));
});
testWidgets('GridView.count respects scrollCacheExtent', (WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: GridView.count(
scrollCacheExtent: const ScrollCacheExtent.viewport(1.0),
crossAxisCount: 1,
),
),
);
final RenderViewport viewport = tester.renderObject(find.byType(Viewport));
expect(viewport.scrollCacheExtent, const ScrollCacheExtent.viewport(1.0));
});
testWidgets('GridView.extent respects scrollCacheExtent', (WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: GridView.extent(
scrollCacheExtent: const ScrollCacheExtent.viewport(1.0),
maxCrossAxisExtent: 100,
),
),
);
final RenderViewport viewport = tester.renderObject(find.byType(Viewport));
expect(viewport.scrollCacheExtent, const ScrollCacheExtent.viewport(1.0));
});
testWidgets('shrinkWrap in unbounded context with scrollCacheExtent style viewport', (
WidgetTester tester,
) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: SingleChildScrollView(
child: ListView.builder(
scrollCacheExtent: const ScrollCacheExtent.viewport(0.5),
shrinkWrap: true,
itemCount: 20,
itemBuilder: (BuildContext context, int index) {
return Text('$index');
},
),
),
),
);
expect(find.text('0'), findsOneWidget);
expect(find.text('19'), findsOneWidget);
expect(tester.takeException(), isNull);
});
testWidgets('ScrollView respects scrollCacheExtent (pixels)', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: CustomScrollView(scrollCacheExtent: ScrollCacheExtent.pixels(100)),
),
);
final RenderViewport viewport = tester.renderObject(find.byType(Viewport));
expect(viewport.scrollCacheExtent, const ScrollCacheExtent.pixels(100));
// The deprecated getter should still return the value.
expect(viewport.cacheExtent, 100);
expect(viewport.cacheExtentStyle, CacheExtentStyle.pixel);
});
testWidgets('ScrollView respects scrollCacheExtent (viewport)', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: CustomScrollView(scrollCacheExtent: ScrollCacheExtent.viewport(2.0)),
),
);
final RenderViewport viewport = tester.renderObject(find.byType(Viewport));
expect(viewport.scrollCacheExtent, const ScrollCacheExtent.viewport(2.0));
// The deprecated getter should still return the value.
expect(viewport.cacheExtent, 2.0);
expect(viewport.cacheExtentStyle, CacheExtentStyle.viewport);
});
testWidgets('Semantics nodes in cache extent are marked as hidden', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: ListView.builder(
scrollCacheExtent: const ScrollCacheExtent.pixels(100.0),
itemCount: 20,
itemBuilder: (BuildContext context, int index) {
return SizedBox(height: 100, child: Text('Item $index'));
},
),
),
);
// Viewport height is 600 by default.
// Items 0-5 are visible (0-600).
// Item 6 is in cache extent (600-700).
// Item 7 is outside (700-800).
expect(find.text('Item 0'), findsOneWidget);
expect(find.text('Item 5'), findsOneWidget);
expect(find.text('Item 6', skipOffstage: false), findsOneWidget);
expect(find.text('Item 7', skipOffstage: false), findsNothing);
// Check semantics
expect(tester.getSemantics(find.text('Item 5')), matchesSemantics(label: 'Item 5'));
expect(
tester.getSemantics(find.text('Item 6', skipOffstage: false)),
matchesSemantics(label: 'Item 6', isHidden: true),
);
handle.dispose();
});
testWidgets('Semantics nodes in cache extent (viewport) are marked as hidden', (
WidgetTester tester,
) async {
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: ListView.builder(
scrollCacheExtent: const ScrollCacheExtent.viewport(2.0),
itemCount: 20,
itemBuilder: (BuildContext context, int index) {
return SizedBox(height: 100, child: Text('Item $index'));
},
),
),
);
// Viewport height is 600 by default.
// Cache extent is 2.0 * 600 = 1200.
// Items 0-5 are visible (0-600).
// Items 6-17 are in cache extent (600-1800).
// Item 18 starts at 1800, so it is just outside.
expect(find.text('Item 0'), findsOneWidget);
expect(find.text('Item 5'), findsOneWidget);
expect(find.text('Item 6', skipOffstage: false), findsOneWidget);
expect(find.text('Item 17', skipOffstage: false), findsOneWidget);
expect(find.text('Item 18', skipOffstage: false), findsNothing);
// Check semantics
expect(tester.getSemantics(find.text('Item 5')), matchesSemantics(label: 'Item 5'));
expect(
tester.getSemantics(find.text('Item 6', skipOffstage: false)),
matchesSemantics(label: 'Item 6', isHidden: true),
);
expect(
tester.getSemantics(find.text('Item 17', skipOffstage: false)),
matchesSemantics(label: 'Item 17', isHidden: true),
);
handle.dispose();
});
}