summaryrefslogtreecommitdiffstats
path: root/tests/auto/qdirectpainter/tst_qdirectpainter.cpp
blob: 84ab645a9611ca02d78d86e82b760673374cba9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QtTest/QtTest>

#ifdef Q_WS_QWS

//TESTED_CLASS=
//TESTED_FILES=

#include <qwindowsystem_qws.h>
#include <qpainter.h>
#include <qdesktopwidget.h>
#include <qdirectpainter_qws.h>
#include <private/qwindowsurface_qws_p.h>
#include <private/qdrawhelper_p.h>

class tst_QDirectPainter : public QObject
{
    Q_OBJECT

public:
    tst_QDirectPainter() {}
    ~tst_QDirectPainter() {}

private slots:
    void initTestCase();
    void setGeometry_data();
    void setGeometry();
    void regionSynchronization();
    void reservedSynchronous();

private:
    QWSWindow* getWindow(int windId);
    QColor bgColor;
};

class ColorPainter : public QDirectPainter
{
public:
    ColorPainter(SurfaceFlag flag = NonReserved,
                 const QColor &color = QColor(Qt::red))
        : QDirectPainter(0, flag), c(color) {}

    QColor color() { return c; }

protected:
    void regionChanged(const QRegion &region) {
        QScreen::instance()->solidFill(c, region);
    }

private:
    QColor c;
    QRegion r;
};

Q_DECLARE_METATYPE(QDirectPainter::SurfaceFlag)

void tst_QDirectPainter::initTestCase()
{
    bgColor = QColor(Qt::green);

    QWSServer *server = QWSServer::instance();
    server->setBackground(bgColor);
}

QWSWindow* tst_QDirectPainter::getWindow(int winId)
{
    QWSServer *server = QWSServer::instance();
    foreach (QWSWindow *w, server->clientWindows()) {
        if (w->winId() == winId)
            return w;
    }
    return 0;
}

class ColorWidget : public QWidget
{
public:
    ColorWidget(QWidget *parent = 0, const QColor &c = QColor(Qt::red))
        : QWidget(parent, Qt::FramelessWindowHint), color(c)
    {
        QPalette opaquePalette = palette();
        opaquePalette.setColor(backgroundRole(), color);
        setPalette(opaquePalette);
        setAutoFillBackground(true);
    }

    void paintEvent(QPaintEvent *e) {
        r += e->region();
    }

    void reset() {
        r = QRegion();
    }

    QColor color;
    QRegion r;
};

#define VERIFY_COLOR(rect, color) {                                     \
    const QPixmap pixmap = QPixmap::grabWindow(QDesktopWidget().winId(), \
                                               rect.left(), rect.top(), \
                                               rect.width(), rect.height()); \
    QCOMPARE(pixmap.size(), rect.size());                               \
    QPixmap expectedPixmap(pixmap); /* ensure equal formats */          \
    expectedPixmap.fill(color);                                         \
    QCOMPARE(pixmap, expectedPixmap);                                   \
}

void tst_QDirectPainter::setGeometry_data()
{
    QTest::addColumn<QDirectPainter::SurfaceFlag>("flag");

    QTest::newRow("NonReserved") << QDirectPainter::NonReserved;
    QTest::newRow("Reserved") << QDirectPainter::Reserved;
    QTest::newRow("ReservedSynchronous") << QDirectPainter::ReservedSynchronous;
}

void tst_QDirectPainter::setGeometry()
{
    QFETCH(QDirectPainter::SurfaceFlag, flag);

    const QRect rect(100, 100, 100, 100);
    {
        ColorPainter w(flag);

        w.setGeometry(rect);
        QApplication::processEvents();
        QCOMPARE(w.geometry(), rect);
        VERIFY_COLOR(rect, w.color());
    }
    QApplication::processEvents();
    VERIFY_COLOR(rect, bgColor);
}

void tst_QDirectPainter::regionSynchronization()
{
#ifdef QT_NO_PROCESS
    QSKIP("Test requires QProcess", SkipAll);
#else
    QRect dpRect(10, 10, 50, 50);

    // Start the direct painter in a different process
    QProcess proc;
    QStringList args;
    args << QString::number(dpRect.x())
         << QString::number(dpRect.y())
         << QString::number(dpRect.width())
         << QString::number(dpRect.height());

    proc.start("runDirectPainter/runDirectPainter", args);
    QVERIFY(proc.waitForStarted(5 * 1000));
    QTest::qWait(1000);
    QApplication::processEvents();
    VERIFY_COLOR(dpRect, Qt::blue); // blue hardcoded in runDirectPainter

    QTime t;
    t.start();
    static int i = 0;
    while (t.elapsed() < 10 * 1000) {
        QApplication::processEvents();

        ColorWidget w;
        w.setGeometry(10, 10, 50, 50);
        const QRect wRect = dpRect.translated(10, 0);
        w.setGeometry(wRect);
        w.show();

        QApplication::processEvents();
        QApplication::processEvents(); //glib event loop workaround
        VERIFY_COLOR(wRect, w.color);
        ++i;
    }
    QVERIFY(i > 100); // sanity check

    proc.kill();
#endif
}

class MyObject : public QObject
{
public:
    MyObject(QObject *p = 0) : QObject(p), lastEvent(0) {}

    bool event(QEvent *e) {
        lastEvent = e;
        return true;
    }

    QEvent *lastEvent;
};

void tst_QDirectPainter::reservedSynchronous()
{
    MyObject o;
    QCoreApplication::postEvent(&o, new QEvent(QEvent::None));
    QDirectPainter p(0, QDirectPainter::ReservedSynchronous);
    p.setRegion(QRect(5, 5, 50, 50));

    // The event loop should not have been executed
    QVERIFY(o.lastEvent == 0);
    QCOMPARE(p.allocatedRegion(), QRegion(QRect(5, 5, 50, 50)));
}

QTEST_MAIN(tst_QDirectPainter)

#include "tst_qdirectpainter.moc"

#else // Q_WS_QWS
QTEST_NOOP_MAIN
#endif