aboutsummaryrefslogtreecommitdiff
path: root/tests/ut_mgconfdatastore/ut_mgconfdatastore.cpp
blob: dd9f3d9880770280d66f119f089ea9745e69c845 (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
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@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
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include "ut_mgconfdatastore.h"
#include <mgconfdatastore.h>

#include <mgconfitem.h>
#include <QtTest/QtTest>

// MGConfItem stubs
struct MGConfItemPrivate {
    QString key;
    QVariant value;
};

QMultiHash<QString, MGConfItem *> gMGConfItems;
QList<QString> gUnsetMGconfItems;

MGConfItem::MGConfItem(const QString &key, QObject *parent)
    : QObject(parent)
{
    priv = new MGConfItemPrivate;
    priv->key = key;

    // Set default value
    if (priv->key == "/gconf/key") {
        priv->value = QVariant("value");
    } else if (priv->key == "/gconf/key1") {
        priv->value = QVariant("value1");
    } else if (priv->key == "/gconf/key2") {
        priv->value = QVariant("value2");
    }

    gMGConfItems.insert(key, this);
}

MGConfItem::~MGConfItem()
{
    gMGConfItems.remove(priv->key, this);
    delete priv;
}

QList<QString> MGConfItem::listEntries() const
{
    QList<QString> entries;
    entries.append("/gconf/key");
    entries.append("/gconf/key1");
    entries.append("/gconf/key2");

    return entries;
}

QVariant MGConfItem::value() const
{
    return priv->value;
}

void MGConfItem::set(const QVariant &val)
{
    priv->value = val;
}

void MGConfItem::unset()
{
    gUnsetMGconfItems.append(priv->key);
    set(QVariant());
}

// Test class
void Ut_MGConfDataStore::initTestCase()
{
}

void Ut_MGConfDataStore::cleanupTestCase()
{
}

void Ut_MGConfDataStore::init()
{
    stringVariantSlotArguments.clear();
    gMGConfItems.clear();
    gUnsetMGconfItems.clear();

    m_subject = new MGConfDataStore("/gconf");
}

void Ut_MGConfDataStore::cleanup()
{
    delete m_subject;
    m_subject = NULL;

    // Check after deleting the test subject that it didn't leave any memory reserved
    QCOMPARE(gMGConfItems.count(), 0);
}

void Ut_MGConfDataStore::testReading()
{
    QCOMPARE(m_subject->value("key").toString(), QString("value"));
}

void Ut_MGConfDataStore::testWriting()
{
    QCOMPARE(m_subject->setValue("key", "newValue"), true);
    QCOMPARE(m_subject->value("key").toString(), QString("newValue"));
    QCOMPARE(m_subject->createValue("foo", "bar"), true);
    QCOMPARE(m_subject->value("foo").toString(), QString("bar"));
}

void Ut_MGConfDataStore::testReadingNonExistingKey()
{
    // Try to read from an empty container
    QCOMPARE(m_subject->value("nonexisting"), QVariant(QVariant::Invalid));

    // Add something to the container but try to read a different key
    QCOMPARE(m_subject->value("nonexisting"), QVariant(QVariant::Invalid));
}

void Ut_MGConfDataStore::testWritingNonExistingKey()
{
    // Try to write to an empty container
    QCOMPARE(m_subject->setValue("nonexisting", "newValue"), false);

    // Add something to the container but try to write a different key
    QCOMPARE(m_subject->setValue("nonexisting", "newValue"), false);
}

void Ut_MGConfDataStore::testSignalReceivedWhenValueChanges()
{
    // Set a new value for the item
    MGConfItem *gconfItem = gMGConfItems.value("/gconf/key");
    gconfItem->set(QString("newValue"));

    // Set up signals
    connect(this, SIGNAL(valueChanged()), gconfItem, SIGNAL(valueChanged()));
    connect(m_subject, SIGNAL(valueChanged(QString, QVariant)), this, SLOT(stringVariantSlot(QString, QVariant)));
    emit valueChanged();

    QCOMPARE(stringVariantSlotArguments.count(), 1);
    {
        const QPair<QString, QVariant>& arguments = stringVariantSlotArguments.at(0);
        QCOMPARE(arguments.first, QString("key"));
        QCOMPARE(arguments.second.type(), QVariant::String);
        QCOMPARE(arguments.second.toString(), QString("newValue"));
    }

    // Add a second key, change the first and check that the correct information is signaled
    gconfItem->set(QString("secondNewValue"));
    stringVariantSlotArguments.clear();
    emit valueChanged();

    QCOMPARE(stringVariantSlotArguments.count(), 1);
    {
        const QPair<QString, QVariant>& arguments = stringVariantSlotArguments.at(0);
        QCOMPARE(arguments.first, QString("key"));
        QCOMPARE(arguments.second.type(), QVariant::String);
        QCOMPARE(arguments.second.toString(), QString("secondNewValue"));
    }

    // Now change the second value and check that the correct information is signaled
    disconnect(this, SIGNAL(valueChanged()), gconfItem, SIGNAL(valueChanged()));
    gconfItem = gMGConfItems.value("/gconf/key1");
    gconfItem->set(QString("newValue1"));
    stringVariantSlotArguments.clear();
    // Set up signals
    connect(this, SIGNAL(valueChanged()), gconfItem, SIGNAL(valueChanged()));
    emit valueChanged();

    QCOMPARE(stringVariantSlotArguments.count(), 1);
    {
        const QPair<QString, QVariant>& arguments = stringVariantSlotArguments.at(0);
        QCOMPARE(arguments.first, QString("key1"));
        QCOMPARE(arguments.second.type(), QVariant::String);
        QCOMPARE(arguments.second.toString(), QString("newValue1"));
    }
}

void Ut_MGConfDataStore::stringVariantSlot(const QString &string, const QVariant &variant)
{
    stringVariantSlotArguments.append(qMakePair(string, variant));
}

void Ut_MGConfDataStore::testAllKeys()
{
    QStringList keys = m_subject->allKeys();
    QCOMPARE(keys.count(), 3);
    QVERIFY(keys.contains("key"));
    QVERIFY(keys.contains("key1"));
    QVERIFY(keys.contains("key2"));
}

void Ut_MGConfDataStore::testRemove()
{
    QVERIFY(m_subject->setValue("key", "newValue"));
    m_subject->remove("key");
    QCOMPARE(m_subject->value("key"), QVariant(QVariant::Invalid));
}

void Ut_MGConfDataStore::testClear()
{
    QCOMPARE(gMGConfItems.count(), 3);
    m_subject->clear();
    QCOMPARE(gMGConfItems.count(), 0);
}

void Ut_MGConfDataStore::testContains()
{
    QVERIFY(m_subject->contains("key"));
    QVERIFY(!m_subject->contains("bar"));
}

void Ut_MGConfDataStore::testSubPathsNotSupported()
{
    QVERIFY(!m_subject->setValue("key/subkey", 123));
    QVERIFY(!m_subject->createValue("key/subkey", 123));
}


QTEST_APPLESS_MAIN(Ut_MGConfDataStore)