2010-08-13 12:08:06 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 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/>.
|
|
|
|
*/
|
|
|
|
|
2010-11-21 17:06:30 -05:00
|
|
|
#include "TestGroup.h"
|
|
|
|
|
2011-06-29 12:40:26 -04:00
|
|
|
#include <QtCore/QPointer>
|
2010-08-15 09:03:47 -04:00
|
|
|
#include <QtTest/QSignalSpy>
|
2010-08-13 12:08:06 -04:00
|
|
|
#include <QtTest/QTest>
|
|
|
|
|
2012-01-01 16:04:27 -05:00
|
|
|
#include "tests.h"
|
2010-08-13 12:08:06 -04:00
|
|
|
#include "core/Database.h"
|
2011-07-08 07:57:02 -04:00
|
|
|
#include "core/Group.h"
|
2011-11-12 14:00:19 -05:00
|
|
|
#include "crypto/Crypto.h"
|
2010-08-13 12:08:06 -04:00
|
|
|
|
2010-09-18 11:15:22 -04:00
|
|
|
void TestGroup::initTestCase()
|
|
|
|
{
|
2012-04-18 11:42:55 -04:00
|
|
|
qRegisterMetaType<Entry*>("Entry*");
|
2010-09-18 11:15:22 -04:00
|
|
|
qRegisterMetaType<Group*>("Group*");
|
2011-11-12 14:00:19 -05:00
|
|
|
Crypto::init();
|
2010-09-18 11:15:22 -04:00
|
|
|
}
|
|
|
|
|
2010-08-13 12:08:06 -04:00
|
|
|
void TestGroup::testParenting()
|
|
|
|
{
|
|
|
|
Database* db = new Database();
|
2011-07-09 15:54:01 -04:00
|
|
|
QPointer<Group> rootGroup = db->rootGroup();
|
2010-08-13 12:08:06 -04:00
|
|
|
Group* tmpRoot = new Group();
|
|
|
|
|
2011-06-29 12:40:26 -04:00
|
|
|
QPointer<Group> g1 = new Group();
|
|
|
|
QPointer<Group> g2 = new Group();
|
|
|
|
QPointer<Group> g3 = new Group();
|
|
|
|
QPointer<Group> g4 = new Group();
|
|
|
|
|
2010-08-13 12:08:06 -04:00
|
|
|
g1->setParent(tmpRoot);
|
|
|
|
g2->setParent(tmpRoot);
|
|
|
|
g3->setParent(tmpRoot);
|
|
|
|
g4->setParent(tmpRoot);
|
|
|
|
|
|
|
|
g2->setParent(g1);
|
|
|
|
g4->setParent(g3);
|
|
|
|
g3->setParent(g1);
|
2011-07-09 15:54:01 -04:00
|
|
|
g1->setParent(db->rootGroup());
|
2010-08-13 12:08:06 -04:00
|
|
|
|
2011-07-09 15:54:01 -04:00
|
|
|
QVERIFY(g1->parent() == rootGroup);
|
2010-08-13 12:08:06 -04:00
|
|
|
QVERIFY(g2->parent() == g1);
|
|
|
|
QVERIFY(g3->parent() == g1);
|
|
|
|
QVERIFY(g4->parent() == g3);
|
|
|
|
|
2010-08-14 08:26:25 -04:00
|
|
|
QVERIFY(g1->database() == db);
|
|
|
|
QVERIFY(g2->database() == db);
|
|
|
|
QVERIFY(g3->database() == db);
|
|
|
|
QVERIFY(g4->database() == db);
|
|
|
|
|
2010-08-18 05:05:46 -04:00
|
|
|
QCOMPARE(tmpRoot->children().size(), 0);
|
2011-07-09 15:54:01 -04:00
|
|
|
QCOMPARE(rootGroup->children().size(), 1);
|
2010-08-18 05:05:46 -04:00
|
|
|
QCOMPARE(g1->children().size(), 2);
|
|
|
|
QCOMPARE(g2->children().size(), 0);
|
|
|
|
QCOMPARE(g3->children().size(), 1);
|
|
|
|
QCOMPARE(g4->children().size(), 0);
|
2010-08-13 12:08:06 -04:00
|
|
|
|
2011-07-09 15:54:01 -04:00
|
|
|
QVERIFY(rootGroup->children().at(0) == g1);
|
2010-08-15 06:31:48 -04:00
|
|
|
QVERIFY(g1->children().at(0) == g2);
|
|
|
|
QVERIFY(g1->children().at(1) == g3);
|
2010-08-13 12:08:06 -04:00
|
|
|
QVERIFY(g3->children().contains(g4));
|
2010-08-15 09:03:47 -04:00
|
|
|
|
2010-08-23 15:30:20 -04:00
|
|
|
QSignalSpy spy(db, SIGNAL(groupDataChanged(Group*)));
|
2010-08-15 09:03:47 -04:00
|
|
|
g2->setName("test");
|
|
|
|
g4->setName("test");
|
|
|
|
g3->setName("test");
|
|
|
|
g1->setName("test");
|
|
|
|
g3->setIcon(Uuid::random());
|
|
|
|
g1->setIcon(2);
|
2010-08-18 05:05:46 -04:00
|
|
|
QCOMPARE(spy.count(), 6);
|
2011-06-29 12:40:26 -04:00
|
|
|
|
|
|
|
delete db;
|
|
|
|
|
2011-07-09 15:54:01 -04:00
|
|
|
QVERIFY(rootGroup.isNull());
|
2011-06-29 12:40:26 -04:00
|
|
|
QVERIFY(g1.isNull());
|
|
|
|
QVERIFY(g2.isNull());
|
|
|
|
QVERIFY(g3.isNull());
|
|
|
|
QVERIFY(g4.isNull());
|
|
|
|
|
|
|
|
delete tmpRoot;
|
2010-08-13 12:08:06 -04:00
|
|
|
}
|
|
|
|
|
2010-08-18 04:27:40 -04:00
|
|
|
void TestGroup::testSignals()
|
|
|
|
{
|
|
|
|
Database* db = new Database();
|
2011-07-09 15:54:01 -04:00
|
|
|
QPointer<Group> root = db->rootGroup();
|
2010-08-18 04:27:40 -04:00
|
|
|
|
|
|
|
Group* g1 = new Group();
|
|
|
|
Group* g2 = new Group();
|
|
|
|
g1->setParent(root);
|
|
|
|
g2->setParent(root);
|
|
|
|
|
2010-08-23 15:30:20 -04:00
|
|
|
QSignalSpy spyAboutToAdd(db, SIGNAL(groupAboutToAdd(Group*,int)));
|
2010-08-18 04:27:40 -04:00
|
|
|
QSignalSpy spyAdded(db, SIGNAL(groupAdded()));
|
2010-08-23 15:30:20 -04:00
|
|
|
QSignalSpy spyAboutToRemove(db, SIGNAL(groupAboutToRemove(Group*)));
|
2010-08-18 04:27:40 -04:00
|
|
|
QSignalSpy spyRemoved(db, SIGNAL(groupRemoved()));
|
|
|
|
|
|
|
|
g2->setParent(root, 0);
|
|
|
|
|
|
|
|
QCOMPARE(spyAboutToAdd.count(), 1);
|
|
|
|
QCOMPARE(spyAdded.count(), 1);
|
|
|
|
QCOMPARE(spyAboutToRemove.count(), 1);
|
|
|
|
QCOMPARE(spyRemoved.count(), 1);
|
2010-08-18 10:22:48 -04:00
|
|
|
|
|
|
|
delete db;
|
2011-06-29 12:40:26 -04:00
|
|
|
|
|
|
|
QVERIFY(root.isNull());
|
2010-08-18 10:22:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestGroup::testEntries()
|
|
|
|
{
|
|
|
|
Group* group = new Group();
|
|
|
|
|
2011-06-29 12:40:26 -04:00
|
|
|
QPointer<Entry> entry1 = new Entry();
|
2010-08-18 10:22:48 -04:00
|
|
|
entry1->setGroup(group);
|
|
|
|
|
2011-06-29 12:40:26 -04:00
|
|
|
QPointer<Entry> entry2 = new Entry();
|
2010-08-18 10:22:48 -04:00
|
|
|
entry2->setGroup(group);
|
|
|
|
|
|
|
|
QCOMPARE(group->entries().size(), 2);
|
|
|
|
QVERIFY(group->entries().at(0) == entry1);
|
|
|
|
QVERIFY(group->entries().at(1) == entry2);
|
|
|
|
|
|
|
|
delete group;
|
2011-06-29 12:40:26 -04:00
|
|
|
|
|
|
|
QVERIFY(entry1.isNull());
|
|
|
|
QVERIFY(entry2.isNull());
|
2010-08-18 04:27:40 -04:00
|
|
|
}
|
|
|
|
|
2011-07-09 15:54:01 -04:00
|
|
|
void TestGroup::testDeleteSignals()
|
|
|
|
{
|
|
|
|
Database* db = new Database();
|
|
|
|
Group* groupRoot = db->rootGroup();
|
|
|
|
Group* groupChild = new Group();
|
|
|
|
Group* groupChildChild = new Group();
|
|
|
|
groupRoot->setObjectName("groupRoot");
|
|
|
|
groupChild->setObjectName("groupChild");
|
|
|
|
groupChildChild->setObjectName("groupChildChild");
|
|
|
|
groupChild->setParent(groupRoot);
|
|
|
|
groupChildChild->setParent(groupChild);
|
|
|
|
QSignalSpy spyAboutToRemove(db, SIGNAL(groupAboutToRemove(Group*)));
|
|
|
|
QSignalSpy spyRemoved(db, SIGNAL(groupRemoved()));
|
|
|
|
|
|
|
|
delete groupChild;
|
|
|
|
QVERIFY(groupRoot->children().isEmpty());
|
|
|
|
QCOMPARE(spyAboutToRemove.count(), 1);
|
|
|
|
QCOMPARE(spyRemoved.count(), 1);
|
|
|
|
delete db;
|
|
|
|
|
|
|
|
|
|
|
|
Group* group = new Group();
|
|
|
|
Entry* entry = new Entry();
|
|
|
|
entry->setGroup(group);
|
|
|
|
QSignalSpy spyEntryAboutToRemove(group, SIGNAL(entryAboutToRemove(Entry*)));
|
|
|
|
QSignalSpy spyEntryRemoved(group, SIGNAL(entryRemoved()));
|
|
|
|
|
|
|
|
delete entry;
|
|
|
|
QVERIFY(group->entries().isEmpty());
|
|
|
|
QCOMPARE(spyEntryAboutToRemove.count(), 1);
|
|
|
|
QCOMPARE(spyEntryRemoved.count(), 1);
|
|
|
|
delete group;
|
2012-04-21 17:31:37 -04:00
|
|
|
|
|
|
|
Database* db2 = new Database();
|
|
|
|
Group* groupRoot2 = db2->rootGroup();
|
|
|
|
Group* group2 = new Group();
|
|
|
|
group2->setParent(groupRoot2);
|
|
|
|
Entry* entry2 = new Entry();
|
|
|
|
entry2->setGroup(group2);
|
|
|
|
QSignalSpy spyEntryAboutToRemove2(group2, SIGNAL(entryAboutToRemove(Entry*)));
|
|
|
|
QSignalSpy spyEntryRemoved2(group2, SIGNAL(entryRemoved()));
|
|
|
|
|
|
|
|
delete group2;
|
|
|
|
QCOMPARE(spyEntryAboutToRemove2.count(), 0);
|
|
|
|
QCOMPARE(spyEntryRemoved2.count(), 0);
|
2012-04-21 18:29:16 -04:00
|
|
|
delete db2;
|
2011-07-09 15:54:01 -04:00
|
|
|
}
|
|
|
|
|
2012-01-01 16:04:27 -05:00
|
|
|
KEEPASSX_QTEST_CORE_MAIN(TestGroup)
|