Merged with master
[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 quitTest();
56 };
57
58 BackboneTest::BackboneTest()
59 {
60     total = 5;
61     for(int i = 0; i < total; i++)
62         dict.push_back(new CommonDictInterfaceMock());
63 }
64
65
66
67 void BackboneTest::init()
68 {
69     for(int i = 0; i < total; i++) {
70         dict[i] = new CommonDictInterfaceMock();
71         CommonDictInterfaceMock * cd = (CommonDictInterfaceMock*) dict[i];
72         cd->tov = "to" + QString(i);
73         cd->fromv = "from" + QString(i);
74         cd->namev = "name" + QString(i);
75         cd->typev = "type" + QString(i);
76     }
77     back = new Backbone("vim");
78 }
79
80 void BackboneTest::addDicts() {
81     for(int i = 0; i < total; i++)
82         back->addDictionary(dict[i]);
83 }
84
85
86
87 void BackboneTest::cleanup()
88 {
89     delete back;
90 }
91
92
93
94 void BackboneTest::addDictionaryTest()
95 {
96     addDicts();
97     QCOMPARE(back->getDictionaries().size(), total);
98     for(int i = 0; i < total; i++)
99         QCOMPARE(back->getDictionaries().keys().contains(dict[i]), QBool(true));
100 }
101
102 void BackboneTest::removeDictionaryTest() {
103     addDicts();
104     for(int i = 0; i < total-1; i++)
105         back->removeDictionary(dict[i]);
106
107     QVERIFY2(back->getDictionaries().contains(dict[total-1]) == 1,
108              "Deleted wrong dictionaries");
109
110 }
111
112
113
114 void BackboneTest::selectedDictionaryTest() {
115     addDicts();
116     QList<CommonDictInterface* > selected;
117     back->selectedDictionaries(selected);
118     foreach(bool d, back->getDictionaries().values())
119         QCOMPARE(d, false);
120
121     selected << dict[0] << dict[4];
122
123     back->selectedDictionaries(selected);
124     foreach(CommonDictInterface* d, back->getDictionaries().keys())
125         if(selected.contains(d))
126             QCOMPARE(back->getDictionaries()[d], true);
127         else
128             QCOMPARE(back->getDictionaries()[d], false);
129 }
130
131
132
133
134 void BackboneTest::stopSearchingTest() {
135     for(int i = 0; i < total; i++) {
136         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*) dict[i];
137         m->stopped = 0;
138         back->addDictionary(dict[i]);
139     }
140
141     for(int i = 0; i < total; i++) {
142         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
143         QCOMPARE(m->stopped, 0);
144     }
145
146     back->stopSearching();
147
148     for(int i = 0; i < total; i++) {
149         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
150         QCOMPARE(m->stopped, 1);
151     }
152
153
154 }
155
156
157
158
159 void BackboneTest::searchTest() {
160     for(int i = 0; i < total; i++) {
161         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*) dict[i];
162         m->stopped = 1;
163         back->addDictionary(dict[i]);
164     }
165
166     for(int i = 0; i < total; i++) {
167         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
168         QCOMPARE(m->stopped, 1);
169     }
170
171     qDebug() << "main " << this->thread()->currentThreadId();
172     back->search("pigwa");
173     usleep(2000);
174
175     for(int i = 0; i < total; i++) {
176         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
177         QCOMPARE(m->stopped, 0);
178     }
179 }
180
181
182
183
184 void BackboneTest::translationTest() {
185     QSignalSpy translatS(back, SIGNAL(ready()));
186     QVERIFY2 (translatS.isValid() == true, "ready() signal is invalid");
187
188     addDicts();
189
190     QTime time;
191     time.start();
192     back->search("nic");
193     qDebug() << "Time for backbone.search: " << time.elapsed();
194     usleep(2000);
195     time.start();
196     back->translation();
197     qDebug() << "Time for backbone->translation: " << time.elapsed();
198
199     QVERIFY2(translatS.count() == 1, "Lost finall 'ready()' signal");
200     QVERIFY2(back->result().size() == total*2, "Lost some of the translations");
201 }
202
203 void BackboneTest::quitTest() {
204     QSignalSpy translatS(back, SIGNAL(closeOk()));
205     for(int i = 0; i < total; i++) {
206         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*) dict[i];
207         m->stopped = 0;
208         back->addDictionary(dict[i]);
209     }
210
211     for(int i = 0; i < total; i++) {
212         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
213         QCOMPARE(m->stopped, 0);
214     }
215
216     back->quit();
217
218     for(int i = 0; i < total; i++) {
219         CommonDictInterfaceMock *m = (CommonDictInterfaceMock*)dict[i];
220         QCOMPARE(m->stopped, 1);
221     }
222     QVERIFY2(translatS.count() == 1, "Lost finall 'closeOk()' signal");
223 }
224
225
226 QTEST_APPLESS_MAIN(BackboneTest);
227
228 #include "tst_Backbone.moc"