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