fix warning
[kdegraphics.git] / gwenview / tests / semanticinfobackendtest.cpp
blobe8f1a62843a5d3819146a8c07e0d83bdec481d88
1 /*
2 Gwenview: an image viewer
3 Copyright 2008 Aurélien Gâteau <aurelien.gateau@free.fr>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 // Local
21 #include "semanticinfobackendtest.moc"
23 // Qt
24 #include <QSignalSpy>
26 // KDE
27 #include <kdebug.h>
28 #include <krandom.h>
29 #include <ktemporaryfile.h>
30 #include <qtest_kde.h>
32 // Local
33 #include "testutils.h"
34 #include <config-gwenview.h>
36 #ifdef GWENVIEW_SEMANTICINFO_BACKEND_FAKE
37 #include <lib/semanticinfo/fakesemanticinfobackend.h>
39 #elif defined(GWENVIEW_SEMANTICINFO_BACKEND_NEPOMUK)
40 #include <lib/semanticinfo/nepomuksemanticinfobackend.h>
42 #else
43 #ifdef __GNUC__
44 #error No metadata backend defined
45 #endif
46 #endif
48 QTEST_KDEMAIN( Gwenview::SemanticInfoBackEndTest, GUI )
50 namespace Gwenview {
53 SemanticInfoBackEndClient::SemanticInfoBackEndClient(AbstractSemanticInfoBackEnd* backEnd)
54 : mBackEnd(backEnd) {
55 connect(backEnd, SIGNAL(semanticInfoRetrieved(const KUrl&, const SemanticInfo&)),
56 SLOT(slotSemanticInfoRetrieved(const KUrl&, const SemanticInfo&)) );
60 void SemanticInfoBackEndClient::slotSemanticInfoRetrieved(const KUrl& url, const SemanticInfo& semanticInfo) {
61 mSemanticInfoForUrl[url] = semanticInfo;
65 void SemanticInfoBackEndTest::initTestCase() {
66 qRegisterMetaType<KUrl>("KUrl");
67 qRegisterMetaType<QString>("SemanticInfoTag");
71 void SemanticInfoBackEndTest::init() {
72 #ifdef GWENVIEW_SEMANTICINFO_BACKEND_FAKE
73 mBackEnd.reset(new FakeSemanticInfoBackEnd(0, FakeSemanticInfoBackEnd::InitializeEmpty));
74 #elif defined(GWENVIEW_SEMANTICINFO_BACKEND_NEPOMUK)
75 mBackEnd.reset(new NepomukSemanticInfoBackEnd(0));
76 #endif
80 /**
81 * Get and set the rating of a temp file
83 void SemanticInfoBackEndTest::testRating() {
84 KTemporaryFile temp;
85 temp.setSuffix(".metadatabackendtest");
86 QVERIFY(temp.open());
88 KUrl url;
89 url.setPath(temp.fileName());
91 SemanticInfoBackEndClient client(mBackEnd.get());
92 QSignalSpy spy(mBackEnd.get(), SIGNAL(semanticInfoRetrieved(const KUrl&, const SemanticInfo&)) );
93 mBackEnd->retrieveSemanticInfo(url);
94 QVERIFY(waitForSignal(spy));
96 SemanticInfo semanticInfo = client.semanticInfoForUrl(url);
97 QCOMPARE(semanticInfo.mRating, 0);
99 semanticInfo.mRating = 5;
100 mBackEnd->storeSemanticInfo(url, semanticInfo);
104 void SemanticInfoBackEndTest::testTagForLabel() {
105 QSignalSpy spy(mBackEnd.get(), SIGNAL(tagAdded(const SemanticInfoTag&, const QString&)) );
107 TagSet oldAllTags = mBackEnd->allTags();
108 QString label = "testTagForLabel-" + KRandom::randomString(5);
109 SemanticInfoTag tag1 = mBackEnd->tagForLabel(label);
110 QVERIFY(!tag1.isEmpty());
111 QVERIFY(!oldAllTags.contains(tag1));
112 QVERIFY(mBackEnd->allTags().contains(tag1));
114 // This is a new tag, we should receive a signal
115 QCOMPARE(spy.count(), 1);
117 SemanticInfoTag tag2 = mBackEnd->tagForLabel(label);
118 QCOMPARE(tag1, tag2);
119 // This is not a new tag, we should not receive a signal
120 QCOMPARE(spy.count(), 1);
122 QString label2 = mBackEnd->labelForTag(tag2);
123 QCOMPARE(label, label2);
127 } // namespace