Commit 7d02880c authored by Adam Barth's avatar Adam Barth

Port some more tests to fn3

parent 06ffa759
...@@ -312,10 +312,9 @@ abstract class ScrollableWidgetList extends Scrollable { ...@@ -312,10 +312,9 @@ abstract class ScrollableWidgetList extends Scrollable {
assert(itemExtent != null); assert(itemExtent != null);
} }
EdgeDims padding; final bool itemsWrap;
bool itemsWrap; final double itemExtent;
double itemExtent; final EdgeDims padding;
Size containerSize = Size.zero;
} }
abstract class ScrollableWidgetListState<T extends ScrollableWidgetList> extends ScrollableState<T> { abstract class ScrollableWidgetListState<T extends ScrollableWidgetList> extends ScrollableState<T> {
...@@ -324,6 +323,8 @@ abstract class ScrollableWidgetListState<T extends ScrollableWidgetList> extends ...@@ -324,6 +323,8 @@ abstract class ScrollableWidgetListState<T extends ScrollableWidgetList> extends
int get itemCount; int get itemCount;
int _previousItemCount; int _previousItemCount;
Size _containerSize = Size.zero;
void didUpdateConfig(T oldConfig) { void didUpdateConfig(T oldConfig) {
super.didUpdateConfig(oldConfig); super.didUpdateConfig(oldConfig);
...@@ -351,13 +352,13 @@ abstract class ScrollableWidgetListState<T extends ScrollableWidgetList> extends ...@@ -351,13 +352,13 @@ abstract class ScrollableWidgetListState<T extends ScrollableWidgetList> extends
double get _containerExtent { double get _containerExtent {
return config.scrollDirection == ScrollDirection.vertical return config.scrollDirection == ScrollDirection.vertical
? config.containerSize.height ? _containerSize.height
: config.containerSize.width; : _containerSize.width;
} }
void _handleSizeChanged(Size newSize) { void _handleSizeChanged(Size newSize) {
setState(() { setState(() {
config.containerSize = newSize; _containerSize = newSize;
_updateScrollBehavior(); _updateScrollBehavior();
}); });
} }
...@@ -501,9 +502,11 @@ class PageableList<T> extends ScrollableList<T> { ...@@ -501,9 +502,11 @@ class PageableList<T> extends ScrollableList<T> {
padding: padding padding: padding
); );
Duration duration; final Duration duration;
Curve curve; final Curve curve;
PageChangedCallback pageChanged; final PageChangedCallback pageChanged;
PageableListState<T> createState() => new PageableListState();
} }
class PageableListState<T> extends ScrollableListState<T, PageableList<T>> { class PageableListState<T> extends ScrollableListState<T, PageableList<T>> {
......
...@@ -34,6 +34,21 @@ class WidgetTester { ...@@ -34,6 +34,21 @@ class WidgetTester {
} }
List<Layer> _layers(Layer layer) {
List<Layer> result = [layer];
if (layer is ContainerLayer) {
ContainerLayer root = layer;
Layer child = root.firstChild;
while(child != null) {
result.addAll(_layers(child));
child = child.nextSibling;
}
}
return result;
}
List<Layer> get layers => _layers(SkyBinding.instance.renderView.layer);
void walkElements(ElementVisitor visitor) { void walkElements(ElementVisitor visitor) {
void walk(Element element) { void walk(Element element) {
visitor(element); visitor(element);
...@@ -110,6 +125,18 @@ class WidgetTester { ...@@ -110,6 +125,18 @@ class WidgetTester {
_dispatchEvent(p.up(), result); _dispatchEvent(p.up(), result);
} }
void scroll(Element element, Offset offset, { int pointer: 1 }) {
Point startLocation = getCenter(element);
Point endLocation = startLocation + offset;
TestPointer p = new TestPointer(pointer);
// Events for the entire press-drag-release gesture are dispatched
// to the widgets "hit" by the pointer down event.
HitTestResult result = _hitTest(startLocation);
_dispatchEvent(p.down(startLocation), result);
_dispatchEvent(p.move(endLocation), result);
_dispatchEvent(p.up(), result);
}
void dispatchEvent(sky.Event event, Point location) { void dispatchEvent(sky.Event event, Point location) {
_dispatchEvent(event, _hitTest(location)); _dispatchEvent(event, _hitTest(location));
} }
......
import 'package:sky/rendering.dart'; import 'package:sky/rendering.dart';
import 'package:sky/widgets.dart'; import 'package:sky/src/fn3.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'widget_tester.dart'; import '../fn3/widget_tester.dart';
void main() { void main() {
test('LinearProgressIndicator changes when its value changes', () { test('LinearProgressIndicator changes when its value changes', () {
WidgetTester tester = new WidgetTester(); WidgetTester tester = new WidgetTester();
tester.pumpFrame(() { tester.pumpFrame(new Block([new LinearProgressIndicator(value: 0.0)]));
return new Block([new LinearProgressIndicator(value: 0.0)]);
});
List<Layer> layers1 = tester.layers; List<Layer> layers1 = tester.layers;
tester.pumpFrame(() { tester.pumpFrame(new Block([new LinearProgressIndicator(value: 0.5)]));
return new Block([new LinearProgressIndicator(value: 0.5)]);
});
List<Layer> layers2 = tester.layers;
List<Layer> layers2 = tester.layers;
expect(layers1, isNot(equals(layers2))); expect(layers1, isNot(equals(layers2)));
}); });
} }
...@@ -2,40 +2,55 @@ ...@@ -2,40 +2,55 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'package:sky/src/widgets/basic.dart'; import 'package:sky/src/fn3.dart';
import 'package:sky/src/widgets/framework.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'widget_tester.dart';
Changer changer; import '../fn3/widget_tester.dart';
ChangerState changer;
class Changer extends StatefulComponent { class Changer extends StatefulComponent {
Changer(this.child); Changer(this.child);
Widget child;
void syncConstructorArguments(Changer source) { final Widget child;
child = source.child;
} ChangerState createState() => new ChangerState();
}
class ChangerState extends State<Changer> {
bool _state = false; bool _state = false;
void initState() { changer = this; }
void initState(BuildContext context) {
super.initState(context);
changer = this;
}
void test() { setState(() { _state = true; }); } void test() { setState(() { _state = true; }); }
Widget build() => _state ? new Wrapper(child) : child;
Widget build(BuildContext) => _state ? new Wrapper(config.child) : config.child;
} }
class Wrapper extends Component { class Wrapper extends StatelessComponent {
Wrapper(this.child); Wrapper(this.child);
final Widget child; final Widget child;
Widget build() => child;
Widget build(BuildContext context) => child;
} }
class Leaf extends StatefulComponent { class Leaf extends StatefulComponent {
void syncConstructorArguments(Leaf source) { } LeafState createState() => new LeafState();
Widget build() => new Text("leaf"); }
class LeafState extends State<Leaf> {
Widget build(BuildContext context) => new Text("leaf");
} }
void main() { void main() {
test('three-way setState() smoke test', () { test('three-way setState() smoke test', () {
WidgetTester tester = new WidgetTester(); WidgetTester tester = new WidgetTester();
tester.pumpFrame(() => new Changer(new Wrapper(new Leaf()))); tester.pumpFrame(new Changer(new Wrapper(new Leaf())));
tester.pumpFrame(() => new Changer(new Wrapper(new Leaf()))); tester.pumpFrame(new Changer(new Wrapper(new Leaf())));
changer.test(); changer.test();
tester.pumpFrameWithoutChange(); tester.pumpFrameWithoutChange();
}); });
......
import 'package:sky/widgets.dart'; import 'package:sky/src/fn3.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import '../engine/mock_events.dart'; import '../engine/mock_events.dart';
import 'widget_tester.dart'; import '../fn3/widget_tester.dart';
class Inside extends StatefulComponent { class Inside extends StatefulComponent {
void syncConstructorArguments(Inside source) { InsideState createState() => new InsideState();
} }
Widget build() { class InsideState extends State<Inside> {
Widget build(BuildContext context) {
return new Listener( return new Listener(
onPointerDown: _handlePointerDown, onPointerDown: _handlePointerDown,
child: new Text('INSIDE') child: new Text('INSIDE')
...@@ -21,29 +22,32 @@ class Inside extends StatefulComponent { ...@@ -21,29 +22,32 @@ class Inside extends StatefulComponent {
} }
class Middle extends StatefulComponent { class Middle extends StatefulComponent {
Inside child;
Middle({ this.child }); Middle({ this.child });
void syncConstructorArguments(Middle source) { final Inside child;
child = source.child;
}
Widget build() { MiddleState createState() => new MiddleState();
}
class MiddleState extends State<Middle> {
Widget build(BuildContext context) {
return new Listener( return new Listener(
onPointerDown: _handlePointerDown, onPointerDown: _handlePointerDown,
child: child child: config.child
); );
} }
void _handlePointerDown(_) { void _handlePointerDown(_) {
setState(() { }); setState(() { });
} }
}
class Outside extends StatefulComponent {
OutsideState createState() => new OutsideState();
} }
class Outside extends App { class OutsideState extends State<Outside> {
Widget build() { Widget build(BuildContext context) {
return new Middle(child: new Inside()); return new Middle(child: new Inside());
} }
} }
...@@ -51,20 +55,12 @@ class Outside extends App { ...@@ -51,20 +55,12 @@ class Outside extends App {
void main() { void main() {
test('setState() smoke test', () { test('setState() smoke test', () {
WidgetTester tester = new WidgetTester(); WidgetTester tester = new WidgetTester();
tester.pumpFrame(new Outside());
tester.pumpFrame(() {
return new Outside();
});
TestPointer pointer = new TestPointer(1); TestPointer pointer = new TestPointer(1);
Point location = tester.getCenter(tester.findText('INSIDE')); Point location = tester.getCenter(tester.findText('INSIDE'));
tester.dispatchEvent(pointer.down(location), location); tester.dispatchEvent(pointer.down(location), location);
tester.pumpFrameWithoutChange(); tester.pumpFrameWithoutChange();
tester.dispatchEvent(pointer.up(), location); tester.dispatchEvent(pointer.up(), location);
tester.pumpFrameWithoutChange(); tester.pumpFrameWithoutChange();
}); });
} }
import 'package:sky/widgets.dart'; import 'package:sky/src/fn3.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'widget_tester.dart'; import '../fn3/widget_tester.dart';
void main() { void main() {
test('Can change position data', () { test('Can change position data', () {
WidgetTester tester = new WidgetTester(); WidgetTester tester = new WidgetTester();
tester.pumpFrame(() { Key key = new Key('container');
return new Stack([
tester.pumpFrame(
new Stack([
new Positioned( new Positioned(
left: 10.0, left: 10.0,
child: new Container( child: new Container(
key: key,
width: 10.0, width: 10.0,
height: 10.0 height: 10.0
) )
) )
]); ])
}); );
Container container = tester.findWidget((Widget widget) => widget is Container); Element container = tester.findElementByKey(key);
expect(container.renderObject.parentData.top, isNull); expect(container.renderObject.parentData.top, isNull);
expect(container.renderObject.parentData.right, isNull); expect(container.renderObject.parentData.right, isNull);
expect(container.renderObject.parentData.bottom, isNull); expect(container.renderObject.parentData.bottom, isNull);
expect(container.renderObject.parentData.left, equals(10.0)); expect(container.renderObject.parentData.left, equals(10.0));
tester.pumpFrame(() { tester.pumpFrame(
return new Stack([ new Stack([
new Positioned( new Positioned(
right: 10.0, right: 10.0,
child: new Container( child: new Container(
key: key,
width: 10.0, width: 10.0,
height: 10.0 height: 10.0
) )
) )
]); ])
}); );
container = tester.findWidget((Widget widget) => widget is Container); container = tester.findElementByKey(key);
expect(container.renderObject.parentData.top, isNull); expect(container.renderObject.parentData.top, isNull);
expect(container.renderObject.parentData.right, equals(10.0)); expect(container.renderObject.parentData.right, equals(10.0));
expect(container.renderObject.parentData.bottom, isNull); expect(container.renderObject.parentData.bottom, isNull);
...@@ -46,26 +50,25 @@ void main() { ...@@ -46,26 +50,25 @@ void main() {
test('Can remove parent data', () { test('Can remove parent data', () {
WidgetTester tester = new WidgetTester(); WidgetTester tester = new WidgetTester();
Container container;
tester.pumpFrame(() { Key key = new Key('container');
container = new Container(width: 10.0, height: 10.0); Container container = new Container(key: key, width: 10.0, height: 10.0);
return new Stack([ new Positioned(left: 10.0, child: container) ]);
});
expect(container.renderObject.parentData.top, isNull); tester.pumpFrame(new Stack([ new Positioned(left: 10.0, child: container) ]));
expect(container.renderObject.parentData.right, isNull); Element containerElement = tester.findElementByKey(key);
expect(container.renderObject.parentData.bottom, isNull);
expect(container.renderObject.parentData.left, equals(10.0));
tester.pumpFrame(() { expect(containerElement.renderObject.parentData.top, isNull);
return new Stack([ container ]); expect(containerElement.renderObject.parentData.right, isNull);
}); expect(containerElement.renderObject.parentData.bottom, isNull);
expect(containerElement.renderObject.parentData.left, equals(10.0));
expect(container.renderObject.parentData.top, isNull); tester.pumpFrame(new Stack([ container ]));
expect(container.renderObject.parentData.right, isNull); containerElement = tester.findElementByKey(key);
expect(container.renderObject.parentData.bottom, isNull);
expect(container.renderObject.parentData.left, isNull); expect(containerElement.renderObject.parentData.top, isNull);
expect(containerElement.renderObject.parentData.right, isNull);
expect(containerElement.renderObject.parentData.bottom, isNull);
expect(containerElement.renderObject.parentData.left, isNull);
}); });
} }
import 'package:sky/widgets.dart'; import 'package:sky/src/fn3.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'widget_tester.dart'; import '../fn3/widget_tester.dart';
class InnerComponent extends StatefulComponent { class InnerComponent extends StatefulComponent {
InnerComponent(); InnerComponent({ Key key }) : super(key: key);
InnerComponentState createState() => new InnerComponentState();
}
class InnerComponentState extends State<InnerComponent> {
bool _didInitState = false; bool _didInitState = false;
void initState() { void initState(BuildContext context) {
super.initState(context);
_didInitState = true; _didInitState = true;
} }
void syncConstructorArguments(InnerComponent source) { Widget build(BuildContext context) {
}
Widget build() {
return new Container(); return new Container();
} }
} }
class OuterContainer extends StatefulComponent { class OuterContainer extends StatefulComponent {
OuterContainer({ this.child }); OuterContainer({ Key key, this.child }) : super(key: key);
InnerComponent child; final InnerComponent child;
void syncConstructorArguments(OuterContainer source) { OuterContainerState createState() => new OuterContainerState();
child = source.child; }
}
Widget build() { class OuterContainerState extends State<OuterContainer> {
return child; Widget build(BuildContext context) {
return config.child;
} }
} }
...@@ -39,36 +40,42 @@ void main() { ...@@ -39,36 +40,42 @@ void main() {
WidgetTester tester = new WidgetTester(); WidgetTester tester = new WidgetTester();
InnerComponent inner1; Key innerKey = new Key('inner');
Key outerKey = new Key('outer');
InnerComponent inner1 = new InnerComponent(key: innerKey);
InnerComponent inner2; InnerComponent inner2;
OuterContainer outer; OuterContainer outer1 = new OuterContainer(key: outerKey, child: inner1);
OuterContainer outer2;
tester.pumpFrame(() { tester.pumpFrame(outer1);
inner1 = new InnerComponent();
outer = new OuterContainer(child: inner1);
return outer;
});
expect(inner1._didInitState, isTrue); StatefulComponentElement innerElement = tester.findElementByKey(innerKey);
expect(inner1.parent, isNotNull); InnerComponentState innerElementState = innerElement.state;
expect(innerElementState.config, equals(inner1));
expect(innerElementState._didInitState, isTrue);
expect(innerElement.renderObject.attached, isTrue);
tester.pumpFrame(() { inner2 = new InnerComponent(key: innerKey);
inner2 = new InnerComponent(); outer2 = new OuterContainer(key: outerKey, child: inner2);
return new OuterContainer(child: inner2);
});
expect(inner1._didInitState, isTrue); tester.pumpFrame(outer2);
expect(inner1.parent, isNotNull);
expect(inner2._didInitState, isFalse);
expect(inner2.parent, isNull);
outer.setState(() {}); expect(tester.findElementByKey(innerKey), equals(innerElement));
tester.pumpFrameWithoutChange(0.0); expect(innerElement.state, equals(innerElementState));
expect(inner1._didInitState, isTrue); expect(innerElementState.config, equals(inner2));
expect(inner1.parent, isNotNull); expect(innerElementState._didInitState, isTrue);
expect(inner2._didInitState, isFalse); expect(innerElement.renderObject.attached, isTrue);
expect(inner2.parent, isNull);
StatefulComponentElement outerElement = tester.findElementByKey(outerKey);
expect(outerElement.state.config, equals(outer2));
outerElement.state.setState(() {});
tester.pumpFrameWithoutChange(0.0);
expect(tester.findElementByKey(innerKey), equals(innerElement));
expect(innerElement.state, equals(innerElementState));
expect(innerElementState.config, equals(inner2));
expect(innerElement.renderObject.attached, isTrue);
}); });
} }
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