~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to tests/semanticinfobackendtest.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
Import upstream version 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Gwenview: an image viewer
3
 
Copyright 2008 Aurélien Gâteau <agateau@kde.org>
4
 
 
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.
9
 
 
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.
14
 
 
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.
18
 
 
19
 
*/
20
 
// Local
21
 
#include "semanticinfobackendtest.moc"
22
 
 
23
 
// Qt
24
 
#include <QSignalSpy>
25
 
 
26
 
// KDE
27
 
#include <kdebug.h>
28
 
#include <krandom.h>
29
 
#include <ktemporaryfile.h>
30
 
#include <qtest_kde.h>
31
 
 
32
 
// Local
33
 
#include "testutils.h"
34
 
#include <config-gwenview.h>
35
 
 
36
 
#ifdef GWENVIEW_SEMANTICINFO_BACKEND_FAKE
37
 
#include <lib/semanticinfo/fakesemanticinfobackend.h>
38
 
 
39
 
#elif defined(GWENVIEW_SEMANTICINFO_BACKEND_NEPOMUK)
40
 
#include <nepomuk/resourcemanager.h>
41
 
#include <lib/semanticinfo/nepomuksemanticinfobackend.h>
42
 
 
43
 
#else
44
 
#ifdef __GNUC__
45
 
        #error No metadata backend defined
46
 
#endif
47
 
#endif
48
 
 
49
 
QTEST_KDEMAIN( Gwenview::SemanticInfoBackEndTest, GUI )
50
 
 
51
 
namespace Gwenview {
52
 
 
53
 
 
54
 
SemanticInfoBackEndClient::SemanticInfoBackEndClient(AbstractSemanticInfoBackEnd* backEnd)
55
 
: mBackEnd(backEnd) {
56
 
        connect(backEnd, SIGNAL(semanticInfoRetrieved(const KUrl&, const SemanticInfo&)),
57
 
                SLOT(slotSemanticInfoRetrieved(const KUrl&, const SemanticInfo&)) );
58
 
}
59
 
 
60
 
 
61
 
void SemanticInfoBackEndClient::slotSemanticInfoRetrieved(const KUrl& url, const SemanticInfo& semanticInfo) {
62
 
        mSemanticInfoForUrl[url] = semanticInfo;
63
 
}
64
 
 
65
 
 
66
 
void SemanticInfoBackEndTest::initTestCase() {
67
 
#ifdef GWENVIEW_SEMANTICINFO_BACKEND_NEPOMUK
68
 
        if (Nepomuk::ResourceManager::instance()->init() != 0) {
69
 
                QSKIP("This test needs Nepomuk", SkipAll);
70
 
        }
71
 
#endif
72
 
        qRegisterMetaType<KUrl>("KUrl");
73
 
        qRegisterMetaType<QString>("SemanticInfoTag");
74
 
}
75
 
 
76
 
 
77
 
void SemanticInfoBackEndTest::init() {
78
 
#ifdef GWENVIEW_SEMANTICINFO_BACKEND_FAKE
79
 
        mBackEnd = new FakeSemanticInfoBackEnd(0, FakeSemanticInfoBackEnd::InitializeEmpty);
80
 
#elif defined(GWENVIEW_SEMANTICINFO_BACKEND_NEPOMUK)
81
 
        mBackEnd = new NepomukSemanticInfoBackEnd(0);
82
 
#endif
83
 
}
84
 
 
85
 
void SemanticInfoBackEndTest::cleanup() {
86
 
        delete mBackEnd;
87
 
        mBackEnd = 0;
88
 
}
89
 
 
90
 
 
91
 
/**
92
 
 * Get and set the rating of a temp file
93
 
 */
94
 
void SemanticInfoBackEndTest::testRating() {
95
 
        KTemporaryFile temp;
96
 
        temp.setSuffix(".metadatabackendtest");
97
 
        QVERIFY(temp.open());
98
 
 
99
 
        KUrl url;
100
 
        url.setPath(temp.fileName());
101
 
 
102
 
        SemanticInfoBackEndClient client(mBackEnd);
103
 
        QSignalSpy spy(mBackEnd, SIGNAL(semanticInfoRetrieved(const KUrl&, const SemanticInfo&)) );
104
 
        mBackEnd->retrieveSemanticInfo(url);
105
 
        QVERIFY(waitForSignal(spy));
106
 
 
107
 
        SemanticInfo semanticInfo = client.semanticInfoForUrl(url);
108
 
        QCOMPARE(semanticInfo.mRating, 0);
109
 
 
110
 
        semanticInfo.mRating = 5;
111
 
        mBackEnd->storeSemanticInfo(url, semanticInfo);
112
 
}
113
 
 
114
 
 
115
 
void SemanticInfoBackEndTest::testTagForLabel() {
116
 
        QSignalSpy spy(mBackEnd, SIGNAL(tagAdded(const SemanticInfoTag&, const QString&)) );
117
 
 
118
 
        TagSet oldAllTags = mBackEnd->allTags();
119
 
        QString label = "testTagForLabel-" + KRandom::randomString(5);
120
 
        SemanticInfoTag tag1 = mBackEnd->tagForLabel(label);
121
 
        QVERIFY(!tag1.isEmpty());
122
 
        QVERIFY(!oldAllTags.contains(tag1));
123
 
        QVERIFY(mBackEnd->allTags().contains(tag1));
124
 
 
125
 
        // This is a new tag, we should receive a signal
126
 
        QCOMPARE(spy.count(), 1);
127
 
 
128
 
        SemanticInfoTag tag2 = mBackEnd->tagForLabel(label);
129
 
        QCOMPARE(tag1, tag2);
130
 
        // This is not a new tag, we should not receive a signal
131
 
        QCOMPARE(spy.count(), 1);
132
 
 
133
 
        QString label2 = mBackEnd->labelForTag(tag2);
134
 
        QCOMPARE(label, label2);
135
 
}
136
 
 
137
 
 
138
 
} // namespace