tests/auto/qpicture/tst_qpicture.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 #include <qpicture.h>
       
    46 #include <qpainter.h>
       
    47 #include <qimage.h>
       
    48 #include <qdesktopwidget.h>
       
    49 #include <qapplication.h>
       
    50 #include <limits.h>
       
    51 
       
    52 //TESTED_CLASS=
       
    53 //TESTED_FILES=
       
    54 
       
    55 class tst_QPicture : public QObject
       
    56 {
       
    57     Q_OBJECT
       
    58 
       
    59 public:
       
    60     tst_QPicture();
       
    61 
       
    62 private slots:
       
    63     void getSetCheck();
       
    64     void devType();
       
    65     void paintingActive();
       
    66     void boundingRect();
       
    67     void operator_lt_lt();
       
    68 
       
    69     void save_restore();
       
    70 
       
    71     void boundaryValues_data();
       
    72     void boundaryValues();
       
    73 };
       
    74 
       
    75 // Testing get/set functions
       
    76 void tst_QPicture::getSetCheck()
       
    77 {
       
    78     QPictureIO obj1;
       
    79     // const QPicture & QPictureIO::picture()
       
    80     // void QPictureIO::setPicture(const QPicture &)
       
    81     // const char * QPictureIO::format()
       
    82     // void QPictureIO::setFormat(const char *)
       
    83     char *var2 = "PNG";
       
    84     obj1.setFormat(var2);
       
    85     QCOMPARE(var2, obj1.format());
       
    86     obj1.setFormat((char *)0);
       
    87     // The format is stored internally in a QString, so return is always a valid char *
       
    88     QVERIFY(QString(obj1.format()).isEmpty());
       
    89 
       
    90     // const char * QPictureIO::parameters()
       
    91     // void QPictureIO::setParameters(const char *)
       
    92     char *var3 = "Bogus data";
       
    93     obj1.setParameters(var3);
       
    94     QCOMPARE(var3, obj1.parameters());
       
    95     obj1.setParameters((char *)0);
       
    96     // The format is stored internally in a QString, so return is always a valid char *
       
    97     QVERIFY(QString(obj1.parameters()).isEmpty());
       
    98 }
       
    99 
       
   100 tst_QPicture::tst_QPicture()
       
   101 {
       
   102 }
       
   103 
       
   104 void tst_QPicture::devType()
       
   105 {
       
   106     QPicture p;
       
   107     QCOMPARE( p.devType(), (int)QInternal::Picture );
       
   108 }
       
   109 
       
   110 void tst_QPicture::paintingActive()
       
   111 {
       
   112     // actually implemented in QPainter but QPicture is a good
       
   113     // example of an external paint device
       
   114     QPicture p;
       
   115     QVERIFY( !p.paintingActive() );
       
   116     QPainter pt( &p );
       
   117     QVERIFY( p.paintingActive() );
       
   118     pt.end();
       
   119     QVERIFY( !p.paintingActive() );
       
   120 }
       
   121 
       
   122 void tst_QPicture::boundingRect()
       
   123 {
       
   124     QPicture p1;
       
   125     // default value
       
   126     QVERIFY( !p1.boundingRect().isValid() );
       
   127 
       
   128     QRect r1( 20, 30, 5, 15 );
       
   129     p1.setBoundingRect( r1 );
       
   130     QCOMPARE( p1.boundingRect(), r1 );
       
   131     p1.setBoundingRect(QRect());
       
   132 
       
   133     QPainter pt( &p1 );
       
   134     pt.drawLine( 10, 20, 110, 80 );
       
   135     pt.end();
       
   136 
       
   137     // assignment and copy constructor
       
   138     QRect r2( 10, 20, 100, 60 );
       
   139     QCOMPARE( p1.boundingRect(), r2 );
       
   140     QPicture p2( p1 );
       
   141     QCOMPARE( p1.boundingRect(), r2 );
       
   142     QPicture p3;
       
   143     p3 = p1;
       
   144     QCOMPARE( p1.boundingRect(), r2 );
       
   145 
       
   146     {
       
   147         QPicture p4;
       
   148         QPainter p(&p4);
       
   149         p.drawLine(0, 0, 5, 0);
       
   150         p.drawLine(0, 0, 0, 5);
       
   151         p.end();
       
   152 
       
   153         QRect r3(0, 0, 5, 5);
       
   154         QCOMPARE(p4.boundingRect(), r3);
       
   155     }
       
   156 }
       
   157 
       
   158 // operator<< and operator>>
       
   159 void tst_QPicture::operator_lt_lt()
       
   160 {
       
   161     // streaming of null pictures
       
   162     {
       
   163 	QPicture pic1, pic2;
       
   164 	QByteArray ba( 100, 0 );
       
   165 	QDataStream str1( &ba, QIODevice::WriteOnly );
       
   166 	str1 << pic1;
       
   167 	QDataStream str2( &ba, QIODevice::ReadOnly );
       
   168 	str2 >> pic2;
       
   169  	QVERIFY( pic2.isNull() );
       
   170     }
       
   171 
       
   172     // picture with a simple line, checking bitwise equality
       
   173     {
       
   174 	QPicture pic1, pic2;
       
   175 	QPainter p( &pic1 );
       
   176 	p.drawLine( 10, 20, 30, 40 );
       
   177 	p.end();
       
   178 	QByteArray ba( 10 * pic1.size(), 0 );
       
   179 	QDataStream str1( &ba, QIODevice::WriteOnly );
       
   180 	str1 << pic1;
       
   181 	QDataStream str2( &ba, QIODevice::ReadOnly );
       
   182 	str2 >> pic2;
       
   183 	QCOMPARE( pic1.size(), pic2.size() );
       
   184 	QVERIFY( memcmp( pic1.data(), pic2.data(), pic1.size() ) == 0 );
       
   185     }
       
   186 }
       
   187 
       
   188 static QPointF scalePoint(const QPointF &point, QPaintDevice *sourceDevice, QPaintDevice *destDevice)
       
   189 {
       
   190     return QPointF(point.x() * qreal(destDevice->logicalDpiX()) / qreal(sourceDevice->logicalDpiX()),
       
   191                    point.y() * qreal(destDevice->logicalDpiY()) / qreal(sourceDevice->logicalDpiY()));
       
   192 }
       
   193 
       
   194 static QRectF scaleRect(const QRectF &rect, QPaintDevice *sourceDevice, QPaintDevice *destDevice)
       
   195 {
       
   196     return QRectF(rect.left() * qreal(destDevice->logicalDpiX()) / qreal(sourceDevice->logicalDpiX()),
       
   197                   rect.top() * qreal(destDevice->logicalDpiY()) / qreal(sourceDevice->logicalDpiY()),
       
   198                   rect.width() * qreal(destDevice->logicalDpiX()) / qreal(sourceDevice->logicalDpiX()),
       
   199                   rect.height() * qreal(destDevice->logicalDpiY()) / qreal(sourceDevice->logicalDpiY()));
       
   200 }
       
   201 
       
   202 static void paintStuff(QPainter *p)
       
   203 {
       
   204     QPaintDevice *screenDevice = QApplication::desktop();
       
   205     p->drawRect(scaleRect(QRectF(100, 100, 100, 100), screenDevice, p->device()));
       
   206     p->save();
       
   207     p->translate(scalePoint(QPointF(10, 10), screenDevice, p->device()));
       
   208     p->restore();
       
   209     p->drawRect(scaleRect(QRectF(100, 100, 100, 100), screenDevice, p->device()));
       
   210 }
       
   211 
       
   212 /* See task: 41469
       
   213    Problem is that the state is not properly restored if the basestate of
       
   214    the painter is different when the picture data was created compared to
       
   215    the base state of the painter when it is played back.
       
   216  */
       
   217 void tst_QPicture::save_restore()
       
   218 {
       
   219     QPicture pic;
       
   220     QPainter p;
       
   221     p.begin(&pic);
       
   222     paintStuff(&p);
       
   223     p.end();
       
   224 
       
   225     QPixmap pix1(300, 300);
       
   226     pix1.fill(Qt::white);
       
   227     p.begin(&pix1);
       
   228     p.drawPicture(50, 50, pic);
       
   229     p.end();
       
   230 
       
   231     QPixmap pix2(300, 300);
       
   232     pix2.fill(Qt::white);
       
   233     p.begin(&pix2);
       
   234     p.translate(50, 50);
       
   235     paintStuff(&p);
       
   236     p.end();
       
   237 
       
   238     QVERIFY( pix1.toImage() == pix2.toImage() );
       
   239 }
       
   240 
       
   241 void tst_QPicture::boundaryValues_data()
       
   242 {
       
   243     QTest::addColumn<int>("x");
       
   244     QTest::addColumn<int>("y");
       
   245     QTest::newRow("max x") << INT_MAX << 50;
       
   246     QTest::newRow("max y") << 50 << INT_MAX;
       
   247     QTest::newRow("max x and y") << INT_MAX << INT_MAX;
       
   248 
       
   249     QTest::newRow("min x") << INT_MIN << 50;
       
   250     QTest::newRow("min y") << 50 << INT_MIN;
       
   251     QTest::newRow("min x and y") << INT_MIN << INT_MIN;
       
   252 
       
   253     QTest::newRow("min x, max y") << INT_MIN << INT_MAX;
       
   254     QTest::newRow("max x, min y") << INT_MAX << INT_MIN;
       
   255 
       
   256 }
       
   257 
       
   258 void tst_QPicture::boundaryValues()
       
   259 {
       
   260     QPicture picture;
       
   261 
       
   262     QPainter painter;
       
   263     painter.begin(&picture);
       
   264 
       
   265     QFETCH(int, x);
       
   266     QFETCH(int, y);
       
   267     painter.drawPoint(QPoint(x, y));
       
   268 
       
   269     painter.end();
       
   270 
       
   271     
       
   272 }
       
   273 
       
   274 
       
   275 QTEST_MAIN(tst_QPicture)
       
   276 #include "tst_qpicture.moc"