scrollable_lazy_list_test.dart 7.24 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.

5 6
import 'dart:async';

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

Hixie's avatar
Hixie committed
10
import 'test_widgets.dart';
Hixie's avatar
Hixie committed
11 12

void main() {
13
  testWidgets('HomogeneousViewport mount/dismount smoke test', (WidgetTester tester) async {
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    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')
      );
    }
39

40
    await tester.pumpWidget(builder());
41

42
    FlipWidgetState testWidget = tester.state(find.byType(FlipWidget));
43

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

46 47
    callbackTracker.clear();
    testWidget.flip();
48
    await tester.pump();
49

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

52 53
    callbackTracker.clear();
    testWidget.flip();
54
    await tester.pump();
55

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

59
  testWidgets('HomogeneousViewport vertical', (WidgetTester tester) async {
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    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;
    };

80
    GlobalKey<ScrollableState> scrollableKey = new GlobalKey<ScrollableState>();
81 82
    FlipWidget testWidget = new FlipWidget(
      left: new ScrollableLazyList(
83
        scrollableKey: scrollableKey,
84 85 86 87 88 89
        itemBuilder: itemBuilder,
        itemExtent: 200.0,
        initialScrollOffset: 300.0
      ),
      right: new Text('Not Today')
    );
90 91 92 93 94
    Completer<Null> scrollTo(double newScrollOffset) {
      Completer<Null> completer = new Completer<Null>();
      scrollableKey.currentState.scrollTo(newScrollOffset).whenComplete(completer.complete);
      return completer;
    }
Hixie's avatar
Hixie committed
95

96
    await tester.pumpWidget(testWidget);
Hixie's avatar
Hixie committed
97

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

100
    callbackTracker.clear();
Hixie's avatar
Hixie committed
101

102 103
    Completer<Null> completer = scrollTo(400.0);
    expect(completer.isCompleted, isFalse);
104
    // now only 3 should fit, numbered 2-4.
Hixie's avatar
Hixie committed
105

106
    await tester.pumpWidget(testWidget);
Hixie's avatar
Hixie committed
107

108
    expect(callbackTracker, equals(<int>[2, 3, 4]));
109
    expect(completer.isCompleted, isTrue);
Hixie's avatar
Hixie committed
110

111
    callbackTracker.clear();
Hixie's avatar
Hixie committed
112 113
  });

114
  testWidgets('HomogeneousViewport horizontal', (WidgetTester tester) async {
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    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;
    };

135
    GlobalKey<ScrollableState> scrollableKey = new GlobalKey<ScrollableState>();
136 137
    FlipWidget testWidget = new FlipWidget(
      left: new ScrollableLazyList(
138
        scrollableKey: scrollableKey,
139 140 141 142 143 144 145
        itemBuilder: itemBuilder,
        itemExtent: 200.0,
        initialScrollOffset: 300.0,
        scrollDirection: Axis.horizontal
      ),
      right: new Text('Not Today')
    );
146 147 148 149 150
    Completer<Null> scrollTo(double newScrollOffset) {
      Completer<Null> completer = new Completer<Null>();
      scrollableKey.currentState.scrollTo(newScrollOffset).whenComplete(completer.complete);
      return completer;
    }
Hixie's avatar
Hixie committed
151

152
    await tester.pumpWidget(testWidget);
Hixie's avatar
Hixie committed
153

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

156
    callbackTracker.clear();
Hixie's avatar
Hixie committed
157

158 159
    Completer<Null> completer = scrollTo(400.0);
    expect(completer.isCompleted, isFalse);
160
    // now only 4 should fit, numbered 2-5.
Hixie's avatar
Hixie committed
161

162
    await tester.pumpWidget(testWidget);
Hixie's avatar
Hixie committed
163

164
    expect(callbackTracker, equals(<int>[2, 3, 4, 5]));
165
    expect(completer.isCompleted, isTrue);
Hixie's avatar
Hixie committed
166

167
    callbackTracker.clear();
Hixie's avatar
Hixie committed
168
  });
169

170
  testWidgets('ScrollableLazyList 10 items, 2-3 items visible', (WidgetTester tester) async {
171 172 173 174 175
    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.
176

177 178 179 180 181 182 183 184 185
    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;
    };

186
    GlobalKey<ScrollableState> scrollableKey = new GlobalKey<ScrollableState>();
187
    Widget testWidget = new ScrollableLazyList(
188
      scrollableKey: scrollableKey,
189 190 191 192
      itemBuilder: itemBuilder,
      itemExtent: 300.0,
      itemCount: 10
    );
193 194 195 196 197
    Completer<Null> scrollTo(double newScrollOffset) {
      Completer<Null> completer = new Completer<Null>();
      scrollableKey.currentState.scrollTo(newScrollOffset).whenComplete(completer.complete);
      return completer;
    }
198

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

203 204
    Completer<Null> completer = scrollTo(150.0);
    expect(completer.isCompleted, isFalse);
205
    await tester.pumpWidget(testWidget);
206
    expect(callbackTracker, equals(<int>[0, 1, 2]));
207
    expect(completer.isCompleted, isTrue);
208 209
    callbackTracker.clear();

210 211
    completer = scrollTo(600.0);
    expect(completer.isCompleted, isFalse);
212
    await tester.pumpWidget(testWidget);
213
    expect(callbackTracker, equals(<int>[2, 3]));
214
    expect(completer.isCompleted, isTrue);
215 216
    callbackTracker.clear();

217 218
    completer = scrollTo(750.0);
    expect(completer.isCompleted, isFalse);
219
    await tester.pumpWidget(testWidget);
220
    expect(callbackTracker, equals(<int>[2, 3, 4]));
221
    expect(completer.isCompleted, isTrue);
222
    callbackTracker.clear();
223 224
  });

Hixie's avatar
Hixie committed
225
}