Support for tests that respond to text input (#7915)
This commit is contained in:
@@ -15,5 +15,6 @@ export 'src/matchers.dart';
|
||||
export 'src/test_async_utils.dart';
|
||||
export 'src/stack_manipulation.dart';
|
||||
export 'src/test_pointer.dart';
|
||||
export 'src/test_text_input.dart';
|
||||
export 'src/test_vsync.dart';
|
||||
export 'src/widget_tester.dart';
|
||||
|
||||
@@ -20,6 +20,7 @@ import 'package:vector_math/vector_math_64.dart';
|
||||
|
||||
import 'stack_manipulation.dart';
|
||||
import 'test_async_utils.dart';
|
||||
import 'test_text_input.dart';
|
||||
|
||||
/// Phases that can be reached by [WidgetTester.pumpWidget] and
|
||||
/// [TestWidgetsFlutterBinding.pump].
|
||||
@@ -125,6 +126,7 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
|
||||
);
|
||||
});
|
||||
};
|
||||
_testTextInput = new TestTextInput()..register();
|
||||
super.initInstances();
|
||||
}
|
||||
|
||||
@@ -188,6 +190,20 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
|
||||
super.dispatchEvent(event, result);
|
||||
}
|
||||
|
||||
/// A stub for the system's onscreen keyboard. Callers must set the
|
||||
/// [focusedEditable] before using this value.
|
||||
TestTextInput get testTextInput => _testTextInput;
|
||||
TestTextInput _testTextInput;
|
||||
|
||||
/// The current client of the onscreen keyboard. Callers must pump
|
||||
/// an additional frame after setting this property to complete the
|
||||
/// the focus change.
|
||||
EditableTextState get focusedEditable => _focusedEditable;
|
||||
EditableTextState _focusedEditable;
|
||||
set focusedEditable (EditableTextState editable) {
|
||||
_focusedEditable = editable..requestKeyboard();
|
||||
}
|
||||
|
||||
/// Returns the exception most recently caught by the Flutter framework.
|
||||
///
|
||||
/// Call this if you expect an exception during a test. If an exception is
|
||||
|
||||
63
packages/flutter_test/lib/src/test_text_input.dart
Normal file
63
packages/flutter_test/lib/src/test_text_input.dart
Normal file
@@ -0,0 +1,63 @@
|
||||
// Copyright 2015 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 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import 'widget_tester.dart';
|
||||
|
||||
const String _kTextInputClientChannel = 'flutter/textinputclient';
|
||||
|
||||
/// A testing stub for the system's onscreen keyboard.
|
||||
///
|
||||
/// Typical app tests will not need to use this class directly.
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [WidgetTester.enterText], which uses this class to simulate keyboard input.
|
||||
/// * [WidgetTester.showKeyboard], which uses this class to simulate showing the
|
||||
/// popup keyboard and initializing its text.
|
||||
class TestTextInput {
|
||||
void register() {
|
||||
PlatformMessages.setMockJSONMessageHandler('flutter/textinput', handleJSONMessage);
|
||||
}
|
||||
|
||||
int _client = 0;
|
||||
Map<String, dynamic> editingState;
|
||||
|
||||
Future<dynamic> handleJSONMessage(dynamic message) async {
|
||||
final String method = message['method'];
|
||||
final List<dynamic> args= message['args'];
|
||||
switch (method) {
|
||||
case 'TextInput.setClient':
|
||||
_client = args[0];
|
||||
break;
|
||||
case 'TextInput.setEditingState':
|
||||
editingState = args[0];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void updateEditingState(TextEditingState state) {
|
||||
expect(_client, isNonZero);
|
||||
String message = JSON.encode(<String, dynamic>{
|
||||
'method': 'TextInputClient.updateEditingState',
|
||||
'args': <dynamic>[_client, state.toJSON()],
|
||||
});
|
||||
Uint8List encoded = UTF8.encoder.convert(message);
|
||||
PlatformMessages.handlePlatformMessage(
|
||||
_kTextInputClientChannel, encoded.buffer.asByteData(), (_) {});
|
||||
}
|
||||
|
||||
void enterText(String text) {
|
||||
updateEditingState(new TextEditingState(
|
||||
text: text,
|
||||
composingBase: 0,
|
||||
composingExtent: text.length,
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import 'binding.dart';
|
||||
import 'controller.dart';
|
||||
import 'finders.dart';
|
||||
import 'test_async_utils.dart';
|
||||
import 'test_text_input.dart';
|
||||
|
||||
export 'package:test/test.dart' hide expect;
|
||||
|
||||
@@ -399,6 +400,36 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker
|
||||
void _endOfTestVerifications() {
|
||||
verifyTickersWereDisposed('at the end of the test');
|
||||
}
|
||||
|
||||
/// Returns the TestTextInput singleton.
|
||||
///
|
||||
/// Typical app tests will not need to use this value. To add text to widgets
|
||||
/// like [Input] or [TextField], call [enterText].
|
||||
TestTextInput get testTextInput => binding.testTextInput;
|
||||
|
||||
/// Give the EditableText widget specified by [finder] the focus, as if the
|
||||
/// onscreen keyboard had appeared.
|
||||
///
|
||||
/// Tests that just need to add text to widgets like [Input] or [TextField]
|
||||
/// only need to call [enterText].
|
||||
Future<Null> showKeyboard(Finder finder) async {
|
||||
// TODO(hansmuller): Once find.descendant (#7789) lands replace the following
|
||||
// RHS with state(find.descendant(finder), find.byType(EditableText)).
|
||||
final EditableTextState editable = state(finder);
|
||||
if (editable != binding.focusedEditable) {
|
||||
binding.focusedEditable = editable;
|
||||
await pump();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Give the EditableText widget specified by [finder] the focus and
|
||||
/// enter [text] as if it been provided by the onscreen keyboard.
|
||||
Future<Null> enterText(Finder finder, String text) async {
|
||||
await showKeyboard(finder);
|
||||
testTextInput.enterText(text);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
typedef void _TickerDisposeCallback(_TestTicker ticker);
|
||||
|
||||
Reference in New Issue
Block a user