Commit 4ad83fa0 authored by Adam Barth's avatar Adam Barth

Port some more tests to fn3

parent 96b0801c
...@@ -86,6 +86,13 @@ class WidgetTester { ...@@ -86,6 +86,13 @@ class WidgetTester {
return element?.state; return element?.state;
} }
State findStateByConfig(Widget config) {
StatefulComponentElement element = findElement((Element element) {
return element is StatefulComponentElement && element.state.config == config;
});
return element?.state;
}
Point getCenter(Element element) { Point getCenter(Element element) {
return _getElementPoint(element, (Size size) => size.center(Point.origin)); return _getElementPoint(element, (Size size) => size.center(Point.origin));
} }
......
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 TestState extends StatefulComponent { class TestWidget extends StatefulComponent {
TestState({ this.child, this.persistentState, this.syncedState }); TestWidget({ this.child, this.persistentState, this.syncedState });
Widget child;
final Widget child;
final int persistentState;
final int syncedState;
TestWidgetState createState() => new TestWidgetState();
}
class TestWidgetState extends State<TestWidget> {
int persistentState; int persistentState;
int syncedState; int syncedState;
int syncs = 0; int updates = 0;
void syncConstructorArguments(TestState source) {
child = source.child; void initState(BuildContext context) {
syncedState = source.syncedState; super.initState(context);
persistentState = config.persistentState;
syncedState = config.syncedState;
}
void didUpdateConfig(TestWidget oldConfig) {
syncedState = config.syncedState;
// we explicitly do NOT sync the persistentState from the new instance // we explicitly do NOT sync the persistentState from the new instance
// because we're using that to track whether we got recreated // because we're using that to track whether we got recreated
syncs += 1; updates += 1;
}
Widget build() {
return child;
} }
Widget build(BuildContext context) => config.child;
} }
void main() { void main() {
test('no change', () { test('no change', () {
WidgetTester tester = new WidgetTester(); WidgetTester tester = new WidgetTester();
tester.pumpFrame(() { tester.pumpFrame(
return new Container( new Container(
child: new Container( child: new Container(
child: new TestState( child: new TestWidget(
persistentState: 1, persistentState: 1,
child: new Container() child: new Container()
) )
) )
)
); );
});
TestState stateWidget = tester.findWidget((widget) => widget is TestState); TestWidgetState state = tester.findStateOfType(TestWidgetState);
expect(stateWidget.persistentState, equals(1)); expect(state.persistentState, equals(1));
expect(stateWidget.syncs, equals(0)); expect(state.updates, equals(0));
tester.pumpFrame(() { tester.pumpFrame(
return new Container( new Container(
child: new Container( child: new Container(
child: new TestState( child: new TestWidget(
persistentState: 2, persistentState: 2,
child: new Container() child: new Container()
) )
) )
)
); );
});
expect(stateWidget.persistentState, equals(1)); expect(state.persistentState, equals(1));
expect(stateWidget.syncs, equals(1)); expect(state.updates, equals(1));
tester.pumpFrame(new Container());
}); });
test('remove one', () { test('remove one', () {
WidgetTester tester = new WidgetTester(); WidgetTester tester = new WidgetTester();
tester.pumpFrame(() { tester.pumpFrame(
return new Container( new Container(
child: new Container( child: new Container(
child: new TestState( child: new TestWidget(
persistentState: 10, persistentState: 10,
child: new Container() child: new Container()
) )
) )
)
); );
});
TestState stateWidget = tester.findWidget((widget) => widget is TestState); TestWidgetState state = tester.findStateOfType(TestWidgetState);
expect(stateWidget.persistentState, equals(10)); expect(state.persistentState, equals(10));
expect(stateWidget.syncs, equals(0)); expect(state.updates, equals(0));
tester.pumpFrame(() { tester.pumpFrame(
return new Container( new Container(
child: new TestState( child: new TestWidget(
persistentState: 11, persistentState: 11,
child: new Container() child: new Container()
) )
)
); );
});
expect(stateWidget.persistentState, equals(10)); state = tester.findStateOfType(TestWidgetState);
expect(stateWidget.syncs, equals(1));
expect(state.persistentState, equals(11));
expect(state.updates, equals(0));
tester.pumpFrame(new Container());
}); });
test('swap instances around', () { test('swap instances around', () {
WidgetTester tester = new WidgetTester(); WidgetTester tester = new WidgetTester();
Widget a, b; Widget a = new TestWidget(persistentState: 0x61, syncedState: 0x41, child: new Text('apple'));
tester.pumpFrame(() { Widget b = new TestWidget(persistentState: 0x62, syncedState: 0x42, child: new Text('banana'));
a = new TestState(persistentState: 0x61, syncedState: 0x41, child: new Text('apple')); tester.pumpFrame(new Column([]));
b = new TestState(persistentState: 0x62, syncedState: 0x42, child: new Text('banana'));
return new Column([]);
});
GlobalKey keyA = new GlobalKey(); GlobalKey keyA = new GlobalKey();
GlobalKey keyB = new GlobalKey(); GlobalKey keyB = new GlobalKey();
TestState foundA, foundB; tester.pumpFrame(
new Column([
tester.pumpFrame(() {
return new Column([
new Container( new Container(
key: keyA, key: keyA,
child: a child: a
...@@ -118,21 +130,23 @@ void main() { ...@@ -118,21 +130,23 @@ void main() {
key: keyB, key: keyB,
child: b child: b
) )
]); ])
}); );
TestWidgetState first, second;
foundA = (tester.findWidget((widget) => widget.key == keyA) as Container).child as TestState; first = tester.findStateByConfig(a);
foundB = (tester.findWidget((widget) => widget.key == keyB) as Container).child as TestState; second = tester.findStateByConfig(b);
expect(foundA, equals(a)); expect(first.config, equals(a));
expect(foundA.persistentState, equals(0x61)); expect(first.persistentState, equals(0x61));
expect(foundA.syncedState, equals(0x41)); expect(first.syncedState, equals(0x41));
expect(foundB, equals(b)); expect(second.config, equals(b));
expect(foundB.persistentState, equals(0x62)); expect(second.persistentState, equals(0x62));
expect(foundB.syncedState, equals(0x42)); expect(second.syncedState, equals(0x42));
tester.pumpFrame(() { tester.pumpFrame(
return new Column([ new Column([
new Container( new Container(
key: keyA, key: keyA,
child: a child: a
...@@ -141,25 +155,25 @@ void main() { ...@@ -141,25 +155,25 @@ void main() {
key: keyB, key: keyB,
child: b child: b
) )
]); ])
}); );
foundA = (tester.findWidget((widget) => widget.key == keyA) as Container).child as TestState; first = tester.findStateByConfig(a);
foundB = (tester.findWidget((widget) => widget.key == keyB) as Container).child as TestState; second = tester.findStateByConfig(b);
// same as before // same as before
expect(foundA, equals(a)); expect(first.config, equals(a));
expect(foundA.persistentState, equals(0x61)); expect(first.persistentState, equals(0x61));
expect(foundA.syncedState, equals(0x41)); expect(first.syncedState, equals(0x41));
expect(foundB, equals(b)); expect(second.config, equals(b));
expect(foundB.persistentState, equals(0x62)); expect(second.persistentState, equals(0x62));
expect(foundB.syncedState, equals(0x42)); expect(second.syncedState, equals(0x42));
// now we swap the nodes over // now we swap the nodes over
// since they are both "old" nodes, they shouldn't sync with each other even though they look alike // since they are both "old" nodes, they shouldn't sync with each other even though they look alike
tester.pumpFrame(() { tester.pumpFrame(
return new Column([ new Column([
new Container( new Container(
key: keyA, key: keyA,
child: b child: b
...@@ -168,18 +182,18 @@ void main() { ...@@ -168,18 +182,18 @@ void main() {
key: keyB, key: keyB,
child: a child: a
) )
]); ])
}); );
foundA = (tester.findWidget((widget) => widget.key == keyA) as Container).child as TestState; first = tester.findStateByConfig(b);
foundB = (tester.findWidget((widget) => widget.key == keyB) as Container).child as TestState; second = tester.findStateByConfig(a);
expect(foundA, equals(b)); expect(first.config, equals(b));
expect(foundA.persistentState, equals(0x62)); expect(first.persistentState, equals(0x61));
expect(foundA.syncedState, equals(0x42)); expect(first.syncedState, equals(0x42));
expect(foundB, equals(a)); expect(second.config, equals(a));
expect(foundB.persistentState, equals(0x61)); expect(second.persistentState, equals(0x62));
expect(foundB.syncedState, equals(0x41)); expect(second.syncedState, equals(0x41));
}); });
......
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('Transform origin', () { test('Transform origin', () {
WidgetTester tester = new WidgetTester(); WidgetTester tester = new WidgetTester();
bool didReceiveTap = false; bool didReceiveTap = false;
tester.pumpFrame(() { tester.pumpFrame(
return new Stack([ new Stack([
new Positioned( new Positioned(
top: 100.0, top: 100.0,
left: 100.0, left: 100.0,
...@@ -39,8 +39,8 @@ void main() { ...@@ -39,8 +39,8 @@ void main() {
) )
) )
) )
]); ])
}); );
expect(didReceiveTap, isFalse); expect(didReceiveTap, isFalse);
tester.tapAt(new Point(110.0, 110.0)); tester.tapAt(new Point(110.0, 110.0));
......
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