forked from firka/flutter
Add Dismissable unit tests
This commit is contained in:
@@ -64,7 +64,7 @@ class CardCollectionApp extends App {
|
||||
});
|
||||
}
|
||||
|
||||
String dismissDirectionText(DismissDirection direction) {
|
||||
String _dismissDirectionText(DismissDirection direction) {
|
||||
String s = direction.toString();
|
||||
return "dismiss ${s.substring(s.indexOf('.') + 1)}";
|
||||
}
|
||||
@@ -85,7 +85,7 @@ class CardCollectionApp extends App {
|
||||
icon: icon,
|
||||
onPressed: () { changeDismissDirection(direction); },
|
||||
child: new Row([
|
||||
new Flexible(child: new Text(dismissDirectionText(direction))),
|
||||
new Flexible(child: new Text(_dismissDirectionText(direction))),
|
||||
new Radio(
|
||||
value: direction,
|
||||
onChanged: changeDismissDirection,
|
||||
@@ -116,7 +116,7 @@ class CardCollectionApp extends App {
|
||||
left: new IconButton(icon: "navigation/menu", onPressed: _handleOpenDrawer),
|
||||
center: new Text('Swipe Away'),
|
||||
right: [
|
||||
new Text(dismissDirectionText(_dismissDirection))
|
||||
new Text(_dismissDirectionText(_dismissDirection))
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -5,70 +5,201 @@ import 'package:test/test.dart';
|
||||
import '../engine/mock_events.dart';
|
||||
import 'widget_tester.dart';
|
||||
|
||||
void main() {
|
||||
test('Horizontal drag triggers dismiss', () {
|
||||
WidgetTester tester = new WidgetTester();
|
||||
TestPointer pointer = new TestPointer(5);
|
||||
const double itemExtent = 100.0;
|
||||
ScrollDirection scrollDirection = ScrollDirection.vertical;
|
||||
DismissDirection dismissDirection = DismissDirection.horizontal;
|
||||
List<int> dismissedItems = [];
|
||||
|
||||
const double itemHeight = 50.0;
|
||||
List<int> dismissedItems = [];
|
||||
void handleOnResized(item) {
|
||||
expect(dismissedItems.contains(item), isFalse);
|
||||
}
|
||||
|
||||
void handleOnResized(item) {
|
||||
expect(dismissedItems.contains(item), isFalse);
|
||||
}
|
||||
void handleOnDismissed(item) {
|
||||
expect(dismissedItems.contains(item), isFalse);
|
||||
dismissedItems.add(item);
|
||||
}
|
||||
|
||||
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 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 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
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
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
|
||||
)
|
||||
);
|
||||
}
|
||||
void dismissItem(WidgetTester tester, int item, { DismissDirection gestureDirection }) {
|
||||
assert(gestureDirection != DismissDirection.horizontal);
|
||||
assert(gestureDirection != DismissDirection.vertical);
|
||||
|
||||
tester.pumpFrame(builder);
|
||||
Widget item3 = tester.findText("3");
|
||||
expect(item3, isNotNull);
|
||||
expect(dismissedItems, isEmpty);
|
||||
Widget itemWidget = tester.findText(item.toString());
|
||||
expect(itemWidget, isNotNull);
|
||||
|
||||
// Gesture: press-drag-release from the Dismissable's top-left corner
|
||||
// to its top-right corner. Triggers the resize animation which concludes
|
||||
// by calling onDismissed().
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
});
|
||||
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() {
|
||||
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();
|
||||
scrollDirection = ScrollDirection.horizontal;
|
||||
dismissDirection = DismissDirection.down;
|
||||
dismissedItems = [];
|
||||
|
||||
tester.pumpFrame(widgetBuilder);
|
||||
expect(dismissedItems, isEmpty);
|
||||
|
||||
dismissItem(tester, 0, gestureDirection: DismissDirection.up);
|
||||
expect(tester.findText('0'), isNotNull);
|
||||
expect(dismissedItems, isEmpty);
|
||||
|
||||
dismissItem(tester, 0, gestureDirection: DismissDirection.down);
|
||||
expect(tester.findText('0'), isNull);
|
||||
expect(dismissedItems, equals([0]));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -76,25 +76,31 @@ class WidgetTester {
|
||||
});
|
||||
}
|
||||
|
||||
Point getCenter(Widget widget) {
|
||||
Point _getWidgetPoint(Widget widget, Function sizeToPoint) {
|
||||
assert(widget != null);
|
||||
RenderBox box = widget.renderObject as RenderBox;
|
||||
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) {
|
||||
assert(widget != null);
|
||||
RenderBox box = widget.renderObject as RenderBox;
|
||||
assert(box != null);
|
||||
return box.localToGlobal(Point.origin);
|
||||
return _getWidgetPoint(widget, (_) => Point.origin);
|
||||
}
|
||||
|
||||
Point getTopRight(Widget widget) {
|
||||
assert(widget != null);
|
||||
RenderBox box = widget.renderObject as RenderBox;
|
||||
assert(box != null);
|
||||
return box.localToGlobal(box.size.topRight(Point.origin));
|
||||
return _getWidgetPoint(widget, (Size size) => 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);
|
||||
|
||||
Reference in New Issue
Block a user