Add infrastructure for auto-type tests.

This commit is contained in:
Felix Geyer 2012-10-28 15:31:57 +01:00
parent f1719cfc5f
commit 606dbc6eb4
9 changed files with 264 additions and 3 deletions

View File

@ -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) {

View File

@ -48,6 +48,7 @@ public:
}
static AutoType* instance();
static void createTestInstance();
public Q_SLOTS:
void performGlobalAutoType(const QList<Database*>& 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<AutoTypeAction*>& actions);

View File

@ -5,3 +5,7 @@ if(Q_WS_X11)
add_subdirectory(x11)
endif()
endif()
if(WITH_TESTS)
add_subdirectory(test)
endif()

View File

@ -0,0 +1,111 @@
/*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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<AutoTypeAction*> 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)

View File

@ -0,0 +1,74 @@
/*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef KEEPASSX_AUTOTYPETEST_H
#define KEEPASSX_AUTOTYPETEST_H
#include <QtCore/QtPlugin>
#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<AutoTypeAction*> actionList();
void clearActions();
void addActionChar(AutoTypeChar* action);
void addActionKey(AutoTypeKey* action);
Q_SIGNALS:
void globalShortcutTriggered();
private:
QString m_activeWindowTitle;
QList<AutoTypeAction*> 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

View File

@ -0,0 +1,36 @@
/*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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<AutoTypeAction*> actionList() = 0;
virtual void clearActions() = 0;
};
Q_DECLARE_INTERFACE(AutoTypeTestInterface, "org.keepassx.AutoTypeTestInterface/1")
#endif // KEEPASSX_AUTOTYPETESTINTERFACE_H

View File

@ -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})

View File

@ -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";

View File

@ -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)