layout_builder_test.dart 5.54 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_test/flutter_test.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

void main() {
10
  testWidgets('LayoutBuilder parent size', (WidgetTester tester) async {
11 12
    Size layoutBuilderSize;
    Key childKey = new UniqueKey();
13
    Key parentKey = new UniqueKey();
14

15
    await tester.pumpWidget(
16
      new Center(
17 18
        child: new ConstrainedBox(
          constraints: const BoxConstraints(maxWidth: 100.0, maxHeight: 200.0),
19
          child: new LayoutBuilder(
20 21 22
            key: parentKey,
            builder: (BuildContext context, BoxConstraints constraints) {
              layoutBuilderSize = constraints.biggest;
23 24
              return new SizedBox(
                key: childKey,
25 26
                width: layoutBuilderSize.width / 2.0,
                height: layoutBuilderSize.height / 2.0
27 28 29 30 31 32 33 34
              );
            }
          )
        )
      )
    );

    expect(layoutBuilderSize, const Size(100.0, 200.0));
35 36 37 38
    RenderBox parentBox = tester.renderObject(find.byKey(parentKey));
    expect(parentBox.size, equals(const Size(50.0, 100.0)));
    RenderBox childBox = tester.renderObject(find.byKey(childKey));
    expect(childBox.size, equals(const Size(50.0, 100.0)));
39 40
  });

41
  testWidgets('LayoutBuilder stateful child', (WidgetTester tester) async {
42 43 44
    Size layoutBuilderSize;
    StateSetter setState;
    Key childKey = new UniqueKey();
45
    Key parentKey = new UniqueKey();
46 47 48
    double childWidth = 10.0;
    double childHeight = 20.0;

49
    await tester.pumpWidget(
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
      new Center(
        child: new LayoutBuilder(
          key: parentKey,
          builder: (BuildContext context, BoxConstraints constraints) {
            layoutBuilderSize = constraints.biggest;
            return new StatefulBuilder(
              builder: (BuildContext context, StateSetter setter) {
                setState = setter;
                return new SizedBox(
                  key: childKey,
                  width: childWidth,
                  height: childHeight
                );
              }
            );
          }
        )
67 68 69 70
      )
    );

    expect(layoutBuilderSize, equals(const Size(800.0, 600.0)));
71 72 73 74
    RenderBox parentBox = tester.renderObject(find.byKey(parentKey));
    expect(parentBox.size, equals(const Size(10.0, 20.0)));
    RenderBox childBox = tester.renderObject(find.byKey(childKey));
    expect(childBox.size, equals(const Size(10.0, 20.0)));
75 76 77 78 79

    setState(() {
      childWidth = 100.0;
      childHeight = 200.0;
    });
80
    await tester.pump();
81 82 83 84
    parentBox = tester.renderObject(find.byKey(parentKey));
    expect(parentBox.size, equals(const Size(100.0, 200.0)));
    childBox = tester.renderObject(find.byKey(childKey));
    expect(childBox.size, equals(const Size(100.0, 200.0)));
85 86
  });

87
  testWidgets('LayoutBuilder stateful parent', (WidgetTester tester) async {
88 89 90 91 92 93
    Size layoutBuilderSize;
    StateSetter setState;
    Key childKey = new UniqueKey();
    double childWidth = 10.0;
    double childHeight = 20.0;

94
    await tester.pumpWidget(
95 96 97 98 99 100 101 102
      new Center(
        child: new StatefulBuilder(
          builder: (BuildContext context, StateSetter setter) {
            setState = setter;
            return new SizedBox(
              width: childWidth,
              height: childHeight,
              child: new LayoutBuilder(
103 104
                builder: (BuildContext context, BoxConstraints constraints) {
                  layoutBuilderSize = constraints.biggest;
105 106
                  return new SizedBox(
                    key: childKey,
107 108
                    width: layoutBuilderSize.width,
                    height: layoutBuilderSize.height
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
                  );
                }
              )
            );
          }
        )
      )
    );

    expect(layoutBuilderSize, equals(const Size(10.0, 20.0)));
    RenderBox box = tester.renderObject(find.byKey(childKey));
    expect(box.size, equals(const Size(10.0, 20.0)));

    setState(() {
      childWidth = 100.0;
      childHeight = 200.0;
    });
126
    await tester.pump();
127 128 129
    box = tester.renderObject(find.byKey(childKey));
    expect(box.size, equals(const Size(100.0, 200.0)));
  });
130

131

132 133 134
  testWidgets('LayoutBuilder and Inherited -- do not rebuild when not using inherited', (WidgetTester tester) async {
    int built = 0;
    Widget target = new LayoutBuilder(
135
      builder: (BuildContext context, BoxConstraints constraints) {
136 137 138 139 140 141 142
        built += 1;
        return new Container();
      }
    );
    expect(built, 0);

    await tester.pumpWidget(new MediaQuery(
143
      data: const MediaQueryData(size: const Size(400.0, 300.0)),
144 145 146 147 148
      child: target
    ));
    expect(built, 1);

    await tester.pumpWidget(new MediaQuery(
149
      data: const MediaQueryData(size: const Size(300.0, 400.0)),
150 151 152 153 154 155 156 157
      child: target
    ));
    expect(built, 1);
  });

  testWidgets('LayoutBuilder and Inherited -- do rebuild when using inherited', (WidgetTester tester) async {
    int built = 0;
    Widget target = new LayoutBuilder(
158
      builder: (BuildContext context, BoxConstraints constraints) {
159 160 161 162 163 164 165 166
        built += 1;
        MediaQuery.of(context);
        return new Container();
      }
    );
    expect(built, 0);

    await tester.pumpWidget(new MediaQuery(
167
      data: const MediaQueryData(size: const Size(400.0, 300.0)),
168 169 170 171 172
      child: target
    ));
    expect(built, 1);

    await tester.pumpWidget(new MediaQuery(
173
      data: const MediaQueryData(size: const Size(300.0, 400.0)),
174 175 176 177
      child: target
    ));
    expect(built, 2);
  });
178
}