~gabriel1984sibiu/minitube/qt5.6

« back to all changes in this revision

Viewing changes to tests/auto/widgets/widgets/qtoolbutton/tst_qtoolbutton.cpp

  • Committer: Grevutiu Gabriel
  • Date: 2017-06-13 08:43:17 UTC
  • Revision ID: gabriel1984sibiu@gmail.com-20170613084317-ek0zqe0u9g3ocvi8
OriginalĀ upstreamĀ code

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2016 The Qt Company Ltd.
 
4
** Contact: https://www.qt.io/licensing/
 
5
**
 
6
** This file is part of the test suite of the Qt Toolkit.
 
7
**
 
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.
 
16
**
 
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.
 
24
**
 
25
** $QT_END_LICENSE$
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
 
 
30
#include <QtTest/QtTest>
 
31
 
 
32
#include <qcoreapplication.h>
 
33
#include <qdebug.h>
 
34
#include <qtoolbutton.h>
 
35
#include <qmenu.h>
 
36
#include <qaction.h>
 
37
#include <qstyleoption.h>
 
38
#include <qscreen.h>
 
39
#include <qlabel.h>
 
40
 
 
41
class tst_QToolButton : public QObject
 
42
{
 
43
Q_OBJECT
 
44
 
 
45
public:
 
46
    tst_QToolButton();
 
47
    virtual ~tst_QToolButton();
 
48
 
 
49
private slots:
 
50
    void getSetCheck();
 
51
    void triggered();
 
52
    void collapseTextOnPriority();
 
53
    void task230994_iconSize();
 
54
    void task176137_autoRepeatOfAction();
 
55
    void qtbug_26956_popupTimerDone();
 
56
    void qtbug_34759_sizeHintResetWhenSettingMenu();
 
57
 
 
58
protected slots:
 
59
    void sendMouseClick();
 
60
private:
 
61
    QPointer<QWidget> m_menu;
 
62
};
 
63
 
 
64
tst_QToolButton::tst_QToolButton()
 
65
{
 
66
}
 
67
 
 
68
tst_QToolButton::~tst_QToolButton()
 
69
{
 
70
}
 
71
 
 
72
// Testing get/set functions
 
73
void tst_QToolButton::getSetCheck()
 
74
{
 
75
    QToolButton obj1;
 
76
    // QMenu* QToolButton::menu()
 
77
    // void QToolButton::setMenu(QMenu*)
 
78
    QMenu *var1 = new QMenu;
 
79
    obj1.setMenu(var1);
 
80
    QCOMPARE(var1, obj1.menu());
 
81
    obj1.setMenu((QMenu *)0);
 
82
    QCOMPARE((QMenu *)0, obj1.menu());
 
83
    delete var1;
 
84
 
 
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());
 
93
 
 
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());
 
100
 
 
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());
 
108
    delete var4;
 
109
}
 
110
 
 
111
void tst_QToolButton::triggered()
 
112
{
 
113
    qRegisterMetaType<QAction *>("QAction *");
 
114
    QWidget mainWidget;
 
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);
 
124
 
 
125
    toolButton->setMenu(menu.data());
 
126
    toolButton->setDefaultAction(defaultAction);
 
127
 
 
128
    mainWidget.show();
 
129
    QApplication::setActiveWindow(&mainWidget);
 
130
    QVERIFY(QTest::qWaitForWindowActive(&mainWidget));
 
131
 
 
132
    defaultAction->trigger();
 
133
    QCOMPARE(spy.count(),1);
 
134
    QCOMPARE(qvariant_cast<QAction *>(spy.at(0).at(0)), defaultAction);
 
135
 
 
136
    m_menu = menu.data();
 
137
 
 
138
    QTimer *timer = new QTimer(this);
 
139
    timer->setInterval(50);
 
140
    connect(timer, SIGNAL(timeout()), this, SLOT(sendMouseClick()));
 
141
    timer->start();
 
142
    QTimer::singleShot(10000, &mainWidget, SLOT(close())); // Emergency bail-out
 
143
    toolButton->showMenu();
 
144
    QTest::qWait(20);
 
145
    QCOMPARE(spy.count(),2);
 
146
    QCOMPARE(qvariant_cast<QAction *>(spy.at(1).at(0)), one);
 
147
}
 
148
 
 
149
void tst_QToolButton::collapseTextOnPriority()
 
150
{
 
151
    class MyToolButton : public QToolButton
 
152
    {
 
153
        friend class tst_QToolButton;
 
154
    public:
 
155
        void initStyleOption(QStyleOptionToolButton *option)
 
156
        {
 
157
            QToolButton::initStyleOption(option);
 
158
        }
 
159
    };
 
160
 
 
161
    MyToolButton button;
 
162
    button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
 
163
    QAction action(button.style()->standardIcon(QStyle::SP_ArrowBack), "test", 0);
 
164
    button.setDefaultAction(&action);
 
165
 
 
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);
 
172
}
 
173
 
 
174
 
 
175
void tst_QToolButton::task230994_iconSize()
 
176
{
 
177
    //we check that the iconsize returned bu initStyleOption is valid
 
178
    //when the toolbutton has no parent
 
179
    class MyToolButton : public QToolButton
 
180
    {
 
181
        friend class tst_QToolButton;
 
182
    public:
 
183
        void initStyleOption(QStyleOptionToolButton *option)
 
184
        {
 
185
            QToolButton::initStyleOption(option);
 
186
        }
 
187
    };
 
188
 
 
189
    MyToolButton button;
 
190
    QStyleOptionToolButton option;
 
191
    button.initStyleOption(&option);
 
192
    QVERIFY(option.iconSize.isValid());
 
193
}
 
194
 
 
195
void tst_QToolButton::task176137_autoRepeatOfAction()
 
196
{
 
197
    QAction action(0);
 
198
    QWidget mainWidget;
 
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);
 
205
    label->move(0, 50);
 
206
 
 
207
    mainWidget.show();
 
208
    QApplication::setActiveWindow(&mainWidget);
 
209
    QVERIFY(QTest::qWaitForWindowActive(&mainWidget));
 
210
 
 
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);
 
215
 
 
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")
 
226
            .arg(expected)
 
227
            .arg(repeatSpy.count())
 
228
            .arg(diff)));
 
229
}
 
230
 
 
231
 
 
232
void tst_QToolButton::sendMouseClick()
 
233
{
 
234
    if (m_menu.isNull()) {
 
235
        qWarning("m_menu is NULL");
 
236
        return;
 
237
    }
 
238
    if (!m_menu->isVisible())
 
239
        return;
 
240
    QTest::mouseClick(m_menu.data(), Qt::LeftButton, 0, QPoint(7, 7));
 
241
    if (QTimer *timer = qobject_cast<QTimer *>(sender())) {
 
242
        timer->stop();
 
243
        timer->deleteLater();
 
244
    }
 
245
}
 
246
 
 
247
void tst_QToolButton::qtbug_26956_popupTimerDone()
 
248
{
 
249
    QToolButton *tb = new QToolButton;
 
250
    tb->setMenu(new QMenu(tb));
 
251
    tb->menu()->addAction("Qt");
 
252
    tb->deleteLater();
 
253
    tb->showMenu();
 
254
}
 
255
 
 
256
void tst_QToolButton::qtbug_34759_sizeHintResetWhenSettingMenu()
 
257
{
 
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
 
261
 
 
262
    QToolButton button1;
 
263
    QToolButton button2;
 
264
 
 
265
    button1.setToolButtonStyle(Qt::ToolButtonIconOnly);
 
266
    button1.setPopupMode(QToolButton::MenuButtonPopup);
 
267
 
 
268
    button2.setToolButtonStyle(Qt::ToolButtonIconOnly);
 
269
    button2.setPopupMode(QToolButton::MenuButtonPopup);
 
270
 
 
271
    button2.setMenu(new QMenu(&button2));
 
272
 
 
273
    button1.show();
 
274
    button2.show();
 
275
 
 
276
    QVERIFY(QTest::qWaitForWindowExposed(&button1));
 
277
    QVERIFY(QTest::qWaitForWindowExposed(&button2));
 
278
 
 
279
    button1.setMenu(new QMenu(&button1));
 
280
    QTRY_COMPARE(button1.sizeHint(), button2.sizeHint());
 
281
}
 
282
 
 
283
QTEST_MAIN(tst_QToolButton)
 
284
#include "tst_qtoolbutton.moc"