Commit cd6c4da3 authored by Hixie's avatar Hixie

Refactor rendering_tester

RenderView has to be a singleton for sanity during tests, otherwise they
all end up in the dirty lists and we end up pumping all of them each frame.
parent 4c79cc22
...@@ -5,7 +5,6 @@ import '../../../../examples/rendering/sector_layout.dart'; ...@@ -5,7 +5,6 @@ import '../../../../examples/rendering/sector_layout.dart';
void main() { void main() {
test('Sector layout can paint', () { test('Sector layout can paint', () {
RenderingTester tester = new RenderingTester(root: buildSectorExample()); layout(buildSectorExample(), phase: EnginePhase.composite);
tester.pumpFrame();
}); });
} }
...@@ -13,7 +13,7 @@ void main() { ...@@ -13,7 +13,7 @@ void main() {
]; ];
RenderGrid grid = new RenderGrid(children: children, maxChildExtent: 100.0); RenderGrid grid = new RenderGrid(children: children, maxChildExtent: 100.0);
RenderingTester tester = layout(grid, constraints: const BoxConstraints(maxWidth: 200.0)); layout(grid, constraints: const BoxConstraints(maxWidth: 200.0));
children.forEach((child) { children.forEach((child) {
expect(child.size.width, equals(100.0), reason: "child width"); expect(child.size.width, equals(100.0), reason: "child width");
...@@ -27,7 +27,7 @@ void main() { ...@@ -27,7 +27,7 @@ void main() {
grid.maxChildExtent = 60.0; grid.maxChildExtent = 60.0;
expect(grid.needsLayout, equals(true)); expect(grid.needsLayout, equals(true));
tester.pumpFrame(phase: EnginePhase.layout); pumpFrame();
children.forEach((child) { children.forEach((child) {
expect(child.size.width, equals(50.0), reason: "child width"); expect(child.size.width, equals(50.0), reason: "child width");
......
...@@ -22,19 +22,19 @@ void main() { ...@@ -22,19 +22,19 @@ void main() {
child: new RenderDecoratedBox(decoration: new BoxDecoration()) child: new RenderDecoratedBox(decoration: new BoxDecoration())
); );
RenderPositionedBox positioner = new RenderPositionedBox(child: sizer, shrinkWrap: ShrinkWrap.width); RenderPositionedBox positioner = new RenderPositionedBox(child: sizer, shrinkWrap: ShrinkWrap.width);
RenderingTester tester = layout(positioner, constraints: new BoxConstraints.loose(new Size(200.0, 200.0))); layout(positioner, constraints: new BoxConstraints.loose(new Size(200.0, 200.0)));
expect(positioner.size.width, equals(100.0), reason: "positioner width"); expect(positioner.size.width, equals(100.0), reason: "positioner width");
expect(positioner.size.height, equals(200.0), reason: "positioner height"); expect(positioner.size.height, equals(200.0), reason: "positioner height");
positioner.shrinkWrap = ShrinkWrap.height; positioner.shrinkWrap = ShrinkWrap.height;
tester.pumpFrame(phase: EnginePhase.layout); pumpFrame();
expect(positioner.size.width, equals(200.0), reason: "positioner width"); expect(positioner.size.width, equals(200.0), reason: "positioner width");
expect(positioner.size.height, equals(100.0), reason: "positioner height"); expect(positioner.size.height, equals(100.0), reason: "positioner height");
positioner.shrinkWrap = ShrinkWrap.both; positioner.shrinkWrap = ShrinkWrap.both;
tester.pumpFrame(phase: EnginePhase.layout); pumpFrame();
expect(positioner.size.width, equals(100.0), reason: "positioner width"); expect(positioner.size.width, equals(100.0), reason: "positioner width");
expect(positioner.size.height, equals(100.0), reason: "positioner height"); expect(positioner.size.height, equals(100.0), reason: "positioner height");
......
...@@ -3,7 +3,7 @@ import 'package:sky/rendering.dart'; ...@@ -3,7 +3,7 @@ import 'package:sky/rendering.dart';
const Size _kTestViewSize = const Size(800.0, 600.0); const Size _kTestViewSize = const Size(800.0, 600.0);
class TestRenderView extends RenderView { class TestRenderView extends RenderView {
TestRenderView({ RenderBox child }) : super(child: child) { TestRenderView() {
attach(); attach();
rootConstraints = new ViewConstraints(size: _kTestViewSize); rootConstraints = new ViewConstraints(size: _kTestViewSize);
scheduleInitialLayout(); scheduleInitialLayout();
...@@ -17,26 +17,18 @@ enum EnginePhase { ...@@ -17,26 +17,18 @@ enum EnginePhase {
composite composite
} }
class RenderingTester { RenderView _renderView;
RenderingTester({ RenderBox root }) { RenderView get renderView => _renderView;
renderView = new TestRenderView(child: root);
}
RenderView renderView; void layout(RenderBox box, { BoxConstraints constraints, EnginePhase phase: EnginePhase.layout }) {
assert(box != null); // if you want to just repump the last box, call pumpFrame().
void pumpFrame({ EnginePhase phase: EnginePhase.composite }) {
RenderObject.flushLayout(); if (renderView == null)
if (phase == EnginePhase.layout) _renderView = new TestRenderView();
return;
renderView.updateCompositingBits(); if (renderView.child != null)
RenderObject.flushPaint(); renderView.child = null;
if (phase == EnginePhase.paint)
return;
renderView.compositeFrame();
}
}
RenderingTester layout(RenderBox box, { BoxConstraints constraints }) {
if (constraints != null) { if (constraints != null) {
box = new RenderPositionedBox( box = new RenderPositionedBox(
child: new RenderConstrainedBox( child: new RenderConstrainedBox(
...@@ -46,7 +38,18 @@ RenderingTester layout(RenderBox box, { BoxConstraints constraints }) { ...@@ -46,7 +38,18 @@ RenderingTester layout(RenderBox box, { BoxConstraints constraints }) {
); );
} }
RenderingTester tester = new RenderingTester(root: box); renderView.child = box;
tester.pumpFrame(phase: EnginePhase.layout);
return tester; pumpFrame(phase: phase);
}
void pumpFrame({ EnginePhase phase: EnginePhase.layout }) {
RenderObject.flushLayout();
if (phase == EnginePhase.layout)
return;
renderView.updateCompositingBits();
RenderObject.flushPaint();
if (phase == EnginePhase.paint)
return;
renderView.compositeFrame();
} }
...@@ -15,16 +15,16 @@ void main() { ...@@ -15,16 +15,16 @@ void main() {
child: size); child: size);
RenderViewport viewport = new RenderViewport(child: red, scrollOffset: new Offset(0.0, -10.0)); RenderViewport viewport = new RenderViewport(child: red, scrollOffset: new Offset(0.0, -10.0));
RenderingTester tester = layout(viewport); layout(viewport);
HitTestResult result; HitTestResult result;
result = new HitTestResult(); result = new HitTestResult();
tester.renderView.hitTest(result, position: new Point(15.0, 0.0)); renderView.hitTest(result, position: new Point(15.0, 0.0));
expect(result.path.first.target, equals(viewport)); expect(result.path.first.target, equals(viewport));
result = new HitTestResult(); result = new HitTestResult();
tester.renderView.hitTest(result, position: new Point(15.0, 15.0)); renderView.hitTest(result, position: new Point(15.0, 15.0));
expect(result.path.first.target, equals(size)); expect(result.path.first.target, equals(size));
}); });
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment