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") );
    }
}
void tst_Q3RichText::moveCursor()
{
    // Still needs to test for MovePageUp and MovePageDown

    int para, index;

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

    textedit.moveCursor( Q3TextEdit::MoveEnd, FALSE );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 0 );
    QCOMPARE( index, 14 );

    textedit.moveCursor( Q3TextEdit::MoveBackward, FALSE );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 0 );
    QCOMPARE( index, 13 );

    textedit.moveCursor( Q3TextEdit::MoveWordBackward, FALSE );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 0 );
    QCOMPARE( index, 10 );

    textedit.moveCursor( Q3TextEdit::MoveHome, FALSE );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 0 );
    QCOMPARE( index, 0 );

    textedit.moveCursor( Q3TextEdit::MoveForward, FALSE );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 0 );
    QCOMPARE( index, 1 );

    textedit.moveCursor( Q3TextEdit::MoveWordForward, FALSE );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 0 );
    QCOMPARE( index, 5 );

    textedit.moveCursor( Q3TextEdit::MoveLineStart, FALSE );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 0 );
    QCOMPARE( index, 0 );

    textedit.moveCursor( Q3TextEdit::MoveLineEnd, FALSE );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 0 );
    QCOMPARE( index, 14 );

    textedit.setText( "This is a test\nThis is a test\nThis is a test" );

    textedit.moveCursor( Q3TextEdit::MoveEnd, FALSE );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 2 );
    QCOMPARE( index, 14 );

    textedit.moveCursor( Q3TextEdit::MoveBackward, FALSE );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 2 );
    QCOMPARE( index, 13 );

    textedit.moveCursor( Q3TextEdit::MoveWordBackward, FALSE );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 2 );
    QCOMPARE( index, 10 );

    textedit.moveCursor( Q3TextEdit::MoveHome, FALSE );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 0 );
    QCOMPARE( index, 0 );

    textedit.moveCursor( Q3TextEdit::MoveForward, FALSE );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 0 );
    QCOMPARE( index, 1 );

    textedit.moveCursor( Q3TextEdit::MoveWordForward, FALSE );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 0 );
    QCOMPARE( index, 5 );

    textedit.moveCursor( Q3TextEdit::MoveDown, FALSE );
    textedit.moveCursor( Q3TextEdit::MoveDown, FALSE );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 2 );
    QCOMPARE( index, 5 );

    textedit.moveCursor( Q3TextEdit::MoveLineStart, FALSE );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 2 );
    QCOMPARE( index, 0 );

    textedit.moveCursor( Q3TextEdit::MoveLineEnd, FALSE );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 2 );
    QCOMPARE( index, 14 );

    textedit.moveCursor( Q3TextEdit::MoveUp, FALSE );
    textedit.getCursorPosition( &para, &index );
    QCOMPARE( para, 1 );
    QCOMPARE( index, 14 );
}