fn3: Listener
In this new world, Listener is just a wrapper around a node in the render tree that hooks directly into the event handling logic.
This commit is contained in:
42
packages/unit/test/fn3/listener_test.dart
Normal file
42
packages/unit/test/fn3/listener_test.dart
Normal file
@@ -0,0 +1,42 @@
|
||||
import 'package:sky/src/fn3.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import 'widget_tester.dart';
|
||||
|
||||
void main() {
|
||||
test('Events bubble up the tree', () {
|
||||
WidgetTester tester = new WidgetTester();
|
||||
|
||||
List<String> log = new List<String>();
|
||||
|
||||
tester.pumpFrame(
|
||||
new Listener(
|
||||
onPointerDown: (_) {
|
||||
log.add('top');
|
||||
},
|
||||
child: new Listener(
|
||||
onPointerDown: (_) {
|
||||
log.add('middle');
|
||||
},
|
||||
child: new DecoratedBox(
|
||||
decoration: const BoxDecoration(),
|
||||
child: new Listener(
|
||||
onPointerDown: (_) {
|
||||
log.add('bottom');
|
||||
},
|
||||
child: new Text('X')
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
tester.tap(tester.findText('X'));
|
||||
|
||||
expect(log, equals([
|
||||
'bottom',
|
||||
'middle',
|
||||
'top',
|
||||
]));
|
||||
});
|
||||
}
|
||||
@@ -1,5 +1,10 @@
|
||||
import 'dart:sky' as sky;
|
||||
|
||||
import 'package:sky/rendering.dart';
|
||||
import 'package:sky/src/fn3.dart';
|
||||
|
||||
import '../engine/mock_events.dart';
|
||||
|
||||
class RootComponent extends StatefulComponent {
|
||||
RootComponentState createState() => new RootComponentState(this);
|
||||
}
|
||||
@@ -20,6 +25,16 @@ class RootComponentState extends ComponentState<RootComponent> {
|
||||
|
||||
class WidgetTester {
|
||||
|
||||
void pumpFrame(Widget widget) {
|
||||
runApp(widget);
|
||||
WidgetFlutterBinding.instance.beginFrame(0.0); // TODO(ianh): https://github.com/flutter/engine/issues/1084
|
||||
}
|
||||
|
||||
void pumpFrameWithoutChange() {
|
||||
WidgetFlutterBinding.instance.beginFrame(0.0); // TODO(ianh): https://github.com/flutter/engine/issues/1084
|
||||
}
|
||||
|
||||
|
||||
void walkElements(ElementVisitor visitor) {
|
||||
void walk(Element element) {
|
||||
visitor(element);
|
||||
@@ -44,13 +59,60 @@ class WidgetTester {
|
||||
return findElement((Element element) => element.widget.key == key);
|
||||
}
|
||||
|
||||
void pumpFrame(Widget widget) {
|
||||
runApp(widget);
|
||||
WidgetFlutterBinding.instance.beginFrame(0.0); // TODO(ianh): https://github.com/flutter/engine/issues/1084
|
||||
Element findText(String text) {
|
||||
return findElement((Element element) {
|
||||
return element.widget is Text && element.widget.data == text;
|
||||
});
|
||||
}
|
||||
|
||||
void pumpFrameWithoutChange() {
|
||||
WidgetFlutterBinding.instance.beginFrame(0.0); // TODO(ianh): https://github.com/flutter/engine/issues/1084
|
||||
|
||||
Point getCenter(Element element) {
|
||||
return _getElementPoint(element, (Size size) => size.center(Point.origin));
|
||||
}
|
||||
|
||||
Point getTopLeft(Element element) {
|
||||
return _getElementPoint(element, (_) => Point.origin);
|
||||
}
|
||||
|
||||
Point getTopRight(Element element) {
|
||||
return _getElementPoint(element, (Size size) => size.topRight(Point.origin));
|
||||
}
|
||||
|
||||
Point getBottomLeft(Element element) {
|
||||
return _getElementPoint(element, (Size size) => size.bottomLeft(Point.origin));
|
||||
}
|
||||
|
||||
Point getBottomRight(Element element) {
|
||||
return _getElementPoint(element, (Size size) => size.bottomRight(Point.origin));
|
||||
}
|
||||
|
||||
Point _getElementPoint(Element element, Function sizeToPoint) {
|
||||
assert(element != null);
|
||||
RenderBox box = element.renderObject as RenderBox;
|
||||
assert(box != null);
|
||||
return box.localToGlobal(sizeToPoint(box.size));
|
||||
}
|
||||
|
||||
|
||||
void tap(Element element, { int pointer: 1 }) {
|
||||
tapAt(getCenter(element), pointer: pointer);
|
||||
}
|
||||
|
||||
void tapAt(Point location, { int pointer: 1 }) {
|
||||
HitTestResult result = _hitTest(location);
|
||||
TestPointer p = new TestPointer(pointer);
|
||||
_dispatchEvent(p.down(location), result);
|
||||
_dispatchEvent(p.up(), result);
|
||||
}
|
||||
|
||||
void dispatchEvent(sky.Event event, Point location) {
|
||||
_dispatchEvent(event, _hitTest(location));
|
||||
}
|
||||
|
||||
HitTestResult _hitTest(Point location) => WidgetFlutterBinding.instance.hitTest(location);
|
||||
|
||||
void _dispatchEvent(sky.Event event, HitTestResult result) {
|
||||
WidgetFlutterBinding.instance.dispatchEvent(event, result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user