##// END OF EJS Templates
added test for overload of copy constructor and double constructor decorators...
florianlink -
r42:2e28b78885ae
parent child
Show More
@@ -1,457 +1,465
1 1 /*
2 2 *
3 3 * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.
4 4 *
5 5 * This library is free software; you can redistribute it and/or
6 6 * modify it under the terms of the GNU Lesser General Public
7 7 * License as published by the Free Software Foundation; either
8 8 * version 2.1 of the License, or (at your option) any later version.
9 9 *
10 10 * This library is distributed in the hope that it will be useful,
11 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 13 * Lesser General Public License for more details.
14 14 *
15 15 * Further, this software is distributed without any warranty that it is
16 16 * free of the rightful claim of any third person regarding infringement
17 17 * or the like. Any license provided herein, whether implied or
18 18 * otherwise, applies only to this software file. Patent licenses, if
19 19 * any, provided herein do not apply to combinations of this program with
20 20 * other software, or any other product whatsoever.
21 21 *
22 22 * You should have received a copy of the GNU Lesser General Public
23 23 * License along with this library; if not, write to the Free Software
24 24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 25 *
26 26 * Contact information: MeVis Research GmbH, Universitaetsallee 29,
27 27 * 28359 Bremen, Germany or:
28 28 *
29 29 * http://www.mevis.de
30 30 *
31 31 */
32 32
33 33 //----------------------------------------------------------------------------------
34 34 /*!
35 35 // \file PythonQtTests.cpp
36 36 // \author Florian Link
37 37 // \author Last changed by $Author: florian $
38 38 // \date 2006-05
39 39 */
40 40 //----------------------------------------------------------------------------------
41 41
42 42 #include "PythonQtTests.h"
43 43
44 44 void PythonQtTestSlotCalling::initTestCase()
45 45 {
46 46 _helper = new PythonQtTestSlotCallingHelper(this);
47 47 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
48 48 main.evalScript("import PythonQt");
49 49 PythonQt::self()->addObject(main, "obj", _helper);
50 50 }
51 51
52 52 void PythonQtTestSlotCalling::init() {
53 53
54 54 }
55 55
56 56 void* polymorphic_ClassB_Handler(const void* ptr, char** className) {
57 57 ClassB* o = (ClassB*)ptr;
58 58 if (o->type()==2) {
59 59 *className = "ClassB";
60 60 return (ClassB*)o;
61 61 }
62 62 if (o->type()==3) {
63 63 *className = "ClassC";
64 64 return (ClassC*)o;
65 65 }
66 66 if (o->type()==4) {
67 67 *className = "ClassD";
68 68 return (ClassD*)o;
69 69 }
70 70 return NULL;
71 71 }
72 72
73 73 void PythonQtTestSlotCalling::testInheritance() {
74 74 PythonQt::self()->registerCPPClass("ClassA",NULL,NULL, PythonQtCreateObject<ClassAWrapper>);
75 75 PythonQt::self()->registerCPPClass("ClassB",NULL,NULL, PythonQtCreateObject<ClassBWrapper>);
76 76 PythonQt::self()->registerCPPClass("ClassC",NULL,NULL, PythonQtCreateObject<ClassCWrapper>);
77 77 PythonQt::self()->addParentClass("ClassC", "ClassA", PythonQtUpcastingOffset<ClassC,ClassA>());
78 78 PythonQt::self()->addParentClass("ClassC", "ClassB", PythonQtUpcastingOffset<ClassC,ClassB>());
79 79 PythonQt::self()->registerClass(&ClassD::staticMetaObject, NULL, PythonQtCreateObject<ClassDWrapper>);
80 80 PythonQt::self()->addParentClass("ClassD", "ClassA", PythonQtUpcastingOffset<ClassD,ClassA>());
81 81 PythonQt::self()->addParentClass("ClassD", "ClassB", PythonQtUpcastingOffset<ClassD,ClassB>());
82 82
83 83 PythonQtObjectPtr classA = PythonQt::self()->getMainModule().getVariable("PythonQt.ClassA");
84 84 PythonQtObjectPtr classB = PythonQt::self()->getMainModule().getVariable("PythonQt.ClassB");
85 85 PythonQtObjectPtr classC = PythonQt::self()->getMainModule().getVariable("PythonQt.ClassC");
86 86 PythonQtObjectPtr classD = PythonQt::self()->getMainModule().getVariable("PythonQt.ClassD");
87 87 QVERIFY(classA);
88 88 QVERIFY(classB);
89 89 QVERIFY(classC);
90 90 QVERIFY(classD);
91 91
92 92 QVERIFY(_helper->runScript("a = PythonQt.ClassA();\nif obj.getClassAPtr(a).getX()==1: obj.setPassed();\n"));
93 93 QEXPECT_FAIL("", "ClassB can not be converted to ClassA", Continue);
94 94 QVERIFY(_helper->runScript("a = PythonQt.ClassB();\nif obj.getClassAPtr(a).getX()==1: obj.setPassed();\n"));
95 95 QVERIFY(_helper->runScript("a = PythonQt.ClassC();\nif obj.getClassAPtr(a).getX()==1: obj.setPassed();\n"));
96 96 QVERIFY(_helper->runScript("a = PythonQt.ClassD();\nif obj.getClassAPtr(a).getX()==1: obj.setPassed();\n"));
97 97
98 98 QEXPECT_FAIL("", "ClassA can not be converted to ClassB", Continue);
99 99 QVERIFY(_helper->runScript("a = PythonQt.ClassA();\nif obj.getClassBPtr(a).getY()==2: obj.setPassed();\n"));
100 100 QVERIFY(_helper->runScript("a = PythonQt.ClassB();\nif obj.getClassBPtr(a).getY()==2: obj.setPassed();\n"));
101 101 QVERIFY(_helper->runScript("a = PythonQt.ClassC();\nif obj.getClassBPtr(a).getY()==2: obj.setPassed();\n"));
102 102 QVERIFY(_helper->runScript("a = PythonQt.ClassD();\nif obj.getClassBPtr(a).getY()==2: obj.setPassed();\n"));
103 103
104 104 QEXPECT_FAIL("", "ClassA can not be converted to ClassC", Continue);
105 105 QVERIFY(_helper->runScript("a = PythonQt.ClassA();\nif obj.getClassCPtr(a).getX()==1: obj.setPassed();\n"));
106 106 QEXPECT_FAIL("", "ClassB can not be converted to ClassC", Continue);
107 107 QVERIFY(_helper->runScript("a = PythonQt.ClassB();\nif obj.getClassCPtr(a).getX()==1: obj.setPassed();\n"));
108 108 QVERIFY(_helper->runScript("a = PythonQt.ClassC();\nif obj.getClassCPtr(a).getX()==1: obj.setPassed();\n"));
109 109 QEXPECT_FAIL("", "ClassD can not be converted to ClassC", Continue);
110 110 QVERIFY(_helper->runScript("a = PythonQt.ClassD();\nif obj.getClassCPtr(a).getX()==1: obj.setPassed();\n"));
111 111
112 112 QVERIFY(_helper->runScript("if type(obj.createClassA())==PythonQt.ClassA: obj.setPassed();\n"));
113 113 QVERIFY(_helper->runScript("if type(obj.createClassB())==PythonQt.ClassB: obj.setPassed();\n"));
114 114 QVERIFY(_helper->runScript("if type(obj.createClassCAsA())==PythonQt.ClassA: obj.setPassed();\n"));
115 115 QVERIFY(_helper->runScript("if type(obj.createClassCAsB())==PythonQt.ClassB: obj.setPassed();\n"));
116 116 QVERIFY(_helper->runScript("if type(obj.createClassD())==PythonQt.ClassD: obj.setPassed();\n"));
117 117 QVERIFY(_helper->runScript("if type(obj.createClassDAsA())==PythonQt.ClassA: obj.setPassed();\n"));
118 118 QVERIFY(_helper->runScript("if type(obj.createClassDAsB())==PythonQt.ClassB: obj.setPassed();\n"));
119 119
120 120 PythonQt::self()->addPolymorphicHandler("ClassB", polymorphic_ClassB_Handler);
121 121
122 122 QVERIFY(_helper->runScript("if type(obj.getClassBPtr(obj.createClassB()))==PythonQt.ClassB: obj.setPassed();\n"));
123 123 QVERIFY(_helper->runScript("if type(obj.createClassCAsB())==PythonQt.ClassC: obj.setPassed();\n"));
124 124 QVERIFY(_helper->runScript("if type(obj.createClassDAsB())==PythonQt.ClassD: obj.setPassed();\n"));
125 125
126 126 }
127 127
128 128 void PythonQtTestSlotCalling::testNoArgSlotCall()
129 129 {
130 130 QVERIFY(_helper->runScript("obj.testNoArg(); obj.setPassed();\n"));
131 131 }
132 132
133 133 void PythonQtTestSlotCalling::testOverloadedCall()
134 134 {
135 135 QVERIFY(_helper->runScript("obj.overload(False); obj.setPassed();\n", 0));
136 136 QVERIFY(_helper->runScript("obj.overload(True); obj.setPassed();\n", 0));
137 137 QVERIFY(_helper->runScript("obj.overload(12.5); obj.setPassed();\n", 1));
138 138 QVERIFY(_helper->runScript("obj.overload(12); obj.setPassed();\n", 2));
139 139 QVERIFY(_helper->runScript("obj.overload('test'); obj.setPassed();\n", 3));
140 140 QVERIFY(_helper->runScript("obj.overload(u'test'); obj.setPassed();\n", 3));
141 141 QVERIFY(_helper->runScript("obj.overload(('test','test2')); obj.setPassed();\n", 4));
142 142 QVERIFY(_helper->runScript("obj.overload(obj); obj.setPassed();\n", 5));
143 143 QVERIFY(_helper->runScript("obj.overload(12,13); obj.setPassed();\n", 6));
144 144 }
145 145
146 146 void PythonQtTestSlotCalling::testPyObjectSlotCall()
147 147 {
148 148 QVERIFY(_helper->runScript("if obj.getPyObject(PythonQt)==PythonQt: obj.setPassed();\n"));
149 149 QVERIFY(_helper->runScript("if obj.getPyObject('Hello')=='Hello': obj.setPassed();\n"));
150 150 QVERIFY(_helper->runScript("if obj.getPyObjectFromVariant(PythonQt)==PythonQt: obj.setPassed();\n"));
151 151 QVERIFY(_helper->runScript("if obj.getPyObjectFromVariant2(PythonQt)==PythonQt: obj.setPassed();\n"));
152 152 // QVERIFY(_helper->runScript("if obj.getPyObjectFromPtr(PythonQt)==PythonQt: obj.setPassed();\n"));
153 153 }
154 154
155 155 void PythonQtTestSlotCalling::testCPPSlotCalls()
156 156 {
157 157 // test QColor compare operation
158 158 QVERIFY(_helper->runScript("if PythonQt.QtGui.QColor(1,2,3)==PythonQt.QtGui.QColor(1,2,3): obj.setPassed();obj.testNoArg()\n"));
159 159 QVERIFY(_helper->runScript("if PythonQt.QtGui.QColor(1,2,3)!=PythonQt.QtGui.QColor(3,2,1): obj.setPassed();obj.testNoArg()\n"));
160 160
161 161 // test passing/returning QColors
162 162 QVERIFY(_helper->runScript("if obj.getQColor1(PythonQt.QtGui.QColor(1,2,3))==PythonQt.QtGui.QColor(1,2,3): obj.setPassed();\n"));
163 163 QVERIFY(_helper->runScript("if obj.getQColor2(PythonQt.QtGui.QColor(1,2,3))==PythonQt.QtGui.QColor(1,2,3): obj.setPassed();\n"));
164 164 QVERIFY(_helper->runScript("if obj.getQColor3(PythonQt.QtGui.QColor(1,2,3))==PythonQt.QtGui.QColor(1,2,3): obj.setPassed();\n"));
165 165 QVERIFY(_helper->runScript("if obj.getQColor4(PythonQt.QtGui.QColor(1,2,3))==PythonQt.QtGui.QColor(1,2,3): obj.setPassed();\n"));
166 166 QVERIFY(_helper->runScript("if obj.getQColor5()==PythonQt.QtGui.QColor(1,2,3): obj.setPassed();\n"));
167 167 }
168 168
169 169 void PythonQtTestSlotCalling::testPODSlotCalls()
170 170 {
171 171 QVERIFY(_helper->runScript("if obj.getBool(False)==False: obj.setPassed();\n"));
172 172 QVERIFY(_helper->runScript("if obj.getBool(True)==True: obj.setPassed();\n"));
173 173 QVERIFY(_helper->runScript("if obj.getInt(-42)==-42: obj.setPassed();\n"));
174 174 QVERIFY(_helper->runScript("if obj.getUInt(42)==42: obj.setPassed();\n"));
175 175 QVERIFY(_helper->runScript("if obj.getShort(-43)==-43: obj.setPassed();\n"));
176 176 QVERIFY(_helper->runScript("if obj.getUShort(43)==43: obj.setPassed();\n"));
177 177 QVERIFY(_helper->runScript("if obj.getChar(-12)==-12: obj.setPassed();\n"));
178 178 QVERIFY(_helper->runScript("if obj.getUChar(12)==12: obj.setPassed();\n"));
179 179 QVERIFY(_helper->runScript("if obj.getLong(-256*256*256)==-256*256*256: obj.setPassed();\n"));
180 180 QVERIFY(_helper->runScript("if obj.getULong(256*256*256)==256*256*256: obj.setPassed();\n"));
181 181 QVERIFY(_helper->runScript("if obj.getLongLong(-42)==-42: obj.setPassed();\n"));
182 182 QVERIFY(_helper->runScript("if obj.getULongLong(42)==42: obj.setPassed();\n"));
183 183 QVERIFY(_helper->runScript("if obj.getQChar(4096)==4096: obj.setPassed();\n"));
184 184 QVERIFY(_helper->runScript("if obj.getDouble(47.12)==47.12: obj.setPassed();\n"));
185 185 QVERIFY(_helper->runScript("if abs(obj.getFloat(47.11)-47.11)<0.01: obj.setPassed();\n"));
186 186 QVERIFY(_helper->runScript("if obj.getQString('testStr')=='testStr': obj.setPassed();\n"));
187 187 QVERIFY(_helper->runScript("if obj.getQString('')=='': obj.setPassed();\n"));
188 188 QVERIFY(_helper->runScript("if obj.getQStringList(('test','test2'))==('test','test2'): obj.setPassed();\n"));
189 189 }
190 190
191 191 void PythonQtTestSlotCalling::testQVariantSlotCalls()
192 192 {
193 193 QVERIFY(_helper->runScript("if obj.getQVariant(-42)==-42: obj.setPassed();\n"));
194 194 QVERIFY(_helper->runScript("if obj.getQVariant('testStr')=='testStr': obj.setPassed();\n"));
195 195 QVERIFY(_helper->runScript("if obj.getQVariant(('test','test2'))==('test','test2'): obj.setPassed();\n"));
196 196 QVERIFY(_helper->runScript("if obj.getQVariant(('test',12, 47.11))==('test',12, 47.11): obj.setPassed();\n"));
197 197 QVERIFY(_helper->runScript("if obj.getQVariant({'test':'bla','test2':47.11})=={'test':'bla','test2':47.11}: obj.setPassed();\n"));
198 198 QEXPECT_FAIL("", "Testing to pass a map and compare with a different map", Continue);
199 199 QVERIFY(_helper->runScript("if obj.getQVariant({'test':'bla2','test2':47.11})=={'test':'bla','test2':47.11}: obj.setPassed();\n"));
200 200 QVERIFY(_helper->runScript("if obj.getQVariant(obj)==obj: obj.setPassed();\n"));
201 201 }
202 202
203 203 void PythonQtTestSlotCalling::testObjectSlotCalls()
204 204 {
205 205 QVERIFY(_helper->runScript("if obj.getQObject(obj)==obj: obj.setPassed();\n"));
206 206 QVERIFY(_helper->runScript("if obj.getTestObject(obj)==obj: obj.setPassed();\n"));
207 207 QVERIFY(_helper->runScript("if obj.getNewObject().className()=='PythonQtTestSlotCallingHelper': obj.setPassed();\n"));
208 208 QEXPECT_FAIL("", "Testing to pass a QObject when another object was expected", Continue);
209 209 QVERIFY(_helper->runScript("if obj.getQWidget(obj)==obj: obj.setPassed();\n"));
210 210 }
211 211
212 212 void PythonQtTestSlotCalling::testCppFactory()
213 213 {
214 214 PythonQtTestCppFactory* f = new PythonQtTestCppFactory;
215 215 PythonQt::self()->addInstanceDecorators(new PQCppObjectDecorator);
216 PythonQt::self()->addInstanceDecorators(new PQCppObjectNoWrapDecorator);
216 PythonQt::self()->addDecorators(new PQCppObjectNoWrapDecorator);
217 217
218 218 PythonQt::self()->addWrapperFactory(f);
219 219 QVERIFY(_helper->runScript("if obj.createPQCppObject(12).getHeight()==12: obj.setPassed();\n"));
220 220 QVERIFY(_helper->runScript("if obj.createPQCppObject(12).getH()==12: obj.setPassed();\n"));
221 221 QVERIFY(_helper->runScript("pq1 = obj.createPQCppObject(12);\n"
222 222 "pq2 = obj.createPQCppObject(13);\n"
223 223 "pq3 = obj.getPQCppObject(pq1);\n"
224 224 "pq4 = obj.getPQCppObject(pq2);\n"
225 225 "if pq3.getHeight()==12 and pq4.getHeight()==13: obj.setPassed();\n"
226 226 ));
227 227
228 228 QVERIFY(_helper->runScript("if obj.createPQCppObjectNoWrap(12).getH()==12: obj.setPassed();\n"));
229
230
231 // expect to get strict call to double overload
232 QVERIFY(_helper->runScript("obj.testNoArg()\nfrom PythonQt import PQCppObjectNoWrap\na = PQCppObjectNoWrap(22.2)\nif a.getH()==2: obj.setPassed();\n"));
233 // expect to get un-strict call to double overload
234 QVERIFY(_helper->runScript("obj.testNoArg()\nfrom PythonQt import PQCppObjectNoWrap\na = PQCppObjectNoWrap(22)\nif a.getH()==2: obj.setPassed();\n"));
235 // expect to get strict call to copy constructor overload
236 QVERIFY(_helper->runScript("obj.testNoArg()\nfrom PythonQt import PQCppObjectNoWrap\na = PQCppObjectNoWrap(PQCppObjectNoWrap())\nprint a.getH()\nif a.getH()==1: obj.setPassed();\n"));
229 237 }
230 238
231 239
232 240 void PythonQtTestSlotCalling::testMultiArgsSlotCall()
233 241 {
234 242 QVERIFY(_helper->runScript("if obj.getMultiArgs(12,47.11,'test')==(12,47.11,'test'): obj.setPassed();\n"));
235 243 }
236 244
237 245 bool PythonQtTestSlotCallingHelper::runScript(const char* script, int expectedOverload)
238 246 {
239 247 _called = false;
240 248 _passed = false;
241 249 _calledOverload = -1;
242 250 PyRun_SimpleString(script);
243 251 return _called && _passed && _calledOverload==expectedOverload;
244 252 }
245 253
246 254
247 255 void PythonQtTestSignalHandler::initTestCase()
248 256 {
249 257 _helper = new PythonQtTestSignalHandlerHelper(this);
250 258 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
251 259 PythonQt::self()->addObject(main, "obj", _helper);
252 260 }
253 261
254 262 void PythonQtTestSignalHandler::testSignalHandler()
255 263 {
256 264 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
257 265 PyRun_SimpleString("def testIntSignal(a):\n if a==12: obj.setPassed();\n");
258 266 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(intSignal(int)), main, "testIntSignal"));
259 267 QVERIFY(_helper->emitIntSignal(12));
260 268
261 269 PyRun_SimpleString("def testFloatSignal(a):\n if a==12: obj.setPassed();\n");
262 270 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(floatSignal(float)), main, "testFloatSignal"));
263 271 QVERIFY(_helper->emitFloatSignal(12));
264 272
265 273 PyRun_SimpleString("def testVariantSignal(a):\n if a==obj.expectedVariant(): obj.setPassed();\n");
266 274 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(variantSignal(QVariant)), main, "testVariantSignal"));
267 275 _helper->setExpectedVariant(QString("Test"));
268 276 QVERIFY(_helper->emitVariantSignal(QString("Test")));
269 277 _helper->setExpectedVariant(12);
270 278 QVERIFY(_helper->emitVariantSignal(12));
271 279 _helper->setExpectedVariant(QStringList() << "test1" << "test2");
272 280 QVERIFY(_helper->emitVariantSignal(QStringList() << "test1" << "test2"));
273 281 _helper->setExpectedVariant(qVariantFromValue((QObject*)_helper));
274 282 QVERIFY(_helper->emitVariantSignal(qVariantFromValue((QObject*)_helper)));
275 283
276 284 PyRun_SimpleString("def testComplexSignal(a,b,l,o):\n if a==12 and b==13 and l==('test1','test2') and o == obj: obj.setPassed();\n");
277 285 // intentionally not normalized signal:
278 286 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(complexSignal( int, float , const QStringList , QObject*)), main, "testComplexSignal"));
279 287 QVERIFY(_helper->emitComplexSignal(12,13,QStringList() << "test1" << "test2", _helper));
280 288
281 289 // try removing the handler
282 290 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(complexSignal( int, float , const QStringList , QObject*)), main, "testComplexSignal"));
283 291 // and emit the signal, which should fail because the handler was removed
284 292 QVERIFY(!_helper->emitComplexSignal(12,13,QStringList() << "test1" << "test2", _helper));
285 293
286 294 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(intSignal(int)), main, "testIntSignal"));
287 295 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(floatSignal(float)), main, "testFloatSignal"));
288 296 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(variantSignal(QVariant)), main, "testVariantSignal"));
289 297
290 298 }
291 299
292 300 void PythonQtTestSignalHandler::testRecursiveSignalHandler()
293 301 {
294 302 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
295 303 PyRun_SimpleString("def testSignal1(a):\n obj.emitSignal2(a);\n");
296 304 PyRun_SimpleString("def testSignal2(a):\n obj.emitSignal3(float(a));\n");
297 305 PyRun_SimpleString("def testSignal3(a):\n if a==12: obj.setPassed();\n");
298 306 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(signal1(int)), main, "testSignal1"));
299 307 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(signal2(const QString&)), main, "testSignal2"));
300 308 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(signal3(float)), main, "testSignal3"));
301 309 QVERIFY(_helper->emitSignal1(12));
302 310 }
303 311
304 312
305 313 void PythonQtTestApi::initTestCase()
306 314 {
307 315 _helper = new PythonQtTestApiHelper();
308 316 _main = PythonQt::self()->getMainModule();
309 317 _main.evalScript("import PythonQt");
310 318 _main.addObject("obj", _helper);
311 319 }
312 320
313 321 bool PythonQtTestApiHelper::call(const QString& function, const QVariantList& args, const QVariant& expectedResult) {
314 322 _passed = false;
315 323 QVariant r = PythonQt::self()->call(PythonQt::self()->getMainModule(), function, args);
316 324 return _passed && expectedResult==r;
317 325 }
318 326
319 327 void PythonQtTestApi::testCall()
320 328 {
321 329 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
322 330
323 331 QVERIFY(qVariantValue<QObject*>(PythonQt::self()->getVariable(main, "obj"))==_helper);
324 332
325 333 PyRun_SimpleString("def testCallNoArgs():\n obj.setPassed();\n");
326 334 QVERIFY(_helper->call("testCallNoArgs", QVariantList(), QVariant()));
327 335
328 336 PyRun_SimpleString("def testCall1(a):\n if a=='test': obj.setPassed();\n return 'test2';\n");
329 337 QVERIFY(_helper->call("testCall1", QVariantList() << QVariant("test"), QVariant(QString("test2"))));
330 338
331 339 PyRun_SimpleString("def testCall2(a, b):\n if a=='test' and b==obj: obj.setPassed();\n return obj;\n");
332 340 QVariant r = PythonQt::self()->call(PythonQt::self()->getMainModule(), "testCall2", QVariantList() << QVariant("test") << qVariantFromValue((QObject*)_helper));
333 341 QObject* p = qVariantValue<QObject*>(r);
334 342 QVERIFY(p==_helper);
335 343 }
336 344
337 345 void PythonQtTestApi::testVariables()
338 346 {
339 347 PythonQt::self()->addObject(PythonQt::self()->getMainModule(), "someObject", _helper);
340 348 QVariant v = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someObject");
341 349 QObject* p = qVariantValue<QObject*>(v);
342 350 QVERIFY(p==_helper);
343 351 // test for unset variable
344 352 QVariant v2 = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someObject2");
345 353 QVERIFY(v2==QVariant());
346 354
347 355 PythonQt::self()->addVariable(PythonQt::self()->getMainModule(), "someValue", QStringList() << "test1" << "test2");
348 356 QVariant v3 = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someValue");
349 357 QVERIFY(v3 == QVariant(QStringList() << "test1" << "test2"));
350 358
351 359 QStringList l = PythonQt::self()->introspection(PythonQt::self()->getMainModule(), QString::null, PythonQt::Variable);
352 360 QSet<QString> s;
353 361 // check that at least these three variables are set
354 362 s << "obj" << "someObject" << "someValue";
355 363 foreach (QString value, s) {
356 364 QVERIFY(l.indexOf(value)!=-1);
357 365 }
358 366
359 367 // insert a second time!
360 368 PythonQt::self()->addObject(PythonQt::self()->getMainModule(), "someObject", _helper);
361 369 // and remove
362 370 PythonQt::self()->removeVariable(PythonQt::self()->getMainModule(), "someObject");
363 371 // we expect to find no variable
364 372 QVariant v4 = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someObject");
365 373 QVERIFY(v4==QVariant());
366 374 }
367 375
368 376 void PythonQtTestApi::testImporter()
369 377 {
370 378 PythonQt::self()->setImporter(_helper);
371 379 PythonQt::self()->overwriteSysPath(QStringList() << "c:\\test");
372 380 PyRun_SimpleString("import bla\n");
373 381 }
374 382
375 383 void PythonQtTestApi::testQtNamespace()
376 384 {
377 385 QVERIFY(!_main.getVariable("PythonQt.QtCore.Qt.red").toInt()==Qt::red);
378 386 QVERIFY(_main.getVariable("PythonQt.QtCore.Qt.FlatCap").toInt()==Qt::FlatCap);
379 387 QVERIFY(PythonQtObjectPtr(_main.getVariable("PythonQt.QtCore.Qt.escape")));
380 388 }
381 389
382 390 void PythonQtTestApi::testQColorDecorators()
383 391 {
384 392 PythonQtObjectPtr colorClass = _main.getVariable("PythonQt.QtGui.QColor");
385 393 QVERIFY(colorClass);
386 394 // verify that the class is in the correct module
387 395 QVERIFY(colorClass.getVariable("__module__") == "PythonQt.QtGui");
388 396 // test on Qt module as well:
389 397 colorClass = _main.getVariable("PythonQt.Qt.QColor");
390 398 QVERIFY(colorClass);
391 399 // constructors
392 400 QVERIFY(qVariantValue<QColor>(colorClass.call(QVariantList() << 1 << 2 << 3)) == QColor(1,2,3));
393 401 QVERIFY(qVariantValue<QColor>(colorClass.call()) == QColor());
394 402 QEXPECT_FAIL("", "Testing non-existing constructor", Continue);
395 403 QVERIFY(colorClass.call(QVariantList() << 1 << 2) != QVariant());
396 404
397 405 // check for decorated Cmyk enum value
398 406 QVERIFY(colorClass.getVariable("Cmyk").toInt() == QColor::Cmyk);
399 407 PythonQtObjectPtr staticMethod = colorClass.getVariable("fromRgb");
400 408 QVERIFY(staticMethod);
401 409 // direct call of static method via class
402 410 QVERIFY(qVariantValue<QColor>(colorClass.call("fromRgb", QVariantList() << 1 << 2 << 3)) == QColor(1,2,3));
403 411 // direct call of static method
404 412 QVERIFY(qVariantValue<QColor>(staticMethod.call(QVariantList() << 1 << 2 << 3)) == QColor(1,2,3));
405 413 PythonQtObjectPtr publicMethod = colorClass.getVariable("red");
406 414 QVERIFY(publicMethod);
407 415 // call with passing self in:
408 416 QVERIFY(colorClass.call("red", QVariantList() << QColor(255,0,0)).toInt() == 255);
409 417 }
410 418
411 419 QByteArray PythonQtTestApiHelper::readFileAsBytes(const QString& filename)
412 420 {
413 421 QByteArray b;
414 422 return b;
415 423 }
416 424
417 425 QByteArray PythonQtTestApiHelper::readSourceFile(const QString& filename, bool& ok)
418 426 {
419 427 QByteArray b;
420 428 ok = true;
421 429 return b;
422 430 }
423 431
424 432 bool PythonQtTestApiHelper::exists(const QString& filename)
425 433 {
426 434 return true;
427 435 }
428 436
429 437 QDateTime PythonQtTestApiHelper::lastModifiedDate(const QString& filename) {
430 438 return QDateTime::currentDateTime();
431 439 }
432 440
433 441
434 442 void PythonQtTestApi::testRedirect()
435 443 {
436 444 connect(PythonQt::self(), SIGNAL(pythonStdOut(const QString&)), _helper, SLOT(stdOut(const QString&)));
437 445 connect(PythonQt::self(), SIGNAL(pythonStdErr(const QString&)), _helper, SLOT(stdErr(const QString&)));
438 446 PyRun_SimpleString("print 'test'\n");
439 447 }
440 448
441 449 void PythonQtTestApiHelper::stdOut(const QString& s)
442 450 {
443 451 qDebug() << s;
444 452 }
445 453
446 454 void PythonQtTestApiHelper::stdErr(const QString& s)
447 455 {
448 456 qDebug() << s;
449 457 }
450 458
451 459 QObject* PythonQtTestCppFactory::create(const QByteArray& name, void *ptr)
452 460 {
453 461 if (name == "PQCppObject") {
454 462 return new PQCppObjectWrapper(ptr);
455 463 }
456 464 return NULL;
457 465 }
@@ -1,423 +1,433
1 1 #ifndef _PYTHONQTTESTS_H
2 2 #define _PYTHONQTTESTS_H
3 3
4 4 /*
5 5 *
6 6 * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.
7 7 *
8 8 * This library is free software; you can redistribute it and/or
9 9 * modify it under the terms of the GNU Lesser General Public
10 10 * License as published by the Free Software Foundation; either
11 11 * version 2.1 of the License, or (at your option) any later version.
12 12 *
13 13 * This library is distributed in the hope that it will be useful,
14 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 16 * Lesser General Public License for more details.
17 17 *
18 18 * Further, this software is distributed without any warranty that it is
19 19 * free of the rightful claim of any third person regarding infringement
20 20 * or the like. Any license provided herein, whether implied or
21 21 * otherwise, applies only to this software file. Patent licenses, if
22 22 * any, provided herein do not apply to combinations of this program with
23 23 * other software, or any other product whatsoever.
24 24 *
25 25 * You should have received a copy of the GNU Lesser General Public
26 26 * License along with this library; if not, write to the Free Software
27 27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 28 *
29 29 * Contact information: MeVis Research GmbH, Universitaetsallee 29,
30 30 * 28359 Bremen, Germany or:
31 31 *
32 32 * http://www.mevis.de
33 33 *
34 34 */
35 35
36 36 //----------------------------------------------------------------------------------
37 37 /*!
38 38 // \file PythonQtTests.h
39 39 // \author Florian Link
40 40 // \author Last changed by $Author: florian $
41 41 // \date 2006-05
42 42 */
43 43 //----------------------------------------------------------------------------------
44 44
45 45 #include "PythonQt.h"
46 46 #include <QtTest/QtTest>
47 47 #include <QVariant>
48 48 #include "PythonQtImportFileInterface.h"
49 49 #include "PythonQtCppWrapperFactory.h"
50 50
51 51 class PythonQtTestSlotCallingHelper;
52 52 class PythonQtTestApiHelper;
53 53 class QWidget;
54 54
55 55 //! test the PythonQt api
56 56 class PythonQtTestApi : public QObject
57 57 {
58 58 Q_OBJECT
59 59
60 60 private slots:
61 61 void initTestCase();
62 62 void testCall();
63 63 void testVariables();
64 64 void testRedirect();
65 65 void testImporter();
66 66 void testQColorDecorators();
67 67 void testQtNamespace();
68 68
69 69 private:
70 70 PythonQtTestApiHelper* _helper;
71 71 PythonQtObjectPtr _main;
72 72
73 73 };
74 74
75 75 class ClassA {
76 76 public:
77 77 ClassA() { x = 1; }
78 78 int x;
79 79 };
80 80
81 81 class ClassB {
82 82 public:
83 83 ClassB() { y = 2; }
84 84 int y;
85 85
86 86 virtual int type() { return 2; }
87 87 };
88 88
89 89 class ClassC : public ClassA, public ClassB {
90 90 public:
91 91 ClassC() { z = 3; }
92 92 int z;
93 93
94 94 virtual int type() { return 3; }
95 95 };
96 96
97 97 class ClassD : public QObject, public ClassA, public ClassB {
98 98 Q_OBJECT
99 99 public:
100 100 ClassD() { d = 4; }
101 101 public slots:
102 102 int getD() { return d; }
103 103 private:
104 104 int d;
105 105
106 106 virtual int type() { return 4; }
107 107 };
108 108
109 109 class ClassAWrapper : public QObject {
110 110 Q_OBJECT
111 111 public slots:
112 112 ClassA* new_ClassA() { return new ClassA; }
113 113 int getX(ClassA* o) { return o->x; }
114 114 };
115 115
116 116 class ClassBWrapper : public QObject {
117 117 Q_OBJECT
118 118 public slots:
119 119 ClassB* new_ClassB() { return new ClassB; }
120 120 int getY(ClassB* o) { return o->y; }
121 121 };
122 122
123 123 class ClassCWrapper : public QObject {
124 124 Q_OBJECT
125 125 public slots:
126 126 ClassC* new_ClassC() { return new ClassC; }
127 127 int getZ(ClassC* o) { return o->z; }
128 128 };
129 129
130 130 class ClassDWrapper : public QObject {
131 131 Q_OBJECT
132 132 public slots:
133 133 ClassD* new_ClassD() { return new ClassD; }
134 134 };
135 135
136 136
137 137 //! test the PythonQt api (helper)
138 138 class PythonQtTestApiHelper : public QObject , public PythonQtImportFileInterface
139 139 {
140 140 Q_OBJECT
141 141 public:
142 142 PythonQtTestApiHelper() {
143 143 };
144 144
145 145 bool call(const QString& function, const QVariantList& args, const QVariant& expectedResult);
146 146
147 147 virtual QByteArray readFileAsBytes(const QString& filename);
148 148
149 149 virtual QByteArray readSourceFile(const QString& filename, bool& ok);
150 150
151 151 virtual bool exists(const QString& filename);
152 152
153 153 virtual QDateTime lastModifiedDate(const QString& filename);
154 154
155 155 public slots:
156 156
157 157 //! call to set that the test has passed (from Python!)
158 158 void setPassed() { _passed = true; }
159 159
160 160 void stdOut(const QString&);
161 161 void stdErr(const QString&);
162 162
163 163 private:
164 164 bool _passed;
165 165 };
166 166
167 167
168 168 // test implementation of the wrapper factory
169 169 class PythonQtTestCppFactory : public PythonQtCppWrapperFactory
170 170 {
171 171 public:
172 172 virtual QObject* create(const QByteArray& name, void *ptr);
173 173 };
174 174
175 175 //! an cpp object to be wrapped
176 176 class PQCppObject {
177 177
178 178 public:
179 179 PQCppObject(int h) { _height = h; }
180 180
181 181 int getHeight() { return _height; }
182 182 void setHeight(int h) { _height = h; }
183 183
184 184 private:
185 185 int _height;
186 186 };
187 187
188 188 //! an qobject that wraps the existing cpp object
189 189 class PQCppObjectWrapper : public QObject {
190 190 Q_OBJECT
191 191 public:
192 192 PQCppObjectWrapper(void* ptr) {
193 193 _ptr = (PQCppObject*)ptr;
194 194 }
195 195
196 196 public slots:
197 197 int getHeight() { return _ptr->getHeight(); }
198 198 void setHeight(int h) { _ptr->setHeight(h); }
199 199
200 200 private:
201 201 PQCppObject* _ptr;
202 202 };
203 203
204 204 class PQCppObjectDecorator : public QObject {
205 205 Q_OBJECT
206 206 public slots:
207 207 int getH(PQCppObject* obj) { return obj->getHeight(); }
208 208
209 209 };
210 210
211 211 //! an cpp object to be wrapped by decorators only
212 212 class PQCppObjectNoWrap {
213 213
214 214 public:
215 215 PQCppObjectNoWrap(int h) { _height = h; }
216 216
217 217 int getHeight() { return _height; }
218 218 void setHeight(int h) { _height = h; }
219 219
220 220 private:
221 221 int _height;
222 222 };
223 223
224 224 class PQCppObjectNoWrapDecorator : public QObject {
225 225 Q_OBJECT
226 226 public slots:
227 PQCppObjectNoWrap* new_PQCppObjectNoWrap() {
228 return new PQCppObjectNoWrap(0);
229 }
230 PQCppObjectNoWrap* new_PQCppObjectNoWrap(const PQCppObjectNoWrap& other) {
231 return new PQCppObjectNoWrap(1);
232 }
233 PQCppObjectNoWrap* new_PQCppObjectNoWrap(double value) {
234 return new PQCppObjectNoWrap(2);
235 }
236
227 237 int getH(PQCppObjectNoWrap* obj) { return obj->getHeight(); }
228 238
229 239 };
230 240
231 241
232 242 //! test the calling of slots
233 243 class PythonQtTestSlotCalling : public QObject
234 244 {
235 245 Q_OBJECT
236 246
237 247 private slots:
238 248 void initTestCase();
239 249 void init();
240 250
241 251 void testNoArgSlotCall();
242 252 void testPODSlotCalls();
243 253 void testCPPSlotCalls();
244 254 void testQVariantSlotCalls();
245 255 void testObjectSlotCalls();
246 256 void testMultiArgsSlotCall();
247 257 void testPyObjectSlotCall();
248 258 void testOverloadedCall();
249 259 void testCppFactory();
250 260 void testInheritance();
251 261
252 262 private:
253 263 PythonQtTestSlotCallingHelper* _helper;
254 264
255 265 };
256 266
257 267 //! helper class for slot calling test
258 268 class PythonQtTestSlotCallingHelper : public QObject
259 269 {
260 270 Q_OBJECT
261 271 public:
262 272 PythonQtTestSlotCallingHelper(PythonQtTestSlotCalling* test) {
263 273 _test = test;
264 274 };
265 275
266 276 bool runScript(const char* script, int expectedOverload = -1);
267 277
268 278 public slots:
269 279
270 280 //! call to set that the test has passed (from Python!)
271 281 void setPassed() { _passed = true; }
272 282
273 283 //! no arguments, no return value:
274 284 void testNoArg() { _called = true; }
275 285
276 286 //! overload test!
277 287 void overload(bool a) { _calledOverload = 0; _called = true; }
278 288 void overload(float a) { _calledOverload = 1; _called = true;}
279 289 void overload(int a) { _calledOverload = 2; _called = true;}
280 290 void overload(const QString& str) { _calledOverload = 3; _called = true;}
281 291 void overload(const QStringList& str) { _calledOverload = 4; _called = true;}
282 292 void overload(QObject* str) { _calledOverload = 5; _called = true;}
283 293 void overload(float a, int b) { _calledOverload = 6; _called = true;}
284 294
285 295 //! POD values:
286 296 int getInt(int a) { _called = true; return a; }
287 297 unsigned int getUInt(unsigned int a) { _called = true; return a; }
288 298 bool getBool(bool a) { _called = true; return a; }
289 299 char getChar(char a) { _called = true; return a; }
290 300 unsigned char getUChar(unsigned char a) { _called = true; return a; }
291 301 long getLong(long a) { _called = true; return a; }
292 302 unsigned long getULong(unsigned long a) { _called = true; return a; }
293 303 short getShort(short a) { _called = true; return a; }
294 304 unsigned short getUShort(unsigned short a) { _called = true; return a; }
295 305 QChar getQChar(QChar a) { _called = true; return a; }
296 306 qint64 getLongLong(qint64 a) { _called = true; return a; }
297 307 quint64 getULongLong(quint64 a) { _called = true; return a; }
298 308 double getDouble(double d) { _called = true; return d; }
299 309 float getFloat(float d) { _called = true; return d; }
300 310
301 311 //! important qt types:
302 312 QString getQString(const QString& s) { _called = true; return s; }
303 313 QStringList getQStringList(const QStringList& l) { _called = true; return l; }
304 314 QVariant getQVariant(const QVariant& var) { _called = true; return var; }
305 315
306 316 // QColor as representative for C++ value classes
307 317 QColor getQColor1(const QColor& var) { _called = true; return var; }
308 318 QColor getQColor2(QColor& var) { _called = true; return var; }
309 319 QColor getQColor3(QColor* col) { _called = true; return *col; }
310 320 QColor getQColor4(const QVariant& color) { _called = true; return qVariantValue<QColor>(color); }
311 321 QColor* getQColor5() { _called = true; static QColor c(1,2,3); return &c; }
312 322
313 323 PyObject* getPyObject(PyObject* obj) { _called = true; return obj; }
314 324 PyObject* getPyObjectFromVariant(const QVariant& val) { _called = true; return PythonQtObjectPtr(val); };
315 325 QVariant getPyObjectFromVariant2(const QVariant& val) { _called = true; return val; };
316 326 // this does not yet work but is not required to work:
317 327 //PyObject* getPyObjectFromPtr(const PythonQtObjectPtr& val) { _called = true; return val; };
318 328
319 329 //! testing pointer passing
320 330 PythonQtTestSlotCallingHelper* getTestObject(PythonQtTestSlotCallingHelper* obj) { _called = true; return obj; }
321 331 //! testing inheritance checking
322 332 QObject* getQObject(QObject* obj) { _called = true; return obj; }
323 333 QWidget* getQWidget(QWidget* obj) { _called = true; return obj; }
324 334 //! testing if an object that was not wrapped is wrapped earlier is wrapped correctly
325 335 QObject* getNewObject() { _called = true; return new PythonQtTestSlotCallingHelper(NULL); }
326 336
327 337 QVariantList getMultiArgs(int a, double b, const QString& str) { _called = true; return (QVariantList() << a << b << str); }
328 338
329 339 //! cpp wrapper factory test
330 340 PQCppObject* createPQCppObject(int h) { _called = true; return new PQCppObject(h); }
331 341
332 342 //! cpp wrapper factory test
333 343 PQCppObject* getPQCppObject(PQCppObject* p) { _called = true; return p; }
334 344
335 345 //! cpp wrapper factory test
336 346 PQCppObjectNoWrap* createPQCppObjectNoWrap(int h) { _called = true; return new PQCppObjectNoWrap(h); }
337 347
338 348 //! cpp wrapper factory test
339 349 PQCppObjectNoWrap* getPQCppObjectNoWrap(PQCppObjectNoWrap* p) { _called = true; return p; }
340 350
341 351 ClassA* getClassAPtr(ClassA* o) { _called = true; return o; }
342 352 ClassB* getClassBPtr(ClassB* o) { _called = true; return o; }
343 353 ClassC* getClassCPtr(ClassC* o) { _called = true; return o; }
344 354 ClassD* getClassDPtr(ClassD* o) { _called = true; return o; }
345 355
346 356 ClassA* createClassA() { _called = true; return new ClassA; }
347 357 ClassB* createClassB() { _called = true; return new ClassB; }
348 358 ClassC* createClassC() { _called = true; return new ClassC; }
349 359 ClassD* createClassD() { _called = true; return new ClassD; }
350 360 ClassA* createClassCAsA() { _called = true; return new ClassC; }
351 361 ClassB* createClassCAsB() { _called = true; return new ClassC; }
352 362 ClassA* createClassDAsA() { _called = true; return new ClassD; }
353 363 ClassB* createClassDAsB() { _called = true; return new ClassD; }
354 364
355 365 private:
356 366 bool _passed;
357 367 bool _called;
358 368 int _calledOverload;
359 369 PythonQtTestSlotCalling* _test;
360 370 };
361 371
362 372 class PythonQtTestSignalHandlerHelper;
363 373
364 374 //! test the connection of signals to python
365 375 class PythonQtTestSignalHandler : public QObject
366 376 {
367 377 Q_OBJECT
368 378
369 379 private slots:
370 380 void initTestCase();
371 381
372 382 void testSignalHandler();
373 383 void testRecursiveSignalHandler();
374 384
375 385 private:
376 386 PythonQtTestSignalHandlerHelper* _helper;
377 387
378 388 };
379 389
380 390 //! helper class for signal testing
381 391 class PythonQtTestSignalHandlerHelper : public QObject
382 392 {
383 393 Q_OBJECT
384 394
385 395 public:
386 396 PythonQtTestSignalHandlerHelper(PythonQtTestSignalHandler* test) {
387 397 _test = test;
388 398 };
389 399
390 400 public slots:
391 401 void setPassed() { _passed = true; }
392 402
393 403 bool emitIntSignal(int a) { _passed = false; emit intSignal(a); return _passed; };
394 404 bool emitFloatSignal(float a) { _passed = false; emit floatSignal(a); return _passed; };
395 405
396 406 bool emitVariantSignal(const QVariant& v) { _passed = false; emit variantSignal(v); return _passed; };
397 407 QVariant expectedVariant() { return _v; }
398 408 void setExpectedVariant(const QVariant& v) { _v = v; }
399 409
400 410 bool emitComplexSignal(int a, float b, const QStringList& l, QObject* obj) { _passed = false; emit complexSignal(a,b,l,obj); return _passed; };
401 411
402 412 bool emitSignal1(int a) { _passed = false; emit signal1(a); return _passed; };
403 413 bool emitSignal2(const QString& s) { _passed = false; emit signal2(s); return _passed; };
404 414 bool emitSignal3(float a) { _passed = false; emit signal3(a); return _passed; };
405 415
406 416 signals:
407 417 void intSignal(int);
408 418 void floatSignal(float);
409 419 void variantSignal(const QVariant& v);
410 420 void complexSignal(int a, float b, const QStringList& l, QObject* obj);
411 421
412 422 void signal1(int);
413 423 void signal2(const QString&);
414 424 void signal3(float);
415 425
416 426 private:
417 427 bool _passed;
418 428 QVariant _v;
419 429
420 430 PythonQtTestSignalHandler* _test;
421 431 };
422 432
423 433 #endif
General Comments 0
You need to be logged in to leave comments. Login now