list_view_test.dart 6.52 KB
Newer Older
Adam Barth's avatar
Adam Barth committed
1 2 3 4 5 6 7
// Copyright 2015 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/widgets.dart';

8 9 10 11 12 13 14 15 16 17 18
class TestSliverChildListDelegate extends SliverChildListDelegate {
  TestSliverChildListDelegate(List<Widget> children) : super(children);

  final List<String> log = <String>[];

  @override
  void didFinishLayout(int firstIndex, int lastIndex) {
    log.add('didFinishLayout firstIndex=$firstIndex lastIndex=$lastIndex');
  }
}

Adam Barth's avatar
Adam Barth committed
19
void main() {
20 21 22 23
  testWidgets('ListView default control', (WidgetTester tester) async {
    await tester.pumpWidget(new Center(child: new ListView(itemExtent: 100.0)));
  });

24
  testWidgets('ListView itemExtent control test', (WidgetTester tester) async {
Adam Barth's avatar
Adam Barth committed
25
    await tester.pumpWidget(
26
      new ListView(
Adam Barth's avatar
Adam Barth committed
27 28 29 30 31 32 33 34 35
        itemExtent: 200.0,
        children: new List<Widget>.generate(20, (int i) {
          return new Container(
            child: new Text('$i'),
          );
        }),
      ),
    );

36
    final RenderBox box = tester.renderObject<RenderBox>(find.byType(Container).first);
Adam Barth's avatar
Adam Barth committed
37 38 39 40 41
    expect(box.size.height, equals(200.0));

    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsOneWidget);
    expect(find.text('2'), findsOneWidget);
Adam Barth's avatar
Adam Barth committed
42
    expect(find.text('3'), findsNothing);
Adam Barth's avatar
Adam Barth committed
43 44
    expect(find.text('4'), findsNothing);

45
    await tester.drag(find.byType(ListView), const Offset(0.0, -250.0));
Adam Barth's avatar
Adam Barth committed
46 47 48 49 50 51 52
    await tester.pump();

    expect(find.text('0'), findsNothing);
    expect(find.text('1'), findsOneWidget);
    expect(find.text('2'), findsOneWidget);
    expect(find.text('3'), findsOneWidget);
    expect(find.text('4'), findsOneWidget);
Adam Barth's avatar
Adam Barth committed
53
    expect(find.text('5'), findsNothing);
Adam Barth's avatar
Adam Barth committed
54 55
    expect(find.text('6'), findsNothing);

56
    await tester.drag(find.byType(ListView), const Offset(0.0, 200.0));
Adam Barth's avatar
Adam Barth committed
57 58 59 60 61 62
    await tester.pump();

    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsOneWidget);
    expect(find.text('2'), findsOneWidget);
    expect(find.text('3'), findsOneWidget);
Adam Barth's avatar
Adam Barth committed
63
    expect(find.text('4'), findsNothing);
Adam Barth's avatar
Adam Barth committed
64 65 66
    expect(find.text('5'), findsNothing);
  });

67
  testWidgets('ListView large scroll jump', (WidgetTester tester) async {
68
    final List<int> log = <int>[];
Adam Barth's avatar
Adam Barth committed
69 70

    await tester.pumpWidget(
71
      new ListView(
Adam Barth's avatar
Adam Barth committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85
        itemExtent: 200.0,
        children: new List<Widget>.generate(20, (int i) {
          return new Builder(
            builder: (BuildContext context) {
              log.add(i);
              return new Container(
                child: new Text('$i'),
              );
            }
          );
        }),
      ),
    );

Adam Barth's avatar
Adam Barth committed
86
    expect(log, equals(<int>[0, 1, 2]));
Adam Barth's avatar
Adam Barth committed
87 88
    log.clear();

89 90
    final ScrollableState state = tester.state(find.byType(Scrollable));
    final ScrollPosition position = state.position;
Adam Barth's avatar
Adam Barth committed
91 92 93 94 95
    position.jumpTo(2025.0);

    expect(log, isEmpty);
    await tester.pump();

Adam Barth's avatar
Adam Barth committed
96
    expect(log, equals(<int>[10, 11, 12, 13]));
Adam Barth's avatar
Adam Barth committed
97 98 99 100 101 102 103
    log.clear();

    position.jumpTo(975.0);

    expect(log, isEmpty);
    await tester.pump();

Adam Barth's avatar
Adam Barth committed
104
    expect(log, equals(<int>[4, 5, 6, 7]));
Adam Barth's avatar
Adam Barth committed
105 106
    log.clear();
  });
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

  testWidgets('ListView can build out of underflow', (WidgetTester tester) async {
    await tester.pumpWidget(
      new ListView(
        itemExtent: 100.0,
      ),
    );

    expect(find.text('0'), findsNothing);
    expect(find.text('1'), findsNothing);
    expect(find.text('2'), findsNothing);
    expect(find.text('3'), findsNothing);
    expect(find.text('4'), findsNothing);
    expect(find.text('5'), findsNothing);

    await tester.pumpWidget(
      new ListView(
        itemExtent: 100.0,
        children: new List<Widget>.generate(2, (int i) {
          return new Container(
            child: new Text('$i'),
          );
        }),
      ),
    );

    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsOneWidget);
    expect(find.text('2'), findsNothing);
    expect(find.text('3'), findsNothing);
    expect(find.text('4'), findsNothing);
    expect(find.text('5'), findsNothing);

    await tester.pumpWidget(
      new ListView(
        itemExtent: 100.0,
        children: new List<Widget>.generate(5, (int i) {
          return new Container(
            child: new Text('$i'),
          );
        }),
      ),
    );

    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsOneWidget);
    expect(find.text('2'), findsOneWidget);
    expect(find.text('3'), findsOneWidget);
    expect(find.text('4'), findsOneWidget);
    expect(find.text('5'), findsNothing);
  });
158

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  testWidgets('ListView can build out of overflow padding', (WidgetTester tester) async {
    await tester.pumpWidget(
      new Center(
        child: new SizedBox(
          width: 0.0,
          height: 0.0,
          child: new ListView(
            padding: const EdgeInsets.all(8.0),
            children: <Widget>[
              const Text('padded'),
            ],
          ),
        ),
      ),
    );
    expect(find.text('padded'), findsOneWidget);
  });

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
  testWidgets('ListView with itemExtent in unbounded context', (WidgetTester tester) async {
    await tester.pumpWidget(
      new SingleChildScrollView(
        child: new ListView(
          itemExtent: 100.0,
          shrinkWrap: true,
          children: new List<Widget>.generate(20, (int i) {
            return new Container(
              child: new Text('$i'),
            );
          }),
        ),
      ),
    );

    expect(find.text('0'), findsOneWidget);
    expect(find.text('19'), findsOneWidget);
  });
195 196 197 198 199 200 201 202 203 204 205 206 207 208 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

  testWidgets('didFinishLayout has correct indicies', (WidgetTester tester) async {
    final TestSliverChildListDelegate delegate = new TestSliverChildListDelegate(
      new List<Widget>.generate(20, (int i) {
        return new Container(
          child: new Text('$i'),
        );
      })
    );

    await tester.pumpWidget(
      new ListView.custom(
        itemExtent: 110.0,
        childrenDelegate: delegate,
      ),
    );

    expect(delegate.log, equals(<String>['didFinishLayout firstIndex=0 lastIndex=5']));
    delegate.log.clear();

    await tester.pumpWidget(
      new ListView.custom(
        itemExtent: 210.0,
        childrenDelegate: delegate,
      ),
    );

    expect(delegate.log, equals(<String>['didFinishLayout firstIndex=0 lastIndex=2']));
    delegate.log.clear();

    await tester.drag(find.byType(ListView), const Offset(0.0, -600.0));

    expect(delegate.log, isEmpty);

    await tester.pump();

    expect(delegate.log, equals(<String>['didFinishLayout firstIndex=2 lastIndex=5']));
    delegate.log.clear();
  });
Adam Barth's avatar
Adam Barth committed
234
}