list_body_test.dart 3.15 KB
Newer Older
1 2 3 4 5 6 7 8 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
// 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>[
  new Container(width: 200.0, height: 150.0),
  new Container(width: 200.0, height: 150.0),
  new Container(width: 200.0, height: 150.0),
  new Container(width: 200.0, height: 150.0),
];

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 {
    await tester.pumpWidget(new Flex(
      direction: Axis.vertical,
      children: <Widget>[ new ListBody(children: children) ],
    ));

    expectRects(
      tester,
      <Rect>[
        new Rect.fromLTWH(0.0, 0.0, 800.0, 150.0),
        new Rect.fromLTWH(0.0, 150.0, 800.0, 150.0),
        new Rect.fromLTWH(0.0, 300.0, 800.0, 150.0),
        new Rect.fromLTWH(0.0, 450.0, 800.0, 150.0),
      ],
    );
  });

  testWidgets('ListBody up', (WidgetTester tester) async {
    await tester.pumpWidget(new Flex(
      direction: Axis.vertical,
      children: <Widget>[ new ListBody(reverse: true, children: children) ],
    ));

    expectRects(
      tester,
      <Rect>[
        new Rect.fromLTWH(0.0, 450.0, 800.0, 150.0),
        new Rect.fromLTWH(0.0, 300.0, 800.0, 150.0),
        new Rect.fromLTWH(0.0, 150.0, 800.0, 150.0),
        new Rect.fromLTWH(0.0, 0.0, 800.0, 150.0),
      ],
    );
  });

  testWidgets('ListBody right', (WidgetTester tester) async {
    await tester.pumpWidget(new Flex(
      textDirection: TextDirection.ltr,
      direction: Axis.horizontal,
      children: <Widget>[
        new Directionality(
          textDirection: TextDirection.ltr,
          child: new ListBody(mainAxis: Axis.horizontal, children: children),
        ),
      ],
    ));

    expectRects(
      tester,
      <Rect>[
        new Rect.fromLTWH(0.0, 0.0, 200.0, 600.0),
        new Rect.fromLTWH(200.0, 0.0, 200.0, 600.0),
        new Rect.fromLTWH(400.0, 0.0, 200.0, 600.0),
        new Rect.fromLTWH(600.0, 0.0, 200.0, 600.0),
      ],
    );
  });

  testWidgets('ListBody left', (WidgetTester tester) async {
    await tester.pumpWidget(new Flex(
      textDirection: TextDirection.ltr,
      direction: Axis.horizontal,
      children: <Widget>[
        new Directionality(
          textDirection: TextDirection.rtl,
          child: new ListBody(mainAxis: Axis.horizontal, children: children),
        ),
      ],
    ));

    expectRects(
      tester,
      <Rect>[
        new Rect.fromLTWH(600.0, 0.0, 200.0, 600.0),
        new Rect.fromLTWH(400.0, 0.0, 200.0, 600.0),
        new Rect.fromLTWH(200.0, 0.0, 200.0, 600.0),
        new Rect.fromLTWH(0.0, 0.0, 200.0, 600.0),
      ],
    );
  });
}