Functionality to check handlers set on platform channels (#57696)

This commit is contained in:
Dan Field
2020-05-21 14:52:05 -07:00
committed by GitHub
parent cdef67797c
commit 02b1080101
10 changed files with 145 additions and 20 deletions

View File

@@ -134,10 +134,20 @@ class TestDefaultBinaryMessenger extends BinaryMessenger {
delegate.setMessageHandler(channel, handler);
}
@override
bool checkMessageHandler(String channel, MessageHandler handler) {
return delegate.checkMessageHandler(channel, handler);
}
@override
void setMockMessageHandler(String channel, MessageHandler handler) {
delegate.setMockMessageHandler(channel, handler);
}
@override
bool checkMockMessageHandler(String channel, MessageHandler handler) {
return delegate.checkMockMessageHandler(channel, handler);
}
}
/// Base class for bindings used by widgets library tests.

View File

@@ -52,20 +52,14 @@ class TestTextInput {
register();
}
/// Installs this object as a mock handler for [SystemChannels.textInput].
void register() {
SystemChannels.textInput.setMockMethodCallHandler(_handleTextInputCall);
_isRegistered = true;
}
void register() => SystemChannels.textInput.setMockMethodCallHandler(_handleTextInputCall);
/// Removes this object as a mock handler for [SystemChannels.textInput].
///
/// After calling this method, the channel will exchange messages with the
/// Flutter engine. Use this with [FlutterDriver] tests that need to display
/// on-screen keyboard provided by the operating system.
void unregister() {
SystemChannels.textInput.setMockMethodCallHandler(null);
_isRegistered = false;
}
void unregister() => SystemChannels.textInput.setMockMethodCallHandler(null);
/// Log for method calls.
///
@@ -76,12 +70,13 @@ class TestTextInput {
/// Whether this [TestTextInput] is registered with [SystemChannels.textInput].
///
/// Use [register] and [unregister] methods to control this value.
// TODO(dnfield): This is unreliable. https://github.com/flutter/flutter/issues/47180
bool get isRegistered => _isRegistered;
bool _isRegistered = false;
bool get isRegistered => SystemChannels.textInput.checkMockMethodCallHandler(_handleTextInputCall);
/// Whether there are any active clients listening to text input.
bool get hasAnyClients => _client > 0;
bool get hasAnyClients {
assert(isRegistered);
return _client > 0;
}
int _client = 0;
@@ -122,11 +117,15 @@ class TestTextInput {
}
/// Whether the onscreen keyboard is visible to the user.
bool get isVisible => _isVisible;
bool get isVisible {
assert(isRegistered);
return _isVisible;
}
bool _isVisible = false;
/// Simulates the user changing the [TextEditingValue] to the given value.
void updateEditingValue(TextEditingValue value) {
assert(isRegistered);
// Not using the `expect` function because in the case of a FlutterDriver
// test this code does not run in a package:test test zone.
if (_client == 0)
@@ -149,6 +148,7 @@ class TestTextInput {
/// - User pressed the home button and sent the application to background.
/// - User closed the virtual keyboard.
void closeConnection() {
assert(isRegistered);
// Not using the `expect` function because in the case of a FlutterDriver
// test this code does not run in a package:test test zone.
if (_client == 0)
@@ -167,6 +167,7 @@ class TestTextInput {
/// Simulates the user typing the given text.
void enterText(String text) {
assert(isRegistered);
updateEditingValue(TextEditingValue(
text: text,
));
@@ -176,6 +177,7 @@ class TestTextInput {
/// Does not check that the [TextInputAction] performed is an acceptable one
/// based on the `inputAction` [setClientArgs].
Future<void> receiveAction(TextInputAction action) async {
assert(isRegistered);
return TestAsyncUtils.guard(() {
// Not using the `expect` function because in the case of a FlutterDriver
// test this code does not run in a package:test test zone.
@@ -215,6 +217,7 @@ class TestTextInput {
/// Simulates the user hiding the onscreen keyboard.
void hide() {
assert(isRegistered);
_isVisible = false;
}
}