diff options
Diffstat (limited to 'tests/auto/widgets/widgets/qtextedit')
-rw-r--r-- | tests/auto/widgets/widgets/qtextedit/CMakeLists.txt | 13 | ||||
-rw-r--r-- | tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp | 213 |
2 files changed, 159 insertions, 67 deletions
diff --git a/tests/auto/widgets/widgets/qtextedit/CMakeLists.txt b/tests/auto/widgets/widgets/qtextedit/CMakeLists.txt index e90b59ccb8..e406e088ca 100644 --- a/tests/auto/widgets/widgets/qtextedit/CMakeLists.txt +++ b/tests/auto/widgets/widgets/qtextedit/CMakeLists.txt @@ -1,16 +1,23 @@ -# Generated from qtextedit.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## tst_qtextedit Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qtextedit LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + # Collect test data list(APPEND test_data "fullWidthSelection") qt_internal_add_test(tst_qtextedit SOURCES tst_qtextedit.cpp - PUBLIC_LIBRARIES + LIBRARIES Qt::CorePrivate Qt::Gui Qt::GuiPrivate @@ -23,6 +30,6 @@ qt_internal_add_test(tst_qtextedit ##################################################################### qt_internal_extend_target(tst_qtextedit CONDITION MACOS - PUBLIC_LIBRARIES + LIBRARIES ${FWAppKit} ) diff --git a/tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp b/tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp index 9126b87472..ddf2bcfa85 100644 --- a/tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp +++ b/tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> @@ -58,6 +33,8 @@ #include "../../../shared/platforminputcontext.h" #include <private/qinputmethod_p.h> +#include <QtWidgets/private/qapplication_p.h> + Q_LOGGING_CATEGORY(lcTests, "qt.widgets.tests") //Used in copyAvailable @@ -139,7 +116,13 @@ private slots: void moveCursor(); #ifndef QT_NO_CLIPBOARD void mimeDataReimplementations(); +#ifndef QT_NO_TEXTHTMLPARSER + void mimeTypesAvailableFromRichText(); +#endif +#if QT_CONFIG(textmarkdownreader) + void mimeTypesAvailableFromMarkdown(); #endif +#endif // QT_NO_CLIPBOARD void ctrlEnterShouldInsertLineSeparator_NOT(); void shiftEnterShouldInsertLineSeparator(); void selectWordsFromStringsContainingSeparators_data(); @@ -175,6 +158,7 @@ private slots: void setDocumentPreservesPalette(); #endif void pasteFromQt3RichText(); + void pasteFromMarkdown(); void noWrapBackgrounds(); void preserveCharFormatAfterUnchangingSetPosition(); void twoSameInputMethodEvents(); @@ -214,10 +198,13 @@ private slots: void nextFormatAfterEnterPressed_data(); void nextFormatAfterEnterPressed(); + void dontCrashWithCss(); + private: void createSelection(); int blockCount() const; void compareWidgetAndImage(QTextEdit &widget, const QString &imageFileName); + bool isMainFontFixed(); QTextEdit *ed; qreal rootFrameMargin; @@ -534,9 +521,9 @@ void tst_QTextEdit::clearShouldClearExtraSelections() sel.cursor = ed->textCursor(); sel.format.setProperty(QTextFormat::FullWidthSelection, true); ed->setExtraSelections(QList<QTextEdit::ExtraSelection>() << sel); - QCOMPARE(ed->extraSelections().count(), 1); + QCOMPARE(ed->extraSelections().size(), 1); ed->clear(); - QCOMPARE(ed->extraSelections().count(), 0); + QCOMPARE(ed->extraSelections().size(), 0); } void tst_QTextEdit::paragSeparatorOnPlaintextAppend() @@ -736,7 +723,7 @@ void tst_QTextEdit::cursorPositionChanged() spy.clear(); QTest::keyClick(ed, Qt::Key_A); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); QTextCursor cursor = ed->textCursor(); cursor.movePosition(QTextCursor::Start); @@ -744,18 +731,18 @@ void tst_QTextEdit::cursorPositionChanged() cursor.movePosition(QTextCursor::End); spy.clear(); cursor.insertText("Test"); - QCOMPARE(spy.count(), 0); + QCOMPARE(spy.size(), 0); cursor.movePosition(QTextCursor::End); ed->setTextCursor(cursor); cursor.movePosition(QTextCursor::Start); spy.clear(); cursor.insertText("Test"); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); spy.clear(); QTest::keyClick(ed, Qt::Key_Left); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); cursor.movePosition(QTextCursor::Start); ed->setTextCursor(cursor); @@ -764,14 +751,19 @@ void tst_QTextEdit::cursorPositionChanged() QTest::mouseDClick(ed->viewport(), Qt::LeftButton, {}, ed->cursorRect().center()); QVERIFY(ed->textCursor().hasSelection()); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); CursorPositionChangedRecorder spy2(ed); QVERIFY(ed->textCursor().position() > 0); ed->setPlainText("Hello World"); - QCOMPARE(spy2.cursorPositions.count(), 1); + QCOMPARE(spy2.cursorPositions.size(), 1); QCOMPARE(spy2.cursorPositions.at(0), 0); QCOMPARE(ed->textCursor().position(), 0); + + ed->selectAll(); + QCOMPARE(spy2.cursorPositions.size(), 2); + QCOMPARE(spy2.cursorPositions.at(1), 11); + QCOMPARE(ed->textCursor().position(), 11); } void tst_QTextEdit::setTextCursor() @@ -786,7 +778,7 @@ void tst_QTextEdit::setTextCursor() spy.clear(); ed->setTextCursor(cursor); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); } #ifndef QT_NO_CLIPBOARD @@ -803,7 +795,7 @@ void tst_QTextEdit::undoAvailableAfterPaste() const QString txt("Test"); QApplication::clipboard()->setText(txt); ed->paste(); - QVERIFY(spy.count() >= 1); + QVERIFY(spy.size() >= 1); QCOMPARE(ed->toPlainText(), txt); } #endif @@ -1056,7 +1048,7 @@ void tst_QTextEdit::noPropertiesOnDefaultTextEditCharFormat() // on a text edit. Font properties instead should be taken from the // widget's font (in sync with defaultFont property in document) and the // foreground color should be taken from the palette. - QCOMPARE(ed->currentCharFormat().properties().count(), 0); + QCOMPARE(ed->currentCharFormat().properties().size(), 0); } void tst_QTextEdit::setPlainTextShouldUseCurrentCharFormat() @@ -1078,9 +1070,9 @@ void tst_QTextEdit::setPlainTextShouldEmitTextChangedOnce() { QSignalSpy spy(ed, SIGNAL(textChanged())); ed->setPlainText("Yankee Doodle"); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); ed->setPlainText(""); - QCOMPARE(spy.count(), 2); + QCOMPARE(spy.size(), 2); } void tst_QTextEdit::overwriteMode() @@ -1362,7 +1354,7 @@ void tst_QTextEdit::copyAvailable_data() //Tests the copyAvailable slot for several cases void tst_QTextEdit::copyAvailable() { - QFETCH(pairListType,keystrokes); + QFETCH(const pairListType, keystrokes); QFETCH(QList<bool>, copyAvailable); QFETCH(QString, function); @@ -1375,9 +1367,8 @@ void tst_QTextEdit::copyAvailable() QSignalSpy spyCopyAvailabe(ed, SIGNAL(copyAvailable(bool))); //Execute Keystrokes - foreach(keyPairType keyPair, keystrokes) { + for (keyPairType keyPair : keystrokes) QTest::keyClick(ed, keyPair.first, keyPair.second ); - } //Execute ed->"function" if (function == "cut") @@ -1394,8 +1385,8 @@ void tst_QTextEdit::copyAvailable() //Compare spied signals QEXPECT_FAIL("Case7 T,A,A, <- + shift, <- + shift, <- + shift, ctrl + x, undo() | signals: true, false, true", "Wrong undo selection behaviour. Should be fixed in some future release. (See task: 132482)", Abort); - QCOMPARE(spyCopyAvailabe.count(), copyAvailable.count()); - for (int i=0;i<spyCopyAvailabe.count(); i++) { + QCOMPARE(spyCopyAvailabe.size(), copyAvailable.size()); + for (int i=0;i<spyCopyAvailabe.size(); i++) { QVariant variantSpyCopyAvailable = spyCopyAvailabe.at(i).at(0); QVERIFY2(variantSpyCopyAvailable.toBool() == copyAvailable.at(i), QString("Spied singnal: %1").arg(i).toLatin1()); } @@ -1431,10 +1422,10 @@ void tst_QTextEdit::moveCursor() QCOMPARE(ed->textCursor().position(), 0); ed->moveCursor(QTextCursor::NextCharacter); QCOMPARE(ed->textCursor().position(), 1); - QCOMPARE(cursorMovedSpy.count(), 1); + QCOMPARE(cursorMovedSpy.size(), 1); ed->moveCursor(QTextCursor::NextCharacter, QTextCursor::KeepAnchor); QCOMPARE(ed->textCursor().position(), 2); - QCOMPARE(cursorMovedSpy.count(), 2); + QCOMPARE(cursorMovedSpy.size(), 2); QCOMPARE(ed->textCursor().selectedText(), QString("e")); } @@ -1505,7 +1496,63 @@ void tst_QTextEdit::mimeDataReimplementations() QCOMPARE(ed.insertCallCount, 1); #endif } + +#ifndef QT_NO_TEXTHTMLPARSER +void tst_QTextEdit::mimeTypesAvailableFromRichText() +{ + MyTextEdit ed; + ed.setHtml("<i>Hello <b>World</b></i>"); + ed.selectAll(); + ed.copy(); + const auto *mimeData = QApplication::clipboard()->mimeData(); + qCDebug(lcTests) << "available mime types" << mimeData->formats(); + QVERIFY(mimeData->formats().contains("text/plain")); +#if QT_CONFIG(textmarkdownwriter) + QVERIFY(mimeData->formats().contains("text/markdown")); + const QByteArray expectedMarkdown = "*Hello **World***\n\n"; + if (mimeData->data("text/markdown") != expectedMarkdown && isMainFontFixed()) + QEXPECT_FAIL("", "fixed-pitch main font (QTBUG-103484)", Continue); + QCOMPARE(mimeData->data("text/markdown"), expectedMarkdown); +#endif +#ifndef QT_NO_TEXTHTMLPARSER + QVERIFY(mimeData->formats().contains("text/html")); + QVERIFY(mimeData->hasHtml()); +#endif +#ifndef QT_NO_TEXTODFWRITER + QVERIFY(mimeData->formats().contains("application/vnd.oasis.opendocument.text")); +#endif +} +#endif // QT_NO_TEXTHTMLPARSER + +#if QT_CONFIG(textmarkdownreader) +void tst_QTextEdit::mimeTypesAvailableFromMarkdown() +{ + MyTextEdit ed; + const QString md("# TODO\n\n- [x] Fix bugs\n- [ ] Have a beer\n"); + ed.setMarkdown(md); + ed.selectAll(); + ed.copy(); + const auto *mimeData = QApplication::clipboard()->mimeData(); + qCDebug(lcTests) << "available mime types" << mimeData->formats(); + QVERIFY(mimeData->formats().contains("text/plain")); +#if QT_CONFIG(textmarkdownwriter) + QVERIFY(mimeData->formats().contains("text/markdown")); + if (mimeData->data("text/markdown") != md && isMainFontFixed()) + QEXPECT_FAIL("", "fixed-pitch main font (QTBUG-103484)", Continue); + QCOMPARE(mimeData->data("text/markdown"), md); +#endif +#ifndef QT_NO_TEXTHTMLPARSER + QVERIFY(mimeData->formats().contains("text/html")); + QVERIFY(mimeData->hasHtml()); + QVERIFY(mimeData->html().contains("checked")); // <li class=\"checked\" ... #endif +#ifndef QT_NO_TEXTODFWRITER + QVERIFY(mimeData->formats().contains("application/vnd.oasis.opendocument.text")); +#endif +} +#endif // textmarkdownreader + +#endif // QT_NO_CLIPBOARD void tst_QTextEdit::ctrlEnterShouldInsertLineSeparator_NOT() { @@ -1653,7 +1700,7 @@ void tst_QTextEdit::ensureVisibleWithRtl() ed->setLayoutDirection(Qt::RightToLeft); ed->setLineWrapMode(QTextEdit::NoWrap); QString txt(500, QChar(QLatin1Char('a'))); - QCOMPARE(txt.length(), 500); + QCOMPARE(txt.size(), 500); ed->setPlainText(txt); ed->resize(100, 100); ed->show(); @@ -1704,7 +1751,7 @@ void tst_QTextEdit::extraSelections() ed->setExtraSelections(QList<QTextEdit::ExtraSelection>() << sel); QList<QTextEdit::ExtraSelection> selections = ed->extraSelections(); - QCOMPARE(selections.count(), 1); + QCOMPARE(selections.size(), 1); QCOMPARE(selections.at(0).cursor.position(), endPos); QCOMPARE(selections.at(0).cursor.anchor(), wordPos); } @@ -1846,27 +1893,27 @@ void tst_QTextEdit::selectionChanged() QTest::keyClick(ed, Qt::Key_Right); QCOMPARE(ed->textCursor().position(), 1); - QCOMPARE(selectionChangedSpy.count(), 0); + QCOMPARE(selectionChangedSpy.size(), 0); QTest::keyClick(ed, Qt::Key_Right, Qt::ShiftModifier); QCOMPARE(ed->textCursor().position(), 2); - QCOMPARE(selectionChangedSpy.count(), 1); + QCOMPARE(selectionChangedSpy.size(), 1); QTest::keyClick(ed, Qt::Key_Right, Qt::ShiftModifier); QCOMPARE(ed->textCursor().position(), 3); - QCOMPARE(selectionChangedSpy.count(), 2); + QCOMPARE(selectionChangedSpy.size(), 2); QTest::keyClick(ed, Qt::Key_Right, Qt::ShiftModifier); QCOMPARE(ed->textCursor().position(), 4); - QCOMPARE(selectionChangedSpy.count(), 3); + QCOMPARE(selectionChangedSpy.size(), 3); QTest::keyClick(ed, Qt::Key_Right); QCOMPARE(ed->textCursor().position(), 4); - QCOMPARE(selectionChangedSpy.count(), 4); + QCOMPARE(selectionChangedSpy.size(), 4); QTest::keyClick(ed, Qt::Key_Right); QCOMPARE(ed->textCursor().position(), 5); - QCOMPARE(selectionChangedSpy.count(), 4); + QCOMPARE(selectionChangedSpy.size(), 4); } #ifndef QT_NO_CLIPBOARD @@ -2158,6 +2205,18 @@ void tst_QTextEdit::compareWidgetAndImage(QTextEdit &widget, const QString &imag } } +bool tst_QTextEdit::isMainFontFixed() +{ + bool ret = QFontInfo(QGuiApplication::font()).fixedPitch(); + if (ret) { + qCWarning(lcTests) << "QFontDatabase::GeneralFont is monospaced: markdown writing is likely to use too many backticks"; + qCWarning(lcTests) << "system fonts: fixed" << QFontDatabase::systemFont(QFontDatabase::FixedFont) + << "fixed?" << QFontInfo(QFontDatabase::systemFont(QFontDatabase::FixedFont)).fixedPitch() + << "general" << QFontDatabase::systemFont(QFontDatabase::GeneralFont); + } + return ret; +} + void tst_QTextEdit::cursorRect() { ed->show(); @@ -2178,9 +2237,8 @@ void tst_QTextEdit::setDocumentPreservesPalette() QWidgetTextControl *control = ed->findChild<QWidgetTextControl *>(); QVERIFY(control); - QPalette defaultPal = ed->palette(); QPalette whitePal = ed->palette(); - whitePal.setColor(QPalette::Active, QPalette::Text, "white"); + whitePal.setColor(QPalette::Active, QPalette::Text, Qt::white); QVERIFY(whitePal != ed->palette()); @@ -2225,6 +2283,24 @@ void tst_QTextEdit::pasteFromQt3RichText() QCOMPARE(ed->toPlainText(), QString::fromLatin1(" QTextEdit is an ")); } +void tst_QTextEdit::pasteFromMarkdown() +{ + QByteArray richtext("*This* text is **rich**"); + + QMimeData mimeData; + mimeData.setData("text/markdown", richtext); + + static_cast<PublicTextEdit *>(ed)->publicInsertFromMimeData(&mimeData); + + QCOMPARE(ed->toPlainText(), "This text is rich"); +#if QT_CONFIG(textmarkdownwriter) + const auto expectedMarkdown = QString::fromLatin1(richtext + "\n\n"); + if (ed->toMarkdown() != expectedMarkdown && isMainFontFixed()) + QEXPECT_FAIL("", "fixed-pitch main font (QTBUG-103484)", Continue); + QCOMPARE(ed->toMarkdown(), expectedMarkdown); +#endif +} + void tst_QTextEdit::noWrapBackgrounds() { QWidget topLevel; @@ -2297,7 +2373,8 @@ void tst_QTextEdit::taskQTBUG_7902_contextMenuCrash() w->connect(&ti, SIGNAL(timeout()), w, SLOT(deleteLater())); ti.start(200); - QContextMenuEvent *cme = new QContextMenuEvent(QContextMenuEvent::Mouse, w->rect().center()); + QContextMenuEvent *cme = new QContextMenuEvent(QContextMenuEvent::Mouse, w->rect().center(), + w->viewport()->mapToGlobal(w->rect().center())); qApp->postEvent(w->viewport(), cme); QTest::qWait(300); @@ -2463,12 +2540,11 @@ void tst_QTextEdit::inputMethodEvent() QInputMethodEvent event; event.setCommitString("text"); QApplication::sendEvent(ed, &event); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); QCOMPARE(ed->toPlainText(), QString("text")); // test that input method gets chance to commit preedit when removing focus ed->setText(""); - QApplication::setActiveWindow(ed); QTRY_VERIFY(QApplication::focusWindow()); QCOMPARE(qApp->focusObject(), ed); @@ -2493,7 +2569,7 @@ void tst_QTextEdit::inputMethodSelection() cursor.setPosition(5, QTextCursor::KeepAnchor); ed->setTextCursor(cursor); - QCOMPARE(selectionSpy.count(), 1); + QCOMPARE(selectionSpy.size(), 1); QCOMPARE(ed->textCursor().selectionStart(), 0); QCOMPARE(ed->textCursor().selectionEnd(), 5); @@ -2502,7 +2578,7 @@ void tst_QTextEdit::inputMethodSelection() QInputMethodEvent event("", attributes); QApplication::sendEvent(ed, &event); - QCOMPARE(selectionSpy.count(), 2); + QCOMPARE(selectionSpy.size(), 2); QCOMPARE(ed->textCursor().selectionStart(), 12); QCOMPARE(ed->textCursor().selectionEnd(), 17); } @@ -2517,7 +2593,7 @@ void tst_QTextEdit::inputMethodQuery() QGuiApplication::sendEvent(ed, &event); int anchor = event.value(Qt::ImAnchorPosition).toInt(); int position = event.value(Qt::ImCursorPosition).toInt(); - QCOMPARE(qAbs(position - anchor), text.length()); + QCOMPARE(qAbs(position - anchor), text.size()); QCOMPARE(event.value(Qt::ImEnabled).toBool(), true); ed->setEnabled(false); @@ -2602,7 +2678,7 @@ void tst_QTextEdit::countTextChangedOnRemove() QKeyEvent event(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier); QCoreApplication::instance()->notify(&edit, &event); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); } #if QT_CONFIG(regularexpression) @@ -2989,5 +3065,14 @@ void tst_QTextEdit::nextFormatAfterEnterPressed() QCOMPARE(prevBlockCursor.charFormat().property(it.key()), it.value()); } +void tst_QTextEdit::dontCrashWithCss() +{ + qApp->setStyleSheet("QWidget { font: 10pt; }"); + QTextEdit edit; + edit.show(); + qApp->setStyleSheet(QString()); +} + + QTEST_MAIN(tst_QTextEdit) #include "tst_qtextedit.moc" |