Commit 8d87e66f authored by Adam Barth's avatar Adam Barth Committed by GitHub

Defunct RawKeyboardListeners shouldn't receive events (#7058)

parent fcf41fc7
......@@ -82,12 +82,14 @@ class _RawKeyboardListenerState extends State<RawKeyboardListener> {
if (_listening)
return;
RawKeyboard.instance.addListener(_handleRawKeyEvent);
_listening = true;
}
void _detachKeyboardIfAttached() {
if (!_listening)
return;
RawKeyboard.instance.removeListener(_handleRawKeyEvent);
_listening = false;
}
void _handleRawKeyEvent(RawKeyEvent event) {
......
......@@ -51,4 +51,44 @@ void main() {
expect(typedData.codePoint, 0x64);
expect(typedData.modifiers, 0x08);
});
testWidgets('Defunct listeners do not receive events',
(WidgetTester tester) async {
List<RawKeyEvent> events = <RawKeyEvent>[];
await tester.pumpWidget(new RawKeyboardListener(
focused: true,
onKey: (RawKeyEvent event) {
events.add(event);
},
child: new Container(),
));
sendFakeKeyEvent(<String, dynamic>{
'type': 'keydown',
'keymap': 'fuchsia',
'hidUsage': 0x04,
'codePoint': 0x64,
'modifiers': 0x08,
});
await tester.idle();
expect(events.length, 1);
events.clear();
await tester.pumpWidget(new Container());
sendFakeKeyEvent(<String, dynamic>{
'type': 'keydown',
'keymap': 'fuchsia',
'hidUsage': 0x04,
'codePoint': 0x64,
'modifiers': 0x08,
});
await tester.idle();
expect(events.length, 0);
});
}
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