tests/auto/q3richtext/tst_q3richtext.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 <q3textedit.h>
       
    46 #include <qapplication.h>
       
    47 #include <qclipboard.h>
       
    48 
       
    49 #ifdef Q_WS_MAC
       
    50 #include <Carbon/Carbon.h>
       
    51 #endif
       
    52 
       
    53 //TESTED_FILES=
       
    54 
       
    55 class tst_Q3RichText : public QObject
       
    56 {
       
    57     Q_OBJECT
       
    58 public:
       
    59     tst_Q3RichText();
       
    60     virtual ~tst_Q3RichText();
       
    61 
       
    62 
       
    63 public slots:
       
    64     void initTestCase();
       
    65     void cleanupTestCase();
       
    66 private slots:
       
    67     void richtext();
       
    68     void append_data();
       
    69     void append();
       
    70     void cursorPosition();
       
    71     void moveCursor();
       
    72     void keyPressEvent();
       
    73 };
       
    74 
       
    75 tst_Q3RichText::tst_Q3RichText()
       
    76 {
       
    77 }
       
    78 
       
    79 tst_Q3RichText::~tst_Q3RichText()
       
    80 {
       
    81 }
       
    82 
       
    83 void tst_Q3RichText::initTestCase()
       
    84 {
       
    85   // create a default mainwindow
       
    86   // If you run a widget test, this will be replaced in the testcase by the
       
    87   // widget under test
       
    88     QWidget *w = new QWidget(0,"mainWidget");
       
    89     w->setFixedSize( 200, 200 );
       
    90     qApp->setMainWidget( w );
       
    91     w->show();
       
    92 }
       
    93 
       
    94 void tst_Q3RichText::cleanupTestCase()
       
    95 {
       
    96     delete qApp->mainWidget();
       
    97 }
       
    98 
       
    99 void tst_Q3RichText::richtext()
       
   100 {
       
   101     Q3TextEdit* textedit = new Q3TextEdit();
       
   102     QString result( "<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body style=\"font-size:%1;font-family:%2\">\n"
       
   103 	    "<p dir=\"ltr\">k <span style=\"font-family:Arial;font-size:20pt%3\">   </span><span style=\"font-family:Arial;font-size:15pt%4\">  </span><span style=\"font-family:Arial;font-size:20pt%5\">k k  </span></p>\n"
       
   104 	    "</body></html>\n" );
       
   105     QFont font = textedit->font();
       
   106     result = result.arg( QString::number(font.pointSize()) + "pt" ).arg( font.family() ).arg( "" ).arg( "" ).arg( "" );
       
   107 
       
   108     textedit->setTextFormat( Qt::RichText );
       
   109 
       
   110     QKeyEvent ke( QEvent::KeyPress, Qt::Key_K, 107, Qt::NoButton, "k" );
       
   111     QApplication::sendEvent( textedit->viewport(), &ke );
       
   112 
       
   113     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Space, 32, Qt::NoButton, " " );
       
   114     QApplication::sendEvent( textedit->viewport(), &ke );
       
   115 
       
   116     textedit->setCurrentFont( QFont( "Arial", 20 ) );
       
   117 
       
   118     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Space, 32, Qt::NoButton, " " );
       
   119     QApplication::sendEvent( textedit->viewport(), &ke );
       
   120 
       
   121     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Space, 32, Qt::NoButton, " " );
       
   122     QApplication::sendEvent( textedit->viewport(), &ke );
       
   123 
       
   124     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Space, 32, Qt::NoButton, " " );
       
   125     QApplication::sendEvent( textedit->viewport(), &ke );
       
   126 
       
   127     textedit->setCurrentFont( QFont( "Arial", 15 ) );
       
   128 
       
   129     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Space, 32, Qt::NoButton, " " );
       
   130     QApplication::sendEvent( textedit->viewport(), &ke );
       
   131 
       
   132     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Space, 32, Qt::NoButton, " " );
       
   133     QApplication::sendEvent( textedit->viewport(), &ke );
       
   134 
       
   135     textedit->setCurrentFont( QFont( "Arial", 20 ) );
       
   136 
       
   137     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_K, 107, Qt::NoButton, "k" );
       
   138     QApplication::sendEvent( textedit->viewport(), &ke );
       
   139 
       
   140     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Space, 32, Qt::NoButton, " " );
       
   141     QApplication::sendEvent( textedit->viewport(), &ke );
       
   142 
       
   143     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_K, 107, Qt::NoButton, "k" );
       
   144     QApplication::sendEvent( textedit->viewport(), &ke );
       
   145 
       
   146     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Space, 32, Qt::NoButton, " " );
       
   147     QApplication::sendEvent( textedit->viewport(), &ke );
       
   148 
       
   149     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Space, 32, Qt::NoButton, " " );
       
   150     QApplication::sendEvent( textedit->viewport(), &ke );
       
   151 
       
   152     QCOMPARE( textedit->text(), result );
       
   153 
       
   154     delete textedit;
       
   155 }
       
   156 
       
   157 void tst_Q3RichText::append_data()
       
   158 {
       
   159     // should we catch bad alloc or something if it fails?
       
   160     // Being this is a test.
       
   161     QTest::addColumn<QString>("firstString");
       
   162     QTest::addColumn<QString>("secondString");
       
   163     QTest::addColumn<QString>("result");
       
   164     QTest::addColumn<int>("textFormat");
       
   165 
       
   166     QTest::newRow( "data0" ) << QString("Hello World...") << QString("First Time")
       
   167 			   << QString("Hello World...\nFirst Time")
       
   168 			   << int(Qt::PlainText);
       
   169     QTest::newRow( "data1" ) << QString("<b>Hello World...</b>") << QString("Second Time")
       
   170 			    << QString("Hello World...</span></p>\n<p dir=\"ltr\">Second Time</p>\n</body></html>\n" ) << int(Qt::RichText);
       
   171 }
       
   172 
       
   173 
       
   174 void tst_Q3RichText::append()
       
   175 {
       
   176     Q3TextEdit textedit;
       
   177     QFETCH( QString, firstString );
       
   178     QFETCH( QString, secondString );
       
   179     QFETCH( QString, result );
       
   180     QFETCH( int, textFormat );
       
   181 
       
   182     textedit.setTextFormat( Qt::TextFormat(textFormat) );
       
   183     textedit.append( firstString );
       
   184     textedit.append( secondString );
       
   185     QCOMPARE( textedit.text().mid(textedit.text().indexOf("Hello World")), result );
       
   186 }
       
   187 
       
   188 void tst_Q3RichText::cursorPosition()
       
   189 {
       
   190     Q3TextEdit textedit;
       
   191     textedit.setText( "This is a test" );
       
   192     textedit.setCursorPosition( textedit.paragraphs() - 1, textedit.paragraphLength( textedit.paragraphs() - 1 ) );
       
   193     int para;
       
   194     int index;
       
   195     textedit.getCursorPosition( &para, &index );
       
   196     QCOMPARE( para, 0 );
       
   197     QCOMPARE( index, 14 );
       
   198     textedit.setText( "This is a test\nThis is a test\nThis is a test" );
       
   199     textedit.setCursorPosition( textedit.paragraphs() - 1, textedit.paragraphLength( textedit.paragraphs() - 1 ) );
       
   200     textedit.getCursorPosition( &para, &index );
       
   201     QCOMPARE( para, 2 );
       
   202     QCOMPARE( index, 14 );
       
   203 }
       
   204 
       
   205 void tst_Q3RichText::moveCursor()
       
   206 {
       
   207     // Still needs to test for MovePageUp and MovePageDown
       
   208 
       
   209     int para, index;
       
   210 
       
   211     Q3TextEdit textedit;
       
   212     textedit.show();
       
   213     textedit.setText( "This is a test" );
       
   214 
       
   215     textedit.moveCursor( Q3TextEdit::MoveEnd, FALSE );
       
   216     textedit.getCursorPosition( &para, &index );
       
   217     QCOMPARE( para, 0 );
       
   218     QCOMPARE( index, 14 );
       
   219 
       
   220     textedit.moveCursor( Q3TextEdit::MoveBackward, FALSE );
       
   221     textedit.getCursorPosition( &para, &index );
       
   222     QCOMPARE( para, 0 );
       
   223     QCOMPARE( index, 13 );
       
   224 
       
   225     textedit.moveCursor( Q3TextEdit::MoveWordBackward, FALSE );
       
   226     textedit.getCursorPosition( &para, &index );
       
   227     QCOMPARE( para, 0 );
       
   228     QCOMPARE( index, 10 );
       
   229 
       
   230     textedit.moveCursor( Q3TextEdit::MoveHome, FALSE );
       
   231     textedit.getCursorPosition( &para, &index );
       
   232     QCOMPARE( para, 0 );
       
   233     QCOMPARE( index, 0 );
       
   234 
       
   235     textedit.moveCursor( Q3TextEdit::MoveForward, FALSE );
       
   236     textedit.getCursorPosition( &para, &index );
       
   237     QCOMPARE( para, 0 );
       
   238     QCOMPARE( index, 1 );
       
   239 
       
   240     textedit.moveCursor( Q3TextEdit::MoveWordForward, FALSE );
       
   241     textedit.getCursorPosition( &para, &index );
       
   242     QCOMPARE( para, 0 );
       
   243     QCOMPARE( index, 5 );
       
   244 
       
   245     textedit.moveCursor( Q3TextEdit::MoveLineStart, FALSE );
       
   246     textedit.getCursorPosition( &para, &index );
       
   247     QCOMPARE( para, 0 );
       
   248     QCOMPARE( index, 0 );
       
   249 
       
   250     textedit.moveCursor( Q3TextEdit::MoveLineEnd, FALSE );
       
   251     textedit.getCursorPosition( &para, &index );
       
   252     QCOMPARE( para, 0 );
       
   253     QCOMPARE( index, 14 );
       
   254 
       
   255     textedit.setText( "This is a test\nThis is a test\nThis is a test" );
       
   256 
       
   257     textedit.moveCursor( Q3TextEdit::MoveEnd, FALSE );
       
   258     textedit.getCursorPosition( &para, &index );
       
   259     QCOMPARE( para, 2 );
       
   260     QCOMPARE( index, 14 );
       
   261 
       
   262     textedit.moveCursor( Q3TextEdit::MoveBackward, FALSE );
       
   263     textedit.getCursorPosition( &para, &index );
       
   264     QCOMPARE( para, 2 );
       
   265     QCOMPARE( index, 13 );
       
   266 
       
   267     textedit.moveCursor( Q3TextEdit::MoveWordBackward, FALSE );
       
   268     textedit.getCursorPosition( &para, &index );
       
   269     QCOMPARE( para, 2 );
       
   270     QCOMPARE( index, 10 );
       
   271 
       
   272     textedit.moveCursor( Q3TextEdit::MoveHome, FALSE );
       
   273     textedit.getCursorPosition( &para, &index );
       
   274     QCOMPARE( para, 0 );
       
   275     QCOMPARE( index, 0 );
       
   276 
       
   277     textedit.moveCursor( Q3TextEdit::MoveForward, FALSE );
       
   278     textedit.getCursorPosition( &para, &index );
       
   279     QCOMPARE( para, 0 );
       
   280     QCOMPARE( index, 1 );
       
   281 
       
   282     textedit.moveCursor( Q3TextEdit::MoveWordForward, FALSE );
       
   283     textedit.getCursorPosition( &para, &index );
       
   284     QCOMPARE( para, 0 );
       
   285     QCOMPARE( index, 5 );
       
   286 
       
   287     textedit.moveCursor( Q3TextEdit::MoveDown, FALSE );
       
   288     textedit.moveCursor( Q3TextEdit::MoveDown, FALSE );
       
   289     textedit.getCursorPosition( &para, &index );
       
   290     QCOMPARE( para, 2 );
       
   291     QCOMPARE( index, 5 );
       
   292 
       
   293     textedit.moveCursor( Q3TextEdit::MoveLineStart, FALSE );
       
   294     textedit.getCursorPosition( &para, &index );
       
   295     QCOMPARE( para, 2 );
       
   296     QCOMPARE( index, 0 );
       
   297 
       
   298     textedit.moveCursor( Q3TextEdit::MoveLineEnd, FALSE );
       
   299     textedit.getCursorPosition( &para, &index );
       
   300     QCOMPARE( para, 2 );
       
   301     QCOMPARE( index, 14 );
       
   302 
       
   303     textedit.moveCursor( Q3TextEdit::MoveUp, FALSE );
       
   304     textedit.getCursorPosition( &para, &index );
       
   305     QCOMPARE( para, 1 );
       
   306     QCOMPARE( index, 14 );
       
   307 }
       
   308 
       
   309 void tst_Q3RichText::keyPressEvent()
       
   310 {
       
   311     // Still needs to test Key_Prior and Key_Next
       
   312 
       
   313     int para, index;
       
   314 
       
   315     Q3TextEdit textedit;
       
   316     textedit.show();
       
   317     textedit.setText( "This is a test" );
       
   318 
       
   319     qWarning( "Consider using QtTestCase::keyEvent() for sending key events" );
       
   320     QKeyEvent ke( QEvent::KeyPress, Qt::Key_Right, 0, Qt::NoButton );
       
   321     QApplication::sendEvent( textedit.viewport(), &ke );
       
   322     textedit.getCursorPosition( &para, &index );
       
   323     QCOMPARE( para, 0 );
       
   324     QCOMPARE( index, 1 );
       
   325 
       
   326     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Right, 0, Qt::ControlModifier );
       
   327     QApplication::sendEvent( textedit.viewport(), &ke );
       
   328     textedit.getCursorPosition( &para, &index );
       
   329     QCOMPARE( para, 0 );
       
   330     QCOMPARE( index, 5 );
       
   331 
       
   332     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Left, 0, Qt::NoButton );
       
   333     QApplication::sendEvent( textedit.viewport(), &ke );
       
   334     textedit.getCursorPosition( &para, &index );
       
   335     QCOMPARE( para, 0 );
       
   336     QCOMPARE( index, 4 );
       
   337 
       
   338     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Left, 0, Qt::ControlModifier );
       
   339     QApplication::sendEvent( textedit.viewport(), &ke );
       
   340     textedit.getCursorPosition( &para, &index );
       
   341     QCOMPARE( para, 0 );
       
   342     QCOMPARE( index, 0 );
       
   343 
       
   344     // Test that the text is removed when Enter/Return is pressed first
       
   345     textedit.selectAll();
       
   346     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Enter, 0, Qt::NoButton );
       
   347     QApplication::sendEvent( textedit.viewport(), &ke );
       
   348     QCOMPARE( textedit.text(), QString("\n") );
       
   349 
       
   350     textedit.setText( "This is a test" );
       
   351     textedit.selectAll();
       
   352     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Return, 0, Qt::NoButton );
       
   353     QApplication::sendEvent( textedit.viewport(), &ke );
       
   354     QCOMPARE( textedit.text(), QString("\n") );
       
   355 
       
   356     // Now test if the line-break is added in rich text mode
       
   357     textedit.setTextFormat( Qt::RichText );
       
   358 
       
   359     textedit.setText( "This is a test" );
       
   360     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Enter, 0, Qt::ControlModifier );
       
   361     QApplication::sendEvent( textedit.viewport(), &ke );
       
   362     QVERIFY( textedit.text().contains( "<br />" ) );
       
   363 
       
   364     textedit.setText( "This is a test" );
       
   365     textedit.moveCursor( Q3TextEdit::MoveLineEnd, FALSE );
       
   366     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Return, 0, Qt::ControlModifier );
       
   367     QApplication::sendEvent( textedit.viewport(), &ke );
       
   368     QVERIFY( textedit.text().contains( "<br />" ) );
       
   369 
       
   370     textedit.setText( "This is a test" );
       
   371     textedit.moveCursor( Q3TextEdit::MoveWordForward, FALSE );
       
   372     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Return, 0, Qt::NoButton );
       
   373     QApplication::sendEvent( textedit.viewport(), &ke );
       
   374     QString es = QString::fromLatin1("<p dir=\"ltr\">");
       
   375     QVERIFY( textedit.text().count( es ) == 2 );
       
   376 
       
   377     textedit.setTextFormat( Qt::AutoText );
       
   378 
       
   379     textedit.setText( "This is a test" );
       
   380     QApplication::clipboard()->setText("");
       
   381     textedit.selectAll();
       
   382     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Delete, 0, Qt::NoButton );
       
   383     QApplication::sendEvent( textedit.viewport(), &ke );
       
   384     QCOMPARE( textedit.text(), QString("") );
       
   385 
       
   386 #if defined (Q_WS_WIN)
       
   387     textedit.setText( "This is a test" );
       
   388     QApplication::clipboard()->setText("");
       
   389     textedit.selectAll();
       
   390     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Delete, 0, Qt::ShiftModifier );
       
   391     QApplication::sendEvent( textedit.viewport(), &ke );
       
   392     QCOMPARE( textedit.text(), QString("") );
       
   393     QCOMPARE( QApplication::clipboard()->text(), QString("This is a test") );
       
   394 #endif
       
   395 
       
   396     textedit.setText( "This is a test" );
       
   397     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Delete, 0, Qt::NoButton );
       
   398     QApplication::sendEvent( textedit.viewport(), &ke );
       
   399     QCOMPARE( textedit.text(), QString("his is a test") );
       
   400 
       
   401     bool nativeClipboardWorking = true;
       
   402 #if defined (Q_WS_MAC)
       
   403     PasteboardRef pasteboard;
       
   404     OSStatus status = PasteboardCreate(0, &pasteboard);
       
   405     if (status == noErr)
       
   406         CFRelease(pasteboard);
       
   407     nativeClipboardWorking = status == noErr;
       
   408 #endif
       
   409 
       
   410     if (nativeClipboardWorking) {
       
   411         textedit.setText( "This is a test" );
       
   412         QApplication::clipboard()->setText(" and this is another test");
       
   413         textedit.moveCursor( Q3TextEdit::MoveLineEnd, FALSE );
       
   414         ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Insert, 0, Qt::ShiftModifier );
       
   415         QApplication::sendEvent( textedit.viewport(), &ke );
       
   416         QCOMPARE( textedit.text(), QString("This is a test and this is another test") );
       
   417     }
       
   418 
       
   419 #if defined (Q_WS_WIN)
       
   420     textedit.setText( "This is a test" );
       
   421     QApplication::clipboard()->setText("");
       
   422     textedit.selectAll();
       
   423     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Insert, 0, Qt::ControlModifier );
       
   424     QApplication::sendEvent( textedit.viewport(), &ke );
       
   425     QCOMPARE( QApplication::clipboard()->text(), QString("This is a test") );
       
   426 #endif
       
   427 
       
   428     textedit.setText( "This is a test" );
       
   429     textedit.selectAll();
       
   430     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Backspace, 0, Qt::NoButton );
       
   431     QApplication::sendEvent( textedit.viewport(), &ke );
       
   432     QCOMPARE( textedit.text(), QString("") );
       
   433 
       
   434     textedit.setText( "This is a test" );
       
   435     textedit.moveCursor( Q3TextEdit::MoveLineEnd, FALSE );
       
   436     ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Backspace, 0, Qt::NoButton );
       
   437     QApplication::sendEvent( textedit.viewport(), &ke );
       
   438     QCOMPARE( textedit.text(), QString( "This is a tes" ) );
       
   439 
       
   440     if (nativeClipboardWorking) {
       
   441         textedit.setText( "This is a test" );
       
   442         QApplication::clipboard()->setText("");
       
   443         textedit.selectAll();
       
   444         ke = QKeyEvent( QEvent::KeyPress, Qt::Key_F16, 0, Qt::NoButton );
       
   445         QApplication::sendEvent( textedit.viewport(), &ke );
       
   446         QCOMPARE( QApplication::clipboard()->text(), QString("This is a test") );
       
   447 
       
   448         textedit.setText( "This is a test" );
       
   449         textedit.moveCursor( Q3TextEdit::MoveLineEnd, FALSE );
       
   450         QApplication::clipboard()->setText(" and this is another test");
       
   451         ke = QKeyEvent( QEvent::KeyPress, Qt::Key_F18, 0, Qt::NoButton );
       
   452         QApplication::sendEvent( textedit.viewport(), &ke );
       
   453         QCOMPARE( textedit.text(), QString("This is a test and this is another test") );
       
   454 
       
   455         textedit.setText( "This is a test" );
       
   456         QApplication::clipboard()->setText("");
       
   457         textedit.selectAll();
       
   458         ke = QKeyEvent( QEvent::KeyPress, Qt::Key_F20, 0, Qt::NoButton );
       
   459         QApplication::sendEvent( textedit.viewport(), &ke );
       
   460         QCOMPARE( textedit.text(), QString("") );
       
   461         QCOMPARE( QApplication::clipboard()->text(), QString("This is a test") );
       
   462     }
       
   463 }
       
   464 
       
   465 QTEST_MAIN(tst_Q3RichText)
       
   466 #include "tst_q3richtext.moc"
       
   467