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,8 +97,10 @@ struct TestHotKey
std::vector<TestAction> actions; std::vector<TestAction> actions;
}; };
Action createAction(const TestAction& test_action) namespace {
{
Action createAction(const TestAction& test_action)
{
Action action; Action action;
action.setType(test_action.type); action.setType(test_action.type);
@ -128,33 +131,48 @@ Action createAction(const TestAction& test_action)
break; break;
} }
return action; 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)
{ {