Merge pull request #1017 from p12tic/gui-hotkey-tests

gui/test: More tests for Hotkey serialization
This commit is contained in:
Povilas Kanapickas 2021-01-10 15:40:01 +02:00 committed by GitHub
commit 618d28d392
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 133 additions and 59 deletions

View File

@ -20,6 +20,7 @@
#include "Utils.h" #include "Utils.h"
#include <QtCore/QSettings> #include <QtCore/QSettings>
#include <QtCore/QTextStream>
struct TestAction struct TestAction
{ {
@ -96,65 +97,82 @@ struct TestHotKey
std::vector<TestAction> actions; std::vector<TestAction> actions;
}; };
Action createAction(const TestAction& test_action) namespace {
{
Action action;
action.setType(test_action.type);
switch (test_action.type) { Action createAction(const TestAction& test_action)
case Action::keyDown: {
case Action::keyUp: Action action;
case Action::keystroke: { action.setType(test_action.type);
KeySequence sequence;
for (auto key : test_action.keys) { switch (test_action.type) {
sequence.appendKey(key.key, key.modifier); case Action::keyDown:
case Action::keyUp:
case Action::keystroke: {
KeySequence sequence;
for (auto key : test_action.keys) {
sequence.appendKey(key.key, key.modifier);
}
action.setKeySequence(sequence);
for (const auto& type_screen_name : test_action.type_screen_names) {
action.appendTypeScreenName(QString::fromStdString(type_screen_name));
}
break;
} }
action.setKeySequence(sequence); case Action::switchToScreen:
for (const auto& type_screen_name : test_action.type_screen_names) { action.setSwitchScreenName(QString::fromStdString(test_action.screen_name));
action.appendTypeScreenName(QString::fromStdString(type_screen_name)); break;
} case Action::toggleScreen:
break; break;
case Action::switchInDirection:
action.setSwitchDirection(test_action.switch_direction);
break;
case Action::lockCursorToScreen:
action.setLockCursorMode(test_action.lock_cursor_mode);
break;
} }
case Action::switchToScreen: return action;
action.setSwitchScreenName(QString::fromStdString(test_action.screen_name));
break;
case Action::toggleScreen:
break;
case Action::switchInDirection:
action.setSwitchDirection(test_action.switch_direction);
break;
case Action::lockCursorToScreen:
action.setLockCursorMode(test_action.lock_cursor_mode);
break;
} }
return action;
}
void doHotkeyLoadSaveTest(const TestHotKey& test_hotkey, QSettings::Format format) Hotkey createHotkey(const TestHotKey& test_hotkey)
{
Hotkey hotkey;
KeySequence sequence;
for (auto key : test_hotkey.keys) {
sequence.appendKey(key.key, key.modifier);
}
hotkey.setKeySequence(sequence);
for (auto action : test_hotkey.actions) {
hotkey.appendAction(createAction(action));
}
return hotkey;
}
std::string hotkeyToStringViaTextStream(const Hotkey& hotkey)
{
QString result;
QTextStream stream{&result};
stream << hotkey;
return result.toStdString();
}
} // namespace
void doHotkeyLoadSaveTest(const TestHotKey& test_hotkey)
{ {
auto filename = getTemporaryFilename(); auto filename = getTemporaryFilename();
Hotkey hotkey_before, hotkey_after; Hotkey hotkey_before, hotkey_after;
{ {
QSettings settings(filename, format); QSettings settings(filename, QSettings::NativeFormat);
hotkey_before = createHotkey(test_hotkey);
KeySequence sequence;
for (auto key : test_hotkey.keys) {
sequence.appendKey(key.key, key.modifier);
}
hotkey_before.setKeySequence(sequence);
for (auto action : test_hotkey.actions) {
hotkey_before.appendAction(createAction(action));
}
settings.beginGroup("test"); settings.beginGroup("test");
hotkey_before.saveSettings(settings); hotkey_before.saveSettings(settings);
settings.endGroup(); settings.endGroup();
} }
{ {
QSettings settings(filename, format); QSettings settings(filename, QSettings::NativeFormat);
settings.beginGroup("test"); settings.beginGroup("test");
hotkey_after.loadSettings(settings); hotkey_after.loadSettings(settings);
@ -189,15 +207,13 @@ void doHotkeyLoadSaveTest(const TestHotKey& test_hotkey, QSettings::Format forma
TEST(HotkeyLoadSaveTests, Empty) TEST(HotkeyLoadSaveTests, Empty)
{ {
TestHotKey hotkey; TestHotKey hotkey;
doHotkeyLoadSaveTest(hotkey, QSettings::NativeFormat); doHotkeyLoadSaveTest(hotkey);
doHotkeyLoadSaveTest(hotkey, QSettings::IniFormat);
} }
TEST(HotkeyLoadSaveTests, KeysNoActions) TEST(HotkeyLoadSaveTests, KeysNoActions)
{ {
TestHotKey hotkey = {{{Qt::Key_A, Qt::NoModifier}, {Qt::Key_B, Qt::NoModifier}}, {}}; TestHotKey hotkey = {{{Qt::Key_A, Qt::NoModifier}, {Qt::Key_B, Qt::NoModifier}}, {}};
doHotkeyLoadSaveTest(hotkey, QSettings::NativeFormat); doHotkeyLoadSaveTest(hotkey);
doHotkeyLoadSaveTest(hotkey, QSettings::IniFormat);
} }
TEST(HotkeyLoadSaveTests, CommaKeyNoActions) TEST(HotkeyLoadSaveTests, CommaKeyNoActions)
@ -208,8 +224,7 @@ TEST(HotkeyLoadSaveTests, CommaKeyNoActions)
{Qt::Key_Comma, Qt::NoModifier}, {Qt::Key_Comma, Qt::NoModifier},
{Qt::Key_B, Qt::NoModifier} {Qt::Key_B, Qt::NoModifier}
}, {}}; }, {}};
doHotkeyLoadSaveTest(hotkey, QSettings::NativeFormat); doHotkeyLoadSaveTest(hotkey);
doHotkeyLoadSaveTest(hotkey, QSettings::IniFormat);
} }
TEST(HotkeyLoadSaveTests, KeysSingleAction) TEST(HotkeyLoadSaveTests, KeysSingleAction)
@ -223,8 +238,7 @@ TEST(HotkeyLoadSaveTests, KeysSingleAction)
TestAction::createKeyDown({{Qt::Key_Z, Qt::NoModifier}}) TestAction::createKeyDown({{Qt::Key_Z, Qt::NoModifier}})
} }
}; };
doHotkeyLoadSaveTest(hotkey, QSettings::NativeFormat); doHotkeyLoadSaveTest(hotkey);
doHotkeyLoadSaveTest(hotkey, QSettings::IniFormat);
} }
TEST(HotkeyLoadSaveTests, KeysMultipleAction) TEST(HotkeyLoadSaveTests, KeysMultipleAction)
@ -239,6 +253,68 @@ TEST(HotkeyLoadSaveTests, KeysMultipleAction)
TestAction::createSwitchToScreen("test_screen") TestAction::createSwitchToScreen("test_screen")
} }
}; };
doHotkeyLoadSaveTest(hotkey, QSettings::NativeFormat); doHotkeyLoadSaveTest(hotkey);
doHotkeyLoadSaveTest(hotkey, QSettings::IniFormat); }
TEST(HotkeyToTexStreamTests, Empty)
{
TestHotKey hotkey;
ASSERT_EQ(hotkeyToStringViaTextStream(createHotkey(hotkey)), "");
}
TEST(HotkeyToTexStreamTests, KeysNoActions)
{
TestHotKey hotkey = {
{
{Qt::Key_A, Qt::NoModifier},
{Qt::Key_B, Qt::NoModifier}
},
{}
};
ASSERT_EQ(hotkeyToStringViaTextStream(createHotkey(hotkey)), "");
}
TEST(HotkeyToTexStreamTests, KeysSingleAction)
{
TestHotKey hotkey = {
{
{Qt::Key_A, Qt::NoModifier},
{Qt::Key_B, Qt::NoModifier}
},
{}
};
ASSERT_EQ(hotkeyToStringViaTextStream(createHotkey(hotkey)), "");
}
TEST(HotkeyToTexStreamTests, KeysCommaSingleAction)
{
TestHotKey hotkey = {
{
{Qt::Key_A, Qt::NoModifier},
{Qt::Key_Comma, Qt::NoModifier},
{Qt::Key_B, Qt::NoModifier}
},
{
TestAction::createKeyDown({{Qt::Key_Z, Qt::NoModifier}})
}
};
ASSERT_EQ(hotkeyToStringViaTextStream(createHotkey(hotkey)),
"\tkeystroke(a+,+b) = keyDown(z,*)\n");
}
TEST(HotkeyToTexStreamTests, KeysMultipleAction)
{
TestHotKey hotkey = {
{
{Qt::Key_A, Qt::NoModifier},
{Qt::Key_B, Qt::NoModifier}
},
{
TestAction::createKeyDown({{Qt::Key_Z, Qt::NoModifier}}),
TestAction::createSwitchToScreen("test_screen")
}
};
ASSERT_EQ(hotkeyToStringViaTextStream(createHotkey(hotkey)),
"\tkeystroke(a+b) = keyDown(z,*)\n\tkeystroke(a+b) = switchToScreen(test_screen)\n");
} }

View File

@ -86,17 +86,16 @@ namespace {
} // namespace } // namespace
class KeySequenceLoadSaveTestFixture : class KeySequenceLoadSaveTestFixture :
public ::testing::TestWithParam<std::tr1::tuple<Qt::Key, QSettings::Format>> {}; public ::testing::TestWithParam<Qt::Key> {};
TEST_P(KeySequenceLoadSaveTestFixture, SupportsSpecialSymbols) TEST_P(KeySequenceLoadSaveTestFixture, SupportsSpecialSymbols)
{ {
int key = std::tr1::get<0>(GetParam()); int key = GetParam();
QSettings::Format format = std::tr1::get<1>(GetParam());
auto filename = getTemporaryFilename(); auto filename = getTemporaryFilename();
{ {
QSettings settings(filename, format); QSettings settings(filename, QSettings::NativeFormat);
KeySequence sequence; KeySequence sequence;
sequence.appendKey(key, 0); sequence.appendKey(key, 0);
@ -106,7 +105,7 @@ TEST_P(KeySequenceLoadSaveTestFixture, SupportsSpecialSymbols)
settings.endGroup(); settings.endGroup();
} }
{ {
QSettings settings(filename, format); QSettings settings(filename, QSettings::NativeFormat);
KeySequence sequence; KeySequence sequence;
settings.beginGroup("test"); settings.beginGroup("test");
@ -125,8 +124,7 @@ TEST_P(KeySequenceLoadSaveTestFixture, SupportsSpecialSymbols)
INSTANTIATE_TEST_CASE_P( INSTANTIATE_TEST_CASE_P(
KeySequenceLoadSaveTests, KeySequenceLoadSaveTests,
KeySequenceLoadSaveTestFixture, KeySequenceLoadSaveTestFixture,
::testing::Combine(::testing::ValuesIn(s_key_sequence_test_keys), ::testing::ValuesIn(s_key_sequence_test_keys));
::testing::Values(QSettings::NativeFormat, QSettings::IniFormat)));
TEST(KeySequenceTests, ToString) TEST(KeySequenceTests, ToString)
{ {