tests/auto/qbrush/tst_qbrush.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 "qbrush.h"
       
    46 #include <QPainter>
       
    47 #include <QBitmap>
       
    48 
       
    49 #include <qdebug.h>
       
    50 
       
    51 //TESTED_CLASS=
       
    52 //TESTED_FILES=
       
    53 
       
    54 class tst_QBrush : public QObject
       
    55 {
       
    56     Q_OBJECT
       
    57 
       
    58 public:
       
    59     tst_QBrush();
       
    60 
       
    61 private slots:
       
    62     void operator_eq_eq();
       
    63     void operator_eq_eq_data();
       
    64 
       
    65     void stream();
       
    66     void stream_data();
       
    67 
       
    68     void badStyles();
       
    69 
       
    70     void testQLinearGradientSetters();
       
    71     void testQRadialGradientSetters();
       
    72     void testQConicalGradientSetters();
       
    73     void testQGradientCopyConstructor();
       
    74 
       
    75     void gradientStops();
       
    76 
       
    77     void textures();
       
    78     
       
    79     void nullBrush();
       
    80     void isOpaque();
       
    81 };
       
    82 
       
    83 Q_DECLARE_METATYPE(QBrush)
       
    84 
       
    85 tst_QBrush::tst_QBrush()
       
    86 {
       
    87 }
       
    88 
       
    89 void tst_QBrush::operator_eq_eq_data()
       
    90 {
       
    91     QTest::addColumn<QBrush>("brush1");
       
    92     QTest::addColumn<QBrush>("brush2");
       
    93     QTest::addColumn<bool>("isEqual");
       
    94 
       
    95     QLinearGradient lg(10, 10, 100, 100);
       
    96     lg.setColorAt(0, Qt::red);
       
    97     lg.setColorAt(0.5, Qt::blue);
       
    98     lg.setColorAt(1, Qt::green);
       
    99 
       
   100     QTest::newRow("black vs black") << QBrush(Qt::black) << QBrush(Qt::black) << true;
       
   101     QTest::newRow("black vs blue") << QBrush(Qt::black) << QBrush(Qt::blue) << false;
       
   102 
       
   103     QTest::newRow("red vs no") << QBrush(Qt::red) << QBrush(Qt::NoBrush) << false;
       
   104     QTest::newRow("no vs no") << QBrush(Qt::NoBrush) << QBrush(Qt::NoBrush) << true;
       
   105 
       
   106     QTest::newRow("lg vs same lg") << QBrush(lg) << QBrush(lg) << true;
       
   107     QTest::newRow("lg vs diff lg") << QBrush(lg) << QBrush(QLinearGradient(QPoint(0, 0), QPoint(1, 1)))
       
   108                                 << false;
       
   109 
       
   110     QTest::newRow("rad vs con") << QBrush(QRadialGradient(0, 0, 0, 0, 0)) << QBrush(QConicalGradient(0, 0, 0)) << false;
       
   111 }
       
   112 
       
   113 void tst_QBrush::operator_eq_eq()
       
   114 {
       
   115     QFETCH(QBrush, brush1);
       
   116     QFETCH(QBrush, brush2);
       
   117     QFETCH(bool, isEqual);
       
   118     QCOMPARE(brush1 == brush2, isEqual);
       
   119 }
       
   120 
       
   121 void tst_QBrush::stream_data()
       
   122 {
       
   123     QTest::addColumn<QBrush>("brush");
       
   124 
       
   125     QLinearGradient lg(10, 10, 100, 100);
       
   126     lg.setColorAt(0, Qt::red);
       
   127     lg.setColorAt(0.5, Qt::blue);
       
   128     lg.setColorAt(1, Qt::green);
       
   129 
       
   130     QTest::newRow("black") << QBrush(Qt::black);
       
   131     QTest::newRow("red") << QBrush(Qt::red);
       
   132     QTest::newRow("no") << QBrush(Qt::NoBrush);
       
   133     QTest::newRow("lg") << QBrush(lg);
       
   134     QTest::newRow("rad") << QBrush(QRadialGradient(0, 0, 0, 0, 0));
       
   135     QTest::newRow("con") << QBrush(QConicalGradient(0, 0, 0));
       
   136 }
       
   137 
       
   138 void tst_QBrush::stream()
       
   139 {
       
   140     QFETCH(QBrush, brush);
       
   141 
       
   142     QByteArray data;
       
   143 
       
   144     {
       
   145         QDataStream stream(&data, QIODevice::WriteOnly);
       
   146         stream << brush;
       
   147     }
       
   148 
       
   149     QBrush cmp;
       
   150     {
       
   151         QDataStream stream(&data, QIODevice::ReadOnly);
       
   152         stream >> cmp;
       
   153     }
       
   154 
       
   155     QCOMPARE(brush.style(), cmp.style());
       
   156     QCOMPARE(brush.color(), cmp.color());
       
   157     QCOMPARE(brush, cmp);
       
   158 }
       
   159 
       
   160 void tst_QBrush::testQLinearGradientSetters()
       
   161 {
       
   162     QLinearGradient lg;
       
   163 
       
   164     QCOMPARE(lg.start(), QPointF(0, 0));
       
   165     QCOMPARE(lg.finalStop(), QPointF(1, 1));
       
   166 
       
   167     lg.setStart(101, 102);
       
   168     QCOMPARE(lg.start(), QPointF(101, 102));
       
   169 
       
   170     lg.setStart(QPointF(201, 202));
       
   171     QCOMPARE(lg.start(), QPointF(201, 202));
       
   172 
       
   173     lg.setFinalStop(103, 104);
       
   174     QCOMPARE(lg.finalStop(), QPointF(103, 104));
       
   175 
       
   176     lg.setFinalStop(QPointF(203, 204));
       
   177     QCOMPARE(lg.finalStop(), QPointF(203, 204));
       
   178 }
       
   179 
       
   180 void tst_QBrush::testQRadialGradientSetters()
       
   181 {
       
   182     QRadialGradient rg;
       
   183 
       
   184     QCOMPARE(rg.radius(), qreal(1.0));
       
   185     QCOMPARE(rg.center(), QPointF(0, 0));
       
   186     QCOMPARE(rg.focalPoint(), QPointF(0, 0));
       
   187 
       
   188     rg.setRadius(100);
       
   189     QCOMPARE(rg.radius(), qreal(100.0));
       
   190 
       
   191     rg.setCenter(101, 102);
       
   192     QCOMPARE(rg.center(), QPointF(101, 102));
       
   193 
       
   194     rg.setCenter(QPointF(201, 202));
       
   195     QCOMPARE(rg.center(), QPointF(201, 202));
       
   196 
       
   197     rg.setFocalPoint(103, 104);
       
   198     QCOMPARE(rg.focalPoint(), QPointF(103, 104));
       
   199 
       
   200     rg.setFocalPoint(QPointF(203, 204));
       
   201     QCOMPARE(rg.focalPoint(), QPointF(203, 204));
       
   202 }
       
   203 
       
   204 void tst_QBrush::testQConicalGradientSetters()
       
   205 {
       
   206     QConicalGradient cg;
       
   207 
       
   208     QCOMPARE(cg.angle(), qreal(0.0));
       
   209     QCOMPARE(cg.center(), QPointF(0, 0));
       
   210 
       
   211     cg.setAngle(100);
       
   212     QCOMPARE(cg.angle(), qreal(100.0));
       
   213 
       
   214     cg.setCenter(102, 103);
       
   215     QCOMPARE(cg.center(), QPointF(102, 103));
       
   216 
       
   217     cg.setCenter(QPointF(202, 203));
       
   218     QCOMPARE(cg.center(), QPointF(202, 203));
       
   219 }
       
   220 
       
   221 void tst_QBrush::testQGradientCopyConstructor()
       
   222 {
       
   223     {
       
   224         QLinearGradient lg1(101, 102, 103, 104);
       
   225 
       
   226         QLinearGradient lg2 = lg1;
       
   227         QCOMPARE(lg1.start(), lg2.start());
       
   228         QCOMPARE(lg1.finalStop(), lg2.finalStop());
       
   229 
       
   230         QGradient g = lg1;
       
   231         QCOMPARE(((QLinearGradient *) &g)->start(), lg1.start());
       
   232         QCOMPARE(((QLinearGradient *) &g)->finalStop(), lg1.finalStop());
       
   233     }
       
   234 
       
   235     {
       
   236         QRadialGradient rg1(101, 102, 103, 104, 105);
       
   237 
       
   238         QRadialGradient rg2 = rg1;
       
   239         QCOMPARE(rg1.center(), rg2.center());
       
   240         QCOMPARE(rg1.focalPoint(), rg2.focalPoint());
       
   241         QCOMPARE(rg1.radius(), rg2.radius());
       
   242 
       
   243         QGradient g = rg1;
       
   244         QCOMPARE(((QRadialGradient *) &g)->center(), rg1.center());
       
   245         QCOMPARE(((QRadialGradient *) &g)->focalPoint(), rg1.focalPoint());
       
   246         QCOMPARE(((QRadialGradient *) &g)->radius(), rg1.radius());
       
   247     }
       
   248 
       
   249     {
       
   250         QConicalGradient cg1(101, 102, 103);
       
   251 
       
   252         QConicalGradient cg2 = cg1;
       
   253         QCOMPARE(cg1.center(), cg2.center());
       
   254         QCOMPARE(cg1.angle(), cg2.angle());
       
   255 
       
   256         QGradient g = cg1;
       
   257         QCOMPARE(((QConicalGradient *) &g)->center(), cg1.center());
       
   258         QCOMPARE(((QConicalGradient *) &g)->angle(), cg1.angle());
       
   259     }
       
   260 
       
   261 }
       
   262 
       
   263 void tst_QBrush::badStyles()
       
   264 {
       
   265     // QBrush(Qt::BrushStyle) constructor
       
   266     QCOMPARE(QBrush(Qt::LinearGradientPattern).style(), Qt::NoBrush);
       
   267     QCOMPARE(QBrush(Qt::RadialGradientPattern).style(), Qt::NoBrush);
       
   268     QCOMPARE(QBrush(Qt::ConicalGradientPattern).style(), Qt::NoBrush);
       
   269     QCOMPARE(QBrush(Qt::TexturePattern).style(), Qt::NoBrush);
       
   270 
       
   271     // QBrush(QColor, Qt::BrushStyle) constructor
       
   272     QCOMPARE(QBrush(QColor(0, 0, 0), Qt::LinearGradientPattern).style(), Qt::NoBrush);
       
   273     QCOMPARE(QBrush(QColor(0, 0, 0), Qt::RadialGradientPattern).style(), Qt::NoBrush);
       
   274     QCOMPARE(QBrush(QColor(0, 0, 0), Qt::ConicalGradientPattern).style(), Qt::NoBrush);
       
   275     QCOMPARE(QBrush(QColor(0, 0, 0), Qt::TexturePattern).style(), Qt::NoBrush);
       
   276 
       
   277     // QBrush(Qt::GlobalColor, Qt::BrushStyle) constructor
       
   278     QCOMPARE(QBrush(Qt::black, Qt::LinearGradientPattern).style(), Qt::NoBrush);
       
   279     QCOMPARE(QBrush(Qt::black, Qt::RadialGradientPattern).style(), Qt::NoBrush);
       
   280     QCOMPARE(QBrush(Qt::black, Qt::ConicalGradientPattern).style(), Qt::NoBrush);
       
   281     QCOMPARE(QBrush(Qt::black, Qt::TexturePattern).style(), Qt::NoBrush);
       
   282 
       
   283     // Set style...
       
   284     QBrush brush(Qt::red);
       
   285 
       
   286     brush.setStyle(Qt::LinearGradientPattern);
       
   287     QCOMPARE(brush.style(), Qt::SolidPattern);
       
   288 
       
   289     brush.setStyle(Qt::RadialGradientPattern);
       
   290     QCOMPARE(brush.style(), Qt::SolidPattern);
       
   291 
       
   292     brush.setStyle(Qt::ConicalGradientPattern);
       
   293     QCOMPARE(brush.style(), Qt::SolidPattern);
       
   294 
       
   295     brush.setStyle(Qt::TexturePattern);
       
   296     QCOMPARE(brush.style(), Qt::SolidPattern);
       
   297 
       
   298 }
       
   299 
       
   300 void tst_QBrush::gradientStops()
       
   301 {
       
   302     QLinearGradient gradient;
       
   303     gradient.setColorAt(0, Qt::red);
       
   304     gradient.setColorAt(1, Qt::blue);
       
   305 
       
   306     QCOMPARE(gradient.stops().size(), 2);
       
   307 
       
   308     QCOMPARE(gradient.stops().at(0), QGradientStop(0, QColor(Qt::red)));
       
   309     QCOMPARE(gradient.stops().at(1), QGradientStop(1, QColor(Qt::blue)));
       
   310 
       
   311     gradient.setColorAt(0, Qt::blue);
       
   312     gradient.setColorAt(1, Qt::red);
       
   313 
       
   314     QCOMPARE(gradient.stops().size(), 2);
       
   315 
       
   316     QCOMPARE(gradient.stops().at(0), QGradientStop(0, QColor(Qt::blue)));
       
   317     QCOMPARE(gradient.stops().at(1), QGradientStop(1, QColor(Qt::red)));
       
   318 
       
   319     gradient.setColorAt(0.5, Qt::green);
       
   320 
       
   321     QCOMPARE(gradient.stops().size(), 3);
       
   322     QCOMPARE(gradient.stops().at(1), QGradientStop(0.5, QColor(Qt::green)));
       
   323 
       
   324     // A hack in parseStopNode() in qsvghandler.cpp depends on inserting stops at NaN.
       
   325     gradient.setStops(QGradientStops() << QGradientStop(qQNaN(), QColor()));
       
   326     QCOMPARE(gradient.stops().size(), 1);
       
   327     QVERIFY(qIsNaN(gradient.stops().at(0).first));
       
   328     QCOMPARE(gradient.stops().at(0).second, QColor());
       
   329 }
       
   330 
       
   331 void fill(QPaintDevice *pd) {
       
   332     QPainter p(pd);
       
   333 
       
   334     int w = pd->width();
       
   335     int h = pd->height();
       
   336 
       
   337     p.fillRect(0, 0, w, h, Qt::white);
       
   338     p.fillRect(0, 0, w/3, h/3, Qt::black);
       
   339 }
       
   340 
       
   341 void tst_QBrush::textures()
       
   342 {
       
   343     QPixmap pixmap_source(10, 10);
       
   344     QImage image_source(10, 10, QImage::Format_RGB32);
       
   345 
       
   346     fill(&pixmap_source);
       
   347     fill(&image_source);
       
   348 
       
   349     // Create a pixmap brush and compare its texture and textureImage
       
   350     // to the expected image
       
   351     QBrush pixmap_brush;
       
   352     pixmap_brush.setTexture(pixmap_source);
       
   353     QImage image = pixmap_brush.texture().toImage().convertToFormat(QImage::Format_RGB32);
       
   354     QCOMPARE(image, image_source);
       
   355     image = pixmap_brush.textureImage().convertToFormat(QImage::Format_RGB32);
       
   356     QCOMPARE(image, image_source);
       
   357 
       
   358     pixmap_brush = QBrush(pixmap_source);
       
   359     image = pixmap_brush.texture().toImage().convertToFormat(QImage::Format_RGB32);
       
   360     QCOMPARE(image, image_source);
       
   361     image = pixmap_brush.textureImage().convertToFormat(QImage::Format_RGB32);
       
   362     QCOMPARE(image, image_source);
       
   363 
       
   364     // Create a image brush and compare its texture and textureImage
       
   365     // to the expected image
       
   366     QBrush image_brush;
       
   367     image_brush.setTextureImage(image_source);
       
   368     image = image_brush.texture().toImage().convertToFormat(QImage::Format_RGB32);
       
   369     QCOMPARE(image, image_source);
       
   370     QCOMPARE(image_brush.textureImage(), image_source);
       
   371 
       
   372     image_brush = QBrush(image_source);
       
   373     image = image_brush.texture().toImage().convertToFormat(QImage::Format_RGB32);
       
   374     QCOMPARE(image, image_source);
       
   375     QCOMPARE(image_brush.textureImage(), image_source);
       
   376 }
       
   377 
       
   378 void tst_QBrush::nullBrush()
       
   379 {
       
   380     QBrush brush(QColor(100,0,0), Qt::NoBrush);
       
   381     QCOMPARE(brush.color(), QColor(100,0,0));
       
   382 }
       
   383 
       
   384 void tst_QBrush::isOpaque()
       
   385 {
       
   386     QBitmap bm(8, 8);
       
   387     bm.fill(Qt::black);
       
   388 
       
   389     QBrush brush(bm);
       
   390     QVERIFY(!brush.isOpaque());
       
   391 }
       
   392 
       
   393 QTEST_MAIN(tst_QBrush)
       
   394 #include "tst_qbrush.moc"