tst_input_mac.cpp
1 #include <QtTest/QtTest> 2 3 #include <gui/input.h> 4 #include "tst_input.h" 5 6 class TestInputMac : public QObject 7 { 8 Q_OBJECT 9 10 private slots: 11 void AltSpecialCharacters() noexcept; 12 void LessThanModifierKeys() noexcept; 13 void SpecialKeys() noexcept; 14 void KeyboardLayoutUnicodeHexInput() noexcept; 15 void CtrlCaretWellFormed() noexcept; 16 void ShiftModifierLetter() noexcept; 17 void GermanKeyboardLayout() noexcept; 18 }; 19 20 void TestInputMac::AltSpecialCharacters() noexcept 21 { 22 // Issue#510: MacOS Alt special key input does not work. 23 QKeyEvent evAltA{ QEvent::KeyPress, Qt::Key_A, Qt::AltModifier, "å" }; 24 QCOMPARE(NeovimQt::Input::convertKey(evAltA), QString{ "å" }); 25 26 QKeyEvent evShiftAltA{ QEvent::KeyPress, Qt::Key_A, Qt::ShiftModifier | Qt::AltModifier, "Å" }; 27 QCOMPARE(NeovimQt::Input::convertKey(evShiftAltA), QString{ "Å" }); 28 29 QKeyEvent evShiftAltL{ QEvent::KeyPress, Qt::Key_L, Qt::ShiftModifier | Qt::AltModifier, "Ò" }; 30 QCOMPARE(NeovimQt::Input::convertKey(evShiftAltL), QString{ "Ò" }); 31 } 32 33 void TestInputMac::LessThanModifierKeys() noexcept 34 { 35 QKeyEvent evLessThanControl{ QEvent::KeyPress, Qt::Key::Key_Less, Qt::ShiftModifier | Qt::ControlModifier, "<" }; 36 QCOMPARE(NeovimQt::Input::convertKey(evLessThanControl), QString{ "<D-lt>" }); 37 38 QKeyEvent evLessThanAlt{ QEvent::KeyPress, Qt::Key::Key_Less, Qt::ShiftModifier | Qt::AltModifier, "<" }; 39 QCOMPARE(NeovimQt::Input::convertKey(evLessThanAlt), QString{ "<A-lt>" }); 40 41 QKeyEvent evLessThanMeta{ QEvent::KeyPress, Qt::Key::Key_Less, Qt::ShiftModifier | Qt::MetaModifier, "<" }; 42 QCOMPARE(NeovimQt::Input::convertKey(evLessThanMeta), QString{ "<C-lt>" }); 43 } 44 45 void TestInputMac::SpecialKeys() noexcept 46 { 47 const QList<int> specialKeys{ NeovimQt::Input::GetSpecialKeysMap().keys() }; 48 49 for (const auto k : specialKeys) { 50 // On Mac Meta is the Control key, treated as C-. 51 QList<InputTest> keyEventList{ 52 { QEvent::KeyPress, k, Qt::NoModifier, "<%1>" }, 53 { QEvent::KeyPress, k, Qt::ControlModifier, "<D-%1>" }, 54 { QEvent::KeyPress, k, Qt::AltModifier, "<A-%1>" }, 55 { QEvent::KeyPress, k, Qt::MetaModifier, "<C-%1>" }, 56 }; 57 58 for (const auto& keyTest : keyEventList) { 59 auto event = QKeyEvent(keyTest.event_type, keyTest.key, keyTest.modifiers); 60 QCOMPARE(NeovimQt::Input::convertKey(event), 61 keyTest.expected_input.arg(NeovimQt::Input::GetSpecialKeysMap().value(k))); 62 } 63 } 64 } 65 66 void TestInputMac::KeyboardLayoutUnicodeHexInput() noexcept 67 { 68 // Issue#579: Cannot map <A-...> on MacOS 69 QKeyEvent evAltA{ QEvent::KeyPress, Qt::Key_A, Qt::AltModifier }; 70 QCOMPARE(NeovimQt::Input::convertKey(evAltA), QString{ "<A-a>" }); 71 72 QKeyEvent evAltShiftA{ QEvent::KeyPress, Qt::Key_A, Qt::AltModifier | Qt::ShiftModifier }; 73 QCOMPARE(NeovimQt::Input::convertKey(evAltShiftA), QString{ "<A-A>" }); 74 75 QKeyEvent evCtrlAltA{ QEvent::KeyPress, Qt::Key_A, Qt::MetaModifier | Qt::AltModifier }; 76 QCOMPARE(NeovimQt::Input::convertKey(evCtrlAltA), QString{ "<C-A-a>" }); 77 78 QKeyEvent evCtrlAltShiftA{ QEvent::KeyPress, Qt::Key_A, 79 Qt::MetaModifier | Qt::AltModifier | Qt::ShiftModifier }; 80 QCOMPARE(NeovimQt::Input::convertKey(evCtrlAltShiftA), QString{ "<C-S-A-A>" }); 81 } 82 83 void TestInputMac::CtrlCaretWellFormed() noexcept 84 { 85 QKeyEvent evCtrl6{ QEvent::KeyPress, Qt::Key_6, Qt::MetaModifier }; 86 QCOMPARE(NeovimQt::Input::convertKey(evCtrl6), QString{ "<C-^>" }); 87 88 QKeyEvent evCtrlShift6{ QEvent::KeyPress, Qt::Key_AsciiCircum, Qt::MetaModifier | Qt::ShiftModifier }; 89 QCOMPARE(NeovimQt::Input::convertKey(evCtrlShift6), QString{ "<C-^>" }); 90 91 QKeyEvent evCtrlShiftMeta6{ QEvent::KeyPress, Qt::Key_AsciiCircum, 92 Qt::MetaModifier | Qt::ShiftModifier | Qt::ControlModifier }; 93 QCOMPARE(NeovimQt::Input::convertKey(evCtrlShiftMeta6), QString{ "<C-^>" }); 94 } 95 96 void TestInputMac::ShiftModifierLetter() noexcept 97 { 98 // Issue#817: Shift should be sent if modifier keys are present 99 // For example, Ctrl + Shift + A is <C-S-A> and not <C-A> 100 101 // CTRL + B 102 QKeyEvent evCtrlB{ QEvent::KeyPress, Qt::Key_B, Qt::MetaModifier }; 103 QCOMPARE(NeovimQt::Input::convertKey(evCtrlB), QString{ "<C-b>" }); 104 105 // CTRL + SHIFT + B 106 QKeyEvent evCtrlShiftB{ QEvent::KeyPress, Qt::Key_B, Qt::MetaModifier | Qt::ShiftModifier }; 107 QCOMPARE(NeovimQt::Input::convertKey(evCtrlShiftB), QString{ "<C-S-B>" }); 108 } 109 110 void TestInputMac::GermanKeyboardLayout() noexcept 111 { 112 QKeyEvent evOption5{ QEvent::KeyPress, Qt::Key_5, Qt::AltModifier, "[" }; 113 QCOMPARE(NeovimQt::Input::convertKey(evOption5), QString{ "[" }); 114 115 QKeyEvent evOption6{ QEvent::KeyPress, Qt::Key_6, Qt::AltModifier, "]" }; 116 QCOMPARE(NeovimQt::Input::convertKey(evOption6), QString{ "]" }); 117 118 QKeyEvent evOption7{ QEvent::KeyPress, Qt::Key_7, Qt::AltModifier, "|" }; 119 QCOMPARE(NeovimQt::Input::convertKey(evOption7), QString{ "|" }); 120 121 QKeyEvent evOption8{ QEvent::KeyPress, Qt::Key_8, Qt::AltModifier, "{" }; 122 QCOMPARE(NeovimQt::Input::convertKey(evOption8), QString{ "{" }); 123 124 QKeyEvent evOption9{ QEvent::KeyPress, Qt::Key_9, Qt::AltModifier, "}" }; 125 QCOMPARE(NeovimQt::Input::convertKey(evOption9), QString{ "}" }); 126 127 QKeyEvent evOptionTilde{ QEvent::KeyPress, Qt::Key_N, Qt::AltModifier, "~" }; 128 QCOMPARE(NeovimQt::Input::convertKey(evOptionTilde), QString{ "~" }); 129 130 QKeyEvent evOptionAtSign{ QEvent::KeyPress, Qt::Key_L, Qt::AltModifier, "@" }; 131 QCOMPARE(NeovimQt::Input::convertKey(evOptionAtSign), QString{ "@" }); 132 } 133 134 #include "tst_input_mac.moc" 135 QTEST_MAIN(TestInputMac)