@@ -45,13 +45,86 @@ void PythonQtTestSlotCalling::initTestCase()
4545{
4646 _helper = new PythonQtTestSlotCallingHelper (this );
4747 PythonQtObjectPtr main = PythonQt::self ()->getMainModule ();
48+ main.evalScript (" import PythonQt" );
4849 PythonQt::self ()->addObject (main, " obj" , _helper);
4950}
5051
5152void PythonQtTestSlotCalling::init () {
5253
5354}
5455
56+ void * polymorphic_ClassB_Handler (const void * ptr, char ** className) {
57+ ClassB* o = (ClassB*)ptr;
58+ if (o->type ()==2 ) {
59+ *className = " ClassB" ;
60+ return (ClassB*)o;
61+ }
62+ if (o->type ()==3 ) {
63+ *className = " ClassC" ;
64+ return (ClassC*)o;
65+ }
66+ if (o->type ()==4 ) {
67+ *className = " ClassD" ;
68+ return (ClassD*)o;
69+ }
70+ return NULL ;
71+ }
72+
73+ void PythonQtTestSlotCalling::testInheritance () {
74+ PythonQt::self ()->registerCPPClass (" ClassA" ,NULL ,NULL , PythonQtCreateObject<ClassAWrapper>);
75+ PythonQt::self ()->registerCPPClass (" ClassB" ,NULL ,NULL , PythonQtCreateObject<ClassBWrapper>);
76+ PythonQt::self ()->registerCPPClass (" ClassC" ,NULL ,NULL , PythonQtCreateObject<ClassCWrapper>);
77+ PythonQt::self ()->addParentClass (" ClassC" , " ClassA" , PythonQtUpcastingOffset<ClassC,ClassA>());
78+ PythonQt::self ()->addParentClass (" ClassC" , " ClassB" , PythonQtUpcastingOffset<ClassC,ClassB>());
79+ PythonQt::self ()->registerClass (&ClassD::staticMetaObject, NULL , PythonQtCreateObject<ClassDWrapper>);
80+ PythonQt::self ()->addParentClass (" ClassD" , " ClassA" , PythonQtUpcastingOffset<ClassD,ClassA>());
81+ PythonQt::self ()->addParentClass (" ClassD" , " ClassB" , PythonQtUpcastingOffset<ClassD,ClassB>());
82+
83+ PythonQtObjectPtr classA = PythonQt::self ()->getMainModule ().getVariable (" PythonQt.ClassA" );
84+ PythonQtObjectPtr classB = PythonQt::self ()->getMainModule ().getVariable (" PythonQt.ClassB" );
85+ PythonQtObjectPtr classC = PythonQt::self ()->getMainModule ().getVariable (" PythonQt.ClassC" );
86+ PythonQtObjectPtr classD = PythonQt::self ()->getMainModule ().getVariable (" PythonQt.ClassD" );
87+ QVERIFY (classA);
88+ QVERIFY (classB);
89+ QVERIFY (classC);
90+ QVERIFY (classD);
91+
92+ QVERIFY (_helper->runScript (" a = PythonQt.ClassA();\n if obj.getClassAPtr(a).getX()==1: obj.setPassed();\n " ));
93+ QEXPECT_FAIL (" " , " ClassB can not be converted to ClassA" , Continue);
94+ QVERIFY (_helper->runScript (" a = PythonQt.ClassB();\n if obj.getClassAPtr(a).getX()==1: obj.setPassed();\n " ));
95+ QVERIFY (_helper->runScript (" a = PythonQt.ClassC();\n if obj.getClassAPtr(a).getX()==1: obj.setPassed();\n " ));
96+ QVERIFY (_helper->runScript (" a = PythonQt.ClassD();\n if obj.getClassAPtr(a).getX()==1: obj.setPassed();\n " ));
97+
98+ QEXPECT_FAIL (" " , " ClassA can not be converted to ClassB" , Continue);
99+ QVERIFY (_helper->runScript (" a = PythonQt.ClassA();\n if obj.getClassBPtr(a).getY()==2: obj.setPassed();\n " ));
100+ QVERIFY (_helper->runScript (" a = PythonQt.ClassB();\n if obj.getClassBPtr(a).getY()==2: obj.setPassed();\n " ));
101+ QVERIFY (_helper->runScript (" a = PythonQt.ClassC();\n if obj.getClassBPtr(a).getY()==2: obj.setPassed();\n " ));
102+ QVERIFY (_helper->runScript (" a = PythonQt.ClassD();\n if obj.getClassBPtr(a).getY()==2: obj.setPassed();\n " ));
103+
104+ QEXPECT_FAIL (" " , " ClassA can not be converted to ClassC" , Continue);
105+ QVERIFY (_helper->runScript (" a = PythonQt.ClassA();\n if obj.getClassCPtr(a).getX()==1: obj.setPassed();\n " ));
106+ QEXPECT_FAIL (" " , " ClassB can not be converted to ClassC" , Continue);
107+ QVERIFY (_helper->runScript (" a = PythonQt.ClassB();\n if obj.getClassCPtr(a).getX()==1: obj.setPassed();\n " ));
108+ QVERIFY (_helper->runScript (" a = PythonQt.ClassC();\n if obj.getClassCPtr(a).getX()==1: obj.setPassed();\n " ));
109+ QEXPECT_FAIL (" " , " ClassD can not be converted to ClassC" , Continue);
110+ QVERIFY (_helper->runScript (" a = PythonQt.ClassD();\n if obj.getClassCPtr(a).getX()==1: obj.setPassed();\n " ));
111+
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 " ));
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 " ));
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 " ));
118+ QVERIFY (_helper->runScript (" if type(obj.createClassDAsB())==PythonQt.ClassB: obj.setPassed();\n " ));
119+
120+ PythonQt::self ()->addPolymorphicHandler (" ClassB" , polymorphic_ClassB_Handler);
121+
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 " ));
124+ QVERIFY (_helper->runScript (" if type(obj.createClassDAsB())==PythonQt.ClassD: obj.setPassed();\n " ));
125+
126+ }
127+
55128void PythonQtTestSlotCalling::testNoArgSlotCall ()
56129{
57130 QVERIFY (_helper->runScript (" obj.testNoArg(); obj.setPassed();\n " ));
@@ -70,6 +143,29 @@ void PythonQtTestSlotCalling::testOverloadedCall()
70143 QVERIFY (_helper->runScript (" obj.overload(12,13); obj.setPassed();\n " , 6 ));
71144}
72145
146+ void PythonQtTestSlotCalling::testPyObjectSlotCall ()
147+ {
148+ QVERIFY (_helper->runScript (" if obj.getPyObject(PythonQt)==PythonQt: 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 " ));
151+ QVERIFY (_helper->runScript (" if obj.getPyObjectFromVariant2(PythonQt)==PythonQt: obj.setPassed();\n " ));
152+ // QVERIFY(_helper->runScript("if obj.getPyObjectFromPtr(PythonQt)==PythonQt: obj.setPassed();\n"));
153+ }
154+
155+ void PythonQtTestSlotCalling::testCPPSlotCalls ()
156+ {
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 " ));
159+ QVERIFY (_helper->runScript (" if PythonQt.QtGui.QColor(1,2,3)!=PythonQt.QtGui.QColor(3,2,1): obj.setPassed();obj.testNoArg()\n " ));
160+
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 " ));
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 " ));
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 " ));
167+ }
168+
73169void PythonQtTestSlotCalling::testPODSlotCalls ()
74170{
75171 QVERIFY (_helper->runScript (" if obj.getBool(False)==False: obj.setPassed();\n " ));
@@ -209,8 +305,9 @@ void PythonQtTestSignalHandler::testRecursiveSignalHandler()
209305void PythonQtTestApi::initTestCase ()
210306{
211307 _helper = new PythonQtTestApiHelper ();
212- PythonQtObjectPtr main = PythonQt::self ()->getMainModule ();
213- PythonQt::self ()->addObject (main, " obj" , _helper);
308+ _main = PythonQt::self ()->getMainModule ();
309+ _main.evalScript (" import PythonQt" );
310+ _main.addObject (" obj" , _helper);
214311}
215312
216313bool PythonQtTestApiHelper::call (const QString& function, const QVariantList& args, const QVariant& expectedResult) {
@@ -275,6 +372,42 @@ void PythonQtTestApi::testImporter()
275372 PyRun_SimpleString (" import bla\n " );
276373}
277374
375+ void PythonQtTestApi::testQtNamespace ()
376+ {
377+ QVERIFY (!_main.getVariable (" PythonQt.QtCore.Qt.red" ).toInt ()==Qt::red);
378+ QVERIFY (_main.getVariable (" PythonQt.QtCore.Qt.FlatCap" ).toInt ()==Qt::FlatCap);
379+ QVERIFY (PythonQtObjectPtr (_main.getVariable (" PythonQt.QtCore.Qt.escape" )));
380+ }
381+
382+ void PythonQtTestApi::testQColorDecorators ()
383+ {
384+ PythonQtObjectPtr colorClass = _main.getVariable (" PythonQt.QtGui.QColor" );
385+ QVERIFY (colorClass);
386+ // verify that the class is in the correct module
387+ QVERIFY (colorClass.getVariable (" __module__" ) == " PythonQt.QtGui" );
388+ // test on Qt module as well:
389+ colorClass = _main.getVariable (" PythonQt.Qt.QColor" );
390+ QVERIFY (colorClass);
391+ // constructors
392+ QVERIFY (qVariantValue<QColor>(colorClass.call (QVariantList () << 1 << 2 << 3 )) == QColor (1 ,2 ,3 ));
393+ QVERIFY (qVariantValue<QColor>(colorClass.call ()) == QColor ());
394+ QEXPECT_FAIL (" " , " Testing non-existing constructor" , Continue);
395+ QVERIFY (colorClass.call (QVariantList () << 1 << 2 ) != QVariant ());
396+
397+ // check for decorated Cmyk enum value
398+ QVERIFY (colorClass.getVariable (" Cmyk" ).toInt () == QColor::Cmyk);
399+ PythonQtObjectPtr staticMethod = colorClass.getVariable (" fromRgb" );
400+ QVERIFY (staticMethod);
401+ // direct call of static method via class
402+ QVERIFY (qVariantValue<QColor>(colorClass.call (" fromRgb" , QVariantList () << 1 << 2 << 3 )) == QColor (1 ,2 ,3 ));
403+ // direct call of static method
404+ QVERIFY (qVariantValue<QColor>(staticMethod.call (QVariantList () << 1 << 2 << 3 )) == QColor (1 ,2 ,3 ));
405+ PythonQtObjectPtr publicMethod = colorClass.getVariable (" red" );
406+ QVERIFY (publicMethod);
407+ // call with passing self in:
408+ QVERIFY (colorClass.call (" red" , QVariantList () << QColor (255 ,0 ,0 )).toInt () == 255 );
409+ }
410+
278411QByteArray PythonQtTestApiHelper::readFileAsBytes (const QString& filename)
279412{
280413 QByteArray b;
0 commit comments