Handle Widget instances being moved as-is to different parts of the tree.

This commit is contained in:
Hixie
2015-09-01 14:32:14 -07:00
parent 4d84e1b732
commit 6cac226fa6
4 changed files with 236 additions and 34 deletions

View File

@@ -43,4 +43,29 @@ void main() {
expect(container.renderObject.parentData.bottom, isNull);
expect(container.renderObject.parentData.left, isNull);
});
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) ]);
});
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([ container ]);
});
expect(container.renderObject.parentData.top, isNull);
expect(container.renderObject.parentData.right, isNull);
expect(container.renderObject.parentData.bottom, isNull);
expect(container.renderObject.parentData.left, isNull);
});
}

View File

@@ -4,13 +4,15 @@ import 'package:test/test.dart';
import 'widget_tester.dart';
class TestState extends StatefulComponent {
TestState({this.child, this.state});
TestState({ this.child, this.persistentState, this.syncedState });
Widget child;
int state;
int persistentState;
int syncedState;
int syncs = 0;
void syncConstructorArguments(TestState source) {
child = source.child;
// we explicitly do NOT sync the state from the new instance
syncedState = source.syncedState;
// we explicitly do NOT sync the persistentState from the new instance
// because we're using that to track whether we got recreated
syncs += 1;
}
@@ -29,7 +31,7 @@ void main() {
return new Container(
child: new Container(
child: new TestState(
state: 1,
persistentState: 1,
child: new Container()
)
)
@@ -38,21 +40,21 @@ void main() {
TestState stateWidget = tester.findWidget((widget) => widget is TestState);
expect(stateWidget.state, equals(1));
expect(stateWidget.persistentState, equals(1));
expect(stateWidget.syncs, equals(0));
tester.pumpFrame(() {
return new Container(
child: new Container(
child: new TestState(
state: 2,
persistentState: 2,
child: new Container()
)
)
);
});
expect(stateWidget.state, equals(1));
expect(stateWidget.persistentState, equals(1));
expect(stateWidget.syncs, equals(1));
});
@@ -92,4 +94,94 @@ void main() {
//
// });
test('swap instances around', () {
WidgetTester tester = new WidgetTester();
Widget a, b;
tester.pumpFrame(() {
a = new TestState(persistentState: 0x61, syncedState: 0x41, child: new Text('apple'));
b = new TestState(persistentState: 0x62, syncedState: 0x42, child: new Text('banana'));
return new Column([]);
});
GlobalKey keyA = new GlobalKey();
GlobalKey keyB = new GlobalKey();
TestState foundA, foundB;
tester.pumpFrame(() {
return new Column([
new Container(
key: keyA,
child: a
),
new Container(
key: keyB,
child: b
)
]);
});
foundA = (tester.findWidget((widget) => widget.key == keyA) as Container).child as TestState;
foundB = (tester.findWidget((widget) => widget.key == keyB) as Container).child as TestState;
expect(foundA, equals(a));
expect(foundA.persistentState, equals(0x61));
expect(foundA.syncedState, equals(0x41));
expect(foundB, equals(b));
expect(foundB.persistentState, equals(0x62));
expect(foundB.syncedState, equals(0x42));
tester.pumpFrame(() {
return new Column([
new Container(
key: keyA,
child: a
),
new Container(
key: keyB,
child: b
)
]);
});
foundA = (tester.findWidget((widget) => widget.key == keyA) as Container).child as TestState;
foundB = (tester.findWidget((widget) => widget.key == keyB) as Container).child as TestState;
// same as before
expect(foundA, equals(a));
expect(foundA.persistentState, equals(0x61));
expect(foundA.syncedState, equals(0x41));
expect(foundB, equals(b));
expect(foundB.persistentState, equals(0x62));
expect(foundB.syncedState, equals(0x42));
// now we swap the nodes over
// since they are both "old" nodes, they shouldn't sync with each other even though they look alike
tester.pumpFrame(() {
return new Column([
new Container(
key: keyA,
child: b
),
new Container(
key: keyB,
child: a
)
]);
});
foundA = (tester.findWidget((widget) => widget.key == keyA) as Container).child as TestState;
foundB = (tester.findWidget((widget) => widget.key == keyB) as Container).child as TestState;
expect(foundA, equals(b));
expect(foundA.persistentState, equals(0x62));
expect(foundA.syncedState, equals(0x42));
expect(foundB, equals(a));
expect(foundB.persistentState, equals(0x61));
expect(foundB.syncedState, equals(0x41));
});
}