1/****************************************************************************
2**
3** Copyright (C) 2013 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@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#include <QtTest/QtTest>
30
31#include "qobject.h"
32
33class tst_QSignalBlocker : public QObject
34{
35 Q_OBJECT
36private slots:
37 void signalBlocking();
38 void moveAssignment();
39};
40
41void tst_QSignalBlocker::signalBlocking()
42{
43 QObject o;
44
45 QVERIFY(!o.signalsBlocked());
46
47 {
48 QSignalBlocker blocker(&o);
49 QVERIFY(o.signalsBlocked());
50
51 o.blockSignals(b: false);
52 QVERIFY(!o.signalsBlocked());
53
54 o.blockSignals(b: true);
55 QVERIFY(o.signalsBlocked());
56
57 blocker.unblock();
58 QVERIFY(!o.signalsBlocked());
59
60 blocker.reblock();
61 QVERIFY(o.signalsBlocked());
62 }
63
64 QVERIFY(!o.signalsBlocked());
65}
66
67void tst_QSignalBlocker::moveAssignment()
68{
69 QObject o1, o2;
70
71 // move-assignment: both block other objects
72 {
73 QSignalBlocker b(&o1);
74 QVERIFY(o1.signalsBlocked());
75
76 QVERIFY(!o2.signalsBlocked());
77 b = QSignalBlocker(&o2);
78 QVERIFY(!o1.signalsBlocked());
79 QVERIFY(o2.signalsBlocked());
80 }
81
82 QVERIFY(!o1.signalsBlocked());
83 QVERIFY(!o2.signalsBlocked());
84
85 // move-assignment: from inert other
86 {
87 QSignalBlocker b(&o1);
88 QVERIFY(o1.signalsBlocked());
89 b = QSignalBlocker(0);
90 }
91
92 QVERIFY(!o1.signalsBlocked());
93 QVERIFY(!o2.signalsBlocked());
94
95 // move-assignment: to inert *this
96 {
97 QSignalBlocker b(0);
98 QVERIFY(!o1.signalsBlocked());
99 {
100 QSignalBlocker inner(&o1);
101 QVERIFY(o1.signalsBlocked());
102 b = std::move(inner);
103 }
104 QVERIFY(o1.signalsBlocked());
105 }
106
107 QVERIFY(!o1.signalsBlocked());
108 QVERIFY(!o2.signalsBlocked());
109
110 // move-assignment: both block the same object, neither is unblocked
111 {
112 QSignalBlocker b(&o1);
113 QVERIFY(o1.signalsBlocked());
114 {
115 b.unblock(); // make sure inner.m_blocked = false
116 QVERIFY(!o1.signalsBlocked());
117 QSignalBlocker inner(&o1);
118 QVERIFY(o1.signalsBlocked());
119 b.reblock();
120 QVERIFY(o1.signalsBlocked());
121 b = std::move(inner);
122 }
123 QVERIFY(o1.signalsBlocked());
124 }
125
126 QVERIFY(!o1.signalsBlocked());
127 QVERIFY(!o2.signalsBlocked());
128
129 // move-assignment: both block the same object, but *this is unblocked
130 {
131 QSignalBlocker b(&o1);
132 QVERIFY(o1.signalsBlocked());
133 b.unblock();
134 QVERIFY(!o1.signalsBlocked());
135 b = QSignalBlocker(&o1);
136 QVERIFY(o1.signalsBlocked());
137 }
138
139 QVERIFY(!o1.signalsBlocked());
140 QVERIFY(!o2.signalsBlocked());
141
142 // move-assignment: both block the same object, but other is unblocked
143 {
144 QSignalBlocker b(&o1);
145 {
146 QVERIFY(o1.signalsBlocked());
147 QSignalBlocker inner(&o1);
148 QVERIFY(o1.signalsBlocked());
149 inner.unblock();
150 QVERIFY(o1.signalsBlocked());
151 b = std::move(inner);
152 QVERIFY(!o1.signalsBlocked());
153 }
154 QVERIFY(!o1.signalsBlocked());
155 }
156
157 QVERIFY(!o1.signalsBlocked());
158 QVERIFY(!o2.signalsBlocked());
159}
160
161QTEST_MAIN(tst_QSignalBlocker)
162#include "tst_qsignalblocker.moc"
163

source code of qtbase/tests/auto/corelib/kernel/qsignalblocker/tst_qsignalblocker.cpp