scrollable_lazy_list_test.dart 6.13 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4
// 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.

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

Hixie's avatar
Hixie committed
8
import 'test_widgets.dart';
Hixie's avatar
Hixie committed
9 10

void main() {
11
  testWidgets('HomogeneousViewport mount/dismount smoke test', (WidgetTester tester) async {
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    List<int> callbackTracker = <int>[];

    // 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() {
      return new FlipWidget(
        left: new ScrollableLazyList(
          itemBuilder: (BuildContext context, int start, int count) {
            List<Widget> result = <Widget>[];
            for (int index = start; index < start + count; index += 1) {
              callbackTracker.add(index);
              result.add(new Container(
                key: new ValueKey<int>(index),
                height: 100.0,
                child: new Text("$index")
              ));
            }
            return result;
          },
          itemExtent: 100.0
        ),
        right: new Text('Not Today')
      );
    }
37

38
    await tester.pumpWidget(builder());
39

40
    FlipWidgetState testWidget = tester.state(find.byType(FlipWidget));
41

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

44 45
    callbackTracker.clear();
    testWidget.flip();
46
    await tester.pump();
47

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

50 51
    callbackTracker.clear();
    testWidget.flip();
52
    await tester.pump();
53

54
    expect(callbackTracker, equals(<int>[0, 1, 2, 3, 4, 5]));
Hixie's avatar
Hixie committed
55 56
  });

57
  testWidgets('HomogeneousViewport vertical', (WidgetTester tester) async {
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    List<int> callbackTracker = <int>[];

    // 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.

    ItemListBuilder itemBuilder = (BuildContext context, int start, int count) {
      List<Widget> result = <Widget>[];
      for (int index = start; index < start + count; index += 1) {
        callbackTracker.add(index);
        result.add(new Container(
          key: new ValueKey<int>(index),
          width: 500.0, // this should be ignored
          height: 400.0, // should be overridden by itemExtent
          child: new Text("$index")
        ));
      }
      return result;
    };

78
    GlobalKey<ScrollableState> scrollableKey = new GlobalKey<ScrollableState>();
79 80
    FlipWidget testWidget = new FlipWidget(
      left: new ScrollableLazyList(
81
        scrollableKey: scrollableKey,
82 83 84 85 86 87
        itemBuilder: itemBuilder,
        itemExtent: 200.0,
        initialScrollOffset: 300.0
      ),
      right: new Text('Not Today')
    );
Hixie's avatar
Hixie committed
88

89
    await tester.pumpWidget(testWidget);
Hixie's avatar
Hixie committed
90

91
    expect(callbackTracker, equals(<int>[1, 2, 3, 4]));
Hixie's avatar
Hixie committed
92

93
    callbackTracker.clear();
Hixie's avatar
Hixie committed
94

95 96
    scrollableKey.currentState.scrollTo(400.0);
    // now only 3 should fit, numbered 2-4.
Hixie's avatar
Hixie committed
97

98
    await tester.pumpWidget(testWidget);
Hixie's avatar
Hixie committed
99

100
    expect(callbackTracker, equals(<int>[2, 3, 4]));
Hixie's avatar
Hixie committed
101

102
    callbackTracker.clear();
Hixie's avatar
Hixie committed
103 104
  });

105
  testWidgets('HomogeneousViewport horizontal', (WidgetTester tester) async {
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    List<int> callbackTracker = <int>[];

    // 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.

    ItemListBuilder itemBuilder = (BuildContext context, int start, int count) {
      List<Widget> result = <Widget>[];
      for (int index = start; index < start + count; index += 1) {
        callbackTracker.add(index);
        result.add(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")
        ));
      }
      return result;
    };

126
    GlobalKey<ScrollableState> scrollableKey = new GlobalKey<ScrollableState>();
127 128
    FlipWidget testWidget = new FlipWidget(
      left: new ScrollableLazyList(
129
        scrollableKey: scrollableKey,
130 131 132 133 134 135 136
        itemBuilder: itemBuilder,
        itemExtent: 200.0,
        initialScrollOffset: 300.0,
        scrollDirection: Axis.horizontal
      ),
      right: new Text('Not Today')
    );
Hixie's avatar
Hixie committed
137

138
    await tester.pumpWidget(testWidget);
Hixie's avatar
Hixie committed
139

140
    expect(callbackTracker, equals(<int>[1, 2, 3, 4, 5]));
Hixie's avatar
Hixie committed
141

142
    callbackTracker.clear();
Hixie's avatar
Hixie committed
143

144 145
    scrollableKey.currentState.scrollTo(400.0);
    // now only 4 should fit, numbered 2-5.
Hixie's avatar
Hixie committed
146

147
    await tester.pumpWidget(testWidget);
Hixie's avatar
Hixie committed
148

149
    expect(callbackTracker, equals(<int>[2, 3, 4, 5]));
Hixie's avatar
Hixie committed
150

151
    callbackTracker.clear();
Hixie's avatar
Hixie committed
152
  });
153

154
  testWidgets('ScrollableLazyList 10 items, 2-3 items visible', (WidgetTester tester) async {
155 156 157 158 159
    List<int> callbackTracker = <int>[];

    // 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.
160

161 162 163 164 165 166 167 168 169
    ItemListBuilder itemBuilder = (BuildContext context, int start, int count) {
      List<Widget> result = <Widget>[];
      for (int index = start; index < start + count; index += 1) {
        callbackTracker.add(index);
        result.add(new Text('$index', key: new ValueKey<int>(index)));
      }
      return result;
    };

170
    GlobalKey<ScrollableState> scrollableKey = new GlobalKey<ScrollableState>();
171
    Widget testWidget = new ScrollableLazyList(
172
      scrollableKey: scrollableKey,
173 174 175 176 177
      itemBuilder: itemBuilder,
      itemExtent: 300.0,
      itemCount: 10
    );

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

    scrollableKey.currentState.scrollTo(150.0);
183
    await tester.pumpWidget(testWidget);
184
    expect(callbackTracker, equals(<int>[0, 1, 2]));
185 186 187
    callbackTracker.clear();

    scrollableKey.currentState.scrollTo(600.0);
188
    await tester.pumpWidget(testWidget);
189
    expect(callbackTracker, equals(<int>[2, 3]));
190 191 192
    callbackTracker.clear();

    scrollableKey.currentState.scrollTo(750.0);
193
    await tester.pumpWidget(testWidget);
194
    expect(callbackTracker, equals(<int>[2, 3, 4]));
195
    callbackTracker.clear();
196 197
  });

Hixie's avatar
Hixie committed
198
}