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 <QtCore/qcoreapplication.h> |
33 | #include <QtCore/qcoreevent.h> |
34 | |
35 | class tst_QEvent : public QObject |
36 | { |
37 | Q_OBJECT |
38 | public: |
39 | tst_QEvent(); |
40 | ~tst_QEvent(); |
41 | |
42 | private slots: |
43 | void registerEventType_data(); |
44 | void registerEventType(); |
45 | void exhaustEventTypeRegistration(); // keep behind registerEventType() test |
46 | |
47 | private: |
48 | bool registerEventTypeSucceeded; // track success of registerEventType for use by exhaustEventTypeRegistration() |
49 | }; |
50 | |
51 | tst_QEvent::tst_QEvent() |
52 | : registerEventTypeSucceeded(true) |
53 | { } |
54 | |
55 | tst_QEvent::~tst_QEvent() |
56 | { } |
57 | |
58 | void tst_QEvent::registerEventType_data() |
59 | { |
60 | QTest::addColumn<int>(name: "hint" ); |
61 | QTest::addColumn<int>(name: "expected" ); |
62 | |
63 | // default argument |
64 | QTest::newRow(dataTag: "default" ) << -1 << int(QEvent::MaxUser); |
65 | // hint not valid |
66 | QTest::newRow(dataTag: "User-1" ) << int(QEvent::User - 1) << int(QEvent::MaxUser - 1); |
67 | // hint not valid II |
68 | QTest::newRow(dataTag: "MaxUser+1" ) << int(QEvent::MaxUser + 1) << int(QEvent::MaxUser - 2); |
69 | // hint valid, but already taken |
70 | QTest::newRow(dataTag: "MaxUser-1" ) << int(QEvent::MaxUser - 1) << int(QEvent::MaxUser - 3); |
71 | // hint valid, but not taken |
72 | QTest::newRow(dataTag: "User + 1000" ) << int(QEvent::User + 1000) << int(QEvent::User + 1000); |
73 | } |
74 | |
75 | void tst_QEvent::registerEventType() |
76 | { |
77 | const bool oldRegisterEventTypeSucceeded = registerEventTypeSucceeded; |
78 | registerEventTypeSucceeded = false; |
79 | QFETCH(int, hint); |
80 | QFETCH(int, expected); |
81 | QCOMPARE(QEvent::registerEventType(hint), expected); |
82 | registerEventTypeSucceeded = oldRegisterEventTypeSucceeded; |
83 | } |
84 | |
85 | void tst_QEvent::exhaustEventTypeRegistration() |
86 | { |
87 | if (!registerEventTypeSucceeded) |
88 | QSKIP("requires the previous test (registerEventType) to have finished successfully" ); |
89 | |
90 | int i = QEvent::User; |
91 | int result; |
92 | while ((result = QEvent::registerEventType(hint: i)) == i) |
93 | ++i; |
94 | QCOMPARE(i, int(QEvent::User + 1000)); |
95 | QCOMPARE(result, int(QEvent::MaxUser - 4)); |
96 | i = QEvent::User + 1001; |
97 | while ((result = QEvent::registerEventType(hint: i)) == i) |
98 | ++i; |
99 | QCOMPARE(result, -1); |
100 | QCOMPARE(i, int(QEvent::MaxUser - 4)); |
101 | } |
102 | |
103 | QTEST_MAIN(tst_QEvent) |
104 | #include "tst_qevent.moc" |
105 | |