Support all keyboard actions. (#11344) (#18855)

* Support all keyboard actions. (#11344)
This commit is contained in:
matthew-carroll
2018-07-02 06:58:35 +00:00
committed by GitHub
parent af5d4c6882
commit 2a65505e8a
8 changed files with 830 additions and 77 deletions

View File

@@ -6,6 +6,7 @@ import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/foundation.dart';
import 'widget_tester.dart';
@@ -126,21 +127,42 @@ class TestTextInput {
/// Simulates the user pressing one of the [TextInputAction] buttons.
/// Does not check that the [TextInputAction] performed is an acceptable one
/// based on the `inputAction` [setClientArgs].
void receiveAction(TextInputAction action) {
// 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)
throw new TestFailure('Tried to use TestTextInput with no keyboard attached. You must use WidgetTester.showKeyboard() first.');
BinaryMessages.handlePlatformMessage(
SystemChannels.textInput.name,
SystemChannels.textInput.codec.encodeMethodCall(
new MethodCall(
'TextInputClient.performAction',
<dynamic>[_client, action.toString()],
Future<Null> receiveAction(TextInputAction action) async {
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.
if (_client == 0) {
throw new TestFailure('Tried to use TestTextInput with no keyboard attached. You must use WidgetTester.showKeyboard() first.');
}
final Completer<Null> completer = new Completer<Null>();
BinaryMessages.handlePlatformMessage(
SystemChannels.textInput.name,
SystemChannels.textInput.codec.encodeMethodCall(
new MethodCall(
'TextInputClient.performAction',
<dynamic>[_client, action.toString()],
),
),
),
(ByteData data) { /* response from framework is discarded */ },
);
(ByteData data) {
try {
// Decoding throws a PlatformException if the data represents an
// error, and that's all we care about here.
SystemChannels.textInput.codec.decodeEnvelope(data);
// No error was found. Complete without issue.
completer.complete();
} catch (error) {
// An exception occurred as a result of receiveAction()'ing. Report
// that error.
completer.completeError(error);
}
},
);
return completer.future;
});
}
/// Simulates the user hiding the onscreen keyboard.

View File

@@ -0,0 +1,36 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('receiveAction() forwards exception when exception occurs during action processing',
(WidgetTester tester) async {
// Setup a widget that can receive focus so that we can open the keyboard.
final Widget widget = new MaterialApp(
home: const Material(
child: const TextField(),
),
);
await tester.pumpWidget(widget);
// Keyboard must be shown for receiveAction() to function.
await tester.showKeyboard(find.byType(TextField));
// Register a handler for the text input channel that throws an error. This
// error should be reported within a PlatformException by TestTextInput.
SystemChannels.textInput.setMethodCallHandler((MethodCall call) {
throw new FlutterError('A fake error occurred during action processing.');
});
try {
await tester.testTextInput.receiveAction(TextInputAction.done);
fail('Expected a PlatformException, but it was not thrown.');
} catch (e) {
expect(e, isInstanceOf<PlatformException>());
}
});
}

View File

@@ -518,10 +518,10 @@ void main() {
),
);
await tester.showKeyboard(find.byType(TextField));
tester.testTextInput.receiveAction(TextInputAction.done);
await tester.testTextInput.receiveAction(TextInputAction.done);
await tester.pump();
await tester.showKeyboard(find.byType(TextField));
tester.testTextInput.receiveAction(TextInputAction.done);
await tester.testTextInput.receiveAction(TextInputAction.done);
await tester.pump();
await tester.showKeyboard(find.byType(TextField));
await tester.showKeyboard(find.byType(TextField));