TestGuiPixmaps.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (C) 2011 Felix Geyer <debfx@fobos.de>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 or (at your option)
  7. * version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "TestGuiPixmaps.h"
  18. #include "core/Metadata.h"
  19. #include <QTest>
  20. #include "core/Group.h"
  21. #include "crypto/Crypto.h"
  22. #include "gui/DatabaseIcons.h"
  23. #include "gui/Icons.h"
  24. void TestGuiPixmaps::initTestCase()
  25. {
  26. QVERIFY(Crypto::init());
  27. }
  28. void TestGuiPixmaps::testDatabaseIcons()
  29. {
  30. QVERIFY(!databaseIcons()->icon(0).isNull());
  31. }
  32. void TestGuiPixmaps::testEntryIcons()
  33. {
  34. QScopedPointer<Database> db(new Database());
  35. auto entry = new Entry();
  36. entry->setGroup(db->rootGroup());
  37. // Test setting standard icon
  38. entry->setIcon(10);
  39. auto pixmap = Icons::entryIconPixmap(entry);
  40. QVERIFY(pixmap.toImage() == databaseIcons()->icon(10).toImage());
  41. // Test setting custom icon
  42. QUuid iconUuid = QUuid::createUuid();
  43. QImage icon(2, 1, QImage::Format_RGB32);
  44. icon.setPixel(0, 0, qRgb(0, 0, 0));
  45. icon.setPixel(1, 0, qRgb(0, 0, 50));
  46. db->metadata()->addCustomIcon(iconUuid, Icons::saveToBytes(icon));
  47. QCOMPARE(db->metadata()->customIconsOrder().count(), 1);
  48. entry->setIcon(iconUuid);
  49. // Confirm the icon is the same as that stored in the database
  50. QVERIFY(Icons::entryIconPixmap(entry).toImage() == Icons::customIconPixmap(db.data(), iconUuid).toImage());
  51. }
  52. void TestGuiPixmaps::testGroupIcons()
  53. {
  54. QScopedPointer<Database> db(new Database());
  55. auto group = db->rootGroup();
  56. // Test setting standard icon
  57. group->setIcon(10);
  58. auto pixmap = Icons::groupIconPixmap(group);
  59. QVERIFY(pixmap.toImage() == databaseIcons()->icon(10).toImage());
  60. // Test setting custom icon
  61. QUuid iconUuid = QUuid::createUuid();
  62. QImage icon(2, 1, QImage::Format_RGB32);
  63. icon.setPixel(0, 0, qRgb(0, 0, 0));
  64. icon.setPixel(1, 0, qRgb(0, 0, 50));
  65. db->metadata()->addCustomIcon(iconUuid, Icons::saveToBytes(icon));
  66. group->setIcon(iconUuid);
  67. // Confirm the icon is the same as that stored in the database
  68. QVERIFY(Icons::groupIconPixmap(group).toImage() == Icons::customIconPixmap(db.data(), iconUuid).toImage());
  69. }
  70. QTEST_MAIN(TestGuiPixmaps)