mixed_viewport_test.dart 4.05 KB
Newer Older
1
import 'package:flutter/widgets.dart';
Hixie's avatar
Hixie committed
2 3 4 5 6 7 8
import 'package:test/test.dart';

import 'widget_tester.dart';
import 'test_widgets.dart';

void main() {
  test('MixedViewport mount/dismount smoke test', () {
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
    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(
          left: new MixedViewport(
            builder: (BuildContext context, int i) {
              callbackTracker.add(i);
              return new Container(
                key: new ValueKey<int>(i),
                height: 100.0,
                child: new Text("$i")
              );
            },
            startOffset: 0.0
          ),
          right: new Text('Not Today')
        );
      }

      tester.pumpWidget(builder());

34
      StatefulComponentElement element = tester.findElement((Element element) => element.widget is FlipComponent);
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
      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
51 52 53
  });

  test('MixedViewport vertical', () {
54 55
    testWidgets((WidgetTester tester) {
      List<int> callbackTracker = <int>[];
Hixie's avatar
Hixie committed
56

57 58 59
      // 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.
Hixie's avatar
Hixie committed
60

61
      double offset = 300.0;
Hixie's avatar
Hixie committed
62

63 64 65 66 67 68 69 70 71
      IndexedBuilder itemBuilder = (BuildContext context, int i) {
        callbackTracker.add(i);
        return new Container(
          key: new ValueKey<int>(i),
          width: 500.0, // this should be ignored
          height: 200.0,
          child: new Text("$i")
        );
      };
Hixie's avatar
Hixie committed
72

73 74 75 76 77 78 79 80 81
      Widget builder() {
        return new FlipComponent(
          left: new MixedViewport(
            builder: itemBuilder,
            startOffset: offset
          ),
          right: new Text('Not Today')
        );
      }
Hixie's avatar
Hixie committed
82

83
      tester.pumpWidget(builder());
Hixie's avatar
Hixie committed
84

85 86
      // 0 is built to find its width
      expect(callbackTracker, equals([0, 1, 2, 3, 4]));
Hixie's avatar
Hixie committed
87

88
      callbackTracker.clear();
Hixie's avatar
Hixie committed
89

90
      offset = 400.0; // now only 3 should fit, numbered 2-4.
Hixie's avatar
Hixie committed
91

92
      tester.pumpWidget(builder());
Hixie's avatar
Hixie committed
93

94 95
      // 0 and 1 aren't built, we know their size and nothing else changed
      expect(callbackTracker, equals([2, 3, 4]));
Hixie's avatar
Hixie committed
96

97 98
      callbackTracker.clear();
    });
Hixie's avatar
Hixie committed
99 100 101
  });

  test('MixedViewport horizontal', () {
102 103
    testWidgets((WidgetTester tester) {
      List<int> callbackTracker = <int>[];
Hixie's avatar
Hixie committed
104

105 106 107
      // 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.
Hixie's avatar
Hixie committed
108

109
      double offset = 300.0;
Hixie's avatar
Hixie committed
110

111 112 113 114 115 116 117 118 119
      IndexedBuilder itemBuilder = (BuildContext context, int i) {
        callbackTracker.add(i);
        return new Container(
          key: new ValueKey<int>(i),
          height: 500.0, // this should be ignored
          width: 200.0,
          child: new Text("$i")
        );
      };
Hixie's avatar
Hixie committed
120

121 122 123 124 125 126 127 128 129 130
      Widget builder() {
        return new FlipComponent(
          left: new MixedViewport(
            builder: itemBuilder,
            startOffset: offset,
            direction: ScrollDirection.horizontal
          ),
          right: new Text('Not Today')
        );
      }
Hixie's avatar
Hixie committed
131

132
      tester.pumpWidget(builder());
Hixie's avatar
Hixie committed
133

134 135
      // 0 is built to find its width
      expect(callbackTracker, equals([0, 1, 2, 3, 4, 5]));
Hixie's avatar
Hixie committed
136

137
      callbackTracker.clear();
Hixie's avatar
Hixie committed
138

139
      offset = 400.0; // now only 4 should fit, numbered 2-5.
Hixie's avatar
Hixie committed
140

141
      tester.pumpWidget(builder());
Hixie's avatar
Hixie committed
142

143 144
      // 0 and 1 aren't built, we know their size and nothing else changed
      expect(callbackTracker, equals([2, 3, 4, 5]));
Hixie's avatar
Hixie committed
145

146 147
      callbackTracker.clear();
    });
Hixie's avatar
Hixie committed
148 149
  });
}