render_object_widget_test.dart 7.13 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4
// 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.

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 14
class TestWidget extends StatelessWidget {
  const TestWidget({ this.child });
15

16
  final Widget child;
17 18

  @override
19
  Widget build(BuildContext context) => child;
20 21
}

22
class TestOrientedBox extends SingleChildRenderObjectWidget {
23
  const TestOrientedBox({ Key key, Widget child }) : super(key: key, child: child);
24 25

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

  @override
38
  RenderDecoratedBox createRenderObject(BuildContext context) => RenderDecoratedBox(decoration: _getDecoration(context));
39 40 41 42 43 44 45

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

Adam Barth's avatar
Adam Barth committed
46
void main() {
47
  testWidgets('RenderObjectWidget smoke test', (WidgetTester tester) async {
48
    await tester.pumpWidget(DecoratedBox(decoration: kBoxDecorationA));
49 50 51 52 53 54 55 56
    SingleChildRenderObjectElement element =
        tester.element(find.byElementType(SingleChildRenderObjectElement));
    expect(element, isNotNull);
    expect(element.renderObject is RenderDecoratedBox, isTrue);
    RenderDecoratedBox renderObject = element.renderObject;
    expect(renderObject.decoration, equals(kBoxDecorationA));
    expect(renderObject.position, equals(DecorationPosition.background));

57
    await tester.pumpWidget(DecoratedBox(decoration: kBoxDecorationB));
58 59 60 61 62 63 64 65
    element = tester.element(find.byElementType(SingleChildRenderObjectElement));
    expect(element, isNotNull);
    expect(element.renderObject is RenderDecoratedBox, isTrue);
    renderObject = element.renderObject;
    expect(renderObject.decoration, equals(kBoxDecorationB));
    expect(renderObject.position, equals(DecorationPosition.background));
  });

66
  testWidgets('RenderObjectWidget can add and remove children', (WidgetTester tester) async {
67 68

    void checkFullTree() {
69
      final SingleChildRenderObjectElement element =
70
          tester.firstElement(find.byElementType(SingleChildRenderObjectElement));
Adam Barth's avatar
Adam Barth committed
71 72
      expect(element, isNotNull);
      expect(element.renderObject is RenderDecoratedBox, isTrue);
73
      final RenderDecoratedBox renderObject = element.renderObject;
Adam Barth's avatar
Adam Barth committed
74
      expect(renderObject.decoration, equals(kBoxDecorationA));
75
      expect(renderObject.position, equals(DecorationPosition.background));
76 77
      expect(renderObject.child, isNotNull);
      expect(renderObject.child is RenderDecoratedBox, isTrue);
78
      final RenderDecoratedBox child = renderObject.child;
79 80 81 82
      expect(child.decoration, equals(kBoxDecorationB));
      expect(child.position, equals(DecorationPosition.background));
      expect(child.child, isNull);
    }
Adam Barth's avatar
Adam Barth committed
83

84
    void childBareTree() {
85
      final SingleChildRenderObjectElement element =
86
          tester.element(find.byElementType(SingleChildRenderObjectElement));
Adam Barth's avatar
Adam Barth committed
87 88
      expect(element, isNotNull);
      expect(element.renderObject is RenderDecoratedBox, isTrue);
89
      final RenderDecoratedBox renderObject = element.renderObject;
90
      expect(renderObject.decoration, equals(kBoxDecorationA));
91
      expect(renderObject.position, equals(DecorationPosition.background));
92 93
      expect(renderObject.child, isNull);
    }
Adam Barth's avatar
Adam Barth committed
94

95
    await tester.pumpWidget(DecoratedBox(
96
      decoration: kBoxDecorationA,
97
      child: DecoratedBox(
98 99 100
        decoration: kBoxDecorationB
      )
    ));
101

102
    checkFullTree();
103

104
    await tester.pumpWidget(DecoratedBox(
105
      decoration: kBoxDecorationA,
106 107
      child: TestWidget(
        child: DecoratedBox(
108 109
          decoration: kBoxDecorationB
        )
110 111 112 113
      )
    ));

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

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

122
    checkFullTree();
Adam Barth's avatar
Adam Barth committed
123

124
    await tester.pumpWidget(DecoratedBox(
125 126
      decoration: kBoxDecorationA
    ));
Adam Barth's avatar
Adam Barth committed
127

128 129
    childBareTree();

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

141
    checkFullTree();
Adam Barth's avatar
Adam Barth committed
142

143
    await tester.pumpWidget(DecoratedBox(
144 145
      decoration: kBoxDecorationA
    ));
Adam Barth's avatar
Adam Barth committed
146

147
    childBareTree();
Adam Barth's avatar
Adam Barth committed
148 149
  });

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

152
    await tester.pumpWidget(DecoratedBox(
153
      decoration: kBoxDecorationA,
154
      child: DecoratedBox(
155
        decoration: kBoxDecorationB,
156
        child: DecoratedBox(
157
          decoration: kBoxDecorationC
Adam Barth's avatar
Adam Barth committed
158
        )
159 160 161 162 163 164
      )
    ));

    SingleChildRenderObjectElement element =
        tester.firstElement(find.byElementType(SingleChildRenderObjectElement));
    expect(element.renderObject is RenderDecoratedBox, isTrue);
165
    final RenderDecoratedBox parent = element.renderObject;
166
    expect(parent.child is RenderDecoratedBox, isTrue);
167
    final RenderDecoratedBox child = parent.child;
168 169
    expect(child.decoration, equals(kBoxDecorationB));
    expect(child.child is RenderDecoratedBox, isTrue);
170
    final RenderDecoratedBox grandChild = child.child;
171 172 173
    expect(grandChild.decoration, equals(kBoxDecorationC));
    expect(grandChild.child, isNull);

174
    await tester.pumpWidget(DecoratedBox(
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
      decoration: kBoxDecorationA
    ));

    element =
        tester.element(find.byElementType(SingleChildRenderObjectElement));
    expect(element.renderObject is RenderDecoratedBox, isTrue);
    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
190
  });
191

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

196
    await tester.pumpWidget(MediaQuery(
197
      data: const MediaQueryData(size: Size(400.0, 300.0)),
198 199
      child: box
    ));
200

201
    final RenderDecoratedBox renderBox = tester.renderObject(find.byKey(boxKey));
202
    BoxDecoration decoration = renderBox.decoration;
203
    expect(decoration.color, equals(const Color(0xFF00FF00)));
204

205
    await tester.pumpWidget(MediaQuery(
206
      data: const MediaQueryData(size: Size(300.0, 400.0)),
207 208
      child: box
    ));
209

210
    decoration = renderBox.decoration;
211
    expect(decoration.color, equals(const Color(0xFF0000FF)));
212
  });
Adam Barth's avatar
Adam Barth committed
213
}