render_object_widget_test.dart 7.26 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 = new BoxDecoration(); // ignore: prefer_const_constructor
final BoxDecoration kBoxDecorationB = new BoxDecoration(); // ignore: prefer_const_constructor
final BoxDecoration kBoxDecorationC = new BoxDecoration(); // ignore: prefer_const_constructor
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 23 24 25
class TestOrientedBox extends SingleChildRenderObjectWidget {
  TestOrientedBox({ Key key, Widget child }) : super(key: key, child: child);

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

  @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
46
void main() {
47 48
  testWidgets('RenderObjectWidget smoke test', (WidgetTester tester) async {
    await tester.pumpWidget(new 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(new 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
      SingleChildRenderObjectElement element =
70
          tester.firstElement(find.byElementType(SingleChildRenderObjectElement));
Adam Barth's avatar
Adam Barth committed
71 72 73 74
      expect(element, isNotNull);
      expect(element.renderObject is RenderDecoratedBox, isTrue);
      RenderDecoratedBox renderObject = element.renderObject;
      expect(renderObject.decoration, equals(kBoxDecorationA));
75
      expect(renderObject.position, equals(DecorationPosition.background));
76 77 78 79 80 81 82
      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
83

84 85 86
    void childBareTree() {
      SingleChildRenderObjectElement element =
          tester.element(find.byElementType(SingleChildRenderObjectElement));
Adam Barth's avatar
Adam Barth committed
87 88
      expect(element, isNotNull);
      expect(element.renderObject is RenderDecoratedBox, isTrue);
89 90
      RenderDecoratedBox renderObject = element.renderObject;
      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(new DecoratedBox(
96 97 98 99 100
      decoration: kBoxDecorationA,
      child: new DecoratedBox(
        decoration: kBoxDecorationB
      )
    ));
101

102
    checkFullTree();
103

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

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

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

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

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

128 129
    childBareTree();

130
    await tester.pumpWidget(new DecoratedBox(
131 132
      decoration: kBoxDecorationA,
      child: new TestWidget(
133
        child: new TestWidget(
134 135
          child: new DecoratedBox(
            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(new 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(new DecoratedBox(
153 154 155
      decoration: kBoxDecorationA,
      child: new DecoratedBox(
        decoration: kBoxDecorationB,
Adam Barth's avatar
Adam Barth committed
156
        child: new DecoratedBox(
157
          decoration: kBoxDecorationC
Adam Barth's avatar
Adam Barth committed
158
        )
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
      )
    ));

    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);

174
    await tester.pumpWidget(new 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
    Key boxKey = new UniqueKey();
    TestOrientedBox box = new TestOrientedBox(key: boxKey);
195

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

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

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

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