Small refactoring and added test
[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 <QList>
30 #include <QTime>
31 #include "../../src/base/backbone/backbone.h"
32 #include "CommonDictInterfaceMock.h"
33
34 class BackboneTest : public QObject
35 {
36     Q_OBJECT
37
38     QList<CommonDictInterface*> dict;
39     int total;
40     Backbone* back;
41     void addDicts();
42
43 public:
44     BackboneTest();
45
46 private Q_SLOTS:
47     void init();
48     void cleanup();
49     void addDictionaryTest();
50     void removeDictionaryTest();
51     void selectedDictionaryTest();
52     void stopSearchingTest();
53     void searchTest();
54     void translationTest();
55     void multiTranslationTest();
56     void quitTest();
57 };
58
59 BackboneTest::BackboneTest()
60 {
61     total = 5;
62     for(int i = 0; i < total; i++)
63         dict.push_back(new CommonDictInterfaceMock());
64 }
65
66
67
68 void BackboneTest::init()
69 {
70     for(int i = 0; i < total; i++) {
71         dict[i] = new CommonDictInterfaceMock();
72         CommonDictInterfaceMock * cd = (CommonDictInterfaceMock*) dict[i];
73         cd->tov =   QString("to%1").arg(i);
74         cd->fromv = QString("from%1").arg(i);
75         cd->namev = QString("name%1").arg(i);
76         cd->typev = QString("type%1").arg(i);
77     }
78     back = new Backbone("vim");
79     addDicts();
80 }
81
82 void BackboneTest::addDicts() {
83     for(int i = 0; i < total; i++) {
84         back->addDictionary(dict[i]);
85     }
86 }
87
88
89
90 void BackboneTest::cleanup()
91 {
92     delete back;
93 }
94
95
96
97
98 void BackboneTest::addDictionaryTest()
99 {
100     QCOMPARE(back->getDictionaries().size(), total);
101     for(int i = 0; i < total; i++)
102         QCOMPARE(back->getDictionaries().keys().contains(dict[i]), QBool(true));
103 }
104
105 void BackboneTest::removeDictionaryTest() {
106     for(int i = 0; i < total-1; i++)
107         back->removeDictionary(dict[i]);
108
109     QVERIFY2(back->getDictionaries().contains(dict[total-1]) == 1,
110              "Deleted wrong dictionaries");
111
112 }
113
114
115
116 void BackboneTest::selectedDictionaryTest() {
117     QList<CommonDictInterface* > selected;
118     back->selectedDictionaries(selected);
119     foreach(bool d, back->getDictionaries().values())
120         QCOMPARE(d, false);
121
122     selected << dict[0] << dict[4];
123
124     back->selectedDictionaries(selected);
125     foreach(CommonDictInterface* d, back->getDictionaries().keys())
126         if(selected.contains(d))
127             QCOMPARE(back->getDictionaries()[d], true);
128         else
129             QCOMPARE(back->getDictionaries()[d], false);
130 }
131
132
133
134
135 void BackboneTest::stopSearchingTest() {
136     for(int i = 0; i < total; i++) {
137         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*) dict[i];
138         m->stopped = 0;
139         back->addDictionary(dict[i]);
140     }
141
142     for(int i = 0; i < total; i++) {
143         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
144         QCOMPARE(m->stopped, 0);
145     }
146
147     back->stopSearching();
148
149     for(int i = 0; i < total; i++) {
150         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
151         QCOMPARE(m->stopped, 1);
152     }
153
154
155 }
156
157
158
159
160 void BackboneTest::searchTest() {
161     for(int i = 0; i < total; i++) {
162         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*) dict[i];
163         m->stopped = 1;
164         back->addDictionary(dict[i]);
165     }
166
167     for(int i = 0; i < total; i++) {
168         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
169         QCOMPARE(m->stopped, 1);
170     }
171
172     qDebug() << "main " << this->thread()->currentThreadId();
173     QStringList list;
174     list << "pigwa";
175
176     back->search(list);
177     usleep(2000);
178
179     for(int i = 0; i < total; i++) {
180         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
181         QCOMPARE(m->stopped, 0);
182     }
183 }
184
185
186
187
188 void BackboneTest::translationTest() {
189     QSignalSpy translatS(back, SIGNAL(ready()));
190     QVERIFY2 (translatS.isValid() == true, "ready() signal is invalid");
191
192
193     QTime time;
194     time.start();
195     QStringList list;
196     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::multiTranslationTest() {
209     QSignalSpy translatS(back, SIGNAL(ready()));
210     QVERIFY2 (translatS.isValid() == true, "ready() signal is invalid");
211
212
213     QTime time;
214     QStringList list;
215
216     list << "a" << "b" << "c";
217     back->search(list);
218     qDebug() << "Time for backbone.search: " << time.elapsed();
219     usleep(1000);
220     time.start();
221     back->translationReady();
222     qDebug() << "Time for backbone->translation: " << time.elapsed();
223
224     QVERIFY2(translatS.count() == 1, "Lost finall 'ready()' signal");
225     QVERIFY2(back->result().size() == total*2*3,
226              "Lost some of the translations");
227 }
228
229
230
231 void BackboneTest::quitTest() {
232     QSignalSpy translatS(back, SIGNAL(closeOk()));
233     for(int i = 0; i < total; i++) {
234         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*) dict[i];
235         m->stopped = 0;
236         back->addDictionary(dict[i]);
237     }
238
239     for(int i = 0; i < total; i++) {
240         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
241         QCOMPARE(m->stopped, 0);
242     }
243
244     back->quit();
245
246     for(int i = 0; i < total; i++) {
247         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
248         QCOMPARE(m->stopped, 1);
249     }
250     QVERIFY2(translatS.count() == 1, "Lost finall 'closeOk()' signal");
251 }
252
253
254 QTEST_APPLESS_MAIN(BackboneTest);
255
256 #include "tst_Backbone.moc"