flutter_flutter/packages/flutter/test/widgets/scroll_position_test.dart
Kate Lovett 9d96df2364
Modernize framework lints (#179089)
WIP

Commits separated as follows:
- Update lints in analysis_options files
- Run `dart fix --apply`
- Clean up leftover analysis issues 
- Run `dart format .` in the right places.

Local analysis and testing passes. Checking CI now.

Part of https://github.com/flutter/flutter/issues/178827
- Adoption of flutter_lints in examples/api coming in a separate change
(cc @loic-sharma)

## 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
2025-11-26 01:10:39 +00:00

277 lines
12 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/material.dart';
import 'package:flutter_test/flutter_test.dart';
ScrollController _controller = ScrollController(initialScrollOffset: 110.0);
class ThePositiveNumbers extends StatelessWidget {
const ThePositiveNumbers({super.key, required this.from});
final int from;
@override
Widget build(BuildContext context) {
return ListView.builder(
key: const PageStorageKey<String>('ThePositiveNumbers'),
itemExtent: 100.0,
controller: _controller,
itemBuilder: (BuildContext context, int index) {
return Text('${index + from}', key: ValueKey<int>(index));
},
);
}
}
Future<void> performTest(WidgetTester tester, bool maintainState) async {
final navigatorKey = GlobalKey<NavigatorState>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: MediaQuery(
data: MediaQueryData.fromView(tester.view),
child: Navigator(
key: navigatorKey,
onGenerateRoute: (RouteSettings settings) {
if (settings.name == '/') {
return MaterialPageRoute<void>(
settings: settings,
builder: (_) => const ThePositiveNumbers(from: 0),
maintainState: maintainState,
);
} else if (settings.name == '/second') {
return MaterialPageRoute<void>(
settings: settings,
builder: (_) => const ThePositiveNumbers(from: 10000),
maintainState: maintainState,
);
}
return null;
},
),
),
),
);
// we're 600 pixels high, each item is 100 pixels high, scroll position is
// 110.0, so we should have 7 items, 1..7.
expect(find.text('0'), findsNothing, reason: 'with maintainState: $maintainState');
expect(find.text('1'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('2'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('3'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('4'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('5'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('6'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('7'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('8'), findsNothing, reason: 'with maintainState: $maintainState');
expect(find.text('10'), findsNothing, reason: 'with maintainState: $maintainState');
expect(find.text('100'), findsNothing, reason: 'with maintainState: $maintainState');
tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(1000.0);
await tester.pump(const Duration(seconds: 1));
// we're 600 pixels high, each item is 100 pixels high, scroll position is
// 1000, so we should have exactly 6 items, 10..15.
expect(find.text('0'), findsNothing, reason: 'with maintainState: $maintainState');
expect(find.text('1'), findsNothing, reason: 'with maintainState: $maintainState');
expect(find.text('8'), findsNothing, reason: 'with maintainState: $maintainState');
expect(find.text('9'), findsNothing, reason: 'with maintainState: $maintainState');
expect(find.text('10'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('11'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('12'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('13'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('14'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('15'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('16'), findsNothing, reason: 'with maintainState: $maintainState');
expect(find.text('100'), findsNothing, reason: 'with maintainState: $maintainState');
navigatorKey.currentState!.pushNamed('/second');
await tester.pump(); // navigating always takes two frames, one to start...
await tester.pump(const Duration(seconds: 1)); // ...and one to end the transition
// the second list is now visible, starting at 10001
expect(find.text('0'), findsNothing, reason: 'with maintainState: $maintainState');
expect(find.text('1'), findsNothing, reason: 'with maintainState: $maintainState');
expect(find.text('10'), findsNothing, reason: 'with maintainState: $maintainState');
expect(find.text('11'), findsNothing, reason: 'with maintainState: $maintainState');
expect(find.text('10000'), findsNothing, reason: 'with maintainState: $maintainState');
expect(find.text('10001'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('10002'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('10003'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('10004'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('10005'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('10006'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('10007'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('10008'), findsNothing, reason: 'with maintainState: $maintainState');
expect(find.text('10010'), findsNothing, reason: 'with maintainState: $maintainState');
expect(find.text('10100'), findsNothing, reason: 'with maintainState: $maintainState');
navigatorKey.currentState!.pop();
await tester.pump(); // again, navigating always takes two frames
// Ensure we don't clamp the scroll offset even during the navigation.
// https://github.com/flutter/flutter/issues/4883
final ScrollableState state = tester.state(find.byType(Scrollable).first);
expect(state.position.pixels, equals(1000.0), reason: 'with maintainState: $maintainState');
await tester.pump(const Duration(seconds: 1));
// we're 600 pixels high, each item is 100 pixels high, scroll position is
// 1000, so we should have exactly 6 items, 10..15.
expect(find.text('0'), findsNothing, reason: 'with maintainState: $maintainState');
expect(find.text('1'), findsNothing, reason: 'with maintainState: $maintainState');
expect(find.text('8'), findsNothing, reason: 'with maintainState: $maintainState');
expect(find.text('9'), findsNothing, reason: 'with maintainState: $maintainState');
expect(find.text('10'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('11'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('12'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('13'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('14'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('15'), findsOneWidget, reason: 'with maintainState: $maintainState');
expect(find.text('16'), findsNothing, reason: 'with maintainState: $maintainState');
expect(find.text('100'), findsNothing, reason: 'with maintainState: $maintainState');
}
void main() {
testWidgets("ScrollPosition jumpTo() doesn't call notifyListeners twice", (
WidgetTester tester,
) async {
var count = 0;
await tester.pumpWidget(
MaterialApp(
home: ListView.builder(
itemBuilder: (BuildContext context, int index) {
return Text('$index', textDirection: TextDirection.ltr);
},
),
),
);
final ScrollPosition position = tester.state<ScrollableState>(find.byType(Scrollable)).position;
position.addListener(() {
count++;
});
position.jumpTo(100);
expect(count, 1);
});
testWidgets('whether we remember our scroll position', (WidgetTester tester) async {
await performTest(tester, true);
await performTest(tester, false);
});
testWidgets('scroll alignment is honored by ensureVisible', (WidgetTester tester) async {
final List<int> items = List<int>.generate(11, (int index) => index).toList();
final List<FocusNode> nodes = List<FocusNode>.generate(
11,
(int index) => FocusNode(debugLabel: 'Item ${index + 1}'),
).toList();
addTearDown(() {
for (final node in nodes) {
node.dispose();
}
});
final controller = ScrollController();
addTearDown(controller.dispose);
await tester.pumpWidget(
MaterialApp(
home: ListView(
controller: controller,
children: items.map<Widget>((int item) {
return Focus(
key: ValueKey<int>(item),
focusNode: nodes[item],
child: Container(height: 110),
);
}).toList(),
),
),
);
controller.position.ensureVisible(
tester.renderObject(find.byKey(const ValueKey<int>(0))),
alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
);
expect(controller.position.pixels, equals(0.0));
controller.position.ensureVisible(
tester.renderObject(find.byKey(const ValueKey<int>(1))),
alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
);
expect(controller.position.pixels, equals(0.0));
controller.position.ensureVisible(
tester.renderObject(find.byKey(const ValueKey<int>(1))),
alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtEnd,
);
expect(controller.position.pixels, equals(0.0));
controller.position.ensureVisible(
tester.renderObject(find.byKey(const ValueKey<int>(4))),
alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
);
expect(controller.position.pixels, equals(0.0));
controller.position.ensureVisible(
tester.renderObject(find.byKey(const ValueKey<int>(5))),
alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
);
expect(controller.position.pixels, equals(0.0));
controller.position.ensureVisible(
tester.renderObject(find.byKey(const ValueKey<int>(5))),
alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtEnd,
);
expect(controller.position.pixels, equals(60.0));
controller.position.ensureVisible(
tester.renderObject(find.byKey(const ValueKey<int>(0))),
alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
);
expect(controller.position.pixels, equals(0.0));
});
testWidgets('jumpTo recommends deferred loading', (WidgetTester tester) async {
var loadedWithDeferral = 0;
var buildCount = 0;
const double height = 500;
await tester.pumpWidget(
MaterialApp(
home: ListView.builder(
itemBuilder: (BuildContext context, int index) {
buildCount += 1;
if (Scrollable.recommendDeferredLoadingForContext(context)) {
loadedWithDeferral += 1;
}
return const SizedBox(height: height);
},
),
),
);
// The two visible on screen should have loaded without deferral.
expect(buildCount, 2);
expect(loadedWithDeferral, 0);
final ScrollPosition position = tester.state<ScrollableState>(find.byType(Scrollable)).position;
position.jumpTo(height * 100);
await tester.pump();
// All but the first two that were loaded normally should have gotten a
// recommendation to defer.
expect(buildCount, 102);
expect(loadedWithDeferral, 100);
position.jumpTo(height * 102);
await tester.pump();
// The smaller jump should not have recommended deferral.
expect(buildCount, 104);
expect(loadedWithDeferral, 100);
});
}