summaryrefslogtreecommitdiffstats
path: root/tests/auto/qwindowsurface/tst_qwindowsurface.cpp
blob: df87a4262bc2b77a84cd4baff151af535a8b550b (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/****************************************************************************
**
** 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>
#include <QPainter>
#include <QPalette>
#include <QWindowsStyle>

#ifndef Q_WS_MAC

#include <private/qwindowsurface_p.h>
#include <QDesktopWidget>
#include <QX11Info>

class tst_QWindowSurface : public QObject
{
    Q_OBJECT

public:
    tst_QWindowSurface() {}
    ~tst_QWindowSurface() {}

private slots:
    void getSetWindowSurface();
    void flushOutsidePaintEvent();
    void grabWidget();
};

class MyWindowSurface : public QWindowSurface
{
public:
    MyWindowSurface(QWidget *w) : QWindowSurface(w) {}

    QPaintDevice *paintDevice() {
        return &image;
    }

    void flush(QWidget*, const QRegion&, const QPoint&) {
        /* nothing */
    }

private:
    QImage image;
};

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(region, color) {                                   \
    const QRegion r = QRegion(region);                                  \
    for (int i = 0; i < r.rects().size(); ++i) {                        \
        const QRect rect = r.rects().at(i);                             \
        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_QWindowSurface::getSetWindowSurface()
{
    QWidget w;
    QVERIFY(!w.windowSurface());

    w.show();
    QApplication::processEvents();
    QVERIFY(w.windowSurface());

    for (int i = 0; i < 2; ++i) {
        MyWindowSurface *surface = new MyWindowSurface(&w);
        QCOMPARE(w.windowSurface(), (QWindowSurface *)surface);

        w.setWindowSurface(surface);
        QCOMPARE(w.windowSurface(), (QWindowSurface *)surface);
    }
}

void tst_QWindowSurface::flushOutsidePaintEvent()
{
#ifdef Q_WS_X11
    if (QX11Info::isCompositingManagerRunning())
        QSKIP("Test is unreliable with composition manager", SkipAll);
#endif

#ifdef Q_WS_WIN
    if (QSysInfo::WindowsVersion & QSysInfo::WV_VISTA) {
        QTest::qWait(1000);
    }
#endif
    ColorWidget w(0, Qt::red);
    w.setGeometry(10, 100, 50, 50);
    // prevent custom styles from messing up the background
    w.setStyle(new QWindowsStyle);
    w.show();

    QApplication::processEvents();
#ifdef Q_WS_X11
    qt_x11_wait_for_window_manager(&w);
#elif defined(Q_WS_QWS)
    QApplication::sendPostedEvents(); //for the glib event loop
#elif defined(Q_WS_S60)
    QTest::qWait(5000);
#endif
    VERIFY_COLOR(w.geometry(), w.color);
    w.reset();

    // trigger a paintEvent() the next time the event loop is entered
    w.update();

    // draw a black rectangle inside the widget
    QWindowSurface *surface = w.windowSurface();
    QVERIFY(surface);
    const QRect rect = surface->rect(&w);
    surface->beginPaint(rect);
    QImage *img = surface->buffer(&w);
    if (img) {
        QPainter p(img);
        p.fillRect(QRect(QPoint(),img->size()), Qt::black);
    }
    surface->endPaint(rect);
    surface->flush(&w, rect, QPoint());

#ifdef Q_WS_QWS
    VERIFY_COLOR(w.geometry(), Qt::black);
#endif

    // the paintEvent() should overwrite the painted rectangle
    QApplication::processEvents();
#ifdef Q_WS_X11
    qt_x11_wait_for_window_manager(&w);
#endif

#if defined(Q_WS_QWS)
    QSKIP("task 176755", SkipAll);
#endif
    VERIFY_COLOR(w.geometry(), w.color);
    QCOMPARE(QRegion(w.rect()), w.r);
    w.reset();
}


void tst_QWindowSurface::grabWidget()
{
    QWidget parentWidget;
    QWidget childWidget(&parentWidget);
    QWidget babyWidget(&childWidget);

    parentWidget.resize(300, 300);
    childWidget.setGeometry(50, 50, 200, 200);
    childWidget.setAutoFillBackground(true);
    babyWidget.setGeometry(50, 50, 100, 100);
    babyWidget.setAutoFillBackground(true);

    QPalette pal = parentWidget.palette();
    pal.setColor(QPalette::Window, QColor(Qt::blue));
    parentWidget.setPalette(pal);

    pal = childWidget.palette();
    pal.setColor(QPalette::Window, QColor(Qt::white));
    childWidget.setPalette(pal);

    pal = babyWidget.palette();
    pal.setColor(QPalette::Window, QColor(Qt::red));
    babyWidget.setPalette(pal);

    // prevent custom styles from messing up the background
    parentWidget.setStyle(new QWindowsStyle);

    babyWidget.show();
    childWidget.show();
    parentWidget.show();

    QTest::qWait(200);

    QPixmap parentPixmap = parentWidget.windowSurface()->grabWidget(&parentWidget);
    QPixmap childPixmap = childWidget.windowSurface()->grabWidget(&childWidget);
    QPixmap babyPixmap = babyWidget.windowSurface()->grabWidget(&babyWidget);
    QPixmap parentSubPixmap = parentWidget.windowSurface()->grabWidget(&parentWidget, QRect(25, 25, 100, 100));
    QPixmap childSubPixmap = childWidget.windowSurface()->grabWidget(&childWidget, QRect(55, 55, 50, 50));
    QPixmap childInvalidSubPixmap = childWidget.windowSurface()->grabWidget(&childWidget, QRect(-50, -50, 150, 150));

    QCOMPARE(parentPixmap.size(), QSize(300, 300));
    QCOMPARE(childPixmap.size(), QSize(200, 200));
    QCOMPARE(babyPixmap.size(), QSize(100, 100));
    QCOMPARE(parentSubPixmap.size(), QSize(100, 100));
    QCOMPARE(childSubPixmap.size(), QSize(50, 50));
    QCOMPARE(childInvalidSubPixmap.size(), QSize(100, 100));

    QImage parentImage = parentPixmap.toImage();
    QImage childImage = childPixmap.toImage();
    QImage babyImage = babyPixmap.toImage();
    QImage parentSubImage = parentSubPixmap.toImage();
    QImage childSubImage = childSubPixmap.toImage();
    QImage childInvalidSubImage = childInvalidSubPixmap.toImage();

    QVERIFY(QColor(parentImage.pixel(0, 0)) == QColor(Qt::blue));
    QVERIFY(QColor(parentImage.pixel(75, 75)) == QColor(Qt::white));
    QVERIFY(QColor(parentImage.pixel(149, 149)) == QColor(Qt::red));

    QVERIFY(QColor(childImage.pixel(0, 0)) == QColor(Qt::white));
    QVERIFY(QColor(childImage.pixel(99, 99)) == QColor(Qt::red));

    QVERIFY(QColor(parentSubImage.pixel(0, 0)) == QColor(Qt::blue));
    QVERIFY(QColor(parentSubImage.pixel(30, 30)) == QColor(Qt::white));
    QVERIFY(QColor(parentSubImage.pixel(80, 80)) == QColor(Qt::red));

    QVERIFY(QColor(childSubImage.pixel(0, 0)) == QColor(Qt::red));

    QVERIFY(QColor(childInvalidSubImage.pixel(0, 0)) == QColor(Qt::white));
}

QTEST_MAIN(tst_QWindowSurface)

#else // Q_WS_MAC

QTEST_NOOP_MAIN

#endif

#include "tst_qwindowsurface.moc"