Skip to content

Commit

Permalink
unit-test: Introduce a cpp file for testbase.h
Browse files Browse the repository at this point in the history
So we don't recompile everything when writing code in testbase
  • Loading branch information
iamsergio committed Jan 28, 2015
1 parent 4e099aa commit c6bd092
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 153 deletions.
6 changes: 5 additions & 1 deletion tests/quick/testui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
*/

#include "testui.h"

#include "quickview.h"
#include "storage.h"
#include "settings.h"
#include "controller.h"
#include <QQuickItem>
#include <QtTest/QtTest>

TestUI::TestUI() : TestBase()
{
Expand Down
173 changes: 173 additions & 0 deletions tests/testbase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*
This file is part of Flow.
Copyright (C) 2014-2015 Sérgio Martins <[email protected]>
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 of the License, or
(at your option) any later version.
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 "testbase.h"

#include "signalspy.h"
#include "modelsignalspy.h"
#include "storage.h"
#include "kernel.h"
#include "runtimeconfiguration.h"
#include "tag.h"
#include "task.h"
#include "webdavsyncer.h"
#include "controller.h"
#include "tagref.h"
#include "settings.h"
#include "quickview.h"
#include <QtTest/QtTest>
#include <QQuickItem>

TestBase::TestBase() : m_view(nullptr)
{
RuntimeConfiguration config;
config.setDataFileName("data.dat");
config.setPluginsSupported(false);
config.setSettings(new Settings("unit-test-settings.ini"));
config.setSaveEnabled(false);
config.setWebDAVFileName("unit-test-flow.dat");
m_kernel = new Kernel(config, this);
m_storage = m_kernel->storage();
m_controller = m_kernel->controller();
m_settings = m_kernel->settings();

if (Storage::storageCount != 1) {
qWarning() << "Storage count is " << Storage::storageCount;
Q_ASSERT(false);
}
}

TestBase::~TestBase()
{
QFile::remove("unit-test-settings.ini");
delete m_view;
}

bool TestBase::checkStorageConsistency(int expectedTagCount)
{
const Tag::List tags = m_storage->tags();
expectedTagCount = expectedTagCount == -1 ? tags.count() : expectedTagCount;

if (expectedTagCount != Tag::tagCount) {
qWarning() << "Actual tag count is" << Tag::tagCount
<< "; expected:" << expectedTagCount
<< "; storageCount:" << Storage::storageCount;
return false;
}

foreach (const Tag::Ptr &tag, m_storage->tags())
if (!tag->kernel()) {
qWarning() << "Invalid storage for tag " << tag->name();
return false;
}

foreach (const Task::Ptr &task, m_storage->tasks()) {
if (!task->kernel()) {
qWarning() << "Invalid storage for task " << task->summary();
return false;
}
foreach (const TagRef &tagref, task->tags()) {
Tag::Ptr tag = m_storage->tag(tagref.tagName(), /*create=*/ false);
if (!tags.contains(tag)) {
qWarning() << "Found unknown tag";
return false;
}

if (!tagref.storage()) {
qWarning() << "Tagref not associated with any storage";
return false;
}

if (!tagref.m_task && tag) {
qWarning() << "Null task or tag in TagRef";
return false;
}
}
}

return true;
}

void TestBase::createNewKernel(const QString &dataFilename, bool load)
{
delete m_kernel;
delete m_view;
m_view = 0;
RuntimeConfiguration config;
config.setDataFileName("data_files/" + dataFilename);
config.setPluginsSupported(false);
config.setSettings(new Settings("unit-test-settings.ini"));
config.setSaveEnabled(false);
config.setWebDAVFileName("unit-test-flow.dat");

m_kernel = new Kernel(config);
m_storage = m_kernel->storage();
m_controller = m_kernel->controller();
m_settings = m_kernel->settings();
if (load)
m_kernel->storage()->load();
}

void TestBase::createInstanceWithUI()
{
createNewKernel("quick_tests.dat");
m_view = new QuickView(m_kernel);
m_view->show();
}

void TestBase::waitForIt()
{
QTestEventLoop::instance().enterLoop(10);
QVERIFY(!QTestEventLoop::instance().timeout());
}

void TestBase::stopWaiting()
{
QTestEventLoop::instance().exitLoop();
}

void TestBase::mouseClick(QQuickItem *item)
{
QVERIFY(item->isVisible());
m_view->mouseClick(item);
QTest::qWait(400);
}

void TestBase::moveMouseTo(QQuickItem *item)
{
QVERIFY(item->isVisible());
m_view->moveMouseTo(item);
QTest::qWait(400);
}

void TestBase::mouseClick(const QString &objectName)
{
m_view->mouseClick(objectName);
QTest::qWait(400);
}

void TestBase::sendText(const QString &text)
{
m_view->sendText(text);
}

void TestBase::sendKey(int key, const QString &text, Qt::KeyboardModifiers modifiers)
{
m_view->sendKey(key, text, modifiers);
}
177 changes: 25 additions & 152 deletions tests/testbase.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This file is part of Flow.
Copyright (C) 2014 Sérgio Martins <[email protected]>
Copyright (C) 2014-2015 Sérgio Martins <[email protected]>
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
Expand All @@ -20,162 +20,35 @@
#ifndef FLOW_TEST_BASE_H
#define FLOW_TEST_BASE_H

#include "signalspy.h"
#include "modelsignalspy.h"
#include "storage.h"
#include "kernel.h"
#include "runtimeconfiguration.h"
#include "tag.h"
#include "task.h"
#include "webdavsyncer.h"
#include "controller.h"
#include "tagref.h"
#include "settings.h"
#include "quickview.h"
#include <QtTest/QtTest>
#include <QQuickItem>

#include <QObject>
#include <qnamespace.h>

class Kernel;
class QuickView;
class Storage;
class Controller;
class Settings;
class QString;
class QQuickItem;

class TestBase : public QObject
{
public:

TestBase() : m_view(nullptr)
{
RuntimeConfiguration config;
config.setDataFileName("data.dat");
config.setPluginsSupported(false);
config.setSettings(new Settings("unit-test-settings.ini"));
config.setSaveEnabled(false);
config.setWebDAVFileName("unit-test-flow.dat");
m_kernel = new Kernel(config, this);
m_storage = m_kernel->storage();
m_controller = m_kernel->controller();
m_settings = m_kernel->settings();

if (Storage::storageCount != 1) {
qWarning() << "Storage count is " << Storage::storageCount;
Q_ASSERT(false);
}
}

~TestBase()
{
QFile::remove("unit-test-settings.ini");
delete m_view;
}

bool checkStorageConsistency(int expectedTagCount = -1)
{
const Tag::List tags = m_storage->tags();
expectedTagCount = expectedTagCount == -1 ? tags.count() : expectedTagCount;

if (expectedTagCount != Tag::tagCount) {
qWarning() << "Actual tag count is" << Tag::tagCount
<< "; expected:" << expectedTagCount
<< "; storageCount:" << Storage::storageCount;
return false;
}

foreach (const Tag::Ptr &tag, m_storage->tags())
if (!tag->kernel()) {
qWarning() << "Invalid storage for tag " << tag->name();
return false;
}

foreach (const Task::Ptr &task, m_storage->tasks()) {
if (!task->kernel()) {
qWarning() << "Invalid storage for task " << task->summary();
return false;
}
foreach (const TagRef &tagref, task->tags()) {
Tag::Ptr tag = m_storage->tag(tagref.tagName(), /*create=*/ false);
if (!tags.contains(tag)) {
qWarning() << "Found unknown tag";
return false;
}

if (!tagref.storage()) {
qWarning() << "Tagref not associated with any storage";
return false;
}

if (!tagref.m_task && tag) {
qWarning() << "Null task or tag in TagRef";
return false;
}
}
}

return true;
}

void createNewKernel(const QString &dataFilename, bool load = true)
{
delete m_kernel;
delete m_view;
m_view = 0;
RuntimeConfiguration config;
config.setDataFileName("data_files/" + dataFilename);
config.setPluginsSupported(false);
config.setSettings(new Settings("unit-test-settings.ini"));
config.setSaveEnabled(false);
config.setWebDAVFileName("unit-test-flow.dat");

m_kernel = new Kernel(config);
m_storage = m_kernel->storage();
m_controller = m_kernel->controller();
m_settings = m_kernel->settings();
if (load)
m_kernel->storage()->load();
}

void createInstanceWithUI()
{
createNewKernel("quick_tests.dat");
m_view = new QuickView(m_kernel);
m_view->show();
}

void waitForIt()
{
QTestEventLoop::instance().enterLoop(10);
QVERIFY(!QTestEventLoop::instance().timeout());
}

void stopWaiting()
{
QTestEventLoop::instance().exitLoop();
}

void mouseClick(QQuickItem *item)
{
QVERIFY(item->isVisible());
m_view->mouseClick(item);
QTest::qWait(400);
}

void moveMouseTo(QQuickItem *item)
{
QVERIFY(item->isVisible());
m_view->moveMouseTo(item);
QTest::qWait(400);
}

void mouseClick(const QString &objectName)
{
m_view->mouseClick(objectName);
QTest::qWait(400);
}

void sendText(const QString &text)
{
m_view->sendText(text);
}

void sendKey(int key, const QString &text = "", Qt::KeyboardModifiers modifiers = 0)
{
m_view->sendKey(key, text, modifiers);
}
TestBase();
~TestBase();

bool checkStorageConsistency(int expectedTagCount = -1);
void createNewKernel(const QString &dataFilename, bool load = true);
void createInstanceWithUI();
void waitForIt();
void stopWaiting();
void mouseClick(QQuickItem *item);
void moveMouseTo(QQuickItem *item);
void mouseClick(const QString &objectName);
void sendText(const QString &text);
void sendKey(int key, const QString &text = "", Qt::KeyboardModifiers modifiers = 0);
protected:
Kernel *m_kernel;
QuickView *m_view;
Expand Down
5 changes: 5 additions & 0 deletions tests/testcheckabletagmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
*/

#include "testcheckabletagmodel.h"
#include "task.h"
#include "storage.h"
#include "kernel.h"
#include "modelsignalspy.h"
#include <QtTest/QtTest>

TestCheckableTagModel::TestCheckableTagModel()
: TestBase()
Expand Down
Loading

0 comments on commit c6bd092

Please sign in to comment.