Debian Codegraph @

tests/auto/corelib/kernel/qwineventnotifier/tst_qwineventnotifier.cpp

raw
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 #include <QtTest/QtTest>
30 #include <qwineventnotifier.h>
31 #include <qtimer.h>
32 #include <qvarlengtharray.h>
33 #include <qvector.h>
34 #include <qt_windows.h>
35
36 #include <algorithm>
37 #include <memory>
38
39 class tst_QWinEventNotifier : public QObject
40 {
41 Q_OBJECT
42
43 protected slots:
44 void simple_activated();
45 void simple_timerSet();
46 private slots:
47 void simple_data();
48 void simple();
49 void blockedWaiting();
50 void manyNotifiers();
51 void disableNotifiersInActivatedSlot_data();
52 void disableNotifiersInActivatedSlot();
53
54 private:
55 HANDLE simpleHEvent;
56 bool simpleActivated;
57 };
58
59 void tst_QWinEventNotifier::simple_activated()
60 {
61 simpleActivated = true;
62 ResetEvent((HANDLE)simpleHEvent);
63 QTestEventLoop::instance().exitLoop();
64 }
65
66 void tst_QWinEventNotifier::simple_timerSet()
67 {
68 SetEvent((HANDLE)simpleHEvent);
69 }
70
71 void tst_QWinEventNotifier::simple_data()
72 {
73 QTest::addColumn<bool>("resetManually");
74 QTest::newRow("manual_reset") << true;
75 QTest::newRow("auto_reset") << false;
76 }
77
78 void tst_QWinEventNotifier::simple()
79 {
80 QFETCH(bool, resetManually);
81 simpleHEvent = CreateEvent(0, resetManually, false, 0);
82 QVERIFY(simpleHEvent);
83
84 QWinEventNotifier n(simpleHEvent);
85 QObject::connect(&n, SIGNAL(activated(HANDLE)), this, SLOT(simple_activated()));
86 simpleActivated = false;
87
88 SetEvent((HANDLE)simpleHEvent);
89
90 QTestEventLoop::instance().enterLoop(30);
91 if (QTestEventLoop::instance().timeout())
92 QFAIL("Timed out");
93
94 QVERIFY(simpleActivated);
95
96
97 simpleActivated = false;
98
99 QTimer::singleShot(3000, this, SLOT(simple_timerSet()));
100
101 QTestEventLoop::instance().enterLoop(30);
102 if (QTestEventLoop::instance().timeout())
103 QFAIL("Timed out");
104
105 QVERIFY(simpleActivated);
106 }
107
108 void tst_QWinEventNotifier::blockedWaiting()
109 {
110 simpleHEvent = CreateEvent(0, true, false, 0);
111 QWinEventNotifier n(simpleHEvent);
112 QObject::connect(&n, &QWinEventNotifier::activated,
113 this, &tst_QWinEventNotifier::simple_activated);
114 simpleActivated = false;
115
116 SetEvent(simpleHEvent);
117 QCOMPARE(WaitForSingleObject(simpleHEvent, 1000), WAIT_OBJECT_0);
118
119 n.setEnabled(false);
120 ResetEvent(simpleHEvent);
121 n.setEnabled(true);
122
123 QTestEventLoop::instance().enterLoop(1);
124 QVERIFY(QTestEventLoop::instance().timeout());
125 QVERIFY(!simpleActivated);
126 }
127
128 class EventWithNotifier : public QObject
129 {
130 Q_OBJECT
131 public:
132 EventWithNotifier()
133 {
134 connect(&notifier, &QWinEventNotifier::activated,
135 this, &EventWithNotifier::onNotifierActivated);
136 notifier.setHandle(CreateEvent(0, TRUE, FALSE, 0));
137 notifier.setEnabled(true);
138 }
139
140 ~EventWithNotifier()
141 {
142 notifier.setEnabled(false);
143 CloseHandle(notifier.handle());
144 }
145
146 HANDLE eventHandle() const { return notifier.handle(); }
147 int numberOfTimesActivated() const { return activatedCount; }
148 void setEnabled(bool b) { notifier.setEnabled(b); }
149 bool isEnabled() const { return notifier.isEnabled(); }
150
151 signals:
152 void activated();
153
154 public slots:
155 void onNotifierActivated()
156 {
157 ResetEvent(notifier.handle());
158 activatedCount++;
159 emit activated();
160 }
161
162 private:
163 QWinEventNotifier notifier;
164 int activatedCount = 0;
165 };
166
167 void tst_QWinEventNotifier::manyNotifiers()
168 {
169 const size_t maxEvents = 100;
170 const size_t middleEvenEvent = maxEvents / 2;
171 Q_ASSERT(middleEvenEvent % 2 == 0);
172 using EventWithNotifierPtr = std::unique_ptr<EventWithNotifier>;
173 std::vector<EventWithNotifierPtr> events(maxEvents);
174 std::generate(events.begin(), events.end(), [] () {
175 return EventWithNotifierPtr(new EventWithNotifier);
176 });
177
178 QTestEventLoop loop;
179 auto connection = connect(events.at(8).get(), &EventWithNotifier::activated, &loop, &QTestEventLoop::exitLoop);
180 for (const auto &ewn : events) {
181 connect(ewn.get(), &EventWithNotifier::activated, [&events, &loop] () {
182 if (std::all_of(events.cbegin(), events.cend(),
183 [] (const EventWithNotifierPtr &ewn) {
184 return ewn->numberOfTimesActivated() > 0; })) {
185 loop.exitLoop();
186 }
187 });
188 }
189
190 // Activate all even events before running the event loop.
191 for (size_t i = 0; i < events.size(); i += 2)
192 SetEvent(events.at(i)->eventHandle());
193
194 // Wait until event notifier with index 8 has been activated.
195 loop.enterLoop(30);
196 QObject::disconnect(connection);
197
198 // Activate all odd events after the event loop has run for a bit.
199 for (size_t i = 1; i < events.size(); i += 2)
200 SetEvent(events.at(i)->eventHandle());
201
202 // Wait until all event notifiers have fired.
203 loop.enterLoop(30);
204
205 // All notifiers must have been activated exactly once.
206 QVERIFY(std::all_of(events.cbegin(), events.cend(), [] (const EventWithNotifierPtr &ewn) {
207 return ewn->numberOfTimesActivated() == 1;
208 }));
209 }
210
211 using Indices = QVector<int>;
212
213 void tst_QWinEventNotifier::disableNotifiersInActivatedSlot_data()
214 {
215 QTest::addColumn<int>("count");
216 QTest::addColumn<Indices>("notifiersToSignal");
217 QTest::addColumn<Indices>("notifiersToDisable");
218 QTest::addColumn<bool>("deleteNotifiers");
219 QTest::newRow("disable_signaled") << 3 << Indices{1} << Indices{1} << false;
220 QTest::newRow("disable_signaled2") << 3 << Indices{1, 2} << Indices{1} << false;
221 QTest::newRow("disable_before_signaled") << 3 << Indices{1} << Indices{0, 1} << false;
222 QTest::newRow("disable_after_signaled") << 3 << Indices{1} << Indices{1, 2} << false;
223 QTest::newRow("delete_signaled") << 3 << Indices{1} << Indices{1} << true;
224 QTest::newRow("delete_before_signaled1") << 3 << Indices{1} << Indices{0} << true;
225 QTest::newRow("delete_before_signaled2") << 3 << Indices{1} << Indices{0, 1} << true;
226 QTest::newRow("delete_before_signaled3") << 4 << Indices{3, 1} << Indices{0, 1} << true;
227 QTest::newRow("delete_after_signaled1") << 3 << Indices{1} << Indices{1, 2} << true;
228 QTest::newRow("delete_after_signaled2") << 4 << Indices{1, 3} << Indices{1, 2} << true;
229 QTest::newRow("delete_after_signaled3") << 5 << Indices{1} << Indices{1, 4} << true;
230 }
231
232 void tst_QWinEventNotifier::disableNotifiersInActivatedSlot()
233 {
234 QFETCH(int, count);
235 QFETCH(Indices, notifiersToSignal);
236 QFETCH(Indices, notifiersToDisable);
237 QFETCH(bool, deleteNotifiers);
238
239 QVarLengthArray<std::unique_ptr<EventWithNotifier>, 10> events(count);
240 for (int i = 0; i < count; ++i)
241 events[i].reset(new EventWithNotifier);
242
243 auto isActivatedOrDisabled = [&events](int i) {
244 return !events.at(i) || !events.at(i)->isEnabled()
245 || events.at(i)->numberOfTimesActivated() > 0;
246 };
247
248 for (auto &e : events) {
249 connect(e.get(), &EventWithNotifier::activated, [&]() {
250 for (int i : notifiersToDisable) {
251 if (deleteNotifiers)
252 events[i].reset();
253 else
254 events.at(i)->setEnabled(false);
255 }
256 if (std::all_of(notifiersToSignal.begin(), notifiersToSignal.end(),
257 isActivatedOrDisabled)) {
258 QTimer::singleShot(0, &QTestEventLoop::instance(), SLOT(exitLoop()));
259 }
260 });
261 }
262 for (int i : notifiersToSignal)
263 SetEvent(events.at(i)->eventHandle());
264 QTestEventLoop::instance().enterLoop(30);
265 QVERIFY(!QTestEventLoop::instance().timeout());
266 }
267
268 QTEST_MAIN(tst_QWinEventNotifier)
269
270 #include "tst_qwineventnotifier.moc"