Debian Codegraph @

tests/online-accounts-service/tst_service/tst_service.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2013 Canonical Ltd.
3 *
4 * Contact: Alberto Mardegan <[email protected]>
5 *
6 * This file is part of lomiri-online-accounts-service
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 3, as published
10 * by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranties of
14 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <online-accounts-common/globals.h>
22 #include "request.h"
23 #include "request-manager.h"
24 #include "service.h"
25 #include "ui-proxy.h"
26
27 #include <QDBusArgument>
28 #include <QDBusConnection>
29 #include <QDBusMessage>
30 #include <QDBusPendingCallWatcher>
31 #include <QDBusPendingReply>
32 #include <QDBusServer>
33 #include <QDebug>
34 #include <QSignalSpy>
35 #include <QString>
36 #include <QTest>
37 #include <QTimer>
38
39 using namespace OnlineAccountsUi;
40
41 #define P2P_SOCKET "unix:path=" TMP_DIR "/tst_service_%1"
42 #define TEST_SERVICE_NAME \
43 QStringLiteral("com.lomiri.OnlineAccountsUi.Test")
44 #define TEST_OBJECT_PATH QStringLiteral("/")
45
46 QList<UiProxyPrivate *> m_uiProxies;
47
48 class RequestReply: public QDBusPendingCallWatcher
49 {
50 Q_OBJECT
51
52 public:
53 RequestReply(const QDBusPendingCall &call, QObject *parent = 0):
54 QDBusPendingCallWatcher(call, parent),
55 m_isError(false)
56 {
57 QObject::connect(this, SIGNAL(finished(QDBusPendingCallWatcher*)),
58 this, SLOT(onFinished()));
59 }
60
61 bool isError() const { return m_isError; }
62 QVariantMap reply() const { return m_reply; }
63 QString errorName() const { return m_errorName; }
64 QString errorMessage() const { return m_errorMessage; }
65
66 private Q_SLOTS:
67 void onFinished() {
68 QDBusPendingReply<QVariantMap> reply = *this;
69 if (reply.isError()) {
70 m_isError = true;
71 m_errorName = reply.error().name();
72 m_errorMessage = reply.error().message();
73 } else {
74 m_reply = qdbus_cast<QVariantMap>(reply.argumentAt(0).
75 value<QDBusArgument>());
76 }
77 Q_EMIT finished();
78 }
79
80 Q_SIGNALS:
81 void finished();
82
83 private:
84 bool m_isError;
85 QVariantMap m_reply;
86 QString m_errorName;
87 QString m_errorMessage;
88 };
89
90 class ServiceTest: public QObject
91 {
92 Q_OBJECT
93
94 public:
95 ServiceTest();
96
97 private:
98 RequestReply *sendRequest(const QVariantMap &parameters) {
99 QDBusMessage msg =
100 QDBusMessage::createMethodCall(TEST_SERVICE_NAME,
101 TEST_OBJECT_PATH,
102 "com.lomiri.OnlineAccountsUi",
103 "requestAccess");
104 msg.setArguments(QVariantList() << parameters);
105 QDBusPendingCall call = m_connection.asyncCall(msg);
106 return new RequestReply(call, this);
107 }
108
109 private Q_SLOTS:
110 void initTestCase();
111 void testResults();
112 void testFailure();
113 void testIdle();
114
115 protected Q_SLOTS:
116 void onNewConnection(const QDBusConnection &connection);
117
118 private:
119 RequestManager m_requestManager;
120 Service m_service;
121 QDBusConnection m_connection;
122 };
123
124 /* Mocking UiProxy { */
125 namespace OnlineAccountsUi {
126
127 class UiProxyPrivate: public QObject
128 {
129 Q_OBJECT
130 Q_DECLARE_PUBLIC(UiProxy)
131
132 public:
133 UiProxyPrivate(UiProxy *uiProxy):
134 QObject(uiProxy),
135 m_initCount(0),
136 m_initReply(true),
137 q_ptr(uiProxy)
138 {
139 }
140 ~UiProxyPrivate() {};
141
142 void emitFinished() { Q_EMIT q_ptr->finished(); }
143
144 Q_SIGNALS:
145 void handleRequestCalled();
146
147 public:
148 int m_initCount;
149 bool m_initReply;
150 QList<Request*> m_requests;
151 QVariantMap m_expectedHasHandlerFor;
152 mutable UiProxy *q_ptr;
153 };
154
155 } // namespace
156
157 UiProxy::UiProxy(pid_t, QObject *parent):
158 QObject(parent),
159 d_ptr(new UiProxyPrivate(this))
160 {
161 m_uiProxies.append(d_ptr);
162 }
163
164 UiProxy::~UiProxy()
165 {
166 m_uiProxies.removeOne(d_ptr);
167 }
168
169 bool UiProxy::init()
170 {
171 Q_D(UiProxy);
172 d->m_initCount++;
173 return d->m_initReply;
174 }
175
176 void UiProxy::handleRequest(Request *request)
177 {
178 Q_D(UiProxy);
179 d->m_requests.append(request);
180 Q_EMIT d->handleRequestCalled();
181 }
182
183 bool UiProxy::hasHandlerFor(const QVariantMap &parameters)
184 {
185 Q_D(UiProxy);
186 return parameters == d->m_expectedHasHandlerFor;
187 }
188
189 /* } mocking UiProxy */
190
191 ServiceTest::ServiceTest():
192 QObject(0),
193 m_connection(QStringLiteral("uninitialized"))
194 {
195 }
196
197 void ServiceTest::onNewConnection(const QDBusConnection &connection)
198 {
199 QDBusConnection conn(connection);
200 conn.registerService(TEST_SERVICE_NAME);
201 conn.registerObject(TEST_OBJECT_PATH, &m_service);
202 }
203
204 void ServiceTest::initTestCase()
205 {
206 QDBusServer *server;
207 for (int i = 0; i < 10; i++) {
208 server = new QDBusServer(QString::fromLatin1(P2P_SOCKET).arg(i), this);
209 if (!server->isConnected()) {
210 delete server;
211 } else {
212 break;
213 }
214 }
215
216 QVERIFY(server->isConnected());
217
218 QObject::connect(server, SIGNAL(newConnection(const QDBusConnection &)),
219 this, SLOT(onNewConnection(const QDBusConnection &)));
220
221 m_connection = QDBusConnection::connectToPeer(server->address(),
222 QStringLiteral("tst"));
223 QTest::qWait(10);
224 QVERIFY(m_connection.isConnected());
225 }
226
227 void ServiceTest::testResults()
228 {
229 QVariantMap parameters;
230 parameters.insert("hello", QString("world"));
231 RequestReply *call = sendRequest(parameters);
232 QSignalSpy callFinished(call, SIGNAL(finished()));
233
234 QTRY_COMPARE(m_uiProxies.count(), 1);
235
236 UiProxyPrivate *proxy = m_uiProxies[0];
237 QCOMPARE(proxy->m_initCount, 1);
238 QCOMPARE(proxy->m_requests.count(), 1);
239
240 Request *request = proxy->m_requests.last();
241 QCOMPARE(request->parameters(), parameters);
242
243 request->setInProgress(true);
244 request->setResult(parameters);
245
246 QVERIFY(callFinished.wait());
247 QCOMPARE(call->isError(), false);
248 QCOMPARE(call->reply(), parameters);
249 delete call;
250
251 proxy->emitFinished();
252 QTRY_COMPARE(m_uiProxies.count(), 0);
253 }
254
255 void ServiceTest::testFailure()
256 {
257 QVariantMap parameters;
258 parameters.insert("hi", "there");
259 RequestReply *call = sendRequest(parameters);
260 QSignalSpy callFinished(call, SIGNAL(finished()));
261
262 QTRY_COMPARE(m_uiProxies.count(), 1);
263
264 UiProxyPrivate *proxy = m_uiProxies[0];
265 QCOMPARE(proxy->m_initCount, 1);
266 QCOMPARE(proxy->m_requests.count(), 1);
267
268 Request *request = proxy->m_requests.last();
269 QCOMPARE(request->parameters(), parameters);
270
271 request->setInProgress(true);
272 QString errorName("com.lomiri.OnlineAccountsUi.BadLuck");
273 QString errorMessage("really unlucky");
274 request->fail(errorName, errorMessage);
275
276 QVERIFY(callFinished.wait());
277 QCOMPARE(call->isError(), true);
278 QCOMPARE(call->errorName(), errorName);
279 QCOMPARE(call->errorMessage(), errorMessage);
280 delete call;
281
282 proxy->emitFinished();
283 QTRY_COMPARE(m_uiProxies.count(), 0);
284 }
285
286 void ServiceTest::testIdle()
287 {
288 QCOMPARE(m_requestManager.isIdle(), true);
289
290 QSignalSpy isIdleChanged(&m_requestManager, SIGNAL(isIdleChanged()));
291
292 QVariantMap parameters;
293 parameters.insert("time", "out");
294 RequestReply *call = sendRequest(parameters);
295 QSignalSpy callFinished(call, SIGNAL(finished()));
296
297 QVERIFY(isIdleChanged.wait());
298 QCOMPARE(m_requestManager.isIdle(), false);
299 isIdleChanged.clear();
300
301 QTRY_COMPARE(m_uiProxies.count(), 1);
302
303 UiProxyPrivate *proxy = m_uiProxies[0];
304 QCOMPARE(proxy->m_initCount, 1);
305 QCOMPARE(proxy->m_requests.count(), 1);
306
307 Request *request = proxy->m_requests.last();
308 QCOMPARE(request->parameters(), parameters);
309
310 request->setInProgress(true);
311 request->setResult(parameters);
312
313 /* the request will terminate, so expect the service
314 * to be idle again */
315 QTRY_COMPARE(isIdleChanged.count(), 1);
316 QCOMPARE(m_requestManager.isIdle(), true);
317
318 QVERIFY(callFinished.wait());
319 QCOMPARE(call->isError(), false);
320 delete call;
321
322 proxy->emitFinished();
323 QTRY_COMPARE(m_uiProxies.count(), 0);
324 }
325
326 QTEST_MAIN(ServiceTest);
327
328 #include "tst_service.moc"