1
/****************************************************************************
3
** Copyright (C) 2016 The Qt Company Ltd.
4
** Contact: https://www.qt.io/licensing/
6
** This file is part of the test suite of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and The Qt Company. For licensing terms
14
** and conditions see https://www.qt.io/terms-conditions. For further
15
** information use the contact form at https://www.qt.io/contact-us.
17
** GNU General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU
19
** General Public License version 3 as published by the Free Software
20
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21
** included in the packaging of this file. Please review the following
22
** information to ensure the GNU General Public License requirements will
23
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
27
****************************************************************************/
30
#include <QtTest/QtTest>
32
#include <qcoreapplication.h>
34
#include <qtoolbutton.h>
37
#include <qstyleoption.h>
41
class tst_QToolButton : public QObject
47
virtual ~tst_QToolButton();
52
void collapseTextOnPriority();
53
void task230994_iconSize();
54
void task176137_autoRepeatOfAction();
55
void qtbug_26956_popupTimerDone();
56
void qtbug_34759_sizeHintResetWhenSettingMenu();
59
void sendMouseClick();
61
QPointer<QWidget> m_menu;
64
tst_QToolButton::tst_QToolButton()
68
tst_QToolButton::~tst_QToolButton()
72
// Testing get/set functions
73
void tst_QToolButton::getSetCheck()
76
// QMenu* QToolButton::menu()
77
// void QToolButton::setMenu(QMenu*)
78
QMenu *var1 = new QMenu;
80
QCOMPARE(var1, obj1.menu());
81
obj1.setMenu((QMenu *)0);
82
QCOMPARE((QMenu *)0, obj1.menu());
85
// ToolButtonPopupMode QToolButton::popupMode()
86
// void QToolButton::setPopupMode(ToolButtonPopupMode)
87
obj1.setPopupMode(QToolButton::ToolButtonPopupMode(QToolButton::DelayedPopup));
88
QCOMPARE(QToolButton::ToolButtonPopupMode(QToolButton::DelayedPopup), obj1.popupMode());
89
obj1.setPopupMode(QToolButton::ToolButtonPopupMode(QToolButton::MenuButtonPopup));
90
QCOMPARE(QToolButton::ToolButtonPopupMode(QToolButton::MenuButtonPopup), obj1.popupMode());
91
obj1.setPopupMode(QToolButton::ToolButtonPopupMode(QToolButton::InstantPopup));
92
QCOMPARE(QToolButton::ToolButtonPopupMode(QToolButton::InstantPopup), obj1.popupMode());
94
// bool QToolButton::autoRaise()
95
// void QToolButton::setAutoRaise(bool)
96
obj1.setAutoRaise(false);
97
QCOMPARE(false, obj1.autoRaise());
98
obj1.setAutoRaise(true);
99
QCOMPARE(true, obj1.autoRaise());
101
// QAction * QToolButton::defaultAction()
102
// void QToolButton::setDefaultAction(QAction *)
103
QAction *var4 = new QAction(0);
104
obj1.setDefaultAction(var4);
105
QCOMPARE(var4, obj1.defaultAction());
106
obj1.setDefaultAction((QAction *)0);
107
QCOMPARE((QAction *)0, obj1.defaultAction());
111
void tst_QToolButton::triggered()
113
qRegisterMetaType<QAction *>("QAction *");
115
mainWidget.setWindowTitle(QStringLiteral("triggered"));
116
mainWidget.resize(200, 200);
117
mainWidget.move(QGuiApplication::primaryScreen()->availableGeometry().center() - QPoint(100, 100));
118
QToolButton *toolButton = new QToolButton(&mainWidget);
119
QSignalSpy spy(toolButton,SIGNAL(triggered(QAction*)));
120
QScopedPointer<QMenu> menu(new QMenu(QStringLiteral("Menu")));
121
QAction *one = menu->addAction("one");
122
menu->addAction("two");
123
QAction *defaultAction = new QAction(QStringLiteral("def"), this);
125
toolButton->setMenu(menu.data());
126
toolButton->setDefaultAction(defaultAction);
129
QApplication::setActiveWindow(&mainWidget);
130
QVERIFY(QTest::qWaitForWindowActive(&mainWidget));
132
defaultAction->trigger();
133
QCOMPARE(spy.count(),1);
134
QCOMPARE(qvariant_cast<QAction *>(spy.at(0).at(0)), defaultAction);
136
m_menu = menu.data();
138
QTimer *timer = new QTimer(this);
139
timer->setInterval(50);
140
connect(timer, SIGNAL(timeout()), this, SLOT(sendMouseClick()));
142
QTimer::singleShot(10000, &mainWidget, SLOT(close())); // Emergency bail-out
143
toolButton->showMenu();
145
QCOMPARE(spy.count(),2);
146
QCOMPARE(qvariant_cast<QAction *>(spy.at(1).at(0)), one);
149
void tst_QToolButton::collapseTextOnPriority()
151
class MyToolButton : public QToolButton
153
friend class tst_QToolButton;
155
void initStyleOption(QStyleOptionToolButton *option)
157
QToolButton::initStyleOption(option);
162
button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
163
QAction action(button.style()->standardIcon(QStyle::SP_ArrowBack), "test", 0);
164
button.setDefaultAction(&action);
166
QStyleOptionToolButton option;
167
button.initStyleOption(&option);
168
QCOMPARE(option.toolButtonStyle, Qt::ToolButtonTextBesideIcon);
169
action.setPriority(QAction::LowPriority);
170
button.initStyleOption(&option);
171
QCOMPARE(option.toolButtonStyle, Qt::ToolButtonIconOnly);
175
void tst_QToolButton::task230994_iconSize()
177
//we check that the iconsize returned bu initStyleOption is valid
178
//when the toolbutton has no parent
179
class MyToolButton : public QToolButton
181
friend class tst_QToolButton;
183
void initStyleOption(QStyleOptionToolButton *option)
185
QToolButton::initStyleOption(option);
190
QStyleOptionToolButton option;
191
button.initStyleOption(&option);
192
QVERIFY(option.iconSize.isValid());
195
void tst_QToolButton::task176137_autoRepeatOfAction()
199
mainWidget.setWindowTitle(QStringLiteral("task176137_autoRepeatOfAction"));
200
mainWidget.resize(200, 200);
201
mainWidget.move(QGuiApplication::primaryScreen()->availableGeometry().center() - QPoint(100, 100));
202
QToolButton *toolButton = new QToolButton(&mainWidget);
203
toolButton->setDefaultAction (&action);
204
QLabel *label = new QLabel(QStringLiteral("This test takes a while."), &mainWidget);
208
QApplication::setActiveWindow(&mainWidget);
209
QVERIFY(QTest::qWaitForWindowActive(&mainWidget));
211
QSignalSpy spy(&action,SIGNAL(triggered()));
212
QTest::mousePress (toolButton, Qt::LeftButton);
213
QTest::mouseRelease (toolButton, Qt::LeftButton, 0, QPoint (), 2000);
214
QCOMPARE(spy.count(),1);
216
// try again with auto repeat
217
toolButton->setAutoRepeat (true);
218
QSignalSpy repeatSpy(&action,SIGNAL(triggered())); // new spy
219
QTest::mousePress (toolButton, Qt::LeftButton);
220
QTest::mouseRelease (toolButton, Qt::LeftButton, 0, QPoint (), 3000);
221
const qreal expected = (3000 - toolButton->autoRepeatDelay()) / toolButton->autoRepeatInterval() + 1;
222
//we check that the difference is small (on some systems timers are not super accurate)
223
qreal diff = (expected - repeatSpy.count()) / expected;
224
QVERIFY2(qAbs(diff) < 0.2, qPrintable(
225
QString("expected: %1, actual: %2, diff (fraction): %3")
227
.arg(repeatSpy.count())
232
void tst_QToolButton::sendMouseClick()
234
if (m_menu.isNull()) {
235
qWarning("m_menu is NULL");
238
if (!m_menu->isVisible())
240
QTest::mouseClick(m_menu.data(), Qt::LeftButton, 0, QPoint(7, 7));
241
if (QTimer *timer = qobject_cast<QTimer *>(sender())) {
243
timer->deleteLater();
247
void tst_QToolButton::qtbug_26956_popupTimerDone()
249
QToolButton *tb = new QToolButton;
250
tb->setMenu(new QMenu(tb));
251
tb->menu()->addAction("Qt");
256
void tst_QToolButton::qtbug_34759_sizeHintResetWhenSettingMenu()
258
// There is no reliable way of checking what's ultimately a style-dependent
259
// sizing. So the idea is checking if the size is the "correct" size w.r.t.
260
// another toolbutton which has had a menu set before it was shown for the first time
265
button1.setToolButtonStyle(Qt::ToolButtonIconOnly);
266
button1.setPopupMode(QToolButton::MenuButtonPopup);
268
button2.setToolButtonStyle(Qt::ToolButtonIconOnly);
269
button2.setPopupMode(QToolButton::MenuButtonPopup);
271
button2.setMenu(new QMenu(&button2));
276
QVERIFY(QTest::qWaitForWindowExposed(&button1));
277
QVERIFY(QTest::qWaitForWindowExposed(&button2));
279
button1.setMenu(new QMenu(&button1));
280
QTRY_COMPARE(button1.sizeHint(), button2.sizeHint());
283
QTEST_MAIN(tst_QToolButton)
284
#include "tst_qtoolbutton.moc"