Reland: Keyboard events (#87174)
This commit is contained in:
@@ -828,6 +828,9 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
|
||||
assert(debugAssertAllSchedulerVarsUnset(
|
||||
'The value of a scheduler debug variable was changed by the test.',
|
||||
));
|
||||
assert(debugAssertAllServicesVarsUnset(
|
||||
'The value of a services debug variable was changed by the test.',
|
||||
));
|
||||
}
|
||||
|
||||
void _verifyAutoUpdateGoldensUnset(bool valueBeforeTest) {
|
||||
@@ -894,6 +897,10 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
|
||||
// tests.
|
||||
// ignore: invalid_use_of_visible_for_testing_member
|
||||
RawKeyboard.instance.clearKeysPressed();
|
||||
// ignore: invalid_use_of_visible_for_testing_member
|
||||
HardwareKeyboard.instance.clearState();
|
||||
// ignore: invalid_use_of_visible_for_testing_member
|
||||
keyEventManager.clearState();
|
||||
assert(!RendererBinding.instance!.mouseTracker.mouseIsConnected,
|
||||
'The MouseTracker thinks that there is still a mouse connected, which indicates that a '
|
||||
'test has not removed the mouse pointer which it added. Call removePointer on the '
|
||||
|
||||
@@ -973,7 +973,7 @@ abstract class WidgetController {
|
||||
return box.size;
|
||||
}
|
||||
|
||||
/// Simulates sending physical key down and up events through the system channel.
|
||||
/// Simulates sending physical key down and up events.
|
||||
///
|
||||
/// This only simulates key events coming from a physical keyboard, not from a
|
||||
/// soft keyboard.
|
||||
@@ -984,6 +984,9 @@ abstract class WidgetController {
|
||||
/// else. Must not be null. Some platforms (e.g. Windows, iOS) are not yet
|
||||
/// supported.
|
||||
///
|
||||
/// Whether the event is sent through [RawKeyEvent] or [KeyEvent] is
|
||||
/// controlled by [debugKeyEventSimulatorTransitModeOverride].
|
||||
///
|
||||
/// Keys that are down when the test completes are cleared after each test.
|
||||
///
|
||||
/// This method sends both the key down and the key up events, to simulate a
|
||||
@@ -1004,7 +1007,7 @@ abstract class WidgetController {
|
||||
return handled;
|
||||
}
|
||||
|
||||
/// Simulates sending a physical key down event through the system channel.
|
||||
/// Simulates sending a physical key down event.
|
||||
///
|
||||
/// This only simulates key down events coming from a physical keyboard, not
|
||||
/// from a soft keyboard.
|
||||
@@ -1015,13 +1018,17 @@ abstract class WidgetController {
|
||||
/// else. Must not be null. Some platforms (e.g. Windows, iOS) are not yet
|
||||
/// supported.
|
||||
///
|
||||
/// Whether the event is sent through [RawKeyEvent] or [KeyEvent] is
|
||||
/// controlled by [debugKeyEventSimulatorTransitModeOverride].
|
||||
///
|
||||
/// Keys that are down when the test completes are cleared after each test.
|
||||
///
|
||||
/// Returns true if the key event was handled by the framework.
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// - [sendKeyUpEvent] to simulate the corresponding key up event.
|
||||
/// - [sendKeyUpEvent] and [sendKeyRepeatEvent] to simulate the corresponding
|
||||
/// key up and repeat event.
|
||||
/// - [sendKeyEvent] to simulate both the key up and key down in the same call.
|
||||
Future<bool> sendKeyDownEvent(LogicalKeyboardKey key, { String? character, String platform = _defaultPlatform }) async {
|
||||
assert(platform != null);
|
||||
@@ -1039,11 +1046,15 @@ abstract class WidgetController {
|
||||
/// that type of system. Defaults to "web" on web, and "android" everywhere
|
||||
/// else. May not be null.
|
||||
///
|
||||
/// Whether the event is sent through [RawKeyEvent] or [KeyEvent] is
|
||||
/// controlled by [debugKeyEventSimulatorTransitModeOverride].
|
||||
///
|
||||
/// Returns true if the key event was handled by the framework.
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// - [sendKeyDownEvent] to simulate the corresponding key down event.
|
||||
/// - [sendKeyDownEvent] and [sendKeyRepeatEvent] to simulate the
|
||||
/// corresponding key down and repeat event.
|
||||
/// - [sendKeyEvent] to simulate both the key up and key down in the same call.
|
||||
Future<bool> sendKeyUpEvent(LogicalKeyboardKey key, { String platform = _defaultPlatform }) async {
|
||||
assert(platform != null);
|
||||
@@ -1051,6 +1062,35 @@ abstract class WidgetController {
|
||||
return simulateKeyUpEvent(key, platform: platform);
|
||||
}
|
||||
|
||||
/// Simulates sending a physical key repeat event.
|
||||
///
|
||||
/// This only simulates key repeat events coming from a physical keyboard, not
|
||||
/// from a soft keyboard.
|
||||
///
|
||||
/// Specify `platform` as one of the platforms allowed in
|
||||
/// [platform.Platform.operatingSystem] to make the event appear to be from that type
|
||||
/// of system. Defaults to "web" on web, and "android" everywhere else. Must not be
|
||||
/// null. Some platforms (e.g. Windows, iOS) are not yet supported.
|
||||
///
|
||||
/// Whether the event is sent through [RawKeyEvent] or [KeyEvent] is
|
||||
/// controlled by [debugKeyEventSimulatorTransitModeOverride]. If through [RawKeyEvent],
|
||||
/// this method is equivalent to [sendKeyDownEvent].
|
||||
///
|
||||
/// Keys that are down when the test completes are cleared after each test.
|
||||
///
|
||||
/// Returns true if the key event was handled by the framework.
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// - [sendKeyDownEvent] and [sendKeyUpEvent] to simulate the corresponding
|
||||
/// key down and up event.
|
||||
/// - [sendKeyEvent] to simulate both the key up and key down in the same call.
|
||||
Future<bool> sendKeyRepeatEvent(LogicalKeyboardKey key, { String? character, String platform = _defaultPlatform }) async {
|
||||
assert(platform != null);
|
||||
// Internally wrapped in async guard.
|
||||
return simulateKeyRepeatEvent(key, character: character, platform: platform);
|
||||
}
|
||||
|
||||
/// Returns the rect of the given widget. This is only valid once
|
||||
/// the widget's render object has been laid out at least once.
|
||||
Rect getRect(Finder finder) => getTopLeft(finder) & getSize(finder);
|
||||
|
||||
@@ -4,9 +4,12 @@
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/foundation.dart' show kIsWeb;
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'binding.dart';
|
||||
import 'test_async_utils.dart';
|
||||
@@ -16,13 +19,14 @@ import 'test_async_utils.dart';
|
||||
// https://github.com/flutter/flutter/issues/33521
|
||||
// This code can only simulate keys which appear in the key maps.
|
||||
|
||||
String _keyLabel(LogicalKeyboardKey key) {
|
||||
String? _keyLabel(LogicalKeyboardKey key) {
|
||||
final String keyLabel = key.keyLabel;
|
||||
if (keyLabel.length == 1)
|
||||
return keyLabel.toLowerCase();
|
||||
return '';
|
||||
return null;
|
||||
}
|
||||
|
||||
// ignore: avoid_classes_with_only_static_members
|
||||
/// A class that serves as a namespace for a bunch of keyboard-key generation
|
||||
/// utilities.
|
||||
class KeyEventSimulator {
|
||||
@@ -153,7 +157,7 @@ class KeyEventSimulator {
|
||||
return result!;
|
||||
}
|
||||
|
||||
static PhysicalKeyboardKey _findPhysicalKey(LogicalKeyboardKey key, String platform) {
|
||||
static PhysicalKeyboardKey _findPhysicalKeyByPlatform(LogicalKeyboardKey key, String platform) {
|
||||
assert(_osIsSupported(platform), 'Platform $platform not supported for key simulation');
|
||||
late Map<dynamic, PhysicalKeyboardKey> map;
|
||||
if (kIsWeb) {
|
||||
@@ -208,7 +212,7 @@ class KeyEventSimulator {
|
||||
key = _getKeySynonym(key);
|
||||
|
||||
// Find a suitable physical key if none was supplied.
|
||||
physicalKey ??= _findPhysicalKey(key, platform);
|
||||
physicalKey ??= _findPhysicalKeyByPlatform(key, platform);
|
||||
|
||||
assert(key.debugName != null);
|
||||
final int keyCode = _getKeyCode(key, platform);
|
||||
@@ -219,7 +223,7 @@ class KeyEventSimulator {
|
||||
'keymap': platform,
|
||||
};
|
||||
|
||||
final String resultCharacter = character ?? _keyLabel(key);
|
||||
final String resultCharacter = character ?? _keyLabel(key) ?? '';
|
||||
void assignWeb() {
|
||||
result['code'] = _getWebKeyCode(key);
|
||||
result['key'] = resultCharacter;
|
||||
@@ -625,7 +629,64 @@ class KeyEventSimulator {
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Simulates sending a hardware key down event through the system channel.
|
||||
static Future<bool> _simulateKeyEventByRawEvent(ValueGetter<Map<String, dynamic>> buildKeyData) async {
|
||||
return TestAsyncUtils.guard<bool>(() async {
|
||||
final Completer<bool> result = Completer<bool>();
|
||||
await TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.handlePlatformMessage(
|
||||
SystemChannels.keyEvent.name,
|
||||
SystemChannels.keyEvent.codec.encodeMessage(buildKeyData()),
|
||||
(ByteData? data) {
|
||||
if (data == null) {
|
||||
result.complete(false);
|
||||
return;
|
||||
}
|
||||
final Map<String, dynamic> decoded = SystemChannels.keyEvent.codec.decodeMessage(data) as Map<String, dynamic>;
|
||||
result.complete(decoded['handled'] as bool);
|
||||
}
|
||||
);
|
||||
return result.future;
|
||||
});
|
||||
}
|
||||
|
||||
static late final Map<String, PhysicalKeyboardKey> _debugNameToPhysicalKey = (() {
|
||||
final Map<String, PhysicalKeyboardKey> result = <String, PhysicalKeyboardKey>{};
|
||||
for (final PhysicalKeyboardKey key in PhysicalKeyboardKey.knownPhysicalKeys) {
|
||||
final String? debugName = key.debugName;
|
||||
if (debugName != null)
|
||||
result[debugName] = key;
|
||||
}
|
||||
return result;
|
||||
})();
|
||||
static PhysicalKeyboardKey _findPhysicalKey(LogicalKeyboardKey key) {
|
||||
final PhysicalKeyboardKey? result = _debugNameToPhysicalKey[key.debugName];
|
||||
assert(result != null, 'Physical key for $key not found in known physical keys');
|
||||
return result!;
|
||||
}
|
||||
|
||||
static const KeyDataTransitMode _defaultTransitMode = KeyDataTransitMode.rawKeyData;
|
||||
|
||||
// The simulation transit mode for [simulateKeyDownEvent], [simulateKeyUpEvent],
|
||||
// and [simulateKeyRepeatEvent].
|
||||
//
|
||||
// Simulation transit mode is the mode that simulated key events are constructed
|
||||
// and delivered. For detailed introduction, see [KeyDataTransitMode] and
|
||||
// its values.
|
||||
//
|
||||
// The `_transitMode` defaults to [KeyDataTransitMode.rawKeyEvent], and can be
|
||||
// overridden with [debugKeyEventSimulatorTransitModeOverride]. In widget tests, it
|
||||
// is often set with [KeySimulationModeVariant].
|
||||
static KeyDataTransitMode get _transitMode {
|
||||
KeyDataTransitMode? result;
|
||||
assert(() {
|
||||
result = debugKeyEventSimulatorTransitModeOverride;
|
||||
return true;
|
||||
}());
|
||||
return result ?? _defaultTransitMode;
|
||||
}
|
||||
|
||||
static String get _defaultPlatform => kIsWeb ? 'web' : Platform.operatingSystem;
|
||||
|
||||
/// Simulates sending a hardware key down event.
|
||||
///
|
||||
/// This only simulates key presses coming from a physical keyboard, not from a
|
||||
/// soft keyboard.
|
||||
@@ -641,29 +702,36 @@ class KeyEventSimulator {
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// - [simulateKeyUpEvent] to simulate the corresponding key up event.
|
||||
static Future<bool> simulateKeyDownEvent(LogicalKeyboardKey key, {String? platform, PhysicalKeyboardKey? physicalKey, String? character}) async {
|
||||
return TestAsyncUtils.guard<bool>(() async {
|
||||
platform ??= Platform.operatingSystem;
|
||||
assert(_osIsSupported(platform!), 'Platform $platform not supported for key simulation');
|
||||
|
||||
|
||||
final Map<String, dynamic> data = getKeyData(key, platform: platform!, isDown: true, physicalKey: physicalKey, character: character);
|
||||
final Completer<bool> result = Completer<bool>();
|
||||
await TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.handlePlatformMessage(
|
||||
SystemChannels.keyEvent.name,
|
||||
SystemChannels.keyEvent.codec.encodeMessage(data),
|
||||
(ByteData? data) {
|
||||
if (data == null) {
|
||||
result.complete(false);
|
||||
return;
|
||||
}
|
||||
final Map<String, dynamic> decoded = SystemChannels.keyEvent.codec.decodeMessage(data) as Map<String, dynamic>;
|
||||
result.complete(decoded['handled'] as bool);
|
||||
}
|
||||
);
|
||||
return result.future;
|
||||
});
|
||||
/// * [simulateKeyUpEvent] to simulate the corresponding key up event.
|
||||
static Future<bool> simulateKeyDownEvent(
|
||||
LogicalKeyboardKey key, {
|
||||
String? platform,
|
||||
PhysicalKeyboardKey? physicalKey,
|
||||
String? character,
|
||||
}) async {
|
||||
Future<bool> _simulateByRawEvent() {
|
||||
return _simulateKeyEventByRawEvent(() {
|
||||
platform ??= _defaultPlatform;
|
||||
return getKeyData(key, platform: platform!, isDown: true, physicalKey: physicalKey, character: character);
|
||||
});
|
||||
}
|
||||
switch (_transitMode) {
|
||||
case KeyDataTransitMode.rawKeyData:
|
||||
return _simulateByRawEvent();
|
||||
case KeyDataTransitMode.keyDataThenRawKeyData:
|
||||
final LogicalKeyboardKey logicalKey = _getKeySynonym(key);
|
||||
final bool resultByKeyEvent = ServicesBinding.instance!.keyEventManager.handleKeyData(
|
||||
ui.KeyData(
|
||||
type: ui.KeyEventType.down,
|
||||
physical: (physicalKey ?? _findPhysicalKey(logicalKey)).usbHidUsage,
|
||||
logical: logicalKey.keyId,
|
||||
timeStamp: Duration.zero,
|
||||
character: character ?? _keyLabel(key),
|
||||
synthesized: false,
|
||||
),
|
||||
);
|
||||
return (await _simulateByRawEvent()) || resultByKeyEvent;
|
||||
}
|
||||
}
|
||||
|
||||
/// Simulates sending a hardware key up event through the system channel.
|
||||
@@ -680,29 +748,81 @@ class KeyEventSimulator {
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// - [simulateKeyDownEvent] to simulate the corresponding key down event.
|
||||
static Future<bool> simulateKeyUpEvent(LogicalKeyboardKey key, {String? platform, PhysicalKeyboardKey? physicalKey}) async {
|
||||
return TestAsyncUtils.guard<bool>(() async {
|
||||
platform ??= Platform.operatingSystem;
|
||||
assert(_osIsSupported(platform!), 'Platform $platform not supported for key simulation');
|
||||
/// * [simulateKeyDownEvent] to simulate the corresponding key down event.
|
||||
static Future<bool> simulateKeyUpEvent(
|
||||
LogicalKeyboardKey key, {
|
||||
String? platform,
|
||||
PhysicalKeyboardKey? physicalKey,
|
||||
}) async {
|
||||
Future<bool> _simulateByRawEvent() {
|
||||
return _simulateKeyEventByRawEvent(() {
|
||||
platform ??= _defaultPlatform;
|
||||
return getKeyData(key, platform: platform!, isDown: false, physicalKey: physicalKey);
|
||||
});
|
||||
}
|
||||
switch (_transitMode) {
|
||||
case KeyDataTransitMode.rawKeyData:
|
||||
return _simulateByRawEvent();
|
||||
case KeyDataTransitMode.keyDataThenRawKeyData:
|
||||
final LogicalKeyboardKey logicalKey = _getKeySynonym(key);
|
||||
final bool resultByKeyEvent = ServicesBinding.instance!.keyEventManager.handleKeyData(
|
||||
ui.KeyData(
|
||||
type: ui.KeyEventType.up,
|
||||
physical: (physicalKey ?? _findPhysicalKey(logicalKey)).usbHidUsage,
|
||||
logical: logicalKey.keyId,
|
||||
timeStamp: Duration.zero,
|
||||
character: null,
|
||||
synthesized: false,
|
||||
),
|
||||
);
|
||||
return (await _simulateByRawEvent()) || resultByKeyEvent;
|
||||
}
|
||||
}
|
||||
|
||||
final Map<String, dynamic> data = getKeyData(key, platform: platform!, isDown: false, physicalKey: physicalKey);
|
||||
bool result = false;
|
||||
await TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.handlePlatformMessage(
|
||||
SystemChannels.keyEvent.name,
|
||||
SystemChannels.keyEvent.codec.encodeMessage(data),
|
||||
(ByteData? data) {
|
||||
if (data == null) {
|
||||
return;
|
||||
}
|
||||
final Map<String, dynamic> decoded = SystemChannels.keyEvent.codec.decodeMessage(data) as Map<String, dynamic>;
|
||||
if (decoded['handled'] as bool) {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
);
|
||||
return result;
|
||||
});
|
||||
/// Simulates sending a hardware key repeat event through the system channel.
|
||||
///
|
||||
/// This only simulates key presses coming from a physical keyboard, not from a
|
||||
/// soft keyboard.
|
||||
///
|
||||
/// Specify `platform` as one of the platforms allowed in
|
||||
/// [Platform.operatingSystem] to make the event appear to be from that type of
|
||||
/// system. Defaults to the operating system that the test is running on. Some
|
||||
/// platforms (e.g. Windows, iOS) are not yet supported.
|
||||
///
|
||||
/// Returns true if the key event was handled by the framework.
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [simulateKeyDownEvent] to simulate the corresponding key down event.
|
||||
static Future<bool> simulateKeyRepeatEvent(
|
||||
LogicalKeyboardKey key, {
|
||||
String? platform,
|
||||
PhysicalKeyboardKey? physicalKey,
|
||||
String? character,
|
||||
}) async {
|
||||
Future<bool> _simulateByRawEvent() {
|
||||
return _simulateKeyEventByRawEvent(() {
|
||||
platform ??= _defaultPlatform;
|
||||
return getKeyData(key, platform: platform!, isDown: true, physicalKey: physicalKey, character: character);
|
||||
});
|
||||
}
|
||||
switch (_transitMode) {
|
||||
case KeyDataTransitMode.rawKeyData:
|
||||
return _simulateByRawEvent();
|
||||
case KeyDataTransitMode.keyDataThenRawKeyData:
|
||||
final LogicalKeyboardKey logicalKey = _getKeySynonym(key);
|
||||
final bool resultByKeyEvent = ServicesBinding.instance!.keyEventManager.handleKeyData(
|
||||
ui.KeyData(
|
||||
type: ui.KeyEventType.repeat,
|
||||
physical: (physicalKey ?? _findPhysicalKey(logicalKey)).usbHidUsage,
|
||||
logical: logicalKey.keyId,
|
||||
timeStamp: Duration.zero,
|
||||
character: character ?? _keyLabel(key),
|
||||
synthesized: false,
|
||||
),
|
||||
);
|
||||
return (await _simulateByRawEvent()) || resultByKeyEvent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -725,8 +845,14 @@ class KeyEventSimulator {
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// - [simulateKeyUpEvent] to simulate the corresponding key up event.
|
||||
Future<bool> simulateKeyDownEvent(LogicalKeyboardKey key, {String? platform, PhysicalKeyboardKey? physicalKey, String? character}) {
|
||||
/// * [simulateKeyUpEvent] and [simulateKeyRepeatEvent] to simulate the
|
||||
/// corresponding key up and repeat event.
|
||||
Future<bool> simulateKeyDownEvent(
|
||||
LogicalKeyboardKey key, {
|
||||
String? platform,
|
||||
PhysicalKeyboardKey? physicalKey,
|
||||
String? character,
|
||||
}) {
|
||||
return KeyEventSimulator.simulateKeyDownEvent(key, platform: platform, physicalKey: physicalKey, character: character);
|
||||
}
|
||||
|
||||
@@ -747,7 +873,85 @@ Future<bool> simulateKeyDownEvent(LogicalKeyboardKey key, {String? platform, Phy
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// - [simulateKeyDownEvent] to simulate the corresponding key down event.
|
||||
Future<bool> simulateKeyUpEvent(LogicalKeyboardKey key, {String? platform, PhysicalKeyboardKey? physicalKey}) {
|
||||
/// * [simulateKeyDownEvent] and [simulateKeyRepeatEvent] to simulate the
|
||||
/// corresponding key down and repeat event.
|
||||
Future<bool> simulateKeyUpEvent(
|
||||
LogicalKeyboardKey key, {
|
||||
String? platform,
|
||||
PhysicalKeyboardKey? physicalKey,
|
||||
}) {
|
||||
return KeyEventSimulator.simulateKeyUpEvent(key, platform: platform, physicalKey: physicalKey);
|
||||
}
|
||||
|
||||
/// Simulates sending a hardware key repeat event through the system channel.
|
||||
///
|
||||
/// This only simulates key presses coming from a physical keyboard, not from a
|
||||
/// soft keyboard.
|
||||
///
|
||||
/// Specify `platform` as one of the platforms allowed in
|
||||
/// [Platform.operatingSystem] to make the event appear to be from that type of
|
||||
/// system. Defaults to the operating system that the test is running on. Some
|
||||
/// platforms (e.g. Windows, iOS) are not yet supported.
|
||||
///
|
||||
/// Returns true if the key event was handled by the framework.
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// - [simulateKeyDownEvent] and [simulateKeyUpEvent] to simulate the
|
||||
/// corresponding key down and up event.
|
||||
Future<bool> simulateKeyRepeatEvent(
|
||||
LogicalKeyboardKey key, {
|
||||
String? platform,
|
||||
PhysicalKeyboardKey? physicalKey,
|
||||
String? character,
|
||||
}) {
|
||||
return KeyEventSimulator.simulateKeyRepeatEvent(key, platform: platform, physicalKey: physicalKey, character: character);
|
||||
}
|
||||
|
||||
/// A [TestVariant] that runs tests with transit modes set to different values
|
||||
/// of [KeyDataTransitMode].
|
||||
class KeySimulatorTransitModeVariant extends TestVariant<KeyDataTransitMode> {
|
||||
/// Creates a [KeySimulatorTransitModeVariant] that tests the given [values].
|
||||
const KeySimulatorTransitModeVariant(this.values);
|
||||
|
||||
/// Creates a [KeySimulatorTransitModeVariant] for each value option of
|
||||
/// [KeyDataTransitMode].
|
||||
KeySimulatorTransitModeVariant.all()
|
||||
: this(KeyDataTransitMode.values.toSet());
|
||||
|
||||
/// Creates a [KeySimulatorTransitModeVariant] that only contains
|
||||
/// [KeyDataTransitMode.keyDataThenRawKeyData].
|
||||
KeySimulatorTransitModeVariant.keyDataThenRawKeyData()
|
||||
: this(<KeyDataTransitMode>{KeyDataTransitMode.keyDataThenRawKeyData});
|
||||
|
||||
@override
|
||||
final Set<KeyDataTransitMode> values;
|
||||
|
||||
@override
|
||||
String describeValue(KeyDataTransitMode value) {
|
||||
switch (value) {
|
||||
case KeyDataTransitMode.rawKeyData:
|
||||
return 'RawKeyEvent';
|
||||
case KeyDataTransitMode.keyDataThenRawKeyData:
|
||||
return 'ui.KeyData then RawKeyEvent';
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<KeyDataTransitMode?> setUp(KeyDataTransitMode value) async {
|
||||
final KeyDataTransitMode? previousSetting = debugKeyEventSimulatorTransitModeOverride;
|
||||
debugKeyEventSimulatorTransitModeOverride = value;
|
||||
return previousSetting;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> tearDown(KeyDataTransitMode value, KeyDataTransitMode? memento) async {
|
||||
// ignore: invalid_use_of_visible_for_testing_member
|
||||
RawKeyboard.instance.clearKeysPressed();
|
||||
// ignore: invalid_use_of_visible_for_testing_member
|
||||
HardwareKeyboard.instance.clearState();
|
||||
// ignore: invalid_use_of_visible_for_testing_member
|
||||
ServicesBinding.instance!.keyEventManager.clearState();
|
||||
debugKeyEventSimulatorTransitModeOverride = memento;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -230,6 +230,7 @@ class TestPointer {
|
||||
timeStamp: timeStamp,
|
||||
kind: kind,
|
||||
device: _device,
|
||||
pointer: pointer,
|
||||
position: _location ?? Offset.zero,
|
||||
);
|
||||
}
|
||||
@@ -255,6 +256,7 @@ class TestPointer {
|
||||
timeStamp: timeStamp,
|
||||
kind: kind,
|
||||
device: _device,
|
||||
pointer: pointer,
|
||||
position: newLocation,
|
||||
delta: delta,
|
||||
);
|
||||
|
||||
@@ -510,7 +510,7 @@ Future<void> expectLater(
|
||||
/// Class that programmatically interacts with widgets and the test environment.
|
||||
///
|
||||
/// For convenience, instances of this class (such as the one provided by
|
||||
/// `testWidget`) can be used as the `vsync` for `AnimationController` objects.
|
||||
/// `testWidgets`) can be used as the `vsync` for `AnimationController` objects.
|
||||
class WidgetTester extends WidgetController implements HitTestDispatcher, TickerProvider {
|
||||
WidgetTester._(TestWidgetsFlutterBinding binding) : super(binding) {
|
||||
if (binding is LiveTestWidgetsFlutterBinding)
|
||||
|
||||
@@ -2,14 +2,44 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
const List<String> platforms = <String>['linux', 'macos', 'android', 'fuchsia'];
|
||||
|
||||
void _verifyKeyEvent<T extends KeyEvent>(KeyEvent event, PhysicalKeyboardKey physical, LogicalKeyboardKey logical, String? character) {
|
||||
expect(event, isA<T>());
|
||||
expect(event.physicalKey, physical);
|
||||
expect(event.logicalKey, logical);
|
||||
expect(event.character, character);
|
||||
expect(event.synthesized, false);
|
||||
}
|
||||
|
||||
void _verifyRawKeyEvent<T extends RawKeyEvent>(RawKeyEvent event, PhysicalKeyboardKey physical, LogicalKeyboardKey logical, String? character) {
|
||||
expect(event, isA<T>());
|
||||
expect(event.physicalKey, physical);
|
||||
expect(event.logicalKey, logical);
|
||||
expect(event.character, character);
|
||||
}
|
||||
|
||||
Future<void> _shouldThrow<T extends Error>(AsyncValueGetter<void> func) async {
|
||||
bool hasError = false;
|
||||
try {
|
||||
await func();
|
||||
} catch (e) {
|
||||
expect(e, isA<T>());
|
||||
hasError = true;
|
||||
} finally {
|
||||
expect(hasError, true);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets('simulates keyboard events', (WidgetTester tester) async {
|
||||
testWidgets('simulates keyboard events (RawEvent)', (WidgetTester tester) async {
|
||||
debugKeyEventSimulatorTransitModeOverride = KeyDataTransitMode.rawKeyData;
|
||||
|
||||
final List<RawKeyEvent> events = <RawKeyEvent>[];
|
||||
|
||||
final FocusNode focusNode = FocusNode();
|
||||
@@ -51,5 +81,247 @@ void main() {
|
||||
|
||||
await tester.pumpWidget(Container());
|
||||
focusNode.dispose();
|
||||
|
||||
debugKeyEventSimulatorTransitModeOverride = null;
|
||||
});
|
||||
|
||||
testWidgets('simulates keyboard events (KeyData then RawKeyEvent)', (WidgetTester tester) async {
|
||||
debugKeyEventSimulatorTransitModeOverride = KeyDataTransitMode.keyDataThenRawKeyData;
|
||||
|
||||
final List<KeyEvent> events = <KeyEvent>[];
|
||||
|
||||
final FocusNode focusNode = FocusNode();
|
||||
|
||||
await tester.pumpWidget(
|
||||
KeyboardListener(
|
||||
focusNode: focusNode,
|
||||
onKeyEvent: events.add,
|
||||
child: Container(),
|
||||
),
|
||||
);
|
||||
|
||||
focusNode.requestFocus();
|
||||
await tester.idle();
|
||||
|
||||
// Key press shiftLeft
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
|
||||
expect(events.length, 1);
|
||||
_verifyKeyEvent<KeyDownEvent>(events[0], PhysicalKeyboardKey.shiftLeft, LogicalKeyboardKey.shiftLeft, null);
|
||||
expect(HardwareKeyboard.instance.physicalKeysPressed, equals(<PhysicalKeyboardKey>{PhysicalKeyboardKey.shiftLeft}));
|
||||
expect(HardwareKeyboard.instance.logicalKeysPressed, equals(<LogicalKeyboardKey>{LogicalKeyboardKey.shiftLeft}));
|
||||
expect(HardwareKeyboard.instance.lockModesEnabled, isEmpty);
|
||||
events.clear();
|
||||
|
||||
await tester.sendKeyRepeatEvent(LogicalKeyboardKey.shiftLeft);
|
||||
expect(events.length, 1);
|
||||
_verifyKeyEvent<KeyRepeatEvent>(events[0], PhysicalKeyboardKey.shiftLeft, LogicalKeyboardKey.shiftLeft, null);
|
||||
expect(HardwareKeyboard.instance.physicalKeysPressed, equals(<PhysicalKeyboardKey>{PhysicalKeyboardKey.shiftLeft}));
|
||||
expect(HardwareKeyboard.instance.logicalKeysPressed, equals(<LogicalKeyboardKey>{LogicalKeyboardKey.shiftLeft}));
|
||||
expect(HardwareKeyboard.instance.lockModesEnabled, isEmpty);
|
||||
events.clear();
|
||||
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft);
|
||||
expect(events.length, 1);
|
||||
_verifyKeyEvent<KeyUpEvent>(events[0], PhysicalKeyboardKey.shiftLeft, LogicalKeyboardKey.shiftLeft, null);
|
||||
expect(HardwareKeyboard.instance.physicalKeysPressed, isEmpty);
|
||||
expect(HardwareKeyboard.instance.logicalKeysPressed, isEmpty);
|
||||
expect(HardwareKeyboard.instance.lockModesEnabled, isEmpty);
|
||||
events.clear();
|
||||
|
||||
// Key press keyA
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.keyA);
|
||||
expect(events.length, 1);
|
||||
_verifyKeyEvent<KeyDownEvent>(events[0], PhysicalKeyboardKey.keyA, LogicalKeyboardKey.keyA, 'a');
|
||||
expect(HardwareKeyboard.instance.physicalKeysPressed, equals(<PhysicalKeyboardKey>{PhysicalKeyboardKey.keyA}));
|
||||
expect(HardwareKeyboard.instance.logicalKeysPressed, equals(<LogicalKeyboardKey>{LogicalKeyboardKey.keyA}));
|
||||
expect(HardwareKeyboard.instance.lockModesEnabled, isEmpty);
|
||||
events.clear();
|
||||
|
||||
await tester.sendKeyRepeatEvent(LogicalKeyboardKey.keyA);
|
||||
_verifyKeyEvent<KeyRepeatEvent>(events[0], PhysicalKeyboardKey.keyA, LogicalKeyboardKey.keyA, 'a');
|
||||
expect(HardwareKeyboard.instance.physicalKeysPressed, equals(<PhysicalKeyboardKey>{PhysicalKeyboardKey.keyA}));
|
||||
expect(HardwareKeyboard.instance.logicalKeysPressed, equals(<LogicalKeyboardKey>{LogicalKeyboardKey.keyA}));
|
||||
expect(HardwareKeyboard.instance.lockModesEnabled, isEmpty);
|
||||
events.clear();
|
||||
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.keyA);
|
||||
_verifyKeyEvent<KeyUpEvent>(events[0], PhysicalKeyboardKey.keyA, LogicalKeyboardKey.keyA, null);
|
||||
expect(HardwareKeyboard.instance.physicalKeysPressed, isEmpty);
|
||||
expect(HardwareKeyboard.instance.logicalKeysPressed, isEmpty);
|
||||
expect(HardwareKeyboard.instance.lockModesEnabled, isEmpty);
|
||||
events.clear();
|
||||
|
||||
// Key press numpad1
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.numpad1);
|
||||
_verifyKeyEvent<KeyDownEvent>(events[0], PhysicalKeyboardKey.numpad1, LogicalKeyboardKey.numpad1, null);
|
||||
expect(HardwareKeyboard.instance.physicalKeysPressed, equals(<PhysicalKeyboardKey>{PhysicalKeyboardKey.numpad1}));
|
||||
expect(HardwareKeyboard.instance.logicalKeysPressed, equals(<LogicalKeyboardKey>{LogicalKeyboardKey.numpad1}));
|
||||
expect(HardwareKeyboard.instance.lockModesEnabled, isEmpty);
|
||||
events.clear();
|
||||
|
||||
await tester.sendKeyRepeatEvent(LogicalKeyboardKey.numpad1);
|
||||
_verifyKeyEvent<KeyRepeatEvent>(events[0], PhysicalKeyboardKey.numpad1, LogicalKeyboardKey.numpad1, null);
|
||||
expect(HardwareKeyboard.instance.physicalKeysPressed, equals(<PhysicalKeyboardKey>{PhysicalKeyboardKey.numpad1}));
|
||||
expect(HardwareKeyboard.instance.logicalKeysPressed, equals(<LogicalKeyboardKey>{LogicalKeyboardKey.numpad1}));
|
||||
expect(HardwareKeyboard.instance.lockModesEnabled, isEmpty);
|
||||
events.clear();
|
||||
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.numpad1);
|
||||
_verifyKeyEvent<KeyUpEvent>(events[0], PhysicalKeyboardKey.numpad1, LogicalKeyboardKey.numpad1, null);
|
||||
expect(HardwareKeyboard.instance.physicalKeysPressed, isEmpty);
|
||||
expect(HardwareKeyboard.instance.logicalKeysPressed, isEmpty);
|
||||
expect(HardwareKeyboard.instance.lockModesEnabled, isEmpty);
|
||||
events.clear();
|
||||
|
||||
// Key press numLock (1st time)
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.numLock);
|
||||
_verifyKeyEvent<KeyDownEvent>(events[0], PhysicalKeyboardKey.numLock, LogicalKeyboardKey.numLock, null);
|
||||
expect(HardwareKeyboard.instance.physicalKeysPressed, equals(<PhysicalKeyboardKey>{PhysicalKeyboardKey.numLock}));
|
||||
expect(HardwareKeyboard.instance.logicalKeysPressed, equals(<LogicalKeyboardKey>{LogicalKeyboardKey.numLock}));
|
||||
expect(HardwareKeyboard.instance.lockModesEnabled, equals(<KeyboardLockMode>{KeyboardLockMode.numLock}));
|
||||
events.clear();
|
||||
|
||||
await tester.sendKeyRepeatEvent(LogicalKeyboardKey.numLock);
|
||||
_verifyKeyEvent<KeyRepeatEvent>(events[0], PhysicalKeyboardKey.numLock, LogicalKeyboardKey.numLock, null);
|
||||
expect(HardwareKeyboard.instance.physicalKeysPressed, equals(<PhysicalKeyboardKey>{PhysicalKeyboardKey.numLock}));
|
||||
expect(HardwareKeyboard.instance.logicalKeysPressed, equals(<LogicalKeyboardKey>{LogicalKeyboardKey.numLock}));
|
||||
expect(HardwareKeyboard.instance.lockModesEnabled, equals(<KeyboardLockMode>{KeyboardLockMode.numLock}));
|
||||
events.clear();
|
||||
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.numLock);
|
||||
_verifyKeyEvent<KeyUpEvent>(events[0], PhysicalKeyboardKey.numLock, LogicalKeyboardKey.numLock, null);
|
||||
expect(HardwareKeyboard.instance.physicalKeysPressed, isEmpty);
|
||||
expect(HardwareKeyboard.instance.logicalKeysPressed, isEmpty);
|
||||
expect(HardwareKeyboard.instance.lockModesEnabled, equals(<KeyboardLockMode>{KeyboardLockMode.numLock}));
|
||||
events.clear();
|
||||
|
||||
// Key press numLock (2nd time)
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.numLock);
|
||||
_verifyKeyEvent<KeyDownEvent>(events[0], PhysicalKeyboardKey.numLock, LogicalKeyboardKey.numLock, null);
|
||||
expect(HardwareKeyboard.instance.physicalKeysPressed, equals(<PhysicalKeyboardKey>{PhysicalKeyboardKey.numLock}));
|
||||
expect(HardwareKeyboard.instance.logicalKeysPressed, equals(<LogicalKeyboardKey>{LogicalKeyboardKey.numLock}));
|
||||
expect(HardwareKeyboard.instance.lockModesEnabled, isEmpty);
|
||||
events.clear();
|
||||
|
||||
await tester.sendKeyRepeatEvent(LogicalKeyboardKey.numLock);
|
||||
_verifyKeyEvent<KeyRepeatEvent>(events[0], PhysicalKeyboardKey.numLock, LogicalKeyboardKey.numLock, null);
|
||||
expect(HardwareKeyboard.instance.physicalKeysPressed, equals(<PhysicalKeyboardKey>{PhysicalKeyboardKey.numLock}));
|
||||
expect(HardwareKeyboard.instance.logicalKeysPressed, equals(<LogicalKeyboardKey>{LogicalKeyboardKey.numLock}));
|
||||
expect(HardwareKeyboard.instance.lockModesEnabled, isEmpty);
|
||||
events.clear();
|
||||
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.numLock);
|
||||
_verifyKeyEvent<KeyUpEvent>(events[0], PhysicalKeyboardKey.numLock, LogicalKeyboardKey.numLock, null);
|
||||
expect(HardwareKeyboard.instance.physicalKeysPressed, isEmpty);
|
||||
expect(HardwareKeyboard.instance.logicalKeysPressed, isEmpty);
|
||||
expect(HardwareKeyboard.instance.lockModesEnabled, isEmpty);
|
||||
events.clear();
|
||||
|
||||
await tester.idle();
|
||||
|
||||
await tester.pumpWidget(Container());
|
||||
focusNode.dispose();
|
||||
|
||||
debugKeyEventSimulatorTransitModeOverride = null;
|
||||
});
|
||||
|
||||
testWidgets('simulates using the correct transit mode: rawKeyData', (WidgetTester tester) async {
|
||||
debugKeyEventSimulatorTransitModeOverride = KeyDataTransitMode.rawKeyData;
|
||||
|
||||
final List<Object> events = <Object>[];
|
||||
|
||||
final FocusNode focusNode = FocusNode();
|
||||
await tester.pumpWidget(
|
||||
Focus(
|
||||
focusNode: focusNode,
|
||||
onKey: (FocusNode node, RawKeyEvent event) {
|
||||
events.add(event);
|
||||
return KeyEventResult.ignored;
|
||||
},
|
||||
onKeyEvent: (FocusNode node, KeyEvent event) {
|
||||
events.add(event);
|
||||
return KeyEventResult.ignored;
|
||||
},
|
||||
child: Container(),
|
||||
),
|
||||
);
|
||||
|
||||
focusNode.requestFocus();
|
||||
await tester.idle();
|
||||
|
||||
// A (physical keyA, logical keyA) is pressed.
|
||||
await simulateKeyDownEvent(LogicalKeyboardKey.keyA);
|
||||
expect(events.length, 2);
|
||||
expect(events[0], isA<KeyEvent>());
|
||||
_verifyKeyEvent<KeyDownEvent>(events[0] as KeyEvent, PhysicalKeyboardKey.keyA, LogicalKeyboardKey.keyA, 'a');
|
||||
expect(events[1], isA<RawKeyEvent>());
|
||||
_verifyRawKeyEvent<RawKeyDownEvent>(events[1] as RawKeyEvent, PhysicalKeyboardKey.keyA, LogicalKeyboardKey.keyA, 'a');
|
||||
events.clear();
|
||||
|
||||
// A (physical keyA, logical keyB) is released.
|
||||
//
|
||||
// Since this event was synthesized and regularized before being sent to
|
||||
// HardwareKeyboard, this event will be accepted.
|
||||
await simulateKeyUpEvent(LogicalKeyboardKey.keyB, physicalKey: PhysicalKeyboardKey.keyA);
|
||||
expect(events.length, 2);
|
||||
expect(events[0], isA<KeyEvent>());
|
||||
_verifyKeyEvent<KeyUpEvent>(events[0] as KeyEvent, PhysicalKeyboardKey.keyA, LogicalKeyboardKey.keyA, null);
|
||||
expect(events[1], isA<RawKeyEvent>());
|
||||
_verifyRawKeyEvent<RawKeyUpEvent>(events[1] as RawKeyEvent, PhysicalKeyboardKey.keyA, LogicalKeyboardKey.keyB, null);
|
||||
events.clear();
|
||||
|
||||
// Manually switch the transit mode to `keyDataThenRawKeyData`. This will
|
||||
// never happen in real applications so the assertion error can verify that
|
||||
// the transit mode is correctly applied.
|
||||
debugKeyEventSimulatorTransitModeOverride = KeyDataTransitMode.keyDataThenRawKeyData;
|
||||
|
||||
await _shouldThrow<AssertionError>(() =>
|
||||
simulateKeyUpEvent(LogicalKeyboardKey.keyB, physicalKey: PhysicalKeyboardKey.keyA));
|
||||
|
||||
debugKeyEventSimulatorTransitModeOverride = null;
|
||||
});
|
||||
|
||||
testWidgets('simulates using the correct transit mode: keyDataThenRawKeyData', (WidgetTester tester) async {
|
||||
debugKeyEventSimulatorTransitModeOverride = KeyDataTransitMode.keyDataThenRawKeyData;
|
||||
|
||||
final List<Object> events = <Object>[];
|
||||
|
||||
final FocusNode focusNode = FocusNode();
|
||||
await tester.pumpWidget(
|
||||
Focus(
|
||||
focusNode: focusNode,
|
||||
onKey: (FocusNode node, RawKeyEvent event) {
|
||||
events.add(event);
|
||||
return KeyEventResult.ignored;
|
||||
},
|
||||
onKeyEvent: (FocusNode node, KeyEvent event) {
|
||||
events.add(event);
|
||||
return KeyEventResult.ignored;
|
||||
},
|
||||
child: Container(),
|
||||
),
|
||||
);
|
||||
|
||||
focusNode.requestFocus();
|
||||
await tester.idle();
|
||||
|
||||
// A (physical keyA, logical keyA) is pressed.
|
||||
await simulateKeyDownEvent(LogicalKeyboardKey.keyA);
|
||||
expect(events.length, 2);
|
||||
expect(events[0], isA<KeyEvent>());
|
||||
_verifyKeyEvent<KeyDownEvent>(events[0] as KeyEvent, PhysicalKeyboardKey.keyA, LogicalKeyboardKey.keyA, 'a');
|
||||
expect(events[1], isA<RawKeyEvent>());
|
||||
_verifyRawKeyEvent<RawKeyDownEvent>(events[1] as RawKeyEvent, PhysicalKeyboardKey.keyA, LogicalKeyboardKey.keyA, 'a');
|
||||
events.clear();
|
||||
|
||||
// A (physical keyA, logical keyB) is released.
|
||||
//
|
||||
// Since this event is transmitted to HardwareKeyboard as-is, it will be rejected due to
|
||||
// inconsistent logical key. This does not indicate behaviral difference,
|
||||
// since KeyData is will never send malformed data sequence in real applications.
|
||||
await _shouldThrow<AssertionError>(() =>
|
||||
simulateKeyUpEvent(LogicalKeyboardKey.keyB, physicalKey: PhysicalKeyboardKey.keyA));
|
||||
|
||||
debugKeyEventSimulatorTransitModeOverride = null;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user