list_view_builder_test.dart 6.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// 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';

import 'test_widgets.dart';

void main() {
  testWidgets('ListView.builder mount/dismount smoke test', (WidgetTester tester) async {
12
    final List<int> callbackTracker = <int>[];
13 14 15 16 17

    // the root view is 800x600 in the test environment
    // so if our widget is 100 pixels tall, it should fit exactly 6 times.

    Widget builder() {
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
      return new Directionality(
        textDirection: TextDirection.ltr,
        child: new FlipWidget(
          left: new ListView.builder(
            itemExtent: 100.0,
            itemBuilder: (BuildContext context, int index) {
              callbackTracker.add(index);
              return new Container(
                key: new ValueKey<int>(index),
                height: 100.0,
                child: new Text('$index'),
              );
            },
          ),
          right: const Text('Not Today'),
33 34 35 36 37 38
        ),
      );
    }

    await tester.pumpWidget(builder());

39
    final FlipWidgetState testWidget = tester.state(find.byType(FlipWidget));
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

    expect(callbackTracker, equals(<int>[0, 1, 2, 3, 4, 5]));

    callbackTracker.clear();
    testWidget.flip();
    await tester.pump();

    expect(callbackTracker, equals(<int>[]));

    callbackTracker.clear();
    testWidget.flip();
    await tester.pump();

    expect(callbackTracker, equals(<int>[0, 1, 2, 3, 4, 5]));
  });

  testWidgets('ListView.builder vertical', (WidgetTester tester) async {
57
    final List<int> callbackTracker = <int>[];
58 59 60 61 62

    // the root view is 800x600 in the test environment
    // so if our widget is 200 pixels tall, it should fit exactly 3 times.
    // but if we are offset by 300 pixels, there will be 4, numbered 1-4.

63
    final IndexedWidgetBuilder itemBuilder = (BuildContext context, int index) {
64 65 66 67 68
      callbackTracker.add(index);
      return new Container(
        key: new ValueKey<int>(index),
        width: 500.0, // this should be ignored
        height: 400.0, // should be overridden by itemExtent
Ian Hickson's avatar
Ian Hickson committed
69
        child: new Text('$index', textDirection: TextDirection.ltr)
70 71 72
      );
    };

73 74 75 76 77 78 79 80 81
    Widget buildWidget() {
      return new Directionality(
        textDirection: TextDirection.ltr,
        child: new FlipWidget(
          left: new ListView.builder(
            controller: new ScrollController(initialScrollOffset: 300.0),
            itemExtent: 200.0,
            itemBuilder: itemBuilder,
          ),
Ian Hickson's avatar
Ian Hickson committed
82
          right: const Text('Not Today'),
83 84 85 86 87
        ),
      );
    }

    void jumpTo(double newScrollOffset) {
Adam Barth's avatar
Adam Barth committed
88
      final ScrollableState scrollable = tester.state(find.byType(Scrollable));
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
      scrollable.position.jumpTo(newScrollOffset);
    }

    await tester.pumpWidget(buildWidget());

    expect(callbackTracker, equals(<int>[1, 2, 3, 4]));
    callbackTracker.clear();

    jumpTo(400.0);
    // now only 3 should fit, numbered 2-4.

    await tester.pumpWidget(buildWidget());

    expect(callbackTracker, equals(<int>[1, 2, 3, 4]));
    callbackTracker.clear();

    await tester.pumpWidget(buildWidget());

    expect(callbackTracker, equals(<int>[2, 3, 4]));
    callbackTracker.clear();

    jumpTo(500.0);
    // now 4 should fit, numbered 2-5.

    await tester.pumpWidget(buildWidget());

    expect(callbackTracker, equals(<int>[2, 3, 4, 5]));
    callbackTracker.clear();
  });

  testWidgets('ListView.builder horizontal', (WidgetTester tester) async {
120
    final List<int> callbackTracker = <int>[];
121 122 123 124 125

    // the root view is 800x600 in the test environment
    // so if our widget is 200 pixels wide, it should fit exactly 4 times.
    // but if we are offset by 300 pixels, there will be 5, numbered 1-5.

126
    final IndexedWidgetBuilder itemBuilder = (BuildContext context, int index) {
127 128 129 130 131 132 133 134 135
      callbackTracker.add(index);
      return new Container(
        key: new ValueKey<int>(index),
        width: 400.0, // this should be overridden by itemExtent
        height: 500.0, // this should be ignored
        child: new Text('$index'),
      );
    };

136 137 138 139 140 141 142 143 144 145 146
    Widget buildWidget() {
      return new Directionality(
        textDirection: TextDirection.ltr,
        child: new FlipWidget(
          left: new ListView.builder(
            controller: new ScrollController(initialScrollOffset: 300.0),
            itemBuilder: itemBuilder,
            itemExtent: 200.0,
            scrollDirection: Axis.horizontal,
          ),
          right: const Text('Not Today'),
147 148 149 150 151
        ),
      );
    }

    void jumpTo(double newScrollOffset) {
Adam Barth's avatar
Adam Barth committed
152
      final ScrollableState scrollable = tester.state(find.byType(Scrollable));
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
      scrollable.position.jumpTo(newScrollOffset);
    }

    await tester.pumpWidget(buildWidget());

    expect(callbackTracker, equals(<int>[1, 2, 3, 4, 5]));

    callbackTracker.clear();

    jumpTo(400.0);
    // now only 4 should fit, numbered 2-5.

    await tester.pumpWidget(buildWidget());

    expect(callbackTracker, equals(<int>[1, 2, 3, 4, 5]));
    callbackTracker.clear();

    await tester.pumpWidget(buildWidget());

    expect(callbackTracker, equals(<int>[2, 3, 4, 5]));
    callbackTracker.clear();

    jumpTo(500.0);
    // now only 5 should fit, numbered 2-6.

    await tester.pumpWidget(buildWidget());

    expect(callbackTracker, equals(<int>[2, 3, 4, 5, 6]));
    callbackTracker.clear();
  });

  testWidgets('ListView.builder 10 items, 2-3 items visible', (WidgetTester tester) async {
185
    final List<int> callbackTracker = <int>[];
186 187 188 189 190

    // The root view is 800x600 in the test environment and our list
    // items are 300 tall. Scrolling should cause two or three items
    // to be built.

191
    final IndexedWidgetBuilder itemBuilder = (BuildContext context, int index) {
192
      callbackTracker.add(index);
Ian Hickson's avatar
Ian Hickson committed
193
      return new Text('$index', key: new ValueKey<int>(index), textDirection: TextDirection.ltr);
194 195
    };

196 197 198 199 200 201 202
    final Widget testWidget = new Directionality(
      textDirection: TextDirection.ltr,
      child: new ListView.builder(
        itemBuilder: itemBuilder,
        itemExtent: 300.0,
        itemCount: 10,
      ),
203 204 205
    );

    void jumpTo(double newScrollOffset) {
Adam Barth's avatar
Adam Barth committed
206
      final ScrollableState scrollable = tester.state(find.byType(Scrollable));
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
      scrollable.position.jumpTo(newScrollOffset);
    }

    await tester.pumpWidget(testWidget);
    expect(callbackTracker, equals(<int>[0, 1]));
    callbackTracker.clear();

    jumpTo(150.0);
    await tester.pump();

    expect(callbackTracker, equals(<int>[2]));
    callbackTracker.clear();

    jumpTo(600.0);
    await tester.pump();

    expect(callbackTracker, equals(<int>[3]));
    callbackTracker.clear();

    jumpTo(750.0);
    await tester.pump();

    expect(callbackTracker, equals(<int>[4]));
    callbackTracker.clear();
  });

}