Commit fbd5460b authored by Adam Barth's avatar Adam Barth

Integrate WidgetTester with FakeAsync

Fixes #1084
parent 67feb40c
...@@ -5,23 +5,22 @@ import 'widget_tester.dart'; ...@@ -5,23 +5,22 @@ import 'widget_tester.dart';
void main() { void main() {
test('Align smoke test', () { test('Align smoke test', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.pumpWidget(
tester.pumpFrame( new Align(
new Align( child: new Container(),
child: new Container(), horizontal: 0.75,
horizontal: 0.75, vertical: 0.75
vertical: 0.75 )
) );
);
tester.pumpFrame(
new Align(
child: new Container(),
horizontal: 0.5,
vertical: 0.5
)
);
tester.pumpWidget(
new Align(
child: new Container(),
horizontal: 0.5,
vertical: 0.5
)
);
});
}); });
} }
...@@ -8,56 +8,56 @@ final Key blockKey = new Key('test'); ...@@ -8,56 +8,56 @@ final Key blockKey = new Key('test');
void main() { void main() {
test('Cannot scroll a non-overflowing block', () { test('Cannot scroll a non-overflowing block', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.pumpWidget(
tester.pumpFrame( new Block([
new Block([ new Container(
new Container( height: 200.0, // less than 600, the height of the test area
height: 200.0, // less than 600, the height of the test area child: new Text('Hello')
child: new Text('Hello') )
) ],
], key: blockKey)
key: blockKey) );
); tester.pump(); // for SizeObservers
tester.pumpFrameWithoutChange(); // for SizeObservers
Point middleOfContainer = tester.getCenter(tester.findText('Hello'));
Point middleOfContainer = tester.getCenter(tester.findText('Hello')); Point target = tester.getCenter(tester.findElementByKey(blockKey));
Point target = tester.getCenter(tester.findElementByKey(blockKey)); TestPointer pointer = new TestPointer();
TestPointer pointer = new TestPointer(); tester.dispatchEvent(pointer.down(target), target);
tester.dispatchEvent(pointer.down(target), target); tester.dispatchEvent(pointer.move(target + const Offset(0.0, -10.0)), target);
tester.dispatchEvent(pointer.move(target + const Offset(0.0, -10.0)), target);
tester.pump(const Duration(milliseconds: 1));
tester.pumpFrameWithoutChange(1.0);
expect(tester.getCenter(tester.findText('Hello')) == middleOfContainer, isTrue);
expect(tester.getCenter(tester.findText('Hello')) == middleOfContainer, isTrue);
tester.dispatchEvent(pointer.up(), target);
tester.dispatchEvent(pointer.up(), target); });
}); });
test('Can scroll an overflowing block', () { test('Can scroll an overflowing block', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.pumpWidget(
tester.pumpFrame( new Block([
new Block([ new Container(
new Container( height: 2000.0, // more than 600, the height of the test area
height: 2000.0, // more than 600, the height of the test area child: new Text('Hello')
child: new Text('Hello') )
) ],
], key: blockKey)
key: blockKey) );
); tester.pump(); // for SizeObservers
tester.pumpFrameWithoutChange(); // for SizeObservers
Point middleOfContainer = tester.getCenter(tester.findText('Hello'));
Point middleOfContainer = tester.getCenter(tester.findText('Hello')); Point target = tester.getCenter(tester.findElementByKey(blockKey));
Point target = tester.getCenter(tester.findElementByKey(blockKey)); TestPointer pointer = new TestPointer();
TestPointer pointer = new TestPointer(); tester.dispatchEvent(pointer.down(target), target);
tester.dispatchEvent(pointer.down(target), target); tester.dispatchEvent(pointer.move(target + const Offset(0.0, -10.0)), target);
tester.dispatchEvent(pointer.move(target + const Offset(0.0, -10.0)), target);
tester.pump(const Duration(milliseconds: 1));
tester.pumpFrameWithoutChange(1.0);
expect(tester.getCenter(tester.findText('Hello')) == middleOfContainer, isFalse);
expect(tester.getCenter(tester.findText('Hello')) == middleOfContainer, isFalse);
tester.dispatchEvent(pointer.up(), target);
tester.dispatchEvent(pointer.up(), target); });
}); });
} }
...@@ -6,18 +6,17 @@ import 'widget_tester.dart'; ...@@ -6,18 +6,17 @@ import 'widget_tester.dart';
void main() { void main() {
test('Circles can have uniform borders', () { test('Circles can have uniform borders', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.pumpWidget(
tester.pumpFrame( new Container(
new Container( padding: new EdgeDims.all(50.0),
padding: new EdgeDims.all(50.0), decoration: new BoxDecoration(
decoration: new BoxDecoration( shape: Shape.circle,
shape: Shape.circle, border: new Border.all(width: 10.0, color: const Color(0x80FF00FF)),
border: new Border.all(width: 10.0, color: const Color(0x80FF00FF)), backgroundColor: Colors.teal[600]
backgroundColor: Colors.teal[600] )
) )
) );
); });
}); });
} }
...@@ -80,47 +80,47 @@ void main() { ...@@ -80,47 +80,47 @@ void main() {
}); });
test('Legal times for setState', () { test('Legal times for setState', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
GlobalKey flipKey = new GlobalKey();
GlobalKey flipKey = new GlobalKey(); expect(ProbeWidgetState.buildCount, equals(0));
expect(ProbeWidgetState.buildCount, equals(0)); tester.pumpWidget(new ProbeWidget());
tester.pumpFrame(new ProbeWidget()); expect(ProbeWidgetState.buildCount, equals(1));
expect(ProbeWidgetState.buildCount, equals(1)); tester.pumpWidget(new ProbeWidget());
tester.pumpFrame(new ProbeWidget()); expect(ProbeWidgetState.buildCount, equals(2));
expect(ProbeWidgetState.buildCount, equals(2)); tester.pumpWidget(new FlipComponent(
tester.pumpFrame(new FlipComponent( key: flipKey,
key: flipKey, left: new Container(),
left: new Container(), right: new ProbeWidget()
right: new ProbeWidget() ));
)); expect(ProbeWidgetState.buildCount, equals(2));
expect(ProbeWidgetState.buildCount, equals(2)); (flipKey.currentState as FlipComponentState).flip();
(flipKey.currentState as FlipComponentState).flip(); tester.pump();
tester.pumpFrameWithoutChange(); expect(ProbeWidgetState.buildCount, equals(3));
expect(ProbeWidgetState.buildCount, equals(3)); (flipKey.currentState as FlipComponentState).flip();
(flipKey.currentState as FlipComponentState).flip(); tester.pump();
tester.pumpFrameWithoutChange(); expect(ProbeWidgetState.buildCount, equals(3));
expect(ProbeWidgetState.buildCount, equals(3)); tester.pumpWidget(new Container());
tester.pumpFrame(new Container()); expect(ProbeWidgetState.buildCount, equals(3));
expect(ProbeWidgetState.buildCount, equals(3)); });
}); });
test('Setting parent state during build is forbidden', () { test('Setting parent state during build is forbidden', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
expect(cachedException, isNull);
expect(cachedException, isNull); tester.pumpWidget(new BadWidgetParent());
tester.pumpFrame(new BadWidgetParent()); expect(cachedException, isNotNull);
expect(cachedException, isNotNull); cachedException = null;
cachedException = null; tester.pumpWidget(new Container());
tester.pumpFrame(new Container()); expect(cachedException, isNull);
expect(cachedException, isNull); });
}); });
test('Setting state during dispose is forbidden', () { test('Setting state during dispose is forbidden', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.pumpWidget(new BadDisposeWidget());
tester.pumpFrame(new BadDisposeWidget()); expect(() {
expect(() { tester.pumpWidget(new Container());
tester.pumpFrame(new Container()); }, throws);
}, throws); });
}); });
} }
...@@ -5,9 +5,8 @@ import 'widget_tester.dart'; ...@@ -5,9 +5,8 @@ import 'widget_tester.dart';
void main() { void main() {
test('Can be placed in an infinte box', () { test('Can be placed in an infinte box', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.pumpWidget(new Block([new Center()]));
tester.pumpFrame(new Block([new Center()])); });
}); });
} }
...@@ -6,41 +6,41 @@ import 'widget_tester.dart'; ...@@ -6,41 +6,41 @@ import 'widget_tester.dart';
void main() { void main() {
test('Comparing coordinates', () { test('Comparing coordinates', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
Key keyA = new GlobalKey();
Key keyB = new GlobalKey();
Key keyA = new GlobalKey(); tester.pumpWidget(
Key keyB = new GlobalKey(); new Stack([
new Positioned(
top: 100.0,
left: 100.0,
child: new SizedBox(
key: keyA,
width: 10.0,
height: 10.0
)
),
new Positioned(
left: 100.0,
top: 200.0,
child: new SizedBox(
key: keyB,
width: 20.0,
height: 10.0
)
),
])
);
tester.pumpFrame( expect((tester.findElementByKey(keyA).renderObject as RenderBox).localToGlobal(const Point(0.0, 0.0)),
new Stack([ equals(const Point(100.0, 100.0)));
new Positioned(
top: 100.0,
left: 100.0,
child: new SizedBox(
key: keyA,
width: 10.0,
height: 10.0
)
),
new Positioned(
left: 100.0,
top: 200.0,
child: new SizedBox(
key: keyB,
width: 20.0,
height: 10.0
)
),
])
);
expect((tester.findElementByKey(keyA).renderObject as RenderBox).localToGlobal(const Point(0.0, 0.0)), expect((tester.findElementByKey(keyB).renderObject as RenderBox).localToGlobal(const Point(0.0, 0.0)),
equals(const Point(100.0, 100.0))); equals(const Point(100.0, 200.0)));
expect((tester.findElementByKey(keyB).renderObject as RenderBox).localToGlobal(const Point(0.0, 0.0)), expect((tester.findElementByKey(keyB).renderObject as RenderBox).globalToLocal(const Point(110.0, 205.0)),
equals(const Point(100.0, 200.0))); equals(const Point(10.0, 5.0)));
});
expect((tester.findElementByKey(keyB).renderObject as RenderBox).globalToLocal(const Point(110.0, 205.0)),
equals(const Point(10.0, 5.0)));
}); });
} }
...@@ -5,30 +5,30 @@ import 'widget_tester.dart'; ...@@ -5,30 +5,30 @@ import 'widget_tester.dart';
void main() { void main() {
test('Can select a day', () { test('Can select a day', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
DateTime currentValue;
DateTime currentValue; Widget widget = new Block([
new DatePicker(
selectedDate: new DateTime.utc(2015, 6, 9, 7, 12),
firstDate: new DateTime.utc(2013),
lastDate: new DateTime.utc(2018),
onChanged: (DateTime dateTime) {
currentValue = dateTime;
}
)
]);
Widget widget = new Block([ tester.pumpWidget(widget);
new DatePicker(
selectedDate: new DateTime.utc(2015, 6, 9, 7, 12),
firstDate: new DateTime.utc(2013),
lastDate: new DateTime.utc(2018),
onChanged: (DateTime dateTime) {
currentValue = dateTime;
}
)
]);
tester.pumpFrame(widget); expect(currentValue, isNull);
tester.tap(tester.findText('2015'));
expect(currentValue, isNull); tester.pumpWidget(widget);
tester.tap(tester.findText('2015')); tester.tap(tester.findText('2014'));
tester.pumpFrame(widget); tester.pumpWidget(widget);
tester.tap(tester.findText('2014')); expect(currentValue, equals(new DateTime(2014, 6, 9)));
tester.pumpFrame(widget); tester.tap(tester.findText('30'));
expect(currentValue, equals(new DateTime(2014, 6, 9))); expect(currentValue, equals(new DateTime(2013, 1, 30)));
tester.tap(tester.findText('30')); });
expect(currentValue, equals(new DateTime(2013, 1, 30)));
}); });
} }
import 'package:quiver/testing/async.dart';
import 'package:sky/widgets.dart'; import 'package:sky/widgets.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
...@@ -86,147 +85,149 @@ void dismissItem(WidgetTester tester, int item, { DismissDirection gestureDirect ...@@ -86,147 +85,149 @@ void dismissItem(WidgetTester tester, int item, { DismissDirection gestureDirect
tester.dispatchEvent(pointer.move(upLocation), downLocation); tester.dispatchEvent(pointer.move(upLocation), downLocation);
tester.dispatchEvent(pointer.up(), downLocation); tester.dispatchEvent(pointer.up(), downLocation);
double t0 = 0.0; tester.pumpWidget(widgetBuilder()); // start the resize animation
new FakeAsync().run((async) { tester.pumpWidget(widgetBuilder(), const Duration(seconds: 1)); // finish the resize animation
tester.pumpFrame(widgetBuilder(), t0); // start the resize animation tester.pumpWidget(widgetBuilder(), const Duration(seconds: 1)); // dismiss
tester.pumpFrame(widgetBuilder(), t0 + 1000.0); // finish the resize animation
async.elapse(new Duration(seconds: 1));
tester.pumpFrame(widgetBuilder(), t0 + 2000.0); // dismiss
async.elapse(new Duration(seconds: 1));
});
} }
void main() { void main() {
test('Horizontal drag triggers dismiss scrollDirection=vertical', () { test('Horizontal drag triggers dismiss scrollDirection=vertical', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
scrollDirection = ScrollDirection.vertical; scrollDirection = ScrollDirection.vertical;
dismissDirection = DismissDirection.horizontal; dismissDirection = DismissDirection.horizontal;
dismissedItems = []; dismissedItems = [];
tester.pumpFrame(widgetBuilder()); tester.pumpWidget(widgetBuilder());
expect(dismissedItems, isEmpty); expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.right); dismissItem(tester, 0, gestureDirection: DismissDirection.right);
expect(tester.findText('0'), isNull); expect(tester.findText('0'), isNull);
expect(dismissedItems, equals([0])); expect(dismissedItems, equals([0]));
dismissItem(tester, 1, gestureDirection: DismissDirection.left); dismissItem(tester, 1, gestureDirection: DismissDirection.left);
expect(tester.findText('1'), isNull); expect(tester.findText('1'), isNull);
expect(dismissedItems, equals([0, 1])); expect(dismissedItems, equals([0, 1]));
});
}); });
test('Vertical drag triggers dismiss scrollDirection=horizontal', () { test('Vertical drag triggers dismiss scrollDirection=horizontal', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
scrollDirection = ScrollDirection.horizontal; scrollDirection = ScrollDirection.horizontal;
dismissDirection = DismissDirection.vertical; dismissDirection = DismissDirection.vertical;
dismissedItems = []; dismissedItems = [];
tester.pumpFrame(widgetBuilder()); tester.pumpWidget(widgetBuilder());
expect(dismissedItems, isEmpty); expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.up); dismissItem(tester, 0, gestureDirection: DismissDirection.up);
expect(tester.findText('0'), isNull); expect(tester.findText('0'), isNull);
expect(dismissedItems, equals([0])); expect(dismissedItems, equals([0]));
dismissItem(tester, 1, gestureDirection: DismissDirection.down); dismissItem(tester, 1, gestureDirection: DismissDirection.down);
expect(tester.findText('1'), isNull); expect(tester.findText('1'), isNull);
expect(dismissedItems, equals([0, 1])); expect(dismissedItems, equals([0, 1]));
});
}); });
test('drag-left with DismissDirection.left triggers dismiss', () { test('drag-left with DismissDirection.left triggers dismiss', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
scrollDirection = ScrollDirection.vertical; scrollDirection = ScrollDirection.vertical;
dismissDirection = DismissDirection.left; dismissDirection = DismissDirection.left;
dismissedItems = []; dismissedItems = [];
tester.pumpFrame(widgetBuilder()); tester.pumpWidget(widgetBuilder());
expect(dismissedItems, isEmpty); expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.right); dismissItem(tester, 0, gestureDirection: DismissDirection.right);
expect(tester.findText('0'), isNotNull); expect(tester.findText('0'), isNotNull);
expect(dismissedItems, isEmpty); expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.left); dismissItem(tester, 0, gestureDirection: DismissDirection.left);
expect(tester.findText('0'), isNull); expect(tester.findText('0'), isNull);
expect(dismissedItems, equals([0])); expect(dismissedItems, equals([0]));
});
}); });
test('drag-right with DismissDirection.right triggers dismiss', () { test('drag-right with DismissDirection.right triggers dismiss', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
scrollDirection = ScrollDirection.vertical; scrollDirection = ScrollDirection.vertical;
dismissDirection = DismissDirection.right; dismissDirection = DismissDirection.right;
dismissedItems = []; dismissedItems = [];
tester.pumpFrame(widgetBuilder()); tester.pumpWidget(widgetBuilder());
expect(dismissedItems, isEmpty); expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.left); dismissItem(tester, 0, gestureDirection: DismissDirection.left);
expect(tester.findText('0'), isNotNull); expect(tester.findText('0'), isNotNull);
expect(dismissedItems, isEmpty); expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.right); dismissItem(tester, 0, gestureDirection: DismissDirection.right);
expect(tester.findText('0'), isNull); expect(tester.findText('0'), isNull);
expect(dismissedItems, equals([0])); expect(dismissedItems, equals([0]));
});
}); });
test('drag-up with DismissDirection.up triggers dismiss', () { test('drag-up with DismissDirection.up triggers dismiss', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
scrollDirection = ScrollDirection.horizontal; scrollDirection = ScrollDirection.horizontal;
dismissDirection = DismissDirection.up; dismissDirection = DismissDirection.up;
dismissedItems = []; dismissedItems = [];
tester.pumpFrame(widgetBuilder()); tester.pumpWidget(widgetBuilder());
expect(dismissedItems, isEmpty); expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.down); dismissItem(tester, 0, gestureDirection: DismissDirection.down);
expect(tester.findText('0'), isNotNull); expect(tester.findText('0'), isNotNull);
expect(dismissedItems, isEmpty); expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.up); dismissItem(tester, 0, gestureDirection: DismissDirection.up);
expect(tester.findText('0'), isNull); expect(tester.findText('0'), isNull);
expect(dismissedItems, equals([0])); expect(dismissedItems, equals([0]));
});
}); });
test('drag-down with DismissDirection.down triggers dismiss', () { test('drag-down with DismissDirection.down triggers dismiss', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
scrollDirection = ScrollDirection.horizontal; scrollDirection = ScrollDirection.horizontal;
dismissDirection = DismissDirection.down; dismissDirection = DismissDirection.down;
dismissedItems = []; dismissedItems = [];
tester.pumpFrame(widgetBuilder()); tester.pumpWidget(widgetBuilder());
expect(dismissedItems, isEmpty); expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.up); dismissItem(tester, 0, gestureDirection: DismissDirection.up);
expect(tester.findText('0'), isNotNull); expect(tester.findText('0'), isNotNull);
expect(dismissedItems, isEmpty); expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.down); dismissItem(tester, 0, gestureDirection: DismissDirection.down);
expect(tester.findText('0'), isNull); expect(tester.findText('0'), isNull);
expect(dismissedItems, equals([0])); expect(dismissedItems, equals([0]));
});
}); });
// This is a regression test for // This is a regression test for
// https://github.com/domokit/sky_engine/issues/1068 // https://github.com/domokit/sky_engine/issues/1068
test('Verify that drag-move events do not assert', () { test('Verify that drag-move events do not assert', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
scrollDirection = ScrollDirection.horizontal; scrollDirection = ScrollDirection.horizontal;
dismissDirection = DismissDirection.down; dismissDirection = DismissDirection.down;
dismissedItems = []; dismissedItems = [];
tester.pumpFrame(widgetBuilder()); tester.pumpWidget(widgetBuilder());
Element itemElement = tester.findText('0'); Element itemElement = tester.findText('0');
TestPointer pointer = new TestPointer(5); TestPointer pointer = new TestPointer(5);
Point location = tester.getTopLeft(itemElement); Point location = tester.getTopLeft(itemElement);
Offset offset = new Offset(0.0, 5.0); Offset offset = new Offset(0.0, 5.0);
tester.dispatchEvent(pointer.down(location), location); tester.dispatchEvent(pointer.down(location), location);
tester.dispatchEvent(pointer.move(location + offset), location); tester.dispatchEvent(pointer.move(location + offset), location);
tester.pumpFrame(widgetBuilder()); tester.pumpWidget(widgetBuilder());
tester.dispatchEvent(pointer.move(location + (offset * 2.0)), location); tester.dispatchEvent(pointer.move(location + (offset * 2.0)), location);
tester.pumpFrame(widgetBuilder()); tester.pumpWidget(widgetBuilder());
tester.dispatchEvent(pointer.move(location + (offset * 3.0)), location); tester.dispatchEvent(pointer.move(location + (offset * 3.0)), location);
tester.pumpFrame(widgetBuilder()); tester.pumpWidget(widgetBuilder());
tester.dispatchEvent(pointer.move(location + (offset * 4.0)), location); tester.dispatchEvent(pointer.move(location + (offset * 4.0)), location);
tester.pumpFrame(widgetBuilder()); tester.pumpWidget(widgetBuilder());
});
}); });
} }
...@@ -6,66 +6,66 @@ import 'widget_tester.dart'; ...@@ -6,66 +6,66 @@ import 'widget_tester.dart';
void main() { void main() {
test('Drag and drop - control test', () { test('Drag and drop - control test', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
TestPointer pointer = new TestPointer(7); TestPointer pointer = new TestPointer(7);
List accepted = []; List accepted = [];
tester.pumpFrame(new Navigator( tester.pumpWidget(new Navigator(
routes: { routes: {
'/': (NavigatorState navigator, Route route) { return new Column([ '/': (NavigatorState navigator, Route route) { return new Column([
new Draggable( new Draggable(
navigator: navigator, navigator: navigator,
data: 1, data: 1,
child: new Text('Source'), child: new Text('Source'),
feedback: new Text('Dragging') feedback: new Text('Dragging')
), ),
new DragTarget( new DragTarget(
builder: (context, data, rejects) { builder: (context, data, rejects) {
return new Container( return new Container(
height: 100.0, height: 100.0,
child: new Text('Target') child: new Text('Target')
); );
}, },
onAccept: (data) { onAccept: (data) {
accepted.add(data); accepted.add(data);
} }
), ),
]); ]);
}, },
} }
)); ));
expect(accepted, isEmpty); expect(accepted, isEmpty);
expect(tester.findText('Source'), isNotNull); expect(tester.findText('Source'), isNotNull);
expect(tester.findText('Dragging'), isNull); expect(tester.findText('Dragging'), isNull);
expect(tester.findText('Target'), isNotNull); expect(tester.findText('Target'), isNotNull);
Point firstLocation = tester.getCenter(tester.findText('Source')); Point firstLocation = tester.getCenter(tester.findText('Source'));
tester.dispatchEvent(pointer.down(firstLocation), firstLocation); tester.dispatchEvent(pointer.down(firstLocation), firstLocation);
tester.pumpFrameWithoutChange(); tester.pump();
expect(accepted, isEmpty); expect(accepted, isEmpty);
expect(tester.findText('Source'), isNotNull); expect(tester.findText('Source'), isNotNull);
expect(tester.findText('Dragging'), isNotNull); expect(tester.findText('Dragging'), isNotNull);
expect(tester.findText('Target'), isNotNull); expect(tester.findText('Target'), isNotNull);
Point secondLocation = tester.getCenter(tester.findText('Target')); Point secondLocation = tester.getCenter(tester.findText('Target'));
tester.dispatchEvent(pointer.move(secondLocation), firstLocation); tester.dispatchEvent(pointer.move(secondLocation), firstLocation);
tester.pumpFrameWithoutChange(); tester.pump();
expect(accepted, isEmpty); expect(accepted, isEmpty);
expect(tester.findText('Source'), isNotNull); expect(tester.findText('Source'), isNotNull);
expect(tester.findText('Dragging'), isNotNull); expect(tester.findText('Dragging'), isNotNull);
expect(tester.findText('Target'), isNotNull); expect(tester.findText('Target'), isNotNull);
tester.dispatchEvent(pointer.up(), firstLocation); tester.dispatchEvent(pointer.up(), firstLocation);
tester.pumpFrameWithoutChange(); tester.pump();
expect(accepted, equals([1])); expect(accepted, equals([1]));
expect(tester.findText('Source'), isNotNull); expect(tester.findText('Source'), isNotNull);
expect(tester.findText('Dragging'), isNull); expect(tester.findText('Dragging'), isNull);
expect(tester.findText('Target'), isNotNull); expect(tester.findText('Target'), isNotNull);
});
}); });
} }
...@@ -42,14 +42,15 @@ Widget builder() { ...@@ -42,14 +42,15 @@ Widget builder() {
void main() { void main() {
test('duplicate key smoke test', () { test('duplicate key smoke test', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.pumpFrame(builder()); tester.pumpWidget(builder());
StatefulLeafState leaf = tester.findStateOfType(StatefulLeafState); StatefulLeafState leaf = tester.findStateOfType(StatefulLeafState);
leaf.test(); leaf.test();
tester.pumpFrameWithoutChange(); tester.pump();
Item lastItem = items[1]; Item lastItem = items[1];
items.remove(lastItem); items.remove(lastItem);
items.insert(0, lastItem); items.insert(0, lastItem);
tester.pumpFrame(builder()); // this marks the app dirty and rebuilds it tester.pumpWidget(builder()); // this marks the app dirty and rebuilds it
});
}); });
} }
...@@ -5,41 +5,41 @@ import 'widget_tester.dart'; ...@@ -5,41 +5,41 @@ import 'widget_tester.dart';
void main() { void main() {
test('Can hit test flex children of stacks', () { test('Can hit test flex children of stacks', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
bool didReceiveTap = false;
bool didReceiveTap = false; tester.pumpWidget(
tester.pumpFrame( new Container(
new Container( decoration: const BoxDecoration(
decoration: const BoxDecoration( backgroundColor: const Color(0xFF00FF00)
backgroundColor: const Color(0xFF00FF00) ),
), child: new Stack([
child: new Stack([ new Positioned(
new Positioned( top: 10.0,
top: 10.0, left: 10.0,
left: 10.0, child: new Column([
child: new Column([ new GestureDetector(
new GestureDetector( onTap: () {
onTap: () { didReceiveTap = true;
didReceiveTap = true; },
}, child: new Container(
child: new Container( decoration: const BoxDecoration(
decoration: const BoxDecoration( backgroundColor: const Color(0xFF0000FF)
backgroundColor: const Color(0xFF0000FF) ),
), width: 100.0,
width: 100.0, height: 100.0,
height: 100.0, child: new Center(
child: new Center( child: new Text('X')
child: new Text('X') )
) )
) )
) ])
]) )
) ])
]) )
) );
);
tester.tap(tester.findText('X')); tester.tap(tester.findText('X'));
expect(didReceiveTap, isTrue); expect(didReceiveTap, isTrue);
});
}); });
} }
...@@ -6,84 +6,86 @@ import 'widget_tester.dart'; ...@@ -6,84 +6,86 @@ import 'widget_tester.dart';
void main() { void main() {
test('Uncontested scrolls start immediately', () { test('Uncontested scrolls start immediately', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
TestPointer pointer = new TestPointer(7); TestPointer pointer = new TestPointer(7);
bool didStartDrag = false; bool didStartDrag = false;
double updatedDragDelta; double updatedDragDelta;
bool didEndDrag = false; bool didEndDrag = false;
Widget widget = new GestureDetector( Widget widget = new GestureDetector(
onVerticalDragStart: () { onVerticalDragStart: () {
didStartDrag = true; didStartDrag = true;
}, },
onVerticalDragUpdate: (double scrollDelta) { onVerticalDragUpdate: (double scrollDelta) {
updatedDragDelta = scrollDelta; updatedDragDelta = scrollDelta;
}, },
onVerticalDragEnd: (Offset velocity) { onVerticalDragEnd: (Offset velocity) {
didEndDrag = true; didEndDrag = true;
}, },
child: new Container() child: new Container()
); );
tester.pumpFrame(widget); tester.pumpWidget(widget);
expect(didStartDrag, isFalse); expect(didStartDrag, isFalse);
expect(updatedDragDelta, isNull); expect(updatedDragDelta, isNull);
expect(didEndDrag, isFalse); expect(didEndDrag, isFalse);
Point firstLocation = new Point(10.0, 10.0); Point firstLocation = new Point(10.0, 10.0);
tester.dispatchEvent(pointer.down(firstLocation), firstLocation); tester.dispatchEvent(pointer.down(firstLocation), firstLocation);
expect(didStartDrag, isTrue); expect(didStartDrag, isTrue);
didStartDrag = false; didStartDrag = false;
expect(updatedDragDelta, isNull); expect(updatedDragDelta, isNull);
expect(didEndDrag, isFalse); expect(didEndDrag, isFalse);
Point secondLocation = new Point(10.0, 9.0); Point secondLocation = new Point(10.0, 9.0);
tester.dispatchEvent(pointer.move(secondLocation), firstLocation); tester.dispatchEvent(pointer.move(secondLocation), firstLocation);
expect(didStartDrag, isFalse); expect(didStartDrag, isFalse);
expect(updatedDragDelta, -1.0); expect(updatedDragDelta, -1.0);
updatedDragDelta = null; updatedDragDelta = null;
expect(didEndDrag, isFalse); expect(didEndDrag, isFalse);
tester.dispatchEvent(pointer.up(), firstLocation); tester.dispatchEvent(pointer.up(), firstLocation);
expect(didStartDrag, isFalse); expect(didStartDrag, isFalse);
expect(updatedDragDelta, isNull); expect(updatedDragDelta, isNull);
expect(didEndDrag, isTrue); expect(didEndDrag, isTrue);
didEndDrag = false; didEndDrag = false;
tester.pumpFrame(new Container()); tester.pumpWidget(new Container());
});
}); });
test('Match two scroll gestures in succession', () { test('Match two scroll gestures in succession', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
TestPointer pointer = new TestPointer(7); TestPointer pointer = new TestPointer(7);
int gestureCount = 0; int gestureCount = 0;
double dragDistance = 0.0; double dragDistance = 0.0;
Point downLocation = new Point(10.0, 10.0); Point downLocation = new Point(10.0, 10.0);
Point upLocation = new Point(10.0, 20.0); Point upLocation = new Point(10.0, 20.0);
Widget widget = new GestureDetector( Widget widget = new GestureDetector(
onVerticalDragUpdate: (double delta) { dragDistance += delta; }, onVerticalDragUpdate: (double delta) { dragDistance += delta; },
onVerticalDragEnd: (Offset velocity) { gestureCount += 1; }, onVerticalDragEnd: (Offset velocity) { gestureCount += 1; },
onHorizontalDragUpdate: (_) { fail("gesture should not match"); }, onHorizontalDragUpdate: (_) { fail("gesture should not match"); },
onHorizontalDragEnd: (Offset velocity) { fail("gesture should not match"); }, onHorizontalDragEnd: (Offset velocity) { fail("gesture should not match"); },
child: new Container() child: new Container()
); );
tester.pumpFrame(widget); tester.pumpWidget(widget);
tester.dispatchEvent(pointer.down(downLocation), downLocation); tester.dispatchEvent(pointer.down(downLocation), downLocation);
tester.dispatchEvent(pointer.move(upLocation), downLocation); tester.dispatchEvent(pointer.move(upLocation), downLocation);
tester.dispatchEvent(pointer.up(), downLocation); tester.dispatchEvent(pointer.up(), downLocation);
tester.dispatchEvent(pointer.down(downLocation), downLocation); tester.dispatchEvent(pointer.down(downLocation), downLocation);
tester.dispatchEvent(pointer.move(upLocation), downLocation); tester.dispatchEvent(pointer.move(upLocation), downLocation);
tester.dispatchEvent(pointer.up(), downLocation); tester.dispatchEvent(pointer.up(), downLocation);
expect(gestureCount, 2); expect(gestureCount, 2);
expect(dragDistance, 20.0); expect(dragDistance, 20.0);
tester.pumpFrame(new Container()); tester.pumpWidget(new Container());
});
}); });
} }
...@@ -6,159 +6,159 @@ import 'test_widgets.dart'; ...@@ -6,159 +6,159 @@ import 'test_widgets.dart';
void main() { void main() {
test('HomogeneousViewport mount/dismount smoke test', () { test('HomogeneousViewport mount/dismount smoke test', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
List<int> callbackTracker = <int>[];
List<int> callbackTracker = <int>[];
// the root view is 800x600 in the test environment
// the root view is 800x600 in the test environment // so if our widget is 100 pixels tall, it should fit exactly 6 times.
// so if our widget is 100 pixels tall, it should fit exactly 6 times.
Widget builder() {
Widget builder() { return new FlipComponent(
return new FlipComponent( left: new HomogeneousViewport(
left: new HomogeneousViewport( builder: (BuildContext context, int start, int count) {
builder: (BuildContext context, int start, int count) { List<Widget> result = <Widget>[];
List<Widget> result = <Widget>[]; for (int index = start; index < start + count; index += 1) {
for (int index = start; index < start + count; index += 1) { callbackTracker.add(index);
callbackTracker.add(index); result.add(new Container(
result.add(new Container( key: new ValueKey<int>(index),
key: new ValueKey<int>(index), height: 100.0,
height: 100.0, child: new Text("$index")
child: new Text("$index") ));
)); }
} return result;
return result; },
}, startOffset: 0.0,
startOffset: 0.0, itemExtent: 100.0
itemExtent: 100.0 ),
), right: new Text('Not Today')
right: new Text('Not Today') );
); }
}
tester.pumpWidget(builder());
tester.pumpFrame(builder());
StatefulComponentElement element = tester.findElement((element) => element.widget is FlipComponent);
StatefulComponentElement element = tester.findElement((element) => element.widget is FlipComponent); FlipComponentState testComponent = element.state;
FlipComponentState testComponent = element.state;
expect(callbackTracker, equals([0, 1, 2, 3, 4, 5]));
expect(callbackTracker, equals([0, 1, 2, 3, 4, 5]));
callbackTracker.clear();
callbackTracker.clear(); testComponent.flip();
testComponent.flip(); tester.pump();
tester.pumpFrameWithoutChange();
expect(callbackTracker, equals([]));
expect(callbackTracker, equals([]));
callbackTracker.clear();
callbackTracker.clear(); testComponent.flip();
testComponent.flip(); tester.pump();
tester.pumpFrameWithoutChange();
expect(callbackTracker, equals([0, 1, 2, 3, 4, 5]));
expect(callbackTracker, equals([0, 1, 2, 3, 4, 5])); });
}); });
test('HomogeneousViewport vertical', () { test('HomogeneousViewport vertical', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
List<int> callbackTracker = <int>[];
List<int> callbackTracker = <int>[];
// the root view is 800x600 in the test environment
// the root view is 800x600 in the test environment // so if our widget is 200 pixels tall, it should fit exactly 3 times.
// so if our widget is 200 pixels tall, it should fit exactly 3 times. // but if we are offset by 300 pixels, there will be 4, numbered 1-4.
// but if we are offset by 300 pixels, there will be 4, numbered 1-4.
double offset = 300.0;
double offset = 300.0;
ListBuilder itemBuilder = (BuildContext context, int start, int count) {
ListBuilder itemBuilder = (BuildContext context, int start, int count) { List<Widget> result = <Widget>[];
List<Widget> result = <Widget>[]; for (int index = start; index < start + count; index += 1) {
for (int index = start; index < start + count; index += 1) { callbackTracker.add(index);
callbackTracker.add(index); result.add(new Container(
result.add(new Container( key: new ValueKey<int>(index),
key: new ValueKey<int>(index), width: 500.0, // this should be ignored
width: 500.0, // this should be ignored height: 400.0, // should be overridden by itemExtent
height: 400.0, // should be overridden by itemExtent child: new Text("$index")
child: new Text("$index") ));
)); }
return result;
};
FlipComponent testComponent;
Widget builder() {
testComponent = new FlipComponent(
left: new HomogeneousViewport(
builder: itemBuilder,
startOffset: offset,
itemExtent: 200.0
),
right: new Text('Not Today')
);
return testComponent;
} }
return result;
};
FlipComponent testComponent; tester.pumpWidget(builder());
Widget builder() {
testComponent = new FlipComponent(
left: new HomogeneousViewport(
builder: itemBuilder,
startOffset: offset,
itemExtent: 200.0
),
right: new Text('Not Today')
);
return testComponent;
}
tester.pumpFrame(builder()); expect(callbackTracker, equals([1, 2, 3, 4]));
expect(callbackTracker, equals([1, 2, 3, 4])); callbackTracker.clear();
callbackTracker.clear(); offset = 400.0; // now only 3 should fit, numbered 2-4.
offset = 400.0; // now only 3 should fit, numbered 2-4. tester.pumpWidget(builder());
tester.pumpFrame(builder()); expect(callbackTracker, equals([2, 3, 4]));
expect(callbackTracker, equals([2, 3, 4])); callbackTracker.clear();
});
callbackTracker.clear();
}); });
test('HomogeneousViewport horizontal', () { test('HomogeneousViewport horizontal', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
List<int> callbackTracker = <int>[];
List<int> callbackTracker = <int>[];
// the root view is 800x600 in the test environment
// the root view is 800x600 in the test environment // so if our widget is 200 pixels wide, it should fit exactly 4 times.
// so if our widget is 200 pixels wide, it should fit exactly 4 times. // but if we are offset by 300 pixels, there will be 5, numbered 1-5.
// but if we are offset by 300 pixels, there will be 5, numbered 1-5.
double offset = 300.0;
double offset = 300.0;
ListBuilder itemBuilder = (BuildContext context, int start, int count) {
ListBuilder itemBuilder = (BuildContext context, int start, int count) { List<Widget> result = <Widget>[];
List<Widget> result = <Widget>[]; for (int index = start; index < start + count; index += 1) {
for (int index = start; index < start + count; index += 1) { callbackTracker.add(index);
callbackTracker.add(index); result.add(new Container(
result.add(new Container( key: new ValueKey<int>(index),
key: new ValueKey<int>(index), width: 400.0, // this should be overridden by itemExtent
width: 400.0, // this should be overridden by itemExtent height: 500.0, // this should be ignored
height: 500.0, // this should be ignored child: new Text("$index")
child: new Text("$index") ));
)); }
return result;
};
FlipComponent testComponent;
Widget builder() {
testComponent = new FlipComponent(
left: new HomogeneousViewport(
builder: itemBuilder,
startOffset: offset,
itemExtent: 200.0,
direction: ScrollDirection.horizontal
),
right: new Text('Not Today')
);
return testComponent;
} }
return result;
};
FlipComponent testComponent;
Widget builder() {
testComponent = new FlipComponent(
left: new HomogeneousViewport(
builder: itemBuilder,
startOffset: offset,
itemExtent: 200.0,
direction: ScrollDirection.horizontal
),
right: new Text('Not Today')
);
return testComponent;
}
tester.pumpFrame(builder()); tester.pumpWidget(builder());
expect(callbackTracker, equals([1, 2, 3, 4, 5])); expect(callbackTracker, equals([1, 2, 3, 4, 5]));
callbackTracker.clear(); callbackTracker.clear();
offset = 400.0; // now only 4 should fit, numbered 2-5. offset = 400.0; // now only 4 should fit, numbered 2-5.
tester.pumpFrame(builder()); tester.pumpWidget(builder());
expect(callbackTracker, equals([2, 3, 4, 5])); expect(callbackTracker, equals([2, 3, 4, 5]));
callbackTracker.clear(); callbackTracker.clear();
});
}); });
} }
import 'package:mojo_services/keyboard/keyboard.mojom.dart'; import 'package:mojo_services/keyboard/keyboard.mojom.dart';
import 'package:quiver/testing/async.dart';
import 'package:sky/rendering.dart'; import 'package:sky/rendering.dart';
import 'package:sky/services.dart'; import 'package:sky/services.dart';
import 'package:sky/widgets.dart'; import 'package:sky/widgets.dart';
...@@ -25,65 +24,63 @@ void main() { ...@@ -25,65 +24,63 @@ void main() {
serviceMocker.registerMockService(KeyboardServiceName, mockKeyboard); serviceMocker.registerMockService(KeyboardServiceName, mockKeyboard);
test('Editable text has consistent width', () { test('Editable text has consistent width', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
GlobalKey inputKey = new GlobalKey();
GlobalKey inputKey = new GlobalKey(); String inputValue;
String inputValue;
Widget builder() {
Widget builder() { return new Center(
return new Center( child: new Input(
child: new Input( key: inputKey,
key: inputKey, placeholder: 'Placeholder',
placeholder: 'Placeholder', onChanged: (value) { inputValue = value; }
onChanged: (value) { inputValue = value; } )
) );
); }
}
tester.pumpFrame(builder()); tester.pumpWidget(builder());
Element input = tester.findElementByKey(inputKey); Element input = tester.findElementByKey(inputKey);
Size emptyInputSize = (input.renderObject as RenderBox).size; Size emptyInputSize = (input.renderObject as RenderBox).size;
// Simulate entry of text through the keyboard. // Simulate entry of text through the keyboard.
expect(mockKeyboard.client, isNotNull); expect(mockKeyboard.client, isNotNull);
const String testValue = 'Test'; const String testValue = 'Test';
mockKeyboard.client.setComposingText(testValue, testValue.length); mockKeyboard.client.setComposingText(testValue, testValue.length);
// Check that the onChanged event handler fired. // Check that the onChanged event handler fired.
expect(inputValue, equals(testValue)); expect(inputValue, equals(testValue));
tester.pumpFrame(builder()); tester.pumpWidget(builder());
// Check that the Input with text has the same size as the empty Input. // Check that the Input with text has the same size as the empty Input.
expect((input.renderObject as RenderBox).size, equals(emptyInputSize)); expect((input.renderObject as RenderBox).size, equals(emptyInputSize));
});
}); });
test('Cursor blinks', () { test('Cursor blinks', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
GlobalKey inputKey = new GlobalKey();
GlobalKey inputKey = new GlobalKey();
Widget builder() {
Widget builder() { return new Center(
return new Center( child: new Input(
child: new Input( key: inputKey,
key: inputKey, placeholder: 'Placeholder'
placeholder: 'Placeholder' )
) );
); }
}
new FakeAsync().run((async) { tester.pumpWidget(builder());
tester.pumpFrame(builder());
EditableTextState editableText = tester.findStateOfType(EditableTextState); EditableTextState editableText = tester.findStateOfType(EditableTextState);
// Check that the cursor visibility toggles after each blink interval. // Check that the cursor visibility toggles after each blink interval.
void checkCursorToggle() { void checkCursorToggle() {
bool initialShowCursor = editableText.test_showCursor; bool initialShowCursor = editableText.test_showCursor;
async.elapse(editableText.test_cursorBlinkPeriod); tester.async.elapse(editableText.test_cursorBlinkPeriod);
expect(editableText.test_showCursor, equals(!initialShowCursor)); expect(editableText.test_showCursor, equals(!initialShowCursor));
async.elapse(editableText.test_cursorBlinkPeriod); tester.async.elapse(editableText.test_cursorBlinkPeriod);
expect(editableText.test_showCursor, equals(initialShowCursor)); expect(editableText.test_showCursor, equals(initialShowCursor));
} }
......
...@@ -5,38 +5,38 @@ import 'widget_tester.dart'; ...@@ -5,38 +5,38 @@ import 'widget_tester.dart';
void main() { void main() {
test('Events bubble up the tree', () { test('Events bubble up the tree', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
List<String> log = new List<String>();
List<String> log = new List<String>(); tester.pumpWidget(
new Listener(
tester.pumpFrame(
new Listener(
onPointerDown: (_) {
log.add('top');
},
child: new Listener(
onPointerDown: (_) { onPointerDown: (_) {
log.add('middle'); log.add('top');
}, },
child: new DecoratedBox( child: new Listener(
decoration: const BoxDecoration(), onPointerDown: (_) {
child: new Listener( log.add('middle');
onPointerDown: (_) { },
log.add('bottom'); child: new DecoratedBox(
}, decoration: const BoxDecoration(),
child: new Text('X') child: new Listener(
onPointerDown: (_) {
log.add('bottom');
},
child: new Text('X')
)
) )
) )
) )
) );
);
tester.tap(tester.findText('X')); tester.tap(tester.findText('X'));
expect(log, equals([ expect(log, equals([
'bottom', 'bottom',
'middle', 'middle',
'top', 'top',
])); ]));
});
}); });
} }
...@@ -6,147 +6,144 @@ import 'test_widgets.dart'; ...@@ -6,147 +6,144 @@ import 'test_widgets.dart';
void main() { void main() {
test('MixedViewport mount/dismount smoke test', () { test('MixedViewport mount/dismount smoke test', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
List<int> callbackTracker = <int>[];
List<int> callbackTracker = <int>[];
// the root view is 800x600 in the test environment
// the root view is 800x600 in the test environment // so if our widget is 100 pixels tall, it should fit exactly 6 times.
// so if our widget is 100 pixels tall, it should fit exactly 6 times.
Widget builder() {
Widget builder() { return new FlipComponent(
return new FlipComponent( left: new MixedViewport(
left: new MixedViewport( builder: (BuildContext context, int i) {
builder: (BuildContext context, int i) { callbackTracker.add(i);
callbackTracker.add(i); return new Container(
return new Container( key: new ValueKey<int>(i),
key: new ValueKey<int>(i), height: 100.0,
height: 100.0, child: new Text("$i")
child: new Text("$i") );
); },
}, startOffset: 0.0
startOffset: 0.0 ),
), right: new Text('Not Today')
right: new Text('Not Today') );
); }
}
tester.pumpWidget(builder());
tester.pumpFrame(builder());
StatefulComponentElement element = tester.findElement((element) => element.widget is FlipComponent);
StatefulComponentElement element = tester.findElement((element) => element.widget is FlipComponent); FlipComponentState testComponent = element.state;
FlipComponentState testComponent = element.state;
expect(callbackTracker, equals([0, 1, 2, 3, 4, 5]));
expect(callbackTracker, equals([0, 1, 2, 3, 4, 5]));
callbackTracker.clear();
callbackTracker.clear(); testComponent.flip();
testComponent.flip(); tester.pump();
tester.pumpFrameWithoutChange();
expect(callbackTracker, equals([]));
expect(callbackTracker, equals([]));
callbackTracker.clear();
callbackTracker.clear(); testComponent.flip();
testComponent.flip(); tester.pump();
tester.pumpFrameWithoutChange();
expect(callbackTracker, equals([0, 1, 2, 3, 4, 5]));
expect(callbackTracker, equals([0, 1, 2, 3, 4, 5])); });
}); });
test('MixedViewport vertical', () { test('MixedViewport vertical', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
List<int> callbackTracker = <int>[];
List<int> callbackTracker = <int>[];
// the root view is 800x600 in the test environment // the root view is 800x600 in the test environment
// so if our widget is 200 pixels tall, it should fit exactly 3 times. // so if our widget is 200 pixels tall, it should fit exactly 3 times.
// but if we are offset by 300 pixels, there will be 4, numbered 1-4. // but if we are offset by 300 pixels, there will be 4, numbered 1-4.
double offset = 300.0; double offset = 300.0;
IndexedBuilder itemBuilder = (BuildContext context, int i) { IndexedBuilder itemBuilder = (BuildContext context, int i) {
callbackTracker.add(i); callbackTracker.add(i);
return new Container( return new Container(
key: new ValueKey<int>(i), key: new ValueKey<int>(i),
width: 500.0, // this should be ignored width: 500.0, // this should be ignored
height: 200.0, height: 200.0,
child: new Text("$i") child: new Text("$i")
); );
}; };
Widget builder() { Widget builder() {
return new FlipComponent( return new FlipComponent(
left: new MixedViewport( left: new MixedViewport(
builder: itemBuilder, builder: itemBuilder,
startOffset: offset startOffset: offset
), ),
right: new Text('Not Today') right: new Text('Not Today')
); );
} }
tester.pumpFrame(builder()); tester.pumpWidget(builder());
// 0 is built to find its width // 0 is built to find its width
expect(callbackTracker, equals([0, 1, 2, 3, 4])); expect(callbackTracker, equals([0, 1, 2, 3, 4]));
callbackTracker.clear(); callbackTracker.clear();
offset = 400.0; // now only 3 should fit, numbered 2-4. offset = 400.0; // now only 3 should fit, numbered 2-4.
tester.pumpFrame(builder()); tester.pumpWidget(builder());
// 0 and 1 aren't built, we know their size and nothing else changed // 0 and 1 aren't built, we know their size and nothing else changed
expect(callbackTracker, equals([2, 3, 4])); expect(callbackTracker, equals([2, 3, 4]));
callbackTracker.clear();
callbackTracker.clear();
});
}); });
test('MixedViewport horizontal', () { test('MixedViewport horizontal', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
List<int> callbackTracker = <int>[];
List<int> callbackTracker = <int>[];
// the root view is 800x600 in the test environment
// so if our widget is 200 pixels wide, it should fit exactly 4 times.
// but if we are offset by 300 pixels, there will be 5, numbered 1-5.
double offset = 300.0; // the root view is 800x600 in the test environment
// so if our widget is 200 pixels wide, it should fit exactly 4 times.
// but if we are offset by 300 pixels, there will be 5, numbered 1-5.
IndexedBuilder itemBuilder = (BuildContext context, int i) { double offset = 300.0;
callbackTracker.add(i);
return new Container(
key: new ValueKey<int>(i),
height: 500.0, // this should be ignored
width: 200.0,
child: new Text("$i")
);
};
Widget builder() { IndexedBuilder itemBuilder = (BuildContext context, int i) {
return new FlipComponent( callbackTracker.add(i);
left: new MixedViewport( return new Container(
builder: itemBuilder, key: new ValueKey<int>(i),
startOffset: offset, height: 500.0, // this should be ignored
direction: ScrollDirection.horizontal width: 200.0,
), child: new Text("$i")
right: new Text('Not Today') );
); };
}
tester.pumpFrame(builder()); Widget builder() {
return new FlipComponent(
left: new MixedViewport(
builder: itemBuilder,
startOffset: offset,
direction: ScrollDirection.horizontal
),
right: new Text('Not Today')
);
}
// 0 is built to find its width tester.pumpWidget(builder());
expect(callbackTracker, equals([0, 1, 2, 3, 4, 5]));
callbackTracker.clear(); // 0 is built to find its width
expect(callbackTracker, equals([0, 1, 2, 3, 4, 5]));
offset = 400.0; // now only 4 should fit, numbered 2-5. callbackTracker.clear();
tester.pumpFrame(builder()); offset = 400.0; // now only 4 should fit, numbered 2-5.
// 0 and 1 aren't built, we know their size and nothing else changed tester.pumpWidget(builder());
expect(callbackTracker, equals([2, 3, 4, 5]));
callbackTracker.clear(); // 0 and 1 aren't built, we know their size and nothing else changed
expect(callbackTracker, equals([2, 3, 4, 5]));
callbackTracker.clear();
});
}); });
} }
...@@ -28,263 +28,265 @@ void checkTree(WidgetTester tester, List<BoxDecoration> expectedDecorations) { ...@@ -28,263 +28,265 @@ void checkTree(WidgetTester tester, List<BoxDecoration> expectedDecorations) {
void main() { void main() {
test('MultiChildRenderObjectElement control test', () { test('MultiChildRenderObjectElement control test', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.pumpFrame( tester.pumpWidget(
new Stack([ new Stack([
new DecoratedBox(decoration: kBoxDecorationA), new DecoratedBox(decoration: kBoxDecorationA),
new DecoratedBox(decoration: kBoxDecorationB), new DecoratedBox(decoration: kBoxDecorationB),
new DecoratedBox(decoration: kBoxDecorationC), new DecoratedBox(decoration: kBoxDecorationC),
]) ])
); );
checkTree(tester, [kBoxDecorationA, kBoxDecorationB, kBoxDecorationC]); checkTree(tester, [kBoxDecorationA, kBoxDecorationB, kBoxDecorationC]);
tester.pumpFrame( tester.pumpWidget(
new Stack([ new Stack([
new DecoratedBox(decoration: kBoxDecorationA), new DecoratedBox(decoration: kBoxDecorationA),
new DecoratedBox(decoration: kBoxDecorationC), new DecoratedBox(decoration: kBoxDecorationC),
]) ])
); );
checkTree(tester, [kBoxDecorationA, kBoxDecorationC]); checkTree(tester, [kBoxDecorationA, kBoxDecorationC]);
tester.pumpFrame( tester.pumpWidget(
new Stack([ new Stack([
new DecoratedBox(decoration: kBoxDecorationA), new DecoratedBox(decoration: kBoxDecorationA),
new DecoratedBox(key: new Key('b'), decoration: kBoxDecorationB), new DecoratedBox(key: new Key('b'), decoration: kBoxDecorationB),
new DecoratedBox(decoration: kBoxDecorationC), new DecoratedBox(decoration: kBoxDecorationC),
]) ])
); );
checkTree(tester, [kBoxDecorationA, kBoxDecorationB, kBoxDecorationC]); checkTree(tester, [kBoxDecorationA, kBoxDecorationB, kBoxDecorationC]);
tester.pumpFrame( tester.pumpWidget(
new Stack([ new Stack([
new DecoratedBox(key: new Key('b'), decoration: kBoxDecorationB), new DecoratedBox(key: new Key('b'), decoration: kBoxDecorationB),
new DecoratedBox(decoration: kBoxDecorationC), new DecoratedBox(decoration: kBoxDecorationC),
new DecoratedBox(key: new Key('a'), decoration: kBoxDecorationA), new DecoratedBox(key: new Key('a'), decoration: kBoxDecorationA),
]) ])
); );
checkTree(tester, [kBoxDecorationB, kBoxDecorationC, kBoxDecorationA]); checkTree(tester, [kBoxDecorationB, kBoxDecorationC, kBoxDecorationA]);
tester.pumpFrame( tester.pumpWidget(
new Stack([ new Stack([
new DecoratedBox(key: new Key('a'), decoration: kBoxDecorationA), new DecoratedBox(key: new Key('a'), decoration: kBoxDecorationA),
new DecoratedBox(decoration: kBoxDecorationC), new DecoratedBox(decoration: kBoxDecorationC),
new DecoratedBox(key: new Key('b'), decoration: kBoxDecorationB), new DecoratedBox(key: new Key('b'), decoration: kBoxDecorationB),
]) ])
); );
checkTree(tester, [kBoxDecorationA, kBoxDecorationC, kBoxDecorationB]); checkTree(tester, [kBoxDecorationA, kBoxDecorationC, kBoxDecorationB]);
tester.pumpFrame( tester.pumpWidget(
new Stack([ new Stack([
new DecoratedBox(decoration: kBoxDecorationC), new DecoratedBox(decoration: kBoxDecorationC),
]) ])
); );
checkTree(tester, [kBoxDecorationC]); checkTree(tester, [kBoxDecorationC]);
tester.pumpFrame( tester.pumpWidget(
new Stack([]) new Stack([])
); );
checkTree(tester, []); checkTree(tester, []);
});
}); });
test('MultiChildRenderObjectElement with stateless components', () { test('MultiChildRenderObjectElement with stateless components', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.pumpFrame( tester.pumpWidget(
new Stack([ new Stack([
new DecoratedBox(decoration: kBoxDecorationA), new DecoratedBox(decoration: kBoxDecorationA),
new DecoratedBox(decoration: kBoxDecorationB), new DecoratedBox(decoration: kBoxDecorationB),
new DecoratedBox(decoration: kBoxDecorationC), new DecoratedBox(decoration: kBoxDecorationC),
]) ])
); );
checkTree(tester, [kBoxDecorationA, kBoxDecorationB, kBoxDecorationC]); checkTree(tester, [kBoxDecorationA, kBoxDecorationB, kBoxDecorationC]);
tester.pumpFrame( tester.pumpWidget(
new Stack([ new Stack([
new DecoratedBox(decoration: kBoxDecorationA), new DecoratedBox(decoration: kBoxDecorationA),
new Container( new Container(
child: new DecoratedBox(decoration: kBoxDecorationB)
),
new DecoratedBox(decoration: kBoxDecorationC),
])
);
checkTree(tester, [kBoxDecorationA, kBoxDecorationB, kBoxDecorationC]);
tester.pumpFrame(
new Stack([
new DecoratedBox(decoration: kBoxDecorationA),
new Container(
child: new Container(
child: new DecoratedBox(decoration: kBoxDecorationB) child: new DecoratedBox(decoration: kBoxDecorationB)
) ),
), new DecoratedBox(decoration: kBoxDecorationC),
new DecoratedBox(decoration: kBoxDecorationC), ])
]) );
);
checkTree(tester, [kBoxDecorationA, kBoxDecorationB, kBoxDecorationC]);
checkTree(tester, [kBoxDecorationA, kBoxDecorationB, kBoxDecorationC]);
tester.pumpWidget(
tester.pumpFrame( new Stack([
new Stack([ new DecoratedBox(decoration: kBoxDecorationA),
new Container( new Container(
child: new Container( child: new Container(
child: new DecoratedBox(decoration: kBoxDecorationB)
)
),
new DecoratedBox(decoration: kBoxDecorationC),
])
);
checkTree(tester, [kBoxDecorationA, kBoxDecorationB, kBoxDecorationC]);
tester.pumpWidget(
new Stack([
new Container(
child: new Container(
child: new DecoratedBox(decoration: kBoxDecorationB)
)
),
new Container(
child: new DecoratedBox(decoration: kBoxDecorationA)
),
new DecoratedBox(decoration: kBoxDecorationC),
])
);
checkTree(tester, [kBoxDecorationB, kBoxDecorationA, kBoxDecorationC]);
tester.pumpWidget(
new Stack([
new Container(
child: new DecoratedBox(decoration: kBoxDecorationB) child: new DecoratedBox(decoration: kBoxDecorationB)
) ),
), new Container(
new Container( child: new DecoratedBox(decoration: kBoxDecorationA)
child: new DecoratedBox(decoration: kBoxDecorationA) ),
), new DecoratedBox(decoration: kBoxDecorationC),
new DecoratedBox(decoration: kBoxDecorationC), ])
]) );
);
checkTree(tester, [kBoxDecorationB, kBoxDecorationA, kBoxDecorationC]);
checkTree(tester, [kBoxDecorationB, kBoxDecorationA, kBoxDecorationC]);
tester.pumpWidget(
tester.pumpFrame( new Stack([
new Stack([ new Container(
new Container( key: new Key('b'),
child: new DecoratedBox(decoration: kBoxDecorationB) child: new DecoratedBox(decoration: kBoxDecorationB)
), ),
new Container( new Container(
child: new DecoratedBox(decoration: kBoxDecorationA) key: new Key('a'),
), child: new DecoratedBox(decoration: kBoxDecorationA)
new DecoratedBox(decoration: kBoxDecorationC), ),
]) ])
); );
checkTree(tester, [kBoxDecorationB, kBoxDecorationA, kBoxDecorationC]); checkTree(tester, [kBoxDecorationB, kBoxDecorationA]);
tester.pumpFrame( tester.pumpWidget(
new Stack([ new Stack([
new Container( new Container(
key: new Key('b'), key: new Key('a'),
child: new DecoratedBox(decoration: kBoxDecorationB) child: new DecoratedBox(decoration: kBoxDecorationA)
), ),
new Container( new Container(
key: new Key('a'), key: new Key('b'),
child: new DecoratedBox(decoration: kBoxDecorationA) child: new DecoratedBox(decoration: kBoxDecorationB)
), ),
]) ])
); );
checkTree(tester, [kBoxDecorationB, kBoxDecorationA]); checkTree(tester, [kBoxDecorationA, kBoxDecorationB]);
tester.pumpFrame( tester.pumpWidget(
new Stack([ new Stack([ ])
new Container( );
key: new Key('a'),
child: new DecoratedBox(decoration: kBoxDecorationA) checkTree(tester, []);
), });
new Container(
key: new Key('b'),
child: new DecoratedBox(decoration: kBoxDecorationB)
),
])
);
checkTree(tester, [kBoxDecorationA, kBoxDecorationB]);
tester.pumpFrame(
new Stack([ ])
);
checkTree(tester, []);
}); });
test('MultiChildRenderObjectElement with stateful components', () { test('MultiChildRenderObjectElement with stateful components', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.pumpWidget(
tester.pumpFrame( new Stack([
new Stack([ new DecoratedBox(decoration: kBoxDecorationA),
new DecoratedBox(decoration: kBoxDecorationA), new DecoratedBox(decoration: kBoxDecorationB),
new DecoratedBox(decoration: kBoxDecorationB), ])
]) );
);
checkTree(tester, [kBoxDecorationA, kBoxDecorationB]);
checkTree(tester, [kBoxDecorationA, kBoxDecorationB]);
tester.pumpWidget(
tester.pumpFrame( new Stack([
new Stack([ new FlipComponent(
new FlipComponent( left: new DecoratedBox(decoration: kBoxDecorationA),
left: new DecoratedBox(decoration: kBoxDecorationA), right: new DecoratedBox(decoration: kBoxDecorationB)
right: new DecoratedBox(decoration: kBoxDecorationB) ),
), new DecoratedBox(decoration: kBoxDecorationC),
new DecoratedBox(decoration: kBoxDecorationC), ])
]) );
);
checkTree(tester, [kBoxDecorationA, kBoxDecorationC]);
checkTree(tester, [kBoxDecorationA, kBoxDecorationC]);
flipStatefulComponent(tester);
flipStatefulComponent(tester); tester.pump();
tester.pumpFrameWithoutChange();
checkTree(tester, [kBoxDecorationB, kBoxDecorationC]);
checkTree(tester, [kBoxDecorationB, kBoxDecorationC]);
tester.pumpWidget(
tester.pumpFrame( new Stack([
new Stack([ new FlipComponent(
new FlipComponent( left: new DecoratedBox(decoration: kBoxDecorationA),
left: new DecoratedBox(decoration: kBoxDecorationA), right: new DecoratedBox(decoration: kBoxDecorationB)
right: new DecoratedBox(decoration: kBoxDecorationB) ),
), ])
]) );
);
checkTree(tester, [kBoxDecorationB]);
checkTree(tester, [kBoxDecorationB]);
flipStatefulComponent(tester);
flipStatefulComponent(tester); tester.pump();
tester.pumpFrameWithoutChange();
checkTree(tester, [kBoxDecorationA]);
checkTree(tester, [kBoxDecorationA]);
tester.pumpWidget(
tester.pumpFrame( new Stack([
new Stack([ new FlipComponent(
new FlipComponent( key: new Key('flip'),
key: new Key('flip'), left: new DecoratedBox(decoration: kBoxDecorationA),
left: new DecoratedBox(decoration: kBoxDecorationA), right: new DecoratedBox(decoration: kBoxDecorationB)
right: new DecoratedBox(decoration: kBoxDecorationB) ),
), ])
]) );
);
tester.pumpWidget(
tester.pumpFrame( new Stack([
new Stack([ new DecoratedBox(key: new Key('c'), decoration: kBoxDecorationC),
new DecoratedBox(key: new Key('c'), decoration: kBoxDecorationC), new FlipComponent(
new FlipComponent( key: new Key('flip'),
key: new Key('flip'), left: new DecoratedBox(decoration: kBoxDecorationA),
left: new DecoratedBox(decoration: kBoxDecorationA), right: new DecoratedBox(decoration: kBoxDecorationB)
right: new DecoratedBox(decoration: kBoxDecorationB) ),
), ])
]) );
);
checkTree(tester, [kBoxDecorationC, kBoxDecorationA]);
checkTree(tester, [kBoxDecorationC, kBoxDecorationA]);
flipStatefulComponent(tester);
flipStatefulComponent(tester); tester.pump();
tester.pumpFrameWithoutChange();
checkTree(tester, [kBoxDecorationC, kBoxDecorationB]);
checkTree(tester, [kBoxDecorationC, kBoxDecorationB]);
tester.pumpWidget(
tester.pumpFrame( new Stack([
new Stack([ new FlipComponent(
new FlipComponent( key: new Key('flip'),
key: new Key('flip'), left: new DecoratedBox(decoration: kBoxDecorationA),
left: new DecoratedBox(decoration: kBoxDecorationA), right: new DecoratedBox(decoration: kBoxDecorationB)
right: new DecoratedBox(decoration: kBoxDecorationB) ),
), new DecoratedBox(key: new Key('c'), decoration: kBoxDecorationC),
new DecoratedBox(key: new Key('c'), decoration: kBoxDecorationC), ])
]) );
);
checkTree(tester, [kBoxDecorationB, kBoxDecorationC]);
checkTree(tester, [kBoxDecorationB, kBoxDecorationC]); });
}); });
} }
import 'package:sky/animation.dart';
import 'package:sky/widgets.dart'; import 'package:sky/widgets.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
...@@ -48,36 +47,35 @@ class SecondComponentState extends State<SecondComponent> { ...@@ -48,36 +47,35 @@ class SecondComponentState extends State<SecondComponent> {
void main() { void main() {
test('Can navigator navigate to and from a stateful component', () { test('Can navigator navigate to and from a stateful component', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
final Map<String, RouteBuilder> routes = <String, RouteBuilder>{
'/': (navigator, route) => new FirstComponent(navigator),
'/second': (navigator, route) => new SecondComponent(navigator),
};
final Map<String, RouteBuilder> routes = <String, RouteBuilder>{ tester.pumpWidget(new Navigator(routes: routes));
'/': (navigator, route) => new FirstComponent(navigator),
'/second': (navigator, route) => new SecondComponent(navigator),
};
tester.pumpFrame(new Navigator(routes: routes)); expect(tester.findText('X'), isNotNull);
expect(tester.findText('Y'), isNull);
expect(tester.findText('X'), isNotNull); tester.tap(tester.findText('X'));
expect(tester.findText('Y'), isNull); tester.pump(const Duration(milliseconds: 10));
tester.tap(tester.findText('X')); expect(tester.findText('X'), isNotNull);
scheduler.beginFrame(10.0); expect(tester.findText('Y'), isNotNull);
expect(tester.findText('X'), isNotNull); tester.pump(const Duration(milliseconds: 10));
expect(tester.findText('Y'), isNotNull); tester.pump(const Duration(milliseconds: 10));
tester.pump(const Duration(seconds: 1));
scheduler.beginFrame(20.0); tester.tap(tester.findText('Y'));
scheduler.beginFrame(30.0); tester.pump(const Duration(milliseconds: 10));
scheduler.beginFrame(1000.0); tester.pump(const Duration(milliseconds: 10));
tester.pump(const Duration(milliseconds: 10));
tester.tap(tester.findText('Y')); tester.pump(const Duration(seconds: 1));
scheduler.beginFrame(1010.0);
scheduler.beginFrame(1020.0);
scheduler.beginFrame(1030.0);
scheduler.beginFrame(2000.0);
expect(tester.findText('X'), isNotNull);
expect(tester.findText('Y'), isNull);
expect(tester.findText('X'), isNotNull);
expect(tester.findText('Y'), isNull);
});
}); });
} }
import 'package:quiver/testing/async.dart';
import 'package:sky/widgets.dart'; import 'package:sky/widgets.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
...@@ -33,13 +32,10 @@ Widget buildFrame() { ...@@ -33,13 +32,10 @@ Widget buildFrame() {
void page(WidgetTester tester, Offset offset) { void page(WidgetTester tester, Offset offset) {
String itemText = currentPage != null ? currentPage.toString() : '0'; String itemText = currentPage != null ? currentPage.toString() : '0';
new FakeAsync().run((async) { tester.scroll(tester.findText(itemText), offset);
tester.scroll(tester.findText(itemText), offset); // One frame to start the animation, a second to complete it.
// One frame to start the animation, a second to complete it. tester.pump();
tester.pumpFrameWithoutChange(); tester.pump(const Duration(seconds: 1));
tester.pumpFrameWithoutChange(1000.0);
async.elapse(new Duration(seconds: 1));
});
} }
void pageLeft(WidgetTester tester) { void pageLeft(WidgetTester tester) {
...@@ -54,59 +50,65 @@ void main() { ...@@ -54,59 +50,65 @@ void main() {
// PageableList with itemsWrap: false // PageableList with itemsWrap: false
test('Scroll left from page 0 to page 1', () { test('Scroll left from page 0 to page 1', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
currentPage = null; currentPage = null;
itemsWrap = false; itemsWrap = false;
tester.pumpFrame(buildFrame()); tester.pumpWidget(buildFrame());
expect(currentPage, isNull); expect(currentPage, isNull);
pageLeft(tester); pageLeft(tester);
expect(currentPage, equals(1)); expect(currentPage, equals(1));
});
}); });
test('Scroll right from page 1 to page 0', () { test('Scroll right from page 1 to page 0', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
itemsWrap = false; itemsWrap = false;
tester.pumpFrame(buildFrame()); tester.pumpWidget(buildFrame());
expect(currentPage, equals(1)); expect(currentPage, equals(1));
pageRight(tester); pageRight(tester);
expect(currentPage, equals(0)); expect(currentPage, equals(0));
});
}); });
test('Scroll right from page 0 does nothing (underscroll)', () { test('Scroll right from page 0 does nothing (underscroll)', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
itemsWrap = false; itemsWrap = false;
tester.pumpFrame(buildFrame()); tester.pumpWidget(buildFrame());
expect(currentPage, equals(0)); expect(currentPage, equals(0));
pageRight(tester); pageRight(tester);
expect(currentPage, equals(0)); expect(currentPage, equals(0));
});
}); });
// PageableList with itemsWrap: true // PageableList with itemsWrap: true
test('Scroll left page 0 to page 1, itemsWrap: true', () { test('Scroll left page 0 to page 1, itemsWrap: true', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.reset(); tester.pumpWidget(new Container());
currentPage = null; currentPage = null;
itemsWrap = true; itemsWrap = true;
tester.pumpFrame(buildFrame()); tester.pumpWidget(buildFrame());
expect(currentPage, isNull); expect(currentPage, isNull);
pageLeft(tester); pageLeft(tester);
expect(currentPage, equals(1)); expect(currentPage, equals(1));
});
}); });
test('Scroll right from page 1 to page 0, itemsWrap: true', () { test('Scroll right from page 1 to page 0, itemsWrap: true', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.pumpFrame(buildFrame()); tester.pumpWidget(buildFrame());
expect(currentPage, equals(1)); expect(currentPage, equals(1));
pageRight(tester); pageRight(tester);
expect(currentPage, equals(0)); expect(currentPage, equals(0));
});
}); });
test('Scroll right from page 0 to page 5, itemsWrap: true (underscroll)', () { test('Scroll right from page 0 to page 5, itemsWrap: true (underscroll)', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.pumpFrame(buildFrame()); tester.pumpWidget(buildFrame());
expect(currentPage, equals(0)); expect(currentPage, equals(0));
pageRight(tester); pageRight(tester);
expect(currentPage, equals(5)); expect(currentPage, equals(5));
});
}); });
} }
...@@ -58,230 +58,230 @@ void main() { ...@@ -58,230 +58,230 @@ void main() {
}); });
test('ParentDataWidget control test', () { test('ParentDataWidget control test', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.pumpFrame(
new Stack([
new DecoratedBox(decoration: kBoxDecorationA),
new Positioned(
top: 10.0,
left: 10.0,
child: new DecoratedBox(decoration: kBoxDecorationB)
),
new DecoratedBox(decoration: kBoxDecorationC),
])
);
checkTree(tester, [
kNonPositioned,
new TestParentData(top: 10.0, left: 10.0),
kNonPositioned,
]);
tester.pumpFrame(
new Stack([
new Positioned(
bottom: 5.0,
right: 7.0,
child: new DecoratedBox(decoration: kBoxDecorationA)
),
new Positioned(
top: 10.0,
left: 10.0,
child: new DecoratedBox(decoration: kBoxDecorationB)
),
new DecoratedBox(decoration: kBoxDecorationC),
])
);
checkTree(tester, [
new TestParentData(bottom: 5.0, right: 7.0),
new TestParentData(top: 10.0, left: 10.0),
kNonPositioned,
]);
DecoratedBox kDecoratedBoxA = new DecoratedBox(decoration: kBoxDecorationA);
DecoratedBox kDecoratedBoxB = new DecoratedBox(decoration: kBoxDecorationB);
DecoratedBox kDecoratedBoxC = new DecoratedBox(decoration: kBoxDecorationC);
tester.pumpFrame(
new Stack([
new Positioned(
bottom: 5.0,
right: 7.0,
child: kDecoratedBoxA
),
new Positioned(
top: 10.0,
left: 10.0,
child: kDecoratedBoxB
),
kDecoratedBoxC,
])
);
checkTree(tester, [
new TestParentData(bottom: 5.0, right: 7.0),
new TestParentData(top: 10.0, left: 10.0),
kNonPositioned,
]);
tester.pumpFrame(
new Stack([
new Positioned(
bottom: 6.0,
right: 8.0,
child: kDecoratedBoxA
),
new Positioned(
left: 10.0,
right: 10.0,
child: kDecoratedBoxB
),
kDecoratedBoxC,
])
);
checkTree(tester, [
new TestParentData(bottom: 6.0, right: 8.0),
new TestParentData(left: 10.0, right: 10.0),
kNonPositioned,
]);
tester.pumpFrame(
new Stack([
kDecoratedBoxA,
new Positioned(
left: 11.0,
right: 12.0,
child: new Container(child: kDecoratedBoxB)
),
kDecoratedBoxC,
])
);
checkTree(tester, [
kNonPositioned,
new TestParentData(left: 11.0, right: 12.0),
kNonPositioned,
]);
tester.pumpFrame(
new Stack([
kDecoratedBoxA,
new Positioned(
right: 10.0,
child: new Container(child: kDecoratedBoxB)
),
new Container(
child: new Positioned(
top: 8.0,
child: kDecoratedBoxC
)
)
])
);
checkTree(tester, [
kNonPositioned,
new TestParentData(right: 10.0),
new TestParentData(top: 8.0),
]);
tester.pumpFrame(
new Stack([
new Positioned(
right: 10.0,
child: new FlipComponent(left: kDecoratedBoxA, right: kDecoratedBoxB)
),
])
);
checkTree(tester, [
new TestParentData(right: 10.0),
]);
flipStatefulComponent(tester);
tester.pumpFrameWithoutChange();
checkTree(tester, [
new TestParentData(right: 10.0),
]);
tester.pumpFrame(
new Stack([
new Positioned(
top: 7.0,
child: new FlipComponent(left: kDecoratedBoxA, right: kDecoratedBoxB)
),
])
);
checkTree(tester, [
new TestParentData(top: 7.0),
]);
flipStatefulComponent(tester);
tester.pumpFrameWithoutChange();
checkTree(tester, [
new TestParentData(top: 7.0),
]);
tester.pumpFrame(
new Stack([])
);
checkTree(tester, []);
});
test('ParentDataWidget conflicting data', () { tester.pumpWidget(
WidgetTester tester = new WidgetTester(); new Stack([
new DecoratedBox(decoration: kBoxDecorationA),
expect(cachedException, isNull); new Positioned(
top: 10.0,
tester.pumpFrame( left: 10.0,
new Stack([
new Positioned(
top: 5.0,
bottom: 8.0,
child: new Positioned(
top: 6.0,
left: 7.0,
child: new DecoratedBox(decoration: kBoxDecorationB) child: new DecoratedBox(decoration: kBoxDecorationB)
) ),
) new DecoratedBox(decoration: kBoxDecorationC),
]) ])
); );
expect(cachedException, isNotNull); checkTree(tester, [
cachedException = null; kNonPositioned,
new TestParentData(top: 10.0, left: 10.0),
kNonPositioned,
]);
tester.pumpWidget(
new Stack([
new Positioned(
bottom: 5.0,
right: 7.0,
child: new DecoratedBox(decoration: kBoxDecorationA)
),
new Positioned(
top: 10.0,
left: 10.0,
child: new DecoratedBox(decoration: kBoxDecorationB)
),
new DecoratedBox(decoration: kBoxDecorationC),
])
);
tester.pumpFrame(new Stack([])); checkTree(tester, [
new TestParentData(bottom: 5.0, right: 7.0),
new TestParentData(top: 10.0, left: 10.0),
kNonPositioned,
]);
checkTree(tester, []); DecoratedBox kDecoratedBoxA = new DecoratedBox(decoration: kBoxDecorationA);
expect(cachedException, isNull); DecoratedBox kDecoratedBoxB = new DecoratedBox(decoration: kBoxDecorationB);
DecoratedBox kDecoratedBoxC = new DecoratedBox(decoration: kBoxDecorationC);
tester.pumpFrame( tester.pumpWidget(
new Container( new Stack([
child: new Flex([
new Positioned( new Positioned(
top: 6.0, bottom: 5.0,
left: 7.0, right: 7.0,
child: new DecoratedBox(decoration: kBoxDecorationB) child: kDecoratedBoxA
),
new Positioned(
top: 10.0,
left: 10.0,
child: kDecoratedBoxB
),
kDecoratedBoxC,
])
);
checkTree(tester, [
new TestParentData(bottom: 5.0, right: 7.0),
new TestParentData(top: 10.0, left: 10.0),
kNonPositioned,
]);
tester.pumpWidget(
new Stack([
new Positioned(
bottom: 6.0,
right: 8.0,
child: kDecoratedBoxA
),
new Positioned(
left: 10.0,
right: 10.0,
child: kDecoratedBoxB
),
kDecoratedBoxC,
])
);
checkTree(tester, [
new TestParentData(bottom: 6.0, right: 8.0),
new TestParentData(left: 10.0, right: 10.0),
kNonPositioned,
]);
tester.pumpWidget(
new Stack([
kDecoratedBoxA,
new Positioned(
left: 11.0,
right: 12.0,
child: new Container(child: kDecoratedBoxB)
),
kDecoratedBoxC,
])
);
checkTree(tester, [
kNonPositioned,
new TestParentData(left: 11.0, right: 12.0),
kNonPositioned,
]);
tester.pumpWidget(
new Stack([
kDecoratedBoxA,
new Positioned(
right: 10.0,
child: new Container(child: kDecoratedBoxB)
),
new Container(
child: new Positioned(
top: 8.0,
child: kDecoratedBoxC
)
) )
]) ])
) );
);
expect(cachedException, isNotNull); checkTree(tester, [
cachedException = null; kNonPositioned,
new TestParentData(right: 10.0),
new TestParentData(top: 8.0),
]);
tester.pumpWidget(
new Stack([
new Positioned(
right: 10.0,
child: new FlipComponent(left: kDecoratedBoxA, right: kDecoratedBoxB)
),
])
);
checkTree(tester, [
new TestParentData(right: 10.0),
]);
tester.pumpFrame( flipStatefulComponent(tester);
new Stack([]) tester.pump();
);
checkTree(tester, []); checkTree(tester, [
new TestParentData(right: 10.0),
]);
tester.pumpWidget(
new Stack([
new Positioned(
top: 7.0,
child: new FlipComponent(left: kDecoratedBoxA, right: kDecoratedBoxB)
),
])
);
checkTree(tester, [
new TestParentData(top: 7.0),
]);
flipStatefulComponent(tester);
tester.pump();
checkTree(tester, [
new TestParentData(top: 7.0),
]);
tester.pumpWidget(
new Stack([])
);
checkTree(tester, []);
});
}); });
test('ParentDataWidget conflicting data', () {
testWidgets((WidgetTester tester) {
expect(cachedException, isNull);
tester.pumpWidget(
new Stack([
new Positioned(
top: 5.0,
bottom: 8.0,
child: new Positioned(
top: 6.0,
left: 7.0,
child: new DecoratedBox(decoration: kBoxDecorationB)
)
)
])
);
expect(cachedException, isNotNull);
cachedException = null;
tester.pumpWidget(new Stack([]));
checkTree(tester, []);
expect(cachedException, isNull);
tester.pumpWidget(
new Container(
child: new Flex([
new Positioned(
top: 6.0,
left: 7.0,
child: new DecoratedBox(decoration: kBoxDecorationB)
)
])
)
);
expect(cachedException, isNotNull);
cachedException = null;
tester.pumpWidget(
new Stack([])
);
checkTree(tester, []);
});
});
} }
...@@ -7,15 +7,15 @@ import 'widget_tester.dart'; ...@@ -7,15 +7,15 @@ import '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(); testWidgets((WidgetTester tester) {
tester.pumpWidget(new Block([new LinearProgressIndicator(value: 0.0)]));
tester.pumpFrame(new Block([new LinearProgressIndicator(value: 0.0)])); List<Layer> layers1 = tester.layers;
List<Layer> layers1 = tester.layers; tester.pumpWidget(new Block([new LinearProgressIndicator(value: 0.5)]));
tester.pumpFrame(new Block([new LinearProgressIndicator(value: 0.5)])); List<Layer> layers2 = tester.layers;
expect(layers1, isNot(equals(layers2)));
List<Layer> layers2 = tester.layers; });
expect(layers1, isNot(equals(layers2)));
}); });
} }
...@@ -16,30 +16,8 @@ class TestComponent extends StatelessComponent { ...@@ -16,30 +16,8 @@ class TestComponent extends StatelessComponent {
void main() { void main() {
test('RenderObjectWidget smoke test', () { test('RenderObjectWidget smoke test', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.pumpWidget(new DecoratedBox(decoration: kBoxDecorationA));
tester.pumpFrame(new DecoratedBox(decoration: kBoxDecorationA));
OneChildRenderObjectElement element =
tester.findElement((element) => element is OneChildRenderObjectElement);
expect(element, isNotNull);
expect(element.renderObject is RenderDecoratedBox, isTrue);
RenderDecoratedBox renderObject = element.renderObject;
expect(renderObject.decoration, equals(kBoxDecorationA));
expect(renderObject.position, equals(BoxDecorationPosition.background));
tester.pumpFrame(new DecoratedBox(decoration: kBoxDecorationB));
element = tester.findElement((element) => element is OneChildRenderObjectElement);
expect(element, isNotNull);
expect(element.renderObject is RenderDecoratedBox, isTrue);
renderObject = element.renderObject;
expect(renderObject.decoration, equals(kBoxDecorationB));
expect(renderObject.position, equals(BoxDecorationPosition.background));
});
test('RenderObjectWidget can add and remove children', () {
WidgetTester tester = new WidgetTester();
void checkFullTree() {
OneChildRenderObjectElement element = OneChildRenderObjectElement element =
tester.findElement((element) => element is OneChildRenderObjectElement); tester.findElement((element) => element is OneChildRenderObjectElement);
expect(element, isNotNull); expect(element, isNotNull);
...@@ -47,120 +25,144 @@ void main() { ...@@ -47,120 +25,144 @@ void main() {
RenderDecoratedBox renderObject = element.renderObject; RenderDecoratedBox renderObject = element.renderObject;
expect(renderObject.decoration, equals(kBoxDecorationA)); expect(renderObject.decoration, equals(kBoxDecorationA));
expect(renderObject.position, equals(BoxDecorationPosition.background)); expect(renderObject.position, equals(BoxDecorationPosition.background));
expect(renderObject.child, isNotNull);
expect(renderObject.child is RenderDecoratedBox, isTrue);
RenderDecoratedBox child = renderObject.child;
expect(child.decoration, equals(kBoxDecorationB));
expect(child.position, equals(BoxDecorationPosition.background));
expect(child.child, isNull);
}
void childBareTree() { tester.pumpWidget(new DecoratedBox(decoration: kBoxDecorationB));
OneChildRenderObjectElement element = element = tester.findElement((element) => element is OneChildRenderObjectElement);
tester.findElement((element) => element is OneChildRenderObjectElement);
expect(element, isNotNull); expect(element, isNotNull);
expect(element.renderObject is RenderDecoratedBox, isTrue); expect(element.renderObject is RenderDecoratedBox, isTrue);
RenderDecoratedBox renderObject = element.renderObject; renderObject = element.renderObject;
expect(renderObject.decoration, equals(kBoxDecorationA)); expect(renderObject.decoration, equals(kBoxDecorationB));
expect(renderObject.position, equals(BoxDecorationPosition.background)); expect(renderObject.position, equals(BoxDecorationPosition.background));
expect(renderObject.child, isNull); });
} });
tester.pumpFrame(new DecoratedBox(
decoration: kBoxDecorationA,
child: new DecoratedBox(
decoration: kBoxDecorationB
)
));
checkFullTree();
tester.pumpFrame(new DecoratedBox( test('RenderObjectWidget can add and remove children', () {
decoration: kBoxDecorationA, testWidgets((WidgetTester tester) {
child: new TestComponent(
void checkFullTree() {
OneChildRenderObjectElement element =
tester.findElement((element) => element is OneChildRenderObjectElement);
expect(element, isNotNull);
expect(element.renderObject is RenderDecoratedBox, isTrue);
RenderDecoratedBox renderObject = element.renderObject;
expect(renderObject.decoration, equals(kBoxDecorationA));
expect(renderObject.position, equals(BoxDecorationPosition.background));
expect(renderObject.child, isNotNull);
expect(renderObject.child is RenderDecoratedBox, isTrue);
RenderDecoratedBox child = renderObject.child;
expect(child.decoration, equals(kBoxDecorationB));
expect(child.position, equals(BoxDecorationPosition.background));
expect(child.child, isNull);
}
void childBareTree() {
OneChildRenderObjectElement element =
tester.findElement((element) => element is OneChildRenderObjectElement);
expect(element, isNotNull);
expect(element.renderObject is RenderDecoratedBox, isTrue);
RenderDecoratedBox renderObject = element.renderObject;
expect(renderObject.decoration, equals(kBoxDecorationA));
expect(renderObject.position, equals(BoxDecorationPosition.background));
expect(renderObject.child, isNull);
}
tester.pumpWidget(new DecoratedBox(
decoration: kBoxDecorationA,
child: new DecoratedBox( child: new DecoratedBox(
decoration: kBoxDecorationB decoration: kBoxDecorationB
) )
) ));
));
checkFullTree();
tester.pumpWidget(new DecoratedBox(
decoration: kBoxDecorationA,
child: new TestComponent(
child: new DecoratedBox(
decoration: kBoxDecorationB
)
)
));
checkFullTree(); checkFullTree();
tester.pumpFrame(new DecoratedBox( tester.pumpWidget(new DecoratedBox(
decoration: kBoxDecorationA, decoration: kBoxDecorationA,
child: new DecoratedBox( child: new DecoratedBox(
decoration: kBoxDecorationB decoration: kBoxDecorationB
) )
)); ));
checkFullTree(); checkFullTree();
tester.pumpFrame(new DecoratedBox( tester.pumpWidget(new DecoratedBox(
decoration: kBoxDecorationA decoration: kBoxDecorationA
)); ));
childBareTree(); childBareTree();
tester.pumpFrame(new DecoratedBox( tester.pumpWidget(new DecoratedBox(
decoration: kBoxDecorationA, decoration: kBoxDecorationA,
child: new TestComponent(
child: new TestComponent( child: new TestComponent(
child: new DecoratedBox( child: new TestComponent(
decoration: kBoxDecorationB child: new DecoratedBox(
decoration: kBoxDecorationB
)
) )
) )
) ));
));
checkFullTree(); checkFullTree();
tester.pumpFrame(new DecoratedBox( tester.pumpWidget(new DecoratedBox(
decoration: kBoxDecorationA decoration: kBoxDecorationA
)); ));
childBareTree(); childBareTree();
});
}); });
test('Detached render tree is intact', () { test('Detached render tree is intact', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.pumpFrame(new DecoratedBox( tester.pumpWidget(new DecoratedBox(
decoration: kBoxDecorationA, decoration: kBoxDecorationA,
child: new DecoratedBox(
decoration: kBoxDecorationB,
child: new DecoratedBox( child: new DecoratedBox(
decoration: kBoxDecorationC decoration: kBoxDecorationB,
child: new DecoratedBox(
decoration: kBoxDecorationC
)
) )
) ));
));
OneChildRenderObjectElement element =
OneChildRenderObjectElement element = tester.findElement((element) => element is OneChildRenderObjectElement);
tester.findElement((element) => element is OneChildRenderObjectElement); expect(element.renderObject is RenderDecoratedBox, isTrue);
expect(element.renderObject is RenderDecoratedBox, isTrue); RenderDecoratedBox parent = element.renderObject;
RenderDecoratedBox parent = element.renderObject; expect(parent.child is RenderDecoratedBox, isTrue);
expect(parent.child is RenderDecoratedBox, isTrue); RenderDecoratedBox child = parent.child;
RenderDecoratedBox child = parent.child; expect(child.decoration, equals(kBoxDecorationB));
expect(child.decoration, equals(kBoxDecorationB)); expect(child.child is RenderDecoratedBox, isTrue);
expect(child.child is RenderDecoratedBox, isTrue); RenderDecoratedBox grandChild = child.child;
RenderDecoratedBox grandChild = child.child; expect(grandChild.decoration, equals(kBoxDecorationC));
expect(grandChild.decoration, equals(kBoxDecorationC)); expect(grandChild.child, isNull);
expect(grandChild.child, isNull);
tester.pumpWidget(new DecoratedBox(
tester.pumpFrame(new DecoratedBox( decoration: kBoxDecorationA
decoration: kBoxDecorationA ));
));
element =
element = tester.findElement((element) => element is OneChildRenderObjectElement);
tester.findElement((element) => element is OneChildRenderObjectElement); expect(element.renderObject is RenderDecoratedBox, isTrue);
expect(element.renderObject is RenderDecoratedBox, isTrue); expect(element.renderObject, equals(parent));
expect(element.renderObject, equals(parent)); expect(parent.child, isNull);
expect(parent.child, isNull);
expect(child.parent, isNull);
expect(child.parent, isNull); expect(child.decoration, equals(kBoxDecorationB));
expect(child.decoration, equals(kBoxDecorationB)); expect(child.child, equals(grandChild));
expect(child.child, equals(grandChild)); expect(grandChild.parent, equals(child));
expect(grandChild.parent, equals(child)); expect(grandChild.decoration, equals(kBoxDecorationC));
expect(grandChild.decoration, equals(kBoxDecorationC)); expect(grandChild.child, isNull);
expect(grandChild.child, isNull); });
}); });
} }
...@@ -8,87 +8,89 @@ const List<int> items = const <int>[0, 1, 2, 3, 4, 5]; ...@@ -8,87 +8,89 @@ const List<int> items = const <int>[0, 1, 2, 3, 4, 5];
List<int> tapped = <int>[]; List<int> tapped = <int>[];
void main() { void main() {
double t = 0.0;
WidgetTester tester = new WidgetTester();
test('Tap item after scroll - horizontal', () { test('Tap item after scroll - horizontal', () {
tester.pumpFrame(new Center( testWidgets((WidgetTester tester) {
child: new Container( tester.pumpWidget(new Container());
height: 50.0, tester.pumpWidget(new Center(
child: new ScrollableList<int>( child: new Container(
key: new GlobalKey(), height: 50.0,
items: items, child: new ScrollableList<int>(
itemBuilder: (BuildContext context, int item) { key: new GlobalKey(),
return new Container( items: items,
key: new ValueKey<int>(item), itemBuilder: (BuildContext context, int item) {
child: new GestureDetector( return new Container(
onTap: () { tapped.add(item); }, key: new ValueKey<int>(item),
child: new Text('$item') child: new GestureDetector(
) onTap: () { tapped.add(item); },
); child: new Text('$item')
}, )
itemExtent: 290.0, );
scrollDirection: ScrollDirection.horizontal },
itemExtent: 290.0,
scrollDirection: ScrollDirection.horizontal
)
) )
) ));
), t); tester.scroll(tester.findText('2'), const Offset(-280.0, 0.0));
tester.scroll(tester.findText('2'), const Offset(-280.0, 0.0)); tester.pump(const Duration(seconds: 1));
tester.pumpFrameWithoutChange(t += 1000.0); // screen is 800px wide, and has the following items:
// screen is 800px wide, and has the following items: // -280..10 = 0
// -280..10 = 0 // 10..300 = 1
// 10..300 = 1 // 300..590 = 2
// 300..590 = 2 // 590..880 = 3
// 590..880 = 3 expect(tester.findText('0'), isNotNull);
expect(tester.findText('0'), isNotNull); expect(tester.findText('1'), isNotNull);
expect(tester.findText('1'), isNotNull); expect(tester.findText('2'), isNotNull);
expect(tester.findText('2'), isNotNull); expect(tester.findText('3'), isNotNull);
expect(tester.findText('3'), isNotNull); expect(tester.findText('4'), isNull);
expect(tester.findText('4'), isNull); expect(tester.findText('5'), isNull);
expect(tester.findText('5'), isNull); expect(tapped, equals([]));
expect(tapped, equals([])); tester.tap(tester.findText('2'));
tester.tap(tester.findText('2')); expect(tapped, equals([2]));
expect(tapped, equals([2])); });
}); });
test('Tap item after scroll - vertical', () { test('Tap item after scroll - vertical', () {
tester.pumpFrame(new Center( testWidgets((WidgetTester tester) {
child: new Container( tester.pumpWidget(new Container());
width: 50.0, tester.pumpWidget(new Center(
child: new ScrollableList<int>( child: new Container(
key: new GlobalKey(), width: 50.0,
items: items, child: new ScrollableList<int>(
itemBuilder: (BuildContext context, int item) { key: new GlobalKey(),
return new Container( items: items,
key: new ValueKey<int>(item), itemBuilder: (BuildContext context, int item) {
child: new GestureDetector( return new Container(
onTap: () { tapped.add(item); }, key: new ValueKey<int>(item),
child: new Text('$item') child: new GestureDetector(
) onTap: () { tapped.add(item); },
); child: new Text('$item')
}, )
itemExtent: 290.0, );
scrollDirection: ScrollDirection.vertical },
itemExtent: 290.0,
scrollDirection: ScrollDirection.vertical
)
) )
) ));
), t); tester.scroll(tester.findText('1'), const Offset(0.0, -280.0));
tester.scroll(tester.findText('1'), const Offset(0.0, -280.0)); tester.pump(const Duration(seconds: 1));
tester.pumpFrameWithoutChange(t += 1000.0); // screen is 600px tall, and has the following items:
// screen is 600px tall, and has the following items: // -280..10 = 0
// -280..10 = 0 // 10..300 = 1
// 10..300 = 1 // 300..590 = 2
// 300..590 = 2 // 590..880 = 3
// 590..880 = 3 expect(tester.findText('0'), isNotNull);
expect(tester.findText('0'), isNotNull); expect(tester.findText('1'), isNotNull);
expect(tester.findText('1'), isNotNull); expect(tester.findText('2'), isNotNull);
expect(tester.findText('2'), isNotNull); expect(tester.findText('3'), isNotNull);
expect(tester.findText('3'), isNotNull); expect(tester.findText('4'), isNull);
expect(tester.findText('4'), isNull); expect(tester.findText('5'), isNull);
expect(tester.findText('5'), isNull); expect(tapped, equals([2]));
expect(tapped, equals([2])); tester.tap(tester.findText('1'));
tester.tap(tester.findText('1')); expect(tapped, equals([2, 1]));
expect(tapped, equals([2, 1])); tester.tap(tester.findText('3'));
tester.tap(tester.findText('3')); expect(tapped, equals([2, 1])); // the center of the third item is off-screen so it shouldn't get hit
expect(tapped, equals([2, 1])); // the center of the third item is off-screen so it shouldn't get hit });
}); });
} }
...@@ -26,135 +26,124 @@ Widget buildFrame() { ...@@ -26,135 +26,124 @@ Widget buildFrame() {
} }
void main() { void main() {
double t = 0.0; test('Drag horizontally', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.pumpFrame(buildFrame()); tester.pumpWidget(buildFrame());
test('Drag to the left using item 1', () { tester.pump(const Duration(seconds: 1));
tester.pumpFrameWithoutChange(t += 1000.0); tester.scroll(tester.findText('1'), const Offset(-300.0, 0.0));
tester.scroll(tester.findText('1'), const Offset(-300.0, 0.0)); tester.pump(const Duration(seconds: 1));
tester.pumpFrameWithoutChange(t += 1000.0); // screen is 800px wide, and has the following items:
// screen is 800px wide, and has the following items: // -10..280 = 1
// -10..280 = 1 // 280..570 = 2
// 280..570 = 2 // 570..860 = 3
// 570..860 = 3 expect(tester.findText('0'), isNull);
expect(tester.findText('0'), isNull); expect(tester.findText('1'), isNotNull);
expect(tester.findText('1'), isNotNull); expect(tester.findText('2'), isNotNull);
expect(tester.findText('2'), isNotNull); expect(tester.findText('3'), isNotNull);
expect(tester.findText('3'), isNotNull); expect(tester.findText('4'), isNull);
expect(tester.findText('4'), isNull); expect(tester.findText('5'), isNull);
expect(tester.findText('5'), isNull);
});
test('Drag to the left using item 3', () { // the center of item 3 is visible, so this works;
// the center of item 3 is visible, so this works; // if item 3 was a bit wider, such that it's center was past the 800px mark, this would fail,
// if item 3 was a bit wider, such that it's center was past the 800px mark, this would fail, // because it wouldn't be hit tested when scrolling from its center, as scroll() does.
// because it wouldn't be hit tested when scrolling from its center, as scroll() does. tester.pump(const Duration(seconds: 1));
tester.pumpFrameWithoutChange(t += 1000.0); tester.scroll(tester.findText('3'), const Offset(-290.0, 0.0));
tester.scroll(tester.findText('3'), const Offset(-290.0, 0.0)); tester.pump(const Duration(seconds: 1));
tester.pumpFrameWithoutChange(t += 1000.0); // screen is 800px wide, and has the following items:
// screen is 800px wide, and has the following items: // -10..280 = 2
// -10..280 = 2 // 280..570 = 3
// 280..570 = 3 // 570..860 = 4
// 570..860 = 4 expect(tester.findText('0'), isNull);
expect(tester.findText('0'), isNull); expect(tester.findText('1'), isNull);
expect(tester.findText('1'), isNull); expect(tester.findText('2'), isNotNull);
expect(tester.findText('2'), isNotNull); expect(tester.findText('3'), isNotNull);
expect(tester.findText('3'), isNotNull); expect(tester.findText('4'), isNotNull);
expect(tester.findText('4'), isNotNull); expect(tester.findText('5'), isNull);
expect(tester.findText('5'), isNull);
});
test('Drag up using item 3', () { tester.pump(const Duration(seconds: 1));
tester.pumpFrameWithoutChange(t += 1000.0); tester.scroll(tester.findText('3'), const Offset(0.0, -290.0));
tester.scroll(tester.findText('3'), const Offset(0.0, -290.0)); tester.pump(const Duration(seconds: 1));
tester.pumpFrameWithoutChange(t += 1000.0); // unchanged
// unchanged expect(tester.findText('0'), isNull);
expect(tester.findText('0'), isNull); expect(tester.findText('1'), isNull);
expect(tester.findText('1'), isNull); expect(tester.findText('2'), isNotNull);
expect(tester.findText('2'), isNotNull); expect(tester.findText('3'), isNotNull);
expect(tester.findText('3'), isNotNull); expect(tester.findText('4'), isNotNull);
expect(tester.findText('4'), isNotNull); expect(tester.findText('5'), isNull);
expect(tester.findText('5'), isNull);
});
test('Drag to the left using item 3 again', () { tester.pump(const Duration(seconds: 1));
tester.pumpFrameWithoutChange(t += 1000.0); tester.scroll(tester.findText('3'), const Offset(-290.0, 0.0));
tester.scroll(tester.findText('3'), const Offset(-290.0, 0.0)); tester.pump(const Duration(seconds: 1));
tester.pumpFrameWithoutChange(t += 1000.0); // screen is 800px wide, and has the following items:
// screen is 800px wide, and has the following items: // -10..280 = 3
// -10..280 = 3 // 280..570 = 4
// 280..570 = 4 // 570..860 = 5
// 570..860 = 5 expect(tester.findText('0'), isNull);
expect(tester.findText('0'), isNull); expect(tester.findText('1'), isNull);
expect(tester.findText('1'), isNull); expect(tester.findText('2'), isNull);
expect(tester.findText('2'), isNull); expect(tester.findText('3'), isNotNull);
expect(tester.findText('3'), isNotNull); expect(tester.findText('4'), isNotNull);
expect(tester.findText('4'), isNotNull); expect(tester.findText('5'), isNotNull);
expect(tester.findText('5'), isNotNull);
});
test('Drag to the left using item 3 again again (past the end of the list)', () { tester.pump(const Duration(seconds: 1));
tester.pumpFrameWithoutChange(t += 1000.0); // at this point we can drag 60 pixels further before we hit the friction zone
// at this point we can drag 60 pixels further before we hit the friction zone // then, every pixel we drag is equivalent to half a pixel of movement
// then, every pixel we drag is equivalent to half a pixel of movement // to move item 3 entirely off screen therefore takes:
// to move item 3 entirely off screen therefore takes: // 60 + (290-60)*2 = 520 pixels
// 60 + (290-60)*2 = 520 pixels // plus a couple more to be sure
// plus a couple more to be sure tester.scroll(tester.findText('3'), const Offset(-522.0, 0.0));
tester.scroll(tester.findText('3'), const Offset(-522.0, 0.0)); tester.pump(); // just after release
tester.pumpFrameWithoutChange(t += 0.0); // just after release // screen is 800px wide, and has the following items:
// screen is 800px wide, and has the following items: // -11..279 = 4
// -11..279 = 4 // 279..569 = 5
// 279..569 = 5 expect(tester.findText('0'), isNull);
expect(tester.findText('0'), isNull); expect(tester.findText('1'), isNull);
expect(tester.findText('1'), isNull); expect(tester.findText('2'), isNull);
expect(tester.findText('2'), isNull); expect(tester.findText('3'), isNull);
expect(tester.findText('3'), isNull); expect(tester.findText('4'), isNotNull);
expect(tester.findText('4'), isNotNull); expect(tester.findText('5'), isNotNull);
expect(tester.findText('5'), isNotNull); tester.pump(const Duration(seconds: 1)); // a second after release
tester.pumpFrameWithoutChange(t += 1000.0); // a second after release // screen is 800px wide, and has the following items:
// screen is 800px wide, and has the following items: // -70..220 = 3
// -70..220 = 3 // 220..510 = 4
// 220..510 = 4 // 510..800 = 5
// 510..800 = 5 expect(tester.findText('0'), isNull);
expect(tester.findText('0'), isNull); expect(tester.findText('1'), isNull);
expect(tester.findText('1'), isNull); expect(tester.findText('2'), isNull);
expect(tester.findText('2'), isNull); expect(tester.findText('3'), isNotNull);
expect(tester.findText('3'), isNotNull); expect(tester.findText('4'), isNotNull);
expect(tester.findText('4'), isNotNull); expect(tester.findText('5'), isNotNull);
expect(tester.findText('5'), isNotNull);
});
test('Drag to the left using item 2 when the scroll offset is big', () { tester.pumpWidget(new Container());
tester.reset(); tester.pumpWidget(buildFrame(), const Duration(seconds: 1));
tester.pumpFrame(buildFrame(), t += 1000.0); tester.scroll(tester.findText('2'), const Offset(-280.0, 0.0));
tester.scroll(tester.findText('2'), const Offset(-280.0, 0.0)); tester.pump(const Duration(seconds: 1));
tester.pumpFrameWithoutChange(t += 1000.0); // screen is 800px wide, and has the following items:
// screen is 800px wide, and has the following items: // -280..10 = 0
// -280..10 = 0 // 10..300 = 1
// 10..300 = 1 // 300..590 = 2
// 300..590 = 2 // 590..880 = 3
// 590..880 = 3 expect(tester.findText('0'), isNotNull);
expect(tester.findText('0'), isNotNull); expect(tester.findText('1'), isNotNull);
expect(tester.findText('1'), isNotNull); expect(tester.findText('2'), isNotNull);
expect(tester.findText('2'), isNotNull); expect(tester.findText('3'), isNotNull);
expect(tester.findText('3'), isNotNull); expect(tester.findText('4'), isNull);
expect(tester.findText('4'), isNull); expect(tester.findText('5'), isNull);
expect(tester.findText('5'), isNull); tester.pump(const Duration(seconds: 1));
tester.pumpFrameWithoutChange(t += 1000.0); tester.scroll(tester.findText('2'), const Offset(-290.0, 0.0));
tester.scroll(tester.findText('2'), const Offset(-290.0, 0.0)); tester.pump(const Duration(seconds: 1));
tester.pumpFrameWithoutChange(t += 1000.0); // screen is 800px wide, and has the following items:
// screen is 800px wide, and has the following items: // -280..10 = 1
// -280..10 = 1 // 10..300 = 2
// 10..300 = 2 // 300..590 = 3
// 300..590 = 3 // 590..880 = 4
// 590..880 = 4 expect(tester.findText('0'), isNull);
expect(tester.findText('0'), isNull); expect(tester.findText('1'), isNotNull);
expect(tester.findText('1'), isNotNull); expect(tester.findText('2'), isNotNull);
expect(tester.findText('2'), isNotNull); expect(tester.findText('3'), isNotNull);
expect(tester.findText('3'), isNotNull); expect(tester.findText('4'), isNotNull);
expect(tester.findText('4'), isNotNull); expect(tester.findText('5'), isNull);
expect(tester.findText('5'), isNull); });
}); });
} }
...@@ -20,52 +20,48 @@ Widget buildFrame() { ...@@ -20,52 +20,48 @@ Widget buildFrame() {
} }
void main() { void main() {
WidgetTester tester = new WidgetTester(); test('Drag vertically', () {
tester.pumpFrame(buildFrame()); testWidgets((WidgetTester tester) {
tester.pumpWidget(buildFrame());
test('Drag up using item 1', () { tester.pump();
tester.pumpFrameWithoutChange(); tester.scroll(tester.findText('1'), const Offset(0.0, -300.0));
tester.scroll(tester.findText('1'), const Offset(0.0, -300.0)); tester.pump();
tester.pumpFrameWithoutChange(); // screen is 600px high, and has the following items:
// screen is 600px high, and has the following items: // -10..280 = 1
// -10..280 = 1 // 280..570 = 2
// 280..570 = 2 // 570..860 = 3
// 570..860 = 3 expect(tester.findText('0'), isNull);
expect(tester.findText('0'), isNull); expect(tester.findText('1'), isNotNull);
expect(tester.findText('1'), isNotNull); expect(tester.findText('2'), isNotNull);
expect(tester.findText('2'), isNotNull); expect(tester.findText('3'), isNotNull);
expect(tester.findText('3'), isNotNull); expect(tester.findText('4'), isNull);
expect(tester.findText('4'), isNull); expect(tester.findText('5'), isNull);
expect(tester.findText('5'), isNull);
});
test('Drag up using item 2', () { tester.pump();
tester.pumpFrameWithoutChange(); tester.scroll(tester.findText('2'), const Offset(0.0, -290.0));
tester.scroll(tester.findText('2'), const Offset(0.0, -290.0)); tester.pump();
tester.pumpFrameWithoutChange(); // screen is 600px high, and has the following items:
// screen is 600px high, and has the following items: // -10..280 = 2
// -10..280 = 2 // 280..570 = 3
// 280..570 = 3 // 570..860 = 4
// 570..860 = 4 expect(tester.findText('0'), isNull);
expect(tester.findText('0'), isNull); expect(tester.findText('1'), isNull);
expect(tester.findText('1'), isNull); expect(tester.findText('2'), isNotNull);
expect(tester.findText('2'), isNotNull); expect(tester.findText('3'), isNotNull);
expect(tester.findText('3'), isNotNull); expect(tester.findText('4'), isNotNull);
expect(tester.findText('4'), isNotNull); expect(tester.findText('5'), isNull);
expect(tester.findText('5'), isNull);
});
test('Drag to the left using item 3', () { tester.pump();
tester.pumpFrameWithoutChange(); tester.scroll(tester.findText('3'), const Offset(-300.0, 0.0));
tester.scroll(tester.findText('3'), const Offset(-300.0, 0.0)); tester.pump();
tester.pumpFrameWithoutChange(); // nothing should have changed
// nothing should have changed expect(tester.findText('0'), isNull);
expect(tester.findText('0'), isNull); expect(tester.findText('1'), isNull);
expect(tester.findText('1'), isNull); expect(tester.findText('2'), isNotNull);
expect(tester.findText('2'), isNotNull); expect(tester.findText('3'), isNotNull);
expect(tester.findText('3'), isNotNull); expect(tester.findText('4'), isNotNull);
expect(tester.findText('4'), isNotNull); expect(tester.findText('5'), isNull);
expect(tester.findText('5'), isNull); });
}); });
} }
...@@ -48,10 +48,11 @@ class LeafState extends State<Leaf> { ...@@ -48,10 +48,11 @@ class LeafState extends State<Leaf> {
void main() { void main() {
test('three-way setState() smoke test', () { test('three-way setState() smoke test', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.pumpFrame(new Changer(new Wrapper(new Leaf()))); tester.pumpWidget(new Changer(new Wrapper(new Leaf())));
tester.pumpFrame(new Changer(new Wrapper(new Leaf()))); tester.pumpWidget(new Changer(new Wrapper(new Leaf())));
changer.test(); changer.test();
tester.pumpFrameWithoutChange(); tester.pump();
});
}); });
} }
...@@ -54,13 +54,14 @@ class OutsideState extends State<Outside> { ...@@ -54,13 +54,14 @@ class OutsideState extends State<Outside> {
void main() { void main() {
test('setState() smoke test', () { test('setState() smoke test', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.pumpFrame(new Outside()); tester.pumpWidget(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.pump();
tester.dispatchEvent(pointer.up(), location); tester.dispatchEvent(pointer.up(), location);
tester.pumpFrameWithoutChange(); tester.pump();
});
}); });
} }
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
import 'dart:async'; import 'dart:async';
import 'package:quiver/testing/async.dart';
import 'package:sky/widgets.dart'; import 'package:sky/widgets.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
...@@ -59,91 +58,60 @@ Future fling(double velocity) { ...@@ -59,91 +58,60 @@ Future fling(double velocity) {
} }
void main() { void main() {
WidgetTester tester = new WidgetTester();
tester.pumpFrame(buildFrame());
test('ScrollableList snap scrolling, fling(-800)', () { test('ScrollableList snap scrolling, fling(-800)', () {
scrollOffset = 0.0; testWidgets((WidgetTester tester) {
tester.pumpFrameWithoutChange(); tester.pumpWidget(buildFrame());
expect(scrollOffset, 0.0);
scrollOffset = 0.0;
tester.pump();
expect(scrollOffset, 0.0);
Duration dt = const Duration(seconds: 2);
double t0 = 0.0;
int dt = 2000;
new FakeAsync().run((async) {
fling(-800.0); fling(-800.0);
tester.pumpFrameWithoutChange(t0); // Start the scheduler at 0.0 tester.pump(); // Start the scheduler at 0.0
tester.pumpFrameWithoutChange(t0 + dt); tester.pump(dt);
async.elapse(new Duration(milliseconds: dt));
expect(scrollOffset, closeTo(200.0, 1.0)); expect(scrollOffset, closeTo(200.0, 1.0));
});
});
test('ScrollableList snap scrolling, fling(-2000)', () { scrollOffset = 0.0;
scrollOffset = 0.0; tester.pump();
tester.pumpFrameWithoutChange(); expect(scrollOffset, 0.0);
expect(scrollOffset, 0.0);
double t0 = 0.0;
int dt = 2000;
new FakeAsync().run((async) {
fling(-2000.0); fling(-2000.0);
tester.pumpFrameWithoutChange(t0); tester.pump();
tester.pumpFrameWithoutChange(t0 + dt); tester.pump(dt);
async.elapse(new Duration(milliseconds: dt));
expect(scrollOffset, closeTo(400.0, 1.0)); expect(scrollOffset, closeTo(400.0, 1.0));
});
});
test('ScrollableList snap scrolling, fling(800)', () { scrollOffset = 400.0;
scrollOffset = 400.0; tester.pump();
tester.pumpFrameWithoutChange(); expect(scrollOffset, 400.0);
expect(scrollOffset, 400.0);
double t0 = 0.0;
int dt = 2000;
new FakeAsync().run((async) {
fling(800.0); fling(800.0);
tester.pumpFrameWithoutChange(t0); tester.pump();
tester.pumpFrameWithoutChange(t0 + dt); tester.pump(dt);
async.elapse(new Duration(milliseconds: dt));
expect(scrollOffset, closeTo(0.0, 1.0)); expect(scrollOffset, closeTo(0.0, 1.0));
});
});
test('ScrollableList snap scrolling, fling(2000)', () { scrollOffset = 800.0;
scrollOffset = 800.0; tester.pump();
tester.pumpFrameWithoutChange(); expect(scrollOffset, 800.0);
expect(scrollOffset, 800.0);
double t0 = 0.0;
int dt = 2000;
new FakeAsync().run((async) {
fling(2000.0); fling(2000.0);
tester.pumpFrameWithoutChange(t0); tester.pump();
tester.pumpFrameWithoutChange(t0 + dt); tester.pump(dt);
async.elapse(new Duration(milliseconds: dt));
expect(scrollOffset, closeTo(200.0, 1.0)); expect(scrollOffset, closeTo(200.0, 1.0));
});
});
test('ScrollableList snap scrolling, fling(2000).then()', () { scrollOffset = 800.0;
scrollOffset = 800.0; tester.pump();
tester.pumpFrameWithoutChange(); expect(scrollOffset, 800.0);
expect(scrollOffset, 800.0);
double t0 = 0.0; bool completed = false;
int dt = 2000;
bool completed = false;
new FakeAsync().run((async) {
fling(2000.0).then((_) { fling(2000.0).then((_) {
completed = true; completed = true;
expect(scrollOffset, closeTo(200.0, 1.0)); expect(scrollOffset, closeTo(200.0, 1.0));
}); });
tester.pumpFrameWithoutChange(t0); tester.pump();
tester.pumpFrameWithoutChange(t0 + dt); tester.pump(dt);
async.elapse(new Duration(milliseconds: dt));
expect(completed, true); expect(completed, true);
}); });
}); });
} }
...@@ -5,70 +5,69 @@ import 'widget_tester.dart'; ...@@ -5,70 +5,69 @@ import 'widget_tester.dart';
void main() { void main() {
test('Can change position data', () { test('Can change position data', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
Key key = new Key('container');
Key key = new Key('container'); tester.pumpWidget(
new Stack([
tester.pumpFrame( new Positioned(
new Stack([ left: 10.0,
new Positioned( child: new Container(
left: 10.0, key: key,
child: new Container( width: 10.0,
key: key, height: 10.0
width: 10.0, )
height: 10.0
) )
) ])
]) );
);
Element container = tester.findElementByKey(key); 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.pumpWidget(
new Stack([ new Stack([
new Positioned( new Positioned(
right: 10.0, right: 10.0,
child: new Container( child: new Container(
key: key, key: key,
width: 10.0, width: 10.0,
height: 10.0 height: 10.0
)
) )
) ])
]) );
);
container = tester.findElementByKey(key); 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);
expect(container.renderObject.parentData.left, isNull); expect(container.renderObject.parentData.left, isNull);
});
}); });
test('Can remove parent data', () { test('Can remove parent data', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
Key key = new Key('container');
Key key = new Key('container'); Container container = new Container(key: key, width: 10.0, height: 10.0);
Container container = new Container(key: key, width: 10.0, height: 10.0);
tester.pumpFrame(new Stack([ new Positioned(left: 10.0, child: container) ])); tester.pumpWidget(new Stack([ new Positioned(left: 10.0, child: container) ]));
Element containerElement = tester.findElementByKey(key); Element containerElement = tester.findElementByKey(key);
expect(containerElement.renderObject.parentData.top, isNull); expect(containerElement.renderObject.parentData.top, isNull);
expect(containerElement.renderObject.parentData.right, isNull); expect(containerElement.renderObject.parentData.right, isNull);
expect(containerElement.renderObject.parentData.bottom, isNull); expect(containerElement.renderObject.parentData.bottom, isNull);
expect(containerElement.renderObject.parentData.left, equals(10.0)); expect(containerElement.renderObject.parentData.left, equals(10.0));
tester.pumpFrame(new Stack([ container ])); tester.pumpWidget(new Stack([ container ]));
containerElement = tester.findElementByKey(key); containerElement = tester.findElementByKey(key);
expect(containerElement.renderObject.parentData.top, isNull); expect(containerElement.renderObject.parentData.top, isNull);
expect(containerElement.renderObject.parentData.right, isNull); expect(containerElement.renderObject.parentData.right, isNull);
expect(containerElement.renderObject.parentData.bottom, isNull); expect(containerElement.renderObject.parentData.bottom, isNull);
expect(containerElement.renderObject.parentData.left, isNull); expect(containerElement.renderObject.parentData.left, isNull);
});
}); });
} }
...@@ -7,68 +7,69 @@ import 'test_widgets.dart'; ...@@ -7,68 +7,69 @@ import 'test_widgets.dart';
void main() { void main() {
test('Stateful component smoke test', () { test('Stateful component smoke test', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
void checkTree(BoxDecoration expectedDecoration) { void checkTree(BoxDecoration expectedDecoration) {
OneChildRenderObjectElement element = OneChildRenderObjectElement element =
tester.findElement((element) => element is OneChildRenderObjectElement); tester.findElement((element) => element is OneChildRenderObjectElement);
expect(element, isNotNull); expect(element, isNotNull);
expect(element.renderObject is RenderDecoratedBox, isTrue); expect(element.renderObject is RenderDecoratedBox, isTrue);
RenderDecoratedBox renderObject = element.renderObject; RenderDecoratedBox renderObject = element.renderObject;
expect(renderObject.decoration, equals(expectedDecoration)); expect(renderObject.decoration, equals(expectedDecoration));
} }
tester.pumpFrame( tester.pumpWidget(
new FlipComponent( new FlipComponent(
left: new DecoratedBox(decoration: kBoxDecorationA), left: new DecoratedBox(decoration: kBoxDecorationA),
right: new DecoratedBox(decoration: kBoxDecorationB) right: new DecoratedBox(decoration: kBoxDecorationB)
) )
); );
checkTree(kBoxDecorationA); checkTree(kBoxDecorationA);
tester.pumpFrame( tester.pumpWidget(
new FlipComponent( new FlipComponent(
left: new DecoratedBox(decoration: kBoxDecorationB), left: new DecoratedBox(decoration: kBoxDecorationB),
right: new DecoratedBox(decoration: kBoxDecorationA) right: new DecoratedBox(decoration: kBoxDecorationA)
) )
); );
checkTree(kBoxDecorationB); checkTree(kBoxDecorationB);
flipStatefulComponent(tester); flipStatefulComponent(tester);
tester.pumpFrameWithoutChange(); tester.pump();
checkTree(kBoxDecorationA); checkTree(kBoxDecorationA);
tester.pumpFrame( tester.pumpWidget(
new FlipComponent( new FlipComponent(
left: new DecoratedBox(decoration: kBoxDecorationA), left: new DecoratedBox(decoration: kBoxDecorationA),
right: new DecoratedBox(decoration: kBoxDecorationB) right: new DecoratedBox(decoration: kBoxDecorationB)
) )
); );
checkTree(kBoxDecorationB); checkTree(kBoxDecorationB);
});
}); });
test('Don\'t rebuild subcomponents', () { test('Don\'t rebuild subcomponents', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.pumpFrame( tester.pumpWidget(
new FlipComponent( new FlipComponent(
key: new Key('rebuild test'), // this is so we don't get the state from the TestComponentConfig in the last test, but instead instantiate a new element with a new state. key: new Key('rebuild test'), // this is so we don't get the state from the TestComponentConfig in the last test, but instead instantiate a new element with a new state.
left: new TestBuildCounter(), left: new TestBuildCounter(),
right: new DecoratedBox(decoration: kBoxDecorationB) right: new DecoratedBox(decoration: kBoxDecorationB)
) )
); );
expect(TestBuildCounter.buildCount, equals(1)); expect(TestBuildCounter.buildCount, equals(1));
flipStatefulComponent(tester); flipStatefulComponent(tester);
tester.pumpFrameWithoutChange(); tester.pump();
expect(TestBuildCounter.buildCount, equals(1)); expect(TestBuildCounter.buildCount, equals(1));
});
}); });
} }
...@@ -37,45 +37,44 @@ class OuterContainerState extends State<OuterContainer> { ...@@ -37,45 +37,44 @@ class OuterContainerState extends State<OuterContainer> {
void main() { void main() {
test('resync stateful widget', () { test('resync stateful widget', () {
testWidgets((WidgetTester tester) {
Key innerKey = new Key('inner');
Key outerKey = new Key('outer');
WidgetTester tester = new WidgetTester(); InnerComponent inner1 = new InnerComponent(key: innerKey);
InnerComponent inner2;
OuterContainer outer1 = new OuterContainer(key: outerKey, child: inner1);
OuterContainer outer2;
Key innerKey = new Key('inner'); tester.pumpWidget(outer1);
Key outerKey = new Key('outer');
InnerComponent inner1 = new InnerComponent(key: innerKey); StatefulComponentElement innerElement = tester.findElementByKey(innerKey);
InnerComponent inner2; InnerComponentState innerElementState = innerElement.state;
OuterContainer outer1 = new OuterContainer(key: outerKey, child: inner1); expect(innerElementState.config, equals(inner1));
OuterContainer outer2; expect(innerElementState._didInitState, isTrue);
expect(innerElement.renderObject.attached, isTrue);
tester.pumpFrame(outer1); inner2 = new InnerComponent(key: innerKey);
outer2 = new OuterContainer(key: outerKey, child: inner2);
StatefulComponentElement innerElement = tester.findElementByKey(innerKey); tester.pumpWidget(outer2);
InnerComponentState innerElementState = innerElement.state;
expect(innerElementState.config, equals(inner1));
expect(innerElementState._didInitState, isTrue);
expect(innerElement.renderObject.attached, isTrue);
inner2 = new InnerComponent(key: innerKey); expect(tester.findElementByKey(innerKey), equals(innerElement));
outer2 = new OuterContainer(key: outerKey, child: inner2); expect(innerElement.state, equals(innerElementState));
tester.pumpFrame(outer2); expect(innerElementState.config, equals(inner2));
expect(innerElementState._didInitState, isTrue);
expect(innerElement.renderObject.attached, isTrue);
expect(tester.findElementByKey(innerKey), equals(innerElement)); StatefulComponentElement outerElement = tester.findElementByKey(outerKey);
expect(innerElement.state, equals(innerElementState)); expect(outerElement.state.config, equals(outer2));
outerElement.state.setState(() {});
tester.pump();
expect(innerElementState.config, equals(inner2)); expect(tester.findElementByKey(innerKey), equals(innerElement));
expect(innerElementState._didInitState, isTrue); expect(innerElement.state, equals(innerElementState));
expect(innerElement.renderObject.attached, isTrue); expect(innerElementState.config, equals(inner2));
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);
}); });
} }
...@@ -37,164 +37,160 @@ class TestWidgetState extends State<TestWidget> { ...@@ -37,164 +37,160 @@ class TestWidgetState extends State<TestWidget> {
void main() { void main() {
test('no change', () { test('no change', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
tester.pumpWidget(
tester.pumpFrame( new Container(
new Container( child: new Container(
child: new Container( child: new TestWidget(
child: new TestWidget( persistentState: 1,
persistentState: 1, child: new Container()
child: new Container() )
) )
) )
) );
);
TestWidgetState state = tester.findStateOfType(TestWidgetState); TestWidgetState state = tester.findStateOfType(TestWidgetState);
expect(state.persistentState, equals(1)); expect(state.persistentState, equals(1));
expect(state.updates, equals(0)); expect(state.updates, equals(0));
tester.pumpFrame( tester.pumpWidget(
new Container( new Container(
child: new Container( child: new Container(
child: new TestWidget( child: new TestWidget(
persistentState: 2, persistentState: 2,
child: new Container() child: new Container()
)
) )
) )
) );
);
expect(state.persistentState, equals(1)); expect(state.persistentState, equals(1));
expect(state.updates, equals(1)); expect(state.updates, equals(1));
tester.pumpFrame(new Container()); tester.pumpWidget(new Container());
});
}); });
test('remove one', () { test('remove one', () {
testWidgets((WidgetTester tester) {
WidgetTester tester = new WidgetTester(); tester.pumpWidget(
new Container(
tester.pumpFrame( child: new Container(
new Container( child: new TestWidget(
child: new Container( persistentState: 10,
child: new TestWidget( child: new Container()
persistentState: 10, )
child: new Container()
) )
) )
) );
);
TestWidgetState state = tester.findStateOfType(TestWidgetState); TestWidgetState state = tester.findStateOfType(TestWidgetState);
expect(state.persistentState, equals(10)); expect(state.persistentState, equals(10));
expect(state.updates, equals(0)); expect(state.updates, equals(0));
tester.pumpFrame( tester.pumpWidget(
new Container( new Container(
child: new TestWidget( child: new TestWidget(
persistentState: 11, persistentState: 11,
child: new Container() child: new Container()
)
) )
) );
);
state = tester.findStateOfType(TestWidgetState); state = tester.findStateOfType(TestWidgetState);
expect(state.persistentState, equals(11)); expect(state.persistentState, equals(11));
expect(state.updates, equals(0)); expect(state.updates, equals(0));
tester.pumpFrame(new Container()); tester.pumpWidget(new Container());
});
}); });
test('swap instances around', () { test('swap instances around', () {
testWidgets((WidgetTester tester) {
WidgetTester tester = new WidgetTester(); Widget a = new TestWidget(persistentState: 0x61, syncedState: 0x41, child: new Text('apple'));
Widget b = new TestWidget(persistentState: 0x62, syncedState: 0x42, child: new Text('banana'));
Widget a = new TestWidget(persistentState: 0x61, syncedState: 0x41, child: new Text('apple')); tester.pumpWidget(new Column([]));
Widget b = new TestWidget(persistentState: 0x62, syncedState: 0x42, child: new Text('banana'));
tester.pumpFrame(new Column([])); GlobalKey keyA = new GlobalKey();
GlobalKey keyB = new GlobalKey();
GlobalKey keyA = new GlobalKey();
GlobalKey keyB = new GlobalKey(); tester.pumpWidget(
new Column([
tester.pumpFrame( new Container(
new Column([ key: keyA,
new Container( child: a
key: keyA, ),
child: a new Container(
), key: keyB,
new Container( child: b
key: keyB, )
child: b ])
) );
])
); TestWidgetState first, second;
TestWidgetState first, second; first = tester.findStateByConfig(a);
second = tester.findStateByConfig(b);
first = tester.findStateByConfig(a);
second = tester.findStateByConfig(b); expect(first.config, equals(a));
expect(first.persistentState, equals(0x61));
expect(first.config, equals(a)); expect(first.syncedState, equals(0x41));
expect(first.persistentState, equals(0x61)); expect(second.config, equals(b));
expect(first.syncedState, equals(0x41)); expect(second.persistentState, equals(0x62));
expect(second.config, equals(b)); expect(second.syncedState, equals(0x42));
expect(second.persistentState, equals(0x62));
expect(second.syncedState, equals(0x42)); tester.pumpWidget(
new Column([
tester.pumpFrame( new Container(
new Column([ key: keyA,
new Container( child: a
key: keyA, ),
child: a new Container(
), key: keyB,
new Container( child: b
key: keyB, )
child: b ])
) );
])
); first = tester.findStateByConfig(a);
second = tester.findStateByConfig(b);
first = tester.findStateByConfig(a);
second = tester.findStateByConfig(b); // same as before
expect(first.config, equals(a));
// same as before expect(first.persistentState, equals(0x61));
expect(first.config, equals(a)); expect(first.syncedState, equals(0x41));
expect(first.persistentState, equals(0x61)); expect(second.config, equals(b));
expect(first.syncedState, equals(0x41)); expect(second.persistentState, equals(0x62));
expect(second.config, equals(b)); expect(second.syncedState, equals(0x42));
expect(second.persistentState, equals(0x62));
expect(second.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
// 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.pumpWidget(
new Column([
tester.pumpFrame( new Container(
new Column([ key: keyA,
new Container( child: b
key: keyA, ),
child: b new Container(
), key: keyB,
new Container( child: a
key: keyB, )
child: a ])
) );
])
); first = tester.findStateByConfig(b);
second = tester.findStateByConfig(a);
first = tester.findStateByConfig(b);
second = tester.findStateByConfig(a); expect(first.config, equals(b));
expect(first.persistentState, equals(0x61));
expect(first.config, equals(b)); expect(first.syncedState, equals(0x42));
expect(first.persistentState, equals(0x61)); expect(second.config, equals(a));
expect(first.syncedState, equals(0x42)); expect(second.persistentState, equals(0x62));
expect(second.config, equals(a)); expect(second.syncedState, equals(0x41));
expect(second.persistentState, equals(0x62)); });
expect(second.syncedState, equals(0x41));
}); });
} }
...@@ -5,47 +5,47 @@ import 'widget_tester.dart'; ...@@ -5,47 +5,47 @@ import 'widget_tester.dart';
void main() { void main() {
test('Transform origin', () { test('Transform origin', () {
WidgetTester tester = new WidgetTester(); testWidgets((WidgetTester tester) {
bool didReceiveTap = false;
bool didReceiveTap = false; tester.pumpWidget(
tester.pumpFrame( new Stack([
new Stack([ new Positioned(
new Positioned( top: 100.0,
top: 100.0, left: 100.0,
left: 100.0, child: new Container(
child: new Container( width: 100.0,
width: 100.0, height: 100.0,
height: 100.0, decoration: new BoxDecoration(
decoration: new BoxDecoration( backgroundColor: new Color(0xFF0000FF)
backgroundColor: new Color(0xFF0000FF) )
) )
) ),
), new Positioned(
new Positioned( top: 100.0,
top: 100.0, left: 100.0,
left: 100.0, child: new Container(
child: new Container( width: 100.0,
width: 100.0, height: 100.0,
height: 100.0, child: new Transform(
child: new Transform( transform: new Matrix4.identity().scale(0.5, 0.5),
transform: new Matrix4.identity().scale(0.5, 0.5), origin: new Offset(100.0, 50.0),
origin: new Offset(100.0, 50.0), child: new GestureDetector(
child: new GestureDetector( onTap: () {
onTap: () { didReceiveTap = true;
didReceiveTap = true; },
}, child: new Container()
child: new Container() )
) )
) )
) )
) ])
]) );
);
expect(didReceiveTap, isFalse); expect(didReceiveTap, isFalse);
tester.tapAt(new Point(110.0, 110.0)); tester.tapAt(new Point(110.0, 110.0));
expect(didReceiveTap, isFalse); expect(didReceiveTap, isFalse);
tester.tapAt(new Point(190.0, 150.0)); tester.tapAt(new Point(190.0, 150.0));
expect(didReceiveTap, isTrue); expect(didReceiveTap, isTrue);
});
}); });
} }
...@@ -3,6 +3,8 @@ import 'dart:sky' as sky; ...@@ -3,6 +3,8 @@ import 'dart:sky' as sky;
import 'package:sky/animation.dart'; import 'package:sky/animation.dart';
import 'package:sky/rendering.dart'; import 'package:sky/rendering.dart';
import 'package:sky/widgets.dart'; import 'package:sky/widgets.dart';
import 'package:quiver/testing/async.dart';
import 'package:quiver/time.dart';
import '../engine/mock_events.dart'; import '../engine/mock_events.dart';
...@@ -24,21 +26,24 @@ class RootComponentState extends State<RootComponent> { ...@@ -24,21 +26,24 @@ class RootComponentState extends State<RootComponent> {
} }
class WidgetTester { class WidgetTester {
WidgetTester._(FakeAsync async)
: async = async,
clock = async.getClock(new DateTime.utc(2015, 1, 1)) {
}
// See thttps://github.com/flutter/engine/issues/1084 regarding frameTimeMs vs FakeAsync final FakeAsync async;
final Clock clock;
void pumpFrame(Widget widget, [ double frameTimeMs = 0.0 ]) { void pumpWidget(Widget widget, [ Duration duration ]) {
runApp(widget); runApp(widget);
scheduler.beginFrame(frameTimeMs); pump(duration);
}
void pumpFrameWithoutChange([ double frameTimeMs = 0.0 ]) {
scheduler.beginFrame(frameTimeMs);
} }
void reset() { void pump([ Duration duration ]) {
runApp(new Container()); if (duration != null)
scheduler.beginFrame(0.0); async.elapse(duration);
scheduler.beginFrame(clock.now().millisecondsSinceEpoch.toDouble());
async.flushMicrotasks();
} }
List<Layer> _layers(Layer layer) { List<Layer> _layers(Layer layer) {
...@@ -162,3 +167,9 @@ class WidgetTester { ...@@ -162,3 +167,9 @@ class WidgetTester {
} }
} }
void testWidgets(callback(WidgetTester tester)) {
new FakeAsync().run((FakeAsync async) {
callback(new WidgetTester._(async));
});
}
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