1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the QtTest module of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
45
#include <QtTest/qtestcase.h>
46
#include <QtCore/qcoreapplication.h>
47
#include <QtCore/qelapsedtimer.h>
49
# include <QtGui/QWindow>
52
# include <QtWidgets/QWidget>
61
inline static void qWait(int ms)
63
Q_ASSERT(QCoreApplication::instance());
68
QCoreApplication::processEvents(QEventLoop::AllEvents, ms);
69
QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
71
} while (timer.elapsed() < ms);
75
inline static bool qWaitForWindowActive(QWindow *window, int timeout = 1000)
79
while (!window->isActive()) {
80
int remaining = timeout - int(timer.elapsed());
83
QCoreApplication::processEvents(QEventLoop::AllEvents, remaining);
84
QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
87
// Try ensuring the platform window receives the real position.
88
// (i.e. that window->pos() reflects reality)
89
// isActive() ( == FocusIn in case of X) does not guarantee this. It seems some WMs randomly
90
// send the final ConfigureNotify (the one with the non-bogus 0,0 position) after the FocusIn.
91
// If we just let things go, every mapTo/FromGlobal call the tests perform directly after
92
// qWaitForWindowShown() will generate bogus results.
93
if (window->isActive()) {
94
int waitNo = 0; // 0, 0 might be a valid position after all, so do not wait for ever
95
while (window->position().isNull()) {
96
if (waitNo++ > timeout / 10)
101
return window->isActive();
104
inline static bool qWaitForWindowExposed(QWindow *window, int timeout = 1000)
108
while (!window->isExposed()) {
109
int remaining = timeout - int(timer.elapsed());
112
QCoreApplication::processEvents(QEventLoop::AllEvents, remaining);
113
QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
116
return window->isExposed();
120
#ifdef QT_WIDGETS_LIB
121
inline static bool qWaitForWindowActive(QWidget *widget, int timeout = 1000)
123
if (QWindow *window = widget->windowHandle())
124
return qWaitForWindowActive(window, timeout);
128
inline static bool qWaitForWindowExposed(QWidget *widget, int timeout = 1000)
130
if (QWindow *window = widget->windowHandle())
131
return qWaitForWindowExposed(window, timeout);
136
#if QT_DEPRECATED_SINCE(5, 0)
137
# ifdef QT_WIDGETS_LIB
138
QT_DEPRECATED inline static bool qWaitForWindowShown(QWidget *widget, int timeout = 1000)
140
return qWaitForWindowExposed(widget, timeout);
142
# endif // QT_WIDGETS_LIB
143
#endif // QT_DEPRECATED_SINCE(5, 0)