|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the test suite of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QtDBus/QDBusServiceWatcher> |
|
43 #include <QtDBus> |
|
44 #include <QtTest> |
|
45 |
|
46 class tst_QDBusServiceWatcher: public QObject |
|
47 { |
|
48 Q_OBJECT |
|
49 QString serviceName; |
|
50 int testCounter; |
|
51 public: |
|
52 tst_QDBusServiceWatcher(); |
|
53 |
|
54 private slots: |
|
55 void initTestCase(); |
|
56 void init(); |
|
57 |
|
58 void watchForCreation(); |
|
59 void watchForDisappearance(); |
|
60 void watchForOwnerChange(); |
|
61 void modeChange(); |
|
62 }; |
|
63 |
|
64 tst_QDBusServiceWatcher::tst_QDBusServiceWatcher() |
|
65 : testCounter(0) |
|
66 { |
|
67 } |
|
68 |
|
69 void tst_QDBusServiceWatcher::initTestCase() |
|
70 { |
|
71 QDBusConnection con = QDBusConnection::sessionBus(); |
|
72 QVERIFY(con.isConnected()); |
|
73 } |
|
74 |
|
75 void tst_QDBusServiceWatcher::init() |
|
76 { |
|
77 // change the service name from test to test |
|
78 serviceName = "com.example.TestService" + QString::number(testCounter++); |
|
79 } |
|
80 |
|
81 void tst_QDBusServiceWatcher::watchForCreation() |
|
82 { |
|
83 QDBusConnection con = QDBusConnection::sessionBus(); |
|
84 QVERIFY(con.isConnected()); |
|
85 |
|
86 QDBusServiceWatcher watcher(serviceName, con, QDBusServiceWatcher::WatchForRegistration); |
|
87 |
|
88 QSignalSpy spyR(&watcher, SIGNAL(serviceRegistered(QString))); |
|
89 QSignalSpy spyU(&watcher, SIGNAL(serviceUnregistered(QString))); |
|
90 QSignalSpy spyO(&watcher, SIGNAL(serviceOwnerChanged(QString,QString,QString))); |
|
91 QTestEventLoop::instance().connect(&watcher, SIGNAL(serviceRegistered(QString)), SLOT(exitLoop())); |
|
92 |
|
93 // register a name |
|
94 QVERIFY(con.registerService(serviceName)); |
|
95 |
|
96 QTestEventLoop::instance().enterLoop(1); |
|
97 QVERIFY(!QTestEventLoop::instance().timeout()); |
|
98 |
|
99 QCOMPARE(spyR.count(), 1); |
|
100 QCOMPARE(spyR.at(0).at(0).toString(), serviceName); |
|
101 |
|
102 QCOMPARE(spyU.count(), 0); |
|
103 |
|
104 QCOMPARE(spyO.count(), 1); |
|
105 QCOMPARE(spyO.at(0).at(0).toString(), serviceName); |
|
106 QVERIFY(spyO.at(0).at(1).toString().isEmpty()); |
|
107 QCOMPARE(spyO.at(0).at(2).toString(), con.baseService()); |
|
108 |
|
109 spyR.clear(); |
|
110 spyU.clear(); |
|
111 spyO.clear(); |
|
112 |
|
113 // unregister it: |
|
114 con.unregisterService(serviceName); |
|
115 |
|
116 // and register again |
|
117 QVERIFY(con.registerService(serviceName)); |
|
118 |
|
119 QTestEventLoop::instance().enterLoop(1); |
|
120 QVERIFY(!QTestEventLoop::instance().timeout()); |
|
121 |
|
122 QCOMPARE(spyR.count(), 1); |
|
123 QCOMPARE(spyR.at(0).at(0).toString(), serviceName); |
|
124 |
|
125 QCOMPARE(spyU.count(), 0); |
|
126 |
|
127 QCOMPARE(spyO.count(), 1); |
|
128 QCOMPARE(spyO.at(0).at(0).toString(), serviceName); |
|
129 QVERIFY(spyO.at(0).at(1).toString().isEmpty()); |
|
130 QCOMPARE(spyO.at(0).at(2).toString(), con.baseService()); |
|
131 } |
|
132 |
|
133 void tst_QDBusServiceWatcher::watchForDisappearance() |
|
134 { |
|
135 QDBusConnection con = QDBusConnection::sessionBus(); |
|
136 QVERIFY(con.isConnected()); |
|
137 |
|
138 QDBusServiceWatcher watcher(serviceName, con, QDBusServiceWatcher::WatchForUnregistration); |
|
139 watcher.setObjectName("watcher for disappearance"); |
|
140 |
|
141 QSignalSpy spyR(&watcher, SIGNAL(serviceRegistered(QString))); |
|
142 QSignalSpy spyU(&watcher, SIGNAL(serviceUnregistered(QString))); |
|
143 QSignalSpy spyO(&watcher, SIGNAL(serviceOwnerChanged(QString,QString,QString))); |
|
144 QTestEventLoop::instance().connect(&watcher, SIGNAL(serviceUnregistered(QString)), SLOT(exitLoop())); |
|
145 |
|
146 // register a name |
|
147 QVERIFY(con.registerService(serviceName)); |
|
148 |
|
149 // unregister it: |
|
150 con.unregisterService(serviceName); |
|
151 |
|
152 QTestEventLoop::instance().enterLoop(1); |
|
153 QVERIFY(!QTestEventLoop::instance().timeout()); |
|
154 |
|
155 QCOMPARE(spyR.count(), 0); |
|
156 |
|
157 QCOMPARE(spyU.count(), 1); |
|
158 QCOMPARE(spyU.at(0).at(0).toString(), serviceName); |
|
159 |
|
160 QCOMPARE(spyO.count(), 1); |
|
161 QCOMPARE(spyO.at(0).at(0).toString(), serviceName); |
|
162 QCOMPARE(spyO.at(0).at(1).toString(), con.baseService()); |
|
163 QVERIFY(spyO.at(0).at(2).toString().isEmpty()); |
|
164 } |
|
165 |
|
166 void tst_QDBusServiceWatcher::watchForOwnerChange() |
|
167 { |
|
168 QDBusConnection con = QDBusConnection::sessionBus(); |
|
169 QVERIFY(con.isConnected()); |
|
170 |
|
171 QDBusServiceWatcher watcher(serviceName, con, QDBusServiceWatcher::WatchForOwnerChange); |
|
172 |
|
173 QSignalSpy spyR(&watcher, SIGNAL(serviceRegistered(QString))); |
|
174 QSignalSpy spyU(&watcher, SIGNAL(serviceUnregistered(QString))); |
|
175 QSignalSpy spyO(&watcher, SIGNAL(serviceOwnerChanged(QString,QString,QString))); |
|
176 QTestEventLoop::instance().connect(&watcher, SIGNAL(serviceRegistered(QString)), SLOT(exitLoop())); |
|
177 |
|
178 // register a name |
|
179 QVERIFY(con.registerService(serviceName)); |
|
180 |
|
181 QTestEventLoop::instance().enterLoop(1); |
|
182 QVERIFY(!QTestEventLoop::instance().timeout()); |
|
183 |
|
184 QCOMPARE(spyR.count(), 1); |
|
185 QCOMPARE(spyR.at(0).at(0).toString(), serviceName); |
|
186 |
|
187 QCOMPARE(spyU.count(), 0); |
|
188 |
|
189 QCOMPARE(spyO.count(), 1); |
|
190 QCOMPARE(spyO.at(0).at(0).toString(), serviceName); |
|
191 QVERIFY(spyO.at(0).at(1).toString().isEmpty()); |
|
192 QCOMPARE(spyO.at(0).at(2).toString(), con.baseService()); |
|
193 |
|
194 spyR.clear(); |
|
195 spyU.clear(); |
|
196 spyO.clear(); |
|
197 |
|
198 // unregister it: |
|
199 con.unregisterService(serviceName); |
|
200 |
|
201 // and register again |
|
202 QVERIFY(con.registerService(serviceName)); |
|
203 |
|
204 QTestEventLoop::instance().enterLoop(1); |
|
205 QVERIFY(!QTestEventLoop::instance().timeout()); |
|
206 |
|
207 QCOMPARE(spyR.count(), 1); |
|
208 QCOMPARE(spyR.at(0).at(0).toString(), serviceName); |
|
209 |
|
210 QCOMPARE(spyU.count(), 1); |
|
211 QCOMPARE(spyU.at(0).at(0).toString(), serviceName); |
|
212 |
|
213 QCOMPARE(spyO.count(), 2); |
|
214 QCOMPARE(spyO.at(0).at(0).toString(), serviceName); |
|
215 QCOMPARE(spyO.at(0).at(1).toString(), con.baseService()); |
|
216 QVERIFY(spyO.at(0).at(2).toString().isEmpty()); |
|
217 QCOMPARE(spyO.at(1).at(0).toString(), serviceName); |
|
218 QVERIFY(spyO.at(1).at(1).toString().isEmpty()); |
|
219 QCOMPARE(spyO.at(1).at(2).toString(), con.baseService()); |
|
220 } |
|
221 |
|
222 void tst_QDBusServiceWatcher::modeChange() |
|
223 { |
|
224 QDBusConnection con = QDBusConnection::sessionBus(); |
|
225 QVERIFY(con.isConnected()); |
|
226 |
|
227 QDBusServiceWatcher watcher(serviceName, con, QDBusServiceWatcher::WatchForRegistration); |
|
228 |
|
229 QSignalSpy spyR(&watcher, SIGNAL(serviceRegistered(QString))); |
|
230 QSignalSpy spyU(&watcher, SIGNAL(serviceUnregistered(QString))); |
|
231 QSignalSpy spyO(&watcher, SIGNAL(serviceOwnerChanged(QString,QString,QString))); |
|
232 QTestEventLoop::instance().connect(&watcher, SIGNAL(serviceRegistered(QString)), SLOT(exitLoop())); |
|
233 |
|
234 // register a name |
|
235 QVERIFY(con.registerService(serviceName)); |
|
236 |
|
237 QTestEventLoop::instance().enterLoop(1); |
|
238 QVERIFY(!QTestEventLoop::instance().timeout()); |
|
239 |
|
240 QCOMPARE(spyR.count(), 1); |
|
241 QCOMPARE(spyR.at(0).at(0).toString(), serviceName); |
|
242 |
|
243 QCOMPARE(spyU.count(), 0); |
|
244 |
|
245 QCOMPARE(spyO.count(), 1); |
|
246 QCOMPARE(spyO.at(0).at(0).toString(), serviceName); |
|
247 QVERIFY(spyO.at(0).at(1).toString().isEmpty()); |
|
248 QCOMPARE(spyO.at(0).at(2).toString(), con.baseService()); |
|
249 |
|
250 spyR.clear(); |
|
251 spyU.clear(); |
|
252 spyO.clear(); |
|
253 |
|
254 watcher.setWatchMode(QDBusServiceWatcher::WatchForUnregistration); |
|
255 |
|
256 // unregister it: |
|
257 con.unregisterService(serviceName); |
|
258 |
|
259 QTestEventLoop::instance().connect(&watcher, SIGNAL(serviceUnregistered(QString)), SLOT(exitLoop())); |
|
260 QTestEventLoop::instance().enterLoop(1); |
|
261 QVERIFY(!QTestEventLoop::instance().timeout()); |
|
262 |
|
263 QCOMPARE(spyR.count(), 0); |
|
264 |
|
265 QCOMPARE(spyU.count(), 1); |
|
266 QCOMPARE(spyU.at(0).at(0).toString(), serviceName); |
|
267 |
|
268 QCOMPARE(spyO.count(), 1); |
|
269 QCOMPARE(spyO.at(0).at(0).toString(), serviceName); |
|
270 QCOMPARE(spyO.at(0).at(1).toString(), con.baseService()); |
|
271 QVERIFY(spyO.at(0).at(2).toString().isEmpty()); |
|
272 } |
|
273 |
|
274 QTEST_MAIN(tst_QDBusServiceWatcher) |
|
275 #include "tst_qdbusservicewatcher.moc" |