list_view_relayout_test.dart 6.01 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// 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';
import 'package:flutter/rendering.dart';
7
import 'package:flutter_test/flutter_test.dart';
8 9 10

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

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

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

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

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

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

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

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

96
    await tester.pumpWidget(
97
      Directionality(
98
        textDirection: TextDirection.ltr,
99
        child: ListView(),
100 101
      ),
    );
102
    expect(list.geometry!.scrollExtent, equals(0.0));
103 104 105
  });

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

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

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

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

135
    await tester.pumpWidget(
136
      Directionality(
137
        textDirection: TextDirection.ltr,
138
        child: ListView(),
139 140
      ),
    );
141 142 143 144 145 146

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

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

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

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

    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.

186
    await tester.pumpWidget(
187
      Directionality(
188
        textDirection: TextDirection.ltr,
189
        child: ListView(
190
          children: const <Widget>[
191
            SizedBox(height: 100.0, child: Text('100')),
192 193 194 195
          ],
        ),
      ),
    );
196

197
    final ScrollableState scrollable = tester.state(find.byType(Scrollable));
198 199
    expect(scrollable.position.maxScrollExtent, 0.0);

200
    await tester.pumpWidget(
201
      Directionality(
202
        textDirection: TextDirection.ltr,
203
        child: ListView(
204
          children: const <Widget>[
205 206 207
            SizedBox(height: 100.0, child: Text('100')),
            SizedBox(height: 200.0, child: Text('200')),
            SizedBox(height: 400.0, child: Text('400')),
208 209 210 211
          ],
        ),
      ),
    );
212 213 214 215 216

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

}