tests: Port away from a few Qt specific waitForEvent

Removes a bunch of asQWidget() from the tests
This commit is contained in:
Sergio Martins
2022-04-11 15:14:51 +01:00
parent 31db3d9562
commit 90bd78846a
6 changed files with 123 additions and 98 deletions

View File

@@ -15,10 +15,39 @@
#include <QWindow>
#include <QDebug>
#include <QGuiApplication>
#include <QElapsedTimer>
#include <QtTest/QTest>
using namespace KDDockWidgets;
namespace KDDockWidgets::Tests {
/// @brief Helper class to help us with tests
class EventFilter : public QObject
{
public:
EventFilter(QEvent::Type type)
: m_type(type)
{
}
~EventFilter() override;
bool eventFilter(QObject *, QEvent *e) override
{
if (e->type() == m_type)
m_got = true;
return false;
}
const QEvent::Type m_type;
bool m_got = false;
};
EventFilter::~EventFilter() = default;
}
Platform_qt::Platform_qt()
{
@@ -62,4 +91,34 @@ bool Platform_qt::tests_waitForWindowActive(Window::Ptr window, int timeout) con
return QTest::qWaitForWindowActive(windowqt->qtWindow(), timeout);
}
bool Platform_qt::tests_waitForEvent(QObject *w, QEvent::Type type, int timeout) const
{
Tests::EventFilter filter(type);
w->installEventFilter(&filter);
QElapsedTimer time;
time.start();
while (!filter.m_got && time.elapsed() < timeout) {
qApp->processEvents();
QTest::qWait(50);
}
return filter.m_got;
}
bool Platform_qt::tests_waitForEvent(View *view, QEvent::Type type, int timeout) const
{
return tests_waitForEvent(view->asQObject(), type, timeout);
}
bool Platform_qt::tests_waitForResize(View *view, int timeout) const
{
return tests_waitForEvent(view->asQObject(), QEvent::Resize, timeout);
}
bool Platform_qt::tests_waitForResize(Controller *c, int timeout) const
{
return tests_waitForResize(c->view(), timeout);
}
#endif