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