Commit b02531a9 authored by Hans Muller's avatar Hans Muller

Add Dismissable unit tests

parent 4e01c054
...@@ -64,7 +64,7 @@ class CardCollectionApp extends App { ...@@ -64,7 +64,7 @@ class CardCollectionApp extends App {
}); });
} }
String dismissDirectionText(DismissDirection direction) { String _dismissDirectionText(DismissDirection direction) {
String s = direction.toString(); String s = direction.toString();
return "dismiss ${s.substring(s.indexOf('.') + 1)}"; return "dismiss ${s.substring(s.indexOf('.') + 1)}";
} }
...@@ -85,7 +85,7 @@ class CardCollectionApp extends App { ...@@ -85,7 +85,7 @@ class CardCollectionApp extends App {
icon: icon, icon: icon,
onPressed: () { changeDismissDirection(direction); }, onPressed: () { changeDismissDirection(direction); },
child: new Row([ child: new Row([
new Flexible(child: new Text(dismissDirectionText(direction))), new Flexible(child: new Text(_dismissDirectionText(direction))),
new Radio( new Radio(
value: direction, value: direction,
onChanged: changeDismissDirection, onChanged: changeDismissDirection,
...@@ -116,7 +116,7 @@ class CardCollectionApp extends App { ...@@ -116,7 +116,7 @@ class CardCollectionApp extends App {
left: new IconButton(icon: "navigation/menu", onPressed: _handleOpenDrawer), left: new IconButton(icon: "navigation/menu", onPressed: _handleOpenDrawer),
center: new Text('Swipe Away'), center: new Text('Swipe Away'),
right: [ right: [
new Text(dismissDirectionText(_dismissDirection)) new Text(_dismissDirectionText(_dismissDirection))
] ]
); );
} }
......
...@@ -5,70 +5,201 @@ import 'package:test/test.dart'; ...@@ -5,70 +5,201 @@ import 'package:test/test.dart';
import '../engine/mock_events.dart'; import '../engine/mock_events.dart';
import 'widget_tester.dart'; import 'widget_tester.dart';
const double itemExtent = 100.0;
ScrollDirection scrollDirection = ScrollDirection.vertical;
DismissDirection dismissDirection = DismissDirection.horizontal;
List<int> dismissedItems = [];
void handleOnResized(item) {
expect(dismissedItems.contains(item), isFalse);
}
void handleOnDismissed(item) {
expect(dismissedItems.contains(item), isFalse);
dismissedItems.add(item);
}
Widget buildDismissableItem(int item) {
return new Dismissable(
key: new ValueKey<int>(item),
direction: dismissDirection,
onDismissed: () { handleOnDismissed(item); },
onResized: () { handleOnResized(item); },
child: new Container(
width: itemExtent,
height: itemExtent,
child: new Text(item.toString())
)
);
}
Widget widgetBuilder() {
return new Container(
padding: const EdgeDims.all(10.0),
child: new ScrollableList<int>(
items: [0, 1, 2, 3, 4],
itemBuilder: buildDismissableItem,
scrollDirection: scrollDirection,
itemExtent: itemExtent
)
);
}
void dismissItem(WidgetTester tester, int item, { DismissDirection gestureDirection }) {
assert(gestureDirection != DismissDirection.horizontal);
assert(gestureDirection != DismissDirection.vertical);
Widget itemWidget = tester.findText(item.toString());
expect(itemWidget, isNotNull);
Point downLocation;
Point upLocation;
switch(gestureDirection) {
case DismissDirection.left:
// Note: getTopRight() returns a point that's just beyond
// itemWidget's right edge and outside the Dismissable event
// listener's bounds.
downLocation = tester.getTopRight(itemWidget) + const Offset(-0.1, 0.0);
upLocation = tester.getTopLeft(itemWidget);
break;
case DismissDirection.right:
downLocation = tester.getTopLeft(itemWidget);
upLocation = tester.getTopRight(itemWidget);
break;
case DismissDirection.up:
// Note: getBottomLeft() returns a point that's just below
// itemWidget's bottom edge and outside the Dismissable event
// listener's bounds.
downLocation = tester.getBottomLeft(itemWidget) + const Offset(0.0, -0.1);
upLocation = tester.getTopLeft(itemWidget);
break;
case DismissDirection.down:
downLocation = tester.getTopLeft(itemWidget);
upLocation = tester.getBottomLeft(itemWidget);
break;
}
TestPointer pointer = new TestPointer(5);
tester.dispatchEvent(pointer.down(downLocation), downLocation);
tester.dispatchEvent(pointer.move(upLocation), downLocation);
tester.dispatchEvent(pointer.up(), downLocation);
double t0 = 0.0;
new FakeAsync().run((async) {
tester.pumpFrame(widgetBuilder, t0); // start the resize animation
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', () { test('Horizontal drag triggers dismiss scrollDirection=vertical', () {
WidgetTester tester = new WidgetTester();
scrollDirection = ScrollDirection.vertical;
dismissDirection = DismissDirection.horizontal;
dismissedItems = [];
tester.pumpFrame(widgetBuilder);
expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.right);
expect(tester.findText('0'), isNull);
expect(dismissedItems, equals([0]));
dismissItem(tester, 1, gestureDirection: DismissDirection.left);
expect(tester.findText('1'), isNull);
expect(dismissedItems, equals([0, 1]));
});
test('Vertical drag triggers dismiss scrollDirection=horizontal', () {
WidgetTester tester = new WidgetTester();
scrollDirection = ScrollDirection.horizontal;
dismissDirection = DismissDirection.vertical;
dismissedItems = [];
tester.pumpFrame(widgetBuilder);
expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.up);
expect(tester.findText('0'), isNull);
expect(dismissedItems, equals([0]));
dismissItem(tester, 1, gestureDirection: DismissDirection.down);
expect(tester.findText('1'), isNull);
expect(dismissedItems, equals([0, 1]));
});
test('drag-left with DismissDirection.left triggers dismiss', () {
WidgetTester tester = new WidgetTester();
scrollDirection = ScrollDirection.vertical;
dismissDirection = DismissDirection.left;
dismissedItems = [];
tester.pumpFrame(widgetBuilder);
expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.right);
expect(tester.findText('0'), isNotNull);
expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.left);
expect(tester.findText('0'), isNull);
expect(dismissedItems, equals([0]));
});
test('drag-right with DismissDirection.right triggers dismiss', () {
WidgetTester tester = new WidgetTester();
scrollDirection = ScrollDirection.vertical;
dismissDirection = DismissDirection.right;
dismissedItems = [];
tester.pumpFrame(widgetBuilder);
expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.left);
expect(tester.findText('0'), isNotNull);
expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.right);
expect(tester.findText('0'), isNull);
expect(dismissedItems, equals([0]));
});
test('drag-up with DismissDirection.up triggers dismiss', () {
WidgetTester tester = new WidgetTester();
scrollDirection = ScrollDirection.horizontal;
dismissDirection = DismissDirection.up;
dismissedItems = [];
tester.pumpFrame(widgetBuilder);
expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.down);
expect(tester.findText('0'), isNotNull);
expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.up);
expect(tester.findText('0'), isNull);
expect(dismissedItems, equals([0]));
});
test('drag-down with DismissDirection.down triggers dismiss', () {
WidgetTester tester = new WidgetTester(); WidgetTester tester = new WidgetTester();
TestPointer pointer = new TestPointer(5); scrollDirection = ScrollDirection.horizontal;
dismissDirection = DismissDirection.down;
const double itemHeight = 50.0; dismissedItems = [];
List<int> dismissedItems = [];
tester.pumpFrame(widgetBuilder);
void handleOnResized(item) {
expect(dismissedItems.contains(item), isFalse);
}
void handleOnDismissed(item) {
expect(dismissedItems.contains(item), isFalse);
dismissedItems.add(item);
}
Widget buildDismissableItem(int item) {
return new Dismissable(
key: new ValueKey<int>(item),
onDismissed: () { handleOnDismissed(item); },
onResized: () { handleOnResized(item); },
child: new Container(
height: itemHeight,
child: new Text(item.toString())
)
);
}
Widget builder() {
return new Container(
padding: const EdgeDims.all(10.0),
child: new ScrollableList<int>(
items: [0, 1, 2, 3, 4, 5],
itemBuilder: buildDismissableItem,
scrollDirection: ScrollDirection.vertical,
itemExtent: itemHeight
)
);
}
tester.pumpFrame(builder);
Widget item3 = tester.findText("3");
expect(item3, isNotNull);
expect(dismissedItems, isEmpty); expect(dismissedItems, isEmpty);
// Gesture: press-drag-release from the Dismissable's top-left corner dismissItem(tester, 0, gestureDirection: DismissDirection.up);
// to its top-right corner. Triggers the resize animation which concludes expect(tester.findText('0'), isNotNull);
// by calling onDismissed(). expect(dismissedItems, isEmpty);
Point downLocation = tester.getTopLeft(item3);
Point upLocation = tester.getTopRight(item3);
tester.dispatchEvent(pointer.down(downLocation), downLocation);
tester.dispatchEvent(pointer.move(upLocation), upLocation);
tester.dispatchEvent(pointer.up(), upLocation);
new FakeAsync().run((async) {
tester.pumpFrame(builder); // start the resize animation
tester.pumpFrame(builder, 1000.0); // finish the resize animation
async.elapse(new Duration(seconds: 1));
tester.pumpFrame(builder, 2000.0); // dismiss
async.elapse(new Duration(seconds: 1));
expect(dismissedItems, equals([3]));
expect(tester.findText("3"), isNull);
});
dismissItem(tester, 0, gestureDirection: DismissDirection.down);
expect(tester.findText('0'), isNull);
expect(dismissedItems, equals([0]));
}); });
} }
...@@ -76,25 +76,31 @@ class WidgetTester { ...@@ -76,25 +76,31 @@ class WidgetTester {
}); });
} }
Point getCenter(Widget widget) { Point _getWidgetPoint(Widget widget, Function sizeToPoint) {
assert(widget != null); assert(widget != null);
RenderBox box = widget.renderObject as RenderBox; RenderBox box = widget.renderObject as RenderBox;
assert(box != null); assert(box != null);
return box.localToGlobal(box.size.center(Point.origin)); return box.localToGlobal(sizeToPoint(box.size));
}
Point getCenter(Widget widget) {
return _getWidgetPoint(widget, (Size size) => size.center(Point.origin));
} }
Point getTopLeft(Widget widget) { Point getTopLeft(Widget widget) {
assert(widget != null); return _getWidgetPoint(widget, (_) => Point.origin);
RenderBox box = widget.renderObject as RenderBox;
assert(box != null);
return box.localToGlobal(Point.origin);
} }
Point getTopRight(Widget widget) { Point getTopRight(Widget widget) {
assert(widget != null); return _getWidgetPoint(widget, (Size size) => size.topRight(Point.origin));
RenderBox box = widget.renderObject as RenderBox; }
assert(box != null);
return box.localToGlobal(box.size.topRight(Point.origin)); Point getBottomLeft(Widget widget) {
return _getWidgetPoint(widget, (Size size) => size.bottomLeft(Point.origin));
}
Point getBottomRight(Widget widget) {
return _getWidgetPoint(widget, (Size size) => size.bottomRight(Point.origin));
} }
HitTestResult _hitTest(Point location) => SkyBinding.instance.hitTest(location); HitTestResult _hitTest(Point location) => SkyBinding.instance.hitTest(location);
......
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