render_object_widget_test.dart 5.7 KB
Newer Older
1 2
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
Adam Barth's avatar
Adam Barth committed
3 4 5 6 7 8 9 10
import 'package:test/test.dart';

import 'widget_tester.dart';

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

11 12 13
class TestComponent extends StatelessComponent {
  const TestComponent({ this.child });
  final Widget child;
14
  Widget build(BuildContext context) => child;
15 16
}

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

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

39 40 41 42 43
  test('RenderObjectWidget can add and remove children', () {
    testWidgets((WidgetTester tester) {

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

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

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

      checkFullTree();

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

87
      checkFullTree();
Adam Barth's avatar
Adam Barth committed
88

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

96
      checkFullTree();
Adam Barth's avatar
Adam Barth committed
97

98 99 100
      tester.pumpWidget(new DecoratedBox(
        decoration: kBoxDecorationA
      ));
Adam Barth's avatar
Adam Barth committed
101

102
      childBareTree();
Adam Barth's avatar
Adam Barth committed
103

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

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

117 118 119
      tester.pumpWidget(new DecoratedBox(
        decoration: kBoxDecorationA
      ));
Adam Barth's avatar
Adam Barth committed
120

121 122
      childBareTree();
    });
Adam Barth's avatar
Adam Barth committed
123 124 125
  });

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

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

      OneChildRenderObjectElement element =
139
          tester.findElement((Element element) => element is OneChildRenderObjectElement);
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
      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 =
155
          tester.findElement((Element element) => element is OneChildRenderObjectElement);
156 157 158 159 160 161 162 163 164 165 166
      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
167 168
  });
}