list_body_test.dart 6.71 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
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
9

10 11 12 13 14
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),
15 16 17
];

void expectRects(WidgetTester tester, List<Rect> expected) {
18
  final Finder finder = find.byType(SizedBox);
19
  final List<Rect> actual = <Rect>[];
20 21 22 23 24 25 26
  finder.runCached(() {
    for (int i = 0; i < expected.length; ++i) {
      final Finder current = finder.at(i);
      expect(current, findsOneWidget);
      actual.add(tester.getRect(finder.at(i)));
    }
  });
27 28 29 30 31 32
  expect(() => finder.at(expected.length), throwsRangeError);
  expect(actual, equals(expected));
}

void main() {

33
  testWidgetsWithLeakTracking('ListBody down', (WidgetTester tester) async {
34
    await tester.pumpWidget(const Flex(
35
      direction: Axis.vertical,
36
      children: <Widget>[ ListBody(children: children) ],
37 38 39 40 41
    ));

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

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

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

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

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

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

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

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

146
  testWidgetsWithLeakTracking('Nested ListBody unbounded cross axis error', (WidgetTester tester) async {
147
    final FlutterExceptionHandler oldHandler = FlutterError.onError!;
148 149 150 151
    final List<FlutterErrorDetails> errors = <FlutterErrorDetails>[];
    FlutterError.onError = (FlutterErrorDetails error) => errors.add(error);
    try {
      await tester.pumpWidget(
152
        const Flex(
153 154 155 156 157 158 159 160 161 162 163
          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,
164
                    children: <Widget>[
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
                      Directionality(
                        textDirection: TextDirection.ltr,
                        child: ListBody(
                          children: children,
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      );
    } finally {
      FlutterError.onError = oldHandler;
    }
    expect(errors, isNotEmpty);
    expect(errors.first.exception, isFlutterError);
184
    expect((errors.first.exception as FlutterError).toStringDeep(), equalsIgnoringHashCodes(
185 186 187
      '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
      '   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'
194
      '   This is relatively expensive, however.\n',
195 196
    ));
  });
197
}