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

Adam Barth's avatar
Adam Barth committed
5
import 'package:flutter_test/flutter_test.dart';
6
import 'package:flutter/rendering.dart';
7
import 'package:flutter/widgets.dart';
Hixie's avatar
Hixie committed
8

9
const Key blockKey = Key('test');
Hixie's avatar
Hixie committed
10 11

void main() {
12 13
  testWidgets('Cannot scroll a non-overflowing block', (WidgetTester tester) async {
    await tester.pumpWidget(
14
      Directionality(
15
        textDirection: TextDirection.ltr,
16
        child: ListView(
17 18
          key: blockKey,
          children: <Widget>[
19
            Container(
20 21 22 23 24 25
              height: 200.0, // less than 600, the height of the test area
              child: const Text('Hello'),
            ),
          ],
        ),
      ),
26 27
    );

28 29
    final Offset middleOfContainer = tester.getCenter(find.text('Hello'));
    final Offset target = tester.getCenter(find.byKey(blockKey));
30
    final TestGesture gesture = await tester.startGesture(target);
31
    await gesture.moveBy(const Offset(0.0, -10.0));
32

33
    await tester.pump(const Duration(milliseconds: 1));
34 35 36

    expect(tester.getCenter(find.text('Hello')) == middleOfContainer, isTrue);

37
    await gesture.up();
Hixie's avatar
Hixie committed
38 39
  });

40 41
  testWidgets('Can scroll an overflowing block', (WidgetTester tester) async {
    await tester.pumpWidget(
42
      Directionality(
43
        textDirection: TextDirection.ltr,
44
        child: ListView(
45 46
          key: blockKey,
          children: <Widget>[
47
            Container(
48 49 50 51 52 53
              height: 2000.0, // more than 600, the height of the test area
              child: const Text('Hello'),
            ),
          ],
        ),
      ),
54
    );
55

56 57 58
    final Offset middleOfContainer = tester.getCenter(find.text('Hello'));
    expect(middleOfContainer.dx, equals(400.0));
    expect(middleOfContainer.dy, equals(1000.0));
59

60
    final Offset target = tester.getCenter(find.byKey(blockKey));
61
    final TestGesture gesture = await tester.startGesture(target);
62
    await gesture.moveBy(const Offset(0.0, -10.0));
63

64
    await tester.pump(); // redo layout
65

66
    expect(tester.getCenter(find.text('Hello')), isNot(equals(middleOfContainer)));
67

68
    await gesture.up();
Hixie's avatar
Hixie committed
69
  });
70

71
  testWidgets('ListView reverse', (WidgetTester tester) async {
72 73 74
    int first = 0;
    int second = 0;

75
    Widget buildBlock({ bool reverse = false }) {
76
      return Directionality(
77
        textDirection: TextDirection.ltr,
78 79
        child: ListView(
          key: UniqueKey(),
80 81
          reverse: reverse,
          children: <Widget>[
82
            GestureDetector(
83
              onTap: () { first += 1; },
84
              child: Container(
85 86
                height: 350.0, // more than half the height of the test area
                color: const Color(0xFF00FF00),
87
              ),
88
            ),
89
            GestureDetector(
90
              onTap: () { second += 1; },
91
              child: Container(
92 93 94 95 96 97
                height: 350.0, // more than half the height of the test area
                color: const Color(0xFF0000FF),
              ),
            ),
          ],
        ),
98 99
      );
    }
100

101
    await tester.pumpWidget(buildBlock(reverse: true));
102

103
    const Offset target = Offset(200.0, 200.0);
104
    await tester.tapAt(target);
105 106
    expect(first, equals(0));
    expect(second, equals(1));
107

108
    await tester.pumpWidget(buildBlock(reverse: false));
109

110
    await tester.tapAt(target);
111 112
    expect(first, equals(1));
    expect(second, equals(1));
113
  });
114

115
  testWidgets('ListView controller', (WidgetTester tester) async {
116
    final ScrollController controller = ScrollController();
117

118
    Widget buildBlock() {
119
      return Directionality(
120
        textDirection: TextDirection.ltr,
121
        child: ListView(
122
          controller: controller,
123
          children: const <Widget>[Text('A'), Text('B'), Text('C')],
124
        ),
125 126 127
      );
    }
    await tester.pumpWidget(buildBlock());
128
    expect(controller.offset, equals(0.0));
129
  });
130

131
  testWidgets('SliverBlockChildListDelegate.estimateMaxScrollOffset hits end', (WidgetTester tester) async {
132 133 134 135 136 137
    final SliverChildListDelegate delegate = SliverChildListDelegate(<Widget>[
      Container(),
      Container(),
      Container(),
      Container(),
      Container(),
138 139
    ]);

140
    await tester.pumpWidget(
141
      Directionality(
142
        textDirection: TextDirection.ltr,
143
        child: CustomScrollView(
144
          slivers: <Widget>[
145
            SliverList(
146 147 148
              delegate: delegate,
            ),
          ],
149
        ),
150 151
      ),
    );
152

153
    final SliverMultiBoxAdaptorElement element = tester.element(find.byType(SliverList, skipOffstage: false));
154 155 156 157 158 159

    final double maxScrollOffset = element.estimateMaxScrollOffset(
      null,
      firstIndex: 3,
      lastIndex: 4,
      leadingScrollOffset: 25.0,
160
      trailingScrollOffset: 26.0,
161 162
    );
    expect(maxScrollOffset, equals(26.0));
163
  });
164 165 166

  testWidgets('Resizing a ListView child restores scroll offset', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/9221
167
    final AnimationController controller = AnimationController(
168 169 170 171 172 173
      vsync: const TestVSync(),
      duration: const Duration(milliseconds: 200),
    );

    // The overall height of the frame is (as ever) 600
    Widget buildFrame() {
174
      return Directionality(
175
        textDirection: TextDirection.ltr,
176
        child: Column(
177
          children: <Widget>[
178
            Flexible(
179
              // The overall height of the ListView's contents is 500
180
              child: ListView(
181
                children: const <Widget>[
182
                  SizedBox(
183
                    height: 150.0,
184
                    child: Center(
185
                      child: Text('top'),
186
                    ),
187
                  ),
188
                  SizedBox(
189
                    height: 200.0,
190
                    child: Center(
191
                      child: Text('middle'),
192
                    ),
193
                  ),
194
                  SizedBox(
195
                    height: 150.0,
196
                    child: Center(
197
                      child: Text('bottom'),
198
                    ),
199
                  ),
200 201
                ],
              ),
202
            ),
203
            // If this widget's height is > 100 the ListView can scroll.
204
            SizeTransition(
205 206 207
              sizeFactor: controller.view,
              child: const SizedBox(
                height: 300.0,
208
                child: Text('keyboard'),
209
              ),
210
            ),
211 212
          ],
        ),
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
      );
    }

    await tester.pumpWidget(buildFrame());
    expect(find.text('top'), findsOneWidget);

    final ScrollPosition position = Scrollable.of(tester.element(find.text('middle'))).position;
    expect(position.viewportDimension, 600.0);
    expect(position.pixels, 0.0);

    // Animate the 'keyboard' height from 0 to 300
    controller.forward();
    await tester.pumpAndSettle();
    expect(position.viewportDimension, 300.0);

    // Scroll the ListView upwards
    position.jumpTo(200.0);
    await tester.pumpAndSettle();
    expect(position.pixels, 200.0);
    expect(find.text('top'), findsNothing);

    // Animate the 'keyboard' height back to 0. This causes the scroll
    // offset to return to 0.0
    controller.reverse();
    await tester.pumpAndSettle();
    expect(position.viewportDimension, 600.0);
    expect(position.pixels, 0.0);
    expect(find.text('top'), findsOneWidget);
  });
Hixie's avatar
Hixie committed
242
}