~ubuntu-branches/ubuntu/oneiric/gwenview/oneiric-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
Gwenview: an image viewer
Copyright 2008 Aurélien Gâteau <agateau@kde.org>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/
// Local
#include "semanticinfobackendtest.moc"

// Qt
#include <QSignalSpy>

// KDE
#include <kdebug.h>
#include <krandom.h>
#include <ktemporaryfile.h>
#include <qtest_kde.h>

// Local
#include "testutils.h"
#include <config-gwenview.h>

#ifdef GWENVIEW_SEMANTICINFO_BACKEND_FAKE
#include <lib/semanticinfo/fakesemanticinfobackend.h>

#elif defined(GWENVIEW_SEMANTICINFO_BACKEND_NEPOMUK)
#include <nepomuk/resourcemanager.h>
#include <lib/semanticinfo/nepomuksemanticinfobackend.h>

#else
#ifdef __GNUC__
	#error No metadata backend defined
#endif
#endif

QTEST_KDEMAIN( Gwenview::SemanticInfoBackEndTest, GUI )

namespace Gwenview {


SemanticInfoBackEndClient::SemanticInfoBackEndClient(AbstractSemanticInfoBackEnd* backEnd)
: mBackEnd(backEnd) {
	connect(backEnd, SIGNAL(semanticInfoRetrieved(const KUrl&, const SemanticInfo&)),
		SLOT(slotSemanticInfoRetrieved(const KUrl&, const SemanticInfo&)) );
}


void SemanticInfoBackEndClient::slotSemanticInfoRetrieved(const KUrl& url, const SemanticInfo& semanticInfo) {
	mSemanticInfoForUrl[url] = semanticInfo;
}


void SemanticInfoBackEndTest::initTestCase() {
#ifdef GWENVIEW_SEMANTICINFO_BACKEND_NEPOMUK
	if (Nepomuk::ResourceManager::instance()->init() != 0) {
		QSKIP("This test needs Nepomuk", SkipAll);
	}
#endif
	qRegisterMetaType<KUrl>("KUrl");
	qRegisterMetaType<QString>("SemanticInfoTag");
}


void SemanticInfoBackEndTest::init() {
#ifdef GWENVIEW_SEMANTICINFO_BACKEND_FAKE
	mBackEnd = new FakeSemanticInfoBackEnd(0, FakeSemanticInfoBackEnd::InitializeEmpty);
#elif defined(GWENVIEW_SEMANTICINFO_BACKEND_NEPOMUK)
	mBackEnd = new NepomukSemanticInfoBackEnd(0);
#endif
}

void SemanticInfoBackEndTest::cleanup() {
	delete mBackEnd;
	mBackEnd = 0;
}


/**
 * Get and set the rating of a temp file
 */
void SemanticInfoBackEndTest::testRating() {
	KTemporaryFile temp;
	temp.setSuffix(".metadatabackendtest");
	QVERIFY(temp.open());

	KUrl url;
	url.setPath(temp.fileName());

	SemanticInfoBackEndClient client(mBackEnd);
	QSignalSpy spy(mBackEnd, SIGNAL(semanticInfoRetrieved(const KUrl&, const SemanticInfo&)) );
	mBackEnd->retrieveSemanticInfo(url);
	QVERIFY(waitForSignal(spy));

	SemanticInfo semanticInfo = client.semanticInfoForUrl(url);
	QCOMPARE(semanticInfo.mRating, 0);

	semanticInfo.mRating = 5;
	mBackEnd->storeSemanticInfo(url, semanticInfo);
}


void SemanticInfoBackEndTest::testTagForLabel() {
	QSignalSpy spy(mBackEnd, SIGNAL(tagAdded(const SemanticInfoTag&, const QString&)) );

	TagSet oldAllTags = mBackEnd->allTags();
	QString label = "testTagForLabel-" + KRandom::randomString(5);
	SemanticInfoTag tag1 = mBackEnd->tagForLabel(label);
	QVERIFY(!tag1.isEmpty());
	QVERIFY(!oldAllTags.contains(tag1));
	QVERIFY(mBackEnd->allTags().contains(tag1));

	// This is a new tag, we should receive a signal
	QCOMPARE(spy.count(), 1);

	SemanticInfoTag tag2 = mBackEnd->tagForLabel(label);
	QCOMPARE(tag1, tag2);
	// This is not a new tag, we should not receive a signal
	QCOMPARE(spy.count(), 1);

	QString label2 = mBackEnd->labelForTag(tag2);
	QCOMPARE(label, label2);
}


} // namespace