##// END OF EJS Templates
adjusted tests for new support of unregistered types as return by value...
florianlink -
r47:2cd0aabb8a89
parent child
Show More
@@ -1,473 +1,475
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 qRegisterMetaType<PQCppObjectNoWrap>("PQCppObjectNoWrap");
216 // do not register, since we want to know if that works as well
217 //qRegisterMetaType<PQCppObjectNoWrap>("PQCppObjectNoWrap");
217 218 PythonQt::self()->addDecorators(new PQCppObjectNoWrapDecorator);
218 219
219 220 PythonQt::self()->addWrapperFactory(f);
220 221 QVERIFY(_helper->runScript("if obj.createPQCppObject(12).getHeight()==12: obj.setPassed();\n"));
221 222 QVERIFY(_helper->runScript("if obj.createPQCppObject(12).getH()==12: obj.setPassed();\n"));
222 223 QVERIFY(_helper->runScript("pq1 = obj.createPQCppObject(12);\n"
223 224 "pq2 = obj.createPQCppObject(13);\n"
224 225 "pq3 = obj.getPQCppObject(pq1);\n"
225 226 "pq4 = obj.getPQCppObject(pq2);\n"
226 227 "if pq3.getHeight()==12 and pq4.getHeight()==13: obj.setPassed();\n"
227 228 ));
228 229
229 230 QVERIFY(_helper->runScript("if obj.createPQCppObjectNoWrap(12).getH()==12: obj.setPassed();\n"));
230 231
231 232 QVERIFY(_helper->runScript("if obj.getPQCppObjectNoWrapAsValue().getH()==47: obj.setPassed();\n"));
232 233
233 234 qRegisterMetaType<PQUnknownButRegisteredValueObject>("PQUnknownButRegisteredValueObject");
234 235 QVERIFY(_helper->runScript("a = obj.getUnknownButRegisteredValueObjectAsPtr();print a;\nif a!=None: obj.setPassed();\n"));
235 236 QVERIFY(_helper->runScript("a = obj.getUnknownButRegisteredValueObjectAsValue();print a;\nif a!=None: obj.setPassed();\n"));
236 237 QVERIFY(_helper->runScript("a = obj.getUnknownValueObjectAsPtr();print a;\nif a!=None: obj.setPassed();\n"));
238 QEXPECT_FAIL("", "Testing by value return without the object being registered as QMetaType or having registered a default constructor decorator", Continue);
237 239 QVERIFY(_helper->runScript("a = obj.getUnknownValueObjectAsValue();print a;\nif a!=None: obj.setPassed();\n"));
238 240
239 241 // expect to get strict call to double overload
240 242 QVERIFY(_helper->runScript("obj.testNoArg()\nfrom PythonQt import PQCppObjectNoWrap\na = PQCppObjectNoWrap(22.2)\nif a.getH()==2: obj.setPassed();\n"));
241 243 // expect to get un-strict call to double overload
242 244 QVERIFY(_helper->runScript("obj.testNoArg()\nfrom PythonQt import PQCppObjectNoWrap\na = PQCppObjectNoWrap(22)\nif a.getH()==2: obj.setPassed();\n"));
243 245 // expect to get strict call to copy constructor overload
244 246 QVERIFY(_helper->runScript("obj.testNoArg()\nfrom PythonQt import PQCppObjectNoWrap\na = PQCppObjectNoWrap(PQCppObjectNoWrap())\nprint a.getH()\nif a.getH()==1: obj.setPassed();\n"));
245 247 }
246 248
247 249
248 250 void PythonQtTestSlotCalling::testMultiArgsSlotCall()
249 251 {
250 252 QVERIFY(_helper->runScript("if obj.getMultiArgs(12,47.11,'test')==(12,47.11,'test'): obj.setPassed();\n"));
251 253 }
252 254
253 255 bool PythonQtTestSlotCallingHelper::runScript(const char* script, int expectedOverload)
254 256 {
255 257 _called = false;
256 258 _passed = false;
257 259 _calledOverload = -1;
258 260 PyRun_SimpleString(script);
259 261 return _called && _passed && _calledOverload==expectedOverload;
260 262 }
261 263
262 264
263 265 void PythonQtTestSignalHandler::initTestCase()
264 266 {
265 267 _helper = new PythonQtTestSignalHandlerHelper(this);
266 268 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
267 269 PythonQt::self()->addObject(main, "obj", _helper);
268 270 }
269 271
270 272 void PythonQtTestSignalHandler::testSignalHandler()
271 273 {
272 274 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
273 275 PyRun_SimpleString("def testIntSignal(a):\n if a==12: obj.setPassed();\n");
274 276 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(intSignal(int)), main, "testIntSignal"));
275 277 QVERIFY(_helper->emitIntSignal(12));
276 278
277 279 PyRun_SimpleString("def testFloatSignal(a):\n if a==12: obj.setPassed();\n");
278 280 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(floatSignal(float)), main, "testFloatSignal"));
279 281 QVERIFY(_helper->emitFloatSignal(12));
280 282
281 283 PyRun_SimpleString("def testVariantSignal(a):\n if a==obj.expectedVariant(): obj.setPassed();\n");
282 284 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(variantSignal(QVariant)), main, "testVariantSignal"));
283 285 _helper->setExpectedVariant(QString("Test"));
284 286 QVERIFY(_helper->emitVariantSignal(QString("Test")));
285 287 _helper->setExpectedVariant(12);
286 288 QVERIFY(_helper->emitVariantSignal(12));
287 289 _helper->setExpectedVariant(QStringList() << "test1" << "test2");
288 290 QVERIFY(_helper->emitVariantSignal(QStringList() << "test1" << "test2"));
289 291 _helper->setExpectedVariant(qVariantFromValue((QObject*)_helper));
290 292 QVERIFY(_helper->emitVariantSignal(qVariantFromValue((QObject*)_helper)));
291 293
292 294 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");
293 295 // intentionally not normalized signal:
294 296 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(complexSignal( int, float , const QStringList , QObject*)), main, "testComplexSignal"));
295 297 QVERIFY(_helper->emitComplexSignal(12,13,QStringList() << "test1" << "test2", _helper));
296 298
297 299 // try removing the handler
298 300 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(complexSignal( int, float , const QStringList , QObject*)), main, "testComplexSignal"));
299 301 // and emit the signal, which should fail because the handler was removed
300 302 QVERIFY(!_helper->emitComplexSignal(12,13,QStringList() << "test1" << "test2", _helper));
301 303
302 304 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(intSignal(int)), main, "testIntSignal"));
303 305 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(floatSignal(float)), main, "testFloatSignal"));
304 306 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(variantSignal(QVariant)), main, "testVariantSignal"));
305 307
306 308 }
307 309
308 310 void PythonQtTestSignalHandler::testRecursiveSignalHandler()
309 311 {
310 312 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
311 313 PyRun_SimpleString("def testSignal1(a):\n obj.emitSignal2(a);\n");
312 314 PyRun_SimpleString("def testSignal2(a):\n obj.emitSignal3(float(a));\n");
313 315 PyRun_SimpleString("def testSignal3(a):\n if a==12: obj.setPassed();\n");
314 316 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(signal1(int)), main, "testSignal1"));
315 317 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(signal2(const QString&)), main, "testSignal2"));
316 318 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(signal3(float)), main, "testSignal3"));
317 319 QVERIFY(_helper->emitSignal1(12));
318 320 }
319 321
320 322
321 323 void PythonQtTestApi::initTestCase()
322 324 {
323 325 _helper = new PythonQtTestApiHelper();
324 326 _main = PythonQt::self()->getMainModule();
325 327 _main.evalScript("import PythonQt");
326 328 _main.addObject("obj", _helper);
327 329 }
328 330
329 331 bool PythonQtTestApiHelper::call(const QString& function, const QVariantList& args, const QVariant& expectedResult) {
330 332 _passed = false;
331 333 QVariant r = PythonQt::self()->call(PythonQt::self()->getMainModule(), function, args);
332 334 return _passed && expectedResult==r;
333 335 }
334 336
335 337 void PythonQtTestApi::testCall()
336 338 {
337 339 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
338 340
339 341 QVERIFY(qVariantValue<QObject*>(PythonQt::self()->getVariable(main, "obj"))==_helper);
340 342
341 343 PyRun_SimpleString("def testCallNoArgs():\n obj.setPassed();\n");
342 344 QVERIFY(_helper->call("testCallNoArgs", QVariantList(), QVariant()));
343 345
344 346 PyRun_SimpleString("def testCall1(a):\n if a=='test': obj.setPassed();\n return 'test2';\n");
345 347 QVERIFY(_helper->call("testCall1", QVariantList() << QVariant("test"), QVariant(QString("test2"))));
346 348
347 349 PyRun_SimpleString("def testCall2(a, b):\n if a=='test' and b==obj: obj.setPassed();\n return obj;\n");
348 350 QVariant r = PythonQt::self()->call(PythonQt::self()->getMainModule(), "testCall2", QVariantList() << QVariant("test") << qVariantFromValue((QObject*)_helper));
349 351 QObject* p = qVariantValue<QObject*>(r);
350 352 QVERIFY(p==_helper);
351 353 }
352 354
353 355 void PythonQtTestApi::testVariables()
354 356 {
355 357 PythonQt::self()->addObject(PythonQt::self()->getMainModule(), "someObject", _helper);
356 358 QVariant v = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someObject");
357 359 QObject* p = qVariantValue<QObject*>(v);
358 360 QVERIFY(p==_helper);
359 361 // test for unset variable
360 362 QVariant v2 = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someObject2");
361 363 QVERIFY(v2==QVariant());
362 364
363 365 PythonQt::self()->addVariable(PythonQt::self()->getMainModule(), "someValue", QStringList() << "test1" << "test2");
364 366 QVariant v3 = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someValue");
365 367 QVERIFY(v3 == QVariant(QStringList() << "test1" << "test2"));
366 368
367 369 QStringList l = PythonQt::self()->introspection(PythonQt::self()->getMainModule(), QString::null, PythonQt::Variable);
368 370 QSet<QString> s;
369 371 // check that at least these three variables are set
370 372 s << "obj" << "someObject" << "someValue";
371 373 foreach (QString value, s) {
372 374 QVERIFY(l.indexOf(value)!=-1);
373 375 }
374 376
375 377 // insert a second time!
376 378 PythonQt::self()->addObject(PythonQt::self()->getMainModule(), "someObject", _helper);
377 379 // and remove
378 380 PythonQt::self()->removeVariable(PythonQt::self()->getMainModule(), "someObject");
379 381 // we expect to find no variable
380 382 QVariant v4 = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someObject");
381 383 QVERIFY(v4==QVariant());
382 384 }
383 385
384 386 void PythonQtTestApi::testImporter()
385 387 {
386 388 PythonQt::self()->setImporter(_helper);
387 389 PythonQt::self()->overwriteSysPath(QStringList() << "c:\\test");
388 390 PyRun_SimpleString("import bla\n");
389 391 }
390 392
391 393 void PythonQtTestApi::testQtNamespace()
392 394 {
393 395 QVERIFY(!_main.getVariable("PythonQt.QtCore.Qt.red").toInt()==Qt::red);
394 396 QVERIFY(_main.getVariable("PythonQt.QtCore.Qt.FlatCap").toInt()==Qt::FlatCap);
395 397 QVERIFY(PythonQtObjectPtr(_main.getVariable("PythonQt.QtCore.Qt.escape")));
396 398 }
397 399
398 400 void PythonQtTestApi::testQColorDecorators()
399 401 {
400 402 PythonQtObjectPtr colorClass = _main.getVariable("PythonQt.QtGui.QColor");
401 403 QVERIFY(colorClass);
402 404 // verify that the class is in the correct module
403 405 QVERIFY(colorClass.getVariable("__module__") == "PythonQt.QtGui");
404 406 // test on Qt module as well:
405 407 colorClass = _main.getVariable("PythonQt.Qt.QColor");
406 408 QVERIFY(colorClass);
407 409 // constructors
408 410 QVERIFY(qVariantValue<QColor>(colorClass.call(QVariantList() << 1 << 2 << 3)) == QColor(1,2,3));
409 411 QVERIFY(qVariantValue<QColor>(colorClass.call()) == QColor());
410 412 QEXPECT_FAIL("", "Testing non-existing constructor", Continue);
411 413 QVERIFY(colorClass.call(QVariantList() << 1 << 2) != QVariant());
412 414
413 415 // check for decorated Cmyk enum value
414 416 QVERIFY(colorClass.getVariable("Cmyk").toInt() == QColor::Cmyk);
415 417 PythonQtObjectPtr staticMethod = colorClass.getVariable("fromRgb");
416 418 QVERIFY(staticMethod);
417 419 // direct call of static method via class
418 420 QVERIFY(qVariantValue<QColor>(colorClass.call("fromRgb", QVariantList() << 1 << 2 << 3)) == QColor(1,2,3));
419 421 // direct call of static method
420 422 QVERIFY(qVariantValue<QColor>(staticMethod.call(QVariantList() << 1 << 2 << 3)) == QColor(1,2,3));
421 423 PythonQtObjectPtr publicMethod = colorClass.getVariable("red");
422 424 QVERIFY(publicMethod);
423 425 // call with passing self in:
424 426 QVERIFY(colorClass.call("red", QVariantList() << QColor(255,0,0)).toInt() == 255);
425 427 }
426 428
427 429 QByteArray PythonQtTestApiHelper::readFileAsBytes(const QString& filename)
428 430 {
429 431 QByteArray b;
430 432 return b;
431 433 }
432 434
433 435 QByteArray PythonQtTestApiHelper::readSourceFile(const QString& filename, bool& ok)
434 436 {
435 437 QByteArray b;
436 438 ok = true;
437 439 return b;
438 440 }
439 441
440 442 bool PythonQtTestApiHelper::exists(const QString& filename)
441 443 {
442 444 return true;
443 445 }
444 446
445 447 QDateTime PythonQtTestApiHelper::lastModifiedDate(const QString& filename) {
446 448 return QDateTime::currentDateTime();
447 449 }
448 450
449 451
450 452 void PythonQtTestApi::testRedirect()
451 453 {
452 454 connect(PythonQt::self(), SIGNAL(pythonStdOut(const QString&)), _helper, SLOT(stdOut(const QString&)));
453 455 connect(PythonQt::self(), SIGNAL(pythonStdErr(const QString&)), _helper, SLOT(stdErr(const QString&)));
454 456 PyRun_SimpleString("print 'test'\n");
455 457 }
456 458
457 459 void PythonQtTestApiHelper::stdOut(const QString& s)
458 460 {
459 461 qDebug() << s;
460 462 }
461 463
462 464 void PythonQtTestApiHelper::stdErr(const QString& s)
463 465 {
464 466 qDebug() << s;
465 467 }
466 468
467 469 QObject* PythonQtTestCppFactory::create(const QByteArray& name, void *ptr)
468 470 {
469 471 if (name == "PQCppObject") {
470 472 return new PQCppObjectWrapper(ptr);
471 473 }
472 474 return NULL;
473 475 }
General Comments 0
You need to be logged in to leave comments. Login now