flutter/packages/unit/test/fn3/render_object_widget_test.dart
Adam Barth b4ff5ca6ae Prototype of fn3
This patch contains a prototype of a new widget framework. In this framework,
Components can be reused in the tree as many times as the author desires. Also,
StatefulComponent is split into two pieces, a ComponentConfiguration and a
ComponentState. The ComponentConfiguration is created by the author and can be
reused as many times as desired. When mounted into the tree, the
ComponentConfiguration creates a ComponentState to hold the state for the
component. The state remains in the tree and cannot be reused.
2015-09-18 23:17:52 -07:00

161 lines
5.3 KiB
Dart

import 'package:sky/rendering.dart';
import 'package:sky/src/fn3.dart';
import 'package:test/test.dart';
import 'widget_tester.dart';
final BoxDecoration kBoxDecorationA = new BoxDecoration();
final BoxDecoration kBoxDecorationB = new BoxDecoration();
final BoxDecoration kBoxDecorationC = new BoxDecoration();
void main() {
test('RenderObjectWidget smoke test', () {
WidgetTester tester = new WidgetTester();
tester.pumpFrame(new DecoratedBox(decoration: kBoxDecorationA));
OneChildRenderObjectElement element =
tester.findElement((element) => element is OneChildRenderObjectElement);
expect(element, isNotNull);
expect(element.renderObject is RenderDecoratedBox, isTrue);
RenderDecoratedBox renderObject = element.renderObject;
expect(renderObject.decoration, equals(kBoxDecorationA));
expect(renderObject.position, equals(BoxDecorationPosition.background));
tester.pumpFrame(new DecoratedBox(decoration: kBoxDecorationB));
element = tester.findElement((element) => element is OneChildRenderObjectElement);
expect(element, isNotNull);
expect(element.renderObject is RenderDecoratedBox, isTrue);
renderObject = element.renderObject;
expect(renderObject.decoration, equals(kBoxDecorationB));
expect(renderObject.position, equals(BoxDecorationPosition.background));
});
test('RenderObjectWidget can add and remove children', () {
WidgetTester tester = new WidgetTester();
void checkFullTree() {
OneChildRenderObjectElement element =
tester.findElement((element) => element is OneChildRenderObjectElement);
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 =
tester.findElement((element) => element is OneChildRenderObjectElement);
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.pumpFrame(new DecoratedBox(
decoration: kBoxDecorationA,
child: new DecoratedBox(
decoration: kBoxDecorationB
)
));
checkFullTree();
tester.pumpFrame(new DecoratedBox(
decoration: kBoxDecorationA,
child: new TestComponent(
child: new DecoratedBox(
decoration: kBoxDecorationB
)
)
));
checkFullTree();
tester.pumpFrame(new DecoratedBox(
decoration: kBoxDecorationA,
child: new DecoratedBox(
decoration: kBoxDecorationB
)
));
checkFullTree();
tester.pumpFrame(new DecoratedBox(
decoration: kBoxDecorationA
));
childBareTree();
tester.pumpFrame(new DecoratedBox(
decoration: kBoxDecorationA,
child: new TestComponent(
child: new TestComponent(
child: new DecoratedBox(
decoration: kBoxDecorationB
)
)
)
));
checkFullTree();
tester.pumpFrame(new DecoratedBox(
decoration: kBoxDecorationA
));
childBareTree();
});
test('Detached render tree is intact', () {
WidgetTester tester = new WidgetTester();
tester.pumpFrame(new DecoratedBox(
decoration: kBoxDecorationA,
child: new DecoratedBox(
decoration: kBoxDecorationB,
child: new DecoratedBox(
decoration: kBoxDecorationC
)
)
));
OneChildRenderObjectElement element =
tester.findElement((element) => element is OneChildRenderObjectElement);
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.pumpFrame(new DecoratedBox(
decoration: kBoxDecorationA
));
element =
tester.findElement((element) => element is OneChildRenderObjectElement);
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);
});
}