render_object_widget_test.dart 7.34 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 12
final Border nullBorder = null; // we want these instances to be separate instances so that we're not just checking with a single object
final BoxDecoration kBoxDecorationA = new BoxDecoration(border: nullBorder);
final BoxDecoration kBoxDecorationB = new BoxDecoration(border: nullBorder);
final BoxDecoration kBoxDecorationC = new BoxDecoration(border: nullBorder);
Adam Barth's avatar
Adam Barth committed
13

14 15
class TestWidget extends StatelessWidget {
  const TestWidget({ this.child });
16

17
  final Widget child;
18 19

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

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

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

  @override
  RenderDecoratedBox createRenderObject(BuildContext context) => new RenderDecoratedBox(decoration: _getDecoration(context));

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

Adam Barth's avatar
Adam Barth committed
47
void main() {
48 49
  testWidgets('RenderObjectWidget smoke test', (WidgetTester tester) async {
    await tester.pumpWidget(new DecoratedBox(decoration: kBoxDecorationA));
50 51 52 53 54 55 56 57
    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));

58
    await tester.pumpWidget(new DecoratedBox(decoration: kBoxDecorationB));
59 60 61 62 63 64 65 66
    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));
  });

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

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

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

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

103
    checkFullTree();
104

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

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

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

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

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

129 130
    childBareTree();

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

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

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

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

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

153
    await tester.pumpWidget(new DecoratedBox(
154 155 156
      decoration: kBoxDecorationA,
      child: new DecoratedBox(
        decoration: kBoxDecorationB,
Adam Barth's avatar
Adam Barth committed
157
        child: new DecoratedBox(
158
          decoration: kBoxDecorationC
Adam Barth's avatar
Adam Barth committed
159
        )
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
      )
    ));

    SingleChildRenderObjectElement element =
        tester.firstElement(find.byElementType(SingleChildRenderObjectElement));
    expect(element.renderObject is RenderDecoratedBox, isTrue);
    RenderDecoratedBox parent = element.renderObject;
    expect(parent.child is RenderDecoratedBox, isTrue);
    RenderDecoratedBox child = parent.child;
    expect(child.decoration, equals(kBoxDecorationB));
    expect(child.child is RenderDecoratedBox, isTrue);
    RenderDecoratedBox grandChild = child.child;
    expect(grandChild.decoration, equals(kBoxDecorationC));
    expect(grandChild.child, isNull);

175
    await tester.pumpWidget(new DecoratedBox(
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
      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
191
  });
192

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

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

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

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

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