| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2017 Henrik Fehlauer <[email protected]> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #include "ksqueezedtextlabelautotest.h" |
| 8 | |
| 9 | #include <KSqueezedTextLabel> |
| 10 | |
| 11 | #include <QTest> |
| 12 | |
| 13 | Q_DECLARE_METATYPE(Qt::TextElideMode) |
| 14 | |
| 15 | namespace |
| 16 | { |
| 17 | |
| 18 | std::unique_ptr<KSqueezedTextLabel> createLabel(const QString &text = QStringLiteral("Squeeze me")) |
| 19 | { |
| 20 | auto label = std::make_unique<KSqueezedTextLabel>(QStringLiteral(""), nullptr); |
| 21 | label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); |
| 22 | // workaround for a QLabel bug, where the sizing for an empty label |
| 23 | // is off, if it was initialized to an empty text. |
| 24 | label->setText(QStringLiteral("x")); |
| 25 | label->setText(text); |
| 26 | label->show(); |
| 27 | return label; |
| 28 | } |
| 29 | |
| 30 | void squeezeLabel(KSqueezedTextLabel *label, const int pixels = 10) |
| 31 | { |
| 32 | label->resize(label->size().width() - pixels, label->size().height()); |
| 33 | } |
| 34 | |
| 35 | } // namespace |
| 36 | |
| 37 | void KSqueezedTextLabelAutotest::testEmptyText() |
| 38 | { |
| 39 | const auto label = createLabel(QString()); |
| 40 | |
| 41 | QVERIFY(label->text().isEmpty()); |
| 42 | QVERIFY(label->fullText().isEmpty()); |
| 43 | QVERIFY(label->toolTip().isEmpty()); |
| 44 | QVERIFY(!label->isSqueezed()); |
| 45 | QCOMPARE(label->width(), 0); |
| 46 | } |
| 47 | |
| 48 | void KSqueezedTextLabelAutotest::testElisionOnResize_data() |
| 49 | { |
| 50 | QTest::addColumn<QString>("text"); |
| 51 | QTest::addColumn<int>("squeezePixels"); |
| 52 | |
| 53 | QTest::newRow("whitespace text") << " " << 10; |
| 54 | QTest::newRow("normal text") << "Squeeze me" << 10; |
| 55 | QTest::newRow("rich text") << "<h1>Squeeze me!</h1>" << 80; |
| 56 | // QTest::newRow("multiline text") << "Squeeze me,\nand\nme too."; |
| 57 | // QTest::newRow("multiline rich text") << "<i>Squeeze</i> me,\n<b>and\nme</b> too."; |
| 58 | } |
| 59 | |
| 60 | void KSqueezedTextLabelAutotest::testElisionOnResize() |
| 61 | { |
| 62 | QFETCH(QString, text); |
| 63 | QFETCH(int, squeezePixels); |
| 64 | |
| 65 | const auto label = createLabel(text); |
| 66 | |
| 67 | QVERIFY(label->text() == text); |
| 68 | QVERIFY(label->fullText() == text); |
| 69 | QVERIFY(label->toolTip().isEmpty()); |
| 70 | QVERIFY(!label->isSqueezed()); |
| 71 | |
| 72 | squeezeLabel(label.get(), squeezePixels); |
| 73 | |
| 74 | QVERIFY(label->text() != text); |
| 75 | QVERIFY(label->fullText() == text); |
| 76 | QVERIFY(label->toolTip() == text); |
| 77 | QVERIFY(label->isSqueezed()); |
| 78 | |
| 79 | squeezeLabel(label.get(), -squeezePixels); |
| 80 | |
| 81 | QVERIFY(label->text() == text); |
| 82 | QVERIFY(label->fullText() == text); |
| 83 | QVERIFY(label->toolTip().isEmpty()); |
| 84 | QVERIFY(!label->isSqueezed()); |
| 85 | } |
| 86 | |
| 87 | void KSqueezedTextLabelAutotest::testElisionOnTextUpdate() |
| 88 | { |
| 89 | const auto label = createLabel(); |
| 90 | |
| 91 | QVERIFY(!label->isSqueezed()); |
| 92 | |
| 93 | label->setText(label->text() + QStringLiteral("!!!")); |
| 94 | |
| 95 | QVERIFY(label->isSqueezed()); |
| 96 | } |
| 97 | |
| 98 | void KSqueezedTextLabelAutotest::testElideMode_data() |
| 99 | { |
| 100 | QTest::addColumn<Qt::TextElideMode>("mode"); |
| 101 | QTest::addColumn<QChar>("prefix"); |
| 102 | QTest::addColumn<QChar>("infix"); |
| 103 | QTest::addColumn<QChar>("postfix"); |
| 104 | |
| 105 | const QChar ellipsisChar(0x2026); |
| 106 | const QChar a(QLatin1Char('a')); |
| 107 | const QChar b(QLatin1Char('b')); |
| 108 | const QChar c(QLatin1Char('c')); |
| 109 | |
| 110 | QTest::newRow("ElideLeft") << Qt::ElideLeft << ellipsisChar << b << c; |
| 111 | QTest::newRow("ElideRight") << Qt::ElideRight << a << b << ellipsisChar; |
| 112 | QTest::newRow("ElideMiddle") << Qt::ElideMiddle << a << ellipsisChar << c; |
| 113 | QTest::newRow("ElideNone") << Qt::ElideNone << a << b << c; |
| 114 | } |
| 115 | |
| 116 | void KSqueezedTextLabelAutotest::testElideMode() |
| 117 | { |
| 118 | QFETCH(Qt::TextElideMode, mode); |
| 119 | QFETCH(QChar, prefix); |
| 120 | QFETCH(QChar, infix); |
| 121 | QFETCH(QChar, postfix); |
| 122 | |
| 123 | // "abc" is not sufficient, because ellipsis might be wider than a single char |
| 124 | const auto label = createLabel(QStringLiteral("aaabbbccc")); |
| 125 | label->setTextElideMode(mode); |
| 126 | squeezeLabel(label.get()); |
| 127 | const int infixPos = label->text().indexOf(infix); |
| 128 | |
| 129 | QVERIFY(label->isSqueezed() || mode == Qt::ElideNone); |
| 130 | QVERIFY(label->text().startsWith(prefix)); |
| 131 | QVERIFY(0 < infixPos && infixPos < label->text().length() - 1); |
| 132 | QVERIFY(label->text().endsWith(postfix)); |
| 133 | } |
| 134 | |
| 135 | void KSqueezedTextLabelAutotest::testSizeHints() |
| 136 | { |
| 137 | const auto label = createLabel(QString()); |
| 138 | |
| 139 | QVERIFY(!label->isSqueezed()); |
| 140 | QCOMPARE(label->size().width(), 0); |
| 141 | QCOMPARE(label->minimumSizeHint().width(), -1); |
| 142 | QCOMPARE(label->sizeHint().width(), 0); |
| 143 | |
| 144 | label->adjustSize(); |
| 145 | |
| 146 | QVERIFY(!label->isSqueezed()); |
| 147 | QCOMPARE(label->size().width(), 0); |
| 148 | QCOMPARE(label->minimumSizeHint().width(), -1); |
| 149 | QCOMPARE(label->sizeHint().width(), 0); |
| 150 | |
| 151 | const QString text = QStringLiteral("Squeeze me"); |
| 152 | label->setText(text); |
| 153 | const int labelWidth = label->sizeHint().width(); |
| 154 | |
| 155 | QVERIFY(label->isSqueezed()); |
| 156 | QCOMPARE(label->size().width(), 0); |
| 157 | QCOMPARE(label->minimumSizeHint().width(), -1); |
| 158 | |
| 159 | label->adjustSize(); |
| 160 | |
| 161 | QVERIFY(!label->isSqueezed()); |
| 162 | QCOMPARE(label->size().width(), labelWidth); |
| 163 | QCOMPARE(label->minimumSizeHint().width(), -1); |
| 164 | QCOMPARE(label->sizeHint().width(), labelWidth); |
| 165 | |
| 166 | const int indent = 40; |
| 167 | label->setIndent(indent); |
| 168 | label->adjustSize(); |
| 169 | |
| 170 | QVERIFY(!label->isSqueezed()); |
| 171 | QCOMPARE(label->size().width(), labelWidth + indent); |
| 172 | QCOMPARE(label->minimumSizeHint().width(), -1); |
| 173 | QCOMPARE(label->sizeHint().width(), labelWidth + indent); |
| 174 | } |
| 175 | |
| 176 | void KSqueezedTextLabelAutotest::testClearing() |
| 177 | { |
| 178 | const auto label = createLabel(); |
| 179 | squeezeLabel(label.get()); |
| 180 | |
| 181 | QVERIFY(label->isSqueezed()); |
| 182 | |
| 183 | label->clear(); |
| 184 | |
| 185 | QVERIFY(label->text().isEmpty()); |
| 186 | QVERIFY(label->fullText().isEmpty()); |
| 187 | QEXPECT_FAIL("", "To fix: Reset tooltip in clear()", Continue); |
| 188 | QVERIFY(label->toolTip().isEmpty()); |
| 189 | QVERIFY(!label->isSqueezed()); |
| 190 | } |
| 191 | |
| 192 | void KSqueezedTextLabelAutotest::testChrome_data() |
| 193 | { |
| 194 | const QFontMetrics fm(KSqueezedTextLabel().fontMetrics()); |
| 195 | const int xWidth = fm.horizontalAdvance(QLatin1Char('x')) / 2; |
| 196 | |
| 197 | QTest::addColumn<QString>("attribute"); |
| 198 | QTest::addColumn<int>("amount"); |
| 199 | QTest::addColumn<int>("widthDifference"); |
| 200 | |
| 201 | QTest::newRow("indent") << "indent" << 20 << 20; |
| 202 | QTest::newRow("margin") << "margin" << 20 << 40; |
| 203 | QTest::newRow("frame") << "lineWidth" << 20 << 40 + xWidth; |
| 204 | } |
| 205 | |
| 206 | void KSqueezedTextLabelAutotest::testChrome() |
| 207 | { |
| 208 | QFETCH(QString, attribute); |
| 209 | QFETCH(int, amount); |
| 210 | QFETCH(int, widthDifference); |
| 211 | |
| 212 | const auto label = createLabel(); |
| 213 | label->setFrameStyle(QFrame::Box); |
| 214 | label->setLineWidth(0); |
| 215 | const int oldWidth = label->width(); |
| 216 | |
| 217 | QVERIFY(!label->isSqueezed()); |
| 218 | |
| 219 | label->setProperty(attribute.toLatin1().data(), amount); |
| 220 | |
| 221 | QVERIFY(label->isSqueezed()); |
| 222 | |
| 223 | label->adjustSize(); |
| 224 | |
| 225 | QVERIFY(!label->isSqueezed()); |
| 226 | QCOMPARE(label->width(), oldWidth + widthDifference); |
| 227 | } |
| 228 | |
| 229 | QTEST_MAIN(KSqueezedTextLabelAutotest) |
| 230 | |
| 231 | // TODO |
| 232 | // currently untested, may need clarification/definition of correct behaviour: |
| 233 | // - multiline text |
| 234 | // - setWordWrap |
| 235 | // - setAlignment |
| 236 | // - setToolTip |
| 237 | // - context menu |
| 238 | // - Qt::TextInteractionFlags, keyboard/mouse interaction |
| 239 | // - linkHovered/linkActivated |
| 240 | // - setPixmap |
| 241 | |
| 242 | #include "moc_ksqueezedtextlabelautotest.cpp" |