mixed_viewport_test.dart 4.22 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 9 10 11 12
import 'package:test/test.dart';

import 'test_widgets.dart';

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

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

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

61 62 63
      // 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
64

65
      double offset = 300.0;
Hixie's avatar
Hixie committed
66

67 68 69 70 71 72 73 74 75
      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
76

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

87
      tester.pumpWidget(builder());
Hixie's avatar
Hixie committed
88

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

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

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

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

98 99
      // 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
100

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

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

109 110 111
      // 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
112

113
      double offset = 300.0;
Hixie's avatar
Hixie committed
114

115 116 117 118 119 120 121 122 123
      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
124

125 126 127 128 129
      Widget builder() {
        return new FlipComponent(
          left: new MixedViewport(
            builder: itemBuilder,
            startOffset: offset,
130
            direction: Axis.horizontal
131 132 133 134
          ),
          right: new Text('Not Today')
        );
      }
Hixie's avatar
Hixie committed
135

136
      tester.pumpWidget(builder());
Hixie's avatar
Hixie committed
137

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

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

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

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

147 148
      // 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
149

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