1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> |
5 | ** Contact: https://www.qt.io/licensing/ |
6 | ** |
7 | ** This file is part of the test suite of the Qt Toolkit. |
8 | ** |
9 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
10 | ** Commercial License Usage |
11 | ** Licensees holding valid commercial Qt licenses may use this file in |
12 | ** accordance with the commercial license agreement provided with the |
13 | ** Software or, alternatively, in accordance with the terms contained in |
14 | ** a written agreement between you and The Qt Company. For licensing terms |
15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
16 | ** information use the contact form at https://www.qt.io/contact-us. |
17 | ** |
18 | ** GNU General Public License Usage |
19 | ** Alternatively, this file may be used under the terms of the GNU |
20 | ** General Public License version 3 as published by the Free Software |
21 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include <QTest> |
31 | #include <QFrame> |
32 | #include <QStyleOptionFrame> |
33 | #include <QPixmap> |
34 | #include <QStyle> |
35 | #include <QStyleFactory> |
36 | |
37 | class tst_QFrame : public QObject |
38 | { |
39 | Q_OBJECT |
40 | |
41 | public: |
42 | static void initMain() { QCoreApplication::setAttribute(attribute: Qt::AA_DisableHighDpiScaling); } |
43 | |
44 | private slots: |
45 | void testDefaults(); |
46 | void testInitStyleOption_data(); |
47 | void testInitStyleOption(); |
48 | void testPainting_data(); |
49 | void testPainting(); |
50 | }; |
51 | |
52 | Q_DECLARE_METATYPE(QFrame::Shape) |
53 | Q_DECLARE_METATYPE(QFrame::Shadow) |
54 | |
55 | void tst_QFrame::testDefaults() |
56 | { |
57 | QFrame frame; |
58 | QCOMPARE(frame.frameStyle(), int(QFrame::NoFrame | QFrame::Plain)); |
59 | frame.setFrameShape(QFrame::Box); |
60 | QCOMPARE(frame.frameStyle(), int(QFrame::Box | QFrame::Plain)); |
61 | frame.setFrameStyle(QFrame::Box); // no shadow specified! |
62 | QCOMPARE(frame.frameStyle(), int(QFrame::Box)); |
63 | } |
64 | |
65 | static void provideFrameData() |
66 | { |
67 | QTest::addColumn<QString>(name: "basename" ); |
68 | QTest::addColumn<int>(name: "lineWidth" ); |
69 | QTest::addColumn<int>(name: "midLineWidth" ); |
70 | QTest::addColumn<QFrame::Shape>(name: "shape" ); |
71 | QTest::addColumn<QFrame::Shadow>(name: "shadow" ); |
72 | |
73 | for (int lineWidth = 0; lineWidth < 3; ++lineWidth) { |
74 | for (int midLineWidth = 0; midLineWidth < 3; ++midLineWidth) { |
75 | const QByteArray postFix = '_' + QByteArray::number(lineWidth) + '_' |
76 | + QByteArray::number(midLineWidth); |
77 | QTest::newRow(dataTag: ("box_noshadow" + postFix).constData()) |
78 | << "box_noshadow" << lineWidth << midLineWidth << QFrame::Box << (QFrame::Shadow)0; |
79 | QTest::newRow(dataTag: ("box_plain" + postFix).constData()) |
80 | << "box_plain" << lineWidth << midLineWidth << QFrame::Box << QFrame::Plain; |
81 | QTest::newRow(dataTag: ("box_raised" + postFix).constData()) |
82 | << "box_raised" << lineWidth << midLineWidth << QFrame::Box << QFrame::Raised; |
83 | QTest::newRow(dataTag: ("box_sunken" + postFix).constData()) |
84 | << "box_sunken" << lineWidth << midLineWidth << QFrame::Box << QFrame::Sunken; |
85 | |
86 | QTest::newRow(dataTag: ("winpanel_noshadow" + postFix).constData()) |
87 | << "winpanel_noshadow" << lineWidth << midLineWidth << QFrame::WinPanel << (QFrame::Shadow)0; |
88 | QTest::newRow(dataTag: ("winpanel_plain" + postFix).constData()) |
89 | << "winpanel_plain" << lineWidth << midLineWidth << QFrame::WinPanel << QFrame::Plain; |
90 | QTest::newRow(dataTag: ("winpanel_raised" + postFix).constData()) |
91 | << "winpanel_raised" << lineWidth << midLineWidth << QFrame::WinPanel << QFrame::Raised; |
92 | QTest::newRow(dataTag: ("winpanel_sunken" + postFix).constData()) |
93 | << "winpanel_sunken" << lineWidth << midLineWidth << QFrame::WinPanel << QFrame::Sunken; |
94 | } |
95 | } |
96 | } |
97 | |
98 | class Frame : public QFrame |
99 | { |
100 | public: |
101 | using QFrame::initStyleOption; |
102 | }; |
103 | |
104 | void tst_QFrame::testInitStyleOption_data() |
105 | { |
106 | provideFrameData(); |
107 | } |
108 | |
109 | void tst_QFrame::testInitStyleOption() |
110 | { |
111 | QFETCH(QString, basename); |
112 | QFETCH(int, lineWidth); |
113 | QFETCH(int, midLineWidth); |
114 | QFETCH(QFrame::Shape, shape); |
115 | QFETCH(QFrame::Shadow, shadow); |
116 | |
117 | Frame frame; |
118 | frame.setFrameStyle(shape | shadow); |
119 | frame.setLineWidth(lineWidth); |
120 | frame.setMidLineWidth(midLineWidth); |
121 | frame.resize(w: 16, h: 16); |
122 | |
123 | QStyleOptionFrame styleOption; |
124 | frame.initStyleOption(option: &styleOption); |
125 | |
126 | switch (shape) { |
127 | case QFrame::Box: |
128 | case QFrame::Panel: |
129 | case QFrame::StyledPanel: |
130 | case QFrame::HLine: |
131 | case QFrame::VLine: |
132 | QCOMPARE(styleOption.lineWidth, lineWidth); |
133 | QCOMPARE(styleOption.midLineWidth, midLineWidth); |
134 | break; |
135 | |
136 | case QFrame::NoFrame: |
137 | case QFrame::WinPanel: |
138 | QCOMPARE(styleOption.lineWidth, frame.frameWidth()); |
139 | QCOMPARE(styleOption.midLineWidth, 0); |
140 | break; |
141 | } |
142 | |
143 | QCOMPARE(styleOption.features, QStyleOptionFrame::None); |
144 | QCOMPARE(styleOption.frameShape, shape); |
145 | if (shadow == QFrame::Sunken) |
146 | QVERIFY(styleOption.state & QStyle::State_Sunken); |
147 | else if (shadow == QFrame::Raised) |
148 | QVERIFY(styleOption.state & QStyle::State_Raised); |
149 | } |
150 | |
151 | QT_BEGIN_NAMESPACE |
152 | Q_GUI_EXPORT QPalette qt_fusionPalette(); |
153 | QT_END_NAMESPACE |
154 | |
155 | void tst_QFrame::testPainting_data() |
156 | { |
157 | provideFrameData(); |
158 | } |
159 | |
160 | void tst_QFrame::testPainting() |
161 | { |
162 | if (QGuiApplication::platformName().startsWith(s: QLatin1String("wayland" ), cs: Qt::CaseInsensitive)) |
163 | QSKIP("Wayland: This fails. Figure out why." ); |
164 | |
165 | QFETCH(QString, basename); |
166 | QFETCH(int, lineWidth); |
167 | QFETCH(int, midLineWidth); |
168 | QFETCH(QFrame::Shape, shape); |
169 | QFETCH(QFrame::Shadow, shadow); |
170 | |
171 | QFrame frame; |
172 | frame.setStyle(QStyleFactory::create(QStringLiteral("fusion" ))); |
173 | frame.setPalette(qt_fusionPalette()); |
174 | frame.setFrameStyle(shape | shadow); |
175 | frame.setLineWidth(lineWidth); |
176 | frame.setMidLineWidth(midLineWidth); |
177 | frame.resize(w: 16, h: 16); |
178 | |
179 | const QPixmap pixmap = frame.grab(); |
180 | |
181 | const QString fileName = QLatin1String("images/" ) + basename + QLatin1Char('_') |
182 | + QString::number(lineWidth) + QLatin1Char('_') + QString::number(midLineWidth) |
183 | + QLatin1String(".png" ); |
184 | const QString referenceFilePath = QFINDTESTDATA(fileName); |
185 | const QPixmap referencePixmap(referenceFilePath); |
186 | QVERIFY2(!referencePixmap.isNull(), qPrintable(QStringLiteral("Could not load reference pixmap %1" ).arg(referenceFilePath))); |
187 | QCOMPARE(pixmap, referencePixmap); |
188 | } |
189 | |
190 | QTEST_MAIN(tst_QFrame) |
191 | |
192 | #include "tst_qframe.moc" |
193 | |