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

5 6
// @dart = 2.8

7 8 9 10 11 12
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

void main() {
  testWidgets('Nested ListView with shrinkWrap', (WidgetTester tester) async {
13
    await tester.pumpWidget(
14
      Directionality(
15
        textDirection: TextDirection.ltr,
16
        child: ListView(
17 18
          shrinkWrap: true,
          children: <Widget>[
19
            ListView(
20
              shrinkWrap: true,
21
              children: const <Widget>[
22 23 24
                Text('1'),
                Text('2'),
                Text('3'),
25 26
              ],
            ),
27
            ListView(
28
              shrinkWrap: true,
29
              children: const <Widget>[
30 31 32
                Text('4'),
                Text('5'),
                Text('6'),
33 34
              ],
            ),
35 36
          ],
        ),
37 38
      ),
    );
39 40 41 42 43
  });

  testWidgets('Underflowing ListView should relayout for additional children', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/5950

44
    await tester.pumpWidget(
45
      Directionality(
46
        textDirection: TextDirection.ltr,
47
        child: ListView(
48
          children: const <Widget>[
49
            SizedBox(height: 100.0, child: Text('100')),
50 51 52 53
          ],
        ),
      ),
    );
54

55
    await tester.pumpWidget(
56
      Directionality(
57
        textDirection: TextDirection.ltr,
58
        child: ListView(
59
          children: const <Widget>[
60 61
            SizedBox(height: 100.0, child: Text('100')),
            SizedBox(height: 200.0, child: Text('200')),
62 63 64 65
          ],
        ),
      ),
    );
66 67 68 69 70

    expect(find.text('200'), findsOneWidget);
  });

  testWidgets('Underflowing ListView contentExtent should track additional children', (WidgetTester tester) async {
71
    await tester.pumpWidget(
72
      Directionality(
73
        textDirection: TextDirection.ltr,
74
        child: ListView(
75
          children: const <Widget>[
76
            SizedBox(height: 100.0, child: Text('100')),
77 78 79 80
          ],
        ),
      ),
    );
81

82
    final RenderSliverList list = tester.renderObject(find.byType(SliverList));
83 84
    expect(list.geometry.scrollExtent, equals(100.0));

85
    await tester.pumpWidget(
86
      Directionality(
87
        textDirection: TextDirection.ltr,
88
        child: ListView(
89
          children: const <Widget>[
90 91
            SizedBox(height: 100.0, child: Text('100')),
            SizedBox(height: 200.0, child: Text('200')),
92 93 94 95
          ],
        ),
      ),
    );
96 97
    expect(list.geometry.scrollExtent, equals(300.0));

98
    await tester.pumpWidget(
99
      Directionality(
100
        textDirection: TextDirection.ltr,
101
        child: ListView(
102
          children: const <Widget>[],
103 104 105
        ),
      ),
    );
106 107 108 109
    expect(list.geometry.scrollExtent, equals(0.0));
  });

  testWidgets('Overflowing ListView should relayout for missing children', (WidgetTester tester) async {
110
    await tester.pumpWidget(
111
      Directionality(
112
        textDirection: TextDirection.ltr,
113
        child: ListView(
114
          children: const <Widget>[
115 116
            SizedBox(height: 300.0, child: Text('300')),
            SizedBox(height: 400.0, child: Text('400')),
117 118 119 120
          ],
        ),
      ),
    );
121 122 123 124

    expect(find.text('300'), findsOneWidget);
    expect(find.text('400'), findsOneWidget);

125
    await tester.pumpWidget(
126
      Directionality(
127
        textDirection: TextDirection.ltr,
128
        child: ListView(
129
          children: const <Widget>[
130
            SizedBox(height: 300.0, child: Text('300')),
131 132 133 134
          ],
        ),
      ),
    );
135 136 137 138

    expect(find.text('300'), findsOneWidget);
    expect(find.text('400'), findsNothing);

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

    expect(find.text('300'), findsNothing);
    expect(find.text('400'), findsNothing);
  });

  testWidgets('Overflowing ListView should not relayout for additional children', (WidgetTester tester) async {
153
    await tester.pumpWidget(
154
      Directionality(
155
        textDirection: TextDirection.ltr,
156
        child: ListView(
157
          children: const <Widget>[
158 159
            SizedBox(height: 300.0, child: Text('300')),
            SizedBox(height: 400.0, child: Text('400')),
160 161 162 163
          ],
        ),
      ),
    );
164 165 166 167

    expect(find.text('300'), findsOneWidget);
    expect(find.text('400'), findsOneWidget);

168
    await tester.pumpWidget(
169
      Directionality(
170
        textDirection: TextDirection.ltr,
171
        child: ListView(
172
          children: const <Widget>[
173 174 175
            SizedBox(height: 300.0, child: Text('300')),
            SizedBox(height: 400.0, child: Text('400')),
            SizedBox(height: 100.0, child: Text('100')),
176 177 178 179
          ],
        ),
      ),
    );
180 181 182 183 184 185 186 187 188 189 190 191

    expect(find.text('300'), findsOneWidget);
    expect(find.text('400'), findsOneWidget);
    expect(find.text('100'), findsNothing);
  });

  testWidgets('Overflowing ListView should become scrollable', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/5920
    // When a ListView's viewport hasn't overflowed, scrolling is disabled.
    // When children are added that cause it to overflow, scrolling should
    // be enabled.

192
    await tester.pumpWidget(
193
      Directionality(
194
        textDirection: TextDirection.ltr,
195
        child: ListView(
196
          children: const <Widget>[
197
            SizedBox(height: 100.0, child: Text('100')),
198 199 200 201
          ],
        ),
      ),
    );
202

203
    final ScrollableState scrollable = tester.state(find.byType(Scrollable));
204 205
    expect(scrollable.position.maxScrollExtent, 0.0);

206
    await tester.pumpWidget(
207
      Directionality(
208
        textDirection: TextDirection.ltr,
209
        child: ListView(
210
          children: const <Widget>[
211 212 213
            SizedBox(height: 100.0, child: Text('100')),
            SizedBox(height: 200.0, child: Text('200')),
            SizedBox(height: 400.0, child: Text('400')),
214 215 216 217
          ],
        ),
      ),
    );
218 219 220 221 222

    expect(scrollable.position.maxScrollExtent, 100.0);
  });

}