diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp b/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp index 80e8335091..798db9fe23 100644 --- a/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp +++ b/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp @@ -182,6 +182,8 @@ private slots: void getText(); void getFormattedText_data(); void getFormattedText(); + void append_data(); + void append(); void insert_data(); void insert(); void remove_data(); @@ -3827,6 +3829,158 @@ void tst_qquicktextedit::getFormattedText() } } +void tst_qquicktextedit::append_data() +{ + QTest::addColumn<QString>("text"); + QTest::addColumn<QQuickTextEdit::TextFormat>("textFormat"); + QTest::addColumn<int>("selectionStart"); + QTest::addColumn<int>("selectionEnd"); + QTest::addColumn<QString>("appendText"); + QTest::addColumn<QString>("expectedText"); + QTest::addColumn<int>("expectedSelectionStart"); + QTest::addColumn<int>("expectedSelectionEnd"); + QTest::addColumn<int>("expectedCursorPosition"); + QTest::addColumn<bool>("selectionChanged"); + QTest::addColumn<bool>("cursorPositionChanged"); + + QTest::newRow("cursor kept intact (beginning)") + << standard.at(0) << QQuickTextEdit::PlainText + << 0 << 0 + << QString("Hello") + << standard.at(0) + QString("\nHello") + << 0 << 0 << 0 + << false << false; + + QTest::newRow("cursor kept intact (middle)") + << standard.at(0) << QQuickTextEdit::PlainText + << 18 << 18 + << QString("Hello") + << standard.at(0) + QString("\nHello") + << 18 << 18 << 18 + << false << false; + + QTest::newRow("cursor follows (end)") + << standard.at(0) << QQuickTextEdit::PlainText + << standard.at(0).length() << standard.at(0).length() + << QString("Hello") + << standard.at(0) + QString("\nHello") + << standard.at(0).length() + 6 << standard.at(0).length() + 6 << standard.at(0).length() + 6 + << false << true; + + QTest::newRow("selection kept intact (beginning)") + << standard.at(0) << QQuickTextEdit::PlainText + << 0 << 18 + << QString("Hello") + << standard.at(0) + QString("\nHello") + << 0 << 18 << 18 + << false << false; + + QTest::newRow("selection kept intact (middle)") + << standard.at(0) << QQuickTextEdit::PlainText + << 14 << 18 + << QString("Hello") + << standard.at(0) + QString("\nHello") + << 14 << 18 << 18 + << false << false; + + QTest::newRow("selection kept intact, cursor follows (end)") + << standard.at(0) << QQuickTextEdit::PlainText + << 18 << standard.at(0).length() + << QString("Hello") + << standard.at(0) + QString("\nHello") + << 18 << standard.at(0).length() + 6 << standard.at(0).length() + 6 + << false << true; + + QTest::newRow("reversed selection kept intact") + << standard.at(0) << QQuickTextEdit::PlainText + << 18 << 14 + << QString("Hello") + << standard.at(0) + QString("\nHello") + << 14 << 18 << 14 + << false << false; + + QTest::newRow("rich text into plain text") + << standard.at(0) << QQuickTextEdit::PlainText + << 0 << 0 + << QString("<b>Hello</b>") + << standard.at(0) + QString("\n<b>Hello</b>") + << 0 << 0 << 0 + << false << false; + + QTest::newRow("rich text into rich text") + << standard.at(0) << QQuickTextEdit::RichText + << 0 << 0 + << QString("<b>Hello</b>") + << standard.at(0) + QChar(QChar::ParagraphSeparator) + QString("Hello") + << 0 << 0 << 0 + << false << false; + + QTest::newRow("rich text into auto text") + << standard.at(0) << QQuickTextEdit::AutoText + << 0 << 0 + << QString("<b>Hello</b>") + << standard.at(0) + QString("\nHello") + << 0 << 0 << 0 + << false << false; +} + +void tst_qquicktextedit::append() +{ + QFETCH(QString, text); + QFETCH(QQuickTextEdit::TextFormat, textFormat); + QFETCH(int, selectionStart); + QFETCH(int, selectionEnd); + QFETCH(QString, appendText); + QFETCH(QString, expectedText); + QFETCH(int, expectedSelectionStart); + QFETCH(int, expectedSelectionEnd); + QFETCH(int, expectedCursorPosition); + QFETCH(bool, selectionChanged); + QFETCH(bool, cursorPositionChanged); + + QString componentStr = "import QtQuick 2.2\nTextEdit { text: \"" + text + "\" }"; + QQmlComponent textEditComponent(&engine); + textEditComponent.setData(componentStr.toLatin1(), QUrl()); + QQuickTextEdit *textEdit = qobject_cast<QQuickTextEdit*>(textEditComponent.create()); + QVERIFY(textEdit != 0); + + textEdit->setTextFormat(textFormat); + textEdit->select(selectionStart, selectionEnd); + + QSignalSpy selectionSpy(textEdit, SIGNAL(selectedTextChanged())); + QSignalSpy selectionStartSpy(textEdit, SIGNAL(selectionStartChanged())); + QSignalSpy selectionEndSpy(textEdit, SIGNAL(selectionEndChanged())); + QSignalSpy textSpy(textEdit, SIGNAL(textChanged())); + QSignalSpy cursorPositionSpy(textEdit, SIGNAL(cursorPositionChanged())); + + textEdit->append(appendText); + + if (textFormat == QQuickTextEdit::RichText || (textFormat == QQuickTextEdit::AutoText && ( + Qt::mightBeRichText(text) || Qt::mightBeRichText(appendText)))) { + QCOMPARE(textEdit->getText(0, expectedText.length()), expectedText); + } else { + QCOMPARE(textEdit->text(), expectedText); + + } + QCOMPARE(textEdit->length(), expectedText.length()); + + QCOMPARE(textEdit->selectionStart(), expectedSelectionStart); + QCOMPARE(textEdit->selectionEnd(), expectedSelectionEnd); + QCOMPARE(textEdit->cursorPosition(), expectedCursorPosition); + + if (selectionStart > selectionEnd) + qSwap(selectionStart, selectionEnd); + + QEXPECT_FAIL("into selection", "selectedTextChanged signal isn't emitted on edits within selection", Continue); + QEXPECT_FAIL("into reversed selection", "selectedTextChanged signal isn't emitted on edits within selection", Continue); + QCOMPARE(selectionSpy.count() > 0, selectionChanged); + QCOMPARE(selectionStartSpy.count() > 0, selectionStart != expectedSelectionStart); + QEXPECT_FAIL("into reversed selection", "selectionEndChanged signal not emitted", Continue); + QCOMPARE(selectionEndSpy.count() > 0, selectionEnd != expectedSelectionEnd); + QCOMPARE(textSpy.count() > 0, text != expectedText); + QCOMPARE(cursorPositionSpy.count() > 0, cursorPositionChanged); +} + void tst_qquicktextedit::insert_data() { QTest::addColumn<QString>("text"); |