##// END OF EJS Templates
fixed test case...
florianlink -
r20:1203bfc88874
parent child
Show More
@@ -1,325 +1,324
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 PythonQt::self()->addObject(main, "obj", _helper);
48 PythonQt::self()->addObject(main, "obj", _helper);
49 }
49 }
50
50
51 void PythonQtTestSlotCalling::init() {
51 void PythonQtTestSlotCalling::init() {
52
52
53 }
53 }
54
54
55 void PythonQtTestSlotCalling::testNoArgSlotCall()
55 void PythonQtTestSlotCalling::testNoArgSlotCall()
56 {
56 {
57 QVERIFY(_helper->runScript("obj.testNoArg(); obj.setPassed();\n"));
57 QVERIFY(_helper->runScript("obj.testNoArg(); obj.setPassed();\n"));
58 }
58 }
59
59
60 void PythonQtTestSlotCalling::testOverloadedCall()
60 void PythonQtTestSlotCalling::testOverloadedCall()
61 {
61 {
62 QVERIFY(_helper->runScript("obj.overload(False); obj.setPassed();\n", 0));
62 QVERIFY(_helper->runScript("obj.overload(False); obj.setPassed();\n", 0));
63 QVERIFY(_helper->runScript("obj.overload(True); obj.setPassed();\n", 0));
63 QVERIFY(_helper->runScript("obj.overload(True); obj.setPassed();\n", 0));
64 QVERIFY(_helper->runScript("obj.overload(12.5); obj.setPassed();\n", 1));
64 QVERIFY(_helper->runScript("obj.overload(12.5); obj.setPassed();\n", 1));
65 QVERIFY(_helper->runScript("obj.overload(12); obj.setPassed();\n", 2));
65 QVERIFY(_helper->runScript("obj.overload(12); obj.setPassed();\n", 2));
66 QVERIFY(_helper->runScript("obj.overload('test'); obj.setPassed();\n", 3));
66 QVERIFY(_helper->runScript("obj.overload('test'); obj.setPassed();\n", 3));
67 QVERIFY(_helper->runScript("obj.overload(u'test'); obj.setPassed();\n", 3));
67 QVERIFY(_helper->runScript("obj.overload(u'test'); obj.setPassed();\n", 3));
68 QVERIFY(_helper->runScript("obj.overload(('test','test2')); obj.setPassed();\n", 4));
68 QVERIFY(_helper->runScript("obj.overload(('test','test2')); obj.setPassed();\n", 4));
69 QVERIFY(_helper->runScript("obj.overload(obj); obj.setPassed();\n", 5));
69 QVERIFY(_helper->runScript("obj.overload(obj); obj.setPassed();\n", 5));
70 QVERIFY(_helper->runScript("obj.overload(12,13); obj.setPassed();\n", 6));
70 QVERIFY(_helper->runScript("obj.overload(12,13); obj.setPassed();\n", 6));
71 }
71 }
72
72
73 void PythonQtTestSlotCalling::testPODSlotCalls()
73 void PythonQtTestSlotCalling::testPODSlotCalls()
74 {
74 {
75 QVERIFY(_helper->runScript("if obj.getBool(False)==False: obj.setPassed();\n"));
75 QVERIFY(_helper->runScript("if obj.getBool(False)==False: obj.setPassed();\n"));
76 QVERIFY(_helper->runScript("if obj.getBool(True)==True: obj.setPassed();\n"));
76 QVERIFY(_helper->runScript("if obj.getBool(True)==True: obj.setPassed();\n"));
77 QVERIFY(_helper->runScript("if obj.getInt(-42)==-42: obj.setPassed();\n"));
77 QVERIFY(_helper->runScript("if obj.getInt(-42)==-42: obj.setPassed();\n"));
78 QVERIFY(_helper->runScript("if obj.getUInt(42)==42: obj.setPassed();\n"));
78 QVERIFY(_helper->runScript("if obj.getUInt(42)==42: obj.setPassed();\n"));
79 QVERIFY(_helper->runScript("if obj.getShort(-43)==-43: obj.setPassed();\n"));
79 QVERIFY(_helper->runScript("if obj.getShort(-43)==-43: obj.setPassed();\n"));
80 QVERIFY(_helper->runScript("if obj.getUShort(43)==43: obj.setPassed();\n"));
80 QVERIFY(_helper->runScript("if obj.getUShort(43)==43: obj.setPassed();\n"));
81 QVERIFY(_helper->runScript("if obj.getChar(-12)==-12: obj.setPassed();\n"));
81 QVERIFY(_helper->runScript("if obj.getChar(-12)==-12: obj.setPassed();\n"));
82 QVERIFY(_helper->runScript("if obj.getUChar(12)==12: obj.setPassed();\n"));
82 QVERIFY(_helper->runScript("if obj.getUChar(12)==12: obj.setPassed();\n"));
83 QVERIFY(_helper->runScript("if obj.getLong(-256*256*256)==-256*256*256: obj.setPassed();\n"));
83 QVERIFY(_helper->runScript("if obj.getLong(-256*256*256)==-256*256*256: obj.setPassed();\n"));
84 QVERIFY(_helper->runScript("if obj.getULong(256*256*256)==256*256*256: obj.setPassed();\n"));
84 QVERIFY(_helper->runScript("if obj.getULong(256*256*256)==256*256*256: obj.setPassed();\n"));
85 QVERIFY(_helper->runScript("if obj.getLongLong(-42)==-42: obj.setPassed();\n"));
85 QVERIFY(_helper->runScript("if obj.getLongLong(-42)==-42: obj.setPassed();\n"));
86 QVERIFY(_helper->runScript("if obj.getULongLong(42)==42: obj.setPassed();\n"));
86 QVERIFY(_helper->runScript("if obj.getULongLong(42)==42: obj.setPassed();\n"));
87 QVERIFY(_helper->runScript("if obj.getQChar(4096)==4096: obj.setPassed();\n"));
87 QVERIFY(_helper->runScript("if obj.getQChar(4096)==4096: obj.setPassed();\n"));
88 QVERIFY(_helper->runScript("if obj.getDouble(47.12)==47.12: obj.setPassed();\n"));
88 QVERIFY(_helper->runScript("if obj.getDouble(47.12)==47.12: obj.setPassed();\n"));
89 QVERIFY(_helper->runScript("if abs(obj.getFloat(47.11)-47.11)<0.01: obj.setPassed();\n"));
89 QVERIFY(_helper->runScript("if abs(obj.getFloat(47.11)-47.11)<0.01: obj.setPassed();\n"));
90 QVERIFY(_helper->runScript("if obj.getQString('testStr')=='testStr': obj.setPassed();\n"));
90 QVERIFY(_helper->runScript("if obj.getQString('testStr')=='testStr': obj.setPassed();\n"));
91 QVERIFY(_helper->runScript("if obj.getQString('')=='': obj.setPassed();\n"));
91 QVERIFY(_helper->runScript("if obj.getQString('')=='': obj.setPassed();\n"));
92 QVERIFY(_helper->runScript("if obj.getQStringList(('test','test2'))==('test','test2'): obj.setPassed();\n"));
92 QVERIFY(_helper->runScript("if obj.getQStringList(('test','test2'))==('test','test2'): obj.setPassed();\n"));
93 }
93 }
94
94
95 void PythonQtTestSlotCalling::testQVariantSlotCalls()
95 void PythonQtTestSlotCalling::testQVariantSlotCalls()
96 {
96 {
97 QVERIFY(_helper->runScript("if obj.getQVariant(-42)==-42: obj.setPassed();\n"));
97 QVERIFY(_helper->runScript("if obj.getQVariant(-42)==-42: obj.setPassed();\n"));
98 QVERIFY(_helper->runScript("if obj.getQVariant('testStr')=='testStr': obj.setPassed();\n"));
98 QVERIFY(_helper->runScript("if obj.getQVariant('testStr')=='testStr': obj.setPassed();\n"));
99 QVERIFY(_helper->runScript("if obj.getQVariant(('test','test2'))==('test','test2'): obj.setPassed();\n"));
99 QVERIFY(_helper->runScript("if obj.getQVariant(('test','test2'))==('test','test2'): obj.setPassed();\n"));
100 QVERIFY(_helper->runScript("if obj.getQVariant(('test',12, 47.11))==('test',12, 47.11): obj.setPassed();\n"));
100 QVERIFY(_helper->runScript("if obj.getQVariant(('test',12, 47.11))==('test',12, 47.11): obj.setPassed();\n"));
101 QVERIFY(_helper->runScript("if obj.getQVariant({'test':'bla','test2':47.11})=={'test':'bla','test2':47.11}: obj.setPassed();\n"));
101 QVERIFY(_helper->runScript("if obj.getQVariant({'test':'bla','test2':47.11})=={'test':'bla','test2':47.11}: obj.setPassed();\n"));
102 QEXPECT_FAIL("", "Testing to pass a map and compare with a different map", Continue);
102 QEXPECT_FAIL("", "Testing to pass a map and compare with a different map", Continue);
103 QVERIFY(_helper->runScript("if obj.getQVariant({'test':'bla2','test2':47.11})=={'test':'bla','test2':47.11}: obj.setPassed();\n"));
103 QVERIFY(_helper->runScript("if obj.getQVariant({'test':'bla2','test2':47.11})=={'test':'bla','test2':47.11}: obj.setPassed();\n"));
104 QVERIFY(_helper->runScript("if obj.getQVariant(obj)==obj: obj.setPassed();\n"));
104 QVERIFY(_helper->runScript("if obj.getQVariant(obj)==obj: obj.setPassed();\n"));
105 }
105 }
106
106
107 void PythonQtTestSlotCalling::testObjectSlotCalls()
107 void PythonQtTestSlotCalling::testObjectSlotCalls()
108 {
108 {
109 QVERIFY(_helper->runScript("if obj.getQObject(obj)==obj: obj.setPassed();\n"));
109 QVERIFY(_helper->runScript("if obj.getQObject(obj)==obj: obj.setPassed();\n"));
110 QVERIFY(_helper->runScript("if obj.getTestObject(obj)==obj: obj.setPassed();\n"));
110 QVERIFY(_helper->runScript("if obj.getTestObject(obj)==obj: obj.setPassed();\n"));
111 QVERIFY(_helper->runScript("if obj.getNewObject().className()=='PythonQtTestSlotCallingHelper': obj.setPassed();\n"));
111 QVERIFY(_helper->runScript("if obj.getNewObject().className()=='PythonQtTestSlotCallingHelper': obj.setPassed();\n"));
112 QEXPECT_FAIL("", "Testing to pass a QObject when another object was expected", Continue);
112 QEXPECT_FAIL("", "Testing to pass a QObject when another object was expected", Continue);
113 QVERIFY(_helper->runScript("if obj.getQWidget(obj)==obj: obj.setPassed();\n"));
113 QVERIFY(_helper->runScript("if obj.getQWidget(obj)==obj: obj.setPassed();\n"));
114 }
114 }
115
115
116 void PythonQtTestSlotCalling::testCppFactory()
116 void PythonQtTestSlotCalling::testCppFactory()
117 {
117 {
118 PythonQtTestCppFactory* f = new PythonQtTestCppFactory;
118 PythonQtTestCppFactory* f = new PythonQtTestCppFactory;
119 PythonQt::self()->addInstanceDecorators(new PQCppObjectDecorator);
119 PythonQt::self()->addInstanceDecorators(new PQCppObjectDecorator);
120 PythonQt::self()->addInstanceDecorators(new PQCppObjectNoWrapDecorator);
120 PythonQt::self()->addInstanceDecorators(new PQCppObjectNoWrapDecorator);
121
121
122 PythonQt::self()->addWrapperFactory(f);
122 PythonQt::self()->addWrapperFactory(f);
123 QVERIFY(_helper->runScript("if obj.createPQCppObject(12).getHeight()==12: obj.setPassed();\n"));
123 QVERIFY(_helper->runScript("if obj.createPQCppObject(12).getHeight()==12: obj.setPassed();\n"));
124 QVERIFY(_helper->runScript("if obj.createPQCppObject(12).getH()==12: obj.setPassed();\n"));
124 QVERIFY(_helper->runScript("if obj.createPQCppObject(12).getH()==12: obj.setPassed();\n"));
125 QVERIFY(_helper->runScript("pq1 = obj.createPQCppObject(12);\n"
125 QVERIFY(_helper->runScript("pq1 = obj.createPQCppObject(12);\n"
126 "pq2 = obj.createPQCppObject(13);\n"
126 "pq2 = obj.createPQCppObject(13);\n"
127 "pq3 = obj.getPQCppObject(pq1);\n"
127 "pq3 = obj.getPQCppObject(pq1);\n"
128 "pq4 = obj.getPQCppObject(pq2);\n"
128 "pq4 = obj.getPQCppObject(pq2);\n"
129 "if pq3.getHeight()==12 and pq4.getHeight()==13: obj.setPassed();\n"
129 "if pq3.getHeight()==12 and pq4.getHeight()==13: obj.setPassed();\n"
130 ));
130 ));
131
131
132 QVERIFY(_helper->runScript("if obj.createPQCppObjectNoWrap(12).getH()==12: obj.setPassed();\n"));
132 QVERIFY(_helper->runScript("if obj.createPQCppObjectNoWrap(12).getH()==12: obj.setPassed();\n"));
133 }
133 }
134
134
135
135
136 void PythonQtTestSlotCalling::testMultiArgsSlotCall()
136 void PythonQtTestSlotCalling::testMultiArgsSlotCall()
137 {
137 {
138 QVERIFY(_helper->runScript("if obj.getMultiArgs(12,47.11,'test')==(12,47.11,'test'): obj.setPassed();\n"));
138 QVERIFY(_helper->runScript("if obj.getMultiArgs(12,47.11,'test')==(12,47.11,'test'): obj.setPassed();\n"));
139 }
139 }
140
140
141 bool PythonQtTestSlotCallingHelper::runScript(const char* script, int expectedOverload)
141 bool PythonQtTestSlotCallingHelper::runScript(const char* script, int expectedOverload)
142 {
142 {
143 _called = false;
143 _called = false;
144 _passed = false;
144 _passed = false;
145 _calledOverload = -1;
145 _calledOverload = -1;
146 PyRun_SimpleString(script);
146 PyRun_SimpleString(script);
147 return _called && _passed && _calledOverload==expectedOverload;
147 return _called && _passed && _calledOverload==expectedOverload;
148 }
148 }
149
149
150
150
151 void PythonQtTestSignalHandler::initTestCase()
151 void PythonQtTestSignalHandler::initTestCase()
152 {
152 {
153 _helper = new PythonQtTestSignalHandlerHelper(this);
153 _helper = new PythonQtTestSignalHandlerHelper(this);
154 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
154 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
155 PythonQt::self()->addObject(main, "obj", _helper);
155 PythonQt::self()->addObject(main, "obj", _helper);
156 }
156 }
157
157
158 void PythonQtTestSignalHandler::testSignalHandler()
158 void PythonQtTestSignalHandler::testSignalHandler()
159 {
159 {
160 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
160 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
161 PyRun_SimpleString("def testIntSignal(a):\n if a==12: obj.setPassed();\n");
161 PyRun_SimpleString("def testIntSignal(a):\n if a==12: obj.setPassed();\n");
162 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(intSignal(int)), main, "testIntSignal"));
162 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(intSignal(int)), main, "testIntSignal"));
163 QVERIFY(_helper->emitIntSignal(12));
163 QVERIFY(_helper->emitIntSignal(12));
164
164
165 PyRun_SimpleString("def testFloatSignal(a):\n if a==12: obj.setPassed();\n");
165 PyRun_SimpleString("def testFloatSignal(a):\n if a==12: obj.setPassed();\n");
166 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(floatSignal(float)), main, "testFloatSignal"));
166 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(floatSignal(float)), main, "testFloatSignal"));
167 QVERIFY(_helper->emitFloatSignal(12));
167 QVERIFY(_helper->emitFloatSignal(12));
168
168
169 PyRun_SimpleString("def testVariantSignal(a):\n if a==obj.expectedVariant(): obj.setPassed();\n");
169 PyRun_SimpleString("def testVariantSignal(a):\n if a==obj.expectedVariant(): obj.setPassed();\n");
170 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(variantSignal(QVariant)), main, "testVariantSignal"));
170 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(variantSignal(QVariant)), main, "testVariantSignal"));
171 _helper->setExpectedVariant(QString("Test"));
171 _helper->setExpectedVariant(QString("Test"));
172 QVERIFY(_helper->emitVariantSignal(QString("Test")));
172 QVERIFY(_helper->emitVariantSignal(QString("Test")));
173 _helper->setExpectedVariant(12);
173 _helper->setExpectedVariant(12);
174 QVERIFY(_helper->emitVariantSignal(12));
174 QVERIFY(_helper->emitVariantSignal(12));
175 _helper->setExpectedVariant(QStringList() << "test1" << "test2");
175 _helper->setExpectedVariant(QStringList() << "test1" << "test2");
176 QVERIFY(_helper->emitVariantSignal(QStringList() << "test1" << "test2"));
176 QVERIFY(_helper->emitVariantSignal(QStringList() << "test1" << "test2"));
177 _helper->setExpectedVariant(qVariantFromValue((QObject*)_helper));
177 _helper->setExpectedVariant(qVariantFromValue((QObject*)_helper));
178 QVERIFY(_helper->emitVariantSignal(qVariantFromValue((QObject*)_helper)));
178 QVERIFY(_helper->emitVariantSignal(qVariantFromValue((QObject*)_helper)));
179
179
180 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");
180 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");
181 // intentionally not normalized signal:
181 // intentionally not normalized signal:
182 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(complexSignal( int, float , const QStringList , QObject*)), main, "testComplexSignal"));
182 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(complexSignal( int, float , const QStringList , QObject*)), main, "testComplexSignal"));
183 QVERIFY(_helper->emitComplexSignal(12,13,QStringList() << "test1" << "test2", _helper));
183 QVERIFY(_helper->emitComplexSignal(12,13,QStringList() << "test1" << "test2", _helper));
184
184
185 // try removing the handler
185 // try removing the handler
186 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(complexSignal( int, float , const QStringList , QObject*)), main, "testComplexSignal"));
186 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(complexSignal( int, float , const QStringList , QObject*)), main, "testComplexSignal"));
187 // and emit the signal, which should fail because the handler was removed
187 // and emit the signal, which should fail because the handler was removed
188 QVERIFY(!_helper->emitComplexSignal(12,13,QStringList() << "test1" << "test2", _helper));
188 QVERIFY(!_helper->emitComplexSignal(12,13,QStringList() << "test1" << "test2", _helper));
189
189
190 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(intSignal(int)), main, "testIntSignal"));
190 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(intSignal(int)), main, "testIntSignal"));
191 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(floatSignal(float)), main, "testFloatSignal"));
191 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(floatSignal(float)), main, "testFloatSignal"));
192 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(variantSignal(QVariant)), main, "testVariantSignal"));
192 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(variantSignal(QVariant)), main, "testVariantSignal"));
193
193
194 }
194 }
195
195
196 void PythonQtTestSignalHandler::testRecursiveSignalHandler()
196 void PythonQtTestSignalHandler::testRecursiveSignalHandler()
197 {
197 {
198 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
198 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
199 PyRun_SimpleString("def testSignal1(a):\n obj.emitSignal2(a);\n");
199 PyRun_SimpleString("def testSignal1(a):\n obj.emitSignal2(a);\n");
200 PyRun_SimpleString("def testSignal2(a):\n obj.emitSignal3(float(a));\n");
200 PyRun_SimpleString("def testSignal2(a):\n obj.emitSignal3(float(a));\n");
201 PyRun_SimpleString("def testSignal3(a):\n if a==12: obj.setPassed();\n");
201 PyRun_SimpleString("def testSignal3(a):\n if a==12: obj.setPassed();\n");
202 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(signal1(int)), main, "testSignal1"));
202 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(signal1(int)), main, "testSignal1"));
203 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(signal2(const QString&)), main, "testSignal2"));
203 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(signal2(const QString&)), main, "testSignal2"));
204 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(signal3(float)), main, "testSignal3"));
204 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(signal3(float)), main, "testSignal3"));
205 QVERIFY(_helper->emitSignal1(12));
205 QVERIFY(_helper->emitSignal1(12));
206 }
206 }
207
207
208
208
209 void PythonQtTestApi::initTestCase()
209 void PythonQtTestApi::initTestCase()
210 {
210 {
211 _helper = new PythonQtTestApiHelper();
211 _helper = new PythonQtTestApiHelper();
212 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
212 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
213 PythonQt::self()->addObject(main, "obj", _helper);
213 PythonQt::self()->addObject(main, "obj", _helper);
214 }
214 }
215
215
216 bool PythonQtTestApiHelper::call(const QString& function, const QVariantList& args, const QVariant& expectedResult) {
216 bool PythonQtTestApiHelper::call(const QString& function, const QVariantList& args, const QVariant& expectedResult) {
217 _passed = false;
217 _passed = false;
218 QVariant r = PythonQt::self()->call(PythonQt::self()->getMainModule(), function, args);
218 QVariant r = PythonQt::self()->call(PythonQt::self()->getMainModule(), function, args);
219 return _passed && expectedResult==r;
219 return _passed && expectedResult==r;
220 }
220 }
221
221
222 void PythonQtTestApi::testCall()
222 void PythonQtTestApi::testCall()
223 {
223 {
224 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
224 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
225
225
226 QVERIFY(qVariantValue<QObject*>(PythonQt::self()->getVariable(main, "obj"))==_helper);
226 QVERIFY(qVariantValue<QObject*>(PythonQt::self()->getVariable(main, "obj"))==_helper);
227
227
228 PyRun_SimpleString("def testCallNoArgs():\n obj.setPassed();\n");
228 PyRun_SimpleString("def testCallNoArgs():\n obj.setPassed();\n");
229 QVERIFY(_helper->call("testCallNoArgs", QVariantList(), QVariant()));
229 QVERIFY(_helper->call("testCallNoArgs", QVariantList(), QVariant()));
230
230
231 PyRun_SimpleString("def testCall1(a):\n if a=='test': obj.setPassed();\n return 'test2';\n");
231 PyRun_SimpleString("def testCall1(a):\n if a=='test': obj.setPassed();\n return 'test2';\n");
232 QVERIFY(_helper->call("testCall1", QVariantList() << QVariant("test"), QVariant(QString("test2"))));
232 QVERIFY(_helper->call("testCall1", QVariantList() << QVariant("test"), QVariant(QString("test2"))));
233
233
234 PyRun_SimpleString("def testCall2(a, b):\n if a=='test' and b==obj: obj.setPassed();\n return obj;\n");
234 PyRun_SimpleString("def testCall2(a, b):\n if a=='test' and b==obj: obj.setPassed();\n return obj;\n");
235 QVariant r = PythonQt::self()->call(PythonQt::self()->getMainModule(), "testCall2", QVariantList() << QVariant("test") << qVariantFromValue((QObject*)_helper));
235 QVariant r = PythonQt::self()->call(PythonQt::self()->getMainModule(), "testCall2", QVariantList() << QVariant("test") << qVariantFromValue((QObject*)_helper));
236 QObject* p = qVariantValue<QObject*>(r);
236 QObject* p = qVariantValue<QObject*>(r);
237 QVERIFY(p==_helper);
237 QVERIFY(p==_helper);
238 }
238 }
239
239
240 void PythonQtTestApi::testVariables()
240 void PythonQtTestApi::testVariables()
241 {
241 {
242 PythonQt::self()->addObject(PythonQt::self()->getMainModule(), "someObject", _helper);
242 PythonQt::self()->addObject(PythonQt::self()->getMainModule(), "someObject", _helper);
243 QVariant v = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someObject");
243 QVariant v = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someObject");
244 QObject* p = qVariantValue<QObject*>(v);
244 QObject* p = qVariantValue<QObject*>(v);
245 QVERIFY(p==_helper);
245 QVERIFY(p==_helper);
246 // test for unset variable
246 // test for unset variable
247 QVariant v2 = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someObject2");
247 QVariant v2 = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someObject2");
248 QVERIFY(v2==QVariant());
248 QVERIFY(v2==QVariant());
249
249
250 PythonQt::self()->addVariable(PythonQt::self()->getMainModule(), "someValue", QStringList() << "test1" << "test2");
250 PythonQt::self()->addVariable(PythonQt::self()->getMainModule(), "someValue", QStringList() << "test1" << "test2");
251 QVariant v3 = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someValue");
251 QVariant v3 = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someValue");
252 QVERIFY(v3 == QVariant(QStringList() << "test1" << "test2"));
252 QVERIFY(v3 == QVariant(QStringList() << "test1" << "test2"));
253
253
254 QStringList l = PythonQt::self()->introspection(PythonQt::self()->getMainModule(), QString::null, PythonQt::Variable);
254 QStringList l = PythonQt::self()->introspection(PythonQt::self()->getMainModule(), QString::null, PythonQt::Variable);
255 QSet<QString> s;
255 QSet<QString> s;
256 // check that at least these three variables are set
256 // check that at least these three variables are set
257 s << "obj" << "someObject" << "someValue";
257 s << "obj" << "someObject" << "someValue";
258 QString a;
258 foreach (QString value, s) {
259 foreach (a, l) {
259 QVERIFY(l.indexOf(value)!=-1);
260 QVERIFY(s.contains(a));
261 }
260 }
262
261
263 // insert a second time!
262 // insert a second time!
264 PythonQt::self()->addObject(PythonQt::self()->getMainModule(), "someObject", _helper);
263 PythonQt::self()->addObject(PythonQt::self()->getMainModule(), "someObject", _helper);
265 // and remove
264 // and remove
266 PythonQt::self()->removeVariable(PythonQt::self()->getMainModule(), "someObject");
265 PythonQt::self()->removeVariable(PythonQt::self()->getMainModule(), "someObject");
267 // we expect to find no variable
266 // we expect to find no variable
268 QVariant v4 = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someObject");
267 QVariant v4 = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someObject");
269 QVERIFY(v4==QVariant());
268 QVERIFY(v4==QVariant());
270 }
269 }
271
270
272 void PythonQtTestApi::testImporter()
271 void PythonQtTestApi::testImporter()
273 {
272 {
274 PythonQt::self()->setImporter(_helper);
273 PythonQt::self()->setImporter(_helper);
275 PythonQt::self()->overwriteSysPath(QStringList() << "c:\\test");
274 PythonQt::self()->overwriteSysPath(QStringList() << "c:\\test");
276 PyRun_SimpleString("import bla\n");
275 PyRun_SimpleString("import bla\n");
277 }
276 }
278
277
279 QByteArray PythonQtTestApiHelper::readFileAsBytes(const QString& filename)
278 QByteArray PythonQtTestApiHelper::readFileAsBytes(const QString& filename)
280 {
279 {
281 QByteArray b;
280 QByteArray b;
282 return b;
281 return b;
283 }
282 }
284
283
285 QByteArray PythonQtTestApiHelper::readSourceFile(const QString& filename, bool& ok)
284 QByteArray PythonQtTestApiHelper::readSourceFile(const QString& filename, bool& ok)
286 {
285 {
287 QByteArray b;
286 QByteArray b;
288 ok = true;
287 ok = true;
289 return b;
288 return b;
290 }
289 }
291
290
292 bool PythonQtTestApiHelper::exists(const QString& filename)
291 bool PythonQtTestApiHelper::exists(const QString& filename)
293 {
292 {
294 return true;
293 return true;
295 }
294 }
296
295
297 QDateTime PythonQtTestApiHelper::lastModifiedDate(const QString& filename) {
296 QDateTime PythonQtTestApiHelper::lastModifiedDate(const QString& filename) {
298 return QDateTime::currentDateTime();
297 return QDateTime::currentDateTime();
299 }
298 }
300
299
301
300
302 void PythonQtTestApi::testRedirect()
301 void PythonQtTestApi::testRedirect()
303 {
302 {
304 connect(PythonQt::self(), SIGNAL(pythonStdOut(const QString&)), _helper, SLOT(stdOut(const QString&)));
303 connect(PythonQt::self(), SIGNAL(pythonStdOut(const QString&)), _helper, SLOT(stdOut(const QString&)));
305 connect(PythonQt::self(), SIGNAL(pythonStdErr(const QString&)), _helper, SLOT(stdErr(const QString&)));
304 connect(PythonQt::self(), SIGNAL(pythonStdErr(const QString&)), _helper, SLOT(stdErr(const QString&)));
306 PyRun_SimpleString("print 'test'\n");
305 PyRun_SimpleString("print 'test'\n");
307 }
306 }
308
307
309 void PythonQtTestApiHelper::stdOut(const QString& s)
308 void PythonQtTestApiHelper::stdOut(const QString& s)
310 {
309 {
311 qDebug() << s;
310 qDebug() << s;
312 }
311 }
313
312
314 void PythonQtTestApiHelper::stdErr(const QString& s)
313 void PythonQtTestApiHelper::stdErr(const QString& s)
315 {
314 {
316 qDebug() << s;
315 qDebug() << s;
317 }
316 }
318
317
319 QObject* PythonQtTestCppFactory::create(const QByteArray& name, void *ptr)
318 QObject* PythonQtTestCppFactory::create(const QByteArray& name, void *ptr)
320 {
319 {
321 if (name == "PQCppObject") {
320 if (name == "PQCppObject") {
322 return new PQCppObjectWrapper(ptr);
321 return new PQCppObjectWrapper(ptr);
323 }
322 }
324 return NULL;
323 return NULL;
325 }
324 }
General Comments 0
You need to be logged in to leave comments. Login now