slivers_evil_test.dart 9.47 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2016 The Chromium 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_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 24
    return new Column(
      children: <Widget>[
25
        new Container(height: minExtent),
26 27 28 29 30 31
        new Expanded(child: new Container()),
      ],
    );
  }

  @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 39 40 41 42 43
    return new GlowingOverscrollIndicator(
      child: child,
      axisDirection: axisDirection,
      color: const Color(0xFFFFFFFF),
    );
  }
44 45 46 47
}

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

  @override
50 51 52
  TestScrollPhysics applyTo(ScrollPhysics ancestor) {
    return new TestScrollPhysics(parent: parent?.applyTo(ancestor) ?? ancestor);
  }
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
    ScrollPhysics physics,
61
    ScrollContext context,
62
    ScrollPosition oldPosition,
63
  }) : super(physics: physics, context: context, oldPosition: oldPosition);
64 65 66 67 68 69 70 71 72 73 74 75

  @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 {
    final GlobalKey centerKey = new GlobalKey();
76 77 78
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new ScrollConfiguration(
79
        behavior: new TestBehavior(),
80
        child: new Scrollbar(
Adam Barth's avatar
Adam Barth committed
81
          child: new Scrollable(
82 83
            axisDirection: AxisDirection.down,
            physics: const TestScrollPhysics(),
84
            viewportBuilder: (BuildContext context, ViewportOffset offset) {
Adam Barth's avatar
Adam Barth committed
85
              return new Viewport(
86 87 88 89 90 91 92 93 94 95 96 97
                axisDirection: AxisDirection.down,
                anchor: 0.25,
                offset: offset,
                center: centerKey,
                slivers: <Widget>[
                  new SliverToBoxAdapter(child: new Container(height: 5.0)),
                  new SliverToBoxAdapter(child: new Container(height: 520.0)),
                  new SliverToBoxAdapter(child: new Container(height: 520.0)),
                  new SliverToBoxAdapter(child: new Container(height: 520.0)),
                  new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(150.0), pinned: true),
                  new SliverToBoxAdapter(child: new Container(height: 520.0)),
                  new SliverPadding(
98
                    padding: const EdgeInsets.all(50.0),
99
                    sliver: new SliverToBoxAdapter(child: new Container(height: 520.0)),
100 101 102 103 104 105 106 107 108 109
                  ),
                  new SliverToBoxAdapter(child: new Container(height: 520.0)),
                  new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(150.0), floating: true),
                  new SliverToBoxAdapter(child: new Container(height: 520.0)),
                  new SliverToBoxAdapter(key: centerKey, child: new Container(height: 520.0)), // ------------------------ CENTER ------------------------
                  new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(150.0), pinned: true),
                  new SliverToBoxAdapter(child: new Container(height: 520.0)),
                  new SliverToBoxAdapter(child: new Container(height: 520.0)),
                  new SliverToBoxAdapter(child: new Container(height: 520.0)),
                  new SliverPadding(
110
                    padding: const EdgeInsets.all(50.0),
111
                    sliver: new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0), pinned: true),
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
                  ),
                  new SliverToBoxAdapter(child: new Container(height: 520.0)),
                  new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0), pinned: true),
                  new SliverToBoxAdapter(child: new Container(height: 5.0)),
                  new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0), pinned: true),
                  new SliverToBoxAdapter(child: new Container(height: 5.0)),
                  new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0), pinned: true),
                  new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0), pinned: true),
                  new SliverToBoxAdapter(child: new Container(height: 5.0)),
                  new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0), pinned: true),
                  new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0)),
                  new SliverToBoxAdapter(child: new Container(height: 520.0)),
                  new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(150.0), floating: true),
                  new SliverToBoxAdapter(child: new Container(height: 520.0)),
                  new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(150.0), floating: true),
                  new SliverToBoxAdapter(child: new Container(height: 5.0)),
                  new SliverList(
                    delegate: new SliverChildListDelegate(<Widget>[
                      new Container(height: 50.0),
                      new Container(height: 50.0),
                      new Container(height: 50.0),
                      new Container(height: 50.0),
                      new Container(height: 50.0),
                      new Container(height: 50.0),
                      new Container(height: 50.0),
                      new Container(height: 50.0),
                      new Container(height: 50.0),
                      new Container(height: 50.0),
                      new Container(height: 50.0),
                      new Container(height: 50.0),
                      new Container(height: 50.0),
                      new Container(height: 50.0),
                      new Container(height: 50.0),
                    ]),
                  ),
                  new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0)),
                  new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0)),
                  new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0)),
                  new SliverPadding(
151
                    padding: const EdgeInsets.symmetric(horizontal: 50.0),
152
                    sliver: new SliverToBoxAdapter(child: new Container(height: 520.0)),
153 154 155 156 157 158 159
                  ),
                  new SliverToBoxAdapter(child: new Container(height: 520.0)),
                  new SliverToBoxAdapter(child: new Container(height: 520.0)),
                  new SliverToBoxAdapter(child: new Container(height: 5.0)),
                ],
              );
            },
160
          ),
161 162
        ),
      ),
163
    ));
164
    final ScrollPosition position = tester.state<ScrollableState>(find.byType(Scrollable)).position;
165

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

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

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

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

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

  });
}