Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit ffe2b5f

Browse files
committed
add test
1 parent 2c3302b commit ffe2b5f

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

shell/platform/android/io/flutter/embedding/android/AndroidKeyProcessor.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import android.view.View;
1010
import androidx.annotation.NonNull;
1111
import androidx.annotation.Nullable;
12-
import io.flutter.embedding.engine.systemchannels.KeyEventChannel;
1312
import io.flutter.Log;
13+
import io.flutter.embedding.engine.systemchannels.KeyEventChannel;
1414
import io.flutter.plugin.editing.TextInputPlugin;
1515
import java.util.ArrayDeque;
1616
import java.util.Deque;
@@ -94,7 +94,7 @@ public boolean onKeyEvent(@NonNull KeyEvent keyEvent) {
9494
// case the theory is wrong.
9595
return false;
9696
}
97-
if (eventResponder.isPendingEvent(keyEvent)) {
97+
if (isPendingEvent(keyEvent)) {
9898
// If the keyEvent is in the queue of pending events we've seen, and has
9999
// the same id, then we know that this is a re-dispatched keyEvent, and we
100100
// shouldn't respond to it, but we should remove it from tracking now.
@@ -124,7 +124,7 @@ public boolean onKeyEvent(@NonNull KeyEvent keyEvent) {
124124
* @return
125125
*/
126126
public boolean isPendingEvent(@NonNull KeyEvent event) {
127-
return eventResponder.isPendingEvent(event);
127+
return eventResponder.findPendingEvent(event) != null;
128128
}
129129

130130
/**
@@ -215,10 +215,6 @@ private KeyEvent findPendingEvent(KeyEvent event) {
215215
return null;
216216
}
217217

218-
private boolean isPendingEvent(KeyEvent event) {
219-
return findPendingEvent(event) != null;
220-
}
221-
222218
/**
223219
* Called whenever the framework responds that a given key event was handled by the framework.
224220
*

shell/platform/android/test/io/flutter/embedding/android/AndroidKeyProcessorTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,54 @@ public void destroyTest() {
7474
.setEventResponseHandler(isNull(KeyEventChannel.EventResponseHandler.class));
7575
}
7676

77+
public void removesPendingEventsWhenKeyDownHandled() {
78+
FlutterEngine flutterEngine = mockFlutterEngine();
79+
KeyEventChannel fakeKeyEventChannel = flutterEngine.getKeyEventChannel();
80+
View fakeView = mock(View.class);
81+
View fakeRootView = mock(View.class);
82+
when(fakeView.getRootView())
83+
.then(
84+
new Answer<View>() {
85+
@Override
86+
public View answer(InvocationOnMock invocation) throws Throwable {
87+
return fakeRootView;
88+
}
89+
});
90+
91+
ArgumentCaptor<KeyEventChannel.EventResponseHandler> handlerCaptor =
92+
ArgumentCaptor.forClass(KeyEventChannel.EventResponseHandler.class);
93+
verify(fakeKeyEventChannel).setEventResponseHandler(handlerCaptor.capture());
94+
AndroidKeyProcessor processor =
95+
new AndroidKeyProcessor(fakeView, fakeKeyEventChannel, mock(TextInputPlugin.class));
96+
ArgumentCaptor<KeyEventChannel.FlutterKeyEvent> eventCaptor =
97+
ArgumentCaptor.forClass(KeyEventChannel.FlutterKeyEvent.class);
98+
FakeKeyEvent fakeKeyEvent = new FakeKeyEvent(KeyEvent.ACTION_DOWN, 65);
99+
100+
boolean result = processor.onKeyEvent(fakeKeyEvent);
101+
assertEquals(true, processor.isPendingEvent(fakeKeyEvent));
102+
assertEquals(true, result);
103+
104+
// Capture the FlutterKeyEvent so we can find out its event ID to use when
105+
// faking our response.
106+
verify(fakeKeyEventChannel, times(1)).keyDown(eventCaptor.capture());
107+
boolean[] dispatchResult = {true};
108+
when(fakeView.dispatchKeyEvent(any(KeyEvent.class)))
109+
.then(
110+
new Answer<Boolean>() {
111+
@Override
112+
public Boolean answer(InvocationOnMock invocation) throws Throwable {
113+
KeyEvent event = (KeyEvent) invocation.getArguments()[0];
114+
assertEquals(fakeKeyEvent, event);
115+
dispatchResult[0] = processor.onKeyEvent(event);
116+
return dispatchResult[0];
117+
}
118+
});
119+
120+
// Fake a response from the framework.
121+
handlerCaptor.getValue().onKeyEventHandled(eventCaptor.getValue().event);
122+
assertEquals(false, processor.isPendingEvent(fakeKeyEvent));
123+
}
124+
77125
public void synthesizesEventsWhenKeyDownNotHandled() {
78126
FlutterEngine flutterEngine = mockFlutterEngine();
79127
KeyEventChannel fakeKeyEventChannel = flutterEngine.getKeyEventChannel();
@@ -98,6 +146,7 @@ public View answer(InvocationOnMock invocation) throws Throwable {
98146
FakeKeyEvent fakeKeyEvent = new FakeKeyEvent(KeyEvent.ACTION_DOWN, 65);
99147

100148
boolean result = processor.onKeyEvent(fakeKeyEvent);
149+
assertEquals(true, processor.isPendingEvent(fakeKeyEvent));
101150
assertEquals(true, result);
102151

103152
// Capture the FlutterKeyEvent so we can find out its event ID to use when
@@ -118,6 +167,7 @@ public Boolean answer(InvocationOnMock invocation) throws Throwable {
118167

119168
// Fake a response from the framework.
120169
handlerCaptor.getValue().onKeyEventNotHandled(eventCaptor.getValue().event);
170+
assertEquals(true, processor.isPendingEvent(fakeKeyEvent));
121171
verify(fakeView, times(1)).dispatchKeyEvent(fakeKeyEvent);
122172
assertEquals(false, dispatchResult[0]);
123173
verify(fakeKeyEventChannel, times(0)).keyUp(any(KeyEventChannel.FlutterKeyEvent.class));
@@ -148,6 +198,7 @@ public View answer(InvocationOnMock invocation) throws Throwable {
148198
FakeKeyEvent fakeKeyEvent = new FakeKeyEvent(KeyEvent.ACTION_UP, 65);
149199

150200
boolean result = processor.onKeyEvent(fakeKeyEvent);
201+
assertEquals(true, processor.isPendingEvent(fakeKeyEvent));
151202
assertEquals(true, result);
152203

153204
// Capture the FlutterKeyEvent so we can find out its event ID to use when
@@ -168,6 +219,7 @@ public Boolean answer(InvocationOnMock invocation) throws Throwable {
168219

169220
// Fake a response from the framework.
170221
handlerCaptor.getValue().onKeyEventNotHandled(eventCaptor.getValue().event);
222+
assertEquals(true, processor.isPendingEvent(fakeKeyEvent));
171223
verify(fakeView, times(1)).dispatchKeyEvent(fakeKeyEvent);
172224
assertEquals(false, dispatchResult[0]);
173225
verify(fakeKeyEventChannel, times(0)).keyUp(any(KeyEventChannel.FlutterKeyEvent.class));

0 commit comments

Comments
 (0)