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 *******************************************************************************/
22 // Created by Bartosz Szatkowski
27 #include <QtCore/QString>
28 #include <QtTest/QtTest>
29 #include <QStringList>
32 #include "../../src/base/backbone/backbone.h"
33 #include "CommonDictInterfaceMock.h"
35 class BackboneTest : public QObject
39 QList<CommonDictInterface*> dict;
50 void addDictionaryTest();
51 void removeDictionaryTest();
52 void selectedDictionaryTest();
53 void stopSearchingTest();
55 void translationTest();
60 BackboneTest::BackboneTest()
63 for(int i = 0; i < total; i++)
64 dict.push_back(new CommonDictInterfaceMock());
69 void BackboneTest::init()
71 for(int i = 0; i < total; i++) {
72 dict[i] = new CommonDictInterfaceMock();
73 CommonDictInterfaceMock * cd = (CommonDictInterfaceMock*) dict[i];
74 cd->tov = QString("to%1").arg(i);
75 cd->fromv = QString("from%1").arg(i);
76 cd->namev = QString("name%1").arg(i);
77 cd->typev = QString("type%1").arg(i);
79 back = new Backbone("empty","empty");
80 qDebug() << back->getDictionaries().size();
84 void BackboneTest::addDicts() {
85 for(int i = 0; i < total; i++) {
86 back->addDictionary(dict[i]);
92 void BackboneTest::cleanup()
100 void BackboneTest::addDictionaryTest()
102 QCOMPARE(back->getDictionaries().size(), total);
103 for(int i = 0; i < total; i++)
104 QCOMPARE(back->getDictionaries().keys().contains(dict[i]), QBool(true));
107 void BackboneTest::removeDictionaryTest() {
108 for(int i = 0; i < total-1; i++)
109 back->removeDictionary(dict[i]);
111 QVERIFY2(back->getDictionaries().contains(dict[total-1]) == 1,
112 "Deleted wrong dictionaries");
118 void BackboneTest::selectedDictionaryTest() {
119 QList<CommonDictInterface* > selected;
120 back->selectedDictionaries(selected);
121 foreach(bool d, back->getDictionaries().values())
124 selected << dict[0] << dict[4];
126 back->selectedDictionaries(selected);
127 foreach(CommonDictInterface* d, back->getDictionaries().keys())
128 if(selected.contains(d))
129 QCOMPARE(back->getDictionaries()[d], true);
131 QCOMPARE(back->getDictionaries()[d], false);
137 void BackboneTest::stopSearchingTest() {
138 for(int i = 0; i < total; i++) {
139 CommonDictInterfaceMock *m = (CommonDictInterfaceMock*) dict[i];
141 back->addDictionary(dict[i]);
144 for(int i = 0; i < total; i++) {
145 CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
146 QCOMPARE(m->stopped, 0);
149 back->stopSearching();
151 for(int i = 0; i < total; i++) {
152 CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
153 QCOMPARE(m->stopped, 1);
162 void BackboneTest::searchTest() {
163 for(int i = 0; i < total; i++) {
164 CommonDictInterfaceMock *m = (CommonDictInterfaceMock*) dict[i];
166 back->addDictionary(dict[i]);
169 for(int i = 0; i < total; i++) {
170 CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
171 QCOMPARE(m->stopped, 1);
174 qDebug() << "main " << this->thread()->currentThreadId();
175 QString list("pigwa");
180 for(int i = 0; i < total; i++) {
181 CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
182 QCOMPARE(m->stopped, 0);
189 void BackboneTest::translationTest() {
190 QSignalSpy translatS(back, SIGNAL(ready()));
191 QVERIFY2 (translatS.isValid() == true, "ready() signal is invalid");
198 qDebug() << "Time for backbone.search: " << time.elapsed();
201 back->translationReady();
202 qDebug() << "Time for backbone->translation: " << time.elapsed();
204 QVERIFY2(translatS.count() == 1, "Lost finall 'ready()' signal");
205 QVERIFY2(back->result().size() == total*2, "Lost some of the translations");
208 void BackboneTest::quitTest() {
209 QSignalSpy translatS(back, SIGNAL(closeOk()));
210 for(int i = 0; i < total; i++) {
211 CommonDictInterfaceMock *m = (CommonDictInterfaceMock*) dict[i];
213 back->addDictionary(dict[i]);
216 for(int i = 0; i < total; i++) {
217 CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
218 QCOMPARE(m->stopped, 0);
223 for(int i = 0; i < total; i++) {
224 CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
225 QCOMPARE(m->stopped, 1);
227 QVERIFY2(translatS.count() == 1, "Lost finall 'closeOk()' signal");
230 void BackboneTest::historyTest() {
231 History* history = back->history();
233 history->add("mleko");
235 QCOMPARE(history->nextAvailable(), FALSE);
236 QCOMPARE(history->prevAvailable(), FALSE);
237 QCOMPARE(history->listAvailable(), TRUE);
240 history->add("szklanka");
241 QCOMPARE(history->nextAvailable(), FALSE);
242 QCOMPARE(history->prevAvailable(), TRUE);
245 QCOMPARE(history->nextAvailable(), TRUE);
246 QCOMPARE(history->prevAvailable(), FALSE);
249 QStringList words = history->list();
250 QCOMPARE(words[0], QString("pic"));
251 QCOMPARE(words[1], QString("mleko"));
258 words = history->list();
259 QCOMPARE(words[0], QString("qqq"));
260 QCOMPARE(words[1], QString("pic"));
261 QCOMPARE(words[2], QString("mleko"));
263 history->setCurrentElement(0);
264 QCOMPARE(history->nextAvailable(), FALSE);
265 QCOMPARE(history->prevAvailable(), TRUE);
266 QCOMPARE(history->listAvailable(), TRUE);
268 history->setCurrentElement(1);
269 QCOMPARE(history->nextAvailable(), TRUE);
270 QCOMPARE(history->prevAvailable(), TRUE);
271 QCOMPARE(history->listAvailable(), TRUE);
273 history->setMaxSize(1);
274 words = history->list();
275 QCOMPARE(words[0], QString("pic"));
277 QCOMPARE(history->nextAvailable(), FALSE);
278 QCOMPARE(history->prevAvailable(), FALSE);
279 QCOMPARE(history->listAvailable(), TRUE);
283 QTEST_APPLESS_MAIN(BackboneTest);
285 #include "tst_Backbone.moc"