File indexing completed on 2024-05-12 04:19:58

0001 /*
0002 Gwenview: an image viewer
0003 Copyright 2008 Aurélien Gâteau <agateau@kde.org>
0004 
0005 This program is free software; you can redistribute it and/or
0006 modify it under the terms of the GNU General Public License
0007 as published by the Free Software Foundation; either version 2
0008 of the License, or (at your option) any later version.
0009 
0010 This program is distributed in the hope that it will be useful,
0011 but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 GNU General Public License for more details.
0014 
0015 You should have received a copy of the GNU General Public License
0016 along with this program; if not, write to the Free Software
0017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0018 
0019 */
0020 // Local
0021 #include "semanticinfobackendtest.h"
0022 
0023 // Qt
0024 #include <QSignalSpy>
0025 #include <QTemporaryFile>
0026 #include <QTest>
0027 
0028 // KF
0029 #include <KRandom>
0030 
0031 // Local
0032 #include "testutils.h"
0033 #include <config-gwenview.h>
0034 
0035 #ifdef GWENVIEW_SEMANTICINFO_BACKEND_FAKE
0036 #include <lib/semanticinfo/fakesemanticinfobackend.h>
0037 
0038 #elif defined(GWENVIEW_SEMANTICINFO_BACKEND_BALOO)
0039 #include <lib/semanticinfo/baloosemanticinfobackend.h>
0040 
0041 #else
0042 #ifdef __GNUC__
0043 #error No metadata backend defined
0044 #endif
0045 #endif
0046 
0047 QTEST_MAIN(Gwenview::SemanticInfoBackEndTest)
0048 
0049 namespace Gwenview
0050 {
0051 SemanticInfoBackEndClient::SemanticInfoBackEndClient(AbstractSemanticInfoBackEnd *backEnd)
0052     : mBackEnd(backEnd)
0053 {
0054     connect(backEnd, SIGNAL(semanticInfoRetrieved(QUrl, SemanticInfo)), SLOT(slotSemanticInfoRetrieved(QUrl, SemanticInfo)));
0055 }
0056 
0057 void SemanticInfoBackEndClient::slotSemanticInfoRetrieved(const QUrl &url, const SemanticInfo &semanticInfo)
0058 {
0059     mSemanticInfoForUrl[url] = semanticInfo;
0060 }
0061 
0062 void SemanticInfoBackEndTest::initTestCase()
0063 {
0064     qRegisterMetaType<QUrl>("QUrl");
0065     qRegisterMetaType<QString>("SemanticInfoTag");
0066 }
0067 
0068 void SemanticInfoBackEndTest::init()
0069 {
0070 #ifdef GWENVIEW_SEMANTICINFO_BACKEND_FAKE
0071     mBackEnd = new FakeSemanticInfoBackEnd(nullptr, FakeSemanticInfoBackEnd::InitializeEmpty);
0072 #elif defined(GWENVIEW_SEMANTICINFO_BACKEND_BALOO)
0073     mBackEnd = new BalooSemanticInfoBackend(nullptr);
0074 #endif
0075 }
0076 
0077 void SemanticInfoBackEndTest::cleanup()
0078 {
0079     delete mBackEnd;
0080     mBackEnd = nullptr;
0081 }
0082 
0083 /**
0084  * Get and set the rating of a temp file
0085  */
0086 void SemanticInfoBackEndTest::testRating()
0087 {
0088     QTemporaryFile temp("XXXXXX.metadatabackendtest");
0089     QVERIFY(temp.open());
0090 
0091     QUrl url;
0092     url.setPath(temp.fileName());
0093 
0094     SemanticInfoBackEndClient client(mBackEnd);
0095     QSignalSpy spy(mBackEnd, SIGNAL(semanticInfoRetrieved(QUrl, SemanticInfo)));
0096     mBackEnd->retrieveSemanticInfo(url);
0097     QVERIFY(waitForSignal(spy));
0098 
0099     SemanticInfo semanticInfo = client.semanticInfoForUrl(url);
0100     QCOMPARE(semanticInfo.mRating, 0);
0101 
0102     semanticInfo.mRating = 5;
0103     mBackEnd->storeSemanticInfo(url, semanticInfo);
0104 }
0105 
0106 #if 0
0107 // Disabled because Baloo does not work like Nepomuk: it does not create tags
0108 // independently of files.
0109 void SemanticInfoBackEndTest::testTagForLabel()
0110 {
0111     QSignalSpy spy(mBackEnd, SIGNAL(tagAdded(SemanticInfoTag,QString)));
0112 
0113     TagSet oldAllTags = mBackEnd->allTags();
0114     QString label = "testTagForLabel-" + KRandom::randomString(5);
0115     SemanticInfoTag tag1 = mBackEnd->tagForLabel(label);
0116     QVERIFY(!tag1.isEmpty());
0117     QVERIFY(!oldAllTags.contains(tag1));
0118     QVERIFY(mBackEnd->allTags().contains(tag1));
0119 
0120     // This is a new tag, we should receive a signal
0121     QCOMPARE(spy.count(), 1);
0122 
0123     SemanticInfoTag tag2 = mBackEnd->tagForLabel(label);
0124     QCOMPARE(tag1, tag2);
0125     // This is not a new tag, we should not receive a signal
0126     QCOMPARE(spy.count(), 1);
0127 
0128     QString label2 = mBackEnd->labelForTag(tag2);
0129     QCOMPARE(label, label2);
0130 }
0131 #endif
0132 
0133 } // namespace
0134 
0135 #include "moc_semanticinfobackendtest.cpp"