list_body_test.dart 6.55 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'package:flutter/src/foundation/assertions.dart';
import 'package:flutter/src/painting/basic_types.dart';
7 8 9 10
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart';

final List<Widget> children = <Widget>[
11 12 13 14
  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),
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
];

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 {
33
    await tester.pumpWidget(Flex(
34
      direction: Axis.vertical,
35
      children: <Widget>[ ListBody(children: children) ],
36 37 38 39 40
    ));

    expectRects(
      tester,
      <Rect>[
Dan Field's avatar
Dan Field committed
41 42 43 44
        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),
45 46 47 48 49
      ],
    );
  });

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

    expectRects(
      tester,
      <Rect>[
Dan Field's avatar
Dan Field committed
58 59 60 61
        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),
62 63 64 65 66
      ],
    );
  });

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

    expectRects(
      tester,
      <Rect>[
Dan Field's avatar
Dan Field committed
81 82 83 84
        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),
85 86 87 88 89
      ],
    );
  });

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

    expectRects(
      tester,
      <Rect>[
Dan Field's avatar
Dan Field committed
104 105 106 107
        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),
108 109 110
      ],
    );
  });
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

  testWidgets('Limited space along main axis error', (WidgetTester tester) async {
    final FlutterExceptionHandler oldHandler = FlutterError.onError;
    final List<FlutterErrorDetails> errors = <FlutterErrorDetails>[];
    FlutterError.onError = (FlutterErrorDetails error) => errors.add(error);
    try {
      await tester.pumpWidget(
        SizedBox(
          width: 100,
          height: 100,
          child: Directionality(
            textDirection: TextDirection.rtl,
            child: ListBody(
              mainAxis: Axis.horizontal,
              children: children,
            ),
          ),
        ),
      );
    } finally {
      FlutterError.onError = oldHandler;
    }
    expect(errors, isNotEmpty);
    expect(errors.first.exception, isFlutterError);
    expect(errors.first.exception.toStringDeep(), equalsIgnoringHashCodes(
      'FlutterError\n'
      '   RenderListBody must have unlimited space along its main axis.\n'
      '   RenderListBody does not clip or resize its children, so it must\n'
      '   be placed in a parent that does not constrain the main axis.\n'
      '   You probably want to put the RenderListBody inside a\n'
      '   RenderViewport with a matching main axis.\n'
    ));
  });

  testWidgets('Nested ListBody unbounded cross axis error', (WidgetTester tester) async {
    final FlutterExceptionHandler oldHandler = FlutterError.onError;
    final List<FlutterErrorDetails> errors = <FlutterErrorDetails>[];
    FlutterError.onError = (FlutterErrorDetails error) => errors.add(error);
    try {
      await tester.pumpWidget(
        Flex(
          textDirection: TextDirection.ltr,
          direction: Axis.horizontal,
          children: <Widget>[
            Directionality(
              textDirection: TextDirection.ltr,
              child: ListBody(
                mainAxis: Axis.horizontal,
                children: <Widget>[
                  Flex(
                    textDirection: TextDirection.ltr,
                    direction: Axis.vertical,
                    children: <Widget>[
                      Directionality(
                        textDirection: TextDirection.ltr,
                        child: ListBody(
                          mainAxis: Axis.vertical,
                          children: children,
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      );
    } finally {
      FlutterError.onError = oldHandler;
    }
    expect(errors, isNotEmpty);
    expect(errors.first.exception, isFlutterError);
    expect(errors.first.exception.toStringDeep(), equalsIgnoringHashCodes(
      'FlutterError\n'
      '   RenderListBody must have a bounded constraint for its cross axis.\n'
      '   RenderListBody forces its children to expand to fit the\n'
188
      "   RenderListBody's container, so it must be placed in a parent that\n"
189 190 191 192 193 194 195 196
      '   constrains the cross axis to a finite dimension.\n'
      '   If you are attempting to nest a RenderListBody with one direction\n'
      '   inside one of another direction, you will want to wrap the inner\n'
      '   one inside a box that fixes the dimension in that direction, for\n'
      '   example, a RenderIntrinsicWidth or RenderIntrinsicHeight object.\n'
      '   This is relatively expensive, however.\n'
    ));
  });
197
}