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