flutter_flutter/packages/flutter/test/rendering/slivers_block_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

346 lines
11 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/animation.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'rendering_tester.dart';
class TestRenderSliverBoxChildManager extends RenderSliverBoxChildManager {
TestRenderSliverBoxChildManager({required this.children});
late RenderSliverList _renderObject;
List<RenderBox> children;
RenderSliverList createRenderObject() {
_renderObject = RenderSliverList(childManager: this);
return _renderObject;
}
int? _currentlyUpdatingChildIndex;
@override
void createChild(int index, {required RenderBox? after}) {
if (index < 0 || index >= children.length) {
return;
}
try {
_currentlyUpdatingChildIndex = index;
_renderObject.insert(children[index], after: after);
} finally {
_currentlyUpdatingChildIndex = null;
}
}
@override
void removeChild(RenderBox child) {
_renderObject.remove(child);
}
@override
double estimateMaxScrollOffset(
SliverConstraints constraints, {
int? firstIndex,
int? lastIndex,
double? leadingScrollOffset,
double? trailingScrollOffset,
}) {
assert(lastIndex! >= firstIndex!);
return children.length *
(trailingScrollOffset! - leadingScrollOffset!) /
(lastIndex! - firstIndex! + 1);
}
@override
int get childCount => children.length;
@override
void didAdoptChild(RenderBox child) {
assert(_currentlyUpdatingChildIndex != null);
final childParentData = child.parentData! as SliverMultiBoxAdaptorParentData;
childParentData.index = _currentlyUpdatingChildIndex;
}
@override
void setDidUnderflow(bool value) {}
}
class ViewportOffsetSpy extends ViewportOffset {
ViewportOffsetSpy(this._pixels);
double _pixels;
@override
double get pixels => _pixels;
@override
bool get hasPixels => true;
bool corrected = false;
@override
bool applyViewportDimension(double viewportDimension) => true;
@override
bool applyContentDimensions(double minScrollExtent, double maxScrollExtent) => true;
@override
void correctBy(double correction) {
_pixels += correction;
corrected = true;
}
@override
void jumpTo(double pixels) {
// Do nothing, not required in test.
}
@override
Future<void> animateTo(double to, {required Duration duration, required Curve curve}) async {
// Do nothing, not required in test.
}
@override
ScrollDirection get userScrollDirection => ScrollDirection.idle;
@override
bool get allowImplicitScrolling => false;
}
void main() {
TestRenderingFlutterBinding.ensureInitialized();
test('RenderSliverList basic test - down', () {
RenderObject inner;
RenderBox a, b, c, d, e;
final childManager = TestRenderSliverBoxChildManager(
children: <RenderBox>[
a = RenderSizedBox(const Size(100.0, 400.0)),
b = RenderSizedBox(const Size(100.0, 400.0)),
c = RenderSizedBox(const Size(100.0, 400.0)),
d = RenderSizedBox(const Size(100.0, 400.0)),
e = RenderSizedBox(const Size(100.0, 400.0)),
],
);
final root = RenderViewport(
crossAxisDirection: AxisDirection.right,
offset: ViewportOffset.zero(),
cacheExtent: 0.0,
children: <RenderSliver>[inner = childManager.createRenderObject()],
);
layout(root);
expect(root.size.width, equals(800.0));
expect(root.size.height, equals(600.0));
expect(a.localToGlobal(Offset.zero), Offset.zero);
expect(b.localToGlobal(Offset.zero), const Offset(0.0, 400.0));
expect(c.attached, false);
expect(d.attached, false);
expect(e.attached, false);
// make sure that layout is stable by laying out again
inner.markNeedsLayout();
pumpFrame();
expect(a.localToGlobal(Offset.zero), Offset.zero);
expect(b.localToGlobal(Offset.zero), const Offset(0.0, 400.0));
expect(c.attached, false);
expect(d.attached, false);
expect(e.attached, false);
// now try various scroll offsets
root.offset = ViewportOffset.fixed(200.0);
pumpFrame();
expect(a.localToGlobal(Offset.zero), const Offset(0.0, -200.0));
expect(b.localToGlobal(Offset.zero), const Offset(0.0, 200.0));
expect(c.attached, false);
expect(d.attached, false);
expect(e.attached, false);
root.offset = ViewportOffset.fixed(600.0);
pumpFrame();
expect(a.attached, false);
expect(b.localToGlobal(Offset.zero), const Offset(0.0, -200.0));
expect(c.localToGlobal(Offset.zero), const Offset(0.0, 200.0));
expect(d.attached, false);
expect(e.attached, false);
root.offset = ViewportOffset.fixed(900.0);
pumpFrame();
expect(a.attached, false);
expect(b.attached, false);
expect(c.localToGlobal(Offset.zero), const Offset(0.0, -100.0));
expect(d.localToGlobal(Offset.zero), const Offset(0.0, 300.0));
expect(e.attached, false);
// try going back up
root.offset = ViewportOffset.fixed(200.0);
pumpFrame();
expect(a.localToGlobal(Offset.zero), const Offset(0.0, -200.0));
expect(b.localToGlobal(Offset.zero), const Offset(0.0, 200.0));
expect(c.attached, false);
expect(d.attached, false);
expect(e.attached, false);
});
test('RenderSliverList basic test - up', () {
RenderObject inner;
RenderBox a, b, c, d, e;
final childManager = TestRenderSliverBoxChildManager(
children: <RenderBox>[
a = RenderSizedBox(const Size(100.0, 400.0)),
b = RenderSizedBox(const Size(100.0, 400.0)),
c = RenderSizedBox(const Size(100.0, 400.0)),
d = RenderSizedBox(const Size(100.0, 400.0)),
e = RenderSizedBox(const Size(100.0, 400.0)),
],
);
final root = RenderViewport(
axisDirection: AxisDirection.up,
crossAxisDirection: AxisDirection.right,
offset: ViewportOffset.zero(),
children: <RenderSliver>[inner = childManager.createRenderObject()],
cacheExtent: 0.0,
);
layout(root);
expect(root.size.width, equals(800.0));
expect(root.size.height, equals(600.0));
expect(a.localToGlobal(Offset.zero), const Offset(0.0, 200.0));
expect(b.localToGlobal(Offset.zero), const Offset(0.0, -200.0));
expect(c.attached, false);
expect(d.attached, false);
expect(e.attached, false);
// make sure that layout is stable by laying out again
inner.markNeedsLayout();
pumpFrame();
expect(a.localToGlobal(Offset.zero), const Offset(0.0, 200.0));
expect(b.localToGlobal(Offset.zero), const Offset(0.0, -200.0));
expect(c.attached, false);
expect(d.attached, false);
expect(e.attached, false);
// now try various scroll offsets
root.offset = ViewportOffset.fixed(200.0);
pumpFrame();
expect(a.localToGlobal(Offset.zero), const Offset(0.0, 400.0));
expect(b.localToGlobal(Offset.zero), Offset.zero);
expect(c.attached, false);
expect(d.attached, false);
expect(e.attached, false);
root.offset = ViewportOffset.fixed(600.0);
pumpFrame();
expect(a.attached, false);
expect(b.localToGlobal(Offset.zero), const Offset(0.0, 400.0));
expect(c.localToGlobal(Offset.zero), Offset.zero);
expect(d.attached, false);
expect(e.attached, false);
root.offset = ViewportOffset.fixed(900.0);
pumpFrame();
expect(a.attached, false);
expect(b.attached, false);
expect(c.localToGlobal(Offset.zero), const Offset(0.0, 300.0));
expect(d.localToGlobal(Offset.zero), const Offset(0.0, -100.0));
expect(e.attached, false);
// try going back up
root.offset = ViewportOffset.fixed(200.0);
pumpFrame();
expect(a.localToGlobal(Offset.zero), const Offset(0.0, 400.0));
expect(b.localToGlobal(Offset.zero), Offset.zero);
expect(c.attached, false);
expect(d.attached, false);
expect(e.attached, false);
});
test('SliverList - no zero scroll offset correction', () {
RenderSliverList inner;
RenderBox a;
final childManager = TestRenderSliverBoxChildManager(
children: <RenderBox>[
a = RenderSizedBox(const Size(100.0, 400.0)),
RenderSizedBox(const Size(100.0, 400.0)),
RenderSizedBox(const Size(100.0, 400.0)),
RenderSizedBox(const Size(100.0, 400.0)),
RenderSizedBox(const Size(100.0, 400.0)),
],
);
final root = RenderViewport(
crossAxisDirection: AxisDirection.right,
offset: ViewportOffset.zero(),
children: <RenderSliver>[inner = childManager.createRenderObject()],
);
layout(root);
final parentData = a.parentData! as SliverMultiBoxAdaptorParentData;
parentData.layoutOffset = 0.001;
root.offset = ViewportOffset.fixed(900.0);
pumpFrame();
root.offset = ViewportOffset.fixed(0.0);
pumpFrame();
expect(inner.geometry?.scrollOffsetCorrection, isNull);
});
test('SliverList - no correction when tiny double precision error', () {
RenderSliverList inner;
RenderBox a;
final childManager = TestRenderSliverBoxChildManager(
children: <RenderBox>[
a = RenderSizedBox(const Size(100.0, 400.0)),
RenderSizedBox(const Size(100.0, 400.0)),
RenderSizedBox(const Size(100.0, 400.0)),
RenderSizedBox(const Size(100.0, 400.0)),
RenderSizedBox(const Size(100.0, 400.0)),
],
);
inner = childManager.createRenderObject();
final root = RenderViewport(
crossAxisDirection: AxisDirection.right,
offset: ViewportOffset.zero(),
children: <RenderSliver>[inner],
);
layout(root);
final parentData = a.parentData! as SliverMultiBoxAdaptorParentData;
// Simulate double precision error.
parentData.layoutOffset = -0.0000000000001;
root.offset = ViewportOffset.fixed(900.0);
pumpFrame();
final spy = ViewportOffsetSpy(0.0);
root.offset = spy;
pumpFrame();
expect(spy.corrected, false);
});
test('SliverMultiBoxAdaptorParentData.toString', () {
final candidate = SliverMultiBoxAdaptorParentData();
expect(candidate.keepAlive, isFalse);
expect(candidate.index, isNull);
expect(candidate.toString(), 'index=null; layoutOffset=None');
candidate.keepAlive = true;
expect(candidate.toString(), 'index=null; keepAlive; layoutOffset=None');
candidate.keepAlive = false;
expect(candidate.toString(), 'index=null; layoutOffset=None');
candidate.index = 0;
expect(candidate.toString(), 'index=0; layoutOffset=None');
candidate.index = 1;
expect(candidate.toString(), 'index=1; layoutOffset=None');
candidate.index = -1;
expect(candidate.toString(), 'index=-1; layoutOffset=None');
candidate.layoutOffset = 100.0;
expect(candidate.toString(), 'index=-1; layoutOffset=100.0');
});
}