list_body_test.dart 3.13 KB
Newer Older
1 2 3 4 5 6 7 8
// 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.

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart';

final List<Widget> children = <Widget>[
9 10 11 12
  Container(width: 200.0, height: 150.0),
  Container(width: 200.0, height: 150.0),
  Container(width: 200.0, height: 150.0),
  Container(width: 200.0, height: 150.0),
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
];

void expectRects(WidgetTester tester, List<Rect> expected) {
  final Finder finder = find.byType(Container);
  finder.precache();
  final List<Rect> actual = <Rect>[];
  for (int i = 0; i < expected.length; ++i) {
    final Finder current = finder.at(i);
    expect(current, findsOneWidget);
    actual.add(tester.getRect(finder.at(i)));
  }
  expect(() => finder.at(expected.length), throwsRangeError);
  expect(actual, equals(expected));
}

void main() {

  testWidgets('ListBody down', (WidgetTester tester) async {
31
    await tester.pumpWidget(Flex(
32
      direction: Axis.vertical,
33
      children: <Widget>[ ListBody(children: children) ],
34 35 36 37 38
    ));

    expectRects(
      tester,
      <Rect>[
Dan Field's avatar
Dan Field committed
39 40 41 42
        const Rect.fromLTWH(0.0, 0.0, 800.0, 150.0),
        const Rect.fromLTWH(0.0, 150.0, 800.0, 150.0),
        const Rect.fromLTWH(0.0, 300.0, 800.0, 150.0),
        const Rect.fromLTWH(0.0, 450.0, 800.0, 150.0),
43 44 45 46 47
      ],
    );
  });

  testWidgets('ListBody up', (WidgetTester tester) async {
48
    await tester.pumpWidget(Flex(
49
      direction: Axis.vertical,
50
      children: <Widget>[ ListBody(reverse: true, children: children) ],
51 52 53 54 55
    ));

    expectRects(
      tester,
      <Rect>[
Dan Field's avatar
Dan Field committed
56 57 58 59
        const Rect.fromLTWH(0.0, 450.0, 800.0, 150.0),
        const Rect.fromLTWH(0.0, 300.0, 800.0, 150.0),
        const Rect.fromLTWH(0.0, 150.0, 800.0, 150.0),
        const Rect.fromLTWH(0.0, 0.0, 800.0, 150.0),
60 61 62 63 64
      ],
    );
  });

  testWidgets('ListBody right', (WidgetTester tester) async {
65
    await tester.pumpWidget(Flex(
66 67 68
      textDirection: TextDirection.ltr,
      direction: Axis.horizontal,
      children: <Widget>[
69
        Directionality(
70
          textDirection: TextDirection.ltr,
71
          child: ListBody(mainAxis: Axis.horizontal, children: children),
72 73 74 75 76 77 78
        ),
      ],
    ));

    expectRects(
      tester,
      <Rect>[
Dan Field's avatar
Dan Field committed
79 80 81 82
        const Rect.fromLTWH(0.0, 0.0, 200.0, 600.0),
        const Rect.fromLTWH(200.0, 0.0, 200.0, 600.0),
        const Rect.fromLTWH(400.0, 0.0, 200.0, 600.0),
        const Rect.fromLTWH(600.0, 0.0, 200.0, 600.0),
83 84 85 86 87
      ],
    );
  });

  testWidgets('ListBody left', (WidgetTester tester) async {
88
    await tester.pumpWidget(Flex(
89 90 91
      textDirection: TextDirection.ltr,
      direction: Axis.horizontal,
      children: <Widget>[
92
        Directionality(
93
          textDirection: TextDirection.rtl,
94
          child: ListBody(mainAxis: Axis.horizontal, children: children),
95 96 97 98 99 100 101
        ),
      ],
    ));

    expectRects(
      tester,
      <Rect>[
Dan Field's avatar
Dan Field committed
102 103 104 105
        const Rect.fromLTWH(600.0, 0.0, 200.0, 600.0),
        const Rect.fromLTWH(400.0, 0.0, 200.0, 600.0),
        const Rect.fromLTWH(200.0, 0.0, 200.0, 600.0),
        const Rect.fromLTWH(0.0, 0.0, 200.0, 600.0),
106 107 108 109
      ],
    );
  });
}