[Android, Keyboard] Extract KeyboardManager's external dependencies as ViewDelegate; Rework testing framework (flutter/engine#32944)
* Compilable * Test pass * recordChannelCallsTo * respondToTextInputWith * textInputPluginHasTheSecondHighestPrecedence * All tests * Docs
This commit is contained in:
@@ -57,6 +57,7 @@ import io.flutter.embedding.engine.renderer.FlutterRenderer.DisplayFeatureType;
|
||||
import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener;
|
||||
import io.flutter.embedding.engine.renderer.RenderSurface;
|
||||
import io.flutter.embedding.engine.systemchannels.SettingsChannel;
|
||||
import io.flutter.plugin.common.BinaryMessenger;
|
||||
import io.flutter.plugin.editing.TextInputPlugin;
|
||||
import io.flutter.plugin.localization.LocalizationPlugin;
|
||||
import io.flutter.plugin.mouse.MouseCursorPlugin;
|
||||
@@ -98,7 +99,8 @@ import java.util.Set;
|
||||
* See <a>https://source.android.com/devices/graphics/arch-tv#surface_or_texture</a> for more
|
||||
* information comparing {@link android.view.SurfaceView} and {@link android.view.TextureView}.
|
||||
*/
|
||||
public class FlutterView extends FrameLayout implements MouseCursorPlugin.MouseCursorViewDelegate {
|
||||
public class FlutterView extends FrameLayout
|
||||
implements MouseCursorPlugin.MouseCursorViewDelegate, KeyboardManager.ViewDelegate {
|
||||
private static final String TAG = "FlutterView";
|
||||
|
||||
// Internal view hierarchy references.
|
||||
@@ -121,7 +123,7 @@ public class FlutterView extends FrameLayout implements MouseCursorPlugin.MouseC
|
||||
// possibly storing intermediate state, and communicating those events to Flutter.
|
||||
//
|
||||
// These components essentially add some additional behavioral logic on top of
|
||||
// existing, stateless system channels, e.g., KeyEventChannel, TextInputChannel, etc.
|
||||
// existing, stateless system channels, e.g., MouseCursorChannel, TextInputChannel, etc.
|
||||
@Nullable private MouseCursorPlugin mouseCursorPlugin;
|
||||
@Nullable private TextInputPlugin textInputPlugin;
|
||||
@Nullable private LocalizationPlugin localizationPlugin;
|
||||
@@ -1073,6 +1075,25 @@ public class FlutterView extends FrameLayout implements MouseCursorPlugin.MouseC
|
||||
}
|
||||
// -------- End: Mouse ---------
|
||||
|
||||
// -------- Start: Keyboard -------
|
||||
|
||||
@Override
|
||||
public BinaryMessenger getBinaryMessenger() {
|
||||
return flutterEngine.getDartExecutor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTextInputKeyEvent(@NonNull KeyEvent keyEvent) {
|
||||
return textInputPlugin.handleKeyEvent(keyEvent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void redispatch(@NonNull KeyEvent keyEvent) {
|
||||
getRootView().dispatchKeyEvent(keyEvent);
|
||||
}
|
||||
|
||||
// -------- End: Keyboard -------
|
||||
|
||||
/**
|
||||
* Connects this {@code FlutterView} to the given {@link
|
||||
* io.flutter.embedding.engine.FlutterEngine}.
|
||||
@@ -1122,13 +1143,7 @@ public class FlutterView extends FrameLayout implements MouseCursorPlugin.MouseC
|
||||
this.flutterEngine.getPlatformViewsController());
|
||||
localizationPlugin = this.flutterEngine.getLocalizationPlugin();
|
||||
|
||||
keyboardManager =
|
||||
new KeyboardManager(
|
||||
this,
|
||||
textInputPlugin,
|
||||
new KeyChannelResponder[] {
|
||||
new KeyChannelResponder(flutterEngine.getKeyEventChannel())
|
||||
});
|
||||
keyboardManager = new KeyboardManager(this);
|
||||
androidTouchProcessor =
|
||||
new AndroidTouchProcessor(this.flutterEngine.getRenderer(), /*trackMotionEvents=*/ false);
|
||||
accessibilityBridge =
|
||||
|
||||
@@ -5,86 +5,55 @@
|
||||
package io.flutter.embedding.android;
|
||||
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import androidx.annotation.NonNull;
|
||||
import io.flutter.Log;
|
||||
import io.flutter.embedding.android.KeyboardManager.Responder.OnKeyEventHandledCallback;
|
||||
import io.flutter.embedding.engine.systemchannels.KeyEventChannel;
|
||||
import io.flutter.plugin.common.BinaryMessenger;
|
||||
import io.flutter.plugin.editing.InputConnectionAdaptor;
|
||||
import io.flutter.plugin.editing.TextInputPlugin;
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
* A class to process {@link KeyEvent}s dispatched to a {@link FlutterView}, either from a hardware
|
||||
* keyboard or an IME event.
|
||||
*
|
||||
* <p>A class that sends Android {@link KeyEvent} to the a list of {@link
|
||||
* KeyboardManager.Responder}s, and re-dispatches those not handled by the primary responders.
|
||||
* Processes keyboard events and cooperate with {@link TextInputPlugin}.
|
||||
*
|
||||
* <p>Flutter uses asynchronous event handling to avoid blocking the UI thread, but Android requires
|
||||
* that events are handled synchronously. So, when the Android system sends new @{link KeyEvent} to
|
||||
* that events are handled synchronously. So when the Android system sends new @{link KeyEvent} to
|
||||
* Flutter, Flutter responds synchronously that the key has been handled so that it won't propagate
|
||||
* to other components. It then uses "delayed event synthesis", where it sends the event to the
|
||||
* framework, and if the framework responds that it has not handled the event, then this class
|
||||
* synthesizes a new event to send to Android, without handling it this time.
|
||||
*
|
||||
* <p>A new {@link KeyEvent} sent to a {@link KeyboardManager} can be propagated to 3 different
|
||||
* types of responders (in the listed order):
|
||||
* <p>Flutter processes an Android {@link KeyEvent} with several components, each can choose whether
|
||||
* to handled the event, and only unhandled events can move to the next section.
|
||||
*
|
||||
* <ul>
|
||||
* <li>{@link KeyboardManager.Responder}s: An immutable list of key responders in a {@link
|
||||
* KeyboardManager} that each implements the {@link KeyboardManager.Responder} interface. A
|
||||
* {@link KeyboardManager.Responder} is a key responder that's capable of handling {@link
|
||||
* KeyEvent}s asynchronously.
|
||||
* <p>When a new {@link KeyEvent} is received, {@link KeyboardManager} calls the {@link
|
||||
* KeyboardManager.Responder#handleEvent(KeyEvent, OnKeyEventHandledCallback)} method on its
|
||||
* {@link KeyboardManager.Responder}s. Each {@link KeyboardManager.Responder} must call the
|
||||
* supplied {@link OnKeyEventHandledCallback} exactly once, when it has decided whether to
|
||||
* handle the key event callback. More than one {@link KeyboardManager.Responder} is allowed
|
||||
* to reply true and handle the same {@link KeyEvent}.
|
||||
* <p>Typically a {@link KeyboardManager} uses a {@link KeyChannelResponder} as its only
|
||||
* {@link KeyboardManager.Responder}.
|
||||
* <li>{@link TextInputPlugin}: if every {@link KeyboardManager.Responder} has replied false to a
|
||||
* {@link KeyEvent}, or if the {@link KeyboardManager} has zero {@link
|
||||
* KeyboardManager.Responder}s, the {@link KeyEvent} will be sent to the currently focused
|
||||
* editable text field in {@link TextInputPlugin}, if any.
|
||||
* <li><b>"Redispatch"</b>: if there's no currently focused text field in {@link TextInputPlugin},
|
||||
* <li><b>Keyboard</b>: Dispatch to the {@link KeyboardManager.Responder}s simultaneously. After
|
||||
* all responders have responded (asynchronously), the event is considered handled if any
|
||||
* responders decide to handle.
|
||||
* <li><b>Text input</b>: Events are sent to {@link TextInputPlugin}, processed synchronously with
|
||||
* a result of whether it is handled.
|
||||
* <li><b>"Redispatch"</b>: If there's no currently focused text field in {@link TextInputPlugin},
|
||||
* or the text field does not handle the {@link KeyEvent} either, the {@link KeyEvent} will be
|
||||
* sent back to the top of the activity's view hierachy, allowing it to be "redispatched",
|
||||
* only this time the {@link KeyboardManager} will not try to handle the redispatched {@link
|
||||
* KeyEvent}.
|
||||
* sent back to the top of the activity's view hierachy, allowing it to be "redispatched". The
|
||||
* {@link KeyboardManager} will remember this event and skip the identical event at the next
|
||||
* encounter.
|
||||
* </ul>
|
||||
*/
|
||||
public class KeyboardManager {
|
||||
public class KeyboardManager implements InputConnectionAdaptor.KeyboardDelegate {
|
||||
private static final String TAG = "KeyboardManager";
|
||||
|
||||
/**
|
||||
* Constructor for {@link KeyboardManager} that takes a list of {@link
|
||||
* KeyboardManager.Responder}s.
|
||||
* Construct a {@link KeyboardManager}.
|
||||
*
|
||||
* <p>The view is used as the destination to send the synthesized key to. This means that the next
|
||||
* thing in the focus chain will get the event when the {@link KeyboardManager.Responder}s return
|
||||
* false from onKeyDown/onKeyUp.
|
||||
*
|
||||
* <p>It is possible that that in the middle of the async round trip, the focus chain could
|
||||
* change, and instead of the native widget that was "next" when the event was fired getting the
|
||||
* event, it may be the next widget when the event is synthesized that gets it. In practice, this
|
||||
* shouldn't be a huge problem, as this is an unlikely occurrence to happen without user input,
|
||||
* and it may actually be desired behavior, but it is possible.
|
||||
*
|
||||
* @param view takes the activity to use for re-dispatching of events that were not handled by the
|
||||
* framework.
|
||||
* @param textInputPlugin a plugin, which, if set, is given key events before the framework is,
|
||||
* and if it has a valid input connection and is accepting text, then it will handle the event
|
||||
* and the framework will not receive it.
|
||||
* @param responders the {@link KeyboardManager.Responder}s new {@link KeyEvent}s will be first
|
||||
* dispatched to.
|
||||
* @param viewDelegate provides a set of interfaces that the keyboard manager needs to interact
|
||||
* with other components and the platform, and is typically implements by {@link FlutterView}.
|
||||
*/
|
||||
public KeyboardManager(
|
||||
@NonNull View view,
|
||||
@NonNull TextInputPlugin textInputPlugin,
|
||||
@NonNull Responder[] responders) {
|
||||
this.view = view;
|
||||
this.textInputPlugin = textInputPlugin;
|
||||
this.responders = responders;
|
||||
public KeyboardManager(@NonNull ViewDelegate viewDelegate) {
|
||||
this.viewDelegate = viewDelegate;
|
||||
this.responders =
|
||||
new KeyChannelResponder[] {
|
||||
new KeyChannelResponder(new KeyEventChannel(viewDelegate.getBinaryMessenger())),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,7 +72,7 @@ public class KeyboardManager {
|
||||
* {@link KeyEvent} will never be sent to the {@link TextInputPlugin}, and the {@link
|
||||
* KeyboardManager} class can't detect such errors as there is no timeout.
|
||||
*/
|
||||
interface Responder {
|
||||
public interface Responder {
|
||||
interface OnKeyEventHandledCallback {
|
||||
void onKeyEventHandled(boolean canHandleEvent);
|
||||
}
|
||||
@@ -119,8 +88,30 @@ public class KeyboardManager {
|
||||
@NonNull KeyEvent keyEvent, @NonNull OnKeyEventHandledCallback onKeyEventHandledCallback);
|
||||
}
|
||||
|
||||
/**
|
||||
* A set of interfaces that the {@link KeyboardManager} needs to interact with other components
|
||||
* and the platform, and is typically implements by {@link FlutterView}.
|
||||
*/
|
||||
public interface ViewDelegate {
|
||||
/** Returns a {@link BinaryMessenger} to send platform messages with. */
|
||||
public BinaryMessenger getBinaryMessenger();
|
||||
|
||||
/**
|
||||
* Send a {@link KeyEvent} that is not handled by the keyboard responders to the text input
|
||||
* system.
|
||||
*
|
||||
* @param keyEvent the {@link KeyEvent} that should be processed by the text input system. It
|
||||
* must not be null.
|
||||
* @return Whether the text input handles the key event.
|
||||
*/
|
||||
public boolean onTextInputKeyEvent(@NonNull KeyEvent keyEvent);
|
||||
|
||||
/** Send a {@link KeyEvent} that is not handled by Flutter back to the platform. */
|
||||
public void redispatch(@NonNull KeyEvent keyEvent);
|
||||
}
|
||||
|
||||
private class PerEventCallbackBuilder {
|
||||
private class Callback implements OnKeyEventHandledCallback {
|
||||
private class Callback implements Responder.OnKeyEventHandledCallback {
|
||||
boolean isCalled = false;
|
||||
|
||||
@Override
|
||||
@@ -146,16 +137,16 @@ public class KeyboardManager {
|
||||
int unrepliedCount = responders.length;
|
||||
boolean isEventHandled = false;
|
||||
|
||||
public OnKeyEventHandledCallback buildCallback() {
|
||||
public Responder.OnKeyEventHandledCallback buildCallback() {
|
||||
return new Callback();
|
||||
}
|
||||
}
|
||||
|
||||
protected final Responder[] responders;
|
||||
private final HashSet<KeyEvent> redispatchedEvents = new HashSet<>();
|
||||
private final TextInputPlugin textInputPlugin;
|
||||
private final View view;
|
||||
private final ViewDelegate viewDelegate;
|
||||
|
||||
@Override
|
||||
public boolean handleEvent(@NonNull KeyEvent keyEvent) {
|
||||
final boolean isRedispatchedEvent = redispatchedEvents.remove(keyEvent);
|
||||
if (isRedispatchedEvent) {
|
||||
@@ -186,12 +177,12 @@ public class KeyboardManager {
|
||||
}
|
||||
|
||||
private void onUnhandled(@NonNull KeyEvent keyEvent) {
|
||||
if (textInputPlugin.handleKeyEvent(keyEvent) || view == null) {
|
||||
if (viewDelegate == null || viewDelegate.onTextInputKeyEvent(keyEvent)) {
|
||||
return;
|
||||
}
|
||||
|
||||
redispatchedEvents.add(keyEvent);
|
||||
view.getRootView().dispatchKeyEvent(keyEvent);
|
||||
viewDelegate.redispatch(keyEvent);
|
||||
if (redispatchedEvents.remove(keyEvent)) {
|
||||
Log.w(TAG, "A redispatched key event was consumed before reaching KeyboardManager");
|
||||
}
|
||||
|
||||
@@ -25,7 +25,6 @@ import io.flutter.embedding.engine.renderer.FlutterRenderer;
|
||||
import io.flutter.embedding.engine.renderer.RenderSurface;
|
||||
import io.flutter.embedding.engine.systemchannels.AccessibilityChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.DeferredComponentChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.KeyEventChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.LifecycleChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.LocalizationChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.MouseCursorChannel;
|
||||
@@ -87,7 +86,6 @@ public class FlutterEngine {
|
||||
// System channels.
|
||||
@NonNull private final AccessibilityChannel accessibilityChannel;
|
||||
@NonNull private final DeferredComponentChannel deferredComponentChannel;
|
||||
@NonNull private final KeyEventChannel keyEventChannel;
|
||||
@NonNull private final LifecycleChannel lifecycleChannel;
|
||||
@NonNull private final LocalizationChannel localizationChannel;
|
||||
@NonNull private final MouseCursorChannel mouseCursorChannel;
|
||||
@@ -301,7 +299,6 @@ public class FlutterEngine {
|
||||
|
||||
accessibilityChannel = new AccessibilityChannel(dartExecutor, flutterJNI);
|
||||
deferredComponentChannel = new DeferredComponentChannel(dartExecutor);
|
||||
keyEventChannel = new KeyEventChannel(dartExecutor);
|
||||
lifecycleChannel = new LifecycleChannel(dartExecutor);
|
||||
localizationChannel = new LocalizationChannel(dartExecutor);
|
||||
mouseCursorChannel = new MouseCursorChannel(dartExecutor);
|
||||
@@ -481,12 +478,6 @@ public class FlutterEngine {
|
||||
return accessibilityChannel;
|
||||
}
|
||||
|
||||
/** System channel that sends key events from Android to Flutter. */
|
||||
@NonNull
|
||||
public KeyEventChannel getKeyEventChannel() {
|
||||
return keyEventChannel;
|
||||
}
|
||||
|
||||
/** System channel that sends Android lifecycle events to Flutter. */
|
||||
@NonNull
|
||||
public LifecycleChannel getLifecycleChannel() {
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
|
||||
package io.flutter.embedding.engine.systemchannels;
|
||||
|
||||
import android.os.Build;
|
||||
import android.view.InputDevice;
|
||||
import android.view.KeyEvent;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
@@ -72,17 +70,6 @@ public class KeyEventChannel {
|
||||
message.put("character", keyEvent.complexCharacter.toString());
|
||||
}
|
||||
message.put("source", keyEvent.event.getSource());
|
||||
InputDevice device = InputDevice.getDevice(keyEvent.event.getDeviceId());
|
||||
int vendorId = 0;
|
||||
int productId = 0;
|
||||
if (device != null) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
vendorId = device.getVendorId();
|
||||
productId = device.getProductId();
|
||||
}
|
||||
}
|
||||
message.put("vendorId", vendorId);
|
||||
message.put("productId", productId);
|
||||
message.put("deviceId", keyEvent.event.getDeviceId());
|
||||
message.put("repeatCount", keyEvent.event.getRepeatCount());
|
||||
return message;
|
||||
|
||||
@@ -23,15 +23,19 @@ import android.view.inputmethod.EditorInfo;
|
||||
import android.view.inputmethod.ExtractedText;
|
||||
import android.view.inputmethod.ExtractedTextRequest;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
import androidx.annotation.NonNull;
|
||||
import io.flutter.Log;
|
||||
import io.flutter.embedding.android.KeyboardManager;
|
||||
import io.flutter.embedding.engine.FlutterJNI;
|
||||
import io.flutter.embedding.engine.systemchannels.TextInputChannel;
|
||||
|
||||
class InputConnectionAdaptor extends BaseInputConnection
|
||||
public class InputConnectionAdaptor extends BaseInputConnection
|
||||
implements ListenableEditingState.EditingStateWatcher {
|
||||
private static final String TAG = "InputConnectionAdaptor";
|
||||
|
||||
public interface KeyboardDelegate {
|
||||
public boolean handleEvent(@NonNull KeyEvent keyEvent);
|
||||
}
|
||||
|
||||
private final View mFlutterView;
|
||||
private final int mClient;
|
||||
private final TextInputChannel textInputChannel;
|
||||
@@ -44,7 +48,7 @@ class InputConnectionAdaptor extends BaseInputConnection
|
||||
private InputMethodManager mImm;
|
||||
private final Layout mLayout;
|
||||
private FlutterTextUtils flutterTextUtils;
|
||||
private final KeyboardManager keyboardManager;
|
||||
private final KeyboardDelegate keyboardDelegate;
|
||||
private int batchEditNestDepth = 0;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@@ -52,7 +56,7 @@ class InputConnectionAdaptor extends BaseInputConnection
|
||||
View view,
|
||||
int client,
|
||||
TextInputChannel textInputChannel,
|
||||
KeyboardManager keyboardManager,
|
||||
KeyboardDelegate keyboardDelegate,
|
||||
ListenableEditingState editable,
|
||||
EditorInfo editorInfo,
|
||||
FlutterJNI flutterJNI) {
|
||||
@@ -63,7 +67,7 @@ class InputConnectionAdaptor extends BaseInputConnection
|
||||
mEditable = editable;
|
||||
mEditable.addEditingStateListener(this);
|
||||
mEditorInfo = editorInfo;
|
||||
this.keyboardManager = keyboardManager;
|
||||
this.keyboardDelegate = keyboardDelegate;
|
||||
this.flutterTextUtils = new FlutterTextUtils(flutterJNI);
|
||||
// We create a dummy Layout with max width so that the selection
|
||||
// shifting acts as if all text were in one line.
|
||||
@@ -83,10 +87,10 @@ class InputConnectionAdaptor extends BaseInputConnection
|
||||
View view,
|
||||
int client,
|
||||
TextInputChannel textInputChannel,
|
||||
KeyboardManager keyboardManager,
|
||||
KeyboardDelegate keyboardDelegate,
|
||||
ListenableEditingState editable,
|
||||
EditorInfo editorInfo) {
|
||||
this(view, client, textInputChannel, keyboardManager, editable, editorInfo, new FlutterJNI());
|
||||
this(view, client, textInputChannel, keyboardDelegate, editable, editorInfo, new FlutterJNI());
|
||||
}
|
||||
|
||||
private ExtractedText getExtractedText(ExtractedTextRequest request) {
|
||||
@@ -272,7 +276,7 @@ class InputConnectionAdaptor extends BaseInputConnection
|
||||
// occur, and need a chance to be handled by the framework.
|
||||
@Override
|
||||
public boolean sendKeyEvent(KeyEvent event) {
|
||||
return keyboardManager.handleEvent(event);
|
||||
return keyboardDelegate.handleEvent(event);
|
||||
}
|
||||
|
||||
public boolean handleKeyEvent(KeyEvent event) {
|
||||
|
||||
@@ -42,13 +42,11 @@ import androidx.annotation.UiThread;
|
||||
import io.flutter.Log;
|
||||
import io.flutter.app.FlutterPluginRegistry;
|
||||
import io.flutter.embedding.android.AndroidTouchProcessor;
|
||||
import io.flutter.embedding.android.KeyChannelResponder;
|
||||
import io.flutter.embedding.android.KeyboardManager;
|
||||
import io.flutter.embedding.engine.dart.DartExecutor;
|
||||
import io.flutter.embedding.engine.renderer.FlutterRenderer;
|
||||
import io.flutter.embedding.engine.renderer.SurfaceTextureWrapper;
|
||||
import io.flutter.embedding.engine.systemchannels.AccessibilityChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.KeyEventChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.LifecycleChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.LocalizationChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.MouseCursorChannel;
|
||||
@@ -78,7 +76,10 @@ import java.util.concurrent.atomic.AtomicLong;
|
||||
*/
|
||||
@Deprecated
|
||||
public class FlutterView extends SurfaceView
|
||||
implements BinaryMessenger, TextureRegistry, MouseCursorPlugin.MouseCursorViewDelegate {
|
||||
implements BinaryMessenger,
|
||||
TextureRegistry,
|
||||
MouseCursorPlugin.MouseCursorViewDelegate,
|
||||
KeyboardManager.ViewDelegate {
|
||||
/**
|
||||
* Interface for those objects that maintain and expose a reference to a {@code FlutterView} (such
|
||||
* as a full-screen Flutter activity).
|
||||
@@ -121,7 +122,6 @@ public class FlutterView extends SurfaceView
|
||||
private final DartExecutor dartExecutor;
|
||||
private final FlutterRenderer flutterRenderer;
|
||||
private final NavigationChannel navigationChannel;
|
||||
private final KeyEventChannel keyEventChannel;
|
||||
private final LifecycleChannel lifecycleChannel;
|
||||
private final LocalizationChannel localizationChannel;
|
||||
private final PlatformChannel platformChannel;
|
||||
@@ -212,7 +212,6 @@ public class FlutterView extends SurfaceView
|
||||
|
||||
// Create all platform channels
|
||||
navigationChannel = new NavigationChannel(dartExecutor);
|
||||
keyEventChannel = new KeyEventChannel(dartExecutor);
|
||||
lifecycleChannel = new LifecycleChannel(dartExecutor);
|
||||
localizationChannel = new LocalizationChannel(dartExecutor);
|
||||
platformChannel = new PlatformChannel(dartExecutor);
|
||||
@@ -233,11 +232,7 @@ public class FlutterView extends SurfaceView
|
||||
mNativeView.getPluginRegistry().getPlatformViewsController();
|
||||
mTextInputPlugin =
|
||||
new TextInputPlugin(this, new TextInputChannel(dartExecutor), platformViewsController);
|
||||
mKeyboardManager =
|
||||
new KeyboardManager(
|
||||
this,
|
||||
mTextInputPlugin,
|
||||
new KeyChannelResponder[] {new KeyChannelResponder(keyEventChannel)});
|
||||
mKeyboardManager = new KeyboardManager(this);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
mMouseCursorPlugin = new MouseCursorPlugin(this, new MouseCursorChannel(dartExecutor));
|
||||
@@ -814,6 +809,8 @@ public class FlutterView extends SurfaceView
|
||||
}
|
||||
}
|
||||
|
||||
// -------- Start: Mouse -------
|
||||
|
||||
@Override
|
||||
@TargetApi(Build.VERSION_CODES.N)
|
||||
@RequiresApi(Build.VERSION_CODES.N)
|
||||
@@ -822,6 +819,27 @@ public class FlutterView extends SurfaceView
|
||||
return PointerIcon.getSystemIcon(getContext(), type);
|
||||
}
|
||||
|
||||
// -------- End: Mouse -------
|
||||
|
||||
// -------- Start: Keyboard -------
|
||||
|
||||
@Override
|
||||
public BinaryMessenger getBinaryMessenger() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTextInputKeyEvent(@NonNull KeyEvent keyEvent) {
|
||||
return mTextInputPlugin.handleKeyEvent(keyEvent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void redispatch(@NonNull KeyEvent keyEvent) {
|
||||
getRootView().dispatchKeyEvent(keyEvent);
|
||||
}
|
||||
|
||||
// -------- End: Keyboard -------
|
||||
|
||||
@Override
|
||||
@UiThread
|
||||
public TaskQueue makeBackgroundTaskQueue(TaskQueueOptions options) {
|
||||
|
||||
@@ -37,7 +37,6 @@ import io.flutter.embedding.engine.plugins.activity.ActivityControlSurface;
|
||||
import io.flutter.embedding.engine.renderer.FlutterRenderer;
|
||||
import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener;
|
||||
import io.flutter.embedding.engine.systemchannels.AccessibilityChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.KeyEventChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.LifecycleChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.LocalizationChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.MouseCursorChannel;
|
||||
@@ -1111,7 +1110,6 @@ public class FlutterActivityAndFragmentDelegateTest {
|
||||
when(engine.getAccessibilityChannel()).thenReturn(mock(AccessibilityChannel.class));
|
||||
when(engine.getActivityControlSurface()).thenReturn(mock(ActivityControlSurface.class));
|
||||
when(engine.getDartExecutor()).thenReturn(mock(DartExecutor.class));
|
||||
when(engine.getKeyEventChannel()).thenReturn(mock(KeyEventChannel.class));
|
||||
when(engine.getLifecycleChannel()).thenReturn(mock(LifecycleChannel.class));
|
||||
when(engine.getLocalizationChannel()).thenReturn(mock(LocalizationChannel.class));
|
||||
when(engine.getLocalizationPlugin()).thenReturn(mock(LocalizationPlugin.class));
|
||||
|
||||
@@ -1,303 +1,369 @@
|
||||
package io.flutter.embedding.android;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
import io.flutter.embedding.android.KeyboardManager.Responder;
|
||||
import io.flutter.embedding.engine.FlutterEngine;
|
||||
import io.flutter.embedding.engine.FlutterJNI;
|
||||
import io.flutter.embedding.engine.systemchannels.KeyEventChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.TextInputChannel;
|
||||
import io.flutter.plugin.editing.TextInputPlugin;
|
||||
import io.flutter.plugin.common.BinaryMessenger;
|
||||
import io.flutter.plugin.common.JSONMessageCodec;
|
||||
import io.flutter.util.FakeKeyEvent;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
@Config(manifest = Config.NONE)
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@TargetApi(28)
|
||||
public class KeyboardManagerTest {
|
||||
static class FakeResponder implements Responder {
|
||||
KeyEvent mLastKeyEvent;
|
||||
OnKeyEventHandledCallback mLastKeyEventHandledCallback;
|
||||
|
||||
@Override
|
||||
public void handleEvent(
|
||||
@NonNull KeyEvent keyEvent, @NonNull OnKeyEventHandledCallback onKeyEventHandledCallback) {
|
||||
mLastKeyEvent = keyEvent;
|
||||
mLastKeyEventHandledCallback = onKeyEventHandledCallback;
|
||||
/**
|
||||
* Records a message that {@link KeyboardManager} sends to outside.
|
||||
*
|
||||
* <p>A call record can originate from many sources, indicated by its {@link type}. Different
|
||||
* types will have different fields filled, leaving others empty.
|
||||
*/
|
||||
static class CallRecord {
|
||||
static enum Type {
|
||||
/**
|
||||
* The channel responder sent a message through the key event channel.
|
||||
*
|
||||
* <p>This call record will have a non-null {@link channelData}, with an optional {@link
|
||||
* reply}.
|
||||
*/
|
||||
kChannel,
|
||||
}
|
||||
|
||||
void eventHandled(boolean isHandled) {
|
||||
mLastKeyEventHandledCallback.onKeyEventHandled(isHandled);
|
||||
/**
|
||||
* Construct an empty call record.
|
||||
*
|
||||
* <p>Use the static functions to constuct specific types instead.
|
||||
*/
|
||||
private CallRecord() {}
|
||||
|
||||
Type type;
|
||||
|
||||
/**
|
||||
* The callback given by the keyboard manager.
|
||||
*
|
||||
* <p>It might be null, which probably means it is a synthesized event and requires no reply.
|
||||
* Otherwise, invoke this callback with whether the event is handled for the keyboard manager to
|
||||
* continue processing the key event.
|
||||
*/
|
||||
public Consumer<Boolean> reply;
|
||||
/** The data for a call record of type {@link Type.kChannel}. */
|
||||
public ChannelCallData channelData;
|
||||
|
||||
/** Construct a call record of type {@link Type.kChannel}. */
|
||||
static CallRecord channelCall(
|
||||
@NonNull ChannelCallData channelData, @Nullable Consumer<Boolean> reply) {
|
||||
final CallRecord record = new CallRecord();
|
||||
record.type = Type.kChannel;
|
||||
record.channelData = channelData;
|
||||
record.reply = reply;
|
||||
return record;
|
||||
}
|
||||
}
|
||||
|
||||
@Mock FlutterJNI mockFlutterJni;
|
||||
/**
|
||||
* The data for a {@link CallRecord} of a channel message sent by the channel responder to the
|
||||
* framework.
|
||||
*/
|
||||
static class ChannelCallData {
|
||||
ChannelCallData(@NonNull String channel, @NonNull JSONObject message) {
|
||||
this.channel = channel;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
FlutterEngine mockEngine;
|
||||
KeyEventChannel mockKeyEventChannel;
|
||||
@Mock TextInputPlugin mockTextInputPlugin;
|
||||
@Mock View mockView;
|
||||
@Mock View mockRootView;
|
||||
KeyboardManager keyboardManager;
|
||||
/** The channel that the message is sent on. */
|
||||
public String channel;
|
||||
/** The parsed JSON message object. */
|
||||
public JSONObject message;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private FlutterEngine mockFlutterEngine() {
|
||||
// Mock FlutterEngine and all of its required direct calls.
|
||||
FlutterEngine engine = mock(FlutterEngine.class);
|
||||
when(engine.getKeyEventChannel()).thenReturn(mock(KeyEventChannel.class));
|
||||
when(engine.getTextInputChannel()).thenReturn(mock(TextInputChannel.class));
|
||||
return engine;
|
||||
/**
|
||||
* Build a response to a channel message sent by the channel responder.
|
||||
*
|
||||
* @param handled whether the event is handled.
|
||||
*/
|
||||
static ByteBuffer buildResponseMessage(boolean handled) {
|
||||
JSONObject body = new JSONObject();
|
||||
try {
|
||||
body.put("handled", handled);
|
||||
} catch (JSONException e) {
|
||||
assertNull(e);
|
||||
}
|
||||
ByteBuffer binaryReply = JSONMessageCodec.INSTANCE.encodeMessage(body);
|
||||
binaryReply.rewind();
|
||||
return binaryReply;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to configure how to process a channel message.
|
||||
*
|
||||
* <p>When the channel responder sends a channel message, this functional interface will be
|
||||
* invoked. Its first argument will be the detailed data. The second argument will be a nullable
|
||||
* reply callback, which should be called to mock the reply from the framework.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
static interface ChannelCallHandler extends BiConsumer<ChannelCallData, Consumer<Boolean>> {}
|
||||
|
||||
static class KeyboardTester {
|
||||
public KeyboardTester() {
|
||||
respondToChannelCallsWith(false);
|
||||
respondToTextInputWith(false);
|
||||
|
||||
BinaryMessenger mockMessenger = mock(BinaryMessenger.class);
|
||||
doAnswer(invocation -> onChannelMessage(invocation))
|
||||
.when(mockMessenger)
|
||||
.send(any(String.class), any(ByteBuffer.class));
|
||||
doAnswer(invocation -> onChannelMessage(invocation))
|
||||
.when(mockMessenger)
|
||||
.send(any(String.class), any(ByteBuffer.class), any(BinaryMessenger.BinaryReply.class));
|
||||
|
||||
mockView = mock(KeyboardManager.ViewDelegate.class);
|
||||
doAnswer(invocation -> mockMessenger).when(mockView).getBinaryMessenger();
|
||||
doAnswer(invocation -> textInputResult)
|
||||
.when(mockView)
|
||||
.onTextInputKeyEvent(any(KeyEvent.class));
|
||||
doAnswer(
|
||||
invocation -> {
|
||||
KeyEvent event = invocation.getArgument(0);
|
||||
boolean handled = keyboardManager.handleEvent(event);
|
||||
assertEquals(handled, false);
|
||||
return null;
|
||||
})
|
||||
.when(mockView)
|
||||
.redispatch(any(KeyEvent.class));
|
||||
|
||||
keyboardManager = new KeyboardManager(mockView);
|
||||
}
|
||||
|
||||
public @Mock KeyboardManager.ViewDelegate mockView;
|
||||
public KeyboardManager keyboardManager;
|
||||
|
||||
/** Set channel calls to respond immediately with the given response. */
|
||||
public void respondToChannelCallsWith(boolean handled) {
|
||||
channelHandler =
|
||||
(ChannelCallData data, Consumer<Boolean> reply) -> {
|
||||
if (reply != null) {
|
||||
reply.accept(handled);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Record embedder calls to the given storage.
|
||||
*
|
||||
* <p>They are not responded to until the stored callbacks are manually called.
|
||||
*/
|
||||
public void recordChannelCallsTo(@NonNull ArrayList<CallRecord> storage) {
|
||||
channelHandler =
|
||||
(ChannelCallData data, Consumer<Boolean> reply) -> {
|
||||
storage.add(CallRecord.channelCall(data, reply));
|
||||
};
|
||||
}
|
||||
|
||||
/** Set text calls to respond with the given response. */
|
||||
public void respondToTextInputWith(boolean response) {
|
||||
textInputResult = response;
|
||||
}
|
||||
|
||||
private ChannelCallHandler channelHandler;
|
||||
private Boolean textInputResult;
|
||||
|
||||
private Object onChannelMessage(@NonNull InvocationOnMock invocation) {
|
||||
final String channel = invocation.getArgument(0);
|
||||
final ByteBuffer buffer = invocation.getArgument(1);
|
||||
buffer.rewind();
|
||||
final JSONObject jsonObject = (JSONObject) JSONMessageCodec.INSTANCE.decodeMessage(buffer);
|
||||
final BinaryMessenger.BinaryReply reply = invocation.getArgument(2);
|
||||
final Consumer<Boolean> jsonReply =
|
||||
reply == null
|
||||
? null
|
||||
: handled -> {
|
||||
reply.reply(buildResponseMessage(handled));
|
||||
};
|
||||
channelHandler.accept(new ChannelCallData(channel, jsonObject), jsonReply);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the channel call is an event that matches the given data.
|
||||
*
|
||||
* <p>For now this function only validates key code, but not scancode or characters.
|
||||
*
|
||||
* @param data the target data to be tested.
|
||||
* @param type the type of the data, usually "keydown" or "keyup".
|
||||
* @param keyCode the key code.
|
||||
*/
|
||||
static void assertChannelEventEquals(
|
||||
@NonNull ChannelCallData data, @NonNull String type, @NonNull Integer keyCode) {
|
||||
final JSONObject message = data.message;
|
||||
assertEquals(data.channel, "flutter/keyevent");
|
||||
try {
|
||||
assertEquals(type, message.get("type"));
|
||||
assertEquals("android", message.get("keymap"));
|
||||
assertEquals(keyCode, message.get("keyCode"));
|
||||
} catch (JSONException e) {
|
||||
assertNull(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
when(mockFlutterJni.isAttached()).thenReturn(true);
|
||||
mockEngine = mockFlutterEngine();
|
||||
mockKeyEventChannel = mockEngine.getKeyEventChannel();
|
||||
when(mockView.getRootView()).thenAnswer(invocation -> mockRootView);
|
||||
when(mockView.dispatchKeyEvent(any(KeyEvent.class)))
|
||||
.thenAnswer(
|
||||
invocation -> keyboardManager.handleEvent((KeyEvent) invocation.getArguments()[0]));
|
||||
when(mockRootView.dispatchKeyEvent(any(KeyEvent.class)))
|
||||
.thenAnswer(
|
||||
invocation -> mockView.dispatchKeyEvent((KeyEvent) invocation.getArguments()[0]));
|
||||
keyboardManager =
|
||||
new KeyboardManager(
|
||||
mockView,
|
||||
mockTextInputPlugin,
|
||||
new Responder[] {new KeyChannelResponder(mockKeyEventChannel)});
|
||||
}
|
||||
|
||||
// Tests start
|
||||
|
||||
@Test
|
||||
public void respondsTrueWhenHandlingNewEvents() {
|
||||
final FakeResponder fakeResponder = new FakeResponder();
|
||||
keyboardManager =
|
||||
new KeyboardManager(
|
||||
mockView, mockTextInputPlugin, new KeyboardManager.Responder[] {fakeResponder});
|
||||
final KeyboardTester tester = new KeyboardTester();
|
||||
final KeyEvent keyEvent = new FakeKeyEvent(KeyEvent.ACTION_DOWN, 65);
|
||||
final boolean result = keyboardManager.handleEvent(keyEvent);
|
||||
final ArrayList<CallRecord> calls = new ArrayList<CallRecord>();
|
||||
|
||||
tester.recordChannelCallsTo(calls);
|
||||
|
||||
final boolean result = tester.keyboardManager.handleEvent(keyEvent);
|
||||
|
||||
assertEquals(true, result);
|
||||
assertEquals(keyEvent, fakeResponder.mLastKeyEvent);
|
||||
assertEquals(calls.size(), 1);
|
||||
assertChannelEventEquals(calls.get(0).channelData, "keydown", 65);
|
||||
|
||||
// Don't send the key event to the text plugin if the only primary responder
|
||||
// hasn't responded.
|
||||
verify(mockTextInputPlugin, times(0)).handleKeyEvent(any(KeyEvent.class));
|
||||
verify(mockRootView, times(0)).dispatchKeyEvent(any(KeyEvent.class));
|
||||
verify(tester.mockView, times(0)).onTextInputKeyEvent(any(KeyEvent.class));
|
||||
verify(tester.mockView, times(0)).redispatch(any(KeyEvent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void primaryRespondersHaveTheHighestPrecedence() {
|
||||
final FakeResponder fakeResponder = new FakeResponder();
|
||||
keyboardManager =
|
||||
new KeyboardManager(
|
||||
mockView, mockTextInputPlugin, new KeyboardManager.Responder[] {fakeResponder});
|
||||
final KeyboardTester tester = new KeyboardTester();
|
||||
final KeyEvent keyEvent = new FakeKeyEvent(KeyEvent.ACTION_DOWN, 65);
|
||||
final boolean result = keyboardManager.handleEvent(keyEvent);
|
||||
final ArrayList<CallRecord> calls = new ArrayList<CallRecord>();
|
||||
|
||||
tester.recordChannelCallsTo(calls);
|
||||
|
||||
final boolean result = tester.keyboardManager.handleEvent(keyEvent);
|
||||
|
||||
assertEquals(true, result);
|
||||
assertEquals(keyEvent, fakeResponder.mLastKeyEvent);
|
||||
assertEquals(calls.size(), 1);
|
||||
assertChannelEventEquals(calls.get(0).channelData, "keydown", 65);
|
||||
|
||||
// Don't send the key event to the text plugin if the only primary responder
|
||||
// hasn't responded.
|
||||
verify(mockTextInputPlugin, times(0)).handleKeyEvent(any(KeyEvent.class));
|
||||
verify(mockRootView, times(0)).dispatchKeyEvent(any(KeyEvent.class));
|
||||
verify(tester.mockView, times(0)).onTextInputKeyEvent(any(KeyEvent.class));
|
||||
verify(tester.mockView, times(0)).redispatch(any(KeyEvent.class));
|
||||
|
||||
// If a primary responder handles the key event the propagation stops.
|
||||
assertNotNull(fakeResponder.mLastKeyEventHandledCallback);
|
||||
fakeResponder.eventHandled(true);
|
||||
verify(mockTextInputPlugin, times(0)).handleKeyEvent(any(KeyEvent.class));
|
||||
verify(mockRootView, times(0)).dispatchKeyEvent(any(KeyEvent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zeroRespondersTest() {
|
||||
keyboardManager =
|
||||
new KeyboardManager(mockView, mockTextInputPlugin, new KeyboardManager.Responder[] {});
|
||||
final KeyEvent keyEvent = new FakeKeyEvent(KeyEvent.ACTION_DOWN, 65);
|
||||
final boolean result = keyboardManager.handleEvent(keyEvent);
|
||||
assertEquals(true, result);
|
||||
|
||||
// Send the key event to the text plugin since there's 0 primary responders.
|
||||
verify(mockTextInputPlugin, times(1)).handleKeyEvent(any(KeyEvent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleRespondersTest() {
|
||||
final FakeResponder fakeResponder1 = new FakeResponder();
|
||||
final FakeResponder fakeResponder2 = new FakeResponder();
|
||||
keyboardManager =
|
||||
new KeyboardManager(
|
||||
mockView,
|
||||
mockTextInputPlugin,
|
||||
new KeyboardManager.Responder[] {fakeResponder1, fakeResponder2});
|
||||
final KeyEvent keyEvent = new FakeKeyEvent(KeyEvent.ACTION_DOWN, 65);
|
||||
final boolean result = keyboardManager.handleEvent(keyEvent);
|
||||
|
||||
assertEquals(true, result);
|
||||
assertEquals(keyEvent, fakeResponder1.mLastKeyEvent);
|
||||
assertEquals(keyEvent, fakeResponder2.mLastKeyEvent);
|
||||
|
||||
fakeResponder2.eventHandled(false);
|
||||
// Don't send the key event to the text plugin, since fakeResponder1
|
||||
// hasn't responded.
|
||||
verify(mockTextInputPlugin, times(0)).handleKeyEvent(any(KeyEvent.class));
|
||||
|
||||
fakeResponder1.eventHandled(false);
|
||||
verify(mockTextInputPlugin, times(1)).handleKeyEvent(any(KeyEvent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleRespondersTest2() {
|
||||
final FakeResponder fakeResponder1 = new FakeResponder();
|
||||
final FakeResponder fakeResponder2 = new FakeResponder();
|
||||
keyboardManager =
|
||||
new KeyboardManager(
|
||||
mockView,
|
||||
mockTextInputPlugin,
|
||||
new KeyboardManager.Responder[] {fakeResponder1, fakeResponder2});
|
||||
final KeyEvent keyEvent = new FakeKeyEvent(KeyEvent.ACTION_DOWN, 65);
|
||||
final boolean result = keyboardManager.handleEvent(keyEvent);
|
||||
|
||||
fakeResponder2.eventHandled(false);
|
||||
fakeResponder1.eventHandled(true);
|
||||
|
||||
// Handled by primary responders, propagation stops.
|
||||
verify(mockTextInputPlugin, times(0)).handleKeyEvent(any(KeyEvent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleRespondersTest3() {
|
||||
final FakeResponder fakeResponder1 = new FakeResponder();
|
||||
final FakeResponder fakeResponder2 = new FakeResponder();
|
||||
keyboardManager =
|
||||
new KeyboardManager(
|
||||
mockView,
|
||||
mockTextInputPlugin,
|
||||
new KeyboardManager.Responder[] {fakeResponder1, fakeResponder2});
|
||||
final KeyEvent keyEvent = new FakeKeyEvent(KeyEvent.ACTION_DOWN, 65);
|
||||
final boolean result = keyboardManager.handleEvent(keyEvent);
|
||||
|
||||
fakeResponder2.eventHandled(false);
|
||||
|
||||
Exception exception = null;
|
||||
try {
|
||||
fakeResponder2.eventHandled(false);
|
||||
} catch (Exception e) {
|
||||
exception = e;
|
||||
}
|
||||
// Throws since the same handle is called twice.
|
||||
assertNotNull(exception);
|
||||
assertNotNull(calls.get(0).reply);
|
||||
calls.get(0).reply.accept(true);
|
||||
verify(tester.mockView, times(0)).onTextInputKeyEvent(any(KeyEvent.class));
|
||||
verify(tester.mockView, times(0)).redispatch(any(KeyEvent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void textInputPluginHasTheSecondHighestPrecedence() {
|
||||
final FakeResponder fakeResponder = new FakeResponder();
|
||||
keyboardManager =
|
||||
spy(
|
||||
new KeyboardManager(
|
||||
mockView, mockTextInputPlugin, new KeyboardManager.Responder[] {fakeResponder}));
|
||||
final KeyboardTester tester = new KeyboardTester();
|
||||
final KeyEvent keyEvent = new FakeKeyEvent(KeyEvent.ACTION_DOWN, 65);
|
||||
final boolean result = keyboardManager.handleEvent(keyEvent);
|
||||
final ArrayList<CallRecord> calls = new ArrayList<CallRecord>();
|
||||
|
||||
tester.recordChannelCallsTo(calls);
|
||||
|
||||
final boolean result = tester.keyboardManager.handleEvent(keyEvent);
|
||||
|
||||
assertEquals(true, result);
|
||||
assertEquals(keyEvent, fakeResponder.mLastKeyEvent);
|
||||
assertEquals(calls.size(), 1);
|
||||
assertChannelEventEquals(calls.get(0).channelData, "keydown", 65);
|
||||
|
||||
// Don't send the key event to the text plugin if the only primary responder
|
||||
// hasn't responded.
|
||||
verify(mockTextInputPlugin, times(0)).handleKeyEvent(any(KeyEvent.class));
|
||||
verify(mockRootView, times(0)).dispatchKeyEvent(any(KeyEvent.class));
|
||||
verify(tester.mockView, times(0)).onTextInputKeyEvent(any(KeyEvent.class));
|
||||
verify(tester.mockView, times(0)).redispatch(any(KeyEvent.class));
|
||||
|
||||
// If no primary responder handles the key event the propagates to the text
|
||||
// input plugin.
|
||||
assertNotNull(fakeResponder.mLastKeyEventHandledCallback);
|
||||
assertNotNull(calls.get(0).reply);
|
||||
// Let text input plugin handle the key event.
|
||||
when(mockTextInputPlugin.handleKeyEvent(any())).thenAnswer(invocation -> true);
|
||||
fakeResponder.eventHandled(false);
|
||||
tester.respondToTextInputWith(true);
|
||||
calls.get(0).reply.accept(false);
|
||||
|
||||
verify(mockTextInputPlugin, times(1)).handleKeyEvent(keyEvent);
|
||||
verify(mockRootView, times(0)).dispatchKeyEvent(any(KeyEvent.class));
|
||||
|
||||
// It's not redispatched to the keyboard manager.
|
||||
verify(keyboardManager, times(1)).handleEvent(any(KeyEvent.class));
|
||||
verify(tester.mockView, times(1)).onTextInputKeyEvent(keyEvent);
|
||||
verify(tester.mockView, times(0)).redispatch(any(KeyEvent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void RedispatchKeyEventIfTextInputPluginFailsToHandle() {
|
||||
final FakeResponder fakeResponder = new FakeResponder();
|
||||
keyboardManager =
|
||||
spy(
|
||||
new KeyboardManager(
|
||||
mockView, mockTextInputPlugin, new KeyboardManager.Responder[] {fakeResponder}));
|
||||
final KeyboardTester tester = new KeyboardTester();
|
||||
final KeyEvent keyEvent = new FakeKeyEvent(KeyEvent.ACTION_DOWN, 65);
|
||||
final boolean result = keyboardManager.handleEvent(keyEvent);
|
||||
final ArrayList<CallRecord> calls = new ArrayList<CallRecord>();
|
||||
|
||||
tester.recordChannelCallsTo(calls);
|
||||
|
||||
final boolean result = tester.keyboardManager.handleEvent(keyEvent);
|
||||
|
||||
assertEquals(true, result);
|
||||
assertEquals(keyEvent, fakeResponder.mLastKeyEvent);
|
||||
assertEquals(calls.size(), 1);
|
||||
assertChannelEventEquals(calls.get(0).channelData, "keydown", 65);
|
||||
|
||||
// Don't send the key event to the text plugin if the only primary responder
|
||||
// hasn't responded.
|
||||
verify(mockTextInputPlugin, times(0)).handleKeyEvent(any(KeyEvent.class));
|
||||
verify(mockRootView, times(0)).dispatchKeyEvent(any(KeyEvent.class));
|
||||
verify(tester.mockView, times(0)).onTextInputKeyEvent(any(KeyEvent.class));
|
||||
verify(tester.mockView, times(0)).redispatch(any(KeyEvent.class));
|
||||
|
||||
// Neither the primary responders nor text input plugin handles the event.
|
||||
when(mockTextInputPlugin.handleKeyEvent(any())).thenAnswer(invocation -> false);
|
||||
fakeResponder.mLastKeyEvent = null;
|
||||
fakeResponder.eventHandled(false);
|
||||
tester.respondToTextInputWith(false);
|
||||
calls.get(0).reply.accept(false);
|
||||
|
||||
verify(mockTextInputPlugin, times(1)).handleKeyEvent(keyEvent);
|
||||
verify(mockRootView, times(1)).dispatchKeyEvent(keyEvent);
|
||||
verify(tester.mockView, times(1)).onTextInputKeyEvent(keyEvent);
|
||||
verify(tester.mockView, times(1)).redispatch(keyEvent);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void respondsFalseWhenHandlingRedispatchedEvents() {
|
||||
final FakeResponder fakeResponder = new FakeResponder();
|
||||
keyboardManager =
|
||||
spy(
|
||||
new KeyboardManager(
|
||||
mockView, mockTextInputPlugin, new KeyboardManager.Responder[] {fakeResponder}));
|
||||
final KeyboardTester tester = new KeyboardTester();
|
||||
final ArrayList<CallRecord> calls = new ArrayList<CallRecord>();
|
||||
|
||||
tester.recordChannelCallsTo(calls);
|
||||
|
||||
final KeyEvent keyEvent = new FakeKeyEvent(KeyEvent.ACTION_DOWN, 65);
|
||||
final boolean result = keyboardManager.handleEvent(keyEvent);
|
||||
final boolean result = tester.keyboardManager.handleEvent(keyEvent);
|
||||
|
||||
assertEquals(true, result);
|
||||
assertEquals(keyEvent, fakeResponder.mLastKeyEvent);
|
||||
assertEquals(calls.size(), 1);
|
||||
assertChannelEventEquals(calls.get(0).channelData, "keydown", 65);
|
||||
|
||||
// Don't send the key event to the text plugin if the only primary responder
|
||||
// hasn't responded.
|
||||
verify(mockTextInputPlugin, times(0)).handleKeyEvent(any(KeyEvent.class));
|
||||
verify(mockRootView, times(0)).dispatchKeyEvent(any(KeyEvent.class));
|
||||
verify(tester.mockView, times(0)).onTextInputKeyEvent(any(KeyEvent.class));
|
||||
verify(tester.mockView, times(0)).redispatch(any(KeyEvent.class));
|
||||
|
||||
// Neither the primary responders nor text input plugin handles the event.
|
||||
when(mockTextInputPlugin.handleKeyEvent(any())).thenAnswer(invocation -> false);
|
||||
fakeResponder.mLastKeyEvent = null;
|
||||
fakeResponder.eventHandled(false);
|
||||
tester.respondToTextInputWith(false);
|
||||
calls.get(0).reply.accept(false);
|
||||
|
||||
verify(mockTextInputPlugin, times(1)).handleKeyEvent(keyEvent);
|
||||
verify(mockRootView, times(1)).dispatchKeyEvent(keyEvent);
|
||||
verify(tester.mockView, times(1)).onTextInputKeyEvent(keyEvent);
|
||||
verify(tester.mockView, times(1)).redispatch(keyEvent);
|
||||
|
||||
// It's redispatched to the keyboard manager, but not the primary
|
||||
// responders.
|
||||
verify(keyboardManager, times(2)).handleEvent(any(KeyEvent.class));
|
||||
assertNull(fakeResponder.mLastKeyEvent);
|
||||
// It's redispatched to the keyboard manager, but no eventual key calls.
|
||||
assertEquals(calls.size(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,6 @@ import io.flutter.embedding.engine.mutatorsstack.FlutterMutatorView;
|
||||
import io.flutter.embedding.engine.mutatorsstack.FlutterMutatorsStack;
|
||||
import io.flutter.embedding.engine.renderer.FlutterRenderer;
|
||||
import io.flutter.embedding.engine.systemchannels.AccessibilityChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.KeyEventChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.MouseCursorChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.SettingsChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.TextInputChannel;
|
||||
@@ -948,7 +947,6 @@ public class PlatformViewsControllerTest {
|
||||
when(engine.getSettingsChannel()).thenReturn(new SettingsChannel(executor));
|
||||
when(engine.getPlatformViewsController()).thenReturn(platformViewsController);
|
||||
when(engine.getLocalizationPlugin()).thenReturn(mock(LocalizationPlugin.class));
|
||||
when(engine.getKeyEventChannel()).thenReturn(mock(KeyEventChannel.class));
|
||||
when(engine.getAccessibilityChannel()).thenReturn(mock(AccessibilityChannel.class));
|
||||
|
||||
flutterView.attachToFlutterEngine(engine);
|
||||
|
||||
@@ -12,8 +12,7 @@
|
||||
* A keyboard event goes through a few sections, each can choose to handled the
|
||||
* event, and only unhandled events can move to the next section:
|
||||
*
|
||||
* - Pre-filtering: Events during IME are sent to the system immediately
|
||||
* (to be implemented).
|
||||
* - Pre-filtering: Events during IME are sent to the system immediately.
|
||||
* - Keyboard: Dispatch to the embedder responder and the channel responder
|
||||
* simultaneously. After both responders have responded (asynchronously), the
|
||||
* event is considered handled if either responder handles.
|
||||
|
||||
Reference in New Issue
Block a user