forked from VsVim/VsVim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyProcessorTest.cs
167 lines (144 loc) · 5.43 KB
/
KeyProcessorTest.cs
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Windows.Input;
using Moq;
using NUnit.Framework;
using Vim.UnitTest.Mock;
namespace Vim.UI.Wpf.Test
{
[TestFixture]
public class KeyProcessorTest
{
protected IntPtr _keyboardId;
protected MockRepository _factory;
protected Mock<IVimBuffer> _buffer;
protected KeyProcessor _processor;
[SetUp]
public void Setup()
{
Setup(null);
}
protected virtual void Setup(string languageId)
{
if (!String.IsNullOrEmpty(languageId))
{
_keyboardId = NativeMethods.LoadKeyboardLayout(languageId, NativeMethods.KLF_ACTIVATE);
Assert.AreNotEqual(_keyboardId, IntPtr.Zero);
}
_factory = new MockRepository(MockBehavior.Strict);
_buffer = _factory.Create<IVimBuffer>();
_processor = new KeyProcessor(_buffer.Object);
}
[TearDown]
public void TearDown()
{
if (_keyboardId != IntPtr.Zero)
{
Assert.IsTrue(NativeMethods.UnloadKeyboardLayout(_keyboardId));
}
_keyboardId = IntPtr.Zero;
}
private KeyEventArgs CreateKeyEventArgs(
Key key,
ModifierKeys modKeys = ModifierKeys.None)
{
var device = new MockKeyboardDevice(InputManager.Current) { ModifierKeysImpl = modKeys };
return device.CreateKeyEventArgs(key, modKeys);
}
[Test]
[Description("Don't handle AltGR keys")]
public void KeyDown1()
{
Setup(NativeMethods.LanguagePortuguese);
var arg = CreateKeyEventArgs(Key.D8, ModifierKeys.Alt | ModifierKeys.Control);
_processor.KeyDown(arg);
Assert.IsFalse(arg.Handled);
}
[Test]
[Description("Don't handle non-input keys")]
public void KeyDown2()
{
foreach (var cur in new Key[] { Key.LeftAlt, Key.RightAlt, Key.LeftCtrl, Key.RightCtrl, Key.LeftShift, Key.RightShift })
{
var arg = CreateKeyEventArgs(cur);
_processor.KeyDown(arg);
Assert.IsFalse(arg.Handled);
}
}
[Test]
[Description("Don't handle raw characters here. Should be done in TextInput")]
public void KeyDown3()
{
for (var i = 0; i < 26; i++)
{
var key = (Key)((int)Key.A + i);
var arg = CreateKeyEventArgs(key);
_processor.KeyDown(arg);
Assert.IsFalse(arg.Handled);
}
}
[Test]
[Description("Do handle non printable characters here")]
public void KeyDown4()
{
_buffer.Setup(x => x.CanProcess(It.IsAny<KeyInput>())).Returns(true).Verifiable();
_buffer.Setup(x => x.Process(It.IsAny<KeyInput>())).Returns(true).Verifiable();
var array = new Key[] { Key.Enter, Key.Left, Key.Right, Key.Return };
foreach (var cur in array)
{
var arg = CreateKeyEventArgs(cur);
_processor.KeyDown(arg);
Assert.IsTrue(arg.Handled);
}
_factory.Verify();
}
[Test]
[Description("Do pass non-printable charcaters onto the IVimBuffer")]
public void KeyDown5()
{
_buffer.Setup(x => x.CanProcess(It.IsAny<KeyInput>())).Returns(false).Verifiable();
var array = new Key[] { Key.Enter, Key.Left, Key.Right, Key.Return };
foreach (var cur in array)
{
var arg = CreateKeyEventArgs(cur);
_processor.KeyDown(arg);
Assert.IsFalse(arg.Handled);
}
_factory.Verify();
}
[Test]
[Description("Do pass Control and Alt modified input onto the IVimBuffer")]
public void KeyDown6()
{
_buffer.Setup(x => x.CanProcess(It.IsAny<KeyInput>())).Returns(false).Verifiable();
for (var i = 0; i < 26; i++)
{
var key = (Key)((int)Key.A + i);
var arg = CreateKeyEventArgs(key, ModifierKeys.Alt);
_processor.KeyDown(arg);
Assert.IsFalse(arg.Handled);
arg = CreateKeyEventArgs(key, ModifierKeys.Control);
_processor.KeyDown(arg);
Assert.IsFalse(arg.Handled);
}
_factory.Verify();
}
[Test]
[Description("Do pass Control and Alt modified input onto the IVimBuffer")]
public void KeyDown7()
{
_buffer.Setup(x => x.CanProcess(It.IsAny<KeyInput>())).Returns(true).Verifiable();
_buffer.Setup(x => x.Process(It.IsAny<KeyInput>())).Returns(true).Verifiable();
for (var i = 0; i < 26; i++)
{
var key = (Key)((int)Key.A + i);
var arg = CreateKeyEventArgs(key, ModifierKeys.Alt);
_processor.KeyDown(arg);
Assert.IsTrue(arg.Handled);
arg = CreateKeyEventArgs(key, ModifierKeys.Control);
_processor.KeyDown(arg);
Assert.IsTrue(arg.Handled);
}
_factory.Verify();
}
}
}