Commit d4b1519c authored by Adam Barth's avatar Adam Barth

Port dimissable_test.dart to fn3

parent 1d0573fd
import 'package:quiver/testing/async.dart'; import 'package:quiver/testing/async.dart';
import 'package:sky/widgets.dart'; import 'package:sky/src/fn3.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import '../engine/mock_events.dart'; import '../engine/mock_events.dart';
import 'widget_tester.dart'; import '../fn3/widget_tester.dart';
const double itemExtent = 100.0; const double itemExtent = 100.0;
ScrollDirection scrollDirection = ScrollDirection.vertical; ScrollDirection scrollDirection = ScrollDirection.vertical;
...@@ -19,7 +19,7 @@ void handleOnDismissed(item) { ...@@ -19,7 +19,7 @@ void handleOnDismissed(item) {
dismissedItems.add(item); dismissedItems.add(item);
} }
Widget buildDismissableItem(int item) { Widget buildDismissableItem(BuildContext context, int item) {
return new Dismissable( return new Dismissable(
key: new ValueKey<int>(item), key: new ValueKey<int>(item),
direction: dismissDirection, direction: dismissDirection,
...@@ -37,7 +37,7 @@ Widget widgetBuilder() { ...@@ -37,7 +37,7 @@ Widget widgetBuilder() {
return new Container( return new Container(
padding: const EdgeDims.all(10.0), padding: const EdgeDims.all(10.0),
child: new ScrollableList<int>( child: new ScrollableList<int>(
items: [0, 1, 2, 3, 4], items: [0, 1, 2, 3, 4].where((int i) => !dismissedItems.contains(i)).toList(),
itemBuilder: buildDismissableItem, itemBuilder: buildDismissableItem,
scrollDirection: scrollDirection, scrollDirection: scrollDirection,
itemExtent: itemExtent itemExtent: itemExtent
...@@ -49,8 +49,8 @@ void dismissItem(WidgetTester tester, int item, { DismissDirection gestureDirect ...@@ -49,8 +49,8 @@ void dismissItem(WidgetTester tester, int item, { DismissDirection gestureDirect
assert(gestureDirection != DismissDirection.horizontal); assert(gestureDirection != DismissDirection.horizontal);
assert(gestureDirection != DismissDirection.vertical); assert(gestureDirection != DismissDirection.vertical);
Widget itemWidget = tester.findText(item.toString()); Element itemElement = tester.findText(item.toString());
expect(itemWidget, isNotNull); expect(itemElement, isNotNull);
Point downLocation; Point downLocation;
Point upLocation; Point upLocation;
...@@ -59,23 +59,23 @@ void dismissItem(WidgetTester tester, int item, { DismissDirection gestureDirect ...@@ -59,23 +59,23 @@ void dismissItem(WidgetTester tester, int item, { DismissDirection gestureDirect
// Note: getTopRight() returns a point that's just beyond // Note: getTopRight() returns a point that's just beyond
// itemWidget's right edge and outside the Dismissable event // itemWidget's right edge and outside the Dismissable event
// listener's bounds. // listener's bounds.
downLocation = tester.getTopRight(itemWidget) + const Offset(-0.1, 0.0); downLocation = tester.getTopRight(itemElement) + const Offset(-0.1, 0.0);
upLocation = tester.getTopLeft(itemWidget); upLocation = tester.getTopLeft(itemElement);
break; break;
case DismissDirection.right: case DismissDirection.right:
downLocation = tester.getTopLeft(itemWidget); downLocation = tester.getTopLeft(itemElement);
upLocation = tester.getTopRight(itemWidget); upLocation = tester.getTopRight(itemElement);
break; break;
case DismissDirection.up: case DismissDirection.up:
// Note: getBottomLeft() returns a point that's just below // Note: getBottomLeft() returns a point that's just below
// itemWidget's bottom edge and outside the Dismissable event // itemWidget's bottom edge and outside the Dismissable event
// listener's bounds. // listener's bounds.
downLocation = tester.getBottomLeft(itemWidget) + const Offset(0.0, -0.1); downLocation = tester.getBottomLeft(itemElement) + const Offset(0.0, -0.1);
upLocation = tester.getTopLeft(itemWidget); upLocation = tester.getTopLeft(itemElement);
break; break;
case DismissDirection.down: case DismissDirection.down:
downLocation = tester.getTopLeft(itemWidget); downLocation = tester.getTopLeft(itemElement);
upLocation = tester.getBottomLeft(itemWidget); upLocation = tester.getBottomLeft(itemElement);
break; break;
default: default:
fail("unsupported gestureDirection"); fail("unsupported gestureDirection");
...@@ -88,10 +88,10 @@ void dismissItem(WidgetTester tester, int item, { DismissDirection gestureDirect ...@@ -88,10 +88,10 @@ void dismissItem(WidgetTester tester, int item, { DismissDirection gestureDirect
double t0 = 0.0; double t0 = 0.0;
new FakeAsync().run((async) { new FakeAsync().run((async) {
tester.pumpFrame(widgetBuilder, t0); // start the resize animation tester.pumpFrame(widgetBuilder(), t0); // start the resize animation
tester.pumpFrame(widgetBuilder, t0 + 1000.0); // finish the resize animation tester.pumpFrame(widgetBuilder(), t0 + 1000.0); // finish the resize animation
async.elapse(new Duration(seconds: 1)); async.elapse(new Duration(seconds: 1));
tester.pumpFrame(widgetBuilder, t0 + 2000.0); // dismiss tester.pumpFrame(widgetBuilder(), t0 + 2000.0); // dismiss
async.elapse(new Duration(seconds: 1)); async.elapse(new Duration(seconds: 1));
}); });
} }
...@@ -103,7 +103,7 @@ void main() { ...@@ -103,7 +103,7 @@ void main() {
dismissDirection = DismissDirection.horizontal; dismissDirection = DismissDirection.horizontal;
dismissedItems = []; dismissedItems = [];
tester.pumpFrame(widgetBuilder); tester.pumpFrame(widgetBuilder());
expect(dismissedItems, isEmpty); expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.right); dismissItem(tester, 0, gestureDirection: DismissDirection.right);
...@@ -121,7 +121,7 @@ void main() { ...@@ -121,7 +121,7 @@ void main() {
dismissDirection = DismissDirection.vertical; dismissDirection = DismissDirection.vertical;
dismissedItems = []; dismissedItems = [];
tester.pumpFrame(widgetBuilder); tester.pumpFrame(widgetBuilder());
expect(dismissedItems, isEmpty); expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.up); dismissItem(tester, 0, gestureDirection: DismissDirection.up);
...@@ -139,7 +139,7 @@ void main() { ...@@ -139,7 +139,7 @@ void main() {
dismissDirection = DismissDirection.left; dismissDirection = DismissDirection.left;
dismissedItems = []; dismissedItems = [];
tester.pumpFrame(widgetBuilder); tester.pumpFrame(widgetBuilder());
expect(dismissedItems, isEmpty); expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.right); dismissItem(tester, 0, gestureDirection: DismissDirection.right);
...@@ -157,7 +157,7 @@ void main() { ...@@ -157,7 +157,7 @@ void main() {
dismissDirection = DismissDirection.right; dismissDirection = DismissDirection.right;
dismissedItems = []; dismissedItems = [];
tester.pumpFrame(widgetBuilder); tester.pumpFrame(widgetBuilder());
expect(dismissedItems, isEmpty); expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.left); dismissItem(tester, 0, gestureDirection: DismissDirection.left);
...@@ -175,7 +175,7 @@ void main() { ...@@ -175,7 +175,7 @@ void main() {
dismissDirection = DismissDirection.up; dismissDirection = DismissDirection.up;
dismissedItems = []; dismissedItems = [];
tester.pumpFrame(widgetBuilder); tester.pumpFrame(widgetBuilder());
expect(dismissedItems, isEmpty); expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.down); dismissItem(tester, 0, gestureDirection: DismissDirection.down);
...@@ -193,7 +193,7 @@ void main() { ...@@ -193,7 +193,7 @@ void main() {
dismissDirection = DismissDirection.down; dismissDirection = DismissDirection.down;
dismissedItems = []; dismissedItems = [];
tester.pumpFrame(widgetBuilder); tester.pumpFrame(widgetBuilder());
expect(dismissedItems, isEmpty); expect(dismissedItems, isEmpty);
dismissItem(tester, 0, gestureDirection: DismissDirection.up); dismissItem(tester, 0, gestureDirection: DismissDirection.up);
...@@ -211,21 +211,22 @@ void main() { ...@@ -211,21 +211,22 @@ void main() {
WidgetTester tester = new WidgetTester(); WidgetTester tester = new WidgetTester();
scrollDirection = ScrollDirection.horizontal; scrollDirection = ScrollDirection.horizontal;
dismissDirection = DismissDirection.down; dismissDirection = DismissDirection.down;
dismissedItems = [];
tester.pumpFrame(widgetBuilder); tester.pumpFrame(widgetBuilder());
Widget itemWidget = tester.findText('0'); Element itemElement = tester.findText('0');
TestPointer pointer = new TestPointer(5); TestPointer pointer = new TestPointer(5);
Point location = tester.getTopLeft(itemWidget); 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.pumpFrame(widgetBuilder());
tester.dispatchEvent(pointer.move(location + (offset * 2.0)), location); tester.dispatchEvent(pointer.move(location + (offset * 2.0)), location);
tester.pumpFrame(widgetBuilder); tester.pumpFrame(widgetBuilder());
tester.dispatchEvent(pointer.move(location + (offset * 3.0)), location); tester.dispatchEvent(pointer.move(location + (offset * 3.0)), location);
tester.pumpFrame(widgetBuilder); tester.pumpFrame(widgetBuilder());
tester.dispatchEvent(pointer.move(location + (offset * 4.0)), location); tester.dispatchEvent(pointer.move(location + (offset * 4.0)), location);
tester.pumpFrame(widgetBuilder); tester.pumpFrame(widgetBuilder());
}); });
} }
import 'package:sky/widgets.dart';
import 'package:test/test.dart';
import 'widget_tester.dart';
void main() {
test('Global keys notify add and remove', () {
GlobalKey globalKey = new GlobalKey();
Container container;
bool syncListenerCalled = false;
bool removeListenerCalled = false;
void syncListener(GlobalKey key, Widget widget) {
syncListenerCalled = true;
expect(key, equals(globalKey));
expect(container, isNotNull);
expect(widget, equals(container));
}
void removeListener(GlobalKey key) {
removeListenerCalled = true;
expect(key, equals(globalKey));
}
WidgetTester tester = new WidgetTester();
GlobalKey.registerSyncListener(globalKey, syncListener);
GlobalKey.registerRemoveListener(globalKey, removeListener);
tester.pumpFrame(() {
container = new Container(key: globalKey);
return container;
});
expect(syncListenerCalled, isTrue);
expect(removeListenerCalled, isFalse);
syncListenerCalled = false;
removeListenerCalled = false;
tester.pumpFrame(() => new Container());
expect(syncListenerCalled, isFalse);
expect(removeListenerCalled, isTrue);
syncListenerCalled = false;
removeListenerCalled = false;
GlobalKey.unregisterSyncListener(globalKey, syncListener);
GlobalKey.unregisterRemoveListener(globalKey, removeListener);
tester.pumpFrame(() {
container = new Container(key: globalKey);
return container;
});
expect(syncListenerCalled, isFalse);
expect(removeListenerCalled, isFalse);
tester.pumpFrame(() => new Container());
expect(syncListenerCalled, isFalse);
expect(removeListenerCalled, isFalse);
});
test('Global key reparenting', () {
GlobalKey globalKey = new GlobalKey();
bool syncListenerCalled = false;
bool removeListenerCalled = false;
void syncListener(GlobalKey key, Widget widget) {
syncListenerCalled = true;
}
void removeListener(GlobalKey key) {
removeListenerCalled = true;
}
GlobalKey.registerSyncListener(globalKey, syncListener);
GlobalKey.registerRemoveListener(globalKey, removeListener);
WidgetTester tester = new WidgetTester();
tester.pumpFrame(() {
return new Container(
child: new Container(
key: globalKey
)
);
});
expect(syncListenerCalled, isTrue);
expect(removeListenerCalled, isFalse);
tester.pumpFrame(() {
return new Container(
key: globalKey,
child: new Container()
);
});
expect(syncListenerCalled, isTrue);
expect(removeListenerCalled, isFalse);
tester.pumpFrame(() {
return new Container(
child: new Container(
key: globalKey
)
);
});
expect(syncListenerCalled, isTrue);
expect(removeListenerCalled, isFalse);
GlobalKey.unregisterSyncListener(globalKey, syncListener);
GlobalKey.unregisterRemoveListener(globalKey, removeListener);
});
test('Global key mutate during iteration', () {
GlobalKey globalKey = new GlobalKey();
bool syncListenerCalled = false;
bool removeListenerCalled = false;
void syncListener(GlobalKey key, Widget widget) {
GlobalKey.unregisterSyncListener(globalKey, syncListener);
syncListenerCalled = true;
}
void removeListener(GlobalKey key) {
GlobalKey.unregisterRemoveListener(globalKey, removeListener);
removeListenerCalled = true;
}
GlobalKey.registerSyncListener(globalKey, syncListener);
GlobalKey.registerRemoveListener(globalKey, removeListener);
WidgetTester tester = new WidgetTester();
tester.pumpFrame(() {
return new Container(key: globalKey);
});
expect(syncListenerCalled, isTrue);
expect(removeListenerCalled, isFalse);
syncListenerCalled = false;
removeListenerCalled = false;
tester.pumpFrame(() {
return new Container();
});
expect(syncListenerCalled, isFalse);
expect(removeListenerCalled, isTrue);
syncListenerCalled = false;
removeListenerCalled = false;
tester.pumpFrame(() {
return new Container(key: globalKey);
});
expect(syncListenerCalled, isFalse);
expect(removeListenerCalled, isFalse);
});
}
import 'package:sky/widgets.dart';
import 'package:test/test.dart';
import 'widget_tester.dart';
void main() {
test('Mimic can track tree movements', () {
GlobalKey globalKey = new GlobalKey();
WidgetTester tester = new WidgetTester();
tester.pumpFrame(() {
return new Flex([
new Mimicable(
key: globalKey,
child: new Container(
key: new Key('inner'),
height: 10.0,
width: 10.0
)
)
]);
});
bool mimicReady = false;
tester.pumpFrame(() {
return new Flex([
new Mimicable(
key: globalKey,
child: new Container(
height: 10.0,
width: 10.0
)
),
new SizedBox(
height: 10.0,
width: 10.0,
child: new Mimic(
original: globalKey,
onMimicReady: () {
mimicReady = true;
}
)
)
]);
});
expect(mimicReady, isTrue);
tester.pumpFrame(() {
return new Flex([
new Container(
key: new Key('outer'),
height: 10.0,
width: 10.0,
child: new Mimicable(
key: globalKey,
child: new Container(
key: new Key('inner'),
height: 10.0,
width: 10.0
)
)
),
new SizedBox(
height: 10.0,
width: 10.0,
child: new Mimic(original: globalKey)
)
]);
});
});
}
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