From 606dbc6eb42f0ca5301335b7f3495a4f64248e21 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Sun, 28 Oct 2012 15:31:57 +0100 Subject: [PATCH] Add infrastructure for auto-type tests. --- src/autotype/AutoType.cpp | 19 +++- src/autotype/AutoType.h | 3 +- src/autotype/CMakeLists.txt | 4 + src/autotype/test/AutoTypeTest.cpp | 111 ++++++++++++++++++++++ src/autotype/test/AutoTypeTest.h | 74 +++++++++++++++ src/autotype/test/AutoTypeTestInterface.h | 36 +++++++ src/autotype/test/CMakeLists.txt | 12 +++ src/core/FilePath.cpp | 5 + tests/CMakeLists.txt | 3 + 9 files changed, 264 insertions(+), 3 deletions(-) create mode 100644 src/autotype/test/AutoTypeTest.cpp create mode 100644 src/autotype/test/AutoTypeTest.h create mode 100644 src/autotype/test/AutoTypeTestInterface.h create mode 100644 src/autotype/test/CMakeLists.txt diff --git a/src/autotype/AutoType.cpp b/src/autotype/AutoType.cpp index 5cf825c22..fa9b5abb9 100644 --- a/src/autotype/AutoType.cpp +++ b/src/autotype/AutoType.cpp @@ -32,7 +32,7 @@ AutoType* AutoType::m_instance = Q_NULLPTR; -AutoType::AutoType(QObject* parent) +AutoType::AutoType(QObject* parent, bool test) : QObject(parent) , m_inAutoType(false) , m_pluginLoader(new QPluginLoader(this)) @@ -42,7 +42,15 @@ AutoType::AutoType(QObject* parent) // prevent crash when the plugin has unresolved symbols m_pluginLoader->setLoadHints(QLibrary::ResolveAllSymbolsHint); - QString pluginPath = filePath()->pluginPath("keepassx-autotype-" + Tools::platform()); + QString pluginName = "keepassx-autotype-"; + if (!test) { + pluginName += Tools::platform(); + } + else { + pluginName += "test"; + } + + QString pluginPath = filePath()->pluginPath(pluginName); if (!pluginPath.isEmpty()) { loadPlugin(pluginPath); @@ -81,6 +89,13 @@ AutoType* AutoType::instance() return m_instance; } +void AutoType::createTestInstance() +{ + Q_ASSERT(!m_instance); + + m_instance = new AutoType(qApp, true); +} + QStringList AutoType::windowTitles() { if (!m_plugin) { diff --git a/src/autotype/AutoType.h b/src/autotype/AutoType.h index 81d625d83..51b6a540a 100644 --- a/src/autotype/AutoType.h +++ b/src/autotype/AutoType.h @@ -48,6 +48,7 @@ public: } static AutoType* instance(); + static void createTestInstance(); public Q_SLOTS: void performGlobalAutoType(const QList& dbList); @@ -60,7 +61,7 @@ private Q_SLOTS: void resetInAutoType(); private: - explicit AutoType(QObject* parent = Q_NULLPTR); + explicit AutoType(QObject* parent = Q_NULLPTR, bool test = false); ~AutoType(); void loadPlugin(const QString& pluginPath); bool parseActions(const QString& sequence, const Entry* entry, QList& actions); diff --git a/src/autotype/CMakeLists.txt b/src/autotype/CMakeLists.txt index 767c8ab49..0e4a69ebc 100644 --- a/src/autotype/CMakeLists.txt +++ b/src/autotype/CMakeLists.txt @@ -5,3 +5,7 @@ if(Q_WS_X11) add_subdirectory(x11) endif() endif() + +if(WITH_TESTS) + add_subdirectory(test) +endif() diff --git a/src/autotype/test/AutoTypeTest.cpp b/src/autotype/test/AutoTypeTest.cpp new file mode 100644 index 000000000..959b03268 --- /dev/null +++ b/src/autotype/test/AutoTypeTest.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2012 Felix Geyer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "AutoTypeTest.h" + +QStringList AutoTypePlatformTest::windowTitles() +{ + return QStringList(); +} + +WId AutoTypePlatformTest::activeWindow() +{ + return 0; +} + +QString AutoTypePlatformTest::activeWindowTitle() +{ + return m_activeWindowTitle; +} + +bool AutoTypePlatformTest::registerGlobalShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers) +{ + Q_UNUSED(key); + Q_UNUSED(modifiers); + + return true; +} + +void AutoTypePlatformTest::unregisterGlobalShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers) +{ + Q_UNUSED(key); + Q_UNUSED(modifiers); +} + +int AutoTypePlatformTest::platformEventFilter(void* event) +{ + Q_UNUSED(event); + + return -1; +} + +AutoTypeExecutor* AutoTypePlatformTest::createExecutor() +{ + return new AutoTypeExecturorTest(this); +} + +void AutoTypePlatformTest::setActiveWindowTitle(const QString& title) +{ + m_activeWindowTitle = title; +} + +QString AutoTypePlatformTest::actionChars() +{ + return m_actionChars; +} + +QList AutoTypePlatformTest::actionList() +{ + return m_actionList; +} + +void AutoTypePlatformTest::clearActions() +{ + qDeleteAll(m_actionList); + m_actionList.clear(); + + m_actionChars.clear(); +} + +void AutoTypePlatformTest::addActionChar(AutoTypeChar* action) +{ + m_actionList.append(action->clone()); + m_actionChars += action->character; +} + +void AutoTypePlatformTest::addActionKey(AutoTypeKey* action) +{ + m_actionList.append(action->clone()); +} + + +AutoTypeExecturorTest::AutoTypeExecturorTest(AutoTypePlatformTest* platform) + : m_platform(platform) +{ +} + +void AutoTypeExecturorTest::execChar(AutoTypeChar* action) +{ + m_platform->addActionChar(action); +} + +void AutoTypeExecturorTest::execKey(AutoTypeKey* action) +{ + m_platform->addActionKey(action); +} + +Q_EXPORT_PLUGIN2(keepassx-autotype-test, AutoTypePlatformTest) diff --git a/src/autotype/test/AutoTypeTest.h b/src/autotype/test/AutoTypeTest.h new file mode 100644 index 000000000..55fa15982 --- /dev/null +++ b/src/autotype/test/AutoTypeTest.h @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2012 Felix Geyer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef KEEPASSX_AUTOTYPETEST_H +#define KEEPASSX_AUTOTYPETEST_H + +#include + +#include "autotype/AutoTypePlatformPlugin.h" +#include "autotype/AutoTypeAction.h" +#include "autotype/test/AutoTypeTestInterface.h" +#include "core/Global.h" + +class AutoTypePlatformTest : public QObject, + public AutoTypePlatformInterface, + public AutoTypeTestInterface +{ + Q_OBJECT + Q_INTERFACES(AutoTypePlatformInterface AutoTypeTestInterface) + +public: + QStringList windowTitles(); + WId activeWindow(); + QString activeWindowTitle(); + bool registerGlobalShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers); + void unregisterGlobalShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers); + int platformEventFilter(void* event); + AutoTypeExecutor* createExecutor(); + + void setActiveWindowTitle(const QString& title); + + QString actionChars(); + QList actionList(); + void clearActions(); + + void addActionChar(AutoTypeChar* action); + void addActionKey(AutoTypeKey* action); + +Q_SIGNALS: + void globalShortcutTriggered(); + +private: + QString m_activeWindowTitle; + QList m_actionList; + QString m_actionChars; +}; + +class AutoTypeExecturorTest : public AutoTypeExecutor +{ +public: + explicit AutoTypeExecturorTest(AutoTypePlatformTest* platform); + + void execChar(AutoTypeChar* action); + void execKey(AutoTypeKey* action); + +private: + AutoTypePlatformTest* const m_platform; +}; + +#endif // KEEPASSX_AUTOTYPETEST_H diff --git a/src/autotype/test/AutoTypeTestInterface.h b/src/autotype/test/AutoTypeTestInterface.h new file mode 100644 index 000000000..b2c6f1b46 --- /dev/null +++ b/src/autotype/test/AutoTypeTestInterface.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2012 Felix Geyer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef KEEPASSX_AUTOTYPETESTINTERFACE_H +#define KEEPASSX_AUTOTYPETESTINTERFACE_H + +#include "autotype/AutoTypeAction.h" + +class AutoTypeTestInterface +{ +public: + virtual ~AutoTypeTestInterface() {} + virtual void setActiveWindowTitle(const QString& title) = 0; + + virtual QString actionChars() = 0; + virtual QList actionList() = 0; + virtual void clearActions() = 0; +}; + +Q_DECLARE_INTERFACE(AutoTypeTestInterface, "org.keepassx.AutoTypeTestInterface/1") + +#endif // KEEPASSX_AUTOTYPETESTINTERFACE_H diff --git a/src/autotype/test/CMakeLists.txt b/src/autotype/test/CMakeLists.txt new file mode 100644 index 000000000..31b1df7e7 --- /dev/null +++ b/src/autotype/test/CMakeLists.txt @@ -0,0 +1,12 @@ +set(autotype_test_SOURCES + AutoTypeTest.cpp +) + +set(autotype_test_MOC + AutoTypeTest.h +) + +qt4_wrap_cpp(autotype_test_SOURCES ${autotype_test_MOC}) + +add_library(keepassx-autotype-test MODULE ${autotype_test_SOURCES}) +target_link_libraries(keepassx-autotype-test ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY}) diff --git a/src/core/FilePath.cpp b/src/core/FilePath.cpp index fd82de372..9a5bdfc69 100644 --- a/src/core/FilePath.cpp +++ b/src/core/FilePath.cpp @@ -38,10 +38,15 @@ QString FilePath::dataPath(const QString& name) QString FilePath::pluginPath(const QString& name) { QStringList pluginPaths; + QDir buildDir(QCoreApplication::applicationDirPath() + "/autotype"); Q_FOREACH (const QString& dir, buildDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) { pluginPaths << QCoreApplication::applicationDirPath() + "/autotype/" + dir; } + + // for TestAutoType + pluginPaths << QCoreApplication::applicationDirPath() + "/../src/autotype/test"; + pluginPaths << QCoreApplication::applicationDirPath(); pluginPaths << QCoreApplication::applicationDirPath() + "/../lib/keepassx"; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8d9c5bdd1..b2e140cd5 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -152,6 +152,9 @@ add_unit_test(NAME testqsavefile SOURCES TestQSaveFile.cpp MOCS TestQSaveFile.h add_unit_test(NAME testwildcardmatcher SOURCES TestWildcardMatcher.cpp MOCS TestWildcardMatcher.h LIBS ${TEST_LIBRARIES}) +add_unit_test(NAME testautotype SOURCES TestAutoType.cpp MOCS TestAutoType.h + LIBS ${TEST_LIBRARIES}) + if(WITH_GUI_TESTS) add_subdirectory(gui) endif(WITH_GUI_TESTS)