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

Adam Barth's avatar
Adam Barth committed
5
import 'package:flutter_test/flutter_test.dart';
6 7
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
Adam Barth's avatar
Adam Barth committed
8

9 10 11
final BoxDecoration kBoxDecorationA = BoxDecoration(border: nonconst(null));
final BoxDecoration kBoxDecorationB = BoxDecoration(border: nonconst(null));
final BoxDecoration kBoxDecorationC = BoxDecoration(border: nonconst(null));
Adam Barth's avatar
Adam Barth committed
12

13
class TestWidget extends StatelessWidget {
14 15 16 17
  const TestWidget({
    Key key,
    this.child,
  }) : super(key: key);
18

19
  final Widget child;
20 21

  @override
22
  Widget build(BuildContext context) => child;
23 24
}

25
class TestOrientedBox extends SingleChildRenderObjectWidget {
26
  const TestOrientedBox({ Key key, Widget child }) : super(key: key, child: child);
27 28

  Decoration _getDecoration(BuildContext context) {
29
    final Orientation orientation = MediaQuery.of(context).orientation;
pq's avatar
pq committed
30
    switch (orientation) {
31
      case Orientation.landscape:
32
        return const BoxDecoration(color: Color(0xFF00FF00));
33
      case Orientation.portrait:
34
        return const BoxDecoration(color: Color(0xFF0000FF));
35
    }
pq's avatar
pq committed
36
    assert(orientation != null);
pq's avatar
pq committed
37
    return null;
38 39 40
  }

  @override
41
  RenderDecoratedBox createRenderObject(BuildContext context) => RenderDecoratedBox(decoration: _getDecoration(context));
42 43 44 45 46 47 48

  @override
  void updateRenderObject(BuildContext context, RenderDecoratedBox renderObject) {
    renderObject.decoration = _getDecoration(context);
  }
}

Adam Barth's avatar
Adam Barth committed
49
void main() {
50
  testWidgets('RenderObjectWidget smoke test', (WidgetTester tester) async {
51
    await tester.pumpWidget(DecoratedBox(decoration: kBoxDecorationA));
52 53 54
    SingleChildRenderObjectElement element =
        tester.element(find.byElementType(SingleChildRenderObjectElement));
    expect(element, isNotNull);
Dan Field's avatar
Dan Field committed
55
    expect(element.renderObject, isA<RenderDecoratedBox>());
56
    RenderDecoratedBox renderObject = element.renderObject as RenderDecoratedBox;
57 58 59
    expect(renderObject.decoration, equals(kBoxDecorationA));
    expect(renderObject.position, equals(DecorationPosition.background));

60
    await tester.pumpWidget(DecoratedBox(decoration: kBoxDecorationB));
61 62
    element = tester.element(find.byElementType(SingleChildRenderObjectElement));
    expect(element, isNotNull);
Dan Field's avatar
Dan Field committed
63
    expect(element.renderObject, isA<RenderDecoratedBox>());
64
    renderObject = element.renderObject as RenderDecoratedBox;
65 66 67 68
    expect(renderObject.decoration, equals(kBoxDecorationB));
    expect(renderObject.position, equals(DecorationPosition.background));
  });

69
  testWidgets('RenderObjectWidget can add and remove children', (WidgetTester tester) async {
70 71

    void checkFullTree() {
72
      final SingleChildRenderObjectElement element =
73
          tester.firstElement(find.byElementType(SingleChildRenderObjectElement));
Adam Barth's avatar
Adam Barth committed
74
      expect(element, isNotNull);
Dan Field's avatar
Dan Field committed
75
      expect(element.renderObject, isA<RenderDecoratedBox>());
76
      final RenderDecoratedBox renderObject = element.renderObject as RenderDecoratedBox;
Adam Barth's avatar
Adam Barth committed
77
      expect(renderObject.decoration, equals(kBoxDecorationA));
78
      expect(renderObject.position, equals(DecorationPosition.background));
79
      expect(renderObject.child, isNotNull);
Dan Field's avatar
Dan Field committed
80
      expect(renderObject.child, isA<RenderDecoratedBox>());
81
      final RenderDecoratedBox child = renderObject.child as RenderDecoratedBox;
82 83 84 85
      expect(child.decoration, equals(kBoxDecorationB));
      expect(child.position, equals(DecorationPosition.background));
      expect(child.child, isNull);
    }
Adam Barth's avatar
Adam Barth committed
86

87
    void childBareTree() {
88
      final SingleChildRenderObjectElement element =
89
          tester.element(find.byElementType(SingleChildRenderObjectElement));
Adam Barth's avatar
Adam Barth committed
90
      expect(element, isNotNull);
Dan Field's avatar
Dan Field committed
91
      expect(element.renderObject, isA<RenderDecoratedBox>());
92
      final RenderDecoratedBox renderObject = element.renderObject as RenderDecoratedBox;
93
      expect(renderObject.decoration, equals(kBoxDecorationA));
94
      expect(renderObject.position, equals(DecorationPosition.background));
95 96
      expect(renderObject.child, isNull);
    }
Adam Barth's avatar
Adam Barth committed
97

98
    await tester.pumpWidget(DecoratedBox(
99
      decoration: kBoxDecorationA,
100
      child: DecoratedBox(
101
        decoration: kBoxDecorationB
102
      ),
103
    ));
104

105
    checkFullTree();
106

107
    await tester.pumpWidget(DecoratedBox(
108
      decoration: kBoxDecorationA,
109 110
      child: TestWidget(
        child: DecoratedBox(
111
          decoration: kBoxDecorationB
112 113
        ),
      ),
114 115 116
    ));

    checkFullTree();
Adam Barth's avatar
Adam Barth committed
117

118
    await tester.pumpWidget(DecoratedBox(
119
      decoration: kBoxDecorationA,
120
      child: DecoratedBox(
121
        decoration: kBoxDecorationB
122
      ),
123
    ));
Adam Barth's avatar
Adam Barth committed
124

125
    checkFullTree();
Adam Barth's avatar
Adam Barth committed
126

127
    await tester.pumpWidget(DecoratedBox(
128 129
      decoration: kBoxDecorationA
    ));
Adam Barth's avatar
Adam Barth committed
130

131 132
    childBareTree();

133
    await tester.pumpWidget(DecoratedBox(
134
      decoration: kBoxDecorationA,
135 136 137
      child: TestWidget(
        child: TestWidget(
          child: DecoratedBox(
138
            decoration: kBoxDecorationB
139 140 141
          ),
        ),
      ),
142
    ));
Adam Barth's avatar
Adam Barth committed
143

144
    checkFullTree();
Adam Barth's avatar
Adam Barth committed
145

146
    await tester.pumpWidget(DecoratedBox(
147 148
      decoration: kBoxDecorationA
    ));
Adam Barth's avatar
Adam Barth committed
149

150
    childBareTree();
Adam Barth's avatar
Adam Barth committed
151 152
  });

153
  testWidgets('Detached render tree is intact', (WidgetTester tester) async {
Adam Barth's avatar
Adam Barth committed
154

155
    await tester.pumpWidget(DecoratedBox(
156
      decoration: kBoxDecorationA,
157
      child: DecoratedBox(
158
        decoration: kBoxDecorationB,
159
        child: DecoratedBox(
160
          decoration: kBoxDecorationC
161 162
        ),
      ),
163 164 165 166
    ));

    SingleChildRenderObjectElement element =
        tester.firstElement(find.byElementType(SingleChildRenderObjectElement));
Dan Field's avatar
Dan Field committed
167
    expect(element.renderObject, isA<RenderDecoratedBox>());
168
    final RenderDecoratedBox parent = element.renderObject as RenderDecoratedBox;
Dan Field's avatar
Dan Field committed
169
    expect(parent.child, isA<RenderDecoratedBox>());
170
    final RenderDecoratedBox child = parent.child as RenderDecoratedBox;
171
    expect(child.decoration, equals(kBoxDecorationB));
Dan Field's avatar
Dan Field committed
172
    expect(child.child, isA<RenderDecoratedBox>());
173
    final RenderDecoratedBox grandChild = child.child as RenderDecoratedBox;
174 175 176
    expect(grandChild.decoration, equals(kBoxDecorationC));
    expect(grandChild.child, isNull);

177
    await tester.pumpWidget(DecoratedBox(
178 179 180 181 182
      decoration: kBoxDecorationA
    ));

    element =
        tester.element(find.byElementType(SingleChildRenderObjectElement));
Dan Field's avatar
Dan Field committed
183
    expect(element.renderObject, isA<RenderDecoratedBox>());
184 185 186 187 188 189 190 191 192
    expect(element.renderObject, equals(parent));
    expect(parent.child, isNull);

    expect(child.parent, isNull);
    expect(child.decoration, equals(kBoxDecorationB));
    expect(child.child, equals(grandChild));
    expect(grandChild.parent, equals(child));
    expect(grandChild.decoration, equals(kBoxDecorationC));
    expect(grandChild.child, isNull);
Adam Barth's avatar
Adam Barth committed
193
  });
194

195
  testWidgets('Can watch inherited widgets', (WidgetTester tester) async {
196 197
    final Key boxKey = UniqueKey();
    final TestOrientedBox box = TestOrientedBox(key: boxKey);
198

199
    await tester.pumpWidget(MediaQuery(
200
      data: const MediaQueryData(size: Size(400.0, 300.0)),
201
      child: box,
202
    ));
203

204
    final RenderDecoratedBox renderBox = tester.renderObject(find.byKey(boxKey));
205
    BoxDecoration decoration = renderBox.decoration as BoxDecoration;
206
    expect(decoration.color, equals(const Color(0xFF00FF00)));
207

208
    await tester.pumpWidget(MediaQuery(
209
      data: const MediaQueryData(size: Size(300.0, 400.0)),
210
      child: box,
211
    ));
212

213
    decoration = renderBox.decoration as BoxDecoration;
214
    expect(decoration.color, equals(const Color(0xFF0000FF)));
215
  });
Adam Barth's avatar
Adam Barth committed
216
}