4 This file is part of GammaRay, the Qt application inspection and
7 Copyright (C) 2010-2011 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8 Author: Milian Wolff <milian.wolff@kdab.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "test_connections.h"
26 #include <QtCore/QDebug>
27 #include <QApplication>
28 #include <QtCore/QTimer>
29 #include <QtTest/QtTestGui>
30 #include <QtCore/QProcessEnvironment>
32 const int TIMEOUTINTERVAL
= 10;
33 const int OBJECTS
= 50;
34 const int TIMEOUTS
= 100;
37 TestObject::TestObject(QObject
*parent
)
39 // test object creation in ctor
40 , child(new QObject(this))
42 setObjectName("TestObject");
43 child
->setObjectName("TestObjectChild");
44 // test connect/disconnect in ctor
45 connect(child
, SIGNAL(destroyed(QObject
*)), this, SLOT(dummySlot()));
46 disconnect(child
, SIGNAL(destroyed(QObject
*)), this, SLOT(dummySlot()));
47 // now connect again for dtor
48 connect(child
, SIGNAL(destroyed(QObject
*)), this, SLOT(dummySlot()));
51 TestObject::~TestObject()
54 disconnect(child
, SIGNAL(destroyed(QObject
*)), this, SLOT(dummySlot()));
55 // test connect, and leave it around to test disconnect-on-delete
56 connect(child
, SIGNAL(destroyed(QObject
*)), this, SLOT(dummySlot()));
61 //BEGIN TestConnections
62 TestConnections::TestConnections(TestConnections::Type type
, int timeOuts
, int timeoutInterval
)
63 : m_type(type
), m_timeOuts(timeOuts
), m_numTimeout(0), m_timer(new QTimer(this))
65 m_timer
= new QTimer(this);
66 connect(m_timer
, SIGNAL(timeout()), SLOT(timeout()));
67 m_timer
->start(timeoutInterval
== -1 ? TIMEOUTINTERVAL
: timeoutInterval
);
70 TestConnections::~TestConnections()
74 void TestConnections::timeout()
76 if (m_numTimeout
== m_timeOuts
) {
77 qDeleteAll(m_objects
);
86 if (m_type
== NoEventLoop
) {
87 // directly create and delete objects without eventloop in between
88 QObject
*obj
= new TestObject(this);
89 connect(obj
, SIGNAL(destroyed(QObject
*)), this, SLOT(dummySlot()));
91 } else if (m_type
== Stack
) {
93 connect(&obj
, SIGNAL(destroyed(QObject
*)), this, SLOT(dummySlot()));
94 disconnect(&obj
, SIGNAL(destroyed(QObject
*)), this, SLOT(dummySlot()));
95 } else if (m_type
== SetParent
) {
96 TestObject
*obj
= new TestObject
;
98 obj
->child
->setParent(0);
99 obj
->child
->setParent(obj
);
102 // delete last objects
103 for (int i
= 0; i
< m_objects
.count(); ++i
) {
104 QObject
*obj
= m_objects
.at(i
);
118 // create some new objects
119 for (int i
= 0; i
< OBJECTS
; ++i
) {
120 QObject
*obj
= new TestObject(this);
122 connect(obj
, SIGNAL(destroyed(QObject
*)), this, SLOT(dummySlot()));
126 //END TestConnections
129 TestThread::TestThread(TestConnections::Type type
, int timeOuts
, int timeoutInterval
,
131 : QThread(parent
), m_type(type
), m_timeOuts(timeOuts
), m_timeoutInterval(timeoutInterval
)
135 TestThread::~TestThread()
139 void TestThread::run()
141 TestConnections
tester(m_type
, m_timeOuts
, m_timeoutInterval
== -1 ?
142 TIMEOUTS
: m_timeoutInterval
);
144 QEventLoop
*loop
= new QEventLoop
;
145 connect(&tester
, SIGNAL(done()), loop
, SLOT(quit()));
152 void TestWaiter::addTester(TestConnections
*tester
)
154 connect(tester
, SIGNAL(done()), SLOT(testerDone()));
158 void TestWaiter::testerDone()
160 TestConnections
* tester
= qobject_cast
<TestConnections
*>(sender());
162 QVERIFY(m_tester
.removeOne(tester
));
166 void TestWaiter::addThread(TestThread
*thread
)
168 connect(thread
, SIGNAL(finished()), SLOT(threadFinished()));
172 void TestWaiter::threadFinished()
174 TestThread
* thread
= qobject_cast
<TestThread
*>(sender());
176 QVERIFY(m_threads
.removeOne(thread
));
180 void TestWaiter::checkFinished()
185 if (m_threads
.isEmpty() && m_tester
.isEmpty()) {
190 void TestWaiter::startThreadsAndWaitForFinished()
192 if (m_threads
.isEmpty() && m_tester
.isEmpty()) {
196 foreach (TestThread
*thread
, m_threads
) {
200 m_loop
= new QEventLoop
;
209 TestMain::TestMain(int argc
, char **argv
)
210 : m_argc(argc
), m_argv(argv
)
212 QMetaObject::invokeMethod(this, "startTests", Qt::QueuedConnection
);
215 void TestMain::startTests()
217 qApp
->exit(QTest::qExec(this, m_argc
, m_argv
));
220 void TestMain::run_data()
222 QTest::addColumn
<int>("type");
223 QTest::newRow("delete") << static_cast<int>(TestConnections::Delete
);
224 QTest::newRow("deleteLater") << static_cast<int>(TestConnections::DeleteLater
);
225 QTest::newRow("noEventLoop") << static_cast<int>(TestConnections::NoEventLoop
);
226 QTest::newRow("stack") << static_cast<int>(TestConnections::Stack
);
227 QTest::newRow("setParent") << static_cast<int>(TestConnections::SetParent
);
234 bool manual
= QProcessEnvironment::systemEnvironment().value("GAMMARAY_TEST_MANUAL").toInt();
235 TestConnections
tester(static_cast<TestConnections::Type
>(type
),
236 manual
? -1 : TIMEOUTS
);
239 waiter
.addTester(&tester
);
240 waiter
.startThreadsAndWaitForFinished();
243 void TestMain::threading()
246 const int timeouts
= 10;
247 // some testers to be run in the main thread
248 // with varying timouts
249 TestConnections
tester1(TestConnections::NoEventLoop
, timeouts
, 10);
250 waiter
.addTester(&tester1
);
251 TestConnections
tester2(TestConnections::Delete
, timeouts
, 11);
252 waiter
.addTester(&tester2
);
253 TestConnections
tester3(TestConnections::DeleteLater
, timeouts
, 12);
254 waiter
.addTester(&tester3
);
255 TestConnections
tester4(TestConnections::Stack
, timeouts
, 13);
256 waiter
.addTester(&tester4
);
257 TestConnections
tester5(TestConnections::SetParent
, timeouts
, 14);
258 waiter
.addTester(&tester5
);
260 TestThread
thread1(TestConnections::NoEventLoop
, timeouts
, 10);
261 waiter
.addThread(&thread1
);
262 TestThread
thread2(TestConnections::Delete
, timeouts
, 11);
263 waiter
.addThread(&thread2
);
264 TestThread
thread3(TestConnections::DeleteLater
, timeouts
, 12);
265 waiter
.addThread(&thread3
);
266 TestThread
thread4(TestConnections::Stack
, timeouts
, 13);
267 waiter
.addThread(&thread4
);
268 TestThread
thread5(TestConnections::SetParent
, timeouts
, 13);
269 waiter
.addThread(&thread5
);
271 waiter
.startThreadsAndWaitForFinished();
275 int main(int argc
, char *argv
[]) {
276 QApplication
app(argc
, argv
);
277 TestMain
tc(argc
, argv
);
280 #include "test_connections.moc"