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#include <QStyleOption>
32
33
34class tst_QStyleOption: public QObject
35{
36 Q_OBJECT
37
38private slots:
39 void qstyleoptioncast_data();
40 void qstyleoptioncast();
41};
42
43// Just a simple container for QStyleOption-pointer
44struct StyleOptionPointerBase
45{
46 QStyleOption *pointer;
47
48 StyleOptionPointerBase(QStyleOption *p = 0) : pointer(p) { }
49
50 virtual ~StyleOptionPointerBase() { pointer = 0; }
51};
52
53template <typename T>
54struct StyleOptionPointer: public StyleOptionPointerBase
55{
56 StyleOptionPointer(T *p = 0): StyleOptionPointerBase(p) {}
57 ~StyleOptionPointer() { delete static_cast<T *>(pointer); pointer = 0; }
58};
59
60Q_DECLARE_METATYPE(StyleOptionPointerBase*)
61
62template <typename T>
63inline StyleOptionPointerBase *stylePtr(T *ptr) { return new StyleOptionPointer<T>(ptr); }
64
65void tst_QStyleOption::qstyleoptioncast_data()
66{
67 QTest::addColumn<StyleOptionPointerBase *>(name: "testOption");
68 QTest::addColumn<bool>(name: "canCastToComplex");
69 QTest::addColumn<int>(name: "type");
70
71 QTest::newRow(dataTag: "optionDefault") << stylePtr(ptr: new QStyleOption) << false << int(QStyleOption::SO_Default);
72 QTest::newRow(dataTag: "optionButton") << stylePtr(ptr: new QStyleOptionButton) << false << int(QStyleOption::SO_Button);
73 QTest::newRow(dataTag: "optionComboBox") << stylePtr(ptr: new QStyleOptionComboBox) << true << int(QStyleOption::SO_ComboBox);
74 QTest::newRow(dataTag: "optionComplex") << stylePtr(ptr: new QStyleOptionComplex) << true << int(QStyleOption::SO_Complex);
75 QTest::newRow(dataTag: "optionDockWidget") << stylePtr(ptr: new QStyleOptionDockWidget) << false << int(QStyleOption::SO_DockWidget);
76 QTest::newRow(dataTag: "optionFocusRect") << stylePtr(ptr: new QStyleOptionFocusRect) << false << int(QStyleOption::SO_FocusRect);
77 QTest::newRow(dataTag: "optionFrame") << stylePtr(ptr: new QStyleOptionFrame) << false << int(QStyleOption::SO_Frame);
78 QTest::newRow(dataTag: "optionHeader") << stylePtr(ptr: new QStyleOptionHeader) << false << int(QStyleOption::SO_Header);
79 QTest::newRow(dataTag: "optionMenuItem") << stylePtr(ptr: new QStyleOptionMenuItem) << false << int(QStyleOption::SO_MenuItem);
80 QTest::newRow(dataTag: "optionProgressBar") << stylePtr(ptr: new QStyleOptionProgressBar) << false << int(QStyleOption::SO_ProgressBar);
81 QTest::newRow(dataTag: "optionSlider") << stylePtr(ptr: new QStyleOptionSlider) << true << int(QStyleOption::SO_Slider);
82 QTest::newRow(dataTag: "optionSpinBox") << stylePtr(ptr: new QStyleOptionSpinBox) << true << int(QStyleOption::SO_SpinBox);
83 QTest::newRow(dataTag: "optionTab") << stylePtr(ptr: new QStyleOptionTab) << false << int(QStyleOption::SO_Tab);
84 QTest::newRow(dataTag: "optionTitleBar") << stylePtr(ptr: new QStyleOptionTitleBar) << true << int(QStyleOption::SO_TitleBar);
85 QTest::newRow(dataTag: "optionToolBox") << stylePtr(ptr: new QStyleOptionToolBox) << false << int(QStyleOption::SO_ToolBox);
86 QTest::newRow(dataTag: "optionToolButton") << stylePtr(ptr: new QStyleOptionToolButton) << true << int(QStyleOption::SO_ToolButton);
87 QTest::newRow(dataTag: "optionViewItem") << stylePtr(ptr: new QStyleOptionViewItem) << false << int(QStyleOption::SO_ViewItem);
88 QTest::newRow(dataTag: "optionGraphicsItem") << stylePtr(ptr: new QStyleOptionGraphicsItem) << false << int(QStyleOption::SO_GraphicsItem);
89}
90
91void tst_QStyleOption::qstyleoptioncast()
92{
93 QFETCH(StyleOptionPointerBase *, testOption);
94 QFETCH(bool, canCastToComplex);
95 QFETCH(int, type);
96
97 QVERIFY(testOption->pointer != 0);
98
99 QCOMPARE(testOption->pointer->type, type);
100
101 // Cast to common base class
102 QStyleOption *castOption = qstyleoption_cast<QStyleOption*>(opt: testOption->pointer);
103 QVERIFY(castOption != 0);
104
105 // Cast to complex base class
106 castOption = qstyleoption_cast<QStyleOptionComplex*>(opt: testOption->pointer);
107 QCOMPARE(canCastToComplex, (castOption != 0));
108
109 // Cast to combo box
110 castOption = qstyleoption_cast<QStyleOptionComboBox*>(opt: testOption->pointer);
111 QCOMPARE((castOption != 0),(testOption->pointer->type == QStyleOption::SO_ComboBox));
112
113 // Cast to button
114 castOption = qstyleoption_cast<QStyleOptionButton*>(opt: testOption->pointer);
115 QCOMPARE((castOption != 0),(testOption->pointer->type == QStyleOption::SO_Button));
116
117 // Cast to lower version
118 testOption->pointer->version += 1;
119 castOption = qstyleoption_cast<QStyleOption*>(opt: testOption->pointer);
120 QVERIFY(castOption);
121
122 // Cast a null pointer
123 castOption = qstyleoption_cast<QStyleOption*>(opt: (QStyleOption*)0);
124 QCOMPARE(castOption, nullptr);
125
126 // Deallocate
127 delete testOption;
128}
129
130QTEST_MAIN(tst_QStyleOption)
131#include "tst_qstyleoption.moc"
132
133

source code of qtbase/tests/auto/widgets/styles/qstyleoption/tst_qstyleoption.cpp