lazy_block_viewport_test.dart 8.46 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/material.dart';
7
import 'package:flutter/rendering.dart';
Hixie's avatar
Hixie committed
8 9 10 11 12
import 'package:test/test.dart';

import 'test_widgets.dart';

void main() {
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
  testWidgets('LazyBlockViewport mount/dismount smoke test', (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 FlipWidget(
        left: new LazyBlockViewport(
          delegate: new LazyBlockBuilder(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')
      );
    }
Hixie's avatar
Hixie committed
35

36
    tester.pumpWidget(builder());
Hixie's avatar
Hixie committed
37

38
    FlipWidgetState testWidget = tester.state(find.byType(FlipWidget));
Hixie's avatar
Hixie committed
39

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

42 43 44
    callbackTracker.clear();
    testWidget.flip();
    tester.pump();
Hixie's avatar
Hixie committed
45

46
    expect(callbackTracker, equals(<int>[]));
Hixie's avatar
Hixie committed
47

48 49 50
    callbackTracker.clear();
    testWidget.flip();
    tester.pump();
Hixie's avatar
Hixie committed
51

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

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

58 59 60
    // 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
61

62
    double offset = 300.0;
Adam Barth's avatar
Adam Barth committed
63

64
    IndexedWidgetBuilder itemBuilder = (BuildContext context, int i) {
65 66 67 68 69 70 71 72
      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
73

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

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

86
    // 0 is built to find its height
87
    expect(callbackTracker, equals(<int>[0, 1, 2, 3, 4]));
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
    // We build all the children to find their new size.
95
    expect(callbackTracker, equals(<int>[0, 1, 2, 3, 4]));
96
    callbackTracker.clear();
Hixie's avatar
Hixie committed
97

98
    tester.pumpWidget(builder());
Hixie's avatar
Hixie committed
99

100
    // 0 isn't built because they're not visible.
101
    expect(callbackTracker, equals(<int>[1, 2, 3, 4]));
102 103
    callbackTracker.clear();
  });
Hixie's avatar
Hixie committed
104

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

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

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

114
    IndexedWidgetBuilder itemBuilder = (BuildContext context, int i) {
115 116 117 118 119 120 121 122
      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
123

124 125 126 127 128 129 130 131 132 133
    Widget builder() {
      return new FlipWidget(
        left: new LazyBlockViewport(
          delegate: new LazyBlockBuilder(builder: itemBuilder),
          startOffset: offset,
          mainAxis: Axis.horizontal
        ),
        right: new Text('Not Today')
      );
    }
Adam Barth's avatar
Adam Barth committed
134

135
    tester.pumpWidget(builder());
Adam Barth's avatar
Adam Barth committed
136

137
    // 0 is built to find its width
138
    expect(callbackTracker, equals(<int>[0, 1, 2, 3, 4, 5]));
Adam Barth's avatar
Adam Barth committed
139

140
    callbackTracker.clear();
Adam Barth's avatar
Adam Barth committed
141

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

144
    tester.pumpWidget(builder());
Adam Barth's avatar
Adam Barth committed
145

146
    // We build all the children to find their new size.
147
    expect(callbackTracker, equals(<int>[0, 1, 2, 3, 4, 5]));
148 149 150 151 152
    callbackTracker.clear();

    tester.pumpWidget(builder());

    // 0 isn't built because they're not visible.
153
    expect(callbackTracker, equals(<int>[1, 2, 3, 4, 5]));
154
    callbackTracker.clear();
Adam Barth's avatar
Adam Barth committed
155
  });
156

157 158 159 160
  testWidgets('LazyBlockViewport reinvoke builders', (WidgetTester tester) {
    List<int> callbackTracker = <int>[];
    List<String> text = <String>[];

161
    IndexedWidgetBuilder itemBuilder = (BuildContext context, int i) {
162 163 164 165 166 167
      callbackTracker.add(i);
      return new Container(
        key: new ValueKey<int>(i),
        width: 500.0, // this should be ignored
        height: 220.0,
        child: new Text("$i")
Adam Barth's avatar
Adam Barth committed
168
      );
169
    };
170

171 172 173 174 175 176 177 178 179
    void collectText(Widget widget) {
      if (widget is Text)
        text.add(widget.data);
    };

    Widget builder() {
      return new LazyBlockViewport(
        delegate: new LazyBlockBuilder(builder: itemBuilder),
        startOffset: 0.0
180
      );
181 182 183
    }

    tester.pumpWidget(builder());
184

185
    expect(callbackTracker, equals(<int>[0, 1, 2]));
186 187
    callbackTracker.clear();
    tester.allWidgets.forEach(collectText);
188
    expect(text, equals(<String>['0', '1', '2']));
189
    text.clear();
190

191
    tester.pumpWidget(builder());
192

193
    expect(callbackTracker, equals(<int>[0, 1, 2]));
194 195
    callbackTracker.clear();
    tester.allWidgets.forEach(collectText);
196
    expect(text, equals(<String>['0', '1', '2']));
197 198
    text.clear();
  });
199

200 201 202 203
  testWidgets('LazyBlockViewport reinvoke builders', (WidgetTester tester) {
    StateSetter setState;
    ThemeData themeData = new ThemeData.light();

204
    IndexedWidgetBuilder itemBuilder = (BuildContext context, int i) {
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
      return new Container(
        key: new ValueKey<int>(i),
        width: 500.0, // this should be ignored
        height: 220.0,
        decoration: new BoxDecoration(
          backgroundColor: Theme.of(context).primaryColor
        ),
        child: new Text("$i")
      );
    };

    Widget viewport = new LazyBlockViewport(
      delegate: new LazyBlockBuilder(builder: itemBuilder)
    );

    tester.pumpWidget(
      new StatefulBuilder(
        builder: (BuildContext context, StateSetter setter) {
          setState = setter;
          return new Theme(data: themeData, child: viewport);
        }
      )
    );

    DecoratedBox widget = tester.firstWidget(find.byType(DecoratedBox));
    BoxDecoration decoraton = widget.decoration;
    expect(decoraton.backgroundColor, equals(Colors.blue[500]));

    setState(() {
      themeData = new ThemeData(primarySwatch: Colors.green);
235
    });
236 237 238 239 240 241

    tester.pump();

    widget = tester.firstWidget(find.byType(DecoratedBox));
    decoraton = widget.decoration;
    expect(decoraton.backgroundColor, equals(Colors.green[500]));
242
  });
243

244
  testWidgets('LazyBlockViewport padding', (WidgetTester tester) {
245
    IndexedWidgetBuilder itemBuilder = (BuildContext context, int i) {
246 247 248 249 250 251 252 253
      return new Container(
        key: new ValueKey<int>(i),
        width: 500.0, // this should be ignored
        height: 220.0,
        decoration: new BoxDecoration(
          backgroundColor: Colors.green[500]
        ),
        child: new Text("$i")
254
      );
255
    };
256

257 258 259 260 261 262 263 264 265 266 267
    tester.pumpWidget(
      new LazyBlockViewport(
        padding: new EdgeInsets.fromLTRB(7.0, 3.0, 5.0, 11.0),
        delegate: new LazyBlockBuilder(builder: itemBuilder)
      )
    );

    RenderBox firstBox = tester.renderObject(find.text('0'));
    Point upperLeft = firstBox.localToGlobal(Point.origin);
    expect(upperLeft, equals(new Point(7.0, 3.0)));
    expect(firstBox.size.width, equals(800.0 - 12.0));
268 269
  });

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
  testWidgets('Underflow extents', (WidgetTester tester) {
    double lastContentExtent;
    double lastContainerExtent;
    double lastMinScrollOffset;
    void handleExtendsChanged(double contentExtent, double containerExtent, double minScrollOffset) {
      lastContentExtent = contentExtent;
      lastContainerExtent = containerExtent;
      lastMinScrollOffset = minScrollOffset;
    }

    tester.pumpWidget(new LazyBlockViewport(
      onExtentsChanged: handleExtendsChanged,
      delegate: new LazyBlockChildren(
        children: <Widget>[
          new Container(height: 100.0),
          new Container(height: 100.0),
          new Container(height: 100.0),
        ]
      )
    ));

    expect(lastContentExtent, equals(300.0));
    expect(lastContainerExtent, equals(600.0));
    expect(lastMinScrollOffset, equals(0.0));
294
  });
Hixie's avatar
Hixie committed
295
}