Ian Hickson | 449f4a6 | 2019-11-27 15:04:02 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
matthew-carroll | 2a65505 | 2018-07-02 06:58:35 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Greg Spencer | 738ce43 | 2021-07-26 23:40:49 -0700 | [diff] [blame] | 5 | // 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 Goderbauer | b308555 | 2022-12-20 16:03:21 -0800 | [diff] [blame] | 10 | library; |
Greg Spencer | 738ce43 | 2021-07-26 23:40:49 -0700 | [diff] [blame] | 11 | |
Matej Knopp | 7e8f0e5 | 2022-08-03 21:51:28 +0200 | [diff] [blame] | 12 | import 'package:flutter/foundation.dart'; |
matthew-carroll | 2a65505 | 2018-07-02 06:58:35 +0000 | [diff] [blame] | 13 | import 'package:flutter/material.dart'; |
| 14 | import 'package:flutter/services.dart'; |
| 15 | import 'package:flutter_test/flutter_test.dart'; |
| 16 | |
| 17 | void main() { |
LongCatIsLooong | c1c5b49 | 2021-04-02 15:44:02 -0700 | [diff] [blame] | 18 | 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 Ardhuin | 440ce8f | 2019-03-07 21:09:28 +0100 | [diff] [blame] | 36 | testWidgets('receiveAction() forwards exception when exception occurs during action processing', (WidgetTester tester) async { |
matthew-carroll | 2a65505 | 2018-07-02 06:58:35 +0000 | [diff] [blame] | 37 | // Setup a widget that can receive focus so that we can open the keyboard. |
Dan Field | ea5435c | 2018-09-25 13:57:12 -0400 | [diff] [blame] | 38 | const Widget widget = MaterialApp( |
| 39 | home: Material( |
Alexandre Ardhuin | eda03e2 | 2018-08-02 12:02:32 +0200 | [diff] [blame] | 40 | child: TextField(), |
matthew-carroll | 2a65505 | 2018-07-02 06:58:35 +0000 | [diff] [blame] | 41 | ), |
| 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 Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 51 | throw FlutterError('A fake error occurred during action processing.'); |
matthew-carroll | 2a65505 | 2018-07-02 06:58:35 +0000 | [diff] [blame] | 52 | }); |
| 53 | |
Alexandre Ardhuin | 4881b33 | 2021-05-12 08:34:02 +0200 | [diff] [blame] | 54 | await expectLater( |
| 55 | () => tester.testTextInput.receiveAction(TextInputAction.done), |
| 56 | throwsA(isA<PlatformException>()), |
| 57 | ); |
matthew-carroll | 2a65505 | 2018-07-02 06:58:35 +0000 | [diff] [blame] | 58 | }); |
Matej Knopp | 7e8f0e5 | 2022-08-03 21:51:28 +0200 | [diff] [blame] | 59 | |
| 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 Volkert | 00aac68 | 2018-07-27 08:44:39 -0700 | [diff] [blame] | 79 | } |