list_body_test.dart 6.48 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
import 'package:flutter/src/foundation/assertions.dart';
6
import 'package:flutter/widgets.dart';
7
import 'package:flutter_test/flutter_test.dart';
8

9 10 11 12 13
const List<Widget> children = <Widget>[
  SizedBox(width: 200.0, height: 150.0),
  SizedBox(width: 200.0, height: 150.0),
  SizedBox(width: 200.0, height: 150.0),
  SizedBox(width: 200.0, height: 150.0),
14 15 16
];

void expectRects(WidgetTester tester, List<Rect> expected) {
17
  final Finder finder = find.byType(SizedBox);
18 19 20 21 22 23 24 25 26 27 28 29 30 31
  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 {
32
    await tester.pumpWidget(Flex(
33
      direction: Axis.vertical,
34
      children: <Widget>[ ListBody(children: children) ],
35 36 37 38 39
    ));

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

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

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

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

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

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

    expectRects(
      tester,
      <Rect>[
Dan Field's avatar
Dan Field committed
103 104 105 106
        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),
107 108 109
      ],
    );
  });
110 111

  testWidgets('Limited space along main axis error', (WidgetTester tester) async {
112
    final FlutterExceptionHandler oldHandler = FlutterError.onError!;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    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);
134
    expect((errors.first.exception as FlutterError).toStringDeep(), equalsIgnoringHashCodes(
135 136 137 138 139
      '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'
140
      '   RenderViewport with a matching main axis.\n',
141 142 143 144
    ));
  });

  testWidgets('Nested ListBody unbounded cross axis error', (WidgetTester tester) async {
145
    final FlutterExceptionHandler oldHandler = FlutterError.onError!;
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
    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(
                          children: children,
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      );
    } finally {
      FlutterError.onError = oldHandler;
    }
    expect(errors, isNotEmpty);
    expect(errors.first.exception, isFlutterError);
182
    expect((errors.first.exception as FlutterError).toStringDeep(), equalsIgnoringHashCodes(
183 184 185
      'FlutterError\n'
      '   RenderListBody must have a bounded constraint for its cross axis.\n'
      '   RenderListBody forces its children to expand to fit the\n'
186
      "   RenderListBody's container, so it must be placed in a parent that\n"
187 188 189 190 191
      '   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'
192
      '   This is relatively expensive, however.\n',
193 194
    ));
  });
195
}