tests/auto/qdirectpainter/tst_qdirectpainter.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     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 
       
    45 #ifdef Q_WS_QWS
       
    46 
       
    47 //TESTED_CLASS=
       
    48 //TESTED_FILES=
       
    49 
       
    50 #include <qwindowsystem_qws.h>
       
    51 #include <qpainter.h>
       
    52 #include <qdesktopwidget.h>
       
    53 #include <qdirectpainter_qws.h>
       
    54 #include <private/qwindowsurface_qws_p.h>
       
    55 #include <private/qdrawhelper_p.h>
       
    56 
       
    57 class tst_QDirectPainter : public QObject
       
    58 {
       
    59     Q_OBJECT
       
    60 
       
    61 public:
       
    62     tst_QDirectPainter() {}
       
    63     ~tst_QDirectPainter() {}
       
    64 
       
    65 private slots:
       
    66     void initTestCase();
       
    67     void setGeometry_data();
       
    68     void setGeometry();
       
    69     void regionSynchronization();
       
    70     void reservedSynchronous();
       
    71 
       
    72 private:
       
    73     QWSWindow* getWindow(int windId);
       
    74     QColor bgColor;
       
    75 };
       
    76 
       
    77 class ColorPainter : public QDirectPainter
       
    78 {
       
    79 public:
       
    80     ColorPainter(SurfaceFlag flag = NonReserved,
       
    81                  const QColor &color = QColor(Qt::red))
       
    82         : QDirectPainter(0, flag), c(color) {}
       
    83 
       
    84     QColor color() { return c; }
       
    85 
       
    86 protected:
       
    87     void regionChanged(const QRegion &region) {
       
    88         QScreen::instance()->solidFill(c, region);
       
    89     }
       
    90 
       
    91 private:
       
    92     QColor c;
       
    93     QRegion r;
       
    94 };
       
    95 
       
    96 Q_DECLARE_METATYPE(QDirectPainter::SurfaceFlag)
       
    97 
       
    98 void tst_QDirectPainter::initTestCase()
       
    99 {
       
   100     bgColor = QColor(Qt::green);
       
   101 
       
   102     QWSServer *server = QWSServer::instance();
       
   103     server->setBackground(bgColor);
       
   104 }
       
   105 
       
   106 QWSWindow* tst_QDirectPainter::getWindow(int winId)
       
   107 {
       
   108     QWSServer *server = QWSServer::instance();
       
   109     foreach (QWSWindow *w, server->clientWindows()) {
       
   110         if (w->winId() == winId)
       
   111             return w;
       
   112     }
       
   113     return 0;
       
   114 }
       
   115 
       
   116 class ColorWidget : public QWidget
       
   117 {
       
   118 public:
       
   119     ColorWidget(QWidget *parent = 0, const QColor &c = QColor(Qt::red))
       
   120         : QWidget(parent, Qt::FramelessWindowHint), color(c)
       
   121     {
       
   122         QPalette opaquePalette = palette();
       
   123         opaquePalette.setColor(backgroundRole(), color);
       
   124         setPalette(opaquePalette);
       
   125         setAutoFillBackground(true);
       
   126     }
       
   127 
       
   128     void paintEvent(QPaintEvent *e) {
       
   129         r += e->region();
       
   130     }
       
   131 
       
   132     void reset() {
       
   133         r = QRegion();
       
   134     }
       
   135 
       
   136     QColor color;
       
   137     QRegion r;
       
   138 };
       
   139 
       
   140 #define VERIFY_COLOR(rect, color) {                                     \
       
   141     const QPixmap pixmap = QPixmap::grabWindow(QDesktopWidget().winId(), \
       
   142                                                rect.left(), rect.top(), \
       
   143                                                rect.width(), rect.height()); \
       
   144     QCOMPARE(pixmap.size(), rect.size());                               \
       
   145     QPixmap expectedPixmap(pixmap); /* ensure equal formats */          \
       
   146     expectedPixmap.fill(color);                                         \
       
   147     QCOMPARE(pixmap, expectedPixmap);                                   \
       
   148 }
       
   149 
       
   150 void tst_QDirectPainter::setGeometry_data()
       
   151 {
       
   152     QTest::addColumn<QDirectPainter::SurfaceFlag>("flag");
       
   153 
       
   154     QTest::newRow("NonReserved") << QDirectPainter::NonReserved;
       
   155     QTest::newRow("Reserved") << QDirectPainter::Reserved;
       
   156     QTest::newRow("ReservedSynchronous") << QDirectPainter::ReservedSynchronous;
       
   157 }
       
   158 
       
   159 void tst_QDirectPainter::setGeometry()
       
   160 {
       
   161     QFETCH(QDirectPainter::SurfaceFlag, flag);
       
   162 
       
   163     const QRect rect(100, 100, 100, 100);
       
   164     {
       
   165         ColorPainter w(flag);
       
   166 
       
   167         w.setGeometry(rect);
       
   168         QApplication::processEvents();
       
   169         QCOMPARE(w.geometry(), rect);
       
   170         VERIFY_COLOR(rect, w.color());
       
   171     }
       
   172     QApplication::processEvents();
       
   173     VERIFY_COLOR(rect, bgColor);
       
   174 }
       
   175 
       
   176 void tst_QDirectPainter::regionSynchronization()
       
   177 {
       
   178 #ifdef QT_NO_PROCESS
       
   179     QSKIP("Test requires QProcess", SkipAll);
       
   180 #else
       
   181     QRect dpRect(10, 10, 50, 50);
       
   182 
       
   183     // Start the direct painter in a different process
       
   184     QProcess proc;
       
   185     QStringList args;
       
   186     args << QString::number(dpRect.x())
       
   187          << QString::number(dpRect.y())
       
   188          << QString::number(dpRect.width())
       
   189          << QString::number(dpRect.height());
       
   190 
       
   191     proc.start("runDirectPainter/runDirectPainter", args);
       
   192     QVERIFY(proc.waitForStarted(5 * 1000));
       
   193     QTest::qWait(1000);
       
   194     QApplication::processEvents();
       
   195     VERIFY_COLOR(dpRect, Qt::blue); // blue hardcoded in runDirectPainter
       
   196 
       
   197     QTime t;
       
   198     t.start();
       
   199     static int i = 0;
       
   200     while (t.elapsed() < 10 * 1000) {
       
   201         QApplication::processEvents();
       
   202 
       
   203         ColorWidget w;
       
   204         w.setGeometry(10, 10, 50, 50);
       
   205         const QRect wRect = dpRect.translated(10, 0);
       
   206         w.setGeometry(wRect);
       
   207         w.show();
       
   208 
       
   209         QApplication::processEvents();
       
   210         QApplication::processEvents(); //glib event loop workaround
       
   211         VERIFY_COLOR(wRect, w.color);
       
   212         ++i;
       
   213     }
       
   214     QVERIFY(i > 100); // sanity check
       
   215 
       
   216     proc.kill();
       
   217 #endif
       
   218 }
       
   219 
       
   220 class MyObject : public QObject
       
   221 {
       
   222 public:
       
   223     MyObject(QObject *p = 0) : QObject(p), lastEvent(0) {}
       
   224 
       
   225     bool event(QEvent *e) {
       
   226         lastEvent = e;
       
   227         return true;
       
   228     }
       
   229 
       
   230     QEvent *lastEvent;
       
   231 };
       
   232 
       
   233 void tst_QDirectPainter::reservedSynchronous()
       
   234 {
       
   235     MyObject o;
       
   236     QCoreApplication::postEvent(&o, new QEvent(QEvent::None));
       
   237     QDirectPainter p(0, QDirectPainter::ReservedSynchronous);
       
   238     p.setRegion(QRect(5, 5, 50, 50));
       
   239 
       
   240     // The event loop should not have been executed
       
   241     QVERIFY(o.lastEvent == 0);
       
   242     QCOMPARE(p.allocatedRegion(), QRegion(QRect(5, 5, 50, 50)));
       
   243 }
       
   244 
       
   245 QTEST_MAIN(tst_QDirectPainter)
       
   246 
       
   247 #include "tst_qdirectpainter.moc"
       
   248 
       
   249 #else // Q_WS_QWS
       
   250 QTEST_NOOP_MAIN
       
   251 #endif