blob: eb911ff2c31d31a647fd85a6567ed2d6a6b309a9 [file] [log] [blame]
Ian Hickson449f4a62019-11-27 15:04:02 -08001// Copyright 2014 The Flutter Authors. All rights reserved.
matthew-carroll2a655052018-07-02 06:58:35 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Greg Spencer738ce432021-07-26 23:40:49 -07005// TODO(gspencergoog): Remove this tag once this test's state leaks/test
6// dependencies have been fixed.
7// https://github.com/flutter/flutter/issues/85160
8// Fails with "flutter test --test-randomize-ordering-seed=20210721"
9@Tags(<String>['no-shuffle'])
Michael Goderbauerb3085552022-12-20 16:03:21 -080010library;
Greg Spencer738ce432021-07-26 23:40:49 -070011
Matej Knopp7e8f0e52022-08-03 21:51:28 +020012import 'package:flutter/foundation.dart';
matthew-carroll2a655052018-07-02 06:58:35 +000013import 'package:flutter/material.dart';
14import 'package:flutter/services.dart';
15import 'package:flutter_test/flutter_test.dart';
16
17void main() {
LongCatIsLooongc1c5b492021-04-02 15:44:02 -070018 testWidgets('enterText works', (WidgetTester tester) async {
19 await tester.pumpWidget(
20 const MaterialApp(
21 home: Material(
22 child: TextField(),
23 ),
24 ),
25 );
26
27 final EditableTextState state = tester.state(find.byType(EditableText));
28 expect(state.textEditingValue.text, '');
29
30 await tester.enterText(find.byType(EditableText), 'let there be text');
31 expect(state.textEditingValue.text, 'let there be text');
32 expect(state.textEditingValue.selection.isCollapsed, isTrue);
33 expect(state.textEditingValue.selection.baseOffset, 17);
34 });
35
Alexandre Ardhuin440ce8f2019-03-07 21:09:28 +010036 testWidgets('receiveAction() forwards exception when exception occurs during action processing', (WidgetTester tester) async {
matthew-carroll2a655052018-07-02 06:58:35 +000037 // Setup a widget that can receive focus so that we can open the keyboard.
Dan Fieldea5435c2018-09-25 13:57:12 -040038 const Widget widget = MaterialApp(
39 home: Material(
Alexandre Ardhuineda03e22018-08-02 12:02:32 +020040 child: TextField(),
matthew-carroll2a655052018-07-02 06:58:35 +000041 ),
42 );
43 await tester.pumpWidget(widget);
44
45 // Keyboard must be shown for receiveAction() to function.
46 await tester.showKeyboard(find.byType(TextField));
47
48 // Register a handler for the text input channel that throws an error. This
49 // error should be reported within a PlatformException by TestTextInput.
50 SystemChannels.textInput.setMethodCallHandler((MethodCall call) {
Alexandre Ardhuind927c932018-09-12 08:29:29 +020051 throw FlutterError('A fake error occurred during action processing.');
matthew-carroll2a655052018-07-02 06:58:35 +000052 });
53
Alexandre Ardhuin4881b332021-05-12 08:34:02 +020054 await expectLater(
55 () => tester.testTextInput.receiveAction(TextInputAction.done),
56 throwsA(isA<PlatformException>()),
57 );
matthew-carroll2a655052018-07-02 06:58:35 +000058 });
Matej Knopp7e8f0e52022-08-03 21:51:28 +020059
60 testWidgets('selectors are called on macOS', (WidgetTester tester) async {
61 List<dynamic>? selectorNames;
62 await SystemChannels.textInput.invokeMethod('TextInput.setClient', <dynamic>[1, <String, dynamic>{}]);
63 await SystemChannels.textInput.invokeMethod('TextInput.show');
64 SystemChannels.textInput.setMethodCallHandler((MethodCall call) async {
65 if (call.method == 'TextInputClient.performSelectors') {
66 selectorNames = (call.arguments as List<dynamic>)[1] as List<dynamic>;
67 }
68 });
69 await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft);
70 await tester.sendKeyDownEvent(LogicalKeyboardKey.arrowUp);
71 await SystemChannels.textInput.invokeMethod('TextInput.clearClient');
72
73 if (defaultTargetPlatform == TargetPlatform.macOS) {
74 expect(selectorNames, <dynamic>['moveBackward:', 'moveToBeginningOfParagraph:']);
75 } else {
76 expect(selectorNames, isNull);
77 }
78 }, variant: TargetPlatformVariant.all());
Todd Volkert00aac682018-07-27 08:44:39 -070079}