Changeset View
Standalone View
autotests/ksqueezedtextlabelautotest.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright 2017 Henrik Fehlauer <rkflx@lab12.net> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Library General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2 of the License, or (at your option) any later version. | ||||
8 | | ||||
9 | This library is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
12 | Library General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Library General Public License | ||||
15 | along with this library; see the file COPYING.LIB. If not, write to | ||||
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
17 | Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include "ksqueezedtextlabelautotest.h" | ||||
21 | | ||||
22 | #include <KSqueezedTextLabel> | ||||
23 | | ||||
24 | #include <QTest> | ||||
25 | | ||||
26 | Q_DECLARE_METATYPE(Qt::TextElideMode) | ||||
27 | | ||||
28 | namespace | ||||
29 | { | ||||
30 | | ||||
31 | KSqueezedTextLabel* createLabel(const QString &text = QStringLiteral("Squeeze me")) | ||||
32 | { | ||||
33 | KSqueezedTextLabel *label = new KSqueezedTextLabel(text, 0); | ||||
34 | label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); | ||||
35 | label->show(); | ||||
36 | return label; | ||||
37 | } | ||||
38 | | ||||
39 | void squeezeLabel(KSqueezedTextLabel *label, const int pixels = 1) | ||||
40 | { | ||||
dhaumann: To me, it was not immediately clear whether amount referts to pixel or to characters. So you… | |||||
41 | label->resize(label->size().width() - pixels, label->size().height()); | ||||
42 | } | ||||
43 | | ||||
44 | | ||||
45 | } // namespace | ||||
46 | | ||||
47 | void KSqueezedTextLabelAutotest::testEmptyText() | ||||
dhaumann: Use QString() instead of QStringLiteral("") for empty strings. | |||||
48 | { | ||||
49 | const QScopedPointer<KSqueezedTextLabel> label(createLabel(QString())); | ||||
50 | | ||||
51 | QVERIFY(label->text().isEmpty()); | ||||
52 | QVERIFY(label->fullText().isEmpty()); | ||||
53 | QVERIFY(label->toolTip().isEmpty()); | ||||
54 | QVERIFY(!label->isSqueezed()); | ||||
55 | QCOMPARE(label->width(), 0); | ||||
56 | } | ||||
57 | | ||||
58 | void KSqueezedTextLabelAutotest::testElisionOnResize_data() | ||||
59 | { | ||||
60 | QTest::addColumn<QString>("text"); | ||||
61 | | ||||
62 | QTest::newRow("whitespace text") << " "; | ||||
63 | QTest::newRow("normal text") << "Squeeze me"; | ||||
64 | QTest::newRow("rich text") << "<h1>Squeeze me!</h1>"; | ||||
65 | // QTest::newRow("multiline text") << "Squeeze me,\nand\nme too."; | ||||
66 | // QTest::newRow("multiline rich text") << "<i>Squeeze</i> me,\n<b>and\nme</b> too."; | ||||
67 | } | ||||
68 | | ||||
69 | void KSqueezedTextLabelAutotest::testElisionOnResize() | ||||
70 | { | ||||
71 | QFETCH(QString, text); | ||||
72 | | ||||
73 | const QScopedPointer<KSqueezedTextLabel> label(createLabel(text)); | ||||
74 | | ||||
75 | QVERIFY(label->text() == text); | ||||
76 | QVERIFY(label->fullText() == text); | ||||
77 | QVERIFY(label->toolTip().isEmpty()); | ||||
78 | QVERIFY(!label->isSqueezed()); | ||||
79 | | ||||
80 | squeezeLabel(label.data()); | ||||
81 | | ||||
82 | QVERIFY(label->text() != text); | ||||
83 | QVERIFY(label->fullText() == text); | ||||
84 | QVERIFY(label->toolTip() == text); | ||||
85 | QVERIFY(label->isSqueezed()); | ||||
86 | | ||||
87 | squeezeLabel(label.data(), -1); | ||||
88 | | ||||
89 | QVERIFY(label->text() == text); | ||||
90 | QVERIFY(label->fullText() == text); | ||||
91 | QVERIFY(label->toolTip().isEmpty()); | ||||
92 | QVERIFY(!label->isSqueezed()); | ||||
93 | } | ||||
94 | | ||||
95 | void KSqueezedTextLabelAutotest::testElisionOnTextUpdate() | ||||
96 | { | ||||
97 | const QScopedPointer<KSqueezedTextLabel> label(createLabel()); | ||||
98 | | ||||
99 | QVERIFY(!label->isSqueezed()); | ||||
100 | | ||||
101 | label->setText(label->text() + QStringLiteral("!!!")); | ||||
102 | | ||||
103 | QVERIFY(label->isSqueezed()); | ||||
104 | } | ||||
105 | | ||||
106 | void KSqueezedTextLabelAutotest::testElideMode_data() | ||||
107 | { | ||||
108 | QTest::addColumn<Qt::TextElideMode>("mode"); | ||||
109 | QTest::addColumn<QChar>("prefix"); | ||||
110 | QTest::addColumn<QChar>("infix"); | ||||
111 | QTest::addColumn<QChar>("postfix"); | ||||
112 | | ||||
113 | const QChar ellipsisChar(0x2026); | ||||
114 | const QChar a(QLatin1Char('a')), b(QLatin1Char('b')), c(QLatin1Char('c')); | ||||
115 | | ||||
116 | QTest::newRow("ElideLeft") << Qt::ElideLeft << ellipsisChar << b << c; | ||||
117 | QTest::newRow("ElideRight") << Qt::ElideRight << a << b << ellipsisChar; | ||||
118 | QTest::newRow("ElideMiddle") << Qt::ElideMiddle << a << ellipsisChar << c; | ||||
119 | QTest::newRow("ElideNone") << Qt::ElideNone << a << b << c; | ||||
120 | } | ||||
121 | | ||||
122 | void KSqueezedTextLabelAutotest::testElideMode() | ||||
123 | { | ||||
124 | QFETCH(Qt::TextElideMode, mode); | ||||
125 | QFETCH(QChar, prefix); | ||||
126 | QFETCH(QChar, infix); | ||||
127 | QFETCH(QChar, postfix); | ||||
128 | | ||||
129 | // "abc" is not sufficient, because ellipsis might be wider than a single char | ||||
130 | const QScopedPointer<KSqueezedTextLabel> label(createLabel(QStringLiteral("aaabbbccc"))); | ||||
131 | label->setTextElideMode(mode); | ||||
132 | squeezeLabel(label.data()); | ||||
133 | const int infixPos = label->text().indexOf(infix); | ||||
134 | | ||||
135 | QVERIFY(label->isSqueezed() || mode == Qt::ElideNone); | ||||
136 | QVERIFY(label->text().startsWith(prefix)); | ||||
137 | QVERIFY(0 < infixPos && infixPos < label->text().length()-1); | ||||
138 | QVERIFY(label->text().endsWith(postfix)); | ||||
139 | } | ||||
140 | | ||||
141 | void KSqueezedTextLabelAutotest::testSizeHints() | ||||
dhaumann: Same here: Prefer QString() over QStringLiteral("") | |||||
142 | { | ||||
143 | const QScopedPointer<KSqueezedTextLabel> label(createLabel(QString())); | ||||
144 | | ||||
145 | QVERIFY(!label->isSqueezed()); | ||||
146 | QCOMPARE(label->size().width(), 0); | ||||
147 | QCOMPARE(label->minimumSizeHint().width(), -1); | ||||
148 | QCOMPARE(label->sizeHint().width(), 0); | ||||
149 | | ||||
150 | label->adjustSize(); | ||||
151 | | ||||
152 | QVERIFY(!label->isSqueezed()); | ||||
153 | QCOMPARE(label->size().width(), 0); | ||||
154 | QCOMPARE(label->minimumSizeHint().width(), -1); | ||||
155 | QCOMPARE(label->sizeHint().width(), 0); | ||||
156 | | ||||
157 | const QString text = QStringLiteral("Squeeze me"); | ||||
158 | const int labelWidth = label->fontMetrics().width(text); // no chrome set | ||||
159 | label->setText(text); | ||||
160 | | ||||
161 | QVERIFY(label->isSqueezed()); | ||||
162 | QCOMPARE(label->size().width(), 0); | ||||
163 | QCOMPARE(label->minimumSizeHint().width(), -1); | ||||
164 | QCOMPARE(label->sizeHint().width(), labelWidth); | ||||
165 | | ||||
166 | label->adjustSize(); | ||||
167 | | ||||
168 | QVERIFY(!label->isSqueezed()); | ||||
169 | QCOMPARE(label->size().width(), labelWidth); | ||||
170 | QCOMPARE(label->minimumSizeHint().width(), -1); | ||||
171 | QCOMPARE(label->sizeHint().width(), labelWidth); | ||||
172 | | ||||
173 | const int indent = 40; | ||||
174 | label->setIndent(indent); | ||||
175 | label->adjustSize(); | ||||
176 | | ||||
177 | QVERIFY(!label->isSqueezed()); | ||||
178 | QEXPECT_FAIL("", "To fix: Respect chrome in sizeHint()", Abort); | ||||
179 | QCOMPARE(label->size().width(), labelWidth + indent); | ||||
180 | QCOMPARE(label->minimumSizeHint().width(), -1); | ||||
181 | QCOMPARE(label->sizeHint().width(), labelWidth + indent); | ||||
182 | } | ||||
183 | | ||||
184 | void KSqueezedTextLabelAutotest::testClearing() | ||||
185 | { | ||||
186 | const QScopedPointer<KSqueezedTextLabel> label(createLabel()); | ||||
187 | squeezeLabel(label.data()); | ||||
188 | | ||||
189 | QVERIFY(label->isSqueezed()); | ||||
190 | | ||||
191 | label->clear(); | ||||
192 | | ||||
193 | QVERIFY(label->text().isEmpty()); | ||||
194 | QVERIFY(label->fullText().isEmpty()); | ||||
195 | QEXPECT_FAIL("", "To fix: Reset tooltip in clear()", Continue); | ||||
196 | QVERIFY(label->toolTip().isEmpty()); | ||||
197 | QVERIFY(!label->isSqueezed()); | ||||
198 | } | ||||
199 | | ||||
200 | void KSqueezedTextLabelAutotest::testChrome_data() | ||||
201 | { | ||||
202 | const QFontMetrics fm(KSqueezedTextLabel().fontMetrics()); | ||||
203 | const int xWidth = fm.width(QLatin1Char('x')) / 2; | ||||
204 | | ||||
205 | QTest::addColumn<QString>("attribute"); | ||||
206 | QTest::addColumn<int>("amount"); | ||||
207 | QTest::addColumn<int>("widthDifference"); | ||||
208 | | ||||
209 | QTest::newRow("indent") << "indent" << 20 << 20; | ||||
210 | QTest::newRow("margin") << "margin" << 20 << 40; | ||||
211 | QTest::newRow("frame") << "lineWidth" << 20 << 40 + xWidth; | ||||
212 | } | ||||
213 | | ||||
214 | void KSqueezedTextLabelAutotest::testChrome() | ||||
215 | { | ||||
216 | QFETCH(QString, attribute); | ||||
217 | QFETCH(int, amount); | ||||
218 | QFETCH(int, widthDifference); | ||||
219 | | ||||
220 | const QScopedPointer<KSqueezedTextLabel> label(createLabel()); | ||||
221 | label->setFrameStyle(QFrame::Box); | ||||
222 | label->setLineWidth(0); | ||||
223 | const int oldWidth = label->width(); | ||||
224 | | ||||
225 | QVERIFY(!label->isSqueezed()); | ||||
226 | | ||||
227 | label->setProperty(attribute.toLatin1().data(), amount); | ||||
228 | QTest::qWaitForWindowExposed(label.data()); | ||||
229 | | ||||
230 | QEXPECT_FAIL("", "To fix: Respect chrome in squeezeTextToLabel()", Abort); | ||||
231 | QVERIFY(label->isSqueezed()); | ||||
232 | | ||||
233 | label->adjustSize(); | ||||
234 | | ||||
235 | QVERIFY(!label->isSqueezed()); | ||||
margin, indent, and lineWidth are all Q_PROPERTYs, maybe you can use QObject::setProperty(const char *, QVariant) to set the values instead of using a function pointer? The function pointer is a bit ugly... Example: dhaumann: margin, indent, and lineWidth are all Q_PROPERTYs, maybe you can use QObject::setProperty(const… | |||||
Looking at your other change requests: You may want to ignore the note about using setProperty(), since this will call QLabel::setIndent(), and not the one that you add in KSqueezedTextLabel (since setIndent() etc. are not virtual). Maybe this needs further discussion to find the best solution. dhaumann: Looking at your other change requests: You may want to ignore the note about using setProperty… | |||||
I like the you suggestion nevertheless. For D7164, we could just set indent, margin and lineWidth as Q_PROPERTIES again. According to [1], this should be BC. This would bring the set of properties of KSqueezedTextLabel more in line with those of QLabel and QFrame, too. [1] https://community.kde.org/Policies/Binary_Compatibility_Issues_With_C%2B%2B#The_Do.27s_and_Don.27ts rkflx: I like the you suggestion nevertheless. For D7164, we could just set indent, margin and… | |||||
236 | QCOMPARE(label->width(), oldWidth + widthDifference); | ||||
237 | } | ||||
238 | | ||||
239 | QTEST_MAIN(KSqueezedTextLabelAutotest) | ||||
240 | | ||||
241 | | ||||
242 | // TODO | ||||
243 | // currently untested, may need clarification/definition of correct behaviour: | ||||
244 | // - multiline text | ||||
245 | // - setWordWrap | ||||
246 | // - setAlignment | ||||
247 | // - setToolTip | ||||
248 | // - context menu | ||||
249 | // - Qt::TextInteractionFlags, keyboard/mouse interaction | ||||
250 | // - linkHovered/linkActivated | ||||
251 | // - setPixmap |
To me, it was not immediately clear whether amount referts to pixel or to characters. So you may want to rename the parameter to pixelAmount or pxAmout or amountInPixels, or maybe even simply "pixels".