tst_input_common.cpp
1 #include <QtTest/QtTest> 2 3 #include <gui/input.h> 4 5 class TestInputCommon : public QObject 6 { 7 Q_OBJECT 8 9 private slots: 10 // Keyboard Input 11 void LessThanKey() noexcept; 12 void ModifierOnlyKeyEventsIgnored() noexcept; 13 void VolumeKeysIgnored() noexcept; 14 void ShiftKeyEventWellFormed() noexcept; 15 void CapsLockIgnored() noexcept; 16 void AltGrAloneIgnored() noexcept; 17 void AltGrKeyEventWellFormed() noexcept; 18 void IgnoreHyperKey() noexcept; 19 20 // Mouse Input 21 void MouseLeftClick() noexcept; 22 void MouseRightClick() noexcept; 23 void MouseMiddleClick() noexcept; 24 void MouseX1Click() noexcept; 25 void MouseX2Click() noexcept; 26 }; 27 28 void TestInputCommon::LessThanKey() noexcept 29 { 30 // Issue#607: Shift is implied with "<", send "<lt>" instead. 31 QKeyEvent evIssue607{ QEvent::KeyPress, Qt::Key::Key_Less, Qt::KeyboardModifier::ShiftModifier, "<" }; 32 QCOMPARE(NeovimQt::Input::convertKey(evIssue607), QString{ "<lt>" }); 33 } 34 35 void TestInputCommon::ModifierOnlyKeyEventsIgnored() noexcept 36 { 37 QKeyEvent meta{ QEvent::KeyPress, Qt::Key_Meta, Qt::MetaModifier, {} }; 38 QCOMPARE(NeovimQt::Input::convertKey(meta), QString{}); 39 40 QKeyEvent ctrl{ QEvent::KeyPress, Qt::Key_Control, Qt::ControlModifier, {} }; 41 QCOMPARE(NeovimQt::Input::convertKey(ctrl), QString{}); 42 43 QKeyEvent alt{ QEvent::KeyPress, Qt::Key_Alt, Qt::AltModifier, {} }; 44 QCOMPARE(NeovimQt::Input::convertKey(alt), QString{}); 45 46 const QList<Qt::Key> modifierKeyList { 47 Qt::Key::Key_Alt, 48 Qt::Key::Key_AltGr, 49 Qt::Key::Key_Control, 50 Qt::Key::Key_Meta, 51 Qt::Key::Key_Shift, 52 Qt::Key::Key_Super_L, 53 Qt::Key::Key_Super_R, 54 }; 55 56 const QList<Qt::KeyboardModifier> keyboardModifierList { 57 Qt::KeyboardModifier::NoModifier, 58 Qt::KeyboardModifier::AltModifier, 59 Qt::KeyboardModifier::ControlModifier, 60 Qt::KeyboardModifier::MetaModifier, 61 Qt::KeyboardModifier::ShiftModifier, 62 Qt::KeyboardModifier::GroupSwitchModifier, 63 }; 64 65 for (const auto& key : modifierKeyList) { 66 for (const auto& mod : keyboardModifierList) { 67 QKeyEvent keyEvent{ QEvent::KeyPress, key, mod }; 68 QCOMPARE(NeovimQt::Input::convertKey(keyEvent), QString{}); 69 } 70 } 71 72 // Issue#344: <C-S-> appears as <C-Space> 73 QKeyEvent evIssue344{ QEvent::KeyPress, Qt::Key::Key_Control, Qt::KeyboardModifier::ShiftModifier}; 74 QCOMPARE(NeovimQt::Input::convertKey(evIssue344), QString{}); 75 76 // Issue#593: Pressing Control + Super inserts ^S 77 QKeyEvent evIssue593{ QEvent::KeyPress, Qt::Key::Key_Super_L, Qt::KeyboardModifier::ControlModifier}; 78 QCOMPARE(NeovimQt::Input::convertKey(evIssue593), QString{}); 79 } 80 81 void TestInputCommon::VolumeKeysIgnored() noexcept 82 { 83 const QList<Qt::Key> volumeKeysList { 84 Qt::Key::Key_VolumeDown, 85 Qt::Key::Key_VolumeMute, 86 Qt::Key::Key_VolumeUp, 87 }; 88 for (const auto &key : volumeKeysList) { 89 QKeyEvent keyEvent{ QEvent::KeyPress, key, Qt::KeyboardModifier::NoModifier }; 90 QCOMPARE(NeovimQt::Input::convertKey(keyEvent), QString{}); 91 } 92 } 93 94 void TestInputCommon::AltGrAloneIgnored() noexcept 95 { 96 // Issue#510: Pressing AltGr inserts weird char 97 QKeyEvent event{ QEvent::KeyPress, Qt::Key::Key_AltGr, Qt::KeyboardModifier::GroupSwitchModifier}; 98 QCOMPARE(NeovimQt::Input::convertKey(event), QString{}); 99 } 100 101 void TestInputCommon::AltGrKeyEventWellFormed() noexcept 102 { 103 QKeyEvent event{ QEvent::KeyPress, Qt::Key::Key_AsciiTilde, Qt::KeyboardModifier::GroupSwitchModifier}; 104 QCOMPARE(NeovimQt::Input::convertKey(event), QString{ "~" }); 105 } 106 107 void TestInputCommon::ShiftKeyEventWellFormed() noexcept 108 { 109 QKeyEvent evShiftBracket{ QEvent::KeyPress, Qt::Key_BraceRight, Qt::ShiftModifier, "}" }; 110 QCOMPARE(NeovimQt::Input::convertKey(evShiftBracket), QString{ "}" }); 111 112 QKeyEvent evShiftQuote{ QEvent::KeyPress, Qt::Key_QuoteDbl, Qt::ShiftModifier, "\"" }; 113 QCOMPARE(NeovimQt::Input::convertKey(evShiftQuote), QString{ "\"" }); 114 115 QKeyEvent evShiftColon{ QEvent::KeyPress, Qt::Key_Colon, Qt::ShiftModifier, ":" }; 116 QCOMPARE(NeovimQt::Input::convertKey(evShiftColon), QString{ ":" }); 117 118 QKeyEvent evShiftA{ QEvent::KeyPress, Qt::Key_A, Qt::ShiftModifier, "A" }; 119 QCOMPARE(NeovimQt::Input::convertKey(evShiftA), QString{ "A" }); 120 121 QKeyEvent evShiftB{ QEvent::KeyPress, Qt::Key_B, Qt::ShiftModifier, "B" }; 122 QCOMPARE(NeovimQt::Input::convertKey(evShiftB), QString{ "B" }); 123 124 QKeyEvent evShiftC{ QEvent::KeyPress, Qt::Key_C, Qt::ShiftModifier, "C" }; 125 QCOMPARE(NeovimQt::Input::convertKey(evShiftC), QString{ "C" }); 126 } 127 128 void TestInputCommon::CapsLockIgnored() noexcept 129 { 130 // Issue#199: Pressing Control + CapsLock inserts $ 131 QKeyEvent evCapsLock{ QEvent::KeyPress, Qt::Key_CapsLock, Qt::NoModifier}; 132 QCOMPARE(NeovimQt::Input::convertKey(evCapsLock), QString{ "" }); 133 134 QKeyEvent evCtrlCapsLock{ QEvent::KeyPress, Qt::Key_CapsLock, Qt::ControlModifier }; 135 QCOMPARE(NeovimQt::Input::convertKey(evCtrlCapsLock), QString{ "" }); 136 137 QKeyEvent evMetaCapsLock{ QEvent::KeyPress, Qt::Key_CapsLock, Qt::MetaModifier}; 138 QCOMPARE(NeovimQt::Input::convertKey(evMetaCapsLock), QString{ "" }); 139 } 140 141 void TestInputCommon::IgnoreHyperKey() noexcept 142 { 143 // Issue#1008: Hyper key should be ignored, but instead inserts "v" instead. 144 QKeyEvent evHyperL{ QEvent::KeyPress, Qt::Key_Hyper_L, Qt::NoModifier }; 145 QCOMPARE(NeovimQt::Input::convertKey(evHyperL), QString{}); 146 147 QKeyEvent evHyperR{ QEvent::KeyPress, Qt::Key_Hyper_R, Qt::NoModifier }; 148 QCOMPARE(NeovimQt::Input::convertKey(evHyperR), QString{}); 149 } 150 151 152 void TestInputCommon::MouseLeftClick() noexcept 153 { 154 QString leftClickPress{ NeovimQt::Input::convertMouse( 155 Qt::LeftButton, 156 QEvent::MouseButtonPress, 157 Qt::NoModifier, 158 { 1, 2 }, 159 1 /*clickCount*/) }; 160 QString leftClickRelease{ NeovimQt::Input::convertMouse( 161 Qt::LeftButton, 162 QEvent::MouseButtonRelease, 163 Qt::NoModifier, 164 { 1, 2 }, 165 1 /*clickCount*/) }; 166 167 QCOMPARE(leftClickPress, QString{ "<LeftMouse><1,2>" }); 168 QCOMPARE(leftClickRelease, QString{ "<LeftRelease><1,2>" }); 169 170 QString leftClickShiftPress{ NeovimQt::Input::convertMouse( 171 Qt::LeftButton, 172 QEvent::MouseButtonPress, 173 Qt::ShiftModifier, 174 { 3, 4 }, 175 1 /*clickCount*/) }; 176 QString leftClickShiftRelease{ NeovimQt::Input::convertMouse( 177 Qt::LeftButton, 178 QEvent::MouseButtonRelease, 179 Qt::ShiftModifier, 180 { 3, 4 }, 181 1 /*clickCount*/) }; 182 183 QCOMPARE(leftClickShiftPress, QString{ "<S-LeftMouse><3,4>" }); 184 QCOMPARE(leftClickShiftRelease, QString{ "<S-LeftRelease><3,4>" }); 185 186 QString leftDoubleClickPress{ NeovimQt::Input::convertMouse( 187 Qt::LeftButton, 188 QEvent::MouseButtonPress, 189 Qt::NoModifier, 190 { 5, 6 }, 191 2 /*clickCount*/) }; 192 193 QString leftDoubleClickRelease{ NeovimQt::Input::convertMouse( 194 Qt::LeftButton, 195 QEvent::MouseButtonRelease, 196 Qt::NoModifier, 197 { 5, 6 }, 198 2 /*clickCount*/) }; 199 200 QCOMPARE(leftDoubleClickPress, QString{ "<2-LeftMouse><5,6>" }); 201 QCOMPARE(leftDoubleClickRelease, QString{ "<2-LeftRelease><5,6>" }); 202 203 QString leftDoubleClickShiftPress{ NeovimQt::Input::convertMouse( 204 Qt::LeftButton, 205 QEvent::MouseButtonPress, 206 Qt::ShiftModifier, 207 { 7, 8 }, 208 2 /*clickCount*/) }; 209 210 QString leftDoubleClickShiftRelease{ NeovimQt::Input::convertMouse( 211 Qt::LeftButton, 212 QEvent::MouseButtonRelease, 213 Qt::ShiftModifier, 214 { 7, 8 }, 215 2 /*clickCount*/) }; 216 217 QCOMPARE(leftDoubleClickShiftPress, QString{ "<S-2-LeftMouse><7,8>" }); 218 QCOMPARE(leftDoubleClickShiftRelease, QString{ "<S-2-LeftRelease><7,8>" }); 219 } 220 221 void TestInputCommon::MouseRightClick() noexcept 222 { 223 QString rightClickPress{ NeovimQt::Input::convertMouse( 224 Qt::RightButton, 225 QEvent::MouseButtonPress, 226 Qt::NoModifier, 227 { 1, 2 }, 228 1 /*clickCount*/) }; 229 QString rightClickRelease{ NeovimQt::Input::convertMouse( 230 Qt::RightButton, 231 QEvent::MouseButtonRelease, 232 Qt::NoModifier, 233 { 1, 2 }, 234 1 /*clickCount*/) }; 235 236 QCOMPARE(rightClickPress, QString{ "<RightMouse><1,2>" }); 237 QCOMPARE(rightClickRelease, QString{ "<RightRelease><1,2>" }); 238 } 239 240 void TestInputCommon::MouseMiddleClick() noexcept 241 { 242 //Qt::MiddleButton 243 QString middleClickPress{ NeovimQt::Input::convertMouse( 244 Qt::MiddleButton, 245 QEvent::MouseButtonPress, 246 Qt::NoModifier, 247 { 1, 2 }, 248 1 /*clickCount*/) }; 249 QString middleClickRelease{ NeovimQt::Input::convertMouse( 250 Qt::MiddleButton, 251 QEvent::MouseButtonRelease, 252 Qt::NoModifier, 253 { 1, 2 }, 254 1 /*clickCount*/) }; 255 256 QCOMPARE(middleClickPress, QString{ "<MiddleMouse><1,2>" }); 257 QCOMPARE(middleClickRelease, QString{ "<MiddleRelease><1,2>" }); 258 } 259 260 void TestInputCommon::MouseX1Click() noexcept 261 { 262 QString x1Press{ NeovimQt::Input::convertMouse( 263 Qt::XButton1, 264 QEvent::MouseButtonPress, 265 Qt::NoModifier, 266 { 1, 2 }, 267 1 /*clickCount*/) }; 268 QString x1Release{ NeovimQt::Input::convertMouse( 269 Qt::XButton1, 270 QEvent::MouseButtonRelease, 271 Qt::NoModifier, 272 { 1, 2 }, 273 1 /*clickCount*/) }; 274 275 QCOMPARE(x1Press, QString{ "<X1Mouse><1,2>" }); 276 QCOMPARE(x1Release, QString{ "<X1Release><1,2>" }); 277 } 278 279 void TestInputCommon::MouseX2Click() noexcept 280 { 281 QString x2Press{ NeovimQt::Input::convertMouse( 282 Qt::XButton2, 283 QEvent::MouseButtonPress, 284 Qt::NoModifier, 285 { 1, 2 }, 286 1 /*clickCount*/) }; 287 QString x2Release{ NeovimQt::Input::convertMouse( 288 Qt::XButton2, 289 QEvent::MouseButtonRelease, 290 Qt::NoModifier, 291 { 1, 2 }, 292 1 /*clickCount*/) }; 293 294 QCOMPARE(x2Press, QString{ "<X2Mouse><1,2>" }); 295 QCOMPARE(x2Release, QString{ "<X2Release><1,2>" }); 296 } 297 298 #include "tst_input_common.moc" 299 QTEST_MAIN(TestInputCommon)