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.
This commit is contained in:
160
packages/unit/test/fn3/render_object_widget_test.dart
Normal file
160
packages/unit/test/fn3/render_object_widget_test.dart
Normal file
@@ -0,0 +1,160 @@
|
||||
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);
|
||||
});
|
||||
}
|
||||
44
packages/unit/test/fn3/widget_tester.dart
Normal file
44
packages/unit/test/fn3/widget_tester.dart
Normal file
@@ -0,0 +1,44 @@
|
||||
import 'package:sky/src/fn3/framework.dart';
|
||||
|
||||
class TestComponent extends Component {
|
||||
TestComponent({ this.child });
|
||||
final Widget child;
|
||||
Widget build() => child;
|
||||
}
|
||||
|
||||
class WidgetTester {
|
||||
ComponentElement _rootElement;
|
||||
|
||||
void walkElements(ElementVisitor visitor) {
|
||||
void walk(Element element) {
|
||||
visitor(element);
|
||||
element.visitChildren(walk);
|
||||
}
|
||||
|
||||
_rootElement.visitChildren(walk);
|
||||
}
|
||||
|
||||
Element findElement(bool predicate(Element widget)) {
|
||||
try {
|
||||
walkElements((Element widget) {
|
||||
if (predicate(widget))
|
||||
throw widget;
|
||||
});
|
||||
} catch (e) {
|
||||
if (e is Element)
|
||||
return e;
|
||||
rethrow;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
void pumpFrame(Widget widget) {
|
||||
if (_rootElement == null) {
|
||||
_rootElement = new ComponentElement(new TestComponent(child: widget));
|
||||
_rootElement.mount(null);
|
||||
} else {
|
||||
_rootElement.update(new TestComponent(child: widget), null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user