##// END OF EJS Templates
added tests for dynamic properties...
florianlink -
r86:d9124d642ab2
parent child
Show More
@@ -1,535 +1,578
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 57 void* polymorphic_ClassB_Handler(const void* ptr, char** className) {
57 58 ClassB* o = (ClassB*)ptr;
58 59 if (o->type()==2) {
59 60 *className = "ClassB";
60 61 return (ClassB*)o;
61 62 }
62 63 if (o->type()==3) {
63 64 *className = "ClassC";
64 65 return (ClassC*)o;
65 66 }
66 67 if (o->type()==4) {
67 68 *className = "ClassD";
68 69 return (ClassD*)o;
69 70 }
70 71 return NULL;
71 72 }
72 73
73 74 void PythonQtTestSlotCalling::testInheritance() {
74 75 PythonQt::self()->registerCPPClass("ClassA",NULL,NULL, PythonQtCreateObject<ClassAWrapper>);
75 76 PythonQt::self()->registerCPPClass("ClassB",NULL,NULL, PythonQtCreateObject<ClassBWrapper>);
76 77 PythonQt::self()->registerCPPClass("ClassC",NULL,NULL, PythonQtCreateObject<ClassCWrapper>);
77 78 PythonQt::self()->addParentClass("ClassC", "ClassA", PythonQtUpcastingOffset<ClassC,ClassA>());
78 79 PythonQt::self()->addParentClass("ClassC", "ClassB", PythonQtUpcastingOffset<ClassC,ClassB>());
79 80 PythonQt::self()->registerClass(&ClassD::staticMetaObject, NULL, PythonQtCreateObject<ClassDWrapper>);
80 81 PythonQt::self()->addParentClass("ClassD", "ClassA", PythonQtUpcastingOffset<ClassD,ClassA>());
81 82 PythonQt::self()->addParentClass("ClassD", "ClassB", PythonQtUpcastingOffset<ClassD,ClassB>());
82 83
83 84 PythonQtObjectPtr classA = PythonQt::self()->getMainModule().getVariable("PythonQt.ClassA");
84 85 PythonQtObjectPtr classB = PythonQt::self()->getMainModule().getVariable("PythonQt.ClassB");
85 86 PythonQtObjectPtr classC = PythonQt::self()->getMainModule().getVariable("PythonQt.ClassC");
86 87 PythonQtObjectPtr classD = PythonQt::self()->getMainModule().getVariable("PythonQt.ClassD");
87 88 QVERIFY(classA);
88 89 QVERIFY(classB);
89 90 QVERIFY(classC);
90 91 QVERIFY(classD);
91 92
92 93 QVERIFY(_helper->runScript("a = PythonQt.ClassA();\nif obj.getClassAPtr(a).getX()==1: obj.setPassed();\n"));
93 94 QEXPECT_FAIL("", "ClassB can not be converted to ClassA", Continue);
94 95 QVERIFY(_helper->runScript("a = PythonQt.ClassB();\nif obj.getClassAPtr(a).getX()==1: obj.setPassed();\n"));
95 96 QVERIFY(_helper->runScript("a = PythonQt.ClassC();\nif obj.getClassAPtr(a).getX()==1: obj.setPassed();\n"));
96 97 QVERIFY(_helper->runScript("a = PythonQt.ClassD();\nif obj.getClassAPtr(a).getX()==1: obj.setPassed();\n"));
97 98
98 99 QEXPECT_FAIL("", "ClassA can not be converted to ClassB", Continue);
99 100 QVERIFY(_helper->runScript("a = PythonQt.ClassA();\nif obj.getClassBPtr(a).getY()==2: obj.setPassed();\n"));
100 101 QVERIFY(_helper->runScript("a = PythonQt.ClassB();\nif obj.getClassBPtr(a).getY()==2: obj.setPassed();\n"));
101 102 QVERIFY(_helper->runScript("a = PythonQt.ClassC();\nif obj.getClassBPtr(a).getY()==2: obj.setPassed();\n"));
102 103 QVERIFY(_helper->runScript("a = PythonQt.ClassD();\nif obj.getClassBPtr(a).getY()==2: obj.setPassed();\n"));
103 104
104 105 QEXPECT_FAIL("", "ClassA can not be converted to ClassC", Continue);
105 106 QVERIFY(_helper->runScript("a = PythonQt.ClassA();\nif obj.getClassCPtr(a).getX()==1: obj.setPassed();\n"));
106 107 QEXPECT_FAIL("", "ClassB can not be converted to ClassC", Continue);
107 108 QVERIFY(_helper->runScript("a = PythonQt.ClassB();\nif obj.getClassCPtr(a).getX()==1: obj.setPassed();\n"));
108 109 QVERIFY(_helper->runScript("a = PythonQt.ClassC();\nif obj.getClassCPtr(a).getX()==1: obj.setPassed();\n"));
109 110 QEXPECT_FAIL("", "ClassD can not be converted to ClassC", Continue);
110 111 QVERIFY(_helper->runScript("a = PythonQt.ClassD();\nif obj.getClassCPtr(a).getX()==1: obj.setPassed();\n"));
111 112
112 113 QVERIFY(_helper->runScript("if type(obj.createClassA())==PythonQt.ClassA: obj.setPassed();\n"));
113 114 QVERIFY(_helper->runScript("if type(obj.createClassB())==PythonQt.ClassB: obj.setPassed();\n"));
114 115 QVERIFY(_helper->runScript("if type(obj.createClassCAsA())==PythonQt.ClassA: obj.setPassed();\n"));
115 116 QVERIFY(_helper->runScript("if type(obj.createClassCAsB())==PythonQt.ClassB: obj.setPassed();\n"));
116 117 QVERIFY(_helper->runScript("if type(obj.createClassD())==PythonQt.ClassD: obj.setPassed();\n"));
117 118 QVERIFY(_helper->runScript("if type(obj.createClassDAsA())==PythonQt.ClassA: obj.setPassed();\n"));
118 119 QVERIFY(_helper->runScript("if type(obj.createClassDAsB())==PythonQt.ClassB: obj.setPassed();\n"));
119 120
120 121 PythonQt::self()->addPolymorphicHandler("ClassB", polymorphic_ClassB_Handler);
121 122
122 123 QVERIFY(_helper->runScript("if type(obj.getClassBPtr(obj.createClassB()))==PythonQt.ClassB: obj.setPassed();\n"));
123 124 QVERIFY(_helper->runScript("if type(obj.createClassCAsB())==PythonQt.ClassC: obj.setPassed();\n"));
124 125 QVERIFY(_helper->runScript("if type(obj.createClassDAsB())==PythonQt.ClassD: obj.setPassed();\n"));
125 126
126 127 }
127 128
128 129 void PythonQtTestSlotCalling::testAutoConversion() {
129 130 QVERIFY(_helper->runScript("if obj.setAutoConvertColor(PythonQt.QtCore.Qt.red)==PythonQt.Qt.QColor(PythonQt.QtCore.Qt.red): obj.setPassed();\n"));
130 131 QVERIFY(_helper->runScript("if obj.setAutoConvertBrush(PythonQt.QtCore.Qt.red)==PythonQt.Qt.QBrush(PythonQt.QtCore.Qt.red): obj.setPassed();\n"));
131 132 QVERIFY(_helper->runScript("if obj.setAutoConvertPen(PythonQt.QtCore.Qt.red)==PythonQt.Qt.QPen(PythonQt.QtCore.Qt.red): obj.setPassed();\n"));
132 133 QVERIFY(_helper->runScript("if obj.setAutoConvertBrush(PythonQt.Qt.QColor(PythonQt.QtCore.Qt.red))==PythonQt.Qt.QBrush(PythonQt.QtCore.Qt.red): obj.setPassed();\n"));
133 134 QVERIFY(_helper->runScript("if obj.setAutoConvertPen(PythonQt.Qt.QColor(PythonQt.QtCore.Qt.red))==PythonQt.Qt.QPen(PythonQt.QtCore.Qt.red): obj.setPassed();\n"));
134 135 QVERIFY(_helper->runScript("if obj.setAutoConvertCursor(PythonQt.Qt.QCursor(PythonQt.QtCore.Qt.UpArrowCursor)).shape()==PythonQt.Qt.QCursor(PythonQt.QtCore.Qt.UpArrowCursor).shape(): obj.setPassed();\n"));
135 136 QVERIFY(_helper->runScript("if obj.setAutoConvertCursor(PythonQt.QtCore.Qt.UpArrowCursor).shape()==PythonQt.Qt.QCursor(PythonQt.QtCore.Qt.UpArrowCursor).shape(): obj.setPassed();\n"));
136 137 }
137 138
138 139 void PythonQtTestSlotCalling::testNoArgSlotCall()
139 140 {
140 141 QVERIFY(_helper->runScript("obj.testNoArg(); obj.setPassed();\n"));
141 142 }
142 143
143 144 void PythonQtTestSlotCalling::testOverloadedCall()
144 145 {
145 146 QVERIFY(_helper->runScript("obj.overload(False); obj.setPassed();\n", 0));
146 147 QVERIFY(_helper->runScript("obj.overload(True); obj.setPassed();\n", 0));
147 148 QVERIFY(_helper->runScript("obj.overload(12.5); obj.setPassed();\n", 1));
148 149 QVERIFY(_helper->runScript("obj.overload(12); obj.setPassed();\n", 2));
149 150 QVERIFY(_helper->runScript("obj.overload('test'); obj.setPassed();\n", 3));
150 151 QVERIFY(_helper->runScript("obj.overload(u'test'); obj.setPassed();\n", 3));
151 152 QVERIFY(_helper->runScript("obj.overload(('test','test2')); obj.setPassed();\n", 4));
152 153 QVERIFY(_helper->runScript("obj.overload(obj); obj.setPassed();\n", 5));
153 154 QVERIFY(_helper->runScript("obj.overload(12,13); obj.setPassed();\n", 6));
154 155 }
155 156
156 157 void PythonQtTestSlotCalling::testPyObjectSlotCall()
157 158 {
158 159 QVERIFY(_helper->runScript("if obj.getPyObject(PythonQt)==PythonQt: obj.setPassed();\n"));
159 160 QVERIFY(_helper->runScript("if obj.getPyObject('Hello')=='Hello': obj.setPassed();\n"));
160 161 QVERIFY(_helper->runScript("if obj.getPyObjectFromVariant(PythonQt)==PythonQt: obj.setPassed();\n"));
161 162 QVERIFY(_helper->runScript("if obj.getPyObjectFromVariant2(PythonQt)==PythonQt: obj.setPassed();\n"));
162 163 // QVERIFY(_helper->runScript("if obj.getPyObjectFromPtr(PythonQt)==PythonQt: obj.setPassed();\n"));
163 164 }
164 165
165 166 void PythonQtTestSlotCalling::testCPPSlotCalls()
166 167 {
167 168 // test QColor compare operation
168 169 QVERIFY(_helper->runScript("if PythonQt.QtGui.QColor(1,2,3)==PythonQt.QtGui.QColor(1,2,3): obj.setPassed();obj.testNoArg()\n"));
169 170 QVERIFY(_helper->runScript("if PythonQt.QtGui.QColor(1,2,3)!=PythonQt.QtGui.QColor(3,2,1): obj.setPassed();obj.testNoArg()\n"));
170 171
171 172 // test passing/returning QColors
172 173 QVERIFY(_helper->runScript("if obj.getQColor1(PythonQt.QtGui.QColor(1,2,3))==PythonQt.QtGui.QColor(1,2,3): obj.setPassed();\n"));
173 174 QVERIFY(_helper->runScript("if obj.getQColor2(PythonQt.QtGui.QColor(1,2,3))==PythonQt.QtGui.QColor(1,2,3): obj.setPassed();\n"));
174 175 QVERIFY(_helper->runScript("if obj.getQColor3(PythonQt.QtGui.QColor(1,2,3))==PythonQt.QtGui.QColor(1,2,3): obj.setPassed();\n"));
175 176 QVERIFY(_helper->runScript("if obj.getQColor4(PythonQt.QtGui.QColor(1,2,3))==PythonQt.QtGui.QColor(1,2,3): obj.setPassed();\n"));
176 177 QVERIFY(_helper->runScript("if obj.getQColor5()==PythonQt.QtGui.QColor(1,2,3): obj.setPassed();\n"));
177 178 }
178 179
179 180 void PythonQtTestSlotCalling::testPODSlotCalls()
180 181 {
181 182 QVERIFY(_helper->runScript("if obj.getBool(False)==False: obj.setPassed();\n"));
182 183 QVERIFY(_helper->runScript("if obj.getBool(True)==True: obj.setPassed();\n"));
183 184 QVERIFY(_helper->runScript("if obj.getInt(-42)==-42: obj.setPassed();\n"));
184 185 QVERIFY(_helper->runScript("if obj.getUInt(42)==42: obj.setPassed();\n"));
185 186 QVERIFY(_helper->runScript("if obj.getShort(-43)==-43: obj.setPassed();\n"));
186 187 QVERIFY(_helper->runScript("if obj.getUShort(43)==43: obj.setPassed();\n"));
187 188 QVERIFY(_helper->runScript("if obj.getChar(-12)==-12: obj.setPassed();\n"));
188 189 QVERIFY(_helper->runScript("if obj.getUChar(12)==12: obj.setPassed();\n"));
189 190 QVERIFY(_helper->runScript("if obj.getLong(-256*256*256)==-256*256*256: obj.setPassed();\n"));
190 191 QVERIFY(_helper->runScript("if obj.getULong(256*256*256)==256*256*256: obj.setPassed();\n"));
191 192 QVERIFY(_helper->runScript("if obj.getLongLong(-42)==-42: obj.setPassed();\n"));
192 193 QVERIFY(_helper->runScript("if obj.getULongLong(42)==42: obj.setPassed();\n"));
193 194 QVERIFY(_helper->runScript("if obj.getQChar(4096)==4096: obj.setPassed();\n"));
194 195 QVERIFY(_helper->runScript("if obj.getDouble(47.12)==47.12: obj.setPassed();\n"));
195 196 QVERIFY(_helper->runScript("if abs(obj.getFloat(47.11)-47.11)<0.01: obj.setPassed();\n"));
196 197 QVERIFY(_helper->runScript("if obj.getQString('testStr')=='testStr': obj.setPassed();\n"));
197 198 QVERIFY(_helper->runScript("if obj.getQString('')=='': obj.setPassed();\n"));
198 199 QVERIFY(_helper->runScript("if obj.getQStringList(('test','test2'))==('test','test2'): obj.setPassed();\n"));
199 200 }
200 201
201 202 void PythonQtTestSlotCalling::testQVariantSlotCalls()
202 203 {
203 204 QVERIFY(_helper->runScript("if obj.getQVariant(-42)==-42: obj.setPassed();\n"));
204 205 QVERIFY(_helper->runScript("if obj.getQVariant('testStr')=='testStr': obj.setPassed();\n"));
205 206 QVERIFY(_helper->runScript("if obj.getQVariant(('test','test2'))==('test','test2'): obj.setPassed();\n"));
206 207 QVERIFY(_helper->runScript("if obj.getQVariant(('test',12, 47.11))==('test',12, 47.11): obj.setPassed();\n"));
207 208 QVERIFY(_helper->runScript("if obj.getQVariant({'test':'bla','test2':47.11})=={'test':'bla','test2':47.11}: obj.setPassed();\n"));
208 209 QEXPECT_FAIL("", "Testing to pass a map and compare with a different map", Continue);
209 210 QVERIFY(_helper->runScript("if obj.getQVariant({'test':'bla2','test2':47.11})=={'test':'bla','test2':47.11}: obj.setPassed();\n"));
210 211 QVERIFY(_helper->runScript("if obj.getQVariant(obj)==obj: obj.setPassed();\n"));
211 212 }
212 213
213 214 void PythonQtTestSlotCalling::testObjectSlotCalls()
214 215 {
215 216 QVERIFY(_helper->runScript("if obj.getQObject(obj)==obj: obj.setPassed();\n"));
216 217 QVERIFY(_helper->runScript("if obj.getTestObject(obj)==obj: obj.setPassed();\n"));
217 218 QVERIFY(_helper->runScript("if obj.getNewObject().className()=='PythonQtTestSlotCallingHelper': obj.setPassed();\n"));
218 219 QEXPECT_FAIL("", "Testing to pass a QObject when another object was expected", Continue);
219 220 QVERIFY(_helper->runScript("if obj.getQWidget(obj)==obj: obj.setPassed();\n"));
220 221 }
221 222
222 223 void PythonQtTestSlotCalling::testCppFactory()
223 224 {
224 225 PythonQtTestCppFactory* f = new PythonQtTestCppFactory;
225 226 PythonQt::self()->addInstanceDecorators(new PQCppObjectDecorator);
226 227 // do not register, since we want to know if that works as well
227 228 //qRegisterMetaType<PQCppObjectNoWrap>("PQCppObjectNoWrap");
228 229 PythonQt::self()->addDecorators(new PQCppObjectNoWrapDecorator);
229 230
230 231 PythonQt::self()->addWrapperFactory(f);
231 232 QVERIFY(_helper->runScript("if obj.createPQCppObject(12).getHeight()==12: obj.setPassed();\n"));
232 233 QVERIFY(_helper->runScript("if obj.createPQCppObject(12).getH()==12: obj.setPassed();\n"));
233 234 QVERIFY(_helper->runScript("pq1 = obj.createPQCppObject(12);\n"
234 235 "pq2 = obj.createPQCppObject(13);\n"
235 236 "pq3 = obj.getPQCppObject(pq1);\n"
236 237 "pq4 = obj.getPQCppObject(pq2);\n"
237 238 "if pq3.getHeight()==12 and pq4.getHeight()==13: obj.setPassed();\n"
238 239 ));
239 240
240 241 QVERIFY(_helper->runScript("if obj.createPQCppObjectNoWrap(12).getH()==12: obj.setPassed();\n"));
241 242
242 243 QVERIFY(_helper->runScript("if obj.getPQCppObjectNoWrapAsValue().getH()==47: obj.setPassed();\n"));
243 244
244 245 qRegisterMetaType<PQUnknownButRegisteredValueObject>("PQUnknownButRegisteredValueObject");
245 246 QVERIFY(_helper->runScript("a = obj.getUnknownButRegisteredValueObjectAsPtr();print a;\nif a!=None: obj.setPassed();\n"));
246 247 QVERIFY(_helper->runScript("a = obj.getUnknownButRegisteredValueObjectAsValue();print a;\nif a!=None: obj.setPassed();\n"));
247 248 QVERIFY(_helper->runScript("a = obj.getUnknownValueObjectAsPtr();print a;\nif a!=None: obj.setPassed();\n"));
248 249 QEXPECT_FAIL("", "Testing by value return without the object being registered as QMetaType or having registered a default constructor decorator", Continue);
249 250 QVERIFY(_helper->runScript("a = obj.getUnknownValueObjectAsValue();print a;\nif a!=None: obj.setPassed();\n"));
250 251
251 252 // expect to get strict call to double overload
252 253 QVERIFY(_helper->runScript("obj.testNoArg()\nfrom PythonQt import PQCppObjectNoWrap\na = PQCppObjectNoWrap(22.2)\nif a.getH()==2: obj.setPassed();\n"));
253 254 // expect to get un-strict call to double overload
254 255 QVERIFY(_helper->runScript("obj.testNoArg()\nfrom PythonQt import PQCppObjectNoWrap\na = PQCppObjectNoWrap(22)\nif a.getH()==2: obj.setPassed();\n"));
255 256 // expect to get strict call to copy constructor overload
256 257 QVERIFY(_helper->runScript("obj.testNoArg()\nfrom PythonQt import PQCppObjectNoWrap\na = PQCppObjectNoWrap(PQCppObjectNoWrap())\nprint a.getH()\nif a.getH()==1: obj.setPassed();\n"));
257 258
258 259 // test decorated enums
259 260 // already registered by signals test
260 261 //PythonQt::self()->registerCPPClass("PQCppObject2",NULL,NULL, PythonQtCreateObject<PQCppObject2Decorator>);
261 262
262 263 // local enum (decorated)
263 264 QVERIFY(_helper->runScript("obj.testNoArg()\nfrom PythonQt import PQCppObject2\na = PQCppObject2()\nprint a.testEnumFlag1\nif a.testEnumFlag1(PQCppObject2.TestEnumValue2)==PQCppObject2.TestEnumValue2: obj.setPassed();\n"));
264 265 // enum with namespace (decorated)
265 266 QVERIFY(_helper->runScript("obj.testNoArg()\nfrom PythonQt import PQCppObject2\na = PQCppObject2()\nif a.testEnumFlag2(PQCppObject2.TestEnumValue2)==PQCppObject2.TestEnumValue2: obj.setPassed();\n"));
266 267 // with int overload to check overloading
267 268 QVERIFY(_helper->runScript("obj.testNoArg()\nfrom PythonQt import PQCppObject2\na = PQCppObject2()\nif a.testEnumFlag3(PQCppObject2.TestEnumValue2)==PQCppObject2.TestEnumValue2: obj.setPassed();\n"));
268 269
269 270 }
270 271
271 272 PQCppObject2Decorator::TestEnumFlag PQCppObject2Decorator::testEnumFlag1(PQCppObject2* obj, PQCppObject2Decorator::TestEnumFlag flag) {
272 273 return flag;
273 274 }
274 275
275 276 PQCppObject2::TestEnumFlag PQCppObject2Decorator::testEnumFlag2(PQCppObject2* obj, PQCppObject2::TestEnumFlag flag) {
276 277 return flag;
277 278 }
278 279
279 280 // with int overload
280 281 PQCppObject2Decorator::TestEnumFlag PQCppObject2Decorator::testEnumFlag3(PQCppObject2* obj, int flag) {
281 282 return (TestEnumFlag)-1;
282 283 }
283 284 PQCppObject2Decorator::TestEnumFlag PQCppObject2Decorator::testEnumFlag3(PQCppObject2* obj, PQCppObject2Decorator::TestEnumFlag flag) {
284 285 return flag;
285 286 }
286 287
287 288 void PythonQtTestSlotCalling::testMultiArgsSlotCall()
288 289 {
289 290 QVERIFY(_helper->runScript("if obj.getMultiArgs(12,47.11,'test')==(12,47.11,'test'): obj.setPassed();\n"));
290 291 }
291 292
292 293 bool PythonQtTestSlotCallingHelper::runScript(const char* script, int expectedOverload)
293 294 {
294 295 _called = false;
295 296 _passed = false;
296 297 _calledOverload = -1;
297 298 PyRun_SimpleString(script);
298 299 return _called && _passed && _calledOverload==expectedOverload;
299 300 }
300 301
301 302
302 303 void PythonQtTestSignalHandler::initTestCase()
303 304 {
304 305 _helper = new PythonQtTestSignalHandlerHelper(this);
305 306 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
306 307 PythonQt::self()->addObject(main, "obj", _helper);
307 308 }
308 309
309 310 void PythonQtTestSignalHandler::testSignalHandler()
310 311 {
311 312 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
312 313 PyRun_SimpleString("def testIntSignal(a):\n if a==12: obj.setPassed();\n");
313 314 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(intSignal(int)), main, "testIntSignal"));
314 315 QVERIFY(_helper->emitIntSignal(12));
315 316
316 317 PyRun_SimpleString("def testFloatSignal(a):\n if a==12: obj.setPassed();\n");
317 318 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(floatSignal(float)), main, "testFloatSignal"));
318 319 QVERIFY(_helper->emitFloatSignal(12));
319 320
320 321 // test decorated enums
321 322 PythonQt::self()->registerCPPClass("PQCppObject2",NULL,NULL, PythonQtCreateObject<PQCppObject2Decorator>);
322 323
323 324 PyRun_SimpleString("def testEnumSignal(a):\n if a==1: obj.setPassed();\n");
324 325 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(enumSignal(PQCppObject2::TestEnumFlag)), main, "testEnumSignal"));
325 326 QVERIFY(_helper->emitEnumSignal(PQCppObject2::TestEnumValue2));
326 327
327 328 PyRun_SimpleString("def testVariantSignal(a):\n if a==obj.expectedVariant(): obj.setPassed();\n");
328 329 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(variantSignal(QVariant)), main, "testVariantSignal"));
329 330 _helper->setExpectedVariant(QString("Test"));
330 331 QVERIFY(_helper->emitVariantSignal(QString("Test")));
331 332 _helper->setExpectedVariant(12);
332 333 QVERIFY(_helper->emitVariantSignal(12));
333 334 _helper->setExpectedVariant(QStringList() << "test1" << "test2");
334 335 QVERIFY(_helper->emitVariantSignal(QStringList() << "test1" << "test2"));
335 336 _helper->setExpectedVariant(qVariantFromValue((QObject*)_helper));
336 337 QVERIFY(_helper->emitVariantSignal(qVariantFromValue((QObject*)_helper)));
337 338
338 339 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");
339 340 // intentionally not normalized signal:
340 341 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(complexSignal( int, float , const QStringList , QObject*)), main, "testComplexSignal"));
341 342 QVERIFY(_helper->emitComplexSignal(12,13,QStringList() << "test1" << "test2", _helper));
342 343
343 344 // try removing the handler
344 345 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(complexSignal( int, float , const QStringList , QObject*)), main, "testComplexSignal"));
345 346 // and emit the signal, which should fail because the handler was removed
346 347 QVERIFY(!_helper->emitComplexSignal(12,13,QStringList() << "test1" << "test2", _helper));
347 348
348 349 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(intSignal(int)), main, "testIntSignal"));
349 350 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(floatSignal(float)), main, "testFloatSignal"));
350 351 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(variantSignal(QVariant)), main, "testVariantSignal"));
351 352 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(enumSignal(PQCppObject2::TestEnumFlag)), main, "testEnumSignal"));
352 353
353 354 }
354 355
355 356 void PythonQtTestSignalHandler::testRecursiveSignalHandler()
356 357 {
357 358 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
358 359 PyRun_SimpleString("def testSignal1(a):\n obj.emitSignal2(a);\n");
359 360 PyRun_SimpleString("def testSignal2(a):\n obj.emitSignal3(float(a));\n");
360 361 PyRun_SimpleString("def testSignal3(a):\n if a==12: obj.setPassed();\n");
361 362 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(signal1(int)), main, "testSignal1"));
362 363 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(signal2(const QString&)), main, "testSignal2"));
363 364 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(signal3(float)), main, "testSignal3"));
364 365 QVERIFY(_helper->emitSignal1(12));
365 366 }
366 367
367 368
368 369 void PythonQtTestApi::initTestCase()
369 370 {
370 371 _helper = new PythonQtTestApiHelper();
371 372 _main = PythonQt::self()->getMainModule();
372 373 _main.evalScript("import PythonQt");
373 374 _main.addObject("obj", _helper);
374 375 }
375 376
377 void PythonQtTestApi::testProperties()
378 {
379 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
380 // check for name alias (for backward comp to Qt3)
381 main.evalScript("obj.name = 'hello'");
382 QVERIFY(QString("hello") == main.getVariable("obj.name").toString());
383
384 main.evalScript("obj.objectName = 'hello2'");
385 QVERIFY(QString("hello2") == main.getVariable("obj.objectName").toString());
386
387 }
388
389 void PythonQtTestApi::testDynamicProperties()
390 {
391 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
392
393 // this fails and should fail, but how could that be tested?
394 // main.evalScript("obj.testProp = 1");
395
396 // create a new dynamic property
397 main.evalScript("obj.setProperty('testProp','testValue')");
398
399 // read the property
400 QVERIFY(QString("testValue") == main.getVariable("obj.testProp").toString());
401 // modify and read again
402 main.evalScript("obj.testProp = 12");
403 QVERIFY(12 == main.getVariable("obj.testProp").toInt());
404
405 // check if dynamic property is in dict
406 QVERIFY(12 == main.evalScript("obj.__dict__['testProp']", Py_eval_input).toInt());
407
408 // check if dynamic property is in introspection
409 QStringList l = PythonQt::self()->introspection(PythonQt::self()->getMainModule(), "obj", PythonQt::Anything);
410 QVERIFY(l.contains("testProp"));
411
412 // check with None, previous value expected
413 main.evalScript("obj.testProp = None");
414 QVERIFY(12 == main.getVariable("obj.testProp").toInt());
415
416 }
417
418
376 419 bool PythonQtTestApiHelper::call(const QString& function, const QVariantList& args, const QVariant& expectedResult) {
377 420 _passed = false;
378 421 QVariant r = PythonQt::self()->call(PythonQt::self()->getMainModule(), function, args);
379 422 return _passed && expectedResult==r;
380 423 }
381 424
382 425 void PythonQtTestApi::testCall()
383 426 {
384 427 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
385 428
386 429 QVERIFY(qVariantValue<QObject*>(PythonQt::self()->getVariable(main, "obj"))==_helper);
387 430
388 431 PyRun_SimpleString("def testCallNoArgs():\n obj.setPassed();\n");
389 432 QVERIFY(_helper->call("testCallNoArgs", QVariantList(), QVariant()));
390 433
391 434 PyRun_SimpleString("def testCall1(a):\n if a=='test': obj.setPassed();\n return 'test2';\n");
392 435 QVERIFY(_helper->call("testCall1", QVariantList() << QVariant("test"), QVariant(QString("test2"))));
393 436
394 437 PyRun_SimpleString("def testCall2(a, b):\n if a=='test' and b==obj: obj.setPassed();\n return obj;\n");
395 438 QVariant r = PythonQt::self()->call(PythonQt::self()->getMainModule(), "testCall2", QVariantList() << QVariant("test") << qVariantFromValue((QObject*)_helper));
396 439 QObject* p = qVariantValue<QObject*>(r);
397 440 QVERIFY(p==_helper);
398 441 }
399 442
400 443 void PythonQtTestApi::testVariables()
401 444 {
402 445 PythonQt::self()->addObject(PythonQt::self()->getMainModule(), "someObject", _helper);
403 446 QVariant v = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someObject");
404 447 QObject* p = qVariantValue<QObject*>(v);
405 448 QVERIFY(p==_helper);
406 449 // test for unset variable
407 450 QVariant v2 = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someObject2");
408 451 QVERIFY(v2==QVariant());
409 452
410 453 PythonQt::self()->addVariable(PythonQt::self()->getMainModule(), "someValue", QStringList() << "test1" << "test2");
411 454 QVariant v3 = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someValue");
412 455 QVERIFY(v3 == QVariant(QStringList() << "test1" << "test2"));
413 456
414 457 QStringList l = PythonQt::self()->introspection(PythonQt::self()->getMainModule(), QString::null, PythonQt::Variable);
415 458 QSet<QString> s;
416 459 // check that at least these three variables are set
417 460 s << "obj" << "someObject" << "someValue";
418 461 foreach (QString value, s) {
419 462 QVERIFY(l.indexOf(value)!=-1);
420 463 }
421 464
422 465 // insert a second time!
423 466 PythonQt::self()->addObject(PythonQt::self()->getMainModule(), "someObject", _helper);
424 467 // and remove
425 468 PythonQt::self()->removeVariable(PythonQt::self()->getMainModule(), "someObject");
426 469 // we expect to find no variable
427 470 QVariant v4 = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someObject");
428 471 QVERIFY(v4==QVariant());
429 472 }
430 473
431 474 void PythonQtTestApi::testImporter()
432 475 {
433 476 PythonQt::self()->setImporter(_helper);
434 477 PythonQt::self()->overwriteSysPath(QStringList() << "c:\\test");
435 478 PyRun_SimpleString("import bla\n");
436 479 }
437 480
438 481 void PythonQtTestApi::testQtNamespace()
439 482 {
440 483 QVERIFY(_main.getVariable("PythonQt.QtCore.Qt.red").toInt()==Qt::red);
441 484 QVERIFY(_main.getVariable("PythonQt.QtCore.Qt.FlatCap").toInt()==Qt::FlatCap);
442 485 QVERIFY(PythonQtObjectPtr(_main.getVariable("PythonQt.QtCore.Qt.escape")));
443 486 // check for an enum type wrapper
444 487 QVERIFY(PythonQtObjectPtr(_main.getVariable("PythonQt.QtCore.Qt.AlignmentFlag")));
445 488 // check for a flags type wrapper
446 489 QVERIFY(PythonQtObjectPtr(_main.getVariable("PythonQt.QtCore.Qt.Alignment")));
447 490 }
448 491
449 492 void PythonQtTestApi::testConnects()
450 493 {
451 494 // QVERIFY(qVariantValue<QColor>(_main.evalScript("PythonQt.Qt.QColor(PythonQt.Qt.Qt.red)" ,Py_eval_input)) == QColor(Qt::red));
452 495 //TODO: add signal/slot connect both with QObject.connect and connect
453 496 }
454 497
455 498 void PythonQtTestApi::testQColorDecorators()
456 499 {
457 500 PythonQtObjectPtr colorClass = _main.getVariable("PythonQt.QtGui.QColor");
458 501 QVERIFY(colorClass);
459 502 // verify that the class is in the correct module
460 503 QVERIFY(colorClass.getVariable("__module__") == "PythonQt.QtGui");
461 504 // test on Qt module as well:
462 505 colorClass = _main.getVariable("PythonQt.Qt.QColor");
463 506 QVERIFY(colorClass);
464 507 // constructors
465 508 QVERIFY(qVariantValue<QColor>(colorClass.call(QVariantList() << 1 << 2 << 3)) == QColor(1,2,3));
466 509 QVERIFY(qVariantValue<QColor>(colorClass.call()) == QColor());
467 510 QEXPECT_FAIL("", "Testing non-existing constructor", Continue);
468 511 QVERIFY(colorClass.call(QVariantList() << 1 << 2) != QVariant());
469 512
470 513 // check that enum overload is taken over int
471 514 QVERIFY(qVariantValue<QColor>(_main.evalScript("PythonQt.Qt.QColor(PythonQt.Qt.Qt.red)" ,Py_eval_input)) == QColor(Qt::red));
472 515 // check that int overload is taken over enum
473 516 QVERIFY(qVariantValue<QColor>(_main.evalScript("PythonQt.Qt.QColor(0x112233)" ,Py_eval_input)) == QColor(0x112233));
474 517
475 518 // check for decorated Cmyk enum value
476 519 QVERIFY(colorClass.getVariable("Cmyk").toInt() == QColor::Cmyk);
477 520 PythonQtObjectPtr staticMethod = colorClass.getVariable("fromRgb");
478 521 QVERIFY(staticMethod);
479 522 // direct call of static method via class
480 523 QVERIFY(qVariantValue<QColor>(colorClass.call("fromRgb", QVariantList() << 1 << 2 << 3)) == QColor(1,2,3));
481 524 // direct call of static method
482 525 QVERIFY(qVariantValue<QColor>(staticMethod.call(QVariantList() << 1 << 2 << 3)) == QColor(1,2,3));
483 526 PythonQtObjectPtr publicMethod = colorClass.getVariable("red");
484 527 QVERIFY(publicMethod);
485 528 // call with passing self in:
486 529 QVERIFY(colorClass.call("red", QVariantList() << QColor(255,0,0)).toInt() == 255);
487 530 }
488 531
489 532 QByteArray PythonQtTestApiHelper::readFileAsBytes(const QString& filename)
490 533 {
491 534 QByteArray b;
492 535 return b;
493 536 }
494 537
495 538 QByteArray PythonQtTestApiHelper::readSourceFile(const QString& filename, bool& ok)
496 539 {
497 540 QByteArray b;
498 541 ok = true;
499 542 return b;
500 543 }
501 544
502 545 bool PythonQtTestApiHelper::exists(const QString& filename)
503 546 {
504 547 return true;
505 548 }
506 549
507 550 QDateTime PythonQtTestApiHelper::lastModifiedDate(const QString& filename) {
508 551 return QDateTime::currentDateTime();
509 552 }
510 553
511 554
512 555 void PythonQtTestApi::testRedirect()
513 556 {
514 557 connect(PythonQt::self(), SIGNAL(pythonStdOut(const QString&)), _helper, SLOT(stdOut(const QString&)));
515 558 connect(PythonQt::self(), SIGNAL(pythonStdErr(const QString&)), _helper, SLOT(stdErr(const QString&)));
516 559 PyRun_SimpleString("print 'test'\n");
517 560 }
518 561
519 562 void PythonQtTestApiHelper::stdOut(const QString& s)
520 563 {
521 564 qDebug() << s;
522 565 }
523 566
524 567 void PythonQtTestApiHelper::stdErr(const QString& s)
525 568 {
526 569 qDebug() << s;
527 570 }
528 571
529 572 QObject* PythonQtTestCppFactory::create(const QByteArray& name, void *ptr)
530 573 {
531 574 if (name == "PQCppObject") {
532 575 return new PQCppObjectWrapper(ptr);
533 576 }
534 577 return NULL;
535 578 }
@@ -1,512 +1,515
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 #include <QPen>
52 52 #include <QColor>
53 53 #include <QBrush>
54 54 #include <QCursor>
55 55
56 56 class PythonQtTestSlotCallingHelper;
57 57 class PythonQtTestApiHelper;
58 58 class QWidget;
59 59
60 60 //! test the PythonQt api
61 61 class PythonQtTestApi : public QObject
62 62 {
63 63 Q_OBJECT
64 64
65 65 private slots:
66 66 void initTestCase();
67 67 void testCall();
68 68 void testVariables();
69 69 void testRedirect();
70 70 void testImporter();
71 71 void testQColorDecorators();
72 72 void testQtNamespace();
73 73 void testConnects();
74 74
75 void testProperties();
76 void testDynamicProperties();
77
75 78 private:
76 79 PythonQtTestApiHelper* _helper;
77 80 PythonQtObjectPtr _main;
78 81
79 82 };
80 83
81 84 class ClassA {
82 85 public:
83 86 ClassA() { x = 1; }
84 87 int x;
85 88 };
86 89
87 90 class ClassB {
88 91 public:
89 92 ClassB() { y = 2; }
90 93 int y;
91 94
92 95 virtual int type() { return 2; }
93 96 };
94 97
95 98 class ClassC : public ClassA, public ClassB {
96 99 public:
97 100 ClassC() { z = 3; }
98 101 int z;
99 102
100 103 virtual int type() { return 3; }
101 104 };
102 105
103 106 class ClassD : public QObject, public ClassA, public ClassB {
104 107 Q_OBJECT
105 108 public:
106 109 ClassD() { d = 4; }
107 110 public slots:
108 111 int getD() { return d; }
109 112 private:
110 113 int d;
111 114
112 115 virtual int type() { return 4; }
113 116 };
114 117
115 118 class ClassAWrapper : public QObject {
116 119 Q_OBJECT
117 120 public slots:
118 121 ClassA* new_ClassA() { return new ClassA; }
119 122 int getX(ClassA* o) { return o->x; }
120 123 };
121 124
122 125 class ClassBWrapper : public QObject {
123 126 Q_OBJECT
124 127 public slots:
125 128 ClassB* new_ClassB() { return new ClassB; }
126 129 int getY(ClassB* o) { return o->y; }
127 130 };
128 131
129 132 class ClassCWrapper : public QObject {
130 133 Q_OBJECT
131 134 public slots:
132 135 ClassC* new_ClassC() { return new ClassC; }
133 136 int getZ(ClassC* o) { return o->z; }
134 137 };
135 138
136 139 class ClassDWrapper : public QObject {
137 140 Q_OBJECT
138 141 public slots:
139 142 ClassD* new_ClassD() { return new ClassD; }
140 143 };
141 144
142 145
143 146 //! test the PythonQt api (helper)
144 147 class PythonQtTestApiHelper : public QObject , public PythonQtImportFileInterface
145 148 {
146 149 Q_OBJECT
147 150 public:
148 151 PythonQtTestApiHelper() {
149 152 };
150 153
151 154 bool call(const QString& function, const QVariantList& args, const QVariant& expectedResult);
152 155
153 156 virtual QByteArray readFileAsBytes(const QString& filename);
154 157
155 158 virtual QByteArray readSourceFile(const QString& filename, bool& ok);
156 159
157 160 virtual bool exists(const QString& filename);
158 161
159 162 virtual QDateTime lastModifiedDate(const QString& filename);
160 163
161 164 public slots:
162 165
163 166 //! call to set that the test has passed (from Python!)
164 167 void setPassed() { _passed = true; }
165 168
166 169 void stdOut(const QString&);
167 170 void stdErr(const QString&);
168 171
169 172 private:
170 173 bool _passed;
171 174 };
172 175
173 176
174 177 // test implementation of the wrapper factory
175 178 class PythonQtTestCppFactory : public PythonQtCppWrapperFactory
176 179 {
177 180 public:
178 181 virtual QObject* create(const QByteArray& name, void *ptr);
179 182 };
180 183
181 184 //! an cpp object to be wrapped
182 185 class PQCppObject {
183 186
184 187 public:
185 188 PQCppObject(int h) { _height = h; }
186 189
187 190 int getHeight() { return _height; }
188 191 void setHeight(int h) { _height = h; }
189 192
190 193 private:
191 194 int _height;
192 195 };
193 196
194 197 //! an qobject that wraps the existing cpp object
195 198 class PQCppObjectWrapper : public QObject {
196 199 Q_OBJECT
197 200 public:
198 201 PQCppObjectWrapper(void* ptr) {
199 202 _ptr = (PQCppObject*)ptr;
200 203 }
201 204
202 205 public slots:
203 206 int getHeight() { return _ptr->getHeight(); }
204 207 void setHeight(int h) { _ptr->setHeight(h); }
205 208
206 209 private:
207 210 PQCppObject* _ptr;
208 211 };
209 212
210 213 class PQCppObjectDecorator : public QObject {
211 214 Q_OBJECT
212 215 public slots:
213 216 int getH(PQCppObject* obj) { return obj->getHeight(); }
214 217
215 218 };
216 219
217 220 //! an cpp object to be wrapped by decorators only
218 221 class PQCppObjectNoWrap {
219 222
220 223 public:
221 224 PQCppObjectNoWrap() { _height = 0; }
222 225 PQCppObjectNoWrap(int h) { _height = h; }
223 226
224 227 int getHeight() { return _height; }
225 228 void setHeight(int h) { _height = h; }
226 229
227 230 private:
228 231 int _height;
229 232 };
230 233
231 234 class PQCppObjectNoWrapDecorator : public QObject {
232 235 Q_OBJECT
233 236
234 237 public slots:
235 238 PQCppObjectNoWrap* new_PQCppObjectNoWrap() {
236 239 return new PQCppObjectNoWrap(0);
237 240 }
238 241 PQCppObjectNoWrap* new_PQCppObjectNoWrap(const PQCppObjectNoWrap& other) {
239 242 return new PQCppObjectNoWrap(1);
240 243 }
241 244 PQCppObjectNoWrap* new_PQCppObjectNoWrap(double value) {
242 245 return new PQCppObjectNoWrap(2);
243 246 }
244 247
245 248 int getH(PQCppObjectNoWrap* obj) { return obj->getHeight(); }
246 249
247 250 };
248 251
249 252
250 253 //! an cpp object that is to be wrapped by decorators only
251 254 class PQCppObject2 {
252 255
253 256 public:
254 257 enum TestEnumFlag {
255 258 TestEnumValue1 = 0,
256 259 TestEnumValue2 = 1
257 260 };
258 261
259 262 PQCppObject2() {}
260 263
261 264 };
262 265
263 266 class PQCppObject2Decorator : public QObject {
264 267 Q_OBJECT
265 268
266 269 public:
267 270 Q_ENUMS(TestEnumFlag)
268 271 Q_FLAGS(TestEnum)
269 272
270 273 enum TestEnumFlag {
271 274 TestEnumValue1 = 0,
272 275 TestEnumValue2 = 1
273 276 };
274 277
275 278 Q_DECLARE_FLAGS(TestEnum, TestEnumFlag)
276 279
277 280 public slots:
278 281 PQCppObject2* new_PQCppObject2() {
279 282 return new PQCppObject2();
280 283 }
281 284
282 285 TestEnumFlag testEnumFlag1(PQCppObject2* obj, TestEnumFlag flag);
283 286
284 287 PQCppObject2::TestEnumFlag testEnumFlag2(PQCppObject2* obj, PQCppObject2::TestEnumFlag flag);
285 288
286 289 // with int overload
287 290 TestEnumFlag testEnumFlag3(PQCppObject2* obj, int flag);
288 291 TestEnumFlag testEnumFlag3(PQCppObject2* obj, TestEnumFlag flag);
289 292
290 293 };
291 294
292 295 class PQUnknownValueObject
293 296 {
294 297 public:
295 298 PQUnknownValueObject() {};
296 299 };
297 300
298 301 class PQUnknownButRegisteredValueObject
299 302 {
300 303 public:
301 304 PQUnknownButRegisteredValueObject() {};
302 305 };
303 306
304 307 //! test the calling of slots
305 308 class PythonQtTestSlotCalling : public QObject
306 309 {
307 310 Q_OBJECT
308 311
309 312 private slots:
310 313 void initTestCase();
311 314 void init();
312 315
313 316 void testNoArgSlotCall();
314 317 void testPODSlotCalls();
315 318 void testCPPSlotCalls();
316 319 void testQVariantSlotCalls();
317 320 void testObjectSlotCalls();
318 321 void testMultiArgsSlotCall();
319 322 void testPyObjectSlotCall();
320 323 void testOverloadedCall();
321 324 void testCppFactory();
322 325 void testInheritance();
323 326 void testAutoConversion();
324 327
325 328 private:
326 329 PythonQtTestSlotCallingHelper* _helper;
327 330
328 331 };
329 332
330 333 //! helper class for slot calling test
331 334 class PythonQtTestSlotCallingHelper : public QObject
332 335 {
333 336 Q_OBJECT
334 337 public:
335 338 PythonQtTestSlotCallingHelper(PythonQtTestSlotCalling* test) {
336 339 _test = test;
337 340 };
338 341
339 342 bool runScript(const char* script, int expectedOverload = -1);
340 343
341 344 public slots:
342 345
343 346 //! call to set that the test has passed (from Python!)
344 347 void setPassed() { _passed = true; }
345 348
346 349 //! no arguments, no return value:
347 350 void testNoArg() { _called = true; }
348 351
349 352 //! overload test!
350 353 void overload(bool a) { _calledOverload = 0; _called = true; }
351 354 void overload(float a) { _calledOverload = 1; _called = true;}
352 355 void overload(int a) { _calledOverload = 2; _called = true;}
353 356 void overload(const QString& str) { _calledOverload = 3; _called = true;}
354 357 void overload(const QStringList& str) { _calledOverload = 4; _called = true;}
355 358 void overload(QObject* str) { _calledOverload = 5; _called = true;}
356 359 void overload(float a, int b) { _calledOverload = 6; _called = true;}
357 360
358 361 //! POD values:
359 362 int getInt(int a) { _called = true; return a; }
360 363 unsigned int getUInt(unsigned int a) { _called = true; return a; }
361 364 bool getBool(bool a) { _called = true; return a; }
362 365 char getChar(char a) { _called = true; return a; }
363 366 unsigned char getUChar(unsigned char a) { _called = true; return a; }
364 367 long getLong(long a) { _called = true; return a; }
365 368 unsigned long getULong(unsigned long a) { _called = true; return a; }
366 369 short getShort(short a) { _called = true; return a; }
367 370 unsigned short getUShort(unsigned short a) { _called = true; return a; }
368 371 QChar getQChar(QChar a) { _called = true; return a; }
369 372 qint64 getLongLong(qint64 a) { _called = true; return a; }
370 373 quint64 getULongLong(quint64 a) { _called = true; return a; }
371 374 double getDouble(double d) { _called = true; return d; }
372 375 float getFloat(float d) { _called = true; return d; }
373 376
374 377 //! important qt types:
375 378 QString getQString(const QString& s) { _called = true; return s; }
376 379 QStringList getQStringList(const QStringList& l) { _called = true; return l; }
377 380 QVariant getQVariant(const QVariant& var) { _called = true; return var; }
378 381
379 382 // QColor as representative for C++ value classes
380 383 QColor getQColor1(const QColor& var) { _called = true; return var; }
381 384 QColor getQColor2(QColor& var) { _called = true; return var; }
382 385 QColor getQColor3(QColor* col) { _called = true; return *col; }
383 386 QColor getQColor4(const QVariant& color) { _called = true; return qVariantValue<QColor>(color); }
384 387 QColor* getQColor5() { _called = true; static QColor c(1,2,3); return &c; }
385 388
386 389 PyObject* getPyObject(PyObject* obj) { _called = true; return obj; }
387 390 PyObject* getPyObjectFromVariant(const QVariant& val) { _called = true; return PythonQtObjectPtr(val); };
388 391 QVariant getPyObjectFromVariant2(const QVariant& val) { _called = true; return val; };
389 392 // this does not yet work but is not required to work:
390 393 //PyObject* getPyObjectFromPtr(const PythonQtObjectPtr& val) { _called = true; return val; };
391 394
392 395 //! testing pointer passing
393 396 PythonQtTestSlotCallingHelper* getTestObject(PythonQtTestSlotCallingHelper* obj) { _called = true; return obj; }
394 397 //! testing inheritance checking
395 398 QObject* getQObject(QObject* obj) { _called = true; return obj; }
396 399 QWidget* getQWidget(QWidget* obj) { _called = true; return obj; }
397 400 //! testing if an object that was not wrapped is wrapped earlier is wrapped correctly
398 401 QObject* getNewObject() { _called = true; return new PythonQtTestSlotCallingHelper(NULL); }
399 402
400 403 QVariantList getMultiArgs(int a, double b, const QString& str) { _called = true; return (QVariantList() << a << b << str); }
401 404
402 405 //! cpp wrapper factory test
403 406 PQCppObject* createPQCppObject(int h) { _called = true; return new PQCppObject(h); }
404 407
405 408 //! cpp wrapper factory test
406 409 PQCppObject* getPQCppObject(PQCppObject* p) { _called = true; return p; }
407 410
408 411 //! cpp wrapper factory test
409 412 PQCppObjectNoWrap* createPQCppObjectNoWrap(int h) { _called = true; return new PQCppObjectNoWrap(h); }
410 413
411 414 //! cpp wrapper factory test
412 415 PQCppObjectNoWrap* getPQCppObjectNoWrap(PQCppObjectNoWrap* p) { _called = true; return p; }
413 416
414 417 //! get a return by value PQCppObjectNoWrap
415 418 PQCppObjectNoWrap getPQCppObjectNoWrapAsValue() { _called = true; return PQCppObjectNoWrap(47); }
416 419
417 420 PQUnknownButRegisteredValueObject getUnknownButRegisteredValueObjectAsValue() { _called = true; return PQUnknownButRegisteredValueObject(); }
418 421 PQUnknownValueObject getUnknownValueObjectAsValue() { _called = true; return PQUnknownValueObject(); }
419 422
420 423 PQUnknownButRegisteredValueObject* getUnknownButRegisteredValueObjectAsPtr() { _called = true; return new PQUnknownButRegisteredValueObject(); }
421 424 PQUnknownValueObject* getUnknownValueObjectAsPtr() { _called = true; return new PQUnknownValueObject(); }
422 425
423 426 ClassA* getClassAPtr(ClassA* o) { _called = true; return o; }
424 427 ClassB* getClassBPtr(ClassB* o) { _called = true; return o; }
425 428 ClassC* getClassCPtr(ClassC* o) { _called = true; return o; }
426 429 ClassD* getClassDPtr(ClassD* o) { _called = true; return o; }
427 430
428 431 ClassA* createClassA() { _called = true; return new ClassA; }
429 432 ClassB* createClassB() { _called = true; return new ClassB; }
430 433 ClassC* createClassC() { _called = true; return new ClassC; }
431 434 ClassD* createClassD() { _called = true; return new ClassD; }
432 435 ClassA* createClassCAsA() { _called = true; return new ClassC; }
433 436 ClassB* createClassCAsB() { _called = true; return new ClassC; }
434 437 ClassA* createClassDAsA() { _called = true; return new ClassD; }
435 438 ClassB* createClassDAsB() { _called = true; return new ClassD; }
436 439
437 440 QColor setAutoConvertColor(const QColor& color) { _called = true; return color; };
438 441 QBrush setAutoConvertBrush(const QBrush& brush) { _called = true; return brush; };
439 442 QPen setAutoConvertPen(const QPen& pen) { _called = true; return pen; };
440 443 QCursor setAutoConvertCursor(const QCursor& cursor) { _called = true; return cursor; };
441 444
442 445 private:
443 446 bool _passed;
444 447 bool _called;
445 448 int _calledOverload;
446 449 PythonQtTestSlotCalling* _test;
447 450 };
448 451
449 452 class PythonQtTestSignalHandlerHelper;
450 453
451 454 //! test the connection of signals to python
452 455 class PythonQtTestSignalHandler : public QObject
453 456 {
454 457 Q_OBJECT
455 458
456 459 private slots:
457 460 void initTestCase();
458 461
459 462 void testSignalHandler();
460 463 void testRecursiveSignalHandler();
461 464
462 465 private:
463 466 PythonQtTestSignalHandlerHelper* _helper;
464 467
465 468 };
466 469
467 470 //! helper class for signal testing
468 471 class PythonQtTestSignalHandlerHelper : public QObject
469 472 {
470 473 Q_OBJECT
471 474
472 475 public:
473 476 PythonQtTestSignalHandlerHelper(PythonQtTestSignalHandler* test) {
474 477 _test = test;
475 478 };
476 479
477 480 public slots:
478 481 void setPassed() { _passed = true; }
479 482
480 483 bool emitIntSignal(int a) { _passed = false; emit intSignal(a); return _passed; };
481 484 bool emitFloatSignal(float a) { _passed = false; emit floatSignal(a); return _passed; };
482 485 bool emitEnumSignal(PQCppObject2::TestEnumFlag flag) { _passed = false; emit enumSignal(flag); return _passed; };
483 486
484 487 bool emitVariantSignal(const QVariant& v) { _passed = false; emit variantSignal(v); return _passed; };
485 488 QVariant expectedVariant() { return _v; }
486 489 void setExpectedVariant(const QVariant& v) { _v = v; }
487 490
488 491 bool emitComplexSignal(int a, float b, const QStringList& l, QObject* obj) { _passed = false; emit complexSignal(a,b,l,obj); return _passed; };
489 492
490 493 bool emitSignal1(int a) { _passed = false; emit signal1(a); return _passed; };
491 494 bool emitSignal2(const QString& s) { _passed = false; emit signal2(s); return _passed; };
492 495 bool emitSignal3(float a) { _passed = false; emit signal3(a); return _passed; };
493 496
494 497 signals:
495 498 void intSignal(int);
496 499 void floatSignal(float);
497 500 void variantSignal(const QVariant& v);
498 501 void complexSignal(int a, float b, const QStringList& l, QObject* obj);
499 502 void enumSignal(PQCppObject2::TestEnumFlag flag);
500 503
501 504 void signal1(int);
502 505 void signal2(const QString&);
503 506 void signal3(float);
504 507
505 508 private:
506 509 bool _passed;
507 510 QVariant _v;
508 511
509 512 PythonQtTestSignalHandler* _test;
510 513 };
511 514
512 515 #endif
General Comments 0
You need to be logged in to leave comments. Login now