1 #ifndef QT_NO_DEBUG
2 #include "testutil.h"
3 namespace QTest{
4 MessageBoxCloser* curCloser=nullptr;
MessageBoxCloser(bool mustExists,QMessageBox::StandardButton button)5 MessageBoxCloser::MessageBoxCloser(bool mustExists, QMessageBox::StandardButton button):QObject(nullptr), m_mustExists(mustExists), m_button(button){
6 	if (button!=QMessageBox::Ok && button!=QMessageBox::Cancel && button!=QMessageBox::NoButton && button!=QMessageBox::Yes)
7 		QVERIFY2(false, "invalid button for messagebox closing");
8 	QTimer::singleShot(1, this, SLOT(closeNow()));
9 }
10 
closeNow()11 void MessageBoxCloser::closeNow(){
12 	deleteLater();
13 	QWidget* messageWindow = QApplication::activeModalWidget();
14     if (!messageWindow){
15 		foreach (QWidget *widget, QApplication::topLevelWidgets())
16 			if (widget->isModal())
17 				messageWindow=widget;
18     }
19 	if (!messageWindow) {
20 		QVERIFY2(!m_mustExists, "messagebox doesn't exists");
21 		return; //keyClick crashes (assert false)  if it can't find a window
22 	}
23 	switch (m_button) {
24 		case QMessageBox::Ok: case QMessageBox::Yes:
25 			QTest::keyClick(messageWindow, Qt::Key_Return);
26 			break;
27 		default:
28 			QTest::keyClick(messageWindow, Qt::Key_Escape);
29 			break;
30 	}
31     curCloser=nullptr;
32 }
closeMessageBoxLater(bool mustExists,QMessageBox::StandardButton button)33 void closeMessageBoxLater(bool mustExists, QMessageBox::StandardButton button){
34 	if (curCloser) QWARN("multiple closing calls");
35 	curCloser=new MessageBoxCloser(mustExists, button);
36 }
messageBoxShouldBeClose()37 void messageBoxShouldBeClose(){
38 	QVERIFY2(!curCloser, "MessageBox couldn't be closed");
39 }
40 
41 /**
42  * \brief Process all pending events except for UI and network events.
43  */
processPendingEvents(void)44 void processPendingEvents(void)
45 {
46 	QCoreApplication::processEvents(QEventLoop::AllEvents);
47 	// Deferred delete must be processed explicitly. Using 0 for event_type does not work.
48 	QCoreApplication::sendPostedEvents(Q_NULLPTR, QEvent::DeferredDelete);
49 }
50 
51 }
52 #endif
53