void tst_Q3RichText::richtext()
{
    Q3TextEdit* textedit = new Q3TextEdit();
    QString result( "<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body style=\"font-size:%1;font-family:%2\">\n"
	    "<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"
	    "</body></html>\n" );
    QFont font = textedit->font();
    result = result.arg( QString::number(font.pointSize()) + "pt" ).arg( font.family() ).arg( "" ).arg( "" ).arg( "" );

    textedit->setTextFormat( Qt::RichText );

    QKeyEvent ke( QEvent::KeyPress, Qt::Key_K, 107, Qt::NoButton, "k" );
    QApplication::sendEvent( textedit->viewport(), &ke );

    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Space, 32, Qt::NoButton, " " );
    QApplication::sendEvent( textedit->viewport(), &ke );

    textedit->setCurrentFont( QFont( "Arial", 20 ) );

    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Space, 32, Qt::NoButton, " " );
    QApplication::sendEvent( textedit->viewport(), &ke );

    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Space, 32, Qt::NoButton, " " );
    QApplication::sendEvent( textedit->viewport(), &ke );

    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Space, 32, Qt::NoButton, " " );
    QApplication::sendEvent( textedit->viewport(), &ke );

    textedit->setCurrentFont( QFont( "Arial", 15 ) );

    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Space, 32, Qt::NoButton, " " );
    QApplication::sendEvent( textedit->viewport(), &ke );

    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Space, 32, Qt::NoButton, " " );
    QApplication::sendEvent( textedit->viewport(), &ke );

    textedit->setCurrentFont( QFont( "Arial", 20 ) );

    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_K, 107, Qt::NoButton, "k" );
    QApplication::sendEvent( textedit->viewport(), &ke );

    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Space, 32, Qt::NoButton, " " );
    QApplication::sendEvent( textedit->viewport(), &ke );

    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_K, 107, Qt::NoButton, "k" );
    QApplication::sendEvent( textedit->viewport(), &ke );

    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Space, 32, Qt::NoButton, " " );
    QApplication::sendEvent( textedit->viewport(), &ke );

    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Space, 32, Qt::NoButton, " " );
    QApplication::sendEvent( textedit->viewport(), &ke );

    QCOMPARE( textedit->text(), result );

    delete textedit;
}
void tst_Q3RichText::keyPressEvent()
{
    // Still needs to test Key_Prior and Key_Next

    int para, index;

    Q3TextEdit textedit;
    textedit.show();
    textedit.setText( "This is a test" );

    qWarning( "Consider using QtTestCase::keyEvent() for sending key events" );
    QKeyEvent ke( QEvent::KeyPress, Qt::Key_Right, 0, Qt::NoButton );
    QApplication::sendEvent( textedit.viewport(), &ke );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 0 );
    QCOMPARE( index, 1 );

    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Right, 0, Qt::ControlModifier );
    QApplication::sendEvent( textedit.viewport(), &ke );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 0 );
    QCOMPARE( index, 5 );

    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Left, 0, Qt::NoButton );
    QApplication::sendEvent( textedit.viewport(), &ke );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 0 );
    QCOMPARE( index, 4 );

    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Left, 0, Qt::ControlModifier );
    QApplication::sendEvent( textedit.viewport(), &ke );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 0 );
    QCOMPARE( index, 0 );

    // Test that the text is removed when Enter/Return is pressed first
    textedit.selectAll();
    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Enter, 0, Qt::NoButton );
    QApplication::sendEvent( textedit.viewport(), &ke );
    QCOMPARE( textedit.text(), QString("\n") );

    textedit.setText( "This is a test" );
    textedit.selectAll();
    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Return, 0, Qt::NoButton );
    QApplication::sendEvent( textedit.viewport(), &ke );
    QCOMPARE( textedit.text(), QString("\n") );

    // Now test if the line-break is added in rich text mode
    textedit.setTextFormat( Qt::RichText );

    textedit.setText( "This is a test" );
    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Enter, 0, Qt::ControlModifier );
    QApplication::sendEvent( textedit.viewport(), &ke );
    QVERIFY( textedit.text().contains( "<br />" ) );

    textedit.setText( "This is a test" );
    textedit.moveCursor( Q3TextEdit::MoveLineEnd, FALSE );
    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Return, 0, Qt::ControlModifier );
    QApplication::sendEvent( textedit.viewport(), &ke );
    QVERIFY( textedit.text().contains( "<br />" ) );

    textedit.setText( "This is a test" );
    textedit.moveCursor( Q3TextEdit::MoveWordForward, FALSE );
    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Return, 0, Qt::NoButton );
    QApplication::sendEvent( textedit.viewport(), &ke );
    QString es = QString::fromLatin1("<p dir=\"ltr\">");
    QVERIFY( textedit.text().count( es ) == 2 );

    textedit.setTextFormat( Qt::AutoText );

    textedit.setText( "This is a test" );
    QApplication::clipboard()->setText("");
    textedit.selectAll();
    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Delete, 0, Qt::NoButton );
    QApplication::sendEvent( textedit.viewport(), &ke );
    QCOMPARE( textedit.text(), QString("") );

#if defined (Q_WS_WIN)
    textedit.setText( "This is a test" );
    QApplication::clipboard()->setText("");
    textedit.selectAll();
    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Delete, 0, Qt::ShiftModifier );
    QApplication::sendEvent( textedit.viewport(), &ke );
    QCOMPARE( textedit.text(), QString("") );
    QCOMPARE( QApplication::clipboard()->text(), QString("This is a test") );
#endif

    textedit.setText( "This is a test" );
    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Delete, 0, Qt::NoButton );
    QApplication::sendEvent( textedit.viewport(), &ke );
    QCOMPARE( textedit.text(), QString("his is a test") );

    bool nativeClipboardWorking = true;
#if defined (Q_WS_MAC)
    PasteboardRef pasteboard;
    OSStatus status = PasteboardCreate(0, &pasteboard);
    if (status == noErr)
        CFRelease(pasteboard);
    nativeClipboardWorking = status == noErr;
#endif

    if (nativeClipboardWorking) {
        textedit.setText( "This is a test" );
        QApplication::clipboard()->setText(" and this is another test");
        textedit.moveCursor( Q3TextEdit::MoveLineEnd, FALSE );
        ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Insert, 0, Qt::ShiftModifier );
        QApplication::sendEvent( textedit.viewport(), &ke );
        QCOMPARE( textedit.text(), QString("This is a test and this is another test") );
    }

#if defined (Q_WS_WIN)
    textedit.setText( "This is a test" );
    QApplication::clipboard()->setText("");
    textedit.selectAll();
    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Insert, 0, Qt::ControlModifier );
    QApplication::sendEvent( textedit.viewport(), &ke );
    QCOMPARE( QApplication::clipboard()->text(), QString("This is a test") );
#endif

    textedit.setText( "This is a test" );
    textedit.selectAll();
    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Backspace, 0, Qt::NoButton );
    QApplication::sendEvent( textedit.viewport(), &ke );
    QCOMPARE( textedit.text(), QString("") );

    textedit.setText( "This is a test" );
    textedit.moveCursor( Q3TextEdit::MoveLineEnd, FALSE );
    ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Backspace, 0, Qt::NoButton );
    QApplication::sendEvent( textedit.viewport(), &ke );
    QCOMPARE( textedit.text(), QString( "This is a tes" ) );

    if (nativeClipboardWorking) {
        textedit.setText( "This is a test" );
        QApplication::clipboard()->setText("");
        textedit.selectAll();
        ke = QKeyEvent( QEvent::KeyPress, Qt::Key_F16, 0, Qt::NoButton );
        QApplication::sendEvent( textedit.viewport(), &ke );
        QCOMPARE( QApplication::clipboard()->text(), QString("This is a test") );

        textedit.setText( "This is a test" );
        textedit.moveCursor( Q3TextEdit::MoveLineEnd, FALSE );
        QApplication::clipboard()->setText(" and this is another test");
        ke = QKeyEvent( QEvent::KeyPress, Qt::Key_F18, 0, Qt::NoButton );
        QApplication::sendEvent( textedit.viewport(), &ke );
        QCOMPARE( textedit.text(), QString("This is a test and this is another test") );

        textedit.setText( "This is a test" );
        QApplication::clipboard()->setText("");
        textedit.selectAll();
        ke = QKeyEvent( QEvent::KeyPress, Qt::Key_F20, 0, Qt::NoButton );
        QApplication::sendEvent( textedit.viewport(), &ke );
        QCOMPARE( textedit.text(), QString("") );
        QCOMPARE( QApplication::clipboard()->text(), QString("This is a test") );
    }
}