1 /*******************************************************************************
3 This file is part of mDictionary.
5 mDictionary is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 mDictionary 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 mDictionary. If not, see <http://www.gnu.org/licenses/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
21 /*! \file tst_backbone.cpp
24 \author Bartosz Szatkowski <bulislaw@linux.com>
28 #include <QtCore/QString>
29 #include <QtTest/QtTest>
30 #include <QStringList>
33 #include "../../src/base/backbone/backbone.h"
34 #include "CommonDictInterfaceMock.h"
36 class BackboneTest : public QObject
40 QList<CommonDictInterface*> dict;
51 void addDictionaryTest();
52 void removeDictionaryTest();
53 void selectedDictionaryTest();
54 void stopSearchingTest();
56 void translationTest();
61 BackboneTest::BackboneTest()
64 for(int i = 0; i < total; i++)
65 dict.push_back(new CommonDictInterfaceMock());
66 QApplication app(0, 0);
71 void BackboneTest::init()
73 for(int i = 0; i < total; i++) {
74 dict[i] = new CommonDictInterfaceMock();
75 CommonDictInterfaceMock * cd = (CommonDictInterfaceMock*) dict[i];
76 cd->tov = QString("to%1").arg(i);
77 cd->fromv = QString("from%1").arg(i);
78 cd->namev = QString("name%1").arg(i);
79 cd->typev = QString("type%1").arg(i);
81 back = new Backbone("empty","empty", 1);
85 void BackboneTest::addDicts() {
86 for(int i = 0; i < total; i++) {
87 back->addInternalDictionary(dict[i],true);
93 void BackboneTest::cleanup()
101 void BackboneTest::addDictionaryTest()
103 QCOMPARE(back->getDictionaries().size(), total);
104 for(int i = 0; i < total; i++)
105 QCOMPARE(back->getDictionaries().keys().contains(dict[i]), QBool(true));
108 void BackboneTest::removeDictionaryTest() {
109 for(int i = 0; i < total-1; i++)
110 back->removeDictionary(dict[i]);
112 QVERIFY2(back->getDictionaries().contains(dict[total-1]) == 1,
113 "Deleted wrong dictionaries");
119 void BackboneTest::selectedDictionaryTest() {
120 QList<CommonDictInterface* > selected;
121 back->selectedDictionaries(selected);
122 foreach(bool d, back->getDictionaries().values())
125 selected << dict[0] << dict[4];
127 back->selectedDictionaries(selected);
128 foreach(CommonDictInterface* d, back->getDictionaries().keys())
129 if(selected.contains(d))
130 QCOMPARE(back->getDictionaries()[d], true);
132 QCOMPARE(back->getDictionaries()[d], false);
138 void BackboneTest::stopSearchingTest() {
139 for(int i = 0; i < total; i++) {
140 CommonDictInterfaceMock *m = (CommonDictInterfaceMock*) dict[i];
142 back->addDictionary(dict[i]);
145 for(int i = 0; i < total; i++) {
146 CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
147 QCOMPARE(m->stopped, 0);
151 back->stopSearching();
154 for(int i = 0; i < total; i++) {
155 CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
156 QCOMPARE(m->stopped, 1);
165 void BackboneTest::searchTest() {
166 for(int i = 0; i < total; i++) {
167 CommonDictInterfaceMock *m = (CommonDictInterfaceMock*) dict[i];
169 back->addDictionary(dict[i]);
172 for(int i = 0; i < total; i++) {
173 CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
174 QCOMPARE(m->stopped, 1);
177 QString list("pigwa");
178 back->_searchBookmarks = 0;
179 back->_searchDicts = 1;
185 for(int i = 0; i < total; i++) {
186 CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
187 QCOMPARE(m->stopped, 0);
194 void BackboneTest::translationTest() {
195 QSignalSpy translatS(back, SIGNAL(ready()));
196 QVERIFY2 (translatS.isValid() == true, "ready() signal is invalid");
202 back->_searchBookmarks = 0;
203 back->_searchDicts = 1;
206 qDebug() << back->result().size() << total;
207 back->translationReady();
208 qDebug() << back->result().size() << total;
210 QVERIFY2(back->result().size() == total*2, "Lost some of the translations");
213 void BackboneTest::quitTest() {
214 QSignalSpy translatS(back, SIGNAL(closeOk()));
215 for(int i = 0; i < total; i++) {
216 CommonDictInterfaceMock *m = (CommonDictInterfaceMock*) dict[i];
218 back->addDictionary(dict[i]);
221 for(int i = 0; i < total; i++) {
222 CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
223 QCOMPARE(m->stopped, 0);
229 for(int i = 0; i < total; i++) {
230 CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
231 QCOMPARE(m->stopped, 1);
233 QVERIFY2(translatS.count() == 1, "Lost finall 'closeOk()' signal");
236 void BackboneTest::historyTest() {
237 History* history = new History(10,this);
239 history->add("mleko");
241 QCOMPARE(history->nextAvailable(), FALSE);
242 QCOMPARE(history->prevAvailable(), FALSE);
243 QCOMPARE(history->listAvailable(), TRUE);
246 history->add("szklanka");
247 QCOMPARE(history->nextAvailable(), FALSE);
248 QCOMPARE(history->prevAvailable(), TRUE);
251 QCOMPARE(history->nextAvailable(), TRUE);
252 QCOMPARE(history->prevAvailable(), FALSE);
255 QStringList words = history->list();
256 QCOMPARE(words[0], QString("pic"));
257 QCOMPARE(words[1], QString("mleko"));
264 words = history->list();
265 QCOMPARE(words[0], QString("qqq"));
266 QCOMPARE(words[1], QString("pic"));
267 QCOMPARE(words[2], QString("mleko"));
269 history->setCurrentElement(0);
270 QCOMPARE(history->nextAvailable(), FALSE);
271 QCOMPARE(history->prevAvailable(), TRUE);
272 QCOMPARE(history->listAvailable(), TRUE);
274 history->setCurrentElement(1);
275 QCOMPARE(history->nextAvailable(), TRUE);
276 QCOMPARE(history->prevAvailable(), TRUE);
277 QCOMPARE(history->listAvailable(), TRUE);
279 history->setMaxSize(1);
280 words = history->list();
281 QCOMPARE(words[0], QString("pic"));
283 QCOMPARE(history->nextAvailable(), FALSE);
284 QCOMPARE(history->prevAvailable(), FALSE);
285 QCOMPARE(history->listAvailable(), TRUE);
289 QTEST_APPLESS_MAIN(BackboneTest);
291 #include "tst_Backbone.moc"