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