slivers_appbar_pinned_test.dart 16.4 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Ian Hickson's avatar
Ian Hickson committed
2 3 4 5
// 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';
6
import 'package:flutter/widgets.dart';
7
import 'package:flutter_test/flutter_test.dart';
8
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
Ian Hickson's avatar
Ian Hickson committed
9 10

void verifyPaintPosition(GlobalKey key, Offset ideal, bool visible) {
11
  final RenderSliver target = key.currentContext!.findRenderObject()! as RenderSliver;
Dan Field's avatar
Dan Field committed
12
  expect(target.parent, isA<RenderViewport>());
13
  final SliverPhysicalParentData parentData = target.parentData! as SliverPhysicalParentData;
14
  final Offset actual = parentData.paintOffset;
Ian Hickson's avatar
Ian Hickson committed
15
  expect(actual, ideal);
16
  final SliverGeometry geometry = target.geometry!;
Ian Hickson's avatar
Ian Hickson committed
17 18 19 20
  expect(geometry.visible, visible);
}

void verifyActualBoxPosition(WidgetTester tester, Finder finder, int index, Rect ideal) {
21
  final RenderBox box = tester.renderObjectList<RenderBox>(finder).elementAt(index);
22
  final Rect rect = Rect.fromPoints(box.localToGlobal(Offset.zero), box.localToGlobal(box.size.bottomRight(Offset.zero)));
Ian Hickson's avatar
Ian Hickson committed
23 24 25 26
  expect(rect, equals(ideal));
}

void main() {
27
  testWidgetsWithLeakTracking('Sliver appbars - pinned', (WidgetTester tester) async {
Ian Hickson's avatar
Ian Hickson committed
28 29 30
    const double bigHeight = 550.0;
    GlobalKey key1, key2, key3, key4, key5;
    await tester.pumpWidget(
31
      Directionality(
32
        textDirection: TextDirection.ltr,
33
        child: CustomScrollView(
34
          slivers: <Widget>[
35 36 37 38 39
            BigSliver(key: key1 = GlobalKey(), height: bigHeight),
            SliverPersistentHeader(key: key2 = GlobalKey(), delegate: TestDelegate(), pinned: true),
            SliverPersistentHeader(key: key3 = GlobalKey(), delegate: TestDelegate(), pinned: true),
            BigSliver(key: key4 = GlobalKey(), height: bigHeight),
            BigSliver(key: key5 = GlobalKey(), height: bigHeight),
40 41
          ],
        ),
Ian Hickson's avatar
Ian Hickson committed
42 43
      ),
    );
44
    final ScrollPosition position = tester.state<ScrollableState>(find.byType(Scrollable)).position;
45
    final double max = bigHeight * 3.0 + TestDelegate().maxExtent * 2.0 - 600.0; // 600 is the height of the test viewport
Ian Hickson's avatar
Ian Hickson committed
46 47 48 49 50
    assert(max < 10000.0);
    expect(max, 1450.0);
    expect(position.pixels, 0.0);
    expect(position.minScrollExtent, 0.0);
    expect(position.maxScrollExtent, max);
51
    position.animateTo(10000.0, curve: Curves.linear, duration: const Duration(minutes: 1));
52
    await tester.pumpAndSettle(const Duration(milliseconds: 10));
Ian Hickson's avatar
Ian Hickson committed
53 54 55
    expect(position.pixels, max);
    expect(position.minScrollExtent, 0.0);
    expect(position.maxScrollExtent, max);
56 57
    verifyPaintPosition(key1, Offset.zero, false);
    verifyPaintPosition(key2, Offset.zero, true);
58
    verifyPaintPosition(key3, const Offset(0.0, 100.0), true);
59
    verifyPaintPosition(key4, Offset.zero, true);
60
    verifyPaintPosition(key5, const Offset(0.0, 50.0), true);
Ian Hickson's avatar
Ian Hickson committed
61 62
  });

63
  testWidgetsWithLeakTracking('Sliver appbars - toStringDeep of maxExtent that throws', (WidgetTester tester) async {
64
    final TestDelegateThatCanThrow delegateThatCanThrow = TestDelegateThatCanThrow();
65 66
    GlobalKey key;
    await tester.pumpWidget(
67
      Directionality(
68
        textDirection: TextDirection.ltr,
69
        child: CustomScrollView(
70
          slivers: <Widget>[
71
            SliverPersistentHeader(key: key = GlobalKey(), delegate: delegateThatCanThrow, pinned: true),
72 73
          ],
        ),
74 75 76 77
      ),
    );
    await tester.pumpAndSettle(const Duration(milliseconds: 10));

78
    final RenderObject renderObject = key.currentContext!.findRenderObject()!;
79 80 81 82 83 84 85
    // The delegate must only start throwing immediately before calling
    // toStringDeep to avoid triggering spurious exceptions.
    // If the _RenderSliverPinnedPersistentHeaderForWidgets class was not
    // private it would make more sense to create an instance of it directly.
    delegateThatCanThrow.shouldThrow = true;
    expect(renderObject, hasAGoodToStringDeep);
    expect(
86
      renderObject.toStringDeep(minLevel: DiagnosticLevel.info),
87 88 89 90 91 92
      equalsIgnoringHashCodes(
        '_RenderSliverPinnedPersistentHeaderForWidgets#00000 relayoutBoundary=up1\n'
        ' │ parentData: paintOffset=Offset(0.0, 0.0) (can use size)\n'
        ' │ constraints: SliverConstraints(AxisDirection.down,\n'
        ' │   GrowthDirection.forward, ScrollDirection.idle, scrollOffset:\n'
        ' │   0.0, remainingPaintExtent: 600.0, crossAxisExtent: 800.0,\n'
93
        ' │   crossAxisDirection: AxisDirection.right,\n'
94 95
        ' │   viewportMainAxisExtent: 600.0, remainingCacheExtent: 850.0,\n'
        ' │   cacheOrigin: 0.0)\n'
96
        ' │ geometry: SliverGeometry(scrollExtent: 200.0, paintExtent: 200.0,\n'
97 98
        ' │   maxPaintExtent: 200.0, hasVisualOverflow: true, cacheExtent:\n'
        ' │   200.0)\n'
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        ' │ maxExtent: EXCEPTION (FlutterError)\n'
        ' │ child position: 0.0\n'
        ' │\n'
        ' └─child: RenderConstrainedBox#00000 relayoutBoundary=up2\n'
        '   │ parentData: <none> (can use size)\n'
        '   │ constraints: BoxConstraints(w=800.0, 0.0<=h<=200.0)\n'
        '   │ size: Size(800.0, 200.0)\n'
        '   │ additionalConstraints: BoxConstraints(0.0<=w<=Infinity,\n'
        '   │   100.0<=h<=200.0)\n'
        '   │\n'
        '   └─child: RenderLimitedBox#00000 relayoutBoundary=up3\n'
        '     │ parentData: <none> (can use size)\n'
        '     │ constraints: BoxConstraints(w=800.0, 100.0<=h<=200.0)\n'
        '     │ size: Size(800.0, 200.0)\n'
        '     │ maxWidth: 0.0\n'
        '     │ maxHeight: 0.0\n'
        '     │\n'
        '     └─child: RenderConstrainedBox#00000 relayoutBoundary=up4\n'
        '         parentData: <none> (can use size)\n'
        '         constraints: BoxConstraints(w=800.0, 100.0<=h<=200.0)\n'
        '         size: Size(800.0, 200.0)\n'
120
        '         additionalConstraints: BoxConstraints(biggest)\n',
121 122 123 124
      ),
    );
  });

125
  testWidgetsWithLeakTracking('Sliver appbars - pinned with slow scroll', (WidgetTester tester) async {
Ian Hickson's avatar
Ian Hickson committed
126 127 128
    const double bigHeight = 550.0;
    GlobalKey key1, key2, key3, key4, key5;
    await tester.pumpWidget(
129
      Directionality(
130
        textDirection: TextDirection.ltr,
131
        child: CustomScrollView(
132
          slivers: <Widget>[
133 134 135 136 137
            BigSliver(key: key1 = GlobalKey(), height: bigHeight),
            SliverPersistentHeader(key: key2 = GlobalKey(), delegate: TestDelegate(), pinned: true),
            SliverPersistentHeader(key: key3 = GlobalKey(), delegate: TestDelegate(), pinned: true),
            BigSliver(key: key4 = GlobalKey(), height: bigHeight),
            BigSliver(key: key5 = GlobalKey(), height: bigHeight),
138 139
          ],
        ),
Ian Hickson's avatar
Ian Hickson committed
140 141
      ),
    );
142

143
    final ScrollPosition position = tester.state<ScrollableState>(find.byType(Scrollable)).position;
144
    verifyPaintPosition(key1, Offset.zero, true);
145
    verifyPaintPosition(key2, const Offset(0.0, 550.0), true);
146 147 148
    verifyPaintPosition(key3, const Offset(0.0, 750.0), false);
    verifyPaintPosition(key4, const Offset(0.0, 950.0), false);
    verifyPaintPosition(key5, const Offset(0.0, 1500.0), false);
149
    position.animateTo(550.0, curve: Curves.linear, duration: const Duration(minutes: 1));
150
    await tester.pumpAndSettle();
151 152
    verifyPaintPosition(key1, Offset.zero, false);
    verifyPaintPosition(key2, Offset.zero, true);
153 154
    verifyPaintPosition(key3, const Offset(0.0, 200.0), true);
    verifyPaintPosition(key4, const Offset(0.0, 400.0), true);
155
    verifyPaintPosition(key5, const Offset(0.0, 950.0), false);
156
    position.animateTo(600.0, curve: Curves.linear, duration: const Duration(minutes: 1));
157
    await tester.pumpAndSettle(const Duration(milliseconds: 200));
158 159
    verifyPaintPosition(key1, Offset.zero, false);
    verifyPaintPosition(key2, Offset.zero, true);
160 161
    verifyPaintPosition(key3, const Offset(0.0, 150.0), true);
    verifyPaintPosition(key4, const Offset(0.0, 350.0), true);
162
    verifyPaintPosition(key5, const Offset(0.0, 900.0), false);
163
    position.animateTo(650.0, curve: Curves.linear, duration: const Duration(minutes: 1));
164
    await tester.pumpAndSettle(const Duration(milliseconds: 300));
165 166
    verifyPaintPosition(key1, Offset.zero, false);
    verifyPaintPosition(key2, Offset.zero, true);
167
    verifyPaintPosition(key3, const Offset(0.0, 100.0), true);
Dan Field's avatar
Dan Field committed
168
    verifyActualBoxPosition(tester, find.byType(Container), 1, const Rect.fromLTWH(0.0, 100.0, 800.0, 200.0));
169
    verifyPaintPosition(key4, const Offset(0.0, 300.0), true);
170
    verifyPaintPosition(key5, const Offset(0.0, 850.0), false);
171
    position.animateTo(700.0, curve: Curves.linear, duration: const Duration(minutes: 1));
172
    await tester.pumpAndSettle(const Duration(milliseconds: 400));
173 174
    verifyPaintPosition(key1, Offset.zero, false);
    verifyPaintPosition(key2, Offset.zero, true);
175
    verifyPaintPosition(key3, const Offset(0.0, 100.0), true);
Dan Field's avatar
Dan Field committed
176
    verifyActualBoxPosition(tester, find.byType(Container), 1, const Rect.fromLTWH(0.0, 100.0, 800.0, 200.0));
177
    verifyPaintPosition(key4, const Offset(0.0, 250.0), true);
178
    verifyPaintPosition(key5, const Offset(0.0, 800.0), false);
179
    position.animateTo(750.0, curve: Curves.linear, duration: const Duration(minutes: 1));
180
    await tester.pumpAndSettle(const Duration(milliseconds: 500));
181 182
    verifyPaintPosition(key1, Offset.zero, false);
    verifyPaintPosition(key2, Offset.zero, true);
183
    verifyPaintPosition(key3, const Offset(0.0, 100.0), true);
Dan Field's avatar
Dan Field committed
184
    verifyActualBoxPosition(tester, find.byType(Container), 1, const Rect.fromLTWH(0.0, 100.0, 800.0, 200.0));
185
    verifyPaintPosition(key4, const Offset(0.0, 200.0), true);
186
    verifyPaintPosition(key5, const Offset(0.0, 750.0), false);
187
    position.animateTo(800.0, curve: Curves.linear, duration: const Duration(minutes: 1));
188
    await tester.pumpAndSettle(const Duration(milliseconds: 60));
189 190
    verifyPaintPosition(key1, Offset.zero, false);
    verifyPaintPosition(key2, Offset.zero, true);
191
    verifyPaintPosition(key3, const Offset(0.0, 100.0), true);
192
    verifyPaintPosition(key4, const Offset(0.0, 150.0), true);
193
    verifyPaintPosition(key5, const Offset(0.0, 700.0), false);
194
    position.animateTo(850.0, curve: Curves.linear, duration: const Duration(minutes: 1));
195
    await tester.pumpAndSettle(const Duration(milliseconds: 70));
196 197
    verifyPaintPosition(key1, Offset.zero, false);
    verifyPaintPosition(key2, Offset.zero, true);
198
    verifyPaintPosition(key3, const Offset(0.0, 100.0), true);
199
    verifyPaintPosition(key4, const Offset(0.0, 100.0), true);
200
    verifyPaintPosition(key5, const Offset(0.0, 650.0), false);
201
    position.animateTo(900.0, curve: Curves.linear, duration: const Duration(minutes: 1));
202
    await tester.pumpAndSettle(const Duration(milliseconds: 80));
203 204
    verifyPaintPosition(key1, Offset.zero, false);
    verifyPaintPosition(key2, Offset.zero, true);
205
    verifyPaintPosition(key3, const Offset(0.0, 100.0), true);
206 207
    verifyPaintPosition(key4, const Offset(0.0, 50.0), true);
    verifyPaintPosition(key5, const Offset(0.0, 600.0), false);
208
    position.animateTo(950.0, curve: Curves.linear, duration: const Duration(minutes: 1));
209
    await tester.pumpAndSettle(const Duration(milliseconds: 90));
210 211
    verifyPaintPosition(key1, Offset.zero, false);
    verifyPaintPosition(key2, Offset.zero, true);
212
    verifyPaintPosition(key3, const Offset(0.0, 100.0), true);
Dan Field's avatar
Dan Field committed
213
    verifyActualBoxPosition(tester, find.byType(Container), 1, const Rect.fromLTWH(0.0, 100.0, 800.0, 100.0));
214
    verifyPaintPosition(key4, Offset.zero, true);
215
    verifyPaintPosition(key5, const Offset(0.0, 550.0), true);
Ian Hickson's avatar
Ian Hickson committed
216 217
  });

218
  testWidgetsWithLeakTracking('Sliver appbars - pinned with less overlap', (WidgetTester tester) async {
Ian Hickson's avatar
Ian Hickson committed
219 220 221
    const double bigHeight = 650.0;
    GlobalKey key1, key2, key3, key4, key5;
    await tester.pumpWidget(
222
      Directionality(
223
        textDirection: TextDirection.ltr,
224
        child: CustomScrollView(
225
          slivers: <Widget>[
226 227 228 229 230
            BigSliver(key: key1 = GlobalKey(), height: bigHeight),
            SliverPersistentHeader(key: key2 = GlobalKey(), delegate: TestDelegate(), pinned: true),
            SliverPersistentHeader(key: key3 = GlobalKey(), delegate: TestDelegate(), pinned: true),
            BigSliver(key: key4 = GlobalKey(), height: bigHeight),
            BigSliver(key: key5 = GlobalKey(), height: bigHeight),
231 232
          ],
        ),
Ian Hickson's avatar
Ian Hickson committed
233 234
      ),
    );
235
    final ScrollPosition position = tester.state<ScrollableState>(find.byType(Scrollable)).position;
236
    final double max = bigHeight * 3.0 + TestDelegate().maxExtent * 2.0 - 600.0; // 600 is the height of the test viewport
Ian Hickson's avatar
Ian Hickson committed
237 238 239 240 241
    assert(max < 10000.0);
    expect(max, 1750.0);
    expect(position.pixels, 0.0);
    expect(position.minScrollExtent, 0.0);
    expect(position.maxScrollExtent, max);
242
    position.animateTo(10000.0, curve: Curves.linear, duration: const Duration(minutes: 1));
243
    await tester.pumpAndSettle(const Duration(milliseconds: 10));
Ian Hickson's avatar
Ian Hickson committed
244 245 246
    expect(position.pixels, max);
    expect(position.minScrollExtent, 0.0);
    expect(position.maxScrollExtent, max);
247 248
    verifyPaintPosition(key1, Offset.zero, false);
    verifyPaintPosition(key2, Offset.zero, true);
249
    verifyPaintPosition(key3, const Offset(0.0, 100.0), true);
250 251
    verifyPaintPosition(key4, Offset.zero, false);
    verifyPaintPosition(key5, Offset.zero, true);
Ian Hickson's avatar
Ian Hickson committed
252
  });
253

254
  testWidgetsWithLeakTracking('Sliver appbars - overscroll gap is below header', (WidgetTester tester) async {
255
    await tester.pumpWidget(
256
      Directionality(
257
        textDirection: TextDirection.ltr,
258
        child: CustomScrollView(
259 260
          physics: const BouncingScrollPhysics(),
          slivers: <Widget>[
261
            SliverPersistentHeader(delegate: TestDelegate(), pinned: true),
262
            SliverList(
263
              delegate: SliverChildListDelegate(<Widget>[
264
                const SizedBox(
265
                  height: 300.0,
266
                  child: Text('X'),
267 268 269 270 271
                ),
              ]),
            ),
          ],
        ),
272 273 274
      ),
    );

275 276
    expect(tester.getTopLeft(find.byType(Container)), Offset.zero);
    expect(tester.getTopLeft(find.text('X')), const Offset(0.0, 200.0));
277

278
    final ScrollPosition position = tester.state<ScrollableState>(find.byType(Scrollable)).position;
279 280 281
    position.jumpTo(-50.0);
    await tester.pump();

282 283
    expect(tester.getTopLeft(find.byType(Container)), Offset.zero);
    expect(tester.getTopLeft(find.text('X')), const Offset(0.0, 250.0));
284 285 286 287

    position.jumpTo(50.0);
    await tester.pump();

288 289
    expect(tester.getTopLeft(find.byType(Container)), Offset.zero);
    expect(tester.getTopLeft(find.text('X')), const Offset(0.0, 150.0));
290 291 292 293

    position.jumpTo(150.0);
    await tester.pump();

294 295
    expect(tester.getTopLeft(find.byType(Container)), Offset.zero);
    expect(tester.getTopLeft(find.text('X')), const Offset(0.0, 50.0));
296
  });
Ian Hickson's avatar
Ian Hickson committed
297 298
}

299
class TestDelegate extends SliverPersistentHeaderDelegate {
Ian Hickson's avatar
Ian Hickson committed
300 301 302 303
  @override
  double get maxExtent => 200.0;

  @override
304 305
  double get minExtent => 100.0;

306 307
  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
308
    return Container(constraints: BoxConstraints(minHeight: minExtent, maxHeight: maxExtent));
309 310 311 312 313 314 315 316 317 318 319
  }

  @override
  bool shouldRebuild(TestDelegate oldDelegate) => false;
}

class TestDelegateThatCanThrow extends SliverPersistentHeaderDelegate {
  bool shouldThrow = false;

  @override
  double get maxExtent {
320
    return shouldThrow ? throw FlutterError('Unavailable maxExtent') : 200.0;
321 322 323 324
  }

  @override
  double get minExtent {
325
    return shouldThrow ? throw FlutterError('Unavailable minExtent') : 100.0;
326 327
  }

328 329
  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
330
    return Container(constraints: BoxConstraints(minHeight: minExtent, maxHeight: maxExtent));
Ian Hickson's avatar
Ian Hickson committed
331 332 333 334 335 336 337 338 339 340 341 342 343
  }

  @override
  bool shouldRebuild(TestDelegate oldDelegate) => false;
}


class RenderBigSliver extends RenderSliver {
  RenderBigSliver(double height) : _height = height;

  double get height => _height;
  double _height;
  set height(double value) {
344
    if (value == _height) {
Ian Hickson's avatar
Ian Hickson committed
345
      return;
346
    }
Ian Hickson's avatar
Ian Hickson committed
347 348 349 350
    _height = value;
    markNeedsLayout();
  }

351
  double get paintExtent => (height - constraints.scrollOffset).clamp(0.0, constraints.remainingPaintExtent);
Ian Hickson's avatar
Ian Hickson committed
352 353 354

  @override
  void performLayout() {
355
    geometry = SliverGeometry(
Ian Hickson's avatar
Ian Hickson committed
356 357 358 359 360 361 362 363
      scrollExtent: height,
      paintExtent: paintExtent,
      maxPaintExtent: height,
    );
  }
}

class BigSliver extends LeafRenderObjectWidget {
364
  const BigSliver({ super.key, required this.height });
Ian Hickson's avatar
Ian Hickson committed
365 366 367 368 369

  final double height;

  @override
  RenderBigSliver createRenderObject(BuildContext context) {
370
    return RenderBigSliver(height);
Ian Hickson's avatar
Ian Hickson committed
371 372 373 374 375 376 377
  }

  @override
  void updateRenderObject(BuildContext context, RenderBigSliver renderObject) {
    renderObject.height = height;
  }
}