grid_view_layout_test.dart 2.29 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2015 The Chromium Authors. All rights reserved.
// 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: const BoxDecoration()),
      const DecoratedBox(decoration: const BoxDecoration()),
      const DecoratedBox(decoration: const BoxDecoration()),
      const DecoratedBox(decoration: const BoxDecoration()),
15 16
    ];

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

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

    for (RenderBox box in tester.renderObjectList<RenderBox>(find.byType(DecoratedBox))) {
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));
41 42 43 44 45
    expect(grid.size.width, equals(200.0), reason: "grid width");
    expect(grid.size.height, equals(200.0), reason: "grid height");

    expect(grid.debugNeedsLayout, false);

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

62
    for (RenderBox box in tester.renderObjectList<RenderBox>(find.byType(DecoratedBox))) {
63 64
      expect(box.size.width, equals(50.0), reason: "child width");
      expect(box.size.height, equals(50.0), reason: "child height");
65
    }
66 67 68 69 70

    expect(grid.size.width, equals(200.0), reason: "grid width");
    expect(grid.size.height, equals(50.0), reason: "grid height");
  });
}