aboutsummaryrefslogtreecommitdiff
path: root/libcontextprovider/unit-tests/service/serviceunittest.cpp
blob: 7659367fc79c66467ff32267819c68e81d591e56 (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
/*
 * Copyright (C) 2008, 2009 Nokia Corporation.
 *
 * Contact: Marius Vollmer <marius.vollmer@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 "servicebackend.h" // mocked

#include "service.h" // to be tested

#include <QtTest/QtTest>
#include <QtCore>
#include <stdlib.h>

using namespace ContextProvider;

#define STATE_UNDEFINED 0
#define STATE_STOPPED 1
#define STATE_STARTED 2
#define STATE_RESTARTED 3

int lastState = STATE_UNDEFINED;

QString *lastKey = NULL;
QVariant *lastValue = NULL;
QDBusConnection *lastConnection = NULL;

/* Mocked ServiceBackend */

ServiceBackend* ServiceBackend::defaultServiceBackend = NULL;

ServiceBackend::ServiceBackend(QDBusConnection connection, const QString &busName) :
    connection(connection)
{
    lastState = STATE_UNDEFINED;
}

void ServiceBackend::setAsDefault()
{
    defaultServiceBackend = this;
}

void ServiceBackend::ref()
{
}

void ServiceBackend::unref()
{
}

bool ServiceBackend::start()
{
    if (lastState <= STATE_STOPPED) {
        lastState = STATE_STARTED;
        return true;
    } else
        return false;
}

void ServiceBackend::stop()
{
    lastState = STATE_STOPPED;
}

bool ServiceBackend::sharedConnection()
{
    return false;
}

ServiceBackend* ServiceBackend::instance(QDBusConnection connection)
{
    return new ServiceBackend(connection);
}

ServiceBackend* ServiceBackend::instance(QDBusConnection::BusType busType,
                                         const QString &busName, bool autoStart)
{
    ServiceBackend *r = new ServiceBackend(QDBusConnection::sessionBus(), busName);
    if (autoStart)
        r->start();

    return r;
}

void ServiceBackend::setValue(const QString &key, const QVariant &v)
{
    lastValue = new QVariant(v);
    lastKey = new QString(key);
}

/* Service unit test */

class ServiceUnitTest : public QObject
{
    Q_OBJECT

private slots:
    void init();
    void cleanup();
    void sanityCheck();
    void defaults();
    void setValue();
    void start();
    void setConnection();

private:
    Service *service;

    friend class ContextProvider::Service;
};

// Before each test
void ServiceUnitTest::init()
{
    service = new Service(QDBusConnection::SessionBus, "test.com");
}

// After each test
void ServiceUnitTest::cleanup()
{
    delete service;

    lastKey = NULL;
    lastValue = NULL;
    lastConnection = NULL;
}

void ServiceUnitTest::sanityCheck()
{
    QVERIFY(service->backend != NULL);
}

void ServiceUnitTest::defaults()
{
    QVERIFY(service->backend->defaultServiceBackend == NULL);
    service->setAsDefault();
    QVERIFY(service->backend->defaultServiceBackend == service->backend);

    Service *otherService = new Service(QDBusConnection::SessionBus, "test2");
    otherService->setAsDefault();
    QVERIFY(service->backend->defaultServiceBackend == otherService->backend);
    delete otherService;
}

void ServiceUnitTest::setValue()
{
    service->setValue("Battery.ChargeLevel", 99);
    QCOMPARE(*lastKey, QString("Battery.ChargeLevel"));
    QCOMPARE(lastValue->toInt(), 99);
}

void ServiceUnitTest::start()
{
    QVERIFY(lastState == STATE_STARTED);

    service->stop();
    QCOMPARE(lastState, STATE_STOPPED);

    QCOMPARE(service->start(), true);
    QCOMPARE(lastState, STATE_STARTED);

    QCOMPARE(service->start(), false);
    QCOMPARE(lastState, STATE_STARTED);

    service->restart();
    QCOMPARE(lastState, STATE_STARTED);
}

void ServiceUnitTest::setConnection()
{
    QCOMPARE(service->backend->connection.name(), QString("qt_default_session_bus"));
    QDBusConnection conn = QDBusConnection::connectToBus(QDBusConnection::SessionBus, "test_bus_name");
    service->setConnection(conn);
    QCOMPARE(service->backend->connection.name(), QString("test_bus_name"));
}


#include "serviceunittest.moc"
QTEST_MAIN(ServiceUnitTest);