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