File indexing completed on 2024-05-19 05:22:18
0001 /* 0002 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "gravatarcachetest.h" 0008 #include "../src/misc/gravatarcache.h" 0009 #include "../src/misc/hash.h" 0010 0011 #include <QCryptographicHash> 0012 #include <QPixmap> 0013 #include <QStandardPaths> 0014 #include <QTest> 0015 0016 using namespace Gravatar; 0017 0018 Q_DECLARE_METATYPE(Gravatar::Hash) 0019 0020 GravatarCacheTest::GravatarCacheTest(QObject *parent) 0021 : QObject(parent) 0022 { 0023 } 0024 0025 GravatarCacheTest::~GravatarCacheTest() = default; 0026 0027 void GravatarCacheTest::initTestCase() 0028 { 0029 QStandardPaths::setTestModeEnabled(true); 0030 } 0031 0032 void GravatarCacheTest::shouldHaveDefaultValue() 0033 { 0034 Gravatar::GravatarCache gravatarCache; 0035 QCOMPARE(gravatarCache.maximumSize(), 20); 0036 } 0037 0038 void GravatarCacheTest::shouldChangeCacheValue() 0039 { 0040 Gravatar::GravatarCache gravatarCache; 0041 int val = 30; 0042 gravatarCache.setMaximumSize(val); 0043 0044 QCOMPARE(gravatarCache.maximumSize(), val); 0045 val = 50; 0046 gravatarCache.setMaximumSize(val); 0047 0048 QCOMPARE(gravatarCache.maximumSize(), val); 0049 } 0050 0051 void GravatarCacheTest::testLookup() 0052 { 0053 Hash hash(QCryptographicHash::hash(QByteArray("test@example.com"), QCryptographicHash::Md5), Hash::Md5); 0054 { 0055 GravatarCache cache; 0056 cache.clearAllCache(); 0057 bool found = false; 0058 const auto result = cache.loadGravatarPixmap(hash, found); 0059 QVERIFY(!found); 0060 QVERIFY(result.isNull()); 0061 } 0062 0063 QPixmap px(42, 42); 0064 px.fill(Qt::blue); 0065 0066 { 0067 GravatarCache cache; 0068 cache.saveGravatarPixmap(hash, px); 0069 0070 // in-memory cache lookup 0071 bool found = false; 0072 const auto result = cache.loadGravatarPixmap(hash, found); 0073 QVERIFY(found); 0074 QVERIFY(!result.isNull()); 0075 QCOMPARE(result.size(), QSize(42, 42)); 0076 } 0077 0078 { 0079 // disk lookup 0080 GravatarCache cache; 0081 bool found = false; 0082 const auto result = cache.loadGravatarPixmap(hash, found); 0083 QVERIFY(found); 0084 QVERIFY(!result.isNull()); 0085 QCOMPARE(result.size(), QSize(42, 42)); 0086 } 0087 } 0088 0089 void GravatarCacheTest::testMissing_data() 0090 { 0091 QTest::addColumn<Hash>("hash"); 0092 QTest::newRow("md5") << Hash(QCryptographicHash::hash(QByteArray("testMD5@example.com"), QCryptographicHash::Md5), Hash::Md5); 0093 QTest::newRow("Sha256") << Hash(QCryptographicHash::hash(QByteArray("testSHA256@example.com"), QCryptographicHash::Sha256), Hash::Sha256); 0094 } 0095 0096 void GravatarCacheTest::testMissing() 0097 { 0098 QFETCH(Hash, hash); 0099 { 0100 GravatarCache cache; 0101 cache.clearAllCache(); 0102 bool found = false; 0103 const auto result = cache.loadGravatarPixmap(hash, found); 0104 QVERIFY(!found); 0105 QVERIFY(result.isNull()); 0106 } 0107 0108 { 0109 // store miss and verify in memory 0110 GravatarCache cache; 0111 cache.saveMissingGravatar(hash); 0112 bool found = false; 0113 const auto result = cache.loadGravatarPixmap(hash, found); 0114 QVERIFY(found); 0115 QVERIFY(result.isNull()); 0116 } 0117 0118 { 0119 // verify miss in disk storage 0120 GravatarCache cache; 0121 bool found = false; 0122 const auto result = cache.loadGravatarPixmap(hash, found); 0123 QVERIFY(found); 0124 QVERIFY(result.isNull()); 0125 } 0126 } 0127 0128 QTEST_MAIN(GravatarCacheTest) 0129 0130 #include "moc_gravatarcachetest.cpp"