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