From ea38b721d6ec46ea94f1f240090285571fe47620 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Thu, 5 Jan 2012 18:45:21 +0100 Subject: [PATCH] Add pixmap gui test. --- tests/gui/CMakeLists.txt | 2 + tests/gui/TestGuiPixmaps.cpp | 150 +++++++++++++++++++++++++++++++++++ tests/gui/TestGuiPixmaps.h | 39 +++++++++ 3 files changed, 191 insertions(+) create mode 100644 tests/gui/TestGuiPixmaps.cpp create mode 100644 tests/gui/TestGuiPixmaps.h diff --git a/tests/gui/CMakeLists.txt b/tests/gui/CMakeLists.txt index 93beccd46..91fcd6956 100644 --- a/tests/gui/CMakeLists.txt +++ b/tests/gui/CMakeLists.txt @@ -14,3 +14,5 @@ # along with this program. If not, see . add_unit_test(NAME testgui SOURCES TestGui.cpp MOCS TestGui.h LIBS ${TEST_LIBRARIES}) + +add_unit_test(NAME testguipixmaps SOURCES TestGuiPixmaps.cpp MOCS TestGuiPixmaps.h LIBS ${TEST_LIBRARIES}) diff --git a/tests/gui/TestGuiPixmaps.cpp b/tests/gui/TestGuiPixmaps.cpp new file mode 100644 index 000000000..e9e98a839 --- /dev/null +++ b/tests/gui/TestGuiPixmaps.cpp @@ -0,0 +1,150 @@ +/* + * Copyright (C) 2011 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 "TestGuiPixmaps.h" + +#include + +#include "tests.h" +#include "core/Database.h" +#include "core/DatabaseIcons.h" +#include "core/Entry.h" +#include "core/Group.h" +#include "core/Metadata.h" +#include "crypto/Crypto.h" + +void TestGuiPixmaps::initTestCase() +{ + Crypto::init(); +} + +void TestGuiPixmaps::testDatabaseIcons() +{ + QImage image; + QPixmap pixmap; + QPixmap pixmapCached; + + image = databaseIcons()->icon(0); + pixmap = databaseIcons()->iconPixmap(0); + compareImages(pixmap, image); + // check if the cache works correctly + pixmapCached = databaseIcons()->iconPixmap(0); + compareImages(pixmapCached, image); + QCOMPARE(pixmapCached.cacheKey(), pixmap.cacheKey()); + + pixmap = databaseIcons()->iconPixmap(1); + image = databaseIcons()->icon(1); + compareImages(pixmap, image); + pixmapCached = databaseIcons()->iconPixmap(1); + compareImages(pixmapCached, image); + QCOMPARE(pixmapCached.cacheKey(), pixmap.cacheKey()); +} + +void TestGuiPixmaps::testEntryIcons() +{ + Database* db = new Database(); + Entry* entry = new Entry(); + entry->setGroup(db->rootGroup()); + + QImage icon; + QImage image; + QPixmap pixmap; + QPixmap pixmapCached1; + QPixmap pixmapCached2; + + icon = databaseIcons()->icon(10); + entry->setIcon(10); + image = entry->icon(); + pixmap = entry->iconPixmap(); + QCOMPARE(image, icon); + compareImages(pixmap, icon); + pixmapCached1 = entry->iconPixmap(); + pixmapCached2 = databaseIcons()->iconPixmap(10); + compareImages(pixmapCached1, icon); + compareImages(pixmapCached2, icon); + QCOMPARE(pixmapCached1.cacheKey(), pixmap.cacheKey()); + QCOMPARE(pixmapCached2.cacheKey(), pixmap.cacheKey()); + + Uuid iconUuid = Uuid::random(); + icon = QImage(2, 1, QImage::Format_RGB32); + icon.setPixel(0, 0, qRgb(0, 0, 0)); + icon.setPixel(1, 0, qRgb(0, 0, 50)); + db->metadata()->addCustomIcon(iconUuid, icon); + entry->setIcon(iconUuid); + + image = entry->icon(); + pixmap = entry->iconPixmap(); + + QCOMPARE(image, icon); + compareImages(pixmap, icon); + pixmapCached1 = entry->iconPixmap(); + compareImages(pixmapCached1, icon); + QCOMPARE(pixmapCached1.cacheKey(), pixmap.cacheKey()); + + delete db; +} + +void TestGuiPixmaps::testGroupIcons() +{ + Database* db = new Database(); + Group* group = db->rootGroup(); + + QImage icon; + QImage image; + QPixmap pixmap; + QPixmap pixmapCached1; + QPixmap pixmapCached2; + + icon = databaseIcons()->icon(10); + group->setIcon(10); + image = group->icon(); + pixmap = group->iconPixmap(); + QCOMPARE(image, icon); + compareImages(pixmap, icon); + pixmapCached1 = group->iconPixmap(); + pixmapCached2 = databaseIcons()->iconPixmap(10); + compareImages(pixmapCached1, icon); + compareImages(pixmapCached2, icon); + QCOMPARE(pixmapCached1.cacheKey(), pixmap.cacheKey()); + QCOMPARE(pixmapCached2.cacheKey(), pixmap.cacheKey()); + + Uuid iconUuid = Uuid::random(); + icon = QImage(2, 1, QImage::Format_RGB32); + icon.setPixel(0, 0, qRgb(0, 0, 0)); + icon.setPixel(1, 0, qRgb(0, 0, 50)); + db->metadata()->addCustomIcon(iconUuid, icon); + group->setIcon(iconUuid); + + image = group->icon(); + pixmap = group->iconPixmap(); + + QCOMPARE(image, icon); + compareImages(pixmap, icon); + pixmapCached1 = group->iconPixmap(); + compareImages(pixmapCached1, icon); + QCOMPARE(pixmapCached1.cacheKey(), pixmap.cacheKey()); + + delete db; +} + +void TestGuiPixmaps::compareImages(const QPixmap& pixmap, const QImage& image) +{ + QCOMPARE(pixmap.toImage().convertToFormat(QImage::Format_ARGB32_Premultiplied), + image.convertToFormat(QImage::Format_ARGB32_Premultiplied)); +} + +KEEPASSX_QTEST_GUI_MAIN(TestGuiPixmaps) diff --git a/tests/gui/TestGuiPixmaps.h b/tests/gui/TestGuiPixmaps.h new file mode 100644 index 000000000..99af8a4e8 --- /dev/null +++ b/tests/gui/TestGuiPixmaps.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2011 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_TESTGUIPIXMAPS_H +#define KEEPASSX_TESTGUIPIXMAPS_H + +#include +#include +#include + +class TestGuiPixmaps : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void initTestCase(); + void testDatabaseIcons(); + void testEntryIcons(); + void testGroupIcons(); + +private: + void compareImages(const QPixmap& pixmap, const QImage& image); +}; + +#endif // KEEPASSX_TESTGUIPIXMAPS_H