scrollable_lazy_list_test.dart 5.05 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 8
import 'package:test/test.dart';

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

void main() {
  test('HomogeneousViewport mount/dismount smoke test', () {
13 14 15 16 17 18 19 20
    testWidgets((WidgetTester tester) {
      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 FlipComponent(
21 22
          left: new ScrollableLazyList(
            itemBuilder: (BuildContext context, int start, int count) {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
              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')
        );
      }

      tester.pumpWidget(builder());

42
      StatefulComponentElement element = tester.findElement((Element element) => element.widget is FlipComponent);
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
      FlipComponentState testComponent = element.state;

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

      callbackTracker.clear();
      testComponent.flip();
      tester.pump();

      expect(callbackTracker, equals([]));

      callbackTracker.clear();
      testComponent.flip();
      tester.pump();

      expect(callbackTracker, equals([0, 1, 2, 3, 4, 5]));
    });
Hixie's avatar
Hixie committed
59 60 61
  });

  test('HomogeneousViewport vertical', () {
62 63 64 65 66 67 68
    testWidgets((WidgetTester tester) {
      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.

69
      ItemListBuilder itemBuilder = (BuildContext context, int start, int count) {
70 71 72 73 74 75 76 77 78 79 80 81 82
        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;
      };

83 84 85 86 87 88 89 90 91 92
      GlobalKey<ScrollableState<ScrollableLazyList>> scrollableKey = new GlobalKey<ScrollableState<ScrollableLazyList>>();
      FlipComponent testComponent = new FlipComponent(
        left: new ScrollableLazyList(
          key: scrollableKey,
          itemBuilder: itemBuilder,
          itemExtent: 200.0,
          initialScrollOffset: 300.0
        ),
        right: new Text('Not Today')
      );
Hixie's avatar
Hixie committed
93

94
      tester.pumpWidget(testComponent);
Hixie's avatar
Hixie committed
95

96
      expect(callbackTracker, equals([1, 2, 3, 4]));
Hixie's avatar
Hixie committed
97

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

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

103
      tester.pumpWidget(testComponent);
Hixie's avatar
Hixie committed
104

105
      expect(callbackTracker, equals([2, 3, 4]));
Hixie's avatar
Hixie committed
106

107 108
      callbackTracker.clear();
    });
Hixie's avatar
Hixie committed
109 110 111
  });

  test('HomogeneousViewport horizontal', () {
112 113 114 115 116 117 118
    testWidgets((WidgetTester tester) {
      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.

119
      ItemListBuilder itemBuilder = (BuildContext context, int start, int count) {
120 121 122 123 124 125 126 127 128 129 130 131 132
        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;
      };

133 134 135 136 137 138 139 140 141 142 143
      GlobalKey<ScrollableState<ScrollableLazyList>> scrollableKey = new GlobalKey<ScrollableState<ScrollableLazyList>>();
      FlipComponent testComponent = new FlipComponent(
        left: new ScrollableLazyList(
          key: scrollableKey,
          itemBuilder: itemBuilder,
          itemExtent: 200.0,
          initialScrollOffset: 300.0,
          scrollDirection: Axis.horizontal
        ),
        right: new Text('Not Today')
      );
Hixie's avatar
Hixie committed
144

145
      tester.pumpWidget(testComponent);
Hixie's avatar
Hixie committed
146

147
      expect(callbackTracker, equals([1, 2, 3, 4, 5]));
Hixie's avatar
Hixie committed
148

149
      callbackTracker.clear();
Hixie's avatar
Hixie committed
150

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

154
      tester.pumpWidget(testComponent);
Hixie's avatar
Hixie committed
155

156
      expect(callbackTracker, equals([2, 3, 4, 5]));
Hixie's avatar
Hixie committed
157

158 159
      callbackTracker.clear();
    });
Hixie's avatar
Hixie committed
160 161
  });
}