|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the test suite of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 |
|
43 #include <QtTest/QtTest> |
|
44 #include <QPainter> |
|
45 #include <QPalette> |
|
46 #include <QWindowsStyle> |
|
47 |
|
48 #ifndef Q_WS_MAC |
|
49 |
|
50 #include <private/qwindowsurface_p.h> |
|
51 #include <QDesktopWidget> |
|
52 #include <QX11Info> |
|
53 |
|
54 |
|
55 #include "../../shared/util.h" |
|
56 |
|
57 class tst_QWindowSurface : public QObject |
|
58 { |
|
59 Q_OBJECT |
|
60 |
|
61 public: |
|
62 tst_QWindowSurface() {} |
|
63 ~tst_QWindowSurface() {} |
|
64 |
|
65 private slots: |
|
66 void getSetWindowSurface(); |
|
67 void flushOutsidePaintEvent(); |
|
68 void grabWidget(); |
|
69 }; |
|
70 |
|
71 class MyWindowSurface : public QWindowSurface |
|
72 { |
|
73 public: |
|
74 MyWindowSurface(QWidget *w) : QWindowSurface(w) {} |
|
75 |
|
76 QPaintDevice *paintDevice() { |
|
77 return ℑ |
|
78 } |
|
79 |
|
80 void flush(QWidget*, const QRegion&, const QPoint&) { |
|
81 /* nothing */ |
|
82 } |
|
83 |
|
84 private: |
|
85 QImage image; |
|
86 }; |
|
87 |
|
88 class ColorWidget : public QWidget |
|
89 { |
|
90 public: |
|
91 ColorWidget(QWidget *parent = 0, const QColor &c = QColor(Qt::red)) |
|
92 : QWidget(parent, Qt::FramelessWindowHint), color(c) |
|
93 { |
|
94 QPalette opaquePalette = palette(); |
|
95 opaquePalette.setColor(backgroundRole(), color); |
|
96 setPalette(opaquePalette); |
|
97 setAutoFillBackground(true); |
|
98 } |
|
99 |
|
100 void paintEvent(QPaintEvent *e) { |
|
101 r += e->region(); |
|
102 } |
|
103 |
|
104 void reset() { |
|
105 r = QRegion(); |
|
106 } |
|
107 |
|
108 QColor color; |
|
109 QRegion r; |
|
110 }; |
|
111 |
|
112 //from tst_qwidget.cpp |
|
113 static void VERIFY_COLOR(const QRegion ®ion, const QColor &color) |
|
114 { |
|
115 const QRegion r = QRegion(region); |
|
116 for (int i = 0; i < r.rects().size(); ++i) { |
|
117 const QRect rect = r.rects().at(i); |
|
118 for (int t = 0; t < 5; t++) { |
|
119 const QPixmap pixmap = QPixmap::grabWindow(QDesktopWidget().winId(), |
|
120 rect.left(), rect.top(), |
|
121 rect.width(), rect.height()); |
|
122 QCOMPARE(pixmap.size(), rect.size()); |
|
123 QPixmap expectedPixmap(pixmap); /* ensure equal formats */ |
|
124 expectedPixmap.fill(color); |
|
125 if (pixmap.toImage().pixel(0,0) != QColor(color).rgb() && t < 4 ) |
|
126 { QTest::qWait(200); continue; } |
|
127 QCOMPARE(pixmap.toImage().pixel(0,0), QColor(color).rgb()); |
|
128 QCOMPARE(pixmap, expectedPixmap); |
|
129 break; |
|
130 } |
|
131 } |
|
132 } |
|
133 |
|
134 void tst_QWindowSurface::getSetWindowSurface() |
|
135 { |
|
136 QWidget w; |
|
137 QVERIFY(!w.windowSurface()); |
|
138 |
|
139 w.show(); |
|
140 QApplication::processEvents(); |
|
141 QVERIFY(w.windowSurface()); |
|
142 |
|
143 for (int i = 0; i < 2; ++i) { |
|
144 MyWindowSurface *surface = new MyWindowSurface(&w); |
|
145 QCOMPARE(w.windowSurface(), (QWindowSurface *)surface); |
|
146 |
|
147 w.setWindowSurface(surface); |
|
148 QCOMPARE(w.windowSurface(), (QWindowSurface *)surface); |
|
149 } |
|
150 } |
|
151 |
|
152 void tst_QWindowSurface::flushOutsidePaintEvent() |
|
153 { |
|
154 #ifdef Q_WS_X11 |
|
155 if (QX11Info::isCompositingManagerRunning()) |
|
156 QSKIP("Test is unreliable with composition manager", SkipAll); |
|
157 #endif |
|
158 |
|
159 #ifdef Q_WS_WIN |
|
160 if (QSysInfo::WindowsVersion & QSysInfo::WV_VISTA) { |
|
161 QTest::qWait(1000); |
|
162 } |
|
163 #endif |
|
164 ColorWidget w(0, Qt::red); |
|
165 w.setGeometry(10, 100, 50, 50); |
|
166 // prevent custom styles from messing up the background |
|
167 w.setStyle(new QWindowsStyle); |
|
168 w.show(); |
|
169 QTest::qWaitForWindowShown(&w); |
|
170 |
|
171 QApplication::processEvents(); |
|
172 #if defined(Q_WS_QWS) |
|
173 QApplication::sendPostedEvents(); //for the glib event loop |
|
174 #elif defined(Q_WS_S60) |
|
175 QTest::qWait(5000); |
|
176 #endif |
|
177 VERIFY_COLOR(w.geometry(), w.color); |
|
178 w.reset(); |
|
179 |
|
180 // trigger a paintEvent() the next time the event loop is entered |
|
181 w.update(); |
|
182 |
|
183 // draw a black rectangle inside the widget |
|
184 QWindowSurface *surface = w.windowSurface(); |
|
185 QVERIFY(surface); |
|
186 const QRect rect = surface->rect(&w); |
|
187 surface->beginPaint(rect); |
|
188 QImage *img = surface->buffer(&w); |
|
189 if (img) { |
|
190 QPainter p(img); |
|
191 p.fillRect(QRect(QPoint(),img->size()), Qt::black); |
|
192 } |
|
193 surface->endPaint(rect); |
|
194 surface->flush(&w, rect, QPoint()); |
|
195 |
|
196 #ifdef Q_WS_QWS |
|
197 VERIFY_COLOR(w.geometry(), Qt::black); |
|
198 #endif |
|
199 |
|
200 // the paintEvent() should overwrite the painted rectangle |
|
201 QApplication::processEvents(); |
|
202 |
|
203 #if defined(Q_WS_QWS) |
|
204 QSKIP("task 176755", SkipAll); |
|
205 #endif |
|
206 VERIFY_COLOR(w.geometry(), w.color); |
|
207 QCOMPARE(QRegion(w.rect()), w.r); |
|
208 w.reset(); |
|
209 } |
|
210 |
|
211 |
|
212 void tst_QWindowSurface::grabWidget() |
|
213 { |
|
214 QWidget parentWidget; |
|
215 QWidget childWidget(&parentWidget); |
|
216 QWidget babyWidget(&childWidget); |
|
217 |
|
218 parentWidget.resize(300, 300); |
|
219 childWidget.setGeometry(50, 50, 200, 200); |
|
220 childWidget.setAutoFillBackground(true); |
|
221 babyWidget.setGeometry(50, 50, 100, 100); |
|
222 babyWidget.setAutoFillBackground(true); |
|
223 |
|
224 QPalette pal = parentWidget.palette(); |
|
225 pal.setColor(QPalette::Window, QColor(Qt::blue)); |
|
226 parentWidget.setPalette(pal); |
|
227 |
|
228 pal = childWidget.palette(); |
|
229 pal.setColor(QPalette::Window, QColor(Qt::white)); |
|
230 childWidget.setPalette(pal); |
|
231 |
|
232 pal = babyWidget.palette(); |
|
233 pal.setColor(QPalette::Window, QColor(Qt::red)); |
|
234 babyWidget.setPalette(pal); |
|
235 |
|
236 // prevent custom styles from messing up the background |
|
237 parentWidget.setStyle(new QWindowsStyle); |
|
238 |
|
239 babyWidget.show(); |
|
240 childWidget.show(); |
|
241 parentWidget.show(); |
|
242 QTest::qWaitForWindowShown(&parentWidget); |
|
243 |
|
244 QPixmap parentPixmap; |
|
245 QTRY_COMPARE((parentPixmap = parentWidget.windowSurface()->grabWidget(&parentWidget)).size(), |
|
246 QSize(300,300)); |
|
247 QPixmap childPixmap = childWidget.windowSurface()->grabWidget(&childWidget); |
|
248 QPixmap babyPixmap = babyWidget.windowSurface()->grabWidget(&babyWidget); |
|
249 QPixmap parentSubPixmap = parentWidget.windowSurface()->grabWidget(&parentWidget, QRect(25, 25, 100, 100)); |
|
250 QPixmap childSubPixmap = childWidget.windowSurface()->grabWidget(&childWidget, QRect(55, 55, 50, 50)); |
|
251 QPixmap childInvalidSubPixmap = childWidget.windowSurface()->grabWidget(&childWidget, QRect(-50, -50, 150, 150)); |
|
252 |
|
253 QCOMPARE(parentPixmap.size(), QSize(300, 300)); |
|
254 QCOMPARE(childPixmap.size(), QSize(200, 200)); |
|
255 QCOMPARE(babyPixmap.size(), QSize(100, 100)); |
|
256 QCOMPARE(parentSubPixmap.size(), QSize(100, 100)); |
|
257 QCOMPARE(childSubPixmap.size(), QSize(50, 50)); |
|
258 QCOMPARE(childInvalidSubPixmap.size(), QSize(100, 100)); |
|
259 |
|
260 QImage parentImage = parentPixmap.toImage(); |
|
261 QImage childImage = childPixmap.toImage(); |
|
262 QImage babyImage = babyPixmap.toImage(); |
|
263 QImage parentSubImage = parentSubPixmap.toImage(); |
|
264 QImage childSubImage = childSubPixmap.toImage(); |
|
265 QImage childInvalidSubImage = childInvalidSubPixmap.toImage(); |
|
266 |
|
267 QVERIFY(QColor(parentImage.pixel(0, 0)) == QColor(Qt::blue)); |
|
268 QVERIFY(QColor(parentImage.pixel(75, 75)) == QColor(Qt::white)); |
|
269 QVERIFY(QColor(parentImage.pixel(149, 149)) == QColor(Qt::red)); |
|
270 |
|
271 QVERIFY(QColor(childImage.pixel(0, 0)) == QColor(Qt::white)); |
|
272 QVERIFY(QColor(childImage.pixel(99, 99)) == QColor(Qt::red)); |
|
273 |
|
274 QVERIFY(QColor(parentSubImage.pixel(0, 0)) == QColor(Qt::blue)); |
|
275 QVERIFY(QColor(parentSubImage.pixel(30, 30)) == QColor(Qt::white)); |
|
276 QVERIFY(QColor(parentSubImage.pixel(80, 80)) == QColor(Qt::red)); |
|
277 |
|
278 QVERIFY(QColor(childSubImage.pixel(0, 0)) == QColor(Qt::red)); |
|
279 |
|
280 QVERIFY(QColor(childInvalidSubImage.pixel(0, 0)) == QColor(Qt::white)); |
|
281 } |
|
282 |
|
283 QTEST_MAIN(tst_QWindowSurface) |
|
284 |
|
285 #else // Q_WS_MAC |
|
286 |
|
287 QTEST_NOOP_MAIN |
|
288 |
|
289 #endif |
|
290 |
|
291 #include "tst_qwindowsurface.moc" |