scrollable_lazy_list_test.dart 7.07 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 80 81 82 83 84 85 86 87
    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;
    };

    FlipWidget testWidget = new FlipWidget(
      left: new ScrollableLazyList(
        itemBuilder: itemBuilder,
        itemExtent: 200.0,
        initialScrollOffset: 300.0
      ),
      right: new Text('Not Today')
    );
88 89
    Completer<Null> scrollTo(double newScrollOffset) {
      Completer<Null> completer = new Completer<Null>();
90 91
      final ScrollableState scrollable = tester.state(find.byType(Scrollable));
      scrollable.scrollTo(newScrollOffset).whenComplete(completer.complete);
92 93
      return completer;
    }
Hixie's avatar
Hixie committed
94

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

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

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

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

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

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

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

113
  testWidgets('HomogeneousViewport horizontal', (WidgetTester tester) async {
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
    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;
    };

    FlipWidget testWidget = new FlipWidget(
      left: new ScrollableLazyList(
        itemBuilder: itemBuilder,
        itemExtent: 200.0,
        initialScrollOffset: 300.0,
        scrollDirection: Axis.horizontal
      ),
      right: new Text('Not Today')
    );
143 144
    Completer<Null> scrollTo(double newScrollOffset) {
      Completer<Null> completer = new Completer<Null>();
145 146
      final ScrollableState scrollable = tester.state(find.byType(Scrollable));
      scrollable.scrollTo(newScrollOffset).whenComplete(completer.complete);
147 148
      return completer;
    }
Hixie's avatar
Hixie committed
149

150
    await tester.pumpWidget(testWidget);
Hixie's avatar
Hixie committed
151

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

154
    callbackTracker.clear();
Hixie's avatar
Hixie committed
155

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

160
    await tester.pumpWidget(testWidget);
Hixie's avatar
Hixie committed
161

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

165
    callbackTracker.clear();
Hixie's avatar
Hixie committed
166
  });
167

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

175 176 177 178 179 180 181 182 183 184 185 186 187 188
    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;
    };

    Widget testWidget = new ScrollableLazyList(
      itemBuilder: itemBuilder,
      itemExtent: 300.0,
      itemCount: 10
    );
189 190
    Completer<Null> scrollTo(double newScrollOffset) {
      Completer<Null> completer = new Completer<Null>();
191 192
      ScrollableState scrollable = tester.state(find.byType(Scrollable));
      scrollable.scrollTo(newScrollOffset).whenComplete(completer.complete);
193 194
      return completer;
    }
195

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

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

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

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

Hixie's avatar
Hixie committed
222
}