1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Volker Krause <volker.krause@kdab.com> |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtCore module 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 <QtCore/private/qhooks_p.h> |
32 | |
33 | class tst_QHooks: public QObject |
34 | { |
35 | Q_OBJECT |
36 | |
37 | private slots: |
38 | void cleanup(); |
39 | void testVersion(); |
40 | void testAddRemoveObject(); |
41 | void testChaining(); |
42 | }; |
43 | |
44 | void tst_QHooks::cleanup() |
45 | { |
46 | qtHookData[QHooks::AddQObject] = 0; |
47 | qtHookData[QHooks::RemoveQObject] = 0; |
48 | } |
49 | |
50 | void tst_QHooks::testVersion() |
51 | { |
52 | QVERIFY(qtHookData[QHooks::HookDataVersion] >= 3); |
53 | QCOMPARE(qtHookData[QHooks::HookDataSize], (quintptr)QHooks::LastHookIndex); |
54 | QCOMPARE(qtHookData[QHooks::QtVersion], (quintptr)QT_VERSION); |
55 | } |
56 | |
57 | static int objectCount = 0; |
58 | |
59 | static void objectAddHook(QObject*) |
60 | { |
61 | ++objectCount; |
62 | } |
63 | |
64 | static void objectRemoveHook(QObject*) |
65 | { |
66 | --objectCount; |
67 | } |
68 | |
69 | void tst_QHooks::testAddRemoveObject() |
70 | { |
71 | QCOMPARE(qtHookData[QHooks::AddQObject], (quintptr)0); |
72 | QCOMPARE(qtHookData[QHooks::RemoveQObject], (quintptr)0); |
73 | |
74 | qtHookData[QHooks::AddQObject] = (quintptr)&objectAddHook; |
75 | qtHookData[QHooks::RemoveQObject] = (quintptr)&objectRemoveHook; |
76 | |
77 | QCOMPARE(objectCount, 0); |
78 | QObject *obj = new QObject; |
79 | QVERIFY(objectCount > 0); |
80 | delete obj; |
81 | QCOMPARE(objectCount, 0); |
82 | } |
83 | |
84 | static QVector<QString> hookOrder; |
85 | |
86 | static QHooks::AddQObjectCallback existingAddHook = 0; |
87 | static QHooks::RemoveQObjectCallback existingRemoveHook = 0; |
88 | |
89 | static void firstAddHook(QObject *) |
90 | { |
91 | hookOrder.append(t: QLatin1String("firstAddHook" )); |
92 | } |
93 | |
94 | static void firstRemoveHook(QObject *) |
95 | { |
96 | hookOrder.append(t: QLatin1String("firstRemoveHook" )); |
97 | } |
98 | |
99 | static void secondAddHook(QObject *object) |
100 | { |
101 | if (existingAddHook) |
102 | existingAddHook(object); |
103 | |
104 | hookOrder.append(t: QLatin1String("secondAddHook" )); |
105 | } |
106 | |
107 | static void secondRemoveHook(QObject *object) |
108 | { |
109 | if (existingRemoveHook) |
110 | existingRemoveHook(object); |
111 | |
112 | hookOrder.append(t: QLatin1String("secondRemoveHook" )); |
113 | } |
114 | |
115 | // Tests that it's possible to "chain" hooks together (i.e. have multiple hooks) |
116 | void tst_QHooks::testChaining() |
117 | { |
118 | QCOMPARE(qtHookData[QHooks::AddQObject], (quintptr)0); |
119 | QCOMPARE(qtHookData[QHooks::RemoveQObject], (quintptr)0); |
120 | |
121 | // Set the add and remove hooks (could just skip this and go straight to the next step, |
122 | // but it's for illustrative purposes). |
123 | qtHookData[QHooks::AddQObject] = (quintptr)&firstAddHook; |
124 | qtHookData[QHooks::RemoveQObject] = (quintptr)&firstRemoveHook; |
125 | |
126 | // Store them so that we can call them later. |
127 | existingAddHook = reinterpret_cast<QHooks::AddQObjectCallback>(qtHookData[QHooks::AddQObject]); |
128 | existingRemoveHook = reinterpret_cast<QHooks::RemoveQObjectCallback>(qtHookData[QHooks::RemoveQObject]); |
129 | |
130 | // Overide them with hooks that call them first. |
131 | qtHookData[QHooks::AddQObject] = (quintptr)&secondAddHook; |
132 | qtHookData[QHooks::RemoveQObject] = (quintptr)&secondRemoveHook; |
133 | |
134 | QObject *obj = new QObject; |
135 | QCOMPARE(hookOrder.size(), 2); |
136 | QCOMPARE(hookOrder.at(0), QLatin1String("firstAddHook" )); |
137 | QCOMPARE(hookOrder.at(1), QLatin1String("secondAddHook" )); |
138 | delete obj; |
139 | QCOMPARE(hookOrder.size(), 4); |
140 | QCOMPARE(hookOrder.at(2), QLatin1String("firstRemoveHook" )); |
141 | QCOMPARE(hookOrder.at(3), QLatin1String("secondRemoveHook" )); |
142 | |
143 | hookOrder.clear(); |
144 | } |
145 | |
146 | QTEST_APPLESS_MAIN(tst_QHooks) |
147 | #include "tst_qhooks.moc" |
148 | |