flutter_flutter/packages/flutter/test/widgets/scroll_controller_test.dart
Jenn Magder 2edf3d91b7
Rebase ios-experimental onto main (#173804)
Rebase ios-experimental branch onto main. This will make the PRs
experimenting with newer versions of Xcode (like
https://github.com/flutter/flutter/pull/173123) smaller and easier to
reason about.

Rebases #168860 and #170274
```
$ git rebase main -Xtheirs
```

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: gaaclarke <30870216+gaaclarke@users.noreply.github.com>
Co-authored-by: Siva <a-siva@users.noreply.github.com>
Co-authored-by: engine-flutter-autoroll <engine-flutter-autoroll@skia.org>
Co-authored-by: Jamil Saadeh <jssaadeh@outlook.com>
Co-authored-by: Dara Adedeji <76637177+SunkenInTime@users.noreply.github.com>
Co-authored-by: Greg Price <gnprice@gmail.com>
Co-authored-by: Ben Konyi <bkonyi@google.com>
Co-authored-by: Ricardo Dalarme <ricardodalarme@outlook.com>
Co-authored-by: Flutter GitHub Bot <fluttergithubbot@gmail.com>
Co-authored-by: Justin McCandless <jmccandless@google.com>
Co-authored-by: Alex Talebi <31685655+SalehTZ@users.noreply.github.com>
Co-authored-by: Qun Cheng <36861262+QuncCccccc@users.noreply.github.com>
Co-authored-by: Mouad Debbar <mdebbar@google.com>
Co-authored-by: Zuckjet <1083941774@qq.com>
Co-authored-by: Loïc Sharma <737941+loic-sharma@users.noreply.github.com>
Co-authored-by: auto-submit[bot] <98614782+auto-submit[bot]@users.noreply.github.com>
Co-authored-by: auto-submit[bot] <flutter-engprod-team@google.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: yim <ybz975218925@gmail.com>
Co-authored-by: bufffun <chenmingding.cmd@alibaba-inc.com>
Co-authored-by: Chinmay Garde <chinmaygarde@google.com>
Co-authored-by: Hannah Jin <jhy03261997@gmail.com>
Co-authored-by: Kate Lovett <katelovett@google.com>
Co-authored-by: Valentin Vignal <32538273+ValentinVignal@users.noreply.github.com>
Co-authored-by: Derek Xu <derekx@google.com>
Co-authored-by: Yash Dhrangdhariya <72062416+Yash-Dhrangdhariya@users.noreply.github.com>
Co-authored-by: bungeman <bungeman@chromium.org>
Co-authored-by: Ahmed Mohamed Sameh <ahmedsameha1@gmail.com>
Co-authored-by: John "codefu" McDole <codefu@google.com>
Co-authored-by: Dmitry Grand <dmgr@google.com>
Co-authored-by: Kostia Sokolovskyi <sokolovskyi.konstantin@gmail.com>
Co-authored-by: Reid Baker <1063596+reidbaker@users.noreply.github.com>
Co-authored-by: Matthew Kosarek <matt.kosarek@canonical.com>
Co-authored-by: Jason Simmons <jason-simmons@users.noreply.github.com>
Co-authored-by: Jim Graham <flar@google.com>
Co-authored-by: Michael Goderbauer <goderbauer@google.com>
Co-authored-by: Gray Mackall <34871572+gmackall@users.noreply.github.com>
Co-authored-by: Gray Mackall <mackall@google.com>
Co-authored-by: Tong Mu <dkwingsmt@users.noreply.github.com>
Co-authored-by: Jon Ihlas <jon.i@hotmail.fr>
Co-authored-by: Micael Cid <micaelcid10@gmail.com>
Co-authored-by: Alexander Aprelev <aam@google.com>
Co-authored-by: hellohuanlin <41930132+hellohuanlin@users.noreply.github.com>
Co-authored-by: Luke Memet <1598289+lukemmtt@users.noreply.github.com>
Co-authored-by: Victoria Ashworth <15619084+vashworth@users.noreply.github.com>
Co-authored-by: Mairramer <50643541+Mairramer@users.noreply.github.com>
Co-authored-by: Florin Malita <fmalita@gmail.com>
Co-authored-by: chunhtai <47866232+chunhtai@users.noreply.github.com>
Co-authored-by: Salem Iranloye <127918074+salemiranloye@users.noreply.github.com>
Co-authored-by: Kevin Moore <kevmoo@google.com>
Co-authored-by: Sydney Bao <sydneybao@google.com>
Co-authored-by: Wdestroier <Wdestroier@gmail.com>
Co-authored-by: Matt Boetger <matt.boetger@gmail.com>
Co-authored-by: Reid Baker <reidbaker@google.com>
Co-authored-by: Victor Sanni <victorsanniay@gmail.com>
Co-authored-by: Jessy Yameogo <jessy.yameogo@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: romain.gyh <11901536+romaingyh@users.noreply.github.com>
Co-authored-by: Robert Ancell <robert.ancell@canonical.com>
Co-authored-by: TheLastFlame <131446187+TheLastFlame@users.noreply.github.com>
Co-authored-by: masato <returnymgstokh@icloud.com>
Co-authored-by: Albin PK <56157868+albinpk@users.noreply.github.com>
Co-authored-by: Huy <huy@nevercode.io>
Co-authored-by: Matan Lurey <matanlurey@users.noreply.github.com>
Co-authored-by: Azat Chorekliyev <azat24680@gmail.com>
Co-authored-by: EdwynZN <edwinzn9@gmail.com>
Co-authored-by: Bruno Leroux <bruno.leroux@gmail.com>
Co-authored-by: Dev TtangKong <ttankkeo112@gmail.com>
Co-authored-by: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com>
Co-authored-by: Houssem Eddine Fadhli <houssemeddinefadhli81@gmail.com>
2025-08-19 12:05:40 -07:00

419 lines
14 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 'dart:ui' as ui;
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
import 'states.dart';
void main() {
testWidgets('ScrollController control test', (WidgetTester tester) async {
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: ListView(
controller: controller,
children: kStates.map<Widget>((String state) {
return SizedBox(height: 200.0, child: Text(state));
}).toList(),
),
),
);
double realOffset() {
return tester.state<ScrollableState>(find.byType(Scrollable)).position.pixels;
}
expect(controller.offset, equals(0.0));
expect(realOffset(), equals(controller.offset));
controller.jumpTo(653.0);
expect(controller.offset, equals(653.0));
expect(realOffset(), equals(controller.offset));
await tester.pump();
expect(controller.offset, equals(653.0));
expect(realOffset(), equals(controller.offset));
controller.animateTo(326.0, duration: const Duration(milliseconds: 300), curve: Curves.ease);
await tester.pumpAndSettle();
expect(controller.offset, equals(326.0));
expect(realOffset(), equals(controller.offset));
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: ListView(
key: const Key('second'),
controller: controller,
children: kStates.map<Widget>((String state) {
return SizedBox(height: 200.0, child: Text(state));
}).toList(),
),
),
);
expect(controller.offset, equals(0.0));
expect(realOffset(), equals(controller.offset));
controller.jumpTo(653.0);
expect(controller.offset, equals(653.0));
expect(realOffset(), equals(controller.offset));
final ScrollController controller2 = ScrollController();
addTearDown(controller2.dispose);
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: ListView(
key: const Key('second'),
controller: controller2,
children: kStates.map<Widget>((String state) {
return SizedBox(height: 200.0, child: Text(state));
}).toList(),
),
),
);
expect(() => controller.offset, throwsAssertionError);
expect(controller2.offset, equals(653.0));
expect(realOffset(), equals(controller2.offset));
expect(() => controller.jumpTo(120.0), throwsAssertionError);
expect(
() => controller.animateTo(
132.0,
duration: const Duration(milliseconds: 300),
curve: Curves.ease,
),
throwsAssertionError,
);
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: ListView(
key: const Key('second'),
controller: controller2,
physics: const BouncingScrollPhysics(),
children: kStates.map<Widget>((String state) {
return SizedBox(height: 200.0, child: Text(state));
}).toList(),
),
),
);
expect(controller2.offset, equals(653.0));
expect(realOffset(), equals(controller2.offset));
controller2.jumpTo(432.0);
expect(controller2.offset, equals(432.0));
expect(realOffset(), equals(controller2.offset));
await tester.pump();
expect(controller2.offset, equals(432.0));
expect(realOffset(), equals(controller2.offset));
});
testWidgets('ScrollController control test', (WidgetTester tester) async {
final ScrollController controller = ScrollController(initialScrollOffset: 209.0);
addTearDown(controller.dispose);
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: GridView.count(
crossAxisCount: 4,
controller: controller,
children: kStates.map<Widget>((String state) => Text(state)).toList(),
),
),
);
double realOffset() {
return tester.state<ScrollableState>(find.byType(Scrollable)).position.pixels;
}
expect(controller.offset, equals(209.0));
expect(realOffset(), equals(controller.offset));
controller.jumpTo(105.0);
await tester.pump();
expect(controller.offset, equals(105.0));
expect(realOffset(), equals(controller.offset));
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: GridView.count(
crossAxisCount: 2,
controller: controller,
children: kStates.map<Widget>((String state) => Text(state)).toList(),
),
),
);
expect(controller.offset, equals(105.0));
expect(realOffset(), equals(controller.offset));
});
testWidgets('DrivenScrollActivity ending after dispose', (WidgetTester tester) async {
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: ListView(controller: controller, children: <Widget>[Container(height: 200000.0)]),
),
);
controller.animateTo(1000.0, duration: const Duration(seconds: 1), curve: Curves.linear);
await tester.pump(); // Start the animation.
// We will now change the tree on the same frame as the animation ends.
await tester.pumpWidget(Container(), duration: const Duration(seconds: 2));
});
testWidgets('Read operations on ScrollControllers with no positions fail', (
WidgetTester tester,
) async {
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);
expect(() => controller.offset, throwsAssertionError);
expect(() => controller.position, throwsAssertionError);
});
testWidgets('Read operations on ScrollControllers with more than one position fail', (
WidgetTester tester,
) async {
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: ListView(
children: <Widget>[
Container(
constraints: const BoxConstraints(maxHeight: 500.0),
child: ListView(
controller: controller,
children: kStates.map<Widget>((String state) {
return SizedBox(height: 200.0, child: Text(state));
}).toList(),
),
),
Container(
constraints: const BoxConstraints(maxHeight: 500.0),
child: ListView(
controller: controller,
children: kStates.map<Widget>((String state) {
return SizedBox(height: 200.0, child: Text(state));
}).toList(),
),
),
],
),
),
);
expect(() => controller.offset, throwsAssertionError);
expect(() => controller.position, throwsAssertionError);
});
testWidgets('Write operations on ScrollControllers with no positions fail', (
WidgetTester tester,
) async {
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);
expect(
() => controller.animateTo(1.0, duration: const Duration(seconds: 1), curve: Curves.linear),
throwsAssertionError,
);
expect(() => controller.jumpTo(1.0), throwsAssertionError);
});
testWidgets('Write operations on ScrollControllers with more than one position do not throw', (
WidgetTester tester,
) async {
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: ListView(
children: <Widget>[
Container(
constraints: const BoxConstraints(maxHeight: 500.0),
child: ListView(
controller: controller,
children: kStates.map<Widget>((String state) {
return SizedBox(height: 200.0, child: Text(state));
}).toList(),
),
),
Container(
constraints: const BoxConstraints(maxHeight: 500.0),
child: ListView(
controller: controller,
children: kStates.map<Widget>((String state) {
return SizedBox(height: 200.0, child: Text(state));
}).toList(),
),
),
],
),
),
);
controller.jumpTo(1.0);
controller.animateTo(1.0, duration: const Duration(seconds: 1), curve: Curves.linear);
await tester.pumpAndSettle();
});
testWidgets('Scroll controllers notify when the position changes', (WidgetTester tester) async {
final ScrollController controller = ScrollController();
final List<double> log = <double>[];
controller.addListener(() {
log.add(controller.offset);
});
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: ListView(
controller: controller,
children: kStates.map<Widget>((String state) {
return SizedBox(height: 200.0, child: Text(state));
}).toList(),
),
),
);
expect(log, isEmpty);
await tester.drag(find.byType(ListView), const Offset(0.0, -250.0));
expect(log, equals(<double>[20.0, 250.0]));
log.clear();
controller.dispose();
await tester.drag(find.byType(ListView), const Offset(0.0, -130.0));
expect(log, isEmpty);
});
testWidgets('keepScrollOffset', (WidgetTester tester) async {
final PageStorageBucket bucket = PageStorageBucket();
Widget buildFrame(ScrollController controller) {
return Directionality(
textDirection: TextDirection.ltr,
child: PageStorage(
bucket: bucket,
child: KeyedSubtree(
key: const PageStorageKey<String>('ListView'),
child: ListView(
key: UniqueKey(), // it's a different ListView every time
controller: controller,
children: List<Widget>.generate(50, (int index) {
return SizedBox(height: 100.0, child: Text('Item $index'));
}).toList(),
),
),
),
);
}
// keepScrollOffset: true (the default). The scroll offset is restored
// when the ListView is recreated with a new ScrollController.
// The initialScrollOffset is used in this case, because there's no saved
// scroll offset.
ScrollController controller = ScrollController(initialScrollOffset: 200.0);
addTearDown(controller.dispose);
await tester.pumpWidget(buildFrame(controller));
expect(tester.getTopLeft(find.widgetWithText(SizedBox, 'Item 2')), Offset.zero);
controller.jumpTo(2000.0);
await tester.pump();
expect(tester.getTopLeft(find.widgetWithText(SizedBox, 'Item 20')), Offset.zero);
// The initialScrollOffset isn't used in this case, because the scrolloffset
// can be restored.
controller = ScrollController(initialScrollOffset: 25.0);
addTearDown(controller.dispose);
await tester.pumpWidget(buildFrame(controller));
expect(controller.offset, 2000.0);
expect(tester.getTopLeft(find.widgetWithText(SizedBox, 'Item 20')), Offset.zero);
// keepScrollOffset: false. The scroll offset is -not- restored
// when the ListView is recreated with a new ScrollController and
// the initialScrollOffset is used.
controller = ScrollController(keepScrollOffset: false, initialScrollOffset: 100.0);
addTearDown(controller.dispose);
await tester.pumpWidget(buildFrame(controller));
expect(controller.offset, 100.0);
expect(tester.getTopLeft(find.widgetWithText(SizedBox, 'Item 1')), Offset.zero);
});
testWidgets('isScrollingNotifier works with pointer scroll', (WidgetTester tester) async {
Widget buildFrame(ScrollController controller) {
return Directionality(
textDirection: TextDirection.ltr,
child: ListView(
controller: controller,
children: List<Widget>.generate(50, (int index) {
return SizedBox(height: 100.0, child: Text('Item $index'));
}).toList(),
),
);
}
bool isScrolling = false;
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);
controller.addListener(() {
isScrolling = controller.position.isScrollingNotifier.value;
});
await tester.pumpWidget(buildFrame(controller));
final Offset scrollEventLocation = tester.getCenter(find.byType(ListView));
final TestPointer testPointer = TestPointer(1, ui.PointerDeviceKind.mouse);
// Create a hover event so that |testPointer| has a location when generating the scroll.
testPointer.hover(scrollEventLocation);
await tester.sendEventToBinding(testPointer.scroll(const Offset(0.0, 300.0)));
// When the listener was notified, the value of the isScrollingNotifier
// should have been true
expect(isScrolling, isTrue);
});
test('$ScrollController dispatches object creation in constructor', () async {
await expectLater(
await memoryEvents(() => ScrollController().dispose(), ScrollController),
areCreateAndDispose,
);
});
}