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