slivers_evil_test.dart 11 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
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/material.dart';
6 7
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
8

9 10
class TestSliverPersistentHeaderDelegate extends SliverPersistentHeaderDelegate {
  TestSliverPersistentHeaderDelegate(this._maxExtent);
11 12 13 14 15 16 17

  final double _maxExtent;

  @override
  double get maxExtent => _maxExtent;

  @override
18 19 20 21
  double get minExtent => 16.0;

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
22
    return Column(
23
      children: <Widget>[
24 25
        Container(height: minExtent),
        Expanded(child: Container()),
26 27 28 29 30
      ],
    );
  }

  @override
31
  bool shouldRebuild(TestSliverPersistentHeaderDelegate oldDelegate) => false;
32 33
}

Adam Barth's avatar
Adam Barth committed
34
class TestBehavior extends ScrollBehavior {
35 36
  const TestBehavior();

37
  @override
38
  Widget buildOverscrollIndicator(BuildContext context, Widget child, ScrollableDetails details) {
39
    return GlowingOverscrollIndicator(
40
      axisDirection: details.direction,
41
      color: const Color(0xFFFFFFFF),
42
      child: child,
43 44
    );
  }
45 46 47
}

class TestScrollPhysics extends ClampingScrollPhysics {
48
  const TestScrollPhysics({ super.parent });
49 50

  @override
51
  TestScrollPhysics applyTo(ScrollPhysics? ancestor) {
52
    return TestScrollPhysics(parent: parent?.applyTo(ancestor) ?? ancestor);
53
  }
54 55

  @override
56
  Tolerance toleranceFor(ScrollMetrics metrics) => const Tolerance(velocity: 20.0, distance: 1.0);
57 58 59 60
}

void main() {
  testWidgets('Evil test of sliver features - 1', (WidgetTester tester) async {
61
    final GlobalKey centerKey = GlobalKey();
62 63 64 65 66 67
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(),
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: ScrollConfiguration(
68
            behavior: const TestBehavior(),
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
            child: Scrollbar(
              child: Scrollable(
                physics: const TestScrollPhysics(),
                viewportBuilder: (BuildContext context, ViewportOffset offset) {
                  return Viewport(
                    anchor: 0.25,
                    offset: offset,
                    center: centerKey,
                    slivers: <Widget>[
                      SliverToBoxAdapter(child: Container(height: 5.0)),
                      SliverToBoxAdapter(child: Container(height: 520.0)),
                      SliverToBoxAdapter(child: Container(height: 520.0)),
                      SliverToBoxAdapter(child: Container(height: 520.0)),
                      SliverPersistentHeader(delegate: TestSliverPersistentHeaderDelegate(150.0), pinned: true),
                      SliverToBoxAdapter(child: Container(height: 520.0)),
                      SliverPadding(
                        padding: const EdgeInsets.all(50.0),
                        sliver: SliverToBoxAdapter(child: Container(height: 520.0)),
                      ),
                      SliverToBoxAdapter(child: Container(height: 520.0)),
                      SliverPersistentHeader(delegate: TestSliverPersistentHeaderDelegate(150.0), floating: true),
                      SliverToBoxAdapter(child: Container(height: 520.0)),
                      SliverToBoxAdapter(key: centerKey, child: Container(height: 520.0)), // ------------------------ CENTER ------------------------
                      SliverPersistentHeader(delegate: TestSliverPersistentHeaderDelegate(150.0), pinned: true),
                      SliverToBoxAdapter(child: Container(height: 520.0)),
                      SliverToBoxAdapter(child: Container(height: 520.0)),
                      SliverToBoxAdapter(child: Container(height: 520.0)),
                      SliverPadding(
                        padding: const EdgeInsets.all(50.0),
                        sliver: SliverPersistentHeader(delegate: TestSliverPersistentHeaderDelegate(250.0), pinned: true),
                      ),
                      SliverToBoxAdapter(child: Container(height: 520.0)),
                      SliverPersistentHeader(delegate: TestSliverPersistentHeaderDelegate(250.0), pinned: true),
                      SliverToBoxAdapter(child: Container(height: 5.0)),
                      SliverPersistentHeader(delegate: TestSliverPersistentHeaderDelegate(250.0), pinned: true),
                      SliverToBoxAdapter(child: Container(height: 5.0)),
                      SliverPersistentHeader(delegate: TestSliverPersistentHeaderDelegate(250.0), pinned: true),
                      SliverPersistentHeader(delegate: TestSliverPersistentHeaderDelegate(250.0), pinned: true),
                      SliverToBoxAdapter(child: Container(height: 5.0)),
                      SliverPersistentHeader(delegate: TestSliverPersistentHeaderDelegate(250.0), pinned: true),
                      SliverPersistentHeader(delegate: TestSliverPersistentHeaderDelegate(250.0)),
                      SliverToBoxAdapter(child: Container(height: 520.0)),
                      SliverPersistentHeader(delegate: TestSliverPersistentHeaderDelegate(150.0), floating: true),
                      SliverToBoxAdapter(child: Container(height: 520.0)),
                      SliverPersistentHeader(delegate: TestSliverPersistentHeaderDelegate(150.0), floating: true),
                      SliverToBoxAdapter(child: Container(height: 5.0)),
                      SliverList(
                        delegate: SliverChildListDelegate(<Widget>[
                          Container(height: 50.0),
                          Container(height: 50.0),
                          Container(height: 50.0),
                          Container(height: 50.0),
                          Container(height: 50.0),
                          Container(height: 50.0),
                          Container(height: 50.0),
                          Container(height: 50.0),
                          Container(height: 50.0),
                          Container(height: 50.0),
                          Container(height: 50.0),
                          Container(height: 50.0),
                          Container(height: 50.0),
                          Container(height: 50.0),
                          Container(height: 50.0),
                        ]),
                      ),
                      SliverPersistentHeader(delegate: TestSliverPersistentHeaderDelegate(250.0)),
                      SliverPersistentHeader(delegate: TestSliverPersistentHeaderDelegate(250.0)),
                      SliverPersistentHeader(delegate: TestSliverPersistentHeaderDelegate(250.0)),
                      SliverPadding(
                        padding: const EdgeInsets.symmetric(horizontal: 50.0),
                        sliver: SliverToBoxAdapter(child: Container(height: 520.0)),
                      ),
                      SliverToBoxAdapter(child: Container(height: 520.0)),
                      SliverToBoxAdapter(child: Container(height: 520.0)),
                      SliverToBoxAdapter(child: Container(height: 5.0)),
                    ],
                  );
                },
              ),
            ),
149
          ),
150 151
        ),
      ),
152
    );
153
    final ScrollPosition position = tester.state<ScrollableState>(find.byType(Scrollable)).position;
154

155
    position.animateTo(10000.0, curve: Curves.linear, duration: const Duration(minutes: 1));
156 157 158
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 50));
159
    await tester.pumpAndSettle(const Duration(milliseconds: 122));
160

161
    position.animateTo(-10000.0, curve: Curves.linear, duration: const Duration(minutes: 1));
162 163 164
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 50));
165
    await tester.pumpAndSettle(const Duration(milliseconds: 122));
166

167
    position.animateTo(10000.0, curve: Curves.linear, duration: const Duration(minutes: 1));
168 169 170
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 50));
171
    await tester.pumpAndSettle(const Duration(milliseconds: 122));
172

173
    position.animateTo(-10000.0, curve: Curves.linear, duration: const Duration(seconds: 1));
174 175 176
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 50));
177
    await tester.pumpAndSettle(const Duration(milliseconds: 122));
178

179
    position.animateTo(10000.0, curve: Curves.linear, duration: const Duration(seconds: 1));
180 181 182
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 50));
183
    await tester.pumpAndSettle(const Duration(milliseconds: 122));
184 185

  });
186 187

  testWidgets('Removing offscreen items above and rescrolling does not crash', (WidgetTester tester) async {
188 189
    await tester.pumpWidget(MaterialApp(
      home: CustomScrollView(
190
        cacheExtent: 0.0,
191
        slivers: <Widget>[
192
          SliverFixedExtentList(
193
            itemExtent: 100.0,
194
            delegate: SliverChildBuilderDelegate(
195
              (BuildContext context, int index) {
196
                return ColoredBox(
197
                  color: Colors.blue,
198
                  child: Text(index.toString()),
199 200 201 202 203 204 205 206 207 208 209 210 211
                );
              },
              childCount: 30,
            ),
          ),
        ],
      ),
    ));

    await tester.drag(find.text('5'), const Offset(0.0, -500.0));
    await tester.pump();

    // Screen is 600px high. Moved bottom item 500px up. It's now at the top.
212 213
    expect(tester.getTopLeft(find.widgetWithText(ColoredBox, '5')).dy, 0.0);
    expect(tester.getBottomLeft(find.widgetWithText(ColoredBox, '10')).dy, 600.0);
214 215

    // Stop returning the first 3 items.
216 217
    await tester.pumpWidget(MaterialApp(
      home: CustomScrollView(
218
        cacheExtent: 0.0,
219
        slivers: <Widget>[
220
          SliverFixedExtentList(
221
            itemExtent: 100.0,
222
            delegate: SliverChildBuilderDelegate(
223 224
              (BuildContext context, int index) {
                if (index > 3) {
225
                  return ColoredBox(
226
                    color: Colors.blue,
227
                    child: Text(index.toString()),
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
                  );
                }
                return null;
              },
              childCount: 30,
            ),
          ),
        ],
      ),
    ));

    await tester.drag(find.text('5'), const Offset(0.0, 400.0));
    await tester.pump();

    // Move up by 4 items, meaning item 1 would have been at the top but
    // 0 through 3 no longer exist, so item 4, 3 items down, is the first one.
    // Item 4 is also shifted to the top.
245
    expect(tester.getTopLeft(find.widgetWithText(ColoredBox, '4')).dy, 0.0);
246 247 248

    // Because the screen is still 600px, item 9 is now visible at the bottom instead
    // of what's supposed to be item 6 had we not re-shifted.
249
    expect(tester.getBottomLeft(find.widgetWithText(ColoredBox, '9')).dy, 600.0);
250
  });
251
}