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
35class tst_QMetaEnum : public QObject
36{
37 Q_OBJECT
38public:
39 enum SuperEnum { SuperValue1 = 1 , SuperValue2 = 2 };
40 enum Flag { Flag1 = 1 , Flag2 = 2 };
41 Q_DECLARE_FLAGS(Flags, Flag)
42 Q_ENUM(SuperEnum)
43 Q_FLAG(Flags)
44
45private slots:
46 void fromType();
47 void valuesToKeys_data();
48 void valuesToKeys();
49 void defaultConstructed();
50};
51
52void tst_QMetaEnum::fromType()
53{
54 QMetaEnum meta = QMetaEnum::fromType<SuperEnum>();
55 QVERIFY(meta.isValid());
56 QVERIFY(!meta.isFlag());
57 QCOMPARE(meta.name(), "SuperEnum");
58 QCOMPARE(meta.enumName(), "SuperEnum");
59 QCOMPARE(meta.enclosingMetaObject(), &staticMetaObject);
60 QCOMPARE(meta.keyCount(), 2);
61
62 meta = QMetaEnum::fromType<Flags>();
63 QVERIFY(meta.isValid());
64 QVERIFY(meta.isFlag());
65 QCOMPARE(meta.name(), "Flags");
66 QCOMPARE(meta.enumName(), "Flag");
67 QCOMPARE(meta.enclosingMetaObject(), &staticMetaObject);
68 QCOMPARE(meta.keyCount(), 2);
69}
70
71Q_DECLARE_METATYPE(Qt::WindowFlags)
72
73void tst_QMetaEnum::valuesToKeys_data()
74{
75 QTest::addColumn<Qt::WindowFlags>(name: "windowFlags");
76 QTest::addColumn<QByteArray>(name: "expected");
77
78 QTest::newRow(dataTag: "Window")
79 << Qt::WindowFlags(Qt::Window)
80 << QByteArrayLiteral("Window");
81
82 // Verify that Qt::Dialog does not cause 'Window' to appear in the output.
83 QTest::newRow(dataTag: "Frameless_Dialog")
84 << (Qt::Dialog | Qt::FramelessWindowHint)
85 << QByteArrayLiteral("Dialog|FramelessWindowHint");
86
87 // Similarly, Qt::WindowMinMaxButtonsHint should not show up as
88 // WindowMinimizeButtonHint|WindowMaximizeButtonHint
89 QTest::newRow(dataTag: "Tool_MinMax_StaysOnTop")
90 << (Qt::Tool | Qt::WindowMinMaxButtonsHint | Qt::WindowStaysOnTopHint)
91 << QByteArrayLiteral("Tool|WindowMinMaxButtonsHint|WindowStaysOnTopHint");
92}
93
94void tst_QMetaEnum::valuesToKeys()
95{
96 QFETCH(Qt::WindowFlags, windowFlags);
97 QFETCH(QByteArray, expected);
98
99 QMetaEnum me = QMetaEnum::fromType<Qt::WindowFlags>();
100 QCOMPARE(me.valueToKeys(windowFlags), expected);
101}
102
103void tst_QMetaEnum::defaultConstructed()
104{
105 QMetaEnum e;
106 QVERIFY(!e.isValid());
107 QVERIFY(!e.isScoped());
108 QVERIFY(!e.isFlag());
109 QCOMPARE(e.name(), QByteArray());
110}
111
112Q_STATIC_ASSERT(QtPrivate::IsQEnumHelper<tst_QMetaEnum::SuperEnum>::Value);
113Q_STATIC_ASSERT(QtPrivate::IsQEnumHelper<Qt::WindowFlags>::Value);
114Q_STATIC_ASSERT(QtPrivate::IsQEnumHelper<Qt::Orientation>::Value);
115Q_STATIC_ASSERT(!QtPrivate::IsQEnumHelper<int>::Value);
116Q_STATIC_ASSERT(!QtPrivate::IsQEnumHelper<QObject>::Value);
117Q_STATIC_ASSERT(!QtPrivate::IsQEnumHelper<QObject*>::Value);
118Q_STATIC_ASSERT(!QtPrivate::IsQEnumHelper<void>::Value);
119
120QTEST_MAIN(tst_QMetaEnum)
121#include "tst_qmetaenum.moc"
122

source code of qtbase/tests/auto/corelib/kernel/qmetaenum/tst_qmetaenum.cpp