File: ktooltiphelper_unittest.cpp

package info (click to toggle)
kf6-kxmlgui 6.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,992 kB
  • sloc: cpp: 16,703; xml: 45; python: 41; ansic: 22; sh: 18; makefile: 7
file content (223 lines) | stat: -rw-r--r-- 8,667 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
    This file is part of the KDE libraries
    SPDX-FileCopyrightText: 2021 Felix Ernst <fe.a.ernst@gmail.com>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#include "ktooltiphelper_unittest.h"

#include <KToolTipHelper>

#include <QTest>

#include <QAction>
#include <QApplication>
#include <QFrame>
#include <QHBoxLayout>
#include <QLabel>
#include <QMenu>
#include <QStyle>
#include <QToolButton>
#include <QToolTip>
#include <QWhatsThis>

#include <memory>

inline bool isWindowsCI()
{
#ifdef Q_OS_WIN
    return qEnvironmentVariable("CI") == QLatin1String("true");
#else
    return false;
#endif
}

QString KToolTipHelper_UnitTest::shownToolTip(QWidget *widget)
{
    QTest::mouseMove(m_frameWithoutToolTip);
    const bool resetWorked = QTest::qWaitFor(
        []() {
            return !QToolTip::isVisible();
        },
        3000);
    if (!resetWorked) {
        qWarning("The tooltip did not properly hide itself after moving to an area without tooltip.");
    }

    QTest::mouseMove(widget);
    if (!QTest::qWaitFor(&QToolTip::isVisible, widget->style()->styleHint(QStyle::SH_ToolTip_WakeUpDelay, nullptr, widget) + 1000)) {
        return QStringLiteral("");
    }
    return QToolTip::text();
}

void KToolTipHelper_UnitTest::initTestCase()
{
    if (isWindowsCI()) {
        QSKIP("GUI Tests on Windows CI are not supported");
    }

    m_window.reset(new QMainWindow());
    m_centralWidget = new QWidget(m_window.get());
    m_centralWidget->setGeometry(0, 0, 100, 100);
    m_window->setCentralWidget(m_centralWidget);

    QHBoxLayout *layout = new QHBoxLayout(m_centralWidget);

    m_frame = new QFrame(m_centralWidget);
    m_frame->setFrameStyle(3);
    new QLabel(QStringLiteral("test area"), m_frame);
    layout->addWidget(m_frame);

    m_toolButton = new QToolButton(m_centralWidget);
    layout->addWidget(m_toolButton);

    m_frameWithoutToolTip = new QFrame(m_centralWidget);
    m_frameWithoutToolTip->setFrameStyle(2);
    new QLabel(QStringLiteral("no tooltip"), m_frameWithoutToolTip);
    layout->addWidget(m_frameWithoutToolTip);

    qApp->installEventFilter(KToolTipHelper::instance());

    m_window->show();
    m_window->ensurePolished();
}

void KToolTipHelper_UnitTest::testGeneralWidget()
{
    // tests without whatsThis()
    QVERIFY(shownToolTip(m_frameWithoutToolTip).isEmpty());
    m_frame->setToolTip(QStringLiteral("frame's tooltip"));
    QCOMPARE(shownToolTip(m_frame), m_frame->toolTip());

    QHelpEvent *helpEvent = new QHelpEvent(QEvent::ToolTip, QPoint(1, 1), m_frame->mapToGlobal(QPoint(1, 1)));
    QVERIFY2(!KToolTipHelper::instance()->eventFilter(m_frame, helpEvent),
             "These most basic tooltips should not be filtered so applications can still rely"
             "on tooltip events in most cases.");

    // tests with whatsThis()
    m_frame->setToolTip(QStringLiteral(""));
    m_frame->setWhatsThis(QStringLiteral("frame's whatsThis"));
    QVERIFY2(shownToolTip(m_frame).isEmpty(), "No whatsThisHint should be shown when no toolTip was set.");

    m_frame->setToolTip(KToolTipHelper::whatsThisHintOnly());
    QVERIFY2(shownToolTip(m_frame) != KToolTipHelper::whatsThisHintOnly(),
             "The KToolTipHelper::whatsThisHintOnly()-string is no user-facing string"
             "and should therefore never actually be displayed.");

    // test when whatsThis() == toolTip()
    m_frame->setToolTip(QStringLiteral("frame's whatsThis"));
    const QString noWhatsThisToolTip = shownToolTip(m_frame);
    QVERIFY(noWhatsThisToolTip.contains(m_frame->toolTip()));
    QVERIFY2(noWhatsThisToolTip.length() == m_frame->toolTip().length(), "No whatsThisHint should be shown when the toolTip is a similar string.");

    m_frame->setToolTip(QStringLiteral("frame's tooltip"));
    const QString toolTip = shownToolTip(m_frame);
    QVERIFY(toolTip.contains(m_frame->toolTip()));
    QVERIFY2(toolTip.length() > m_frame->toolTip().length(), "The frame's toolTip is supposed to contain the whatsThisHint.");

    auto layout = new QVBoxLayout(m_frame);
    auto subFrame = std::unique_ptr<QFrame>(new QFrame(m_frame));
    new QLabel(QStringLiteral("subFrame"), subFrame.get());
    layout->addWidget(subFrame.get());
    QCOMPARE(shownToolTip(subFrame.get()), toolTip);

    subFrame->setToolTip(QStringLiteral("subFrame's tooltip"));
    QCOMPARE(shownToolTip(subFrame.get()), subFrame->toolTip());
}

void KToolTipHelper_UnitTest::testInvokingWhatsThis()
{
    m_frame->setWhatsThis(QStringLiteral("frame's whatsThis"));
    m_frame->setToolTip(KToolTipHelper::whatsThisHintOnly());
    shownToolTip(m_frame);
    QTest::keyClick(m_frame, Qt::Key_Shift);
    QVERIFY2(QTest::qWaitFor(
                 []() {
                     return !QToolTip::isVisible();
                 },
                 4000),
             "whatsThis should be shown now.");
    QVERIFY2(shownToolTip(m_frame).isEmpty(),
             "A toolTip was shown which shouldn't be possible because a WhatsThis widget"
             "should be displayed at this moment.");
    QWhatsThis::hideText();
    QVERIFY2(!shownToolTip(m_frame).isEmpty(),
             "A toolTip was not shown although this should now be possible again "
             "because the WhatsThis widget was just hidden.");
}

void KToolTipHelper_UnitTest::testQToolButton()
{
    QVERIFY(shownToolTip(m_toolButton).isEmpty());

    auto action = std::unique_ptr<QAction>(new QAction(QStringLiteral("action")));
    action->setToolTip(QStringLiteral("action's tooltip"));
    m_toolButton->setDefaultAction(action.get());
    QCOMPARE(shownToolTip(m_toolButton), action->toolTip());

    auto helpEvent = std::unique_ptr<QHelpEvent>(new QHelpEvent(QEvent::ToolTip, QPoint(1, 1), m_toolButton->mapToGlobal(QPoint(1, 1))));
    QVERIFY2(!KToolTipHelper::instance()->eventFilter(m_toolButton, helpEvent.get()),
             "These most basic tooltips should not be filtered so applications can still rely"
             "on tooltip events in most cases.");

    action->setShortcut(Qt::CTRL | Qt::Key_K);
    const QString toolTip(shownToolTip(m_toolButton));
    QVERIFY(toolTip.contains(action->toolTip()));
    // qDebug("%s > %s", qPrintable(toolTip), qPrintable(action->toolTip()));
    QVERIFY2(toolTip.length() > action->toolTip().length(), "The Keyboard shortcut should be visible.");

    action->setWhatsThis(QStringLiteral("action's whatsThis"));
    const QString toolTipWithWhatsThisHint(shownToolTip(m_toolButton));
    QVERIFY(toolTipWithWhatsThisHint.contains(toolTip));
    // qDebug("%s > %s", qPrintable(toolTipWithWhatsThisHint), qPrintable(toolTip));
    QVERIFY2(toolTipWithWhatsThisHint.length() > toolTip.length(), "The whatsThisHint should be visible.");

    action->setShortcut(QKeySequence());
    QVERIFY(shownToolTip(m_toolButton).length() < toolTipWithWhatsThisHint.length());

    action->setWhatsThis(QStringLiteral(""));
    QCOMPARE(shownToolTip(m_toolButton), action->toolTip());

    action->setToolTip(KToolTipHelper::whatsThisHintOnly());
    QVERIFY2(shownToolTip(m_toolButton).isEmpty(), "It should not show the whatsThisHint if there is no whatsThis text.");

    action->setWhatsThis(QStringLiteral("action's whatsThis"));
    QVERIFY2(!shownToolTip(m_toolButton).isEmpty(), "The whatsThisHint should be shown.");
}

void KToolTipHelper_UnitTest::testQMenu()
{
    auto menu = std::unique_ptr<QMenu>(new QMenu(m_centralWidget));
    auto action = std::unique_ptr<QAction>(new QAction(QStringLiteral("action")));
    action->setDisabled(true); // The tooltip should also be shown for disabled actions.
    menu->addAction(action.get());
    menu->show();
    QVERIFY(shownToolTip(menu.get()).isEmpty());

    action->setToolTip(QStringLiteral("action's tooltip"));
    const QString toolTip(shownToolTip(menu.get()));
    QCOMPARE(toolTip, action->toolTip());
    action->setShortcut(Qt::CTRL | Qt::Key_K);
    QCOMPARE(shownToolTip(menu.get()), toolTip);

    action->setWhatsThis(QStringLiteral("action's whatsThis"));
    const QString toolTipWithWhatsThisHint(shownToolTip(menu.get()));
    QVERIFY2(toolTipWithWhatsThisHint.length() > toolTip.length(), "The tooltip is supposed to contain a whatsThisHint.");

    action->setToolTip(KToolTipHelper::whatsThisHintOnly());
    QVERIFY(shownToolTip(menu.get()).length() < toolTipWithWhatsThisHint.length());

    action->setWhatsThis(QStringLiteral(""));
    QVERIFY(shownToolTip(menu.get()).isEmpty());
}

void KToolTipHelper_UnitTest::cleanupTestCase()
{
    qApp->removeEventFilter(KToolTipHelper::instance());
}

QTEST_MAIN(KToolTipHelper_UnitTest)

#include "moc_ktooltiphelper_unittest.cpp"