testlib: Move qtestsystem helpers to their respective modules
Having the helpers in each respective module lets us implement the helpers using private APIs without forcing the test to add private dependencies. It also makes it easier to test Qt using a third party testing framework (for running the test suite), while still using the helpers for ensuring tests behave expectedly. Change-Id: I2a6ce24526ed345f3513548f11da05c7804c203f Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
This commit is contained in:
parent
ff78e6fe35
commit
88867e39bc
@ -43,6 +43,7 @@ HEADERS += \
|
||||
kernel/qsystemerror_p.h \
|
||||
kernel/qmetatype_p.h \
|
||||
kernel/qmetatypeswitcher_p.h \
|
||||
kernel/qtestsupport_core.h
|
||||
|
||||
SOURCES += \
|
||||
kernel/qabstracteventdispatcher.cpp \
|
||||
@ -69,7 +70,8 @@ SOURCES += \
|
||||
kernel/qsystemsemaphore.cpp \
|
||||
kernel/qpointer.cpp \
|
||||
kernel/qmath.cpp \
|
||||
kernel/qsystemerror.cpp
|
||||
kernel/qsystemerror.cpp \
|
||||
kernel/qtestsupport_core.cpp
|
||||
|
||||
win32 {
|
||||
SOURCES += \
|
||||
|
126
src/corelib/kernel/qtestsupport_core.cpp
Normal file
126
src/corelib/kernel/qtestsupport_core.cpp
Normal file
@ -0,0 +1,126 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2018 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the QtTest module of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:LGPL$
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU Lesser General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU Lesser
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.LGPL3 included in the
|
||||
** packaging of this file. Please review the following information to
|
||||
** ensure the GNU Lesser General Public License version 3 requirements
|
||||
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 2.0 or (at your option) the GNU General
|
||||
** Public license version 3 or any later version approved by the KDE Free
|
||||
** Qt Foundation. The licenses are as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
|
||||
** https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "qtestsupport_core.h"
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
#include <qt_windows.h>
|
||||
#endif
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
Q_CORE_EXPORT void QTestPrivate::qSleep(int ms)
|
||||
{
|
||||
Q_ASSERT(ms > 0);
|
||||
|
||||
#if defined(Q_OS_WINRT)
|
||||
WaitForSingleObjectEx(GetCurrentThread(), ms, true);
|
||||
#elif defined(Q_OS_WIN)
|
||||
Sleep(uint(ms));
|
||||
#else
|
||||
struct timespec ts = { time_t(ms / 1000), (ms % 1000) * 1000 * 1000 };
|
||||
nanosleep(&ts, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
/*! \fn template <typename Functor> bool qWaitFor(Functor predicate, int timeout)
|
||||
\relates QTest
|
||||
|
||||
Waits for \a timeout milliseconds or until the \a predicate returns true.
|
||||
|
||||
Returns \c true if the \a predicate returned true at any point, otherwise returns \c false.
|
||||
|
||||
Example:
|
||||
|
||||
\code
|
||||
MyObject obj;
|
||||
obj.startup();
|
||||
QTest::qWaitFor([&]() {
|
||||
return obj.isReady();
|
||||
}, 3000);
|
||||
\endcode
|
||||
|
||||
The code above will wait for the object to become ready, for a
|
||||
maximum of three seconds.
|
||||
|
||||
\since 5.10
|
||||
*/
|
||||
|
||||
|
||||
/*! \fn void qWait(int ms)
|
||||
\relates QTest
|
||||
|
||||
Waits for \a ms milliseconds. While waiting, events will be processed and
|
||||
your test will stay responsive to user interface events or network communication.
|
||||
|
||||
Example:
|
||||
|
||||
\code
|
||||
int i = 0;
|
||||
while (myNetworkServerNotResponding() && i++ < 50)
|
||||
QTest::qWait(250);
|
||||
\endcode
|
||||
|
||||
The code above will wait until the network server is responding for a
|
||||
maximum of about 12.5 seconds.
|
||||
|
||||
\sa QTest::qSleep(), QSignalSpy::wait()
|
||||
*/
|
||||
Q_CORE_EXPORT void QTest::qWait(int ms)
|
||||
{
|
||||
// Ideally this method would be implemented in terms of qWaitFor, with
|
||||
// a predicate that always returns false, but due to a compiler bug in
|
||||
// GCC 6 we can't do that.
|
||||
|
||||
Q_ASSERT(QCoreApplication::instance());
|
||||
|
||||
QDeadlineTimer timer(ms, Qt::PreciseTimer);
|
||||
int remaining = ms;
|
||||
do {
|
||||
QCoreApplication::processEvents(QEventLoop::AllEvents, remaining);
|
||||
QCoreApplication::sendPostedEvents(Q_NULLPTR, QEvent::DeferredDelete);
|
||||
remaining = timer.remainingTime();
|
||||
if (remaining <= 0)
|
||||
break;
|
||||
QTestPrivate::qSleep(qMin(10, remaining));
|
||||
remaining = timer.remainingTime();
|
||||
} while (remaining > 0);
|
||||
}
|
||||
|
||||
QT_END_NAMESPACE
|
92
src/corelib/kernel/qtestsupport_core.h
Normal file
92
src/corelib/kernel/qtestsupport_core.h
Normal file
@ -0,0 +1,92 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2018 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the QtTest module of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:LGPL$
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU Lesser General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU Lesser
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.LGPL3 included in the
|
||||
** packaging of this file. Please review the following information to
|
||||
** ensure the GNU Lesser General Public License version 3 requirements
|
||||
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 2.0 or (at your option) the GNU General
|
||||
** Public license version 3 or any later version approved by the KDE Free
|
||||
** Qt Foundation. The licenses are as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
|
||||
** https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef QTESTSUPPORT_CORE_H
|
||||
#define QTESTSUPPORT_CORE_H
|
||||
|
||||
#include <QtCore/qcoreapplication.h>
|
||||
#include <QtCore/qdeadlinetimer.h>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
namespace QTestPrivate {
|
||||
Q_CORE_EXPORT void qSleep(int ms);
|
||||
}
|
||||
|
||||
namespace QTest {
|
||||
|
||||
template <typename Functor>
|
||||
Q_REQUIRED_RESULT static bool qWaitFor(Functor predicate, int timeout = 5000)
|
||||
{
|
||||
// We should not spin the event loop in case the predicate is already true,
|
||||
// otherwise we might send new events that invalidate the predicate.
|
||||
if (predicate())
|
||||
return true;
|
||||
|
||||
// qWait() is expected to spin the event loop, even when called with a small
|
||||
// timeout like 1ms, so we we can't use a simple while-loop here based on
|
||||
// the deadline timer not having timed out. Use do-while instead.
|
||||
|
||||
int remaining = timeout;
|
||||
QDeadlineTimer deadline(remaining, Qt::PreciseTimer);
|
||||
|
||||
do {
|
||||
QCoreApplication::processEvents(QEventLoop::AllEvents, remaining);
|
||||
QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
|
||||
|
||||
remaining = deadline.remainingTime();
|
||||
if (remaining > 0)
|
||||
QTestPrivate::qSleep(qMin(10, remaining));
|
||||
|
||||
if (predicate())
|
||||
return true;
|
||||
|
||||
remaining = deadline.remainingTime();
|
||||
} while (remaining > 0);
|
||||
|
||||
return predicate(); // Last chance
|
||||
}
|
||||
|
||||
Q_CORE_EXPORT void qWait(int ms);
|
||||
|
||||
} // namespace QTest
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
||||
#endif
|
@ -74,8 +74,8 @@ HEADERS += \
|
||||
kernel/qplatformgraphicsbufferhelper.h \
|
||||
kernel/qinputdevicemanager_p.h \
|
||||
kernel/qinputdevicemanager_p_p.h \
|
||||
kernel/qhighdpiscaling_p.h
|
||||
|
||||
kernel/qhighdpiscaling_p.h \
|
||||
kernel/qtestsupport_gui.h
|
||||
|
||||
SOURCES += \
|
||||
kernel/qgenericpluginfactory.cpp \
|
||||
@ -128,7 +128,8 @@ SOURCES += \
|
||||
kernel/qplatformgraphicsbuffer.cpp \
|
||||
kernel/qplatformgraphicsbufferhelper.cpp \
|
||||
kernel/qinputdevicemanager.cpp \
|
||||
kernel/qhighdpiscaling.cpp
|
||||
kernel/qhighdpiscaling.cpp \
|
||||
kernel/qtestsupport_gui.cpp
|
||||
|
||||
qtConfig(draganddrop) {
|
||||
HEADERS += \
|
||||
|
83
src/gui/kernel/qtestsupport_gui.cpp
Normal file
83
src/gui/kernel/qtestsupport_gui.cpp
Normal file
@ -0,0 +1,83 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2018 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the QtTest module of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:LGPL$
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU Lesser General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU Lesser
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.LGPL3 included in the
|
||||
** packaging of this file. Please review the following information to
|
||||
** ensure the GNU Lesser General Public License version 3 requirements
|
||||
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 2.0 or (at your option) the GNU General
|
||||
** Public license version 3 or any later version approved by the KDE Free
|
||||
** Qt Foundation. The licenses are as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
|
||||
** https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "qtestsupport_gui.h"
|
||||
#include "qwindow.h"
|
||||
|
||||
#include <QtCore/qtestsupport_core.h>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
/*! \fn bool qWaitForWindowActive(QWindow *window, int timeout)
|
||||
\relates QTest
|
||||
\since 5.0
|
||||
|
||||
Waits for \a timeout milliseconds or until the \a window is active.
|
||||
|
||||
Returns \c true if \c window is active within \a timeout milliseconds, otherwise returns \c false.
|
||||
|
||||
\sa QTest::qWaitForWindowExposed(), QWindow::isActive()
|
||||
*/
|
||||
Q_GUI_EXPORT bool QTest::qWaitForWindowActive(QWindow *window, int timeout)
|
||||
{
|
||||
return QTest::qWaitFor([&]() { return window->isActive(); }, timeout);
|
||||
}
|
||||
|
||||
/*! \fn bool qWaitForWindowExposed(QWindow *window, int timeout)
|
||||
\relates QTest
|
||||
\since 5.0
|
||||
|
||||
Waits for \a timeout milliseconds or until the \a window is exposed.
|
||||
Returns \c true if \c window is exposed within \a timeout milliseconds, otherwise returns \c false.
|
||||
|
||||
This is mainly useful for asynchronous systems like X11, where a window will be mapped to screen some
|
||||
time after being asked to show itself on the screen.
|
||||
|
||||
Note that a window that is mapped to screen may still not be considered exposed if the window client
|
||||
area is completely covered by other windows, or if the window is otherwise not visible. This function
|
||||
will then time out when waiting for such a window.
|
||||
|
||||
\sa QTest::qWaitForWindowActive(), QWindow::isExposed()
|
||||
*/
|
||||
Q_GUI_EXPORT bool QTest::qWaitForWindowExposed(QWindow *window, int timeout)
|
||||
{
|
||||
return QTest::qWaitFor([&]() { return window->isExposed(); }, timeout);
|
||||
}
|
||||
|
||||
QT_END_NAMESPACE
|
56
src/gui/kernel/qtestsupport_gui.h
Normal file
56
src/gui/kernel/qtestsupport_gui.h
Normal file
@ -0,0 +1,56 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2018 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the QtTest module of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:LGPL$
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU Lesser General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU Lesser
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.LGPL3 included in the
|
||||
** packaging of this file. Please review the following information to
|
||||
** ensure the GNU Lesser General Public License version 3 requirements
|
||||
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 2.0 or (at your option) the GNU General
|
||||
** Public license version 3 or any later version approved by the KDE Free
|
||||
** Qt Foundation. The licenses are as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
|
||||
** https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef QTESTSUPPORT_GUI_H
|
||||
#define QTESTSUPPORT_GUI_H
|
||||
|
||||
#include "qtguiglobal.h"
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
class QWindow;
|
||||
|
||||
namespace QTest {
|
||||
Q_GUI_EXPORT Q_REQUIRED_RESULT bool qWaitForWindowActive(QWindow *window, int timeout = 5000);
|
||||
Q_GUI_EXPORT Q_REQUIRED_RESULT bool qWaitForWindowExposed(QWindow *window, int timeout = 5000);
|
||||
}
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
||||
#endif
|
@ -177,13 +177,6 @@ namespace MyNamespace {
|
||||
}
|
||||
//! [toString-overload]
|
||||
|
||||
//! [17]
|
||||
int i = 0;
|
||||
while (myNetworkServerNotResponding() && i++ < 50)
|
||||
QTest::qWait(250);
|
||||
//! [17]
|
||||
|
||||
|
||||
//! [18]
|
||||
MyTestObject test1;
|
||||
QTest::qExec(&test1);
|
||||
@ -245,11 +238,6 @@ void MyTestClass::cleanup()
|
||||
QTest::qSleep(250);
|
||||
//! [23]
|
||||
|
||||
//! [24]
|
||||
QWidget widget;
|
||||
widget.show();
|
||||
QTest::qWaitForWindowShown(&widget);
|
||||
//! [24]
|
||||
|
||||
//! [25]
|
||||
QTouchDevice *dev = QTest::createTouchDevice();
|
||||
@ -306,13 +294,5 @@ QTest::keyClick(myWindow, Qt::Key_Escape);
|
||||
QTest::keyClick(myWindow, Qt::Key_Escape, Qt::ShiftModifier, 200);
|
||||
//! [29]
|
||||
|
||||
//! [30]
|
||||
MyObject obj;
|
||||
obj.startup();
|
||||
QTest::qWaitFor([&]() {
|
||||
return obj.isReady();
|
||||
}, 3000);
|
||||
//! [30]
|
||||
|
||||
}
|
||||
|
||||
|
@ -60,6 +60,8 @@
|
||||
#include <QtCore/qwaitcondition.h>
|
||||
#include <QtCore/qmutex.h>
|
||||
|
||||
#include <QtCore/qtestsupport_core.h>
|
||||
|
||||
#include <QtTest/private/qtestlog_p.h>
|
||||
#include <QtTest/private/qtesttable_p.h>
|
||||
#include <QtTest/qtestdata.h>
|
||||
@ -2411,16 +2413,9 @@ bool QTest::currentTestFailed()
|
||||
*/
|
||||
void QTest::qSleep(int ms)
|
||||
{
|
||||
// ### Qt 6, move to QtCore or remove altogether
|
||||
QTEST_ASSERT(ms > 0);
|
||||
|
||||
#if defined(Q_OS_WINRT)
|
||||
WaitForSingleObjectEx(GetCurrentThread(), ms, true);
|
||||
#elif defined(Q_OS_WIN)
|
||||
Sleep(uint(ms));
|
||||
#else
|
||||
struct timespec ts = { time_t(ms / 1000), (ms % 1000) * 1000 * 1000 };
|
||||
nanosleep(&ts, NULL);
|
||||
#endif
|
||||
QTestPrivate::qSleep(ms);
|
||||
}
|
||||
|
||||
/*! \internal
|
||||
|
@ -1112,105 +1112,6 @@
|
||||
Returns a textual representation of size policy \a sp.
|
||||
*/
|
||||
|
||||
/*! \fn void QTest::qWait(int ms)
|
||||
|
||||
Waits for \a ms milliseconds. While waiting, events will be processed and
|
||||
your test will stay responsive to user interface events or network communication.
|
||||
|
||||
Example:
|
||||
\snippet code/src_qtestlib_qtestcase.cpp 17
|
||||
|
||||
The code above will wait until the network server is responding for a
|
||||
maximum of about 12.5 seconds.
|
||||
|
||||
\sa QTest::qSleep(), QSignalSpy::wait()
|
||||
*/
|
||||
|
||||
/*! \fn template <typename Functor> bool QTest::qWaitFor(Functor predicate, int timeout)
|
||||
|
||||
Waits for \a timeout milliseconds or until the \a predicate returns true.
|
||||
|
||||
Returns \c true if the \a predicate returned true at any point, otherwise returns \c false.
|
||||
|
||||
Example:
|
||||
\snippet code/src_qtestlib_qtestcase.cpp 30
|
||||
|
||||
The code above will wait for the object to become ready, for a
|
||||
maximum of three seconds.
|
||||
|
||||
\since 5.10
|
||||
*/
|
||||
|
||||
/*! \fn bool QTest::qWaitForWindowExposed(QWindow *window, int timeout)
|
||||
\since 5.0
|
||||
|
||||
Waits for \a timeout milliseconds or until the \a window is exposed.
|
||||
Returns \c true if \c window is exposed within \a timeout milliseconds, otherwise returns \c false.
|
||||
|
||||
This is mainly useful for asynchronous systems like X11, where a window will be mapped to screen some
|
||||
time after being asked to show itself on the screen.
|
||||
|
||||
Note that a window that is mapped to screen may still not be considered exposed if the window client
|
||||
area is completely covered by other windows, or if the window is otherwise not visible. This function
|
||||
will then time out when waiting for such a window.
|
||||
|
||||
\sa QTest::qWaitForWindowActive(), QWindow::isExposed()
|
||||
*/
|
||||
|
||||
/*! \fn bool QTest::qWaitForWindowActive(QWindow *window, int timeout)
|
||||
\since 5.0
|
||||
|
||||
Waits for \a timeout milliseconds or until the \a window is active.
|
||||
|
||||
Returns \c true if \c window is active within \a timeout milliseconds, otherwise returns \c false.
|
||||
|
||||
\sa QTest::qWaitForWindowExposed(), QWindow::isActive()
|
||||
*/
|
||||
|
||||
/*! \fn bool QTest::qWaitForWindowExposed(QWidget *widget, int timeout)
|
||||
\since 5.0
|
||||
|
||||
Waits for \a timeout milliseconds or until the \a widget's window is exposed.
|
||||
Returns \c true if \c widget's window is exposed within \a timeout milliseconds, otherwise returns \c false.
|
||||
|
||||
This is mainly useful for asynchronous systems like X11, where a window will be mapped to screen some
|
||||
time after being asked to show itself on the screen.
|
||||
|
||||
Note that a window that is mapped to screen may still not be considered exposed if the window client
|
||||
area is completely covered by other windows, or if the window is otherwise not visible. This function
|
||||
will then time out when waiting for such a window.
|
||||
|
||||
A specific configuration where this happens is when using QGLWidget as a viewport widget on macOS:
|
||||
The viewport widget gets the expose event, not the parent widget.
|
||||
|
||||
\sa QTest::qWaitForWindowActive()
|
||||
*/
|
||||
|
||||
/*! \fn bool QTest::qWaitForWindowActive(QWidget *widget, int timeout)
|
||||
\since 5.0
|
||||
|
||||
Waits for \a timeout milliseconds or until the \a widget's window is active.
|
||||
|
||||
Returns \c true if \c widget's window is active within \a timeout milliseconds, otherwise returns \c false.
|
||||
|
||||
\sa QTest::qWaitForWindowExposed(), QWidget::isActiveWindow()
|
||||
*/
|
||||
|
||||
/*! \fn bool QTest::qWaitForWindowShown(QWidget *widget, int timeout)
|
||||
\since 5.0
|
||||
\deprecated
|
||||
|
||||
Waits for \a timeout milliseconds or until the \a widget's window is exposed.
|
||||
Returns \c true if \c widget's window is exposed within \a timeout milliseconds, otherwise returns \c false.
|
||||
|
||||
This function does the same as qWaitForWindowExposed().
|
||||
|
||||
Example:
|
||||
\snippet code/src_qtestlib_qtestcase.cpp 24
|
||||
|
||||
\sa QTest::qWaitForWindowActive(), QTest::qWaitForWindowExposed()
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn QTouchDevice *QTest::createTouchDevice(QTouchDevice::DeviceType devType = QTouchDevice::TouchScreen)
|
||||
\since 5.8
|
||||
|
@ -41,111 +41,16 @@
|
||||
#define QTESTSYSTEM_H
|
||||
|
||||
#include <QtTest/qtestcase.h>
|
||||
#include <QtCore/qcoreapplication.h>
|
||||
#include <QtCore/qdeadlinetimer.h>
|
||||
|
||||
#include <QtCore/qtestsupport_core.h>
|
||||
#ifdef QT_GUI_LIB
|
||||
# include <QtGui/QWindow>
|
||||
# include <QtGui/qtestsupport_gui.h>
|
||||
#endif
|
||||
#ifdef QT_WIDGETS_LIB
|
||||
# include <QtWidgets/QWidget>
|
||||
# include <QtWidgets/qtestsupport_widgets.h>
|
||||
#endif
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
namespace QTest
|
||||
{
|
||||
template <typename Functor>
|
||||
Q_REQUIRED_RESULT static bool qWaitFor(Functor predicate, int timeout = 5000)
|
||||
{
|
||||
// We should not spin the event loop in case the predicate is already true,
|
||||
// otherwise we might send new events that invalidate the predicate.
|
||||
if (predicate())
|
||||
return true;
|
||||
|
||||
// qWait() is expected to spin the event loop, even when called with a small
|
||||
// timeout like 1ms, so we we can't use a simple while-loop here based on
|
||||
// the deadline timer not having timed out. Use do-while instead.
|
||||
|
||||
int remaining = timeout;
|
||||
QDeadlineTimer deadline(remaining, Qt::PreciseTimer);
|
||||
|
||||
do {
|
||||
QCoreApplication::processEvents(QEventLoop::AllEvents, remaining);
|
||||
QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
|
||||
|
||||
remaining = deadline.remainingTime();
|
||||
if (remaining > 0)
|
||||
QTest::qSleep(qMin(10, remaining));
|
||||
|
||||
if (predicate())
|
||||
return true;
|
||||
|
||||
remaining = deadline.remainingTime();
|
||||
} while (remaining > 0);
|
||||
|
||||
return predicate(); // Last chance
|
||||
}
|
||||
|
||||
Q_DECL_UNUSED inline static void qWait(int ms)
|
||||
{
|
||||
// Ideally this method would be implemented in terms of qWaitFor, with
|
||||
// a predicate that always returns false, but due to a compiler bug in
|
||||
// GCC 6 we can't do that.
|
||||
|
||||
Q_ASSERT(QCoreApplication::instance());
|
||||
|
||||
QDeadlineTimer timer(ms, Qt::PreciseTimer);
|
||||
int remaining = ms;
|
||||
do {
|
||||
QCoreApplication::processEvents(QEventLoop::AllEvents, remaining);
|
||||
QCoreApplication::sendPostedEvents(Q_NULLPTR, QEvent::DeferredDelete);
|
||||
remaining = timer.remainingTime();
|
||||
if (remaining <= 0)
|
||||
break;
|
||||
QTest::qSleep(qMin(10, remaining));
|
||||
remaining = timer.remainingTime();
|
||||
} while (remaining > 0);
|
||||
}
|
||||
|
||||
#ifdef QT_GUI_LIB
|
||||
Q_REQUIRED_RESULT inline static bool qWaitForWindowActive(QWindow *window, int timeout = 5000)
|
||||
{
|
||||
return qWaitFor([&]() { return window->isActive(); }, timeout);
|
||||
}
|
||||
|
||||
Q_REQUIRED_RESULT inline static bool qWaitForWindowExposed(QWindow *window, int timeout = 5000)
|
||||
{
|
||||
return qWaitFor([&]() { return window->isExposed(); }, timeout);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef QT_WIDGETS_LIB
|
||||
Q_REQUIRED_RESULT inline static bool qWaitForWindowActive(QWidget *widget, int timeout = 5000)
|
||||
{
|
||||
if (QWindow *window = widget->window()->windowHandle())
|
||||
return qWaitForWindowActive(window, timeout);
|
||||
return false;
|
||||
}
|
||||
|
||||
Q_REQUIRED_RESULT inline static bool qWaitForWindowExposed(QWidget *widget, int timeout = 5000)
|
||||
{
|
||||
if (QWindow *window = widget->window()->windowHandle())
|
||||
return qWaitForWindowExposed(window, timeout);
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if QT_DEPRECATED_SINCE(5, 0)
|
||||
# ifdef QT_WIDGETS_LIB
|
||||
|
||||
QT_DEPRECATED Q_REQUIRED_RESULT inline static bool qWaitForWindowShown(QWidget *widget, int timeout = 5000)
|
||||
{
|
||||
return qWaitForWindowExposed(widget, timeout);
|
||||
}
|
||||
# endif // QT_WIDGETS_LIB
|
||||
#endif // QT_DEPRECATED_SINCE(5, 0)
|
||||
}
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
||||
#endif
|
||||
|
@ -35,7 +35,8 @@ HEADERS += \
|
||||
kernel/qgesturemanager_p.h \
|
||||
kernel/qdesktopwidget_p.h \
|
||||
kernel/qwidgetwindow_p.h \
|
||||
kernel/qwindowcontainer_p.h
|
||||
kernel/qwindowcontainer_p.h \
|
||||
kernel/qtestsupport_widgets.h
|
||||
|
||||
SOURCES += \
|
||||
kernel/qaction.cpp \
|
||||
@ -60,7 +61,8 @@ SOURCES += \
|
||||
kernel/qdesktopwidget.cpp \
|
||||
kernel/qwidgetsvariant.cpp \
|
||||
kernel/qwidgetwindow.cpp \
|
||||
kernel/qwindowcontainer.cpp
|
||||
kernel/qwindowcontainer.cpp \
|
||||
kernel/qtestsupport_widgets.cpp
|
||||
|
||||
macx: {
|
||||
HEADERS += kernel/qmacgesturerecognizer_p.h
|
||||
|
119
src/widgets/kernel/qtestsupport_widgets.cpp
Normal file
119
src/widgets/kernel/qtestsupport_widgets.cpp
Normal file
@ -0,0 +1,119 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2018 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the QtTest module of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:LGPL$
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU Lesser General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU Lesser
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.LGPL3 included in the
|
||||
** packaging of this file. Please review the following information to
|
||||
** ensure the GNU Lesser General Public License version 3 requirements
|
||||
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 2.0 or (at your option) the GNU General
|
||||
** Public license version 3 or any later version approved by the KDE Free
|
||||
** Qt Foundation. The licenses are as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
|
||||
** https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "qtestsupport_widgets.h"
|
||||
|
||||
#include "qwidget.h"
|
||||
|
||||
#include <QtGui/qwindow.h>
|
||||
#include <QtGui/qtestsupport_gui.h>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
/*! \fn bool qWaitForWindowActive(QWidget *widget, int timeout)
|
||||
\relates QTest
|
||||
\since 5.0
|
||||
|
||||
Waits for \a timeout milliseconds or until the \a widget's window is active.
|
||||
|
||||
Returns \c true if \c widget's window is active within \a timeout milliseconds, otherwise returns \c false.
|
||||
|
||||
\sa QTest::qWaitForWindowExposed(), QWidget::isActiveWindow()
|
||||
*/
|
||||
Q_WIDGETS_EXPORT Q_REQUIRED_RESULT bool QTest::qWaitForWindowActive(QWidget *widget, int timeout)
|
||||
{
|
||||
if (QWindow *window = widget->window()->windowHandle())
|
||||
return QTest::qWaitForWindowActive(window, timeout);
|
||||
return false;
|
||||
}
|
||||
|
||||
/*! \fn bool qWaitForWindowExposed(QWidget *widget, int timeout)
|
||||
\relates QTest
|
||||
\since 5.0
|
||||
|
||||
Waits for \a timeout milliseconds or until the \a widget's window is exposed.
|
||||
Returns \c true if \c widget's window is exposed within \a timeout milliseconds, otherwise returns \c false.
|
||||
|
||||
This is mainly useful for asynchronous systems like X11, where a window will be mapped to screen some
|
||||
time after being asked to show itself on the screen.
|
||||
|
||||
Note that a window that is mapped to screen may still not be considered exposed if the window client
|
||||
area is completely covered by other windows, or if the window is otherwise not visible. This function
|
||||
will then time out when waiting for such a window.
|
||||
|
||||
A specific configuration where this happens is when using QGLWidget as a viewport widget on macOS:
|
||||
The viewport widget gets the expose event, not the parent widget.
|
||||
|
||||
\sa QTest::qWaitForWindowActive()
|
||||
*/
|
||||
Q_WIDGETS_EXPORT Q_REQUIRED_RESULT bool QTest::qWaitForWindowExposed(QWidget *widget, int timeout)
|
||||
{
|
||||
if (QWindow *window = widget->window()->windowHandle())
|
||||
return QTest::qWaitForWindowExposed(window, timeout);
|
||||
return false;
|
||||
}
|
||||
|
||||
#if QT_DEPRECATED_SINCE(5, 0)
|
||||
/*! \fn bool qWaitForWindowShown(QWidget *widget, int timeout)
|
||||
\relates QTest
|
||||
\since 5.0
|
||||
\deprecated
|
||||
|
||||
Waits for \a timeout milliseconds or until the \a widget's window is exposed.
|
||||
Returns \c true if \c widget's window is exposed within \a timeout milliseconds, otherwise returns \c false.
|
||||
|
||||
This function does the same as qWaitForWindowExposed().
|
||||
|
||||
Example:
|
||||
|
||||
\code
|
||||
QWidget widget;
|
||||
widget.show();
|
||||
QTest::qWaitForWindowShown(&widget);
|
||||
\endcode
|
||||
|
||||
\sa QTest::qWaitForWindowActive(), QTest::qWaitForWindowExposed()
|
||||
*/
|
||||
Q_WIDGETS_EXPORT QT_DEPRECATED Q_REQUIRED_RESULT bool QTest::qWaitForWindowShown(QWidget *widget, int timeout)
|
||||
{
|
||||
return QTest::qWaitForWindowExposed(widget, timeout);
|
||||
}
|
||||
#endif
|
||||
|
||||
QT_END_NAMESPACE
|
60
src/widgets/kernel/qtestsupport_widgets.h
Normal file
60
src/widgets/kernel/qtestsupport_widgets.h
Normal file
@ -0,0 +1,60 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2018 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the QtTest module of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:LGPL$
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU Lesser General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU Lesser
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.LGPL3 included in the
|
||||
** packaging of this file. Please review the following information to
|
||||
** ensure the GNU Lesser General Public License version 3 requirements
|
||||
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 2.0 or (at your option) the GNU General
|
||||
** Public license version 3 or any later version approved by the KDE Free
|
||||
** Qt Foundation. The licenses are as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
|
||||
** https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef QTESTSUPPORT_WIDGETS_H
|
||||
#define QTESTSUPPORT_WIDGETS_H
|
||||
|
||||
#include "qtwidgetsglobal.h"
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
class QWidget;
|
||||
|
||||
namespace QTest {
|
||||
Q_WIDGETS_EXPORT Q_REQUIRED_RESULT bool qWaitForWindowActive(QWidget *widget, int timeout = 5000);
|
||||
Q_WIDGETS_EXPORT Q_REQUIRED_RESULT bool qWaitForWindowExposed(QWidget *widget, int timeout = 5000);
|
||||
|
||||
#if QT_DEPRECATED_SINCE(5, 0)
|
||||
Q_WIDGETS_EXPORT QT_DEPRECATED Q_REQUIRED_RESULT bool qWaitForWindowShown(QWidget *widget, int timeout = 5000);
|
||||
#endif
|
||||
}
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user