Changeset View
Changeset View
Standalone View
Standalone View
autotests/gravatarcachetest.cpp
Show All 13 Lines | 1 | /* | |||
---|---|---|---|---|---|
14 | You should have received a copy of the GNU Library General Public License | 14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to | 15 | along with this library; see the file COPYING.LIB. If not, write to | ||
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
17 | Boston, MA 02110-1301, USA. | 17 | Boston, MA 02110-1301, USA. | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include "gravatarcachetest.h" | 20 | #include "gravatarcachetest.h" | ||
21 | #include "../src/misc/gravatarcache.h" | 21 | #include "../src/misc/gravatarcache.h" | ||
22 | #include "../src/misc/hash.h" | ||||
22 | 23 | | |||
24 | #include <QCryptographicHash> | ||||
23 | #include <QPixmap> | 25 | #include <QPixmap> | ||
24 | #include <QStandardPaths> | 26 | #include <QStandardPaths> | ||
25 | #include <qtest.h> | 27 | #include <qtest.h> | ||
26 | 28 | | |||
27 | using namespace Gravatar; | 29 | using namespace Gravatar; | ||
28 | 30 | | |||
31 | Q_DECLARE_METATYPE(Gravatar::Hash) | ||||
32 | | ||||
29 | GravatarCacheTest::GravatarCacheTest(QObject *parent) | 33 | GravatarCacheTest::GravatarCacheTest(QObject *parent) | ||
30 | : QObject(parent) | 34 | : QObject(parent) | ||
31 | { | 35 | { | ||
32 | } | 36 | } | ||
33 | 37 | | |||
34 | GravatarCacheTest::~GravatarCacheTest() | 38 | GravatarCacheTest::~GravatarCacheTest() | ||
35 | { | 39 | { | ||
36 | } | 40 | } | ||
Show All 19 Lines | 54 | { | |||
56 | val = 50; | 60 | val = 50; | ||
57 | gravatarCache.setMaximumSize(val); | 61 | gravatarCache.setMaximumSize(val); | ||
58 | 62 | | |||
59 | QCOMPARE(gravatarCache.maximumSize(), val); | 63 | QCOMPARE(gravatarCache.maximumSize(), val); | ||
60 | } | 64 | } | ||
61 | 65 | | |||
62 | void GravatarCacheTest::testLookup() | 66 | void GravatarCacheTest::testLookup() | ||
63 | { | 67 | { | ||
68 | Hash hash(QCryptographicHash::hash(QByteArray("test@example.com"), QCryptographicHash::Md5), Hash::Md5); | ||||
64 | { | 69 | { | ||
65 | GravatarCache cache; | 70 | GravatarCache cache; | ||
66 | cache.clearAllCache(); | 71 | cache.clearAllCache(); | ||
67 | bool found = false; | 72 | bool found = false; | ||
68 | const auto result = cache.loadGravatarPixmap(QStringLiteral("fa1afe1"), found); | 73 | const auto result = cache.loadGravatarPixmap(hash, found); | ||
69 | QVERIFY(!found); | 74 | QVERIFY(!found); | ||
70 | QVERIFY(result.isNull()); | 75 | QVERIFY(result.isNull()); | ||
71 | } | 76 | } | ||
72 | 77 | | |||
73 | QPixmap px(42, 42); | 78 | QPixmap px(42, 42); | ||
74 | px.fill(Qt::blue); | 79 | px.fill(Qt::blue); | ||
75 | 80 | | |||
76 | { | 81 | { | ||
77 | GravatarCache cache; | 82 | GravatarCache cache; | ||
78 | cache.saveGravatarPixmap(QStringLiteral("fa1afe1"), px); | 83 | cache.saveGravatarPixmap(hash, px); | ||
79 | 84 | | |||
80 | // in-memory cache lookup | 85 | // in-memory cache lookup | ||
81 | bool found = false; | 86 | bool found = false; | ||
82 | const auto result = cache.loadGravatarPixmap(QStringLiteral("fa1afe1"), found); | 87 | const auto result = cache.loadGravatarPixmap(hash, found); | ||
83 | QVERIFY(found); | 88 | QVERIFY(found); | ||
84 | QVERIFY(!result.isNull()); | 89 | QVERIFY(!result.isNull()); | ||
85 | QCOMPARE(result.size(), QSize(42, 42)); | 90 | QCOMPARE(result.size(), QSize(42, 42)); | ||
86 | } | 91 | } | ||
87 | 92 | | |||
88 | { | 93 | { | ||
89 | // disk lookup | 94 | // disk lookup | ||
90 | GravatarCache cache; | 95 | GravatarCache cache; | ||
91 | bool found = false; | 96 | bool found = false; | ||
92 | const auto result = cache.loadGravatarPixmap(QStringLiteral("fa1afe1"), found); | 97 | const auto result = cache.loadGravatarPixmap(hash, found); | ||
93 | QVERIFY(found); | 98 | QVERIFY(found); | ||
94 | QVERIFY(!result.isNull()); | 99 | QVERIFY(!result.isNull()); | ||
95 | QCOMPARE(result.size(), QSize(42, 42)); | 100 | QCOMPARE(result.size(), QSize(42, 42)); | ||
96 | } | 101 | } | ||
97 | } | 102 | } | ||
98 | 103 | | |||
104 | void GravatarCacheTest::testMissing_data() | ||||
105 | { | ||||
106 | QTest::addColumn<Hash>("hash"); | ||||
107 | QTest::newRow("md5") << Hash(QCryptographicHash::hash(QByteArray("testMD5@example.com"), QCryptographicHash::Md5), Hash::Md5); | ||||
108 | QTest::newRow("Sha256") << Hash(QCryptographicHash::hash(QByteArray("testSHA256@example.com"), QCryptographicHash::Sha256), Hash::Sha256); | ||||
109 | } | ||||
110 | | ||||
111 | void GravatarCacheTest::testMissing() | ||||
112 | { | ||||
113 | QFETCH(Hash, hash); | ||||
114 | { | ||||
115 | GravatarCache cache; | ||||
116 | cache.clearAllCache(); | ||||
117 | bool found = false; | ||||
118 | const auto result = cache.loadGravatarPixmap(hash, found); | ||||
119 | QVERIFY(!found); | ||||
120 | QVERIFY(result.isNull()); | ||||
121 | } | ||||
122 | | ||||
123 | { | ||||
124 | // store miss and verify in memory | ||||
125 | GravatarCache cache; | ||||
126 | cache.saveMissingGravatar(hash); | ||||
127 | bool found = false; | ||||
128 | const auto result = cache.loadGravatarPixmap(hash, found); | ||||
129 | QVERIFY(found); | ||||
130 | QVERIFY(result.isNull()); | ||||
131 | } | ||||
132 | | ||||
133 | { | ||||
134 | // verify miss in disk storage | ||||
135 | GravatarCache cache; | ||||
136 | bool found = false; | ||||
137 | const auto result = cache.loadGravatarPixmap(hash, found); | ||||
138 | QVERIFY(found); | ||||
139 | QVERIFY(result.isNull()); | ||||
140 | } | ||||
141 | } | ||||
142 | | ||||
99 | QTEST_MAIN(GravatarCacheTest) | 143 | QTEST_MAIN(GravatarCacheTest) |