further code cleanup
[mdictionary] / trunk / tests / mDictionaryTests / tst_Backbone.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
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.
9
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.
14
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/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21 /*! \file tst_backbone.cpp
22 \brief Backbone tests
23
24 \author Bartosz Szatkowski <bulislaw@linux.com>
25 */
26
27
28 #include <QtCore/QString>
29 #include <QtTest/QtTest>
30 #include <QStringList>
31 #include <QList>
32 #include <QTime>
33 #include "../../src/base/backbone/backbone.h"
34 #include "CommonDictInterfaceMock.h"
35
36 class BackboneTest : public QObject
37 {
38     Q_OBJECT
39
40     QList<CommonDictInterface*> dict;
41     int total;
42     Backbone* back;
43     void addDicts();
44
45 public:
46     BackboneTest();
47
48 private Q_SLOTS:
49     void init();
50     void cleanup();
51     void addDictionaryTest();
52     void removeDictionaryTest();
53     void selectedDictionaryTest();
54     void stopSearchingTest();
55     void searchTest();
56     void translationTest();
57     void historyTest();
58     void quitTest();
59 };
60
61 BackboneTest::BackboneTest()
62 {
63     total = 5;
64     for(int i = 0; i < total; i++)
65         dict.push_back(new CommonDictInterfaceMock());
66     QApplication app(0, 0);
67 }
68
69
70
71 void BackboneTest::init()
72 {
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);
80     }
81     back = new Backbone("empty","empty", 1);
82     qDebug() << back->getDictionaries().size();
83     addDicts();
84 }
85
86 void BackboneTest::addDicts() {
87     for(int i = 0; i < total; i++) {
88         back->addInternalDictionary(dict[i],true);
89     }
90 }
91
92
93
94 void BackboneTest::cleanup()
95 {
96     delete back;
97 }
98
99
100
101
102 void BackboneTest::addDictionaryTest()
103 {
104     QCOMPARE(back->getDictionaries().size(), total);
105     for(int i = 0; i < total; i++)
106         QCOMPARE(back->getDictionaries().keys().contains(dict[i]), QBool(true));
107 }
108
109 void BackboneTest::removeDictionaryTest() {
110     for(int i = 0; i < total-1; i++)
111         back->removeDictionary(dict[i]);
112
113     QVERIFY2(back->getDictionaries().contains(dict[total-1]) == 1,
114              "Deleted wrong dictionaries");
115
116 }
117
118
119
120 void BackboneTest::selectedDictionaryTest() {
121     QList<CommonDictInterface* > selected;
122     back->selectedDictionaries(selected);
123     foreach(bool d, back->getDictionaries().values())
124         QCOMPARE(d, false);
125
126     selected << dict[0] << dict[4];
127
128     back->selectedDictionaries(selected);
129     foreach(CommonDictInterface* d, back->getDictionaries().keys())
130         if(selected.contains(d))
131             QCOMPARE(back->getDictionaries()[d], true);
132         else
133             QCOMPARE(back->getDictionaries()[d], false);
134 }
135
136
137
138
139 void BackboneTest::stopSearchingTest() {
140     for(int i = 0; i < total; i++) {
141         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*) dict[i];
142         m->stopped = 0;
143         back->addDictionary(dict[i]);
144     }
145
146     for(int i = 0; i < total; i++) {
147         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
148         QCOMPARE(m->stopped, 0);
149     }
150
151     back->stopSearching();
152
153     for(int i = 0; i < total; i++) {
154         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
155         QCOMPARE(m->stopped, 1);
156     }
157
158
159 }
160
161
162
163
164 void BackboneTest::searchTest() {
165     for(int i = 0; i < total; i++) {
166         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*) dict[i];
167         m->stopped = 1;
168         back->addDictionary(dict[i]);
169     }
170
171     for(int i = 0; i < total; i++) {
172         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
173         QCOMPARE(m->stopped, 1);
174     }
175
176     qDebug() << "main search " << this->thread()->currentThreadId();
177     QString list("pigwa");
178     back->_searchBookmarks = 0;
179     back->_searchDicts = 1;
180
181     back->search(list);
182     qDebug() << back->activeDicts().size() << "!!!!!!!!!!!!!";
183
184
185     for(int i = 0; i < total; i++) {
186         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
187         QCOMPARE(m->stopped, 0);
188     }
189 }
190
191
192
193
194 void BackboneTest::translationTest() {
195     QSignalSpy translatS(back, SIGNAL(ready()));
196     QVERIFY2 (translatS.isValid() == true, "ready() signal is invalid");
197
198
199     QTime time;
200     time.start();
201     QString list("nic");
202     back->search(list);
203     qDebug() << "Time for backbone.search: " << time.elapsed();
204     usleep(1000);
205     time.start();
206     back->translationReady();
207     qDebug() << "Time for backbone->translation: " << time.elapsed();
208
209     QVERIFY2(translatS.count() == 1, "Lost finall 'ready()' signal");
210     QVERIFY2(back->result().size() == total*2, "Lost some of the translations");
211 }
212
213 void BackboneTest::quitTest() {
214     QSignalSpy translatS(back, SIGNAL(closeOk()));
215     for(int i = 0; i < total; i++) {
216         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*) dict[i];
217         m->stopped = 0;
218         back->addDictionary(dict[i]);
219     }
220
221     for(int i = 0; i < total; i++) {
222         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
223         QCOMPARE(m->stopped, 0);
224     }
225
226     back->quit();
227
228     for(int i = 0; i < total; i++) {
229         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
230         QCOMPARE(m->stopped, 1);
231     }
232     QVERIFY2(translatS.count() == 1, "Lost finall 'closeOk()' signal");
233 }
234
235 void BackboneTest::historyTest() {
236     History* history = back->history();
237
238     history->add("mleko");
239
240     QCOMPARE(history->nextAvailable(), FALSE);
241     QCOMPARE(history->prevAvailable(), FALSE);
242     QCOMPARE(history->listAvailable(), TRUE);
243
244
245     history->add("szklanka");
246     QCOMPARE(history->nextAvailable(), FALSE);
247     QCOMPARE(history->prevAvailable(), TRUE);
248
249     history->previous();
250     QCOMPARE(history->nextAvailable(), TRUE);
251     QCOMPARE(history->prevAvailable(), FALSE);
252
253     history->add("pic");
254     QStringList words = history->list();
255     QCOMPARE(words[0], QString("pic"));
256     QCOMPARE(words[1], QString("mleko"));
257
258     history->add("qqq");
259
260     history->previous();
261     history->add("pic");
262
263     words = history->list();
264     QCOMPARE(words[0], QString("qqq"));
265     QCOMPARE(words[1], QString("pic"));
266     QCOMPARE(words[2], QString("mleko"));
267
268     history->setCurrentElement(0);
269     QCOMPARE(history->nextAvailable(), FALSE);
270     QCOMPARE(history->prevAvailable(), TRUE);
271     QCOMPARE(history->listAvailable(), TRUE);
272
273     history->setCurrentElement(1);
274     QCOMPARE(history->nextAvailable(), TRUE);
275     QCOMPARE(history->prevAvailable(), TRUE);
276     QCOMPARE(history->listAvailable(), TRUE);
277
278     history->setMaxSize(1);
279     words = history->list();
280     QCOMPARE(words[0], QString("pic"));
281
282     QCOMPARE(history->nextAvailable(), FALSE);
283     QCOMPARE(history->prevAvailable(), FALSE);
284     QCOMPARE(history->listAvailable(), TRUE);
285 }
286
287
288 QTEST_APPLESS_MAIN(BackboneTest);
289
290 #include "tst_Backbone.moc"