grid_view_layout_test.dart 2.26 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
// @dart = 2.8

7 8 9 10 11
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('Empty GridView', (WidgetTester tester) async {
12
    final List<Widget> children = <Widget>[
13 14 15 16
      const DecoratedBox(decoration: BoxDecoration()),
      const DecoratedBox(decoration: BoxDecoration()),
      const DecoratedBox(decoration: BoxDecoration()),
      const DecoratedBox(decoration: BoxDecoration()),
17 18
    ];

19
    await tester.pumpWidget(
20
      Directionality(
21
        textDirection: TextDirection.ltr,
22 23
        child: Center(
          child: Container(
24
            width: 200.0,
25
            child: GridView.extent(
26 27 28 29 30
              maxCrossAxisExtent: 100.0,
              shrinkWrap: true,
              children: children,
            ),
          ),
31 32
        ),
      ),
33
    );
34

35 36
    expect(tester.renderObjectList<RenderBox>(find.byType(DecoratedBox)), hasLength(4));

37
    for (final RenderBox box in tester.renderObjectList<RenderBox>(find.byType(DecoratedBox))) {
Ian Hickson's avatar
Ian Hickson committed
38 39
      expect(box.size.width, equals(100.0), reason: 'child width');
      expect(box.size.height, equals(100.0), reason: 'child height');
40
    }
41

42
    final RenderBox grid = tester.renderObject(find.byType(GridView));
Ian Hickson's avatar
Ian Hickson committed
43 44
    expect(grid.size.width, equals(200.0), reason: 'grid width');
    expect(grid.size.height, equals(200.0), reason: 'grid height');
45 46 47

    expect(grid.debugNeedsLayout, false);

48
    await tester.pumpWidget(
49
      Directionality(
50
        textDirection: TextDirection.ltr,
51 52
        child: Center(
          child: Container(
53
            width: 200.0,
54
            child: GridView.extent(
55 56 57 58 59
              maxCrossAxisExtent: 60.0,
              shrinkWrap: true,
              children: children,
            ),
          ),
60 61
        ),
      ),
62
    );
63

64
    for (final RenderBox box in tester.renderObjectList<RenderBox>(find.byType(DecoratedBox))) {
Ian Hickson's avatar
Ian Hickson committed
65 66
      expect(box.size.width, equals(50.0), reason: 'child width');
      expect(box.size.height, equals(50.0), reason: 'child height');
67
    }
68

Ian Hickson's avatar
Ian Hickson committed
69 70
    expect(grid.size.width, equals(200.0), reason: 'grid width');
    expect(grid.size.height, equals(50.0), reason: 'grid height');
71 72
  });
}