Added docstrings and changed file header (license + file doc)
[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 }
67
68
69
70 void BackboneTest::init()
71 {
72     for(int i = 0; i < total; i++) {
73         dict[i] = new CommonDictInterfaceMock();
74         CommonDictInterfaceMock * cd = (CommonDictInterfaceMock*) dict[i];
75         cd->tov =   QString("to%1").arg(i);
76         cd->fromv = QString("from%1").arg(i);
77         cd->namev = QString("name%1").arg(i);
78         cd->typev = QString("type%1").arg(i);
79     }
80     back = new Backbone("empty","empty", 1);
81     qDebug() << back->getDictionaries().size();
82     addDicts();
83 }
84
85 void BackboneTest::addDicts() {
86     for(int i = 0; i < total; i++) {
87         back->addDictionary(dict[i]);
88     }
89 }
90
91
92
93 void BackboneTest::cleanup()
94 {
95     delete back;
96 }
97
98
99
100
101 void BackboneTest::addDictionaryTest()
102 {
103     QCOMPARE(back->getDictionaries().size(), total);
104     for(int i = 0; i < total; i++)
105         QCOMPARE(back->getDictionaries().keys().contains(dict[i]), QBool(true));
106 }
107
108 void BackboneTest::removeDictionaryTest() {
109     for(int i = 0; i < total-1; i++)
110         back->removeDictionary(dict[i]);
111
112     QVERIFY2(back->getDictionaries().contains(dict[total-1]) == 1,
113              "Deleted wrong dictionaries");
114
115 }
116
117
118
119 void BackboneTest::selectedDictionaryTest() {
120     QList<CommonDictInterface* > selected;
121     back->selectedDictionaries(selected);
122     foreach(bool d, back->getDictionaries().values())
123         QCOMPARE(d, false);
124
125     selected << dict[0] << dict[4];
126
127     back->selectedDictionaries(selected);
128     foreach(CommonDictInterface* d, back->getDictionaries().keys())
129         if(selected.contains(d))
130             QCOMPARE(back->getDictionaries()[d], true);
131         else
132             QCOMPARE(back->getDictionaries()[d], false);
133 }
134
135
136
137
138 void BackboneTest::stopSearchingTest() {
139     for(int i = 0; i < total; i++) {
140         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*) dict[i];
141         m->stopped = 0;
142         back->addDictionary(dict[i]);
143     }
144
145     for(int i = 0; i < total; i++) {
146         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
147         QCOMPARE(m->stopped, 0);
148     }
149
150     back->stopSearching();
151
152     for(int i = 0; i < total; i++) {
153         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
154         QCOMPARE(m->stopped, 1);
155     }
156
157
158 }
159
160
161
162
163 void BackboneTest::searchTest() {
164     for(int i = 0; i < total; i++) {
165         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*) dict[i];
166         m->stopped = 1;
167         back->addDictionary(dict[i]);
168     }
169
170     for(int i = 0; i < total; i++) {
171         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
172         QCOMPARE(m->stopped, 1);
173     }
174
175     qDebug() << "main " << this->thread()->currentThreadId();
176     QString list("pigwa");
177
178     back->search(list);
179     usleep(2000);
180
181     for(int i = 0; i < total; i++) {
182         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
183         QCOMPARE(m->stopped, 0);
184     }
185 }
186
187
188
189
190 void BackboneTest::translationTest() {
191     QSignalSpy translatS(back, SIGNAL(ready()));
192     QVERIFY2 (translatS.isValid() == true, "ready() signal is invalid");
193
194
195     QTime time;
196     time.start();
197     QString list("nic");
198     back->search(list);
199     qDebug() << "Time for backbone.search: " << time.elapsed();
200     usleep(1000);
201     time.start();
202     back->translationReady();
203     qDebug() << "Time for backbone->translation: " << time.elapsed();
204
205     QVERIFY2(translatS.count() == 1, "Lost finall 'ready()' signal");
206     QVERIFY2(back->result().size() == total*2, "Lost some of the translations");
207 }
208
209 void BackboneTest::quitTest() {
210     QSignalSpy translatS(back, SIGNAL(closeOk()));
211     for(int i = 0; i < total; i++) {
212         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*) dict[i];
213         m->stopped = 0;
214         back->addDictionary(dict[i]);
215     }
216
217     for(int i = 0; i < total; i++) {
218         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
219         QCOMPARE(m->stopped, 0);
220     }
221
222     back->quit();
223
224     for(int i = 0; i < total; i++) {
225         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
226         QCOMPARE(m->stopped, 1);
227     }
228     QVERIFY2(translatS.count() == 1, "Lost finall 'closeOk()' signal");
229 }
230
231 void BackboneTest::historyTest() {
232     History* history = back->history();
233
234     history->add("mleko");
235
236     QCOMPARE(history->nextAvailable(), FALSE);
237     QCOMPARE(history->prevAvailable(), FALSE);
238     QCOMPARE(history->listAvailable(), TRUE);
239
240
241     history->add("szklanka");
242     QCOMPARE(history->nextAvailable(), FALSE);
243     QCOMPARE(history->prevAvailable(), TRUE);
244
245     history->previous();
246     QCOMPARE(history->nextAvailable(), TRUE);
247     QCOMPARE(history->prevAvailable(), FALSE);
248
249     history->add("pic");
250     QStringList words = history->list();
251     QCOMPARE(words[0], QString("pic"));
252     QCOMPARE(words[1], QString("mleko"));
253
254     history->add("qqq");
255
256     history->previous();
257     history->add("pic");
258
259     words = history->list();
260     QCOMPARE(words[0], QString("qqq"));
261     QCOMPARE(words[1], QString("pic"));
262     QCOMPARE(words[2], QString("mleko"));
263
264     history->setCurrentElement(0);
265     QCOMPARE(history->nextAvailable(), FALSE);
266     QCOMPARE(history->prevAvailable(), TRUE);
267     QCOMPARE(history->listAvailable(), TRUE);
268
269     history->setCurrentElement(1);
270     QCOMPARE(history->nextAvailable(), TRUE);
271     QCOMPARE(history->prevAvailable(), TRUE);
272     QCOMPARE(history->listAvailable(), TRUE);
273
274     history->setMaxSize(1);
275     words = history->list();
276     QCOMPARE(words[0], QString("pic"));
277
278     QCOMPARE(history->nextAvailable(), FALSE);
279     QCOMPARE(history->prevAvailable(), FALSE);
280     QCOMPARE(history->listAvailable(), TRUE);
281 }
282
283
284 QTEST_APPLESS_MAIN(BackboneTest);
285
286 #include "tst_Backbone.moc"