tests/auto/qsvgdevice/tst_qsvgdevice.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 #if 0
       
    46 #include <private/qpaintengine_svg_p.h>
       
    47 #endif
       
    48 
       
    49 #include <qapplication.h>
       
    50 #include <qpainter.h>
       
    51 #include <qbuffer.h>
       
    52 #include <qimage.h>
       
    53 #include <qpicture.h>
       
    54 #include <qdrawutil.h>
       
    55 #include <qpaintdevice.h>
       
    56 
       
    57 
       
    58 
       
    59 //TESTED_CLASS=
       
    60 //TESTED_FILES= xml/qsvgdevice.cpp
       
    61 
       
    62 class tst_QSvgDevice : public QObject
       
    63 {
       
    64     Q_OBJECT
       
    65 public:
       
    66     tst_QSvgDevice();
       
    67 
       
    68 private slots:
       
    69     void play_data();
       
    70     void play();
       
    71     void boundingRect();
       
    72 
       
    73 private:
       
    74     void playPaint( QPainter *p, const QString &type );
       
    75 };
       
    76 
       
    77 tst_QSvgDevice::tst_QSvgDevice()
       
    78 {
       
    79 }
       
    80 
       
    81 void tst_QSvgDevice::play_data()
       
    82 {
       
    83     // we only use the tag name
       
    84     QTest::newRow( "lines" );
       
    85     QTest::newRow( "font" );
       
    86     QTest::newRow( "polyline" );
       
    87     QTest::newRow( "translate" );
       
    88     QTest::newRow( "scaleRect" );
       
    89     QTest::newRow( "ellipseOdd" );
       
    90     QTest::newRow( "ellipseEven" );
       
    91     QTest::newRow( "ellipseRandom" );
       
    92     QTest::newRow( "scaleText" );
       
    93     QTest::newRow( "scaleTextWithFont" );
       
    94     QTest::newRow( "scaleTextSaveRestore" );
       
    95     QTest::newRow( "scaleLineWithPen" );
       
    96     QTest::newRow( "task-17637" );
       
    97     QTest::newRow( "dashed-lines" );
       
    98     QTest::newRow( "dot-lines" );
       
    99     QTest::newRow( "dashed-dot-lines" );
       
   100     QTest::newRow( "dashed-dot-dot-lines" );
       
   101     QTest::newRow( "scaleDashed-lines" );
       
   102     QTest::newRow( "thick-dashed-lines" );
       
   103     QTest::newRow( "negative-rect" );
       
   104     QTest::newRow( "lightText" );
       
   105     QTest::newRow( "boldText" );
       
   106     QTest::newRow( "demiBoldText" );
       
   107     QTest::newRow( "blackText" );
       
   108     QTest::newRow( "task-20239" );
       
   109     QTest::newRow( "clipRect" );
       
   110     QTest::newRow( "multipleClipRects" );
       
   111     QTest::newRow("qsimplerichtext");
       
   112 }
       
   113 
       
   114 #if 0
       
   115 
       
   116 class SVGDummyDevice : public QPaintDevice
       
   117 {
       
   118 public:
       
   119     SVGDummyDevice()
       
   120 	: QPaintDevice(QInternal::ExternalDevice), eng(0) { }
       
   121     ~SVGDummyDevice() {
       
   122 	delete eng;
       
   123     }
       
   124     QPaintEngine *engine() const {
       
   125 	if (!eng)
       
   126 	    const_cast<SVGDummyDevice *>(this)->eng = new QSVGPaintEngine;
       
   127 	return eng;
       
   128     }
       
   129 
       
   130 private:
       
   131     QPaintEngine *eng;
       
   132 };
       
   133 
       
   134 void tst_QSvgDevice::play()
       
   135 {
       
   136     // current tag name
       
   137     QString type = data()->dataTag();
       
   138 
       
   139     // reference pixmap
       
   140     QPixmap ref( 100, 100 );
       
   141     ref.fill( Qt::white );
       
   142     QPainter pref( &ref );
       
   143     playPaint( &pref, type );
       
   144     pref.end();
       
   145 
       
   146     // draw the same into the SVG device
       
   147     SVGDummyDevice dev;
       
   148     QPainter pdev( &dev );
       
   149     playPaint( &pdev, type );
       
   150     pdev.end();
       
   151 
       
   152 
       
   153     //dev.setBoundingRect( QRect( 0, 0, 100, 100 ) );
       
   154     //dev.save( type + "-res.svg" ); // ### sets bounding rect to 0 !
       
   155 
       
   156     // replay on a result pixmap and compare
       
   157     QPixmap res( 100, 100 );
       
   158     res.fill( Qt::white );
       
   159     QPainter pres( &res );
       
   160     static_cast<QSVGPaintEngine *>(dev.engine())->play( &pres );
       
   161 
       
   162 #if 0
       
   163     // for visual inspection
       
   164     ref.save( type + "-ref.xpm", "XPM" );
       
   165     res.save( type + "-res.xpm", "XPM" );
       
   166 #endif
       
   167 
       
   168     QVERIFY( res.convertToImage() == ref.convertToImage() );
       
   169 }
       
   170 
       
   171 // helper function for play()
       
   172 void tst_QSvgDevice::playPaint( QPainter *p, const QString &type )
       
   173 {
       
   174     if ( type == "lines" ) {
       
   175 	// line with pen width 0
       
   176 	p->setPen( QPen( Qt::black, 0, Qt::SolidLine ) );
       
   177 	p->drawLine( 10, 0, 20, 3 );
       
   178 
       
   179 	// line with pen width 1
       
   180 	p->setPen( QPen( Qt::black, 1, Qt::SolidLine ) );
       
   181 	p->drawLine( 2, 0, 10, 3 );
       
   182 
       
   183 	// rect without outline (qt-bugs/arc-17/35556)
       
   184 	p->setPen( Qt::NoPen );
       
   185 	p->setBrush( Qt::red );
       
   186 	p->drawRect( 5, 10, 20, 30 );
       
   187     } else if ( type == "text" ) {
       
   188 	QFont f = p->font();
       
   189 	f.setItalic( TRUE );
       
   190 	f.setBold( TRUE );
       
   191 	p->setFont( f );
       
   192 	p->drawText( 5, 55, "Text" );
       
   193     } else if ( type == "polyline" ) {
       
   194 	// we'll draw 4 triangular polylines. Only two will show up
       
   195 	// as the QPainter::drawPolyline() doesn't respect the brush
       
   196 	// just the pen setting
       
   197 	QPolygon pa( 3 );
       
   198 	pa.setPoint( 0, 0, 0 );
       
   199 	pa.setPoint( 1, 10, 0 );
       
   200 	pa.setPoint( 2, 0, 10 );
       
   201 
       
   202 	// frame around the following 4 polylines
       
   203 	p->setBrush( Qt::NoBrush );
       
   204 	p->setPen( Qt::SolidLine );
       
   205 	p->drawRect( 46, 3, 19, 60 );
       
   206 
       
   207 	// polyline with blue brush, no pen
       
   208 	p->setPen( Qt::NoPen );
       
   209 	p->setBrush( Qt::blue );
       
   210 	p->translate( 50, 5 );
       
   211 	p->drawPolyline( pa );
       
   212 
       
   213 	// polyline without brush, no pen
       
   214 	p->setBrush( Qt::NoBrush );
       
   215 	p->translate( 0, 15 );
       
   216 	p->drawPolyline( pa );
       
   217 
       
   218 	// polyline with green brush, solid pen
       
   219 	p->setBrush( Qt::green );
       
   220 	p->setPen( Qt::SolidLine );
       
   221 	p->translate( 0, 15 );
       
   222 	p->drawPolyline( pa );
       
   223 
       
   224 	// polyline without brush, solid pen
       
   225 	p->setBrush( Qt::NoBrush );
       
   226 	p->setPen( Qt::SolidLine );
       
   227 	p->translate( 0, 15 );
       
   228 	p->drawPolyline( pa );
       
   229     } else if ( type == "translate" ) {
       
   230 	p->translate(-10,-10);
       
   231 	p->save();
       
   232 	p->setBrush( Qt::blue );
       
   233 	p->drawRect( 20, 30, 50, 20 );
       
   234 	p->restore();
       
   235 	p->setBrush( Qt::green );
       
   236 	p->drawRect( 70, 50, 10, 10 );
       
   237     } else if ( type == "scaleRect" ) {
       
   238 	p->scale( 1, 2 );
       
   239 	p->setBrush( Qt::blue );
       
   240 	p->drawRect( 20, 20, 60, 60 );
       
   241     } else if ( type == "ellipseEven" ) {
       
   242 	p->setBrush( Qt::blue );
       
   243 	p->drawEllipse( 20, 20, 60, 60 );
       
   244     } else if ( type == "ellipseOdd" ) {
       
   245 	p->setBrush( Qt::blue );
       
   246 	p->drawEllipse( 20, 20, 59, 59 );
       
   247     } else if ( type == "ellipseRandom" ) {
       
   248 	p->setBrush( Qt::blue );
       
   249 	p->drawEllipse( 20, 34, 89, 123 );
       
   250     } else if ( type == "scaleText" ) {
       
   251 	p->scale(0.25,0.25);
       
   252 	p->drawText(200,200,"Hello!");
       
   253     } else if ( type == "scaleTextWithFont" ) {
       
   254 	p->setFont(QFont("Helvetica",12));
       
   255 	p->scale(0.25,0.25);
       
   256 	p->drawText(200,200,"Hello!");
       
   257 #ifdef Q_WS_WIN
       
   258     // Only test on Windows for now, visually it looks fine, but
       
   259     // it's failing for some reason on Linux
       
   260     } else if ( type == "scaleTextSaveRestore" ) {
       
   261         p->scale(1,-1);
       
   262 	p->save();
       
   263 	p->setFont(QFont("Helvetica",12));
       
   264 	p->scale(0.5,0.5);
       
   265 	p->drawText(0,0,"Hello!");
       
   266 	p->restore();
       
   267 #endif
       
   268     } else if ( type == "scaleLineWithPen" ) {
       
   269         p->scale(0.1,0.1);
       
   270 	p->setPen(QPen(Qt::red,100));
       
   271 	p->drawLine(3,3,500,500);
       
   272     } else if ( type == "task-17637" ) {
       
   273 	p->translate(0,200);
       
   274 	p->scale(0.01,-0.01);
       
   275 	p->setBrush(Qt::blue);
       
   276 	p->drawEllipse(2000,2000,6000,6000);
       
   277 	p->setPen(QPen(Qt::red,100));
       
   278 	p->setFont(QFont("Helvetica",12));
       
   279 	p->save();
       
   280 	p->scale(1,-1);
       
   281 	p->drawText(4000,4000,"Hello!");
       
   282 	p->restore();
       
   283 	p->drawLine(3000,3000,5000,5000);
       
   284     } else if ( type == "dashed-lines" ) {
       
   285 	p->setPen(QPen(Qt::red, 1, Qt::DashLine));
       
   286 	p->drawLine(10,10,50,50);
       
   287     } else if ( type == "dot-lines" ) {
       
   288 	p->setPen(QPen(Qt::red, 1, Qt::DotLine));
       
   289 	p->drawLine(10,10,50,50);
       
   290     } else if ( type == "dashed-dot-lines" ) {
       
   291 	p->setPen(QPen(Qt::red, 1, Qt::DashDotLine));
       
   292 	p->drawLine(10,10,50,50);
       
   293     } else if ( type == "dashed-dot-dot-lines" ) {
       
   294 	p->setPen(QPen(Qt::red, 1, Qt::DashDotDotLine));
       
   295 	p->drawLine(10,10,50,50);
       
   296     } else if ( type == "scaleDashed-lines" ) {
       
   297 	p->scale(0.1,0.1);
       
   298 	p->setPen(QPen(Qt::red, 10, Qt::DashLine));
       
   299 	p->drawLine(10,10,500,500);
       
   300     } else if ( type == "thick-dashed-lines" ) {
       
   301 	p->setPen(QPen(Qt::red, 10, Qt::DashLine));
       
   302 	p->drawLine(10,10,50,50);
       
   303     } else if ( type == "negative-rect" ) {
       
   304 	p->drawRect(70, 0,-30,30);
       
   305     } else if ( type == "lightText" ) {
       
   306 	QFont f("Helvetica",12);
       
   307 	f.setWeight( QFont::Light );
       
   308 	p->setFont(f);
       
   309 	p->drawText(0,0,"Hello!");
       
   310     } else if ( type == "boldText" ) {
       
   311 	QFont f("Helvetica",12);
       
   312 	f.setWeight( QFont::Bold );
       
   313 	p->setFont(f);
       
   314 	p->drawText(0,0,"Hello!");
       
   315     } else if ( type == "demiBoldText" ) {
       
   316 	QFont f("Helvetica",12);
       
   317 	f.setWeight( QFont::DemiBold );
       
   318 	p->setFont(f);
       
   319 	p->drawText(0,0,"Hello!");
       
   320     } else if ( type == "blackText" ) {
       
   321 	QFont f("Helvetica",12);
       
   322 	f.setWeight( QFont::Black );
       
   323 	p->setFont(f);
       
   324 	p->drawText(0,0,"Hello!");
       
   325     } else if ( type == "task-20239" ) {
       
   326         p->translate(0,200);
       
   327 	p->scale(0.02,-0.02);
       
   328 	p->scale(1,-1);
       
   329 	for(int y = 1000; y <=10000; y += 2000) {
       
   330 	    QBrush br(Qt::green);
       
   331 	    QPalette palette(Qt::green,Qt::blue);
       
   332 	    qDrawShadePanel(p,4000,5000,4000,2000,palette.active(),false,200,&br);
       
   333 	    qDrawShadeRect(p,2000,2000,4000,2000,palette.active(),true,100,0,&br);
       
   334 	}
       
   335     } else if (type=="clipRect") {
       
   336 	p->setClipRect(15,25,10,10);
       
   337 	p->drawEllipse(10,20,50,70);
       
   338     } else if (type=="multipleClipRects") {
       
   339 	p->setClipRect(15,25,10,10);
       
   340 	p->drawEllipse(10,20,50,70);
       
   341 	p->setClipRect(100,200,20,20);
       
   342 	p->drawEllipse(110,220,50,70);
       
   343     } else if (type == "qsimplerichtext") {
       
   344 	p->setPen(QColor(0,0,0));
       
   345         p->setBrush(Qt::NoBrush);
       
   346         QRect rect1(10, 10, 100, 50);
       
   347         QRect rect2(200, 20, 80, 50);
       
   348         p->drawRect(10, 10, 100, 50);
       
   349         p->drawRect(200, 20, 80, 50);
       
   350 
       
   351         QSimpleRichText text1("Text 1", QApplication::font());
       
   352         QSimpleRichText text2("Text 2", QApplication::font());
       
   353 
       
   354         QColorGroup cg(Qt::black, Qt::red, Qt::gray, Qt::gray, Qt::gray, Qt::black, Qt::blue, Qt::black, Qt::white);
       
   355         text1.draw(p, rect1.x(), rect1.y(), rect1, cg);
       
   356         text2.draw(p, rect2.x(), rect2.y(), rect2, cg);
       
   357     }
       
   358 }
       
   359 
       
   360 void tst_QSvgDevice::boundingRect()
       
   361 {
       
   362     // ### the bounding rect is only calculated/parsed
       
   363     // ### when replaying. Therefore we go through QPicture.
       
   364     QRect r( 10, 20, 30, 40 );
       
   365     // create document
       
   366     QPicture pic;
       
   367     QPainter p( &pic );
       
   368     p.drawRect( r );
       
   369     p.end();
       
   370     pic.save( "tmp.svg", "svg" );
       
   371     // load it
       
   372     QPicture pic2;
       
   373     pic2.load( "tmp.svg", "svg" );
       
   374     QCOMPARE( pic2.boundingRect(), r );
       
   375 }
       
   376 
       
   377 #else
       
   378 
       
   379 void tst_QSvgDevice::play()
       
   380 {
       
   381     QSKIP("This test needs some redoing, this is just a temp measure until I work it out", SkipAll);
       
   382 }
       
   383 
       
   384 void tst_QSvgDevice::boundingRect()
       
   385 {
       
   386     QSKIP("This test needs some redoing, this is just a temp measure until I work it out", SkipAll);
       
   387 }
       
   388 
       
   389 #endif
       
   390 
       
   391 QTEST_MAIN(tst_QSvgDevice)
       
   392 #include "tst_qsvgdevice.moc"