tests/auto/qlabel/tst_qlabel.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
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 
       
    46 #include "qlabel.h"
       
    47 #include <qapplication.h>
       
    48 #include <qboxlayout.h>
       
    49 #include <qlabel.h>
       
    50 #include <qlineedit.h>
       
    51 #include <qmovie.h>
       
    52 #include <qpicture.h>
       
    53 #include <qmessagebox.h>
       
    54 
       
    55 //TESTED_CLASS=
       
    56 //TESTED_FILES=
       
    57 #if defined(Q_OS_SYMBIAN)
       
    58 # define SRCDIR ""
       
    59 #endif
       
    60 
       
    61 #include "../../shared/util.h"
       
    62 
       
    63 class Widget : public QWidget
       
    64 {
       
    65 public:
       
    66     Widget() { }
       
    67 
       
    68     QList<QEvent::Type> events;
       
    69 
       
    70 protected:
       
    71     bool event(QEvent *ev) {
       
    72         events.append(ev->type());
       
    73         return QWidget::event(ev);
       
    74     }
       
    75 
       
    76 };
       
    77 
       
    78 class tst_QLabel : public QObject
       
    79 {
       
    80 Q_OBJECT
       
    81 
       
    82 public:
       
    83     tst_QLabel();
       
    84     virtual ~tst_QLabel();
       
    85 
       
    86 
       
    87 public slots:
       
    88     void initTestCase();
       
    89     void cleanupTestCase();
       
    90     void init();
       
    91     void cleanup();
       
    92 private slots:
       
    93     void getSetCheck();
       
    94     void text();
       
    95     void setText_data();
       
    96     void setText();
       
    97     void textFormat();
       
    98     void setTextFormat();
       
    99     void buddy();
       
   100     void setBuddy();
       
   101     void setFont();
       
   102     void setNum();
       
   103     void clear();
       
   104     void wordWrap();
       
   105     void eventPropagation_data();
       
   106     void eventPropagation();
       
   107     void focusPolicy();
       
   108 
       
   109     void task190318_sizes();
       
   110 
       
   111     void sizeHint();
       
   112 
       
   113     void task226479_movieResize();
       
   114     void emptyPixmap();
       
   115 
       
   116     void unicodeText_data();
       
   117     void unicodeText();
       
   118 
       
   119 private:
       
   120     QLabel *testWidget;
       
   121     QPointer<Widget> test_box;
       
   122     QPointer<QLabel> test_label;
       
   123     QLineEdit *test_edit;
       
   124 };
       
   125 
       
   126 // Testing get/set functions
       
   127 void tst_QLabel::getSetCheck()
       
   128 {
       
   129     QLabel obj1;
       
   130     // bool QLabel::wordWrap()
       
   131     // void QLabel::setWordWrap(bool)
       
   132     obj1.setWordWrap(false);
       
   133     QCOMPARE(false, obj1.wordWrap());
       
   134     obj1.setWordWrap(true);
       
   135     QCOMPARE(true, obj1.wordWrap());
       
   136 
       
   137     // QWidget * QLabel::buddy()
       
   138     // void QLabel::setBuddy(QWidget *)
       
   139     QWidget *var2 = new QWidget();
       
   140     obj1.setBuddy(var2);
       
   141     QCOMPARE(var2, obj1.buddy());
       
   142     obj1.setBuddy((QWidget *)0);
       
   143     QCOMPARE((QWidget *)0, obj1.buddy());
       
   144     delete var2;
       
   145 
       
   146     // QMovie * QLabel::movie()
       
   147     // void QLabel::setMovie(QMovie *)
       
   148     QMovie *var3 = new QMovie;
       
   149     obj1.setMovie(var3);
       
   150     QCOMPARE(var3, obj1.movie());
       
   151     obj1.setMovie((QMovie *)0);
       
   152     QCOMPARE((QMovie *)0, obj1.movie());
       
   153     delete var3;
       
   154 }
       
   155 
       
   156 
       
   157 tst_QLabel::tst_QLabel(): test_box(0)
       
   158 {
       
   159 }
       
   160 
       
   161 tst_QLabel::~tst_QLabel()
       
   162 {
       
   163 }
       
   164 
       
   165 void tst_QLabel::initTestCase()
       
   166 {
       
   167     // Create the test class
       
   168     testWidget = new QLabel(0);
       
   169     testWidget->resize( 200, 200 );
       
   170     testWidget->show();
       
   171 }
       
   172 
       
   173 void tst_QLabel::cleanupTestCase()
       
   174 {
       
   175     delete testWidget;
       
   176     testWidget = 0;
       
   177     if (test_box)
       
   178         delete test_box;
       
   179 }
       
   180 
       
   181 void tst_QLabel::init()
       
   182 {
       
   183     testWidget->setTextFormat( Qt::AutoText );
       
   184     testWidget->setBuddy( 0 );
       
   185     testWidget->setIndent( 0 );
       
   186     testWidget->setAlignment( Qt::AlignLeft | Qt::AlignVCenter );
       
   187     testWidget->setScaledContents( FALSE );
       
   188 }
       
   189 
       
   190 void tst_QLabel::cleanup()
       
   191 {
       
   192     if (QTest::currentTestFunction() == QLatin1String("setBuddy")) {
       
   193 	testWidget->show();
       
   194 
       
   195 	delete test_box; // this should delete tst_labl and test_edit as well.
       
   196     }
       
   197 }
       
   198 
       
   199 void tst_QLabel::buddy()
       
   200 {
       
   201     DEPENDS_ON( "setBuddy" );
       
   202 }
       
   203 
       
   204 void tst_QLabel::setBuddy()
       
   205 {
       
   206 #ifdef Q_WS_MAC
       
   207     QSKIP("Set buddy doesn't make much sense on Mac OS X", SkipAll);
       
   208 #endif
       
   209     testWidget->hide();
       
   210 
       
   211     test_box = new Widget;
       
   212     test_label= new QLabel( test_box );
       
   213     test_label->setText( "&Test with a buddy" );
       
   214     test_edit = new QLineEdit( test_box );
       
   215     QVBoxLayout *layout = new QVBoxLayout(test_box);
       
   216     layout->addWidget(test_label);
       
   217     layout->addWidget(test_edit);
       
   218     test_box->show();
       
   219     qApp->setActiveWindow(test_box);
       
   220     QVERIFY(test_box->isActiveWindow());
       
   221 
       
   222     test_label->setBuddy( test_edit );
       
   223     test_label->setFocus();
       
   224     QVERIFY( !test_edit->hasFocus() );
       
   225     QTest::keyClick( test_box, 't', Qt::AltModifier );
       
   226     QVERIFY( test_edit->hasFocus() );
       
   227 }
       
   228 
       
   229 void tst_QLabel::text()
       
   230 {
       
   231     DEPENDS_ON( "setText" );
       
   232 }
       
   233 
       
   234 void tst_QLabel::setText_data()
       
   235 {
       
   236     QTest::addColumn<QString>("txt");
       
   237     QTest::addColumn<QString>("font");
       
   238 
       
   239     QString prefix = "";
       
   240 #ifdef Q_OS_WIN32
       
   241     prefix = "win32_";
       
   242 #endif
       
   243 
       
   244     QTest::newRow( QString(prefix + "data0").toLatin1() ) << QString("This is a single line") << QString("Helvetica");
       
   245     QTest::newRow( QString(prefix + "data1").toLatin1() ) << QString("This is the first line\nThis is the second line") << QString("Courier");
       
   246     QTest::newRow( QString(prefix + "data2").toLatin1() ) << QString("This is the first line\nThis is the second line\nThis is the third line") << QString("Helvetica");
       
   247     QTest::newRow( QString(prefix + "data3").toLatin1() ) << QString("This is <b>bold</b> richtext") << QString("Courier");
       
   248 }
       
   249 
       
   250 void tst_QLabel::setText()
       
   251 {
       
   252     QFETCH( QString, txt );
       
   253     QFETCH( QString, font );
       
   254     QFont f( font, 8 );
       
   255     testWidget->setFont( f );
       
   256     testWidget->setText( txt );
       
   257     QCOMPARE( testWidget->text(), txt );
       
   258 }
       
   259 
       
   260 
       
   261 void tst_QLabel::textFormat()
       
   262 {
       
   263     DEPENDS_ON( "setTextFormat" );
       
   264 }
       
   265 
       
   266 void tst_QLabel::setTextFormat()
       
   267 {
       
   268     // lets' start with the simple stuff...
       
   269     testWidget->setTextFormat( Qt::PlainText );
       
   270     QVERIFY( testWidget->textFormat() == Qt::PlainText );
       
   271 
       
   272     testWidget->setTextFormat( Qt::RichText );
       
   273     QVERIFY( testWidget->textFormat() == Qt::RichText );
       
   274 
       
   275     testWidget->setTextFormat( Qt::LogText );
       
   276     QVERIFY( testWidget->textFormat() == Qt::LogText );
       
   277 
       
   278     testWidget->setTextFormat( Qt::AutoText );
       
   279     QVERIFY( testWidget->textFormat() == Qt::AutoText );
       
   280 }
       
   281 
       
   282 
       
   283 void tst_QLabel::setFont()
       
   284 {
       
   285     DEPENDS_ON( "setText" );
       
   286 }
       
   287 
       
   288 void tst_QLabel::setNum()
       
   289 {
       
   290     testWidget->setText( "This is a text" );
       
   291     testWidget->setNum( 12 );
       
   292     QCOMPARE( testWidget->text(), QString("12") );
       
   293     testWidget->setNum( 12.345 );
       
   294     QCOMPARE( testWidget->text(), QString("12.345") );
       
   295 }
       
   296 
       
   297 void tst_QLabel::clear()
       
   298 {
       
   299     const QString TEXT = "blah blah";
       
   300     testWidget->setText(TEXT);
       
   301     QCOMPARE(testWidget->text(), TEXT);
       
   302     testWidget->clear();
       
   303     QVERIFY(testWidget->text().isEmpty());
       
   304 }
       
   305 
       
   306 void tst_QLabel::wordWrap()
       
   307 {
       
   308     QLabel label;
       
   309 
       
   310     QVERIFY(!label.wordWrap());
       
   311 
       
   312     label.setText("Plain Text");
       
   313     QVERIFY(!label.wordWrap());
       
   314 
       
   315     label.setText("<b>rich text</b>");
       
   316     QVERIFY(!label.wordWrap());
       
   317 
       
   318     label.setWordWrap(false);
       
   319     label.setText("<b>rich text</b>");
       
   320     QVERIFY(!label.wordWrap());
       
   321 }
       
   322 
       
   323 void tst_QLabel::eventPropagation_data()
       
   324 {
       
   325     QTest::addColumn<QString>("text");
       
   326     QTest::addColumn<int>("textInteractionFlags");
       
   327     QTest::addColumn<int>("focusPolicy");
       
   328     QTest::addColumn<bool>("propagation");
       
   329 
       
   330     QTest::newRow("plain text1") << QString("plain text") << int(Qt::LinksAccessibleByMouse) << int(Qt::NoFocus) << true;
       
   331     QTest::newRow("plain text2") << QString("plain text") << (int)Qt::TextSelectableByKeyboard << (int)Qt::ClickFocus << false;
       
   332     QTest::newRow("plain text3") << QString("plain text") << (int)Qt::TextSelectableByMouse << (int)Qt::ClickFocus << false;
       
   333     QTest::newRow("plain text4") << QString("plain text") << (int)Qt::NoTextInteraction << (int)Qt::NoFocus << true;
       
   334     QTest::newRow("rich text1") << QString("<b>rich text</b>") << (int)Qt::LinksAccessibleByMouse << (int)Qt::NoFocus << false;
       
   335     QTest::newRow("rich text2") << QString("<b>rich text</b>") << (int)Qt::TextSelectableByKeyboard << (int)Qt::ClickFocus << false;
       
   336     QTest::newRow("rich text3") << QString("<b>rich text</b>") << (int)Qt::TextSelectableByMouse << (int)Qt::ClickFocus << false;
       
   337     QTest::newRow("rich text4") << QString("<b>rich text</b>") << (int)Qt::NoTextInteraction << (int)Qt::NoFocus << true;
       
   338     QTest::newRow("rich text4") << QString("<b>rich text</b>") << (int)Qt::LinksAccessibleByKeyboard << (int)Qt::StrongFocus << false;
       
   339 
       
   340     if (!test_box)
       
   341         test_box = new Widget;
       
   342     if (!test_label)
       
   343         test_label = new QLabel(test_box);
       
   344 }
       
   345 
       
   346 void tst_QLabel::eventPropagation()
       
   347 {
       
   348     QFETCH(QString, text);
       
   349     QFETCH(int, textInteractionFlags);
       
   350     QFETCH(int, focusPolicy);
       
   351     QFETCH(bool, propagation);
       
   352 
       
   353     // plain text (accepts mouse event _only_ when label selectable by mouse)
       
   354     test_label->setText(text);
       
   355     test_box->events.clear();
       
   356     test_label->setTextInteractionFlags(Qt::TextInteractionFlags(textInteractionFlags));
       
   357     QVERIFY(int(test_label->focusPolicy()) == focusPolicy);
       
   358     QTest::mousePress(test_label, Qt::LeftButton);
       
   359     QVERIFY(test_box->events.contains(QEvent::MouseButtonPress) == propagation); // should have propagated!
       
   360 }
       
   361 
       
   362 void tst_QLabel::focusPolicy()
       
   363 {
       
   364     delete test_label;
       
   365     test_label = new QLabel;
       
   366     QCOMPARE(test_label->focusPolicy(), Qt::NoFocus); // default
       
   367     test_label->setFocusPolicy(Qt::StrongFocus);
       
   368     test_label->setText("Whatever"); // setting text should not change the focus policy
       
   369     QCOMPARE(test_label->focusPolicy(), Qt::StrongFocus);
       
   370     test_label->setTextInteractionFlags(Qt::TextSelectableByKeyboard); // this should
       
   371     QCOMPARE(test_label->focusPolicy(), Qt::ClickFocus);
       
   372     test_label->setFocusPolicy(Qt::StrongFocus);
       
   373     test_label->setText("Whatever"); // setting text should not change the focus policy
       
   374     QCOMPARE(test_label->focusPolicy(), Qt::StrongFocus);
       
   375     test_label->setTextInteractionFlags(Qt::NoTextInteraction);
       
   376     QCOMPARE(test_label->focusPolicy(), Qt::NoFocus);
       
   377     test_label->setFocusPolicy(Qt::StrongFocus);
       
   378     test_label->setTextInteractionFlags(Qt::NoTextInteraction);
       
   379     QCOMPARE(test_label->focusPolicy(), Qt::StrongFocus); // is not touched since value didn't change
       
   380     delete test_label;
       
   381 }
       
   382 
       
   383 void tst_QLabel::task190318_sizes()
       
   384 {
       
   385     QLabel label(" ");
       
   386     QSize ms(500,600);
       
   387     label.setMinimumSize(ms);
       
   388     QCOMPARE(label.minimumSize(), ms);
       
   389     QCOMPARE(label.sizeHint(), ms);
       
   390     QCOMPARE(label.minimumSizeHint(), ms);
       
   391 }
       
   392 
       
   393 void tst_QLabel::sizeHint()
       
   394 {
       
   395     QLabel label(QLatin1String("Test"));
       
   396     label.setIndent(0);
       
   397     label.setMargin(0);
       
   398     label.setContentsMargins(0, 0, 0, 0);
       
   399     label.setAlignment(Qt::AlignVCenter);
       
   400     int h = label.sizeHint().height();
       
   401 
       
   402     QLabel l1(QLatin1String("Test"));
       
   403     l1.setIndent(0);
       
   404     l1.setMargin(0);
       
   405     l1.setContentsMargins(0, 0, 0, 0);
       
   406     l1.setAlignment(Qt::AlignVCenter);
       
   407     l1.setTextInteractionFlags(Qt::TextSelectableByMouse);   // will now use qtextcontrol
       
   408     int h1 = l1.sizeHint().height();
       
   409     QCOMPARE(h1, h);
       
   410 
       
   411 }
       
   412 
       
   413 void tst_QLabel::task226479_movieResize()
       
   414 {
       
   415     class Label : public QLabel {
       
   416         protected:
       
   417             void paintEvent(QPaintEvent *e)
       
   418             {
       
   419                 paintedRegion += e->region();
       
   420                 QLabel::paintEvent(e);
       
   421             }
       
   422 
       
   423         public:
       
   424             QRegion paintedRegion;
       
   425     };
       
   426 
       
   427     Label label;
       
   428     label.resize(350,350);
       
   429     label.show();
       
   430     QMovie *movie = new QMovie( &label );
       
   431     label.setMovie(movie);
       
   432     QTest::qWaitForWindowShown(&label);
       
   433     movie->setFileName(SRCDIR "red.png");
       
   434     movie->start();
       
   435     QTest::qWait(50);
       
   436     movie->stop();
       
   437     label.paintedRegion = QRegion();
       
   438     movie->setFileName(SRCDIR "green.png");
       
   439     movie->start();
       
   440     QTest::qWait(50);
       
   441 
       
   442     QTRY_COMPARE(label.paintedRegion , QRegion(label.rect()) );
       
   443 }
       
   444 
       
   445 void tst_QLabel::emptyPixmap()
       
   446 {
       
   447     //task 197919
       
   448     QLabel label1, label2, label3, label4;
       
   449     label2.setPixmap(QPixmap("/tmp/idonotexist"));
       
   450     QMovie movie;
       
   451     label3.setMovie(&movie);
       
   452     label4.setPicture(QPicture());
       
   453     QCOMPARE(label1.sizeHint(), label2.sizeHint());
       
   454     QCOMPARE(label1.sizeHint(), label3.sizeHint());
       
   455     QCOMPARE(label1.sizeHint(), label4.sizeHint());
       
   456 }
       
   457 
       
   458 /**
       
   459     Test for QTBUG-4848 - unicode data corrupting QLabel display
       
   460 */
       
   461 void tst_QLabel::unicodeText_data()
       
   462 {
       
   463     QTest::addColumn<QString>("text");
       
   464     QTest::addColumn<QString>("languageName");
       
   465 
       
   466     /*
       
   467     The "glass" phrase in Thai was the initial report for bug QTBUG-4848, was
       
   468     originally found on http://www.columbia.edu/kermit/utf8.html.
       
   469 
       
   470     The phrase is from an internet tradition regarding a striking phrase
       
   471     that is translated into many different languages.  The utf8 strings
       
   472     below were generated by using http://translate.google.com.
       
   473 
       
   474     The glass phrase in Thai contains the ้ว character which manifests bug
       
   475     QTBUG-4848
       
   476 
       
   477     The last long phrase is an excerpt from Churchills "on the beaches"
       
   478     speech, also translated using http://translate.google.com.
       
   479     */
       
   480 
       
   481     QTest::newRow("english") << QString::fromUtf8("I can eat glass and it doesn't hurt me.") << QString("english");
       
   482     QTest::newRow("thai") << QString::fromUtf8("ฉันจะกินแก้วและไม่เจ็บฉัน") << QString("thai");
       
   483     QTest::newRow("chinese") << QString::fromUtf8("我可以吃玻璃,并没有伤害我。") << QString("chinese");
       
   484     QTest::newRow("arabic") << QString::fromUtf8("أستطيع أكل الزجاج ، وأنه لا يؤذيني.") << QString("arabic");
       
   485     QTest::newRow("russian") << QString::fromUtf8("Я могу есть стекло, и не больно.") << QString("russian");
       
   486     QTest::newRow("korean") << QString::fromUtf8("유리를 먹을 수있는, 그리고 그게 날 다치게하지 않습니다.") << QString("korean");
       
   487     QTest::newRow("greek") << QString::fromUtf8("Μπορώ να φάτε γυαλί και δεν μου κάνει κακό.") << QString("greek");
       
   488     QTest::newRow("german") << QString::fromUtf8("Ich kann Glas essen und es macht mich nicht heiß.") << QString("german");
       
   489 
       
   490     QTest::newRow("thai_long") << QString::fromUtf8("เราจะต่อสู้ในทะเลและมหาสมุทร. เราจะต่อสู้ด้วยความมั่นใจเติบโตและความเจริญเติบโตในอากาศเราจะปกป้องเกาะของเราค่าใช้จ่ายใดๆอาจ."
       
   491                                                     "เราจะต่อสู้บนชายหาดเราจะต่อสู้ในบริเวณเชื่อมโยงไปถึงเราจะต่อสู้ในช่องและในถนนที่เราจะต่อสู้ในภูเขานั้นเราจะไม่ยอม.")
       
   492             << QString("thai_long");
       
   493 }
       
   494 
       
   495 void tst_QLabel::unicodeText()
       
   496 {
       
   497     const QString testDataPath("testdata/unicodeText");
       
   498     QFETCH(QString, text);
       
   499     QFETCH(QString, languageName);
       
   500     QFrame frame;
       
   501     QVBoxLayout *layout = new QVBoxLayout();
       
   502     QLabel *label = new QLabel(text, &frame);
       
   503     layout->addWidget(label);
       
   504     layout->setMargin(8);
       
   505     frame.setLayout(layout);
       
   506     frame.show();
       
   507     QTest::qWaitForWindowShown(&frame);
       
   508     QVERIFY(frame.isVisible());  // was successfully sized and shown
       
   509     testWidget->show();
       
   510 }
       
   511 
       
   512 QTEST_MAIN(tst_QLabel)
       
   513 #include "tst_qlabel.moc"