diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index 5f46b4c..a906a23 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -1,34 +1,35 @@ include(ECMAddTests) find_package(Qt5 ${REQUIRED_QT_VERSION} CONFIG REQUIRED Test) ecm_add_tests( kcollapsiblegroupbox_test.cpp kcolorbuttontest.cpp kdatecomboboxtest.cpp kdatetimeedittest.cpp kdualactiontest.cpp kpixmapsequencewidgettest.cpp knewpasswordwidgettest.cpp kselectaction_unittest.cpp + ksqueezedtextlabelautotest.cpp ktimecomboboxtest.cpp ktooltipwidgettest.cpp kmessagewidgetautotest.cpp kpagedialogautotest.cpp kpassworddialogautotest.cpp kpasswordlineedittest.cpp ksplittercollapserbuttontest.cpp LINK_LIBRARIES Qt5::Test KF5::WidgetsAddons ) set (CMAKE_AUTOUIC TRUE) ecm_add_test( kcolumnresizertest.cpp kcolumnresizertest-forms.ui kcolumnresizertest-grids.ui kcolumnresizertest-grid-and-form.ui TEST_NAME kcolumnresizertest NAME_PREFIX "kwidgetsaddons-" LINK_LIBRARIES Qt5::Test KF5::WidgetsAddons ) diff --git a/autotests/ksqueezedtextlabelautotest.cpp b/autotests/ksqueezedtextlabelautotest.cpp new file mode 100644 index 0000000..e6a1642 --- /dev/null +++ b/autotests/ksqueezedtextlabelautotest.cpp @@ -0,0 +1,251 @@ +/* + Copyright 2017 Henrik Fehlauer + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "ksqueezedtextlabelautotest.h" + +#include + +#include + +Q_DECLARE_METATYPE(Qt::TextElideMode) + +namespace +{ + +KSqueezedTextLabel* createLabel(const QString &text = QStringLiteral("Squeeze me")) +{ + KSqueezedTextLabel *label = new KSqueezedTextLabel(text, 0); + label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); + label->show(); + return label; +} + +void squeezeLabel(KSqueezedTextLabel *label, const int pixels = 1) +{ + label->resize(label->size().width() - pixels, label->size().height()); +} + + +} // namespace + +void KSqueezedTextLabelAutotest::testEmptyText() +{ + const QScopedPointer label(createLabel(QString())); + + QVERIFY(label->text().isEmpty()); + QVERIFY(label->fullText().isEmpty()); + QVERIFY(label->toolTip().isEmpty()); + QVERIFY(!label->isSqueezed()); + QCOMPARE(label->width(), 0); +} + +void KSqueezedTextLabelAutotest::testElisionOnResize_data() +{ + QTest::addColumn("text"); + + QTest::newRow("whitespace text") << " "; + QTest::newRow("normal text") << "Squeeze me"; + QTest::newRow("rich text") << "

Squeeze me!

"; +// QTest::newRow("multiline text") << "Squeeze me,\nand\nme too."; +// QTest::newRow("multiline rich text") << "Squeeze me,\nand\nme too."; +} + +void KSqueezedTextLabelAutotest::testElisionOnResize() +{ + QFETCH(QString, text); + + const QScopedPointer label(createLabel(text)); + + QVERIFY(label->text() == text); + QVERIFY(label->fullText() == text); + QVERIFY(label->toolTip().isEmpty()); + QVERIFY(!label->isSqueezed()); + + squeezeLabel(label.data()); + + QVERIFY(label->text() != text); + QVERIFY(label->fullText() == text); + QVERIFY(label->toolTip() == text); + QVERIFY(label->isSqueezed()); + + squeezeLabel(label.data(), -1); + + QVERIFY(label->text() == text); + QVERIFY(label->fullText() == text); + QVERIFY(label->toolTip().isEmpty()); + QVERIFY(!label->isSqueezed()); +} + +void KSqueezedTextLabelAutotest::testElisionOnTextUpdate() +{ + const QScopedPointer label(createLabel()); + + QVERIFY(!label->isSqueezed()); + + label->setText(label->text() + QStringLiteral("!!!")); + + QVERIFY(label->isSqueezed()); +} + +void KSqueezedTextLabelAutotest::testElideMode_data() +{ + QTest::addColumn("mode"); + QTest::addColumn("prefix"); + QTest::addColumn("infix"); + QTest::addColumn("postfix"); + + const QChar ellipsisChar(0x2026); + const QChar a(QLatin1Char('a')), b(QLatin1Char('b')), c(QLatin1Char('c')); + + QTest::newRow("ElideLeft") << Qt::ElideLeft << ellipsisChar << b << c; + QTest::newRow("ElideRight") << Qt::ElideRight << a << b << ellipsisChar; + QTest::newRow("ElideMiddle") << Qt::ElideMiddle << a << ellipsisChar << c; + QTest::newRow("ElideNone") << Qt::ElideNone << a << b << c; +} + +void KSqueezedTextLabelAutotest::testElideMode() +{ + QFETCH(Qt::TextElideMode, mode); + QFETCH(QChar, prefix); + QFETCH(QChar, infix); + QFETCH(QChar, postfix); + + // "abc" is not sufficient, because ellipsis might be wider than a single char + const QScopedPointer label(createLabel(QStringLiteral("aaabbbccc"))); + label->setTextElideMode(mode); + squeezeLabel(label.data()); + const int infixPos = label->text().indexOf(infix); + + QVERIFY(label->isSqueezed() || mode == Qt::ElideNone); + QVERIFY(label->text().startsWith(prefix)); + QVERIFY(0 < infixPos && infixPos < label->text().length()-1); + QVERIFY(label->text().endsWith(postfix)); +} + +void KSqueezedTextLabelAutotest::testSizeHints() +{ + const QScopedPointer label(createLabel(QString())); + + QVERIFY(!label->isSqueezed()); + QCOMPARE(label->size().width(), 0); + QCOMPARE(label->minimumSizeHint().width(), -1); + QCOMPARE(label->sizeHint().width(), 0); + + label->adjustSize(); + + QVERIFY(!label->isSqueezed()); + QCOMPARE(label->size().width(), 0); + QCOMPARE(label->minimumSizeHint().width(), -1); + QCOMPARE(label->sizeHint().width(), 0); + + const QString text = QStringLiteral("Squeeze me"); + const int labelWidth = label->fontMetrics().width(text); // no chrome set + label->setText(text); + + QVERIFY(label->isSqueezed()); + QCOMPARE(label->size().width(), 0); + QCOMPARE(label->minimumSizeHint().width(), -1); + QCOMPARE(label->sizeHint().width(), labelWidth); + + label->adjustSize(); + + QVERIFY(!label->isSqueezed()); + QCOMPARE(label->size().width(), labelWidth); + QCOMPARE(label->minimumSizeHint().width(), -1); + QCOMPARE(label->sizeHint().width(), labelWidth); + + const int indent = 40; + label->setIndent(indent); + label->adjustSize(); + + QVERIFY(!label->isSqueezed()); + QEXPECT_FAIL("", "To fix: Respect chrome in sizeHint()", Abort); + QCOMPARE(label->size().width(), labelWidth + indent); + QCOMPARE(label->minimumSizeHint().width(), -1); + QCOMPARE(label->sizeHint().width(), labelWidth + indent); +} + +void KSqueezedTextLabelAutotest::testClearing() +{ + const QScopedPointer label(createLabel()); + squeezeLabel(label.data()); + + QVERIFY(label->isSqueezed()); + + label->clear(); + + QVERIFY(label->text().isEmpty()); + QVERIFY(label->fullText().isEmpty()); + QEXPECT_FAIL("", "To fix: Reset tooltip in clear()", Continue); + QVERIFY(label->toolTip().isEmpty()); + QVERIFY(!label->isSqueezed()); +} + +void KSqueezedTextLabelAutotest::testChrome_data() +{ + const QFontMetrics fm(KSqueezedTextLabel().fontMetrics()); + const int xWidth = fm.width(QLatin1Char('x')) / 2; + + QTest::addColumn("attribute"); + QTest::addColumn("amount"); + QTest::addColumn("widthDifference"); + + QTest::newRow("indent") << "indent" << 20 << 20; + QTest::newRow("margin") << "margin" << 20 << 40; + QTest::newRow("frame") << "lineWidth" << 20 << 40 + xWidth; +} + +void KSqueezedTextLabelAutotest::testChrome() +{ + QFETCH(QString, attribute); + QFETCH(int, amount); + QFETCH(int, widthDifference); + + const QScopedPointer label(createLabel()); + label->setFrameStyle(QFrame::Box); + label->setLineWidth(0); + const int oldWidth = label->width(); + + QVERIFY(!label->isSqueezed()); + + label->setProperty(attribute.toLatin1().data(), amount); + QTest::qWaitForWindowExposed(label.data()); + + QEXPECT_FAIL("", "To fix: Respect chrome in squeezeTextToLabel()", Abort); + QVERIFY(label->isSqueezed()); + + label->adjustSize(); + + QVERIFY(!label->isSqueezed()); + QCOMPARE(label->width(), oldWidth + widthDifference); +} + +QTEST_MAIN(KSqueezedTextLabelAutotest) + + +// TODO +// currently untested, may need clarification/definition of correct behaviour: +// - multiline text +// - setWordWrap +// - setAlignment +// - setToolTip +// - context menu +// - Qt::TextInteractionFlags, keyboard/mouse interaction +// - linkHovered/linkActivated +// - setPixmap diff --git a/autotests/ksqueezedtextlabelautotest.h b/autotests/ksqueezedtextlabelautotest.h new file mode 100644 index 0000000..399d4c4 --- /dev/null +++ b/autotests/ksqueezedtextlabelautotest.h @@ -0,0 +1,44 @@ +/* + Copyright 2017 Henrik Fehlauer + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef KSQUEEZEDTEXTLABELAUTOTEST_H +#define KSQUEEZEDTEXTLABELAUTOTEST_H + +#include + +class KSqueezedTextLabel; + +class KSqueezedTextLabelAutotest : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void testEmptyText(); + void testElisionOnResize_data(); + void testElisionOnResize(); + void testElisionOnTextUpdate(); + void testElideMode_data(); + void testElideMode(); + void testSizeHints(); + void testClearing(); + void testChrome_data(); + void testChrome(); +}; + +#endif