1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2015 Olivier Goffart <ogoffart@woboq.com> |
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 <QtCore/qobject.h> |
33 | #include <QtCore/qmetaobject.h> |
34 | #include "wobjectimpl.h" |
35 | |
36 | class tst_QMetaEnum : public QObject |
37 | { |
38 | W_OBJECT(tst_QMetaEnum) |
39 | public: |
40 | enum SuperEnum { SuperValue1 = 1 , SuperValue2 = 2 }; |
41 | enum Flag { Flag1 = 1 , Flag2 = 2 }; |
42 | W_DECLARE_FLAGS(Flags, Flag) |
43 | W_ENUM(SuperEnum, SuperValue1, SuperValue2) |
44 | W_FLAG(Flags, Flag1, Flag2) |
45 | |
46 | private: |
47 | void fromType(); W_SLOT(fromType) |
48 | void valuesToKeys_data(); W_SLOT(valuesToKeys_data) |
49 | void valuesToKeys(); W_SLOT(valuesToKeys) |
50 | void defaultConstructed(); W_SLOT(defaultConstructed) |
51 | }; |
52 | |
53 | W_OBJECT_IMPL(tst_QMetaEnum) |
54 | |
55 | void tst_QMetaEnum::fromType() |
56 | { |
57 | QMetaEnum meta = QMetaEnum::fromType<SuperEnum>(); |
58 | QVERIFY(meta.isValid()); |
59 | QVERIFY(!meta.isFlag()); |
60 | QCOMPARE(meta.name(), "SuperEnum" ); |
61 | #if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0) |
62 | QCOMPARE(meta.enumName(), "SuperEnum" ); |
63 | #endif |
64 | QCOMPARE(meta.enclosingMetaObject(), &staticMetaObject); |
65 | QCOMPARE(meta.keyCount(), 2); |
66 | |
67 | meta = QMetaEnum::fromType<Flags>(); |
68 | QVERIFY(meta.isValid()); |
69 | QVERIFY(meta.isFlag()); |
70 | QCOMPARE(meta.name(), "Flags" ); |
71 | #if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0) |
72 | QCOMPARE(meta.enumName(), "Flag" ); |
73 | #endif |
74 | QCOMPARE(meta.enclosingMetaObject(), &staticMetaObject); |
75 | QCOMPARE(meta.keyCount(), 2); |
76 | } |
77 | |
78 | Q_DECLARE_METATYPE(Qt::WindowFlags) |
79 | |
80 | void tst_QMetaEnum::valuesToKeys_data() |
81 | { |
82 | QTest::addColumn<Qt::WindowFlags>("windowFlags" ); |
83 | QTest::addColumn<QByteArray>("expected" ); |
84 | |
85 | QTest::newRow("Window" ) |
86 | << Qt::WindowFlags(Qt::Window) |
87 | << QByteArrayLiteral("Window" ); |
88 | |
89 | // Verify that Qt::Dialog does not cause 'Window' to appear in the output. |
90 | QTest::newRow("Frameless_Dialog" ) |
91 | << (Qt::Dialog | Qt::FramelessWindowHint) |
92 | << QByteArrayLiteral("Dialog|FramelessWindowHint" ); |
93 | |
94 | // Similarly, Qt::WindowMinMaxButtonsHint should not show up as |
95 | // WindowMinimizeButtonHint|WindowMaximizeButtonHint |
96 | QTest::newRow("Tool_MinMax_StaysOnTop" ) |
97 | << (Qt::Tool | Qt::WindowMinMaxButtonsHint | Qt::WindowStaysOnTopHint) |
98 | << QByteArrayLiteral("Tool|WindowMinMaxButtonsHint|WindowStaysOnTopHint" ); |
99 | } |
100 | |
101 | void tst_QMetaEnum::valuesToKeys() |
102 | { |
103 | QFETCH(Qt::WindowFlags, windowFlags); |
104 | QFETCH(QByteArray, expected); |
105 | |
106 | QMetaEnum me = QMetaEnum::fromType<Qt::WindowFlags>(); |
107 | QCOMPARE(me.valueToKeys(windowFlags), expected); |
108 | } |
109 | |
110 | void tst_QMetaEnum::defaultConstructed() |
111 | { |
112 | QMetaEnum e; |
113 | QVERIFY(!e.isValid()); |
114 | #if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0) |
115 | QVERIFY(!e.isScoped()); |
116 | #endif |
117 | QVERIFY(!e.isFlag()); |
118 | QCOMPARE(QByteArray(e.name()), QByteArray()); |
119 | } |
120 | |
121 | Q_STATIC_ASSERT(QtPrivate::IsQEnumHelper<tst_QMetaEnum::SuperEnum>::Value); |
122 | Q_STATIC_ASSERT(QtPrivate::IsQEnumHelper<Qt::WindowFlags>::Value); |
123 | Q_STATIC_ASSERT(QtPrivate::IsQEnumHelper<Qt::Orientation>::Value); |
124 | Q_STATIC_ASSERT(!QtPrivate::IsQEnumHelper<int>::Value); |
125 | Q_STATIC_ASSERT(!QtPrivate::IsQEnumHelper<QObject>::Value); |
126 | Q_STATIC_ASSERT(!QtPrivate::IsQEnumHelper<QObject*>::Value); |
127 | Q_STATIC_ASSERT(!QtPrivate::IsQEnumHelper<void>::Value); |
128 | |
129 | |
130 | QTEST_MAIN(tst_QMetaEnum) |
131 | |