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