~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to src/testlib/qtestsystem.h

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the QtTest module of the Qt Toolkit.
 
7
**
 
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.
 
16
**
 
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.
 
24
**
 
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.
 
28
**
 
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.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#ifndef QTESTSYSTEM_H
 
43
#define QTESTSYSTEM_H
 
44
 
 
45
#include <QtTest/qtestcase.h>
 
46
#include <QtCore/qcoreapplication.h>
 
47
#include <QtCore/qelapsedtimer.h>
 
48
#ifdef QT_GUI_LIB
 
49
#  include <QtGui/QWindow>
 
50
#endif
 
51
#ifdef QT_WIDGETS_LIB
 
52
#  include <QtWidgets/QWidget>
 
53
#endif
 
54
 
 
55
QT_BEGIN_HEADER
 
56
 
 
57
QT_BEGIN_NAMESPACE
 
58
 
 
59
namespace QTest
 
60
{
 
61
    inline static void qWait(int ms)
 
62
    {
 
63
        Q_ASSERT(QCoreApplication::instance());
 
64
 
 
65
        QElapsedTimer timer;
 
66
        timer.start();
 
67
        do {
 
68
            QCoreApplication::processEvents(QEventLoop::AllEvents, ms);
 
69
            QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
 
70
            QTest::qSleep(10);
 
71
        } while (timer.elapsed() < ms);
 
72
    }
 
73
 
 
74
#ifdef QT_GUI_LIB
 
75
    inline static bool qWaitForWindowActive(QWindow *window, int timeout = 1000)
 
76
    {
 
77
        QElapsedTimer timer;
 
78
        timer.start();
 
79
        while (!window->isActive()) {
 
80
            int remaining = timeout - int(timer.elapsed());
 
81
            if (remaining <= 0)
 
82
                break;
 
83
            QCoreApplication::processEvents(QEventLoop::AllEvents, remaining);
 
84
            QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
 
85
            QTest::qSleep(10);
 
86
        }
 
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)
 
97
                    break;
 
98
                qWait(10);
 
99
            }
 
100
        }
 
101
        return window->isActive();
 
102
    }
 
103
 
 
104
    inline static bool qWaitForWindowExposed(QWindow *window, int timeout = 1000)
 
105
    {
 
106
        QElapsedTimer timer;
 
107
        timer.start();
 
108
        while (!window->isExposed()) {
 
109
            int remaining = timeout - int(timer.elapsed());
 
110
            if (remaining <= 0)
 
111
                break;
 
112
            QCoreApplication::processEvents(QEventLoop::AllEvents, remaining);
 
113
            QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
 
114
            QTest::qSleep(10);
 
115
        }
 
116
        return window->isExposed();
 
117
    }
 
118
#endif
 
119
 
 
120
#ifdef QT_WIDGETS_LIB
 
121
    inline static bool qWaitForWindowActive(QWidget *widget, int timeout = 1000)
 
122
    {
 
123
        if (QWindow *window = widget->windowHandle())
 
124
            return qWaitForWindowActive(window, timeout);
 
125
        return false;
 
126
    }
 
127
 
 
128
    inline static bool qWaitForWindowExposed(QWidget *widget, int timeout = 1000)
 
129
    {
 
130
        if (QWindow *window = widget->windowHandle())
 
131
            return qWaitForWindowExposed(window, timeout);
 
132
        return false;
 
133
    }
 
134
#endif
 
135
 
 
136
#if QT_DEPRECATED_SINCE(5, 0)
 
137
#  ifdef QT_WIDGETS_LIB
 
138
    QT_DEPRECATED inline static bool qWaitForWindowShown(QWidget *widget, int timeout = 1000)
 
139
    {
 
140
        return qWaitForWindowExposed(widget, timeout);
 
141
    }
 
142
#  endif // QT_WIDGETS_LIB
 
143
#endif // QT_DEPRECATED_SINCE(5, 0)
 
144
}
 
145
 
 
146
QT_END_NAMESPACE
 
147
 
 
148
QT_END_HEADER
 
149
 
 
150
#endif
 
151