aboutsummaryrefslogtreecommitdiff
path: root/libcontextprovider/customer-tests/service/servicetest.cpp
blob: 378f6bbfaf78ae7fb41ff30b3f54685ba6bd8f57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * This file is part of libcontextprovider.
 *
 * Copyright (C) 2009 Nokia Corporation.
 *
 * Contact: Jean-Luc Lamadon <jean-luc.lamadon@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include "servicetest.h"
#include "sconnect.h"

#include "service.h"
#include "property.h"

#include <QtTest/QtTest>
#include <QProcess>
#include <QStringList>
#include <string>

#define SERVICE_NAME "org.maemo.contextkit.testProvider"
int serviceNameIx = 0;

namespace ContextProvider {

void ServiceTest::initTestCase()
{
}

void ServiceTest::cleanupTestCase()
{
}

// Before each test
void ServiceTest::init()
{
    // Initialize test program state
    isReadyToRead = false;

    // Start the client
    client = new QProcess();
    sconnect(client, SIGNAL(readyReadStandardOutput()), this, SLOT(readStandardOutput()));
    client->start("client");
    // Record whether the client was successfully started
    clientStarted = client->waitForStarted();
}

// After each test
void ServiceTest::cleanup()
{
    // Stop the client
    if (clientStarted) {
        client->kill();
        client->waitForFinished();
    }
    delete client; client = NULL;
}

void ServiceTest::startStopStart()
{
    // Check that the initialization went well.
    // Doing this only in init() is not enough; doesn't stop the test case.
    QVERIFY(clientStarted);

    // Use a different service name in each test; this way the ServiceBackends
    QString serviceName = QString::number(serviceNameIx).prepend(SERVICE_NAME);
    writeToClient(("assign session " + serviceName + " service\n").toStdString().c_str());
    ++serviceNameIx;

    // Test: create a Service and an associated Property
    Service* service = new Service(QDBusConnection::SessionBus, serviceName);
    Property* property = new Property(*service, "Test.Property");

    // Test: command client to subscribe
    QString actual = writeToClient("subscribe service Test.Property\n");

    // Expected result: service is started automatically and Subscribe works
    QString expected = "Subscribe returned: Unknown";
    QCOMPARE(actual.simplified(), expected.simplified());

    // Test: stop the service and try to subscribe again
    service->stop();
    actual = writeToClient("subscribe service Test.Property\n");

    // Expected result: the client can no more subscribe
    expected = "Subscribe error: org.freedesktop.DBus.Error.ServiceUnknown";
    QCOMPARE(actual.simplified(), expected.simplified());

    // Test: start the service and try to subscribe again
    service->start();
    actual = writeToClient("subscribe service Test.Property\n");

    // Expected result: service is started and Subscribe works
    expected = "Subscribe returned: Unknown";
    QCOMPARE(actual.simplified(), expected.simplified());

    delete service;
    delete property;
}

void ServiceTest::recreate()
{
    // Check that the initialization went well.
    // Doing this only in init() is not enough; doesn't stop the test case.
    QVERIFY(clientStarted);

    // Use a different service name in each test; this way the ServiceBackends
    QString serviceName = QString::number(serviceNameIx).prepend(SERVICE_NAME);
    writeToClient(("assign session " + serviceName + " service\n").toStdString().c_str());
    ++serviceNameIx;

    // Test: create a Service and an associated Property
    Service* service = new Service(QDBusConnection::SessionBus, serviceName);
    Property* property = new Property(*service, "Test.Property");

    // Test: command client to subscribe
    QString actual = writeToClient("subscribe service Test.Property\n");

    // Expected result: service is started automatically and Subscribe works
    QString expected = "Subscribe returned: Unknown";
    QCOMPARE(actual.simplified(), expected.simplified());

    // Test: delete the property and the service
    delete property;
    delete service;

    actual = writeToClient("subscribe service Test.Property\n");

    // Expected result: the client can no more subscribe
    expected = "Subscribe error: org.freedesktop.DBus.Error.ServiceUnknown";
    QCOMPARE(actual.simplified(), expected.simplified());

    // Test: recreate the service and try to subscribe again
    service = new Service(QDBusConnection::SessionBus, serviceName);
    property = new Property(*service, "Test.Property");
    actual = writeToClient("subscribe service Test.Property\n");

    // Expected result: service is started and Subscribe works
    expected = "Subscribe returned: Unknown";
    QCOMPARE(actual.simplified(), expected.simplified());

    delete service;
    delete property;
}

void ServiceTest::multiStart()
{
    // Check that the initialization went well.
    // Doing this only in init() is not enough; doesn't stop the test case.
    QVERIFY(clientStarted);

    // Use a different service name in each test; this way the ServiceBackends
    QString serviceName = QString::number(serviceNameIx).prepend(SERVICE_NAME);
    writeToClient(("assign session " + serviceName + " service\n").toStdString().c_str());
    ++serviceNameIx;

    // Test: create a Service and an associated Property
    Service* service = new Service(QDBusConnection::SessionBus, serviceName);
    Property* property = new Property(*service, "Test.Property");

    // Test: command client to subscribe
    QString actual = writeToClient("subscribe service Test.Property\n");

    // Expected result: service is started automatically and Subscribe works
    QString expected = "Subscribe returned: Unknown";
    QCOMPARE(actual.simplified(), expected.simplified());

    // Start listening to signals (we already have one subscriber)
    QSignalSpy firstSpy(property,
                        SIGNAL(firstSubscriberAppeared(const QString&)));
    QSignalSpy lastSpy(property,
                        SIGNAL(firstSubscriberAppeared(const QString&)));

    // Test: start the service again (even though it's started)
    service->start();

    // Expected result: the service is still there, and remembers the client
    actual = writeToClient("subscribe service Test.Property\n");
    expected = "Subscribe returned: Unknown";
    QCOMPARE(actual.simplified(), expected.simplified());

    // (so we don't get a signal)
    QTest::qWait(1000);
    QCOMPARE(firstSpy.count(), 0);
    QCOMPARE(lastSpy.count(), 0);

    delete service;
    delete property;
}

void ServiceTest::defaultService()
{
    // Check that the initialization went well.
    // Doing this only in init() is not enough; doesn't stop the test case.
    QVERIFY(clientStarted);

    // Use a different service name in each test; this way the ServiceBackends
    QString serviceName = QString::number(serviceNameIx).prepend(SERVICE_NAME);
    writeToClient(("assign session " + serviceName + " service\n").toStdString().c_str());
    ++serviceNameIx;

    // Test: create a Service. Set the Service as default. Then create
    // a Property without a Service.
    Service* service = new Service(QDBusConnection::SessionBus, serviceName);
    service->setAsDefault();
    Property* property = new Property("Test.Property");

    // Expected result: the Property got associated with the default
    // Service.

    // Test: command client to subscribe
    QString actual = writeToClient("subscribe service Test.Property\n");

    // Expected result: Subscribe works
    QString expected = "Subscribe returned: Unknown";
    QCOMPARE(actual.simplified(), expected.simplified());

    delete service;
    delete property;
}

void ServiceTest::recreateProperty()
{
    // Check that the initialization went well.
    // Doing this only in init() is not enough; doesn't stop the test case.
    QVERIFY(clientStarted);

    // Use a different service name in each test; this way the ServiceBackends
    QString serviceName = QString::number(serviceNameIx).prepend(SERVICE_NAME);
    writeToClient(("assign session " + serviceName + " service\n").toStdString().c_str());
    ++serviceNameIx;

    // Test: create a Service and an associated Property. Set a value
    // to the property.
    Service* service = new Service(QDBusConnection::SessionBus, serviceName);
    Property* property = new Property(*service, "Test.Property");

    property->setValue("keep this value");

    // Test: delete the property and create it again
    delete property;
    property = new Property(*service, "Test.Property");

    // Expeted result: the Property has kept its value
    QCOMPARE(property->value(), QVariant("keep this value"));

    delete property;
    delete service;
}

void ServiceTest::readStandardOutput()
{
    isReadyToRead = true;
}

// Note: input must end with \n
QString ServiceTest::writeToClient(const char* input)
{
    isReadyToRead = false;
    client->write(input);
    client->waitForBytesWritten();

    // Blocking for reading operation is bad idea since the client
    // expects provider to reply to dbus calls

    while (!isReadyToRead) {
        QCoreApplication::processEvents(QEventLoop::AllEvents);
    }

    // Return the output from the client
    return client->readAll();
}

} // end namespace

QTEST_MAIN(ContextProvider::ServiceTest);