tests/auto/qstatictext/tst_qstatictext.cpp
changeset 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 #include <QtTest/QtTest>
       
    43 #include <QtGui/QApplication>
       
    44 #include <QtGui/QPainter>
       
    45 #include <QtGui/QImage>
       
    46 
       
    47 #include <qstatictext.h>
       
    48 #include <qpaintengine.h>
       
    49 
       
    50 #include <private/qstatictext_p.h>
       
    51 #include <private/qapplication_p.h>
       
    52 
       
    53 // #define DEBUG_SAVE_IMAGE
       
    54 
       
    55 class tst_QStaticText: public QObject
       
    56 {
       
    57     Q_OBJECT
       
    58 
       
    59 private:
       
    60     bool supportsTransformations() const;
       
    61 
       
    62 private slots:
       
    63     void init();
       
    64     void cleanup();
       
    65 
       
    66     void constructionAndDestruction();
       
    67     void drawToPoint_data();
       
    68     void drawToPoint();
       
    69     void drawToRect_data();
       
    70     void drawToRect();
       
    71     void setFont();
       
    72     void setTextWidth();
       
    73     void prepareToCorrectData();
       
    74     void prepareToWrongData();
       
    75 
       
    76     void translatedPainter();
       
    77     void rotatedPainter();
       
    78     void scaledPainter();
       
    79     void projectedPainter();
       
    80     void rotatedScaledAndTranslatedPainter();
       
    81     void transformationChanged();    
       
    82 
       
    83     void plainTextVsRichText();
       
    84 
       
    85     void setPenPlainText();
       
    86     void setPenRichText();
       
    87     void richTextOverridesPen();
       
    88 };
       
    89 
       
    90 void tst_QStaticText::init()
       
    91 {
       
    92 }
       
    93 
       
    94 void tst_QStaticText::cleanup()
       
    95 {
       
    96 }
       
    97 
       
    98 void tst_QStaticText::constructionAndDestruction()
       
    99 {
       
   100     QStaticText text("My text");
       
   101 }
       
   102 
       
   103 Q_DECLARE_METATYPE(QStaticText::PerformanceHint)
       
   104 void tst_QStaticText::drawToPoint_data()
       
   105 {
       
   106     QTest::addColumn<QStaticText::PerformanceHint>("performanceHint");
       
   107 
       
   108     QTest::newRow("Moderate caching") << QStaticText::ModerateCaching;
       
   109     QTest::newRow("Aggressive caching") << QStaticText::AggressiveCaching;
       
   110 }
       
   111 
       
   112 void tst_QStaticText::drawToPoint()
       
   113 {
       
   114     QFETCH(QStaticText::PerformanceHint, performanceHint);
       
   115 
       
   116     QPixmap imageDrawText(1000, 1000);
       
   117     imageDrawText.fill(Qt::white);
       
   118     {
       
   119         QPainter p(&imageDrawText);
       
   120         p.drawText(11, 12, "Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   121     }
       
   122 
       
   123     QPixmap imageDrawStaticText(1000, 1000);
       
   124     imageDrawStaticText.fill(Qt::white);
       
   125     {
       
   126         QPainter p(&imageDrawStaticText);
       
   127         QStaticText text("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   128         text.setTextFormat(Qt::PlainText);
       
   129         text.setPerformanceHint(performanceHint);
       
   130         p.drawStaticText(QPointF(11, 12 - QFontMetricsF(p.font()).ascent()), text);
       
   131     }
       
   132 
       
   133     QCOMPARE(imageDrawStaticText, imageDrawText);
       
   134 }
       
   135 
       
   136 void tst_QStaticText::drawToRect_data()
       
   137 {
       
   138     QTest::addColumn<QStaticText::PerformanceHint>("performanceHint");
       
   139 
       
   140     QTest::newRow("Moderate caching") << QStaticText::ModerateCaching;
       
   141     QTest::newRow("Aggressive caching") << QStaticText::AggressiveCaching;
       
   142 }
       
   143 
       
   144 void tst_QStaticText::drawToRect()
       
   145 {
       
   146     QFETCH(QStaticText::PerformanceHint, performanceHint);
       
   147 
       
   148     QPixmap imageDrawText(1000, 1000);
       
   149     imageDrawText.fill(Qt::white);
       
   150     {
       
   151         QPainter p(&imageDrawText);
       
   152         p.drawText(QRectF(11, 12, 10, 500), "Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   153     }
       
   154 
       
   155     QPixmap imageDrawStaticText(1000, 1000);
       
   156     imageDrawStaticText.fill(Qt::white);
       
   157     {
       
   158         QPainter p(&imageDrawStaticText);
       
   159         QStaticText text("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   160         text.setTextWidth(10),
       
   161         p.setClipRect(QRectF(11, 12, 10, 500));
       
   162         text.setPerformanceHint(performanceHint);
       
   163         text.setTextFormat(Qt::PlainText);
       
   164         p.drawStaticText(QPointF(11, 12), text);
       
   165     }
       
   166 
       
   167 #if defined(DEBUG_SAVE_IMAGE)
       
   168     imageDrawText.save("drawToRect_imageDrawText.png");
       
   169     imageDrawStaticText.save("drawToRect_imageDrawStaticText.png");
       
   170 #endif
       
   171 
       
   172     QCOMPARE(imageDrawStaticText, imageDrawText);   
       
   173 }
       
   174 
       
   175 void tst_QStaticText::prepareToCorrectData()
       
   176 {
       
   177     QTransform transform;
       
   178     transform.scale(2.0, 2.0);
       
   179     transform.rotate(90, Qt::ZAxis);
       
   180 
       
   181     QPixmap imageDrawText(1000, 1000);
       
   182     imageDrawText.fill(Qt::white);
       
   183     {
       
   184         QPainter p(&imageDrawText);
       
   185         p.setTransform(transform);
       
   186         p.drawText(11, 12, "Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   187     }
       
   188 
       
   189     QPixmap imageDrawStaticText(1000, 1000);
       
   190     imageDrawStaticText.fill(Qt::white);
       
   191     {
       
   192         QPainter p(&imageDrawStaticText);
       
   193         p.setTransform(transform);
       
   194         QStaticText text("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   195         text.prepare(transform, p.font());
       
   196         text.setTextFormat(Qt::PlainText);
       
   197         p.drawStaticText(QPointF(11, 12  - QFontMetricsF(p.font()).ascent()), text);
       
   198     }
       
   199 
       
   200     if (!supportsTransformations())
       
   201       QEXPECT_FAIL("", "Graphics system does not support transformed text on this platform", Abort);
       
   202     QCOMPARE(imageDrawStaticText, imageDrawText);
       
   203 }
       
   204 
       
   205 void tst_QStaticText::prepareToWrongData()
       
   206 {
       
   207     QTransform transform;
       
   208     transform.scale(2.0, 2.0);
       
   209     transform.rotate(90, Qt::ZAxis);
       
   210 
       
   211     QPixmap imageDrawText(1000, 1000);
       
   212     imageDrawText.fill(Qt::white);
       
   213     {
       
   214         QPainter p(&imageDrawText);
       
   215         p.drawText(11, 12, "Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   216     }
       
   217 
       
   218     QPixmap imageDrawStaticText(1000, 1000);
       
   219     imageDrawStaticText.fill(Qt::white);
       
   220     {
       
   221         QPainter p(&imageDrawStaticText);
       
   222         QStaticText text("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   223         text.prepare(transform, p.font());
       
   224         text.setTextFormat(Qt::PlainText);
       
   225         p.drawStaticText(QPointF(11, 12  - QFontMetricsF(p.font()).ascent()), text);
       
   226     }
       
   227 
       
   228     QCOMPARE(imageDrawStaticText, imageDrawText);
       
   229 }
       
   230 
       
   231 
       
   232 void tst_QStaticText::setFont()
       
   233 {
       
   234     QFont font = QApplication::font();
       
   235     font.setBold(true);
       
   236     font.setPointSize(28);
       
   237 
       
   238     QPixmap imageDrawText(1000, 1000);
       
   239     imageDrawText.fill(Qt::white);
       
   240     {
       
   241         QPainter p(&imageDrawText);
       
   242         p.drawText(QRectF(0, 0, 1000, 1000), 0, "Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   243 
       
   244         p.setFont(font);
       
   245         p.drawText(QRectF(11, 120, 1000, 1000), 0, "Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   246     }
       
   247 
       
   248     QPixmap imageDrawStaticText(1000, 1000);
       
   249     imageDrawStaticText.fill(Qt::white);
       
   250     {
       
   251         QPainter p(&imageDrawStaticText);
       
   252 
       
   253         QStaticText text;
       
   254         text.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   255         text.setTextFormat(Qt::PlainText);
       
   256 
       
   257         p.drawStaticText(0, 0, text);
       
   258 
       
   259         p.setFont(font);
       
   260         p.drawStaticText(11, 120, text);
       
   261     }
       
   262 
       
   263 #if defined(DEBUG_SAVE_IMAGE)
       
   264     imageDrawText.save("setFont_imageDrawText.png");
       
   265     imageDrawStaticText.save("setFont_imageDrawStaticText.png");
       
   266 #endif
       
   267 
       
   268     QCOMPARE(imageDrawStaticText, imageDrawText);
       
   269 }
       
   270 
       
   271 void tst_QStaticText::setTextWidth()
       
   272 {
       
   273     QPixmap imageDrawText(1000, 1000);
       
   274     imageDrawText.fill(Qt::white);
       
   275     {
       
   276         QPainter p(&imageDrawText);
       
   277         p.drawText(QRectF(11, 12, 10, 500), "Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   278     }
       
   279 
       
   280     QPixmap imageDrawStaticText(1000, 1000);
       
   281     imageDrawStaticText.fill(Qt::white);
       
   282     {
       
   283         QPainter p(&imageDrawStaticText);
       
   284         QStaticText text("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   285         text.setTextWidth(10);
       
   286         p.setClipRect(QRectF(11, 12, 10, 500));
       
   287         p.drawStaticText(QPointF(11, 12), text);
       
   288     }
       
   289 
       
   290     QCOMPARE(imageDrawStaticText, imageDrawText);
       
   291 }
       
   292 
       
   293 void tst_QStaticText::translatedPainter()
       
   294 {
       
   295     QPixmap imageDrawText(1000, 1000);
       
   296     imageDrawText.fill(Qt::white);
       
   297     {
       
   298         QPainter p(&imageDrawText);
       
   299         p.translate(100, 200);
       
   300 
       
   301         p.drawText(11, 12, "Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   302     }
       
   303 
       
   304     QPixmap imageDrawStaticText(1000, 1000);
       
   305     imageDrawStaticText.fill(Qt::white);
       
   306     {
       
   307         QPainter p(&imageDrawStaticText);
       
   308         p.translate(100, 200);
       
   309 
       
   310         QStaticText text("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   311         text.setTextFormat(Qt::PlainText);
       
   312 
       
   313         p.drawStaticText(QPointF(11, 12 - QFontMetricsF(p.font()).ascent()), text);
       
   314     }
       
   315 
       
   316     QCOMPARE(imageDrawStaticText, imageDrawText);
       
   317 }
       
   318 
       
   319 bool tst_QStaticText::supportsTransformations() const
       
   320 {
       
   321     QPixmap pm(10, 10);
       
   322     QPainter p(&pm);
       
   323     QPaintEngine *engine = p.paintEngine();
       
   324 
       
   325     QPaintEngine::Type type = engine->type();
       
   326 
       
   327     if (type == QPaintEngine::OpenGL2
       
   328         || type == QPaintEngine::OpenGL
       
   329 #if !defined Q_WS_WIN
       
   330         || type == QPaintEngine::Raster
       
   331 #endif
       
   332         )
       
   333         return false;
       
   334 
       
   335     return true;
       
   336 }
       
   337 
       
   338 void tst_QStaticText::rotatedPainter()
       
   339 {
       
   340     QPixmap imageDrawText(1000, 1000);
       
   341     imageDrawText.fill(Qt::white);
       
   342     {
       
   343         QPainter p(&imageDrawText);
       
   344         p.rotate(30.0);
       
   345         p.drawText(QRectF(0, 0, 1000, 100), 0, "Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   346     }
       
   347 
       
   348     QPixmap imageDrawStaticText(1000, 1000);
       
   349     imageDrawStaticText.fill(Qt::white);
       
   350     {
       
   351         QStaticText text("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   352         text.setTextFormat(Qt::PlainText);
       
   353 
       
   354         QPainter p(&imageDrawStaticText);
       
   355         p.rotate(30.0);
       
   356         p.drawStaticText(QPoint(0, 0), text);
       
   357     }
       
   358 
       
   359 #if defined(DEBUG_SAVE_IMAGE)
       
   360     imageDrawText.save("rotatedPainter_imageDrawText.png");
       
   361     imageDrawStaticText.save("rotatedPainter_imageDrawStaticText.png");
       
   362 #endif
       
   363 
       
   364     if (!supportsTransformations())
       
   365       QEXPECT_FAIL("", "Graphics system does not support transformed text on this platform", Abort);
       
   366     QCOMPARE(imageDrawStaticText, imageDrawText);
       
   367 }
       
   368 
       
   369 void tst_QStaticText::scaledPainter()
       
   370 {
       
   371     QPixmap imageDrawText(1000, 1000);
       
   372     imageDrawText.fill(Qt::white);
       
   373     {
       
   374         QPainter p(&imageDrawText);
       
   375         p.scale(2.0, 0.2);
       
   376 
       
   377         p.drawText(11, 12, "Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   378     }
       
   379 
       
   380     QPixmap imageDrawStaticText(1000, 1000);
       
   381     imageDrawStaticText.fill(Qt::white);
       
   382     {
       
   383         QPainter p(&imageDrawStaticText);
       
   384         p.scale(2.0, 0.2);
       
   385 
       
   386         QStaticText text("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   387         text.setTextFormat(Qt::PlainText);
       
   388 
       
   389         p.drawStaticText(QPointF(11, 12 - QFontMetricsF(p.font()).ascent()), text);
       
   390     }
       
   391 
       
   392     if (!supportsTransformations())
       
   393       QEXPECT_FAIL("", "Graphics system does not support transformed text on this platform", Abort);
       
   394     QCOMPARE(imageDrawStaticText, imageDrawText);
       
   395 }
       
   396 
       
   397 void tst_QStaticText::projectedPainter()
       
   398 {
       
   399     QTransform transform;
       
   400     transform.rotate(90, Qt::XAxis);
       
   401 
       
   402     QPixmap imageDrawText(1000, 1000);
       
   403     imageDrawText.fill(Qt::white);
       
   404     {
       
   405         QPainter p(&imageDrawText);
       
   406         p.setTransform(transform);
       
   407 
       
   408         p.drawText(11, 12, "Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   409     }
       
   410 
       
   411     QPixmap imageDrawStaticText(1000, 1000);
       
   412     imageDrawStaticText.fill(Qt::white);
       
   413     {
       
   414         QPainter p(&imageDrawStaticText);
       
   415         p.setTransform(transform);
       
   416 
       
   417         QStaticText text("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   418         text.setTextFormat(Qt::PlainText);
       
   419 
       
   420         p.drawStaticText(QPointF(11, 12 - QFontMetricsF(p.font()).ascent()), text);
       
   421     }
       
   422 
       
   423     QCOMPARE(imageDrawStaticText, imageDrawText);
       
   424 }
       
   425 
       
   426 void tst_QStaticText::rotatedScaledAndTranslatedPainter()
       
   427 {
       
   428     QPixmap imageDrawText(1000, 1000);
       
   429     imageDrawText.fill(Qt::white);
       
   430     {
       
   431         QPainter p(&imageDrawText);
       
   432         p.rotate(45.0);
       
   433         p.scale(2.0, 2.0);
       
   434         p.translate(100, 200);
       
   435 
       
   436         p.drawText(11, 12, "Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   437     }
       
   438 
       
   439     QPixmap imageDrawStaticText(1000, 1000);
       
   440     imageDrawStaticText.fill(Qt::white);
       
   441     {
       
   442         QPainter p(&imageDrawStaticText);
       
   443         p.rotate(45.0);
       
   444         p.scale(2.0, 2.0);
       
   445         p.translate(100, 200);
       
   446 
       
   447         QStaticText text("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   448         text.setTextFormat(Qt::PlainText);
       
   449 
       
   450         p.drawStaticText(QPointF(11, 12 - QFontMetricsF(p.font()).ascent()), text);
       
   451     }
       
   452 
       
   453 #if defined(DEBUG_SAVE_IMAGE)
       
   454     imageDrawText.save("rotatedScaledAndPainter_imageDrawText.png");
       
   455     imageDrawStaticText.save("rotatedScaledAndPainter_imageDrawStaticText.png");
       
   456 #endif
       
   457 
       
   458     if (!supportsTransformations())
       
   459       QEXPECT_FAIL("", "Graphics system does not support transformed text on this platform", Abort);
       
   460     QCOMPARE(imageDrawStaticText, imageDrawText);
       
   461 }
       
   462 
       
   463 void tst_QStaticText::transformationChanged()
       
   464 {
       
   465     QPixmap imageDrawText(1000, 1000);
       
   466     imageDrawText.fill(Qt::white);
       
   467     {
       
   468         QPainter p(&imageDrawText);
       
   469         p.rotate(33.0);
       
   470         p.scale(0.5, 0.7);
       
   471 
       
   472         p.drawText(QRectF(0, 0, 1000, 1000), 0, "Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   473 
       
   474         p.scale(7.0, 5.0);
       
   475         p.drawText(QRectF(0, 0, 1000, 1000), 0, "Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   476     }
       
   477 
       
   478     QPixmap imageDrawStaticText(1000, 1000);
       
   479     imageDrawStaticText.fill(Qt::white);
       
   480     {
       
   481         QPainter p(&imageDrawStaticText);
       
   482         p.rotate(33.0);
       
   483         p.scale(0.5, 0.7);
       
   484 
       
   485         QStaticText text("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
       
   486         text.setTextFormat(Qt::PlainText);
       
   487 
       
   488         p.drawStaticText(QPointF(0, 0), text);
       
   489 
       
   490         p.scale(7.0, 5.0);
       
   491         p.drawStaticText(QPointF(0, 0), text);
       
   492     }
       
   493 
       
   494 #if defined(DEBUG_SAVE_IMAGE)
       
   495     imageDrawText.save("transformationChanged_imageDrawText.png");
       
   496     imageDrawStaticText.save("transformationChanged_imageDrawStaticText.png");
       
   497 #endif
       
   498 
       
   499     if (!supportsTransformations())
       
   500       QEXPECT_FAIL("", "Graphics system does not support transformed text on this platform", Abort);
       
   501     QCOMPARE(imageDrawStaticText, imageDrawText);
       
   502 }
       
   503 
       
   504 void tst_QStaticText::plainTextVsRichText()
       
   505 {
       
   506     QPixmap imagePlainText(1000, 1000);
       
   507     imagePlainText.fill(Qt::white);
       
   508     {
       
   509         QPainter p(&imagePlainText);
       
   510 
       
   511         QStaticText staticText;
       
   512         staticText.setText("FOObar");
       
   513         staticText.setTextFormat(Qt::PlainText);
       
   514 
       
   515         p.drawStaticText(10, 10, staticText);
       
   516     }
       
   517 
       
   518     QPixmap imageRichText(1000, 1000);
       
   519     imageRichText.fill(Qt::white);
       
   520     {
       
   521         QPainter p(&imageRichText);
       
   522 
       
   523         QStaticText staticText;
       
   524         staticText.setText("<html><body>FOObar</body></html>");
       
   525         staticText.setTextFormat(Qt::RichText);
       
   526 
       
   527         p.drawStaticText(10, 10, staticText);
       
   528     }
       
   529 
       
   530 #if defined(DEBUG_SAVE_IMAGE)
       
   531     imagePlainText.save("plainTextVsRichText_imagePlainText.png");
       
   532     imageRichText.save("plainTextVsRichText_imageRichText.png");
       
   533 #endif
       
   534 
       
   535     QCOMPARE(imagePlainText, imageRichText);
       
   536 }
       
   537 
       
   538 void tst_QStaticText::setPenPlainText()
       
   539 {
       
   540     QFont font = QApplication::font();
       
   541     font.setStyleStrategy(QFont::NoAntialias);
       
   542 
       
   543     QFontMetricsF fm(font);
       
   544     QPixmap image(qCeil(fm.width("XXXXX")), qCeil(fm.height()));
       
   545     image.fill(Qt::white);
       
   546     {
       
   547         QPainter p(&image);
       
   548         p.setFont(font);
       
   549         p.setPen(Qt::green);
       
   550 
       
   551         QStaticText staticText("XXXXX");
       
   552         staticText.setTextFormat(Qt::PlainText);
       
   553         p.drawStaticText(0, fm.ascent(), staticText);
       
   554     }
       
   555 
       
   556     QImage img = image.toImage();
       
   557     for (int x=0; x<img.width(); ++x) {
       
   558         for (int y=0; y<img.height(); ++y) {
       
   559             QRgb pixel = img.pixel(x, y);
       
   560             QVERIFY(pixel == QColor(Qt::white).rgba()
       
   561                     || pixel == QColor(Qt::green).rgba());
       
   562         }
       
   563     }
       
   564 }
       
   565 
       
   566 void tst_QStaticText::setPenRichText()
       
   567 {
       
   568     QFont font = QApplication::font();
       
   569     font.setStyleStrategy(QFont::NoAntialias);
       
   570 
       
   571     QFontMetricsF fm(font);
       
   572     QPixmap image(qCeil(fm.width("XXXXX")), qCeil(fm.height()));
       
   573     image.fill(Qt::white);
       
   574     {
       
   575         QPainter p(&image);
       
   576         p.setFont(font);
       
   577         p.setPen(Qt::green);
       
   578 
       
   579         QStaticText staticText;
       
   580         staticText.setText("<html><body>XXXXX</body></html>");
       
   581         staticText.setTextFormat(Qt::RichText);
       
   582         p.drawStaticText(0, 0, staticText);
       
   583     }
       
   584 
       
   585     QImage img = image.toImage();
       
   586     for (int x=0; x<img.width(); ++x) {
       
   587         for (int y=0; y<img.height(); ++y) {
       
   588             QRgb pixel = img.pixel(x, y);
       
   589             QVERIFY(pixel == QColor(Qt::white).rgba()
       
   590                     || pixel == QColor(Qt::green).rgba());
       
   591         }
       
   592     }
       
   593 }
       
   594 
       
   595 void tst_QStaticText::richTextOverridesPen()
       
   596 {
       
   597     QFont font = QApplication::font();
       
   598     font.setStyleStrategy(QFont::NoAntialias);
       
   599 
       
   600     QFontMetricsF fm(font);
       
   601     QPixmap image(qCeil(fm.width("XXXXX")), qCeil(fm.height()));
       
   602     image.fill(Qt::white);
       
   603     {
       
   604         QPainter p(&image);
       
   605         p.setFont(font);
       
   606         p.setPen(Qt::green);
       
   607 
       
   608         QStaticText staticText;
       
   609         staticText.setText("<html><body><font color=\"#ff0000\">XXXXX</font></body></html>");
       
   610         staticText.setTextFormat(Qt::RichText);
       
   611         p.drawStaticText(0, 0, staticText);
       
   612     }
       
   613 
       
   614     QImage img = image.toImage();
       
   615     for (int x=0; x<img.width(); ++x) {
       
   616         for (int y=0; y<img.height(); ++y) {
       
   617             QRgb pixel = img.pixel(x, y);
       
   618             QVERIFY(pixel == QColor(Qt::white).rgba()
       
   619                     || pixel == QColor(Qt::red).rgba());
       
   620         }
       
   621     }
       
   622 }
       
   623 
       
   624 QTEST_MAIN(tst_QStaticText)
       
   625 #include "tst_qstatictext.moc"