summaryrefslogtreecommitdiffstats
path: root/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp')
-rw-r--r--tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp245
1 files changed, 181 insertions, 64 deletions
diff --git a/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp b/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp
index 297eb5ec51..ca7cc6d4b4 100644
--- a/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp
+++ b/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.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>
@@ -38,6 +13,7 @@
#include <qclipboard.h>
#include <qtextbrowser.h>
#include <private/qwidgettextcontrol_p.h>
+#include <private/qplaintextedit_p.h>
#include <qscrollbar.h>
#include <qtextobject.h>
#include <qmenu.h>
@@ -143,7 +119,7 @@ private slots:
void layoutAfterMultiLineRemove();
void undoCommandRemovesAndReinsertsBlock();
void taskQTBUG_43562_lineCountCrash();
-#ifndef QT_NO_CONTEXTMENU
+#if !defined(QT_NO_CONTEXTMENU) && !defined(QT_NO_CLIPBOARD)
void contextMenu();
#endif
void inputMethodCursorRect();
@@ -154,6 +130,10 @@ private slots:
void updateCursorPositionAfterEdit();
#endif
void appendTextWhenInvisible();
+ void placeholderVisibility_data();
+ void placeholderVisibility();
+ void scrollBarSignals();
+ void dontCrashWithCss();
private:
void createSelection();
@@ -415,7 +395,7 @@ void tst_QPlainTextEdit::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);
@@ -423,23 +403,23 @@ void tst_QPlainTextEdit::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);
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);
}
@@ -456,7 +436,7 @@ void tst_QPlainTextEdit::setTextCursor()
spy.clear();
ed->setTextCursor(cursor);
- QCOMPARE(spy.count(), 1);
+ QCOMPARE(spy.size(), 1);
}
#ifndef QT_NO_CLIPBOARD
@@ -473,7 +453,7 @@ void tst_QPlainTextEdit::undoAvailableAfterPaste()
const QString txt("Test");
QApplication::clipboard()->setText(txt);
ed->paste();
- QVERIFY(spy.count() >= 1);
+ QVERIFY(spy.size() >= 1);
QCOMPARE(ed->toPlainText(), txt);
}
#endif
@@ -727,16 +707,16 @@ void tst_QPlainTextEdit::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->textCursor().charFormat().properties().count(), 0);
+ QCOMPARE(ed->textCursor().charFormat().properties().size(), 0);
}
void tst_QPlainTextEdit::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_QPlainTextEdit::overwriteMode()
@@ -1016,7 +996,7 @@ void tst_QPlainTextEdit::copyAvailable_data()
//Tests the copyAvailable slot for several cases
void tst_QPlainTextEdit::copyAvailable()
{
- QFETCH(pairListType,keystrokes);
+ QFETCH(const pairListType, keystrokes);
QFETCH(QList<bool>, copyAvailable);
QFETCH(QString, function);
@@ -1029,9 +1009,8 @@ void tst_QPlainTextEdit::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")
@@ -1048,8 +1027,8 @@ void tst_QPlainTextEdit::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());
}
@@ -1085,10 +1064,10 @@ void tst_QPlainTextEdit::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"));
}
@@ -1300,7 +1279,7 @@ void tst_QPlainTextEdit::ensureVisibleWithRtl()
ed->setLayoutDirection(Qt::RightToLeft);
ed->setLineWrapMode(QPlainTextEdit::NoWrap);
QString txt(500, QChar(QLatin1Char('a')));
- QCOMPARE(txt.length(), 500);
+ QCOMPARE(txt.size(), 500);
ed->setPlainText(txt);
ed->resize(100, 100);
ed->show();
@@ -1353,7 +1332,7 @@ void tst_QPlainTextEdit::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);
}
@@ -1368,7 +1347,9 @@ void tst_QPlainTextEdit::adjustScrollbars()
ed->setFont(ff);
ed->setMinimumSize(140, 100);
ed->setMaximumSize(140, 100);
- ed->show();
+ // We use showNormal() here, because otherwise on Android the widget will
+ // be shown fullscreen, and the scrollbar will not appear.
+ ed->showNormal();
QLatin1String txt("\nabc def ghi jkl mno pqr stu vwx");
ed->setPlainText(txt + txt + txt + txt);
@@ -1481,44 +1462,44 @@ void tst_QPlainTextEdit::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);
}
void tst_QPlainTextEdit::blockCountChanged()
{
QSignalSpy blockCountCpangedSpy(ed, SIGNAL(blockCountChanged(int)));
ed->setPlainText("Hello");
- QCOMPARE(blockCountCpangedSpy.count(), 0);
+ QCOMPARE(blockCountCpangedSpy.size(), 0);
ed->setPlainText("Hello World");
- QCOMPARE(blockCountCpangedSpy.count(), 0);
+ QCOMPARE(blockCountCpangedSpy.size(), 0);
ed->setPlainText("Hello \n World \n this \n has \n more \n blocks \n than \n just \n one");
- QCOMPARE(blockCountCpangedSpy.count(), 1);
+ QCOMPARE(blockCountCpangedSpy.size(), 1);
ed->setPlainText("One");
- QCOMPARE(blockCountCpangedSpy.count(), 2);
+ QCOMPARE(blockCountCpangedSpy.size(), 2);
ed->setPlainText("One \n Two");
- QCOMPARE(blockCountCpangedSpy.count(), 3);
+ QCOMPARE(blockCountCpangedSpy.size(), 3);
ed->setPlainText("Three \n Four");
- QCOMPARE(blockCountCpangedSpy.count(), 3);
+ QCOMPARE(blockCountCpangedSpy.size(), 3);
}
@@ -1696,7 +1677,7 @@ void tst_QPlainTextEdit::taskQTBUG_43562_lineCountCrash()
disconnect(ed->document(), SIGNAL(contentsChange(int, int, int)), 0, 0);
}
-#ifndef QT_NO_CONTEXTMENU
+#if !defined(QT_NO_CONTEXTMENU) && !defined(QT_NO_CLIPBOARD)
void tst_QPlainTextEdit::contextMenu()
{
ed->appendHtml(QStringLiteral("Hello <a href='http://www.qt.io'>Qt</a>"));
@@ -1719,7 +1700,7 @@ void tst_QPlainTextEdit::contextMenu()
QVERIFY(!ed->findChild<QAction *>(QStringLiteral("link-copy")));
QTextCursor cursor = ed->textCursor();
- cursor.setPosition(ed->toPlainText().length() - 2);
+ cursor.setPosition(ed->toPlainText().size() - 2);
ed->setTextCursor(cursor);
menu = ed->createStandardContextMenu(ed->cursorRect().center());
@@ -1730,7 +1711,7 @@ void tst_QPlainTextEdit::contextMenu()
delete menu;
QVERIFY(!ed->findChild<QAction *>(QStringLiteral("link-copy")));
}
-#endif // QT_NO_CONTEXTMENU
+#endif // QT_NO_CONTEXTMENU && QT_NO_CLIPBOARD
// QTBUG-51923: Verify that the cursor rectangle returned by the input
// method query correctly reflects the viewport offset.
@@ -1802,7 +1783,7 @@ void tst_QPlainTextEdit::updateCursorPositionAfterEdit()
QTest::keyClick(&plaintextEdit, Qt::Key_Up);
// The curser should move back to the end of the copied text
- QCOMPARE(plaintextEdit.textCursor().position(), initialPosition + txt.length());
+ QCOMPARE(plaintextEdit.textCursor().position(), initialPosition + txt.size());
}
#endif
@@ -1837,5 +1818,141 @@ void tst_QPlainTextEdit::appendTextWhenInvisible()
QCOMPARE(maxAfterAppend, maxAfterSet);
}
+enum SetupCommand {
+ ClearPlaceHolder, // set empty placeholder text
+ SetPlaceHolder, // set a non-empty placeholder text
+ ClearContent, // set empty text as content
+ SetContent // set non-empty text as content
+};
+
+void tst_QPlainTextEdit::placeholderVisibility_data()
+{
+ QTest::addColumn<QList<SetupCommand>>("setupCommands");
+ QTest::addColumn<bool>("placeholderVisible");
+ QTest::addRow("no placeholder set + no text set")
+ << QList<SetupCommand>{} << false;
+ QTest::addRow("no placeholder set + text set or text set + no placeholder set")
+ << QList<SetupCommand>{ SetContent } << false;
+ QTest::addRow("no placeholder set + text set + empty text set")
+ << QList<SetupCommand>{ SetContent , ClearContent }
+ << false;
+ QTest::addRow("no placeholder set + empty text set + text set")
+ << QList<SetupCommand>{ ClearContent, SetContent }
+ << false;
+ QTest::addRow("empty placeholder set + no text set")
+ << QList<SetupCommand>{ ClearPlaceHolder } << false;
+ QTest::addRow("empty placeholder set + text set")
+ << QList<SetupCommand>{ ClearPlaceHolder, SetContent }
+ << false;
+ QTest::addRow("empty placeholder set + text set + empty text set")
+ << QList<SetupCommand>{ ClearPlaceHolder, SetContent, ClearContent }
+ << false;
+ QTest::addRow("empty placeholder set + empty text set + text set")
+ << QList<SetupCommand>{ ClearPlaceHolder, ClearContent, SetContent }
+ << false;
+ QTest::addRow("placeholder set + no text set")
+ << QList<SetupCommand>{ SetPlaceHolder, ClearContent }
+ << true;
+ QTest::addRow("placeholder set + text set")
+ << QList<SetupCommand>{ SetPlaceHolder, SetContent }
+ << false;
+ QTest::addRow("placeholder set + text set + empty text set")
+ << QList<SetupCommand>{ SetPlaceHolder, SetContent, ClearContent }
+ << true;
+ QTest::addRow("placeholder set + empty text set + text set")
+ << QList<SetupCommand>{ SetPlaceHolder, ClearContent, SetContent }
+ << false;
+ QTest::addRow("placeholder set + text set + empty placeholder set")
+ << QList<SetupCommand>{ SetPlaceHolder, SetContent, ClearPlaceHolder}
+ << false;
+ QTest::addRow("placeholder set + empty placeholder set + text set")
+ << QList<SetupCommand>{ SetPlaceHolder, ClearPlaceHolder, SetContent }
+ << false;
+ QTest::addRow("placeholder set + empty placeholder set + empty text set")
+ << QList<SetupCommand>{ SetPlaceHolder, ClearPlaceHolder, ClearContent }
+ << false;
+ QTest::addRow("placeholder set + empty text set + empty placeholder set")
+ << QList<SetupCommand>{ SetPlaceHolder, ClearContent, ClearPlaceHolder }
+ << false;
+ QTest::addRow("text set + no placeholder set + empty text set")
+ << QList<SetupCommand>{ SetContent, ClearContent }
+ << false;
+ QTest::addRow("text set + empty placeholder set")
+ << QList<SetupCommand>{ SetContent, ClearPlaceHolder }
+ << false;
+ QTest::addRow("text set + placeholder set")
+ << QList<SetupCommand>{ SetContent, SetPlaceHolder }
+ << false;
+ QTest::addRow("text set + placeholder set + empty text set")
+ << QList<SetupCommand>{ SetContent, SetPlaceHolder, ClearContent }
+ << true;
+ QTest::addRow("text set + placeholder set + empty placeholder set")
+ << QList<SetupCommand>{ SetContent, SetPlaceHolder, ClearPlaceHolder }
+ << false;
+}
+
+void tst_QPlainTextEdit::placeholderVisibility()
+{
+ QFETCH(QList<SetupCommand>, setupCommands);
+ QFETCH(bool, placeholderVisible);
+
+ QPlainTextEdit plainTextEdit;
+ for (auto command : setupCommands) {
+ switch (command) {
+ case ClearPlaceHolder:
+ plainTextEdit.setPlaceholderText("");
+ break;
+ case SetPlaceHolder:
+ plainTextEdit.setPlaceholderText("Qt is awesome !");
+ break;
+ case ClearContent:
+ plainTextEdit.setPlainText("");
+ break;
+ case SetContent:
+ plainTextEdit.setPlainText("PlainText...");
+ break;
+ }
+ }
+ auto *plainTextEdit_d = static_cast<QPlainTextEditPrivate *>(qt_widget_private(&plainTextEdit));
+
+ plainTextEdit.show();
+ QVERIFY(QTest::qWaitForWindowExposed(&plainTextEdit));
+ QTRY_COMPARE(plainTextEdit_d->placeholderTextShown, placeholderVisible);
+}
+
+
+void tst_QPlainTextEdit::scrollBarSignals()
+{
+ QPlainTextEdit plainTextEdit;
+ QString longText;
+ for (uint i = 0; i < 500; ++i)
+ longText += "This is going to be a very long text for scroll signal testing.\n";
+ plainTextEdit.setPlainText(longText);
+ QScrollBar *vbar = plainTextEdit.verticalScrollBar();
+ plainTextEdit.show();
+ QVERIFY(QTest::qWaitForWindowExposed(&plainTextEdit));
+ QSignalSpy spy(vbar, &QScrollBar::valueChanged);
+
+ QTest::keyClick(vbar, Qt::Key_Down);
+ QTRY_COMPARE(spy.count(), 1);
+ QTest::keyClick(vbar, Qt::Key_PageDown);
+ QTRY_COMPARE(spy.count(), 2);
+ QTest::keyClick(vbar, Qt::Key_PageDown);
+ QTRY_COMPARE(spy.count(), 3);
+ QTest::keyClick(vbar, Qt::Key_Up);
+ QTRY_COMPARE(spy.count(), 4);
+ QTest::keyClick(vbar, Qt::Key_PageUp);
+ QTRY_COMPARE(spy.count(), 5);
+}
+
+void tst_QPlainTextEdit::dontCrashWithCss()
+{
+ qApp->setStyleSheet("QWidget { font: 10pt; }");
+ QPlainTextEdit edit;
+ edit.show();
+ qApp->setStyleSheet(QString());
+}
+
+
QTEST_MAIN(tst_QPlainTextEdit)
#include "tst_qplaintextedit.moc"