Changeset View
Changeset View
Standalone View
Standalone View
autotests/ktooltipwidgettest.cpp
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | * Copyright (c) 2017 Elvis Angelaccio <elvis.angelaccio@kde.org> | 2 | * Copyright (c) 2017 Elvis Angelaccio <elvis.angelaccio@kde.org> | ||
3 | * Copyright (c) 2018 Michael Heidelbach <ottwolt@gmail.com> | ||||
3 | * | 4 | * | ||
4 | * This library is free software; you can redistribute it and/or | 5 | * This library is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Lesser General Public | 6 | * modify it under the terms of the GNU Lesser General Public | ||
6 | * License as published by the Free Software Foundation; either | 7 | * License as published by the Free Software Foundation; either | ||
7 | * version 2.1 of the License, or (at your option) any later version. | 8 | * version 2.1 of the License, or (at your option) any later version. | ||
8 | * | 9 | * | ||
9 | * This library is distributed in the hope that it will be useful, | 10 | * This library is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | * Lesser General Public License for more details. | 13 | * Lesser General Public License for more details. | ||
13 | * | 14 | * | ||
14 | * You should have received a copy of the GNU Lesser General Public | 15 | * You should have received a copy of the GNU Lesser General Public | ||
15 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | 16 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
16 | */ | 17 | */ | ||
17 | 18 | | |||
18 | #include "ktooltipwidgettest.h" | 19 | #include "ktooltipwidgettest.h" | ||
19 | 20 | | |||
20 | #include <KToolTipWidget> | 21 | #include <KToolTipWidget> | ||
21 | 22 | | |||
23 | #include <QWindow> | ||||
24 | #include <QDesktopWidget> | ||||
25 | #include <QScreen> | ||||
26 | #include <QLayout> | ||||
27 | #include <QStyle> | ||||
22 | #include <QLabel> | 28 | #include <QLabel> | ||
23 | #include <QLineEdit> | 29 | #include <QLineEdit> | ||
24 | #include <QSignalSpy> | 30 | #include <QSignalSpy> | ||
25 | #include <QTest> | 31 | #include <QTest> | ||
26 | 32 | | |||
33 | void KToolTipWidgetTest::initTestCase() | ||||
34 | { | ||||
35 | m_screenGeometry = QGuiApplication::primaryScreen()->geometry(); | ||||
36 | } | ||||
37 | | ||||
27 | void KToolTipWidgetTest::showTooltipShouldShowContent() | 38 | void KToolTipWidgetTest::showTooltipShouldShowContent() | ||
28 | { | 39 | { | ||
29 | KToolTipWidget tooltip; | 40 | KToolTipWidget tooltip; | ||
30 | QLabel label; | 41 | QLabel label; | ||
31 | 42 | | |||
32 | tooltip.showAt(QPoint(10, 10), &label, nullptr); | 43 | tooltip.showAt(QPoint(10, 10), &label, nullptr); | ||
33 | QVERIFY(tooltip.isVisible()); | 44 | QVERIFY(tooltip.isVisible()); | ||
34 | QVERIFY(label.isVisible()); | 45 | QVERIFY(label.isVisible()); | ||
▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Line(s) | 101 | { | |||
101 | QVERIFY(!label->isVisible()); | 112 | QVERIFY(!label->isVisible()); | ||
102 | QCOMPARE(label->parent(), parent); | 113 | QCOMPARE(label->parent(), parent); | ||
103 | QCOMPARE(spy.count(), 0); | 114 | QCOMPARE(spy.count(), 0); | ||
104 | 115 | | |||
105 | delete parent; | 116 | delete parent; | ||
106 | QCOMPARE(spy.count(), 1); | 117 | QCOMPARE(spy.count(), 1); | ||
107 | } | 118 | } | ||
108 | 119 | | |||
120 | void KToolTipWidgetTest::shouldNotObscureTarget_data() | ||||
121 | { | ||||
122 | QTest::addColumn<QPoint>("position"); | ||||
123 | QTest::addColumn<QString>("content"); | ||||
124 | | ||||
125 | const QMap<QString,QPoint> positions{ | ||||
126 | {QStringLiteral("topleft"), QPoint(m_offset, m_offset)}, | ||||
127 | {QStringLiteral("topright"), QPoint(-m_offset, m_offset)}, | ||||
128 | {QStringLiteral("bottomright"), QPoint(-m_offset, -m_offset)}, | ||||
129 | {QStringLiteral("bottomleft"), QPoint(m_offset, -m_offset)}, | ||||
130 | {QStringLiteral("centered"), | ||||
131 | QPoint(m_screenGeometry.width() / 2, m_screenGeometry.height() / 2)} | ||||
132 | }; | ||||
133 | | ||||
134 | QMapIterator<QString, QPoint> i(positions); | ||||
135 | while (i.hasNext()) { | ||||
136 | i.next(); | ||||
137 | QTest::newRow(qPrintable(QStringLiteral("small/%1").arg(i.key())) ) | ||||
138 | << i.value() | ||||
139 | << QStringLiteral("dummy text"); | ||||
140 | | ||||
141 | QTest::newRow(qPrintable(QStringLiteral("multiline/%1").arg(i.key()))) | ||||
142 | << i.value() | ||||
143 | << QStringLiteral("dummy text\nLine 1\nLine 2\nLine 3"); | ||||
144 | | ||||
145 | QTest::newRow(qPrintable(QStringLiteral("one long line/%1").arg(i.key()))) | ||||
146 | << i.value() | ||||
147 | << QStringLiteral( | ||||
148 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam nec felis sed elit auctor lobortis non a urna. Quisque non posuere mauris. Suspendisse potenti. In diam leo, lobortis at placerat nec, sagittis at tortor. Pellentesque scelerisque enim vel elementum scelerisque. Integer eget lectus vitae lorem pulvinar hendrerit. Suspendisse auctor sapien vel semper porta. Vestibulum fringilla aliquet tincidunt. Maecenas mollis mauris et erat viverra mollis. Proin suscipit felis nisi, a dapibus est hendrerit euismod. Suspendisse quis faucibus quam. Fusce eu cursus magna, et egestas purus. Duis enim sapien, feugiat id facilisis non, rhoncus ut lectus. Aliquam at nisi vel ligula interdum ultricies. Donec condimentum ante quam, eu congue lectus pulvinar in. Cras interdum, neque quis fermentum consequat, lectus tellus eleifend turpis." | ||||
149 | ); | ||||
150 | | ||||
151 | if (m_screenGeometry.width() >= 1600 && m_screenGeometry.height() >= 900) { | ||||
152 | QTest::newRow(qPrintable(QStringLiteral("super large/%1").arg(i.key()))) | ||||
153 | << i.value() | ||||
154 | << QStringLiteral( | ||||
155 | "dummy 0 text\nLine 1\nLine 2\nLine 3" | ||||
156 | "dummy 1 text\nLine 1\nLine 2\nLine 3" | ||||
157 | "dummy 2 text\nLine 1\nLine 2\nLine 3" | ||||
158 | "dummy 3 text\nLine 1\nLine 2\nLine 3" | ||||
159 | "dummy 4 text\nLine 1\nLine 2\nLine 3" | ||||
160 | | ||||
161 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam nec felis sed elit auctor lobortis non a urna. Quisque non posuere mauris. Suspendisse potenti. In diam leo, lobortis at placerat nec, sagittis at tortor. Pellentesque scelerisque enim vel elementum scelerisque. Integer eget lectus vitae lorem pulvinar hendrerit. Suspendisse auctor sapien vel semper porta. Vestibulum fringilla aliquet tincidunt. Maecenas mollis mauris et erat viverra mollis. Proin suscipit felis nisi, a dapibus est hendrerit euismod. Suspendisse quis faucibus quam. Fusce eu cursus magna, et egestas purus. Duis enim sapien, feugiat id facilisis non, rhoncus ut lectus. Aliquam at nisi vel ligula interdum ultricies. Donec condimentum ante quam, eu congue lectus pulvinar in. Cras interdum, neque quis fermentum consequat, lectus tellus eleifend turpis." | ||||
162 | ); | ||||
163 | } | ||||
164 | } | ||||
165 | } | ||||
166 | | ||||
167 | void KToolTipWidgetTest::shouldNotObscureTarget() | ||||
168 | { | ||||
169 | QFETCH(QPoint, position); | ||||
170 | QFETCH(QString, content); | ||||
171 | | ||||
172 | QWidget* containerWidget = new QWidget(); | ||||
173 | const int margin = containerWidget->style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth); | ||||
174 | QLabel* targetWidget = new QLabel(QStringLiteral("dummy file")); | ||||
175 | QLayout* layout = new QHBoxLayout(containerWidget); | ||||
176 | layout->addWidget(targetWidget); | ||||
177 | | ||||
178 | containerWidget->adjustSize(); | ||||
179 | containerWidget->move( | ||||
180 | position.x() >= 0 | ||||
181 | ? position.x() | ||||
182 | : m_screenGeometry.right() + position.x() - containerWidget->width(), | ||||
183 | position.y() >= 0 | ||||
184 | ? position.y() | ||||
185 | : m_screenGeometry.bottom() + position.y() - containerWidget->height() | ||||
186 | ); | ||||
187 | containerWidget->show(); | ||||
188 | QVERIFY(QTest::qWaitForWindowActive(containerWidget)); | ||||
189 | QVERIFY(targetWidget->isVisible()); | ||||
190 | | ||||
191 | QRect targetRect = QRect(targetWidget->frameGeometry()); | ||||
192 | targetRect.moveTo(targetWidget->mapToGlobal(QPoint(0, 0))); | ||||
193 | | ||||
194 | QLabel* contentWidget = new QLabel(content); | ||||
195 | const QSize shrinkSize(2 * m_offset, 2 * m_offset); | ||||
elvisangelaccio: Don't use the `{}` constructor syntax only here, it breaks consistency. | |||||
196 | contentWidget->setMaximumSize(m_screenGeometry.size() - shrinkSize); | ||||
197 | | ||||
198 | QVERIFY2(containerWidget->windowHandle(), "Container's window handle is invalid"); | ||||
199 | KToolTipWidget tooltipWidget; | ||||
200 | tooltipWidget.showBelow(targetRect, contentWidget, containerWidget->windowHandle()); | ||||
201 | QVERIFY(QTest::qWaitForWindowActive(&tooltipWidget)); | ||||
202 | const QRect tooltipRect = tooltipWidget.frameGeometry(); | ||||
203 | | ||||
204 | qDebug() << QStringLiteral("tooltip: %1x%2 x=%3, y=%4") | ||||
205 | .arg(tooltipRect.width()) | ||||
206 | .arg(tooltipRect.height()) | ||||
207 | .arg(tooltipRect.left()) | ||||
208 | .arg(tooltipRect.top()); | ||||
209 | qDebug() << QStringLiteral("target: %1x%2 x=%3, y=%4") | ||||
210 | .arg(targetRect.width()) | ||||
211 | .arg(targetRect.height()) | ||||
212 | .arg(targetRect.left()) | ||||
213 | .arg(targetRect.top()); | ||||
214 | | ||||
215 | QVERIFY2(!tooltipRect.intersects(targetRect), "Target obscured"); | ||||
216 | QCOMPARE(tooltipRect.intersected(m_screenGeometry), tooltipRect); | ||||
217 | | ||||
218 | // Check margins | ||||
219 | if (tooltipRect.bottom() < targetRect.top()) { | ||||
220 | QCOMPARE(margin, targetRect.top() - tooltipRect.bottom()); | ||||
221 | } else if (tooltipRect.top() > targetRect.bottom()) { | ||||
222 | QCOMPARE(margin, tooltipRect.top() - targetRect.bottom()); | ||||
223 | } else if (tooltipRect.right() < targetRect.left()) { | ||||
224 | QCOMPARE(margin, targetRect.left() - tooltipRect.right()); | ||||
225 | } else if (tooltipRect.left() > targetRect.right() ) { | ||||
226 | QCOMPARE(margin, tooltipRect.left() - targetRect.right()); | ||||
227 | } | ||||
228 | | ||||
229 | tooltipWidget.hide(); | ||||
230 | QVERIFY(tooltipWidget.isHidden()); | ||||
231 | | ||||
232 | delete contentWidget; | ||||
233 | delete containerWidget; | ||||
234 | } | ||||
235 | | ||||
236 | | ||||
109 | QTEST_MAIN(KToolTipWidgetTest) | 237 | QTEST_MAIN(KToolTipWidgetTest) | ||
110 | 238 | | |||
111 | 239 | |
Don't use the {} constructor syntax only here, it breaks consistency.