-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathcode_controller_shortcut_test.dart
More file actions
150 lines (128 loc) · 4.33 KB
/
code_controller_shortcut_test.dart
File metadata and controls
150 lines (128 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import '../common/create_app.dart';
import '../common/snippets.dart';
import '../common/widget_tester.dart';
const _copiedText = '''
void method() {// [START section1]
}// [END section1]
''';
const _visibleTextAfterCut = '''
class MyClass {
}
''';
void main() {
final calls = <MethodCall>[];
void mockClipboardHandler(WidgetTester wt) {
calls.clear();
wt.binding.defaultBinaryMessenger.setMockMethodCallHandler(
SystemChannels.platform,
(MethodCall call) async {
calls.add(call);
return null;
},
);
}
group('CodeController. Shortcuts.', () {
testWidgets('Select all', (WidgetTester wt) async {
final controller = await pumpController(wt, MethodSnippet.full);
controller.foldAt(1);
await wt.sendKeyDownEvent(LogicalKeyboardKey.control);
await wt.sendKeyEvent(LogicalKeyboardKey.keyA);
await wt.sendKeyUpEvent(LogicalKeyboardKey.control);
expect(
controller.value,
const TextEditingValue(
text: MethodSnippet.visibleFolded1,
selection: TextSelection(
baseOffset: 0,
extentOffset: MethodSnippet.visibleFolded1.length,
),
),
);
});
testWidgets('Copy, Cut', (WidgetTester wt) async {
final examples = [
//
_CopyExample(
'Copy with Ctrl-C',
act: () async {
await wt.sendKeyDownEvent(LogicalKeyboardKey.control);
await wt.sendKeyEvent(LogicalKeyboardKey.keyC);
await wt.sendKeyUpEvent(LogicalKeyboardKey.control);
},
visibleTextAfter: MethodSnippet.visibleFolded1,
),
_CopyExample(
'Copy with Ctrl-Insert',
act: () async {
await wt.sendKeyDownEvent(LogicalKeyboardKey.control);
await wt.sendKeyEvent(LogicalKeyboardKey.insert);
await wt.sendKeyUpEvent(LogicalKeyboardKey.control);
},
visibleTextAfter: MethodSnippet.visibleFolded1,
),
_CopyExample(
'Cut with Ctrl-X',
act: () async {
await wt.sendKeyDownEvent(LogicalKeyboardKey.control);
await wt.sendKeyEvent(LogicalKeyboardKey.keyX);
await wt.sendKeyUpEvent(LogicalKeyboardKey.control);
},
visibleTextAfter: _visibleTextAfterCut,
),
_CopyExample(
'Cut with Shift-Delete',
act: () async {
await wt.sendKeyDownEvent(LogicalKeyboardKey.shift);
await wt.sendKeyEvent(LogicalKeyboardKey.delete);
await wt.sendKeyUpEvent(LogicalKeyboardKey.shift);
},
visibleTextAfter: _visibleTextAfterCut,
),
];
final controller = await pumpController(wt, '');
for (final example in examples) {
controller.value = const TextEditingValue(text: MethodSnippet.full);
controller.foldAt(1);
await wt.selectFromHome(18, offset: 16);
mockClipboardHandler(wt);
await example.act();
expect(calls.length, 1, reason: example.name);
expect(calls[0].method, 'Clipboard.setData', reason: example.name);
expect(calls[0].arguments, {'text': _copiedText}, reason: example.name);
expect(controller.text, example.visibleTextAfter, reason: example.name);
}
});
test('IME composition bypasses popup arrow key handling', () {
final controller = createController('');
controller.popupController.show(['one', 'two']);
controller.value = const TextEditingValue(
text: 'ni',
selection: TextSelection.collapsed(offset: 2),
composing: TextRange(start: 0, end: 2),
);
final result = controller.onKey(
const KeyDownEvent(
timeStamp: Duration.zero,
physicalKey: PhysicalKeyboardKey.arrowDown,
logicalKey: LogicalKeyboardKey.arrowDown,
),
);
expect(result, KeyEventResult.ignored);
expect(controller.popupController.selectedIndex, 0);
controller.dispose();
});
});
}
class _CopyExample {
final String name;
final Future<void> Function() act;
final String visibleTextAfter;
const _CopyExample(
this.name, {
required this.act,
required this.visibleTextAfter,
});
}