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