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

import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

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

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

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

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

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

    expect(grid.debugNeedsLayout, false);

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

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

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