slivers_evil_test.dart 11.7 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/physics.dart';
import 'package:flutter/material.dart';

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

  final double _maxExtent;

  @override
  double get maxExtent => _maxExtent;

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

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

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

Adam Barth's avatar
Adam Barth committed
35
class TestBehavior extends ScrollBehavior {
36
  @override
37
  Widget buildViewportChrome(BuildContext context, Widget child, AxisDirection axisDirection) {
38
    return GlowingOverscrollIndicator(
39 40 41 42 43
      child: child,
      axisDirection: axisDirection,
      color: const Color(0xFFFFFFFF),
    );
  }
44 45 46
}

class TestScrollPhysics extends ClampingScrollPhysics {
47
  const TestScrollPhysics({ ScrollPhysics? parent }) : super(parent: parent);
48 49

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

  @override
55
  Tolerance get tolerance => const Tolerance(velocity: 20.0, distance: 1.0);
56 57
}

58
class TestViewportScrollPosition extends ScrollPositionWithSingleContext {
59
  TestViewportScrollPosition({
60 61 62
    required ScrollPhysics physics,
    required ScrollContext context,
    ScrollPosition? oldPosition,
63
  }) : super(physics: physics, context: context, oldPosition: oldPosition);
64 65 66 67 68 69 70 71 72 73 74

  @override
  bool applyContentDimensions(double minScrollExtent, double maxScrollExtent) {
    expect(minScrollExtent, moreOrLessEquals(-3895.0));
    expect(maxScrollExtent, moreOrLessEquals(8575.0));
    return super.applyContentDimensions(minScrollExtent, maxScrollExtent);
  }
}

void main() {
  testWidgets('Evil test of sliver features - 1', (WidgetTester tester) async {
75
    final GlobalKey centerKey = GlobalKey();
76 77 78 79 80 81 82 83 84
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(),
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: ScrollConfiguration(
            behavior: TestBehavior(),
            child: Scrollbar(
              child: Scrollable(
85
                axisDirection: AxisDirection.down,
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
                physics: const TestScrollPhysics(),
                viewportBuilder: (BuildContext context, ViewportOffset offset) {
                  return Viewport(
                    axisDirection: AxisDirection.down,
                    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)),
                    ],
                  );
                },
              ),
            ),
165
          ),
166 167
        ),
      ),
168
    );
169
    final ScrollPosition position = tester.state<ScrollableState>(find.byType(Scrollable)).position;
170

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

177
    position.animateTo(-10000.0, curve: Curves.linear, duration: const Duration(minutes: 1));
178 179 180
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 50));
181
    await tester.pumpAndSettle(const Duration(milliseconds: 122));
182

183
    position.animateTo(10000.0, curve: Curves.linear, duration: const Duration(minutes: 1));
184 185 186
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 50));
187
    await tester.pumpAndSettle(const Duration(milliseconds: 122));
188

189
    position.animateTo(-10000.0, curve: Curves.linear, duration: const Duration(seconds: 1));
190 191 192
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 50));
193
    await tester.pumpAndSettle(const Duration(milliseconds: 122));
194

195
    position.animateTo(10000.0, curve: Curves.linear, duration: const Duration(seconds: 1));
196 197 198
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 50));
199
    await tester.pumpAndSettle(const Duration(milliseconds: 122));
200 201

  });
202 203

  testWidgets('Removing offscreen items above and rescrolling does not crash', (WidgetTester tester) async {
204 205
    await tester.pumpWidget(MaterialApp(
      home: CustomScrollView(
206
        cacheExtent: 0.0,
207
        slivers: <Widget>[
208
          SliverFixedExtentList(
209
            itemExtent: 100.0,
210
            delegate: SliverChildBuilderDelegate(
211
              (BuildContext context, int index) {
212
                return Container(
213
                  color: Colors.blue,
214
                  child: Text(index.toString()),
215 216 217 218 219 220 221 222 223 224 225 226 227
                );
              },
              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.
228 229
    expect(tester.getTopLeft(find.widgetWithText(Container, '5')).dy, 0.0);
    expect(tester.getBottomLeft(find.widgetWithText(Container, '10')).dy, 600.0);
230 231

    // Stop returning the first 3 items.
232 233
    await tester.pumpWidget(MaterialApp(
      home: CustomScrollView(
234
        cacheExtent: 0.0,
235
        slivers: <Widget>[
236
          SliverFixedExtentList(
237
            itemExtent: 100.0,
238
            delegate: SliverChildBuilderDelegate(
239 240
              (BuildContext context, int index) {
                if (index > 3) {
241
                  return Container(
242
                    color: Colors.blue,
243
                    child: Text(index.toString()),
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
                  );
                }
                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.
261
    expect(tester.getTopLeft(find.widgetWithText(Container, '4')).dy, 0.0);
262 263 264

    // 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.
265
    expect(tester.getBottomLeft(find.widgetWithText(Container, '9')).dy, 600.0);
266
  });
267
}