From ec3cca1f770303d230e44a8b69c73a64d7ba91be Mon Sep 17 00:00:00 2001 From: Florian Geyer Date: Wed, 18 Apr 2012 13:27:16 +0200 Subject: [PATCH] Add test for modified signals. --- tests/CMakeLists.txt | 2 + tests/TestModified.cpp | 306 +++++++++++++++++++++++++++++++++++++++++ tests/TestModified.h | 34 +++++ 3 files changed, 342 insertions(+) create mode 100644 tests/TestModified.cpp create mode 100644 tests/TestModified.h diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 31f2539a3..d64ac0249 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -123,6 +123,8 @@ add_unit_test(NAME testhashedblockstream SOURCES TestHashedBlockStream.cpp MOCS add_unit_test(NAME testkeepass2randomstream SOURCES TestKeePass2RandomStream.cpp MOCS TestKeePass2RandomStream.h LIBS ${TEST_LIBRARIES}) +add_unit_test(NAME testmodified SOURCES TestModified.cpp MOCS TestModified.h LIBS ${TEST_LIBRARIES}) + if(WITH_GUI_TESTS) add_subdirectory(gui) diff --git a/tests/TestModified.cpp b/tests/TestModified.cpp new file mode 100644 index 000000000..7d1309250 --- /dev/null +++ b/tests/TestModified.cpp @@ -0,0 +1,306 @@ +/* + * 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 "TestModified.h" + +#include +#include +#include + +#include "tests.h" +#include "core/Database.h" +#include "core/Group.h" +#include "crypto/Crypto.h" + +void TestModified::initTestCase() +{ + Crypto::init(); +} + +void TestModified::testSignals() +{ + int spyCount = 0; + int spyCount2 = 0; + + CompositeKey* compositeKey = new CompositeKey(); + + Database* db = new Database(); + QPointer root = db->rootGroup(); + QSignalSpy spyModified(db, SIGNAL(modified())); + + db->setKey(*compositeKey); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + + Group* g1 = new Group(); + g1->setParent(root); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + + Group* g2 = new Group(); + g2->setParent(root); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + + QPointer entry1 = new Entry(); + entry1->setGroup(g1); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + + Database* db2 = new Database(); + QPointer root2 = db2->rootGroup(); + QSignalSpy spyModified2(db2, SIGNAL(modified())); + + g1->setParent(root2); + spyCount++; + spyCount2++; + QCOMPARE(spyModified.count(), spyCount); + QCOMPARE(spyModified2.count(), spyCount2); + + QPointer entry2 = new Entry(); + entry2->setGroup(g2); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + QCOMPARE(spyModified2.count(), spyCount2); + + entry2->setGroup(root2); + spyCount++; + spyCount2++; + QCOMPARE(spyModified.count(), spyCount); + QCOMPARE(spyModified2.count(), spyCount2); + + Group* g3 = new Group(); + g3->setParent(root); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + + Group* g4 = new Group(); + g4->setParent(g3); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + + QCOMPARE(spyModified2.count(), spyCount2); +} + +void TestModified::testGroupSets() +{ + int spyCount = 0; + Database* db = new Database(); + QPointer root = db->rootGroup(); + + Group* g = new Group(); + g->setParent(root); + + QSignalSpy spyModified(db, SIGNAL(modified())); + + root->setUuid(Uuid::random()); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + root->setUuid(root->uuid()); + QCOMPARE(spyModified.count(), spyCount); + + root->setName("test"); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + root->setName(root->name()); + QCOMPARE(spyModified.count(), spyCount); + + root->setNotes("test"); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + root->setNotes(root->notes()); + QCOMPARE(spyModified.count(), spyCount); + + root->setIcon(1); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + root->setIcon(root->iconNumber()); + QCOMPARE(spyModified.count(), spyCount); + + root->setIcon(Uuid::random()); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + root->setIcon(root->iconUuid()); + QCOMPARE(spyModified.count(), spyCount); + + + g->setUuid(Uuid::random()); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + g->setUuid(g->uuid()); + QCOMPARE(spyModified.count(), spyCount); + + g->setName("test"); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + g->setName(g->name()); + QCOMPARE(spyModified.count(), spyCount); + + g->setNotes("test"); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + g->setNotes(g->notes()); + QCOMPARE(spyModified.count(), spyCount); + + g->setIcon(1); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + g->setIcon(g->iconNumber()); + QCOMPARE(spyModified.count(), spyCount); + + g->setIcon(Uuid::random()); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + g->setIcon(g->iconUuid()); + QCOMPARE(spyModified.count(), spyCount); +} + +void TestModified::testEntrySets() +{ + int spyCount = 0; + Database* db = new Database(); + QPointer root = db->rootGroup(); + + Group* g = new Group(); + g->setParent(root); + QPointer entry = new Entry(); + entry->setGroup(g); + + QSignalSpy spyModified(db, SIGNAL(modified())); + + entry->setUuid(Uuid::random()); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + entry->setUuid(entry->uuid()); + QCOMPARE(spyModified.count(), spyCount); + + entry->setTitle("test"); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + entry->setTitle(entry->title()); + QCOMPARE(spyModified.count(), spyCount); + + entry->setUrl("test"); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + entry->setUrl(entry->url()); + QCOMPARE(spyModified.count(), spyCount); + + entry->setUsername("test"); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + entry->setUsername(entry->username()); + QCOMPARE(spyModified.count(), spyCount); + + entry->setPassword("test"); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + entry->setPassword(entry->password()); + QCOMPARE(spyModified.count(), spyCount); + + entry->setNotes("test"); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + entry->setNotes(entry->notes()); + QCOMPARE(spyModified.count(), spyCount); + + entry->setIcon(1); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + entry->setIcon(entry->iconNumber()); + QCOMPARE(spyModified.count(), spyCount); + + entry->setIcon(Uuid::random()); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + entry->setIcon(entry->iconUuid()); + QCOMPARE(spyModified.count(), spyCount); + + entry->setTags("test"); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + entry->setTags(entry->tags()); + QCOMPARE(spyModified.count(), spyCount); + + entry->setExpires(true); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + entry->setExpires(entry->timeInfo().expires()); + QCOMPARE(spyModified.count(), spyCount); + + entry->setExpiryTime(QDateTime::currentDateTimeUtc().addYears(1)); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + entry->setExpiryTime(entry->timeInfo().expiryTime()); + QCOMPARE(spyModified.count(), spyCount); + + entry->setAutoTypeEnabled(false); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + entry->setAutoTypeEnabled(entry->autoTypeEnabled()); + QCOMPARE(spyModified.count(), spyCount); + + entry->setAutoTypeObfuscation(1); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + entry->setAutoTypeObfuscation(entry->autoTypeObfuscation()); + QCOMPARE(spyModified.count(), spyCount); + + entry->setDefaultAutoTypeSequence("test"); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + entry->setDefaultAutoTypeSequence(entry->defaultAutoTypeSequence()); + QCOMPARE(spyModified.count(), spyCount); + + entry->setForegroundColor(Qt::red); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + entry->setForegroundColor(entry->foregroundColor()); + QCOMPARE(spyModified.count(), spyCount); + + entry->setBackgroundColor(Qt::red); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + entry->setBackgroundColor(entry->backgroundColor()); + QCOMPARE(spyModified.count(), spyCount); + + entry->setOverrideUrl("test"); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + entry->setOverrideUrl(entry->overrideUrl()); + QCOMPARE(spyModified.count(), spyCount); + + entry->attributes()->set("test key", "test value", false); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + entry->attributes()->set("test key", entry->attributes()->value("test key"), false); + QCOMPARE(spyModified.count(), spyCount); + entry->attributes()->set("test key", entry->attributes()->value("test key"), true); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + entry->attributes()->set("test key", "new test value", true); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + + entry->attributes()->set("test key2", "test value2", true); + spyCount++; + QCOMPARE(spyModified.count(), spyCount); + entry->attributes()->set("test key2", entry->attributes()->value("test key2"), true); + QCOMPARE(spyModified.count(), spyCount); +} + +KEEPASSX_QTEST_CORE_MAIN(TestModified) diff --git a/tests/TestModified.h b/tests/TestModified.h new file mode 100644 index 000000000..ed8b20530 --- /dev/null +++ b/tests/TestModified.h @@ -0,0 +1,34 @@ +/* + * 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_TESTMODIFIED_H +#define KEEPASSX_TESTMODIFIED_H + +#include + +class TestModified : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void initTestCase(); + void testSignals(); + void testGroupSets(); + void testEntrySets(); +}; + +#endif