render_object_widget_test.dart 5.87 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 13
import 'package:test/test.dart';

final BoxDecoration kBoxDecorationA = new BoxDecoration();
final BoxDecoration kBoxDecorationB = new BoxDecoration();
final BoxDecoration kBoxDecorationC = new BoxDecoration();

14 15 16
class TestComponent extends StatelessComponent {
  const TestComponent({ this.child });
  final Widget child;
17
  Widget build(BuildContext context) => child;
18 19
}

Adam Barth's avatar
Adam Barth committed
20 21
void main() {
  test('RenderObjectWidget smoke test', () {
22 23
    testWidgets((WidgetTester tester) {
      tester.pumpWidget(new DecoratedBox(decoration: kBoxDecorationA));
Adam Barth's avatar
Adam Barth committed
24
      OneChildRenderObjectElement element =
25
          tester.findElement((Element element) => element is OneChildRenderObjectElement);
Adam Barth's avatar
Adam Barth committed
26 27 28 29
      expect(element, isNotNull);
      expect(element.renderObject is RenderDecoratedBox, isTrue);
      RenderDecoratedBox renderObject = element.renderObject;
      expect(renderObject.decoration, equals(kBoxDecorationA));
30
      expect(renderObject.position, equals(DecorationPosition.background));
Adam Barth's avatar
Adam Barth committed
31

32
      tester.pumpWidget(new DecoratedBox(decoration: kBoxDecorationB));
33
      element = tester.findElement((Element element) => element is OneChildRenderObjectElement);
Adam Barth's avatar
Adam Barth committed
34 35
      expect(element, isNotNull);
      expect(element.renderObject is RenderDecoratedBox, isTrue);
36 37
      renderObject = element.renderObject;
      expect(renderObject.decoration, equals(kBoxDecorationB));
38
      expect(renderObject.position, equals(DecorationPosition.background));
39 40
    });
  });
Adam Barth's avatar
Adam Barth committed
41

42 43 44 45 46
  test('RenderObjectWidget can add and remove children', () {
    testWidgets((WidgetTester tester) {

      void checkFullTree() {
        OneChildRenderObjectElement element =
47
            tester.findElement((Element element) => element is OneChildRenderObjectElement);
48 49 50 51
        expect(element, isNotNull);
        expect(element.renderObject is RenderDecoratedBox, isTrue);
        RenderDecoratedBox renderObject = element.renderObject;
        expect(renderObject.decoration, equals(kBoxDecorationA));
52
        expect(renderObject.position, equals(DecorationPosition.background));
53 54 55 56
        expect(renderObject.child, isNotNull);
        expect(renderObject.child is RenderDecoratedBox, isTrue);
        RenderDecoratedBox child = renderObject.child;
        expect(child.decoration, equals(kBoxDecorationB));
57
        expect(child.position, equals(DecorationPosition.background));
58 59 60 61 62
        expect(child.child, isNull);
      }

      void childBareTree() {
        OneChildRenderObjectElement element =
63
            tester.findElement((Element element) => element is OneChildRenderObjectElement);
64 65 66 67
        expect(element, isNotNull);
        expect(element.renderObject is RenderDecoratedBox, isTrue);
        RenderDecoratedBox renderObject = element.renderObject;
        expect(renderObject.decoration, equals(kBoxDecorationA));
68
        expect(renderObject.position, equals(DecorationPosition.background));
69 70 71 72 73
        expect(renderObject.child, isNull);
      }

      tester.pumpWidget(new DecoratedBox(
        decoration: kBoxDecorationA,
Adam Barth's avatar
Adam Barth committed
74 75 76
        child: new DecoratedBox(
          decoration: kBoxDecorationB
        )
77 78 79 80 81 82 83 84 85 86 87 88
      ));

      checkFullTree();

      tester.pumpWidget(new DecoratedBox(
        decoration: kBoxDecorationA,
        child: new TestComponent(
          child: new DecoratedBox(
            decoration: kBoxDecorationB
          )
        )
      ));
Adam Barth's avatar
Adam Barth committed
89

90
      checkFullTree();
Adam Barth's avatar
Adam Barth committed
91

92 93 94 95 96 97
      tester.pumpWidget(new DecoratedBox(
        decoration: kBoxDecorationA,
        child: new DecoratedBox(
          decoration: kBoxDecorationB
        )
      ));
Adam Barth's avatar
Adam Barth committed
98

99
      checkFullTree();
Adam Barth's avatar
Adam Barth committed
100

101 102 103
      tester.pumpWidget(new DecoratedBox(
        decoration: kBoxDecorationA
      ));
Adam Barth's avatar
Adam Barth committed
104

105
      childBareTree();
Adam Barth's avatar
Adam Barth committed
106

107 108
      tester.pumpWidget(new DecoratedBox(
        decoration: kBoxDecorationA,
Adam Barth's avatar
Adam Barth committed
109
        child: new TestComponent(
110 111 112 113
          child: new TestComponent(
            child: new DecoratedBox(
              decoration: kBoxDecorationB
            )
Adam Barth's avatar
Adam Barth committed
114 115
          )
        )
116
      ));
Adam Barth's avatar
Adam Barth committed
117

118
      checkFullTree();
Adam Barth's avatar
Adam Barth committed
119

120 121 122
      tester.pumpWidget(new DecoratedBox(
        decoration: kBoxDecorationA
      ));
Adam Barth's avatar
Adam Barth committed
123

124 125
      childBareTree();
    });
Adam Barth's avatar
Adam Barth committed
126 127 128
  });

  test('Detached render tree is intact', () {
129
    testWidgets((WidgetTester tester) {
Adam Barth's avatar
Adam Barth committed
130

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

      OneChildRenderObjectElement element =
142
          tester.findElement((Element element) => element is OneChildRenderObjectElement);
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
      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);

      tester.pumpWidget(new DecoratedBox(
        decoration: kBoxDecorationA
      ));

      element =
158
          tester.findElement((Element element) => element is OneChildRenderObjectElement);
159 160 161 162 163 164 165 166 167 168 169
      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
170 171
  });
}