File: communicationtest.cpp

package info (click to toggle)
libindicate-qt 0.2.5.91-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 388 kB
  • ctags: 262
  • sloc: cpp: 1,655; makefile: 6
file content (278 lines) | stat: -rw-r--r-- 8,586 bytes parent folder | download
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
/*
 * Qt wrapper for libindicate
 *
 * Copyright 2009 Canonical Ltd.
 *
 * Authors:
 * - Aurélien Gâteau <aurelien.gateau@canonical.com>
 *
 * License: LGPL v2.1 or LGPL v3
 */
// Self
#include "communicationtest.h"

// Qt
#include <QMetaType>
#include <QImage>
#include <QPainter>
#include <QtTest>

// Local
#include "qindicatedecode.h"

const QString SERVER_TYPE = "message";

QTEST_MAIN(CommunicationTest)

void CommunicationTest::initTestCase()
{
    qRegisterMetaType<QIndicate::Listener::Server*>("QIndicate::Listener::Server*");
    qRegisterMetaType<QIndicate::Listener::Indicator*>("QIndicate::Listener::Indicator*");
    qRegisterMetaType<QIndicate::Interest>("QIndicate::Interest");
    qRegisterMetaType<QIndicate::Indicator*>("QIndicate::Indicator*");
}

void CommunicationTest::init()
{
    mListener = QIndicate::Listener::defaultInstance(); //new QIndicate::Listener;

    mServer = QIndicate::Server::defaultInstance();
    mServer->setType(SERVER_TYPE);

    mIndicator = new QIndicate::Indicator(mServer);

    mListenerServer = 0;
    mListenerIndicator = 0;
}

void CommunicationTest::showIndicatorAndGetProxies()
{
    QSignalSpy addedSpy(mListener, SIGNAL(indicatorAdded(QIndicate::Listener::Server*, QIndicate::Listener::Indicator*)));
    mIndicator->show();
    QTest::qWait(500);

    QCOMPARE(addedSpy.count(), 1);
    QVariantList args = addedSpy.takeFirst();
    mListenerServer = args[0].value<QIndicate::Listener::Server*>();
    mListenerIndicator = args[1].value<QIndicate::Listener::Indicator*>();
    QVERIFY(mListenerServer);
    QVERIFY(mListenerIndicator);
}

void CommunicationTest::cleanup()
{
    delete mServer;
    mServer = 0;
}

void CommunicationTest::testIndicatorModified()
{
    const QString key = "key";
    const QString v1 = "v1";
    const QString v2 = "v2";

    QSignalSpy spy(mListener, SIGNAL(indicatorModified(QIndicate::Listener::Server*, QIndicate::Listener::Indicator*, const QString&)));

    // Should not be notified of changes until mIndicator is shown
    mIndicator->setIndicatorProperty(key, v1);
    QTest::qWait(500);
    QCOMPARE(spy.count(), 0);

    // Now we should get some change
    mIndicator->show();
    QTest::qWait(500);
    mIndicator->setIndicatorProperty(key, v2);
    QTest::qWait(500);

    QCOMPARE(spy.count(), 1);
    QVariantList args = spy.takeFirst();
    QCOMPARE(args[2].toString(), key);
}

static QImage generateTestImage()
{
    QImage image(22, 22, QImage::Format_ARGB32);
    QPainter painter(&image);
    painter.fillRect(image.rect(), Qt::blue);
    painter.fillRect(image.rect().adjusted(4, 4, -4, -4), Qt::black);
    return image;
}

void CommunicationTest::testGetIndicatorProperty()
{
    const QString key1 = "key1";
    const QString v1 = "v1";
    const QString key2 = "key2";
    const QImage v2(generateTestImage());
    const QString key3 = "key3";
    const QDateTime v3 = QDateTime(QDate::currentDate(), QTime(23, 45, 56));

    showIndicatorAndGetProxies();

    mIndicator->setIndicatorProperty(key1, v1);
    mIndicator->setIndicatorProperty(key2, v2);
    mIndicator->setIndicatorProperty(key3, v3);
    QTest::qWait(500);

    PropertyReceiver receiver;

    // key1
    mListener->getIndicatorProperty(mListenerServer, mListenerIndicator, key1,
        &receiver, SLOT(slotIndicatorPropertyReceived(
            QIndicate::Listener::Server*,
            QIndicate::Listener::Indicator*,
            const QString&,
            const QByteArray&)
            )
        );
    QTest::qWait(500);

    QCOMPARE(receiver.server, mListenerServer);
    QCOMPARE(receiver.indicator, mListenerIndicator);
    QCOMPARE(receiver.key, key1);
    QCOMPARE(QIndicate::Decode::stringFromValue(receiver.value.toByteArray()), v1);

    // key2
    mListener->getIndicatorProperty(mListenerServer, mListenerIndicator, key2,
        &receiver, SLOT(slotIndicatorPropertyReceived(
            QIndicate::Listener::Server*,
            QIndicate::Listener::Indicator*,
            const QString&,
            const QByteArray&)
            )
        );
    QTest::qWait(500);

    QCOMPARE(receiver.server, mListenerServer);
    QCOMPARE(receiver.indicator, mListenerIndicator);
    QCOMPARE(receiver.key, key2);
    QCOMPARE(QIndicate::Decode::imageFromValue(receiver.value.toByteArray()), v2);

    // key3
    mListener->getIndicatorProperty(mListenerServer, mListenerIndicator, key3,
        &receiver, SLOT(slotIndicatorPropertyReceived(
            QIndicate::Listener::Server*,
            QIndicate::Listener::Indicator*,
            const QString&,
            const QByteArray&)
            )
        );
    QTest::qWait(500);

    QCOMPARE(receiver.server, mListenerServer);
    QCOMPARE(receiver.indicator, mListenerIndicator);
    QCOMPARE(receiver.key, key3);
    QCOMPARE(QIndicate::Decode::dateTimeFromValue(receiver.value.toByteArray()), v3);
}

void CommunicationTest::testGetServerDesktop()
{
    const QString value = "/path/to/foo.desktop";

    showIndicatorAndGetProxies();

    mServer->setDesktopFile(value);
    QTest::qWait(500);

    PropertyReceiver receiver;

    mListener->getServerDesktopFile(mListenerServer,
                                &receiver,
                                SLOT(slotServerQByteArrayPropertyReceived(
                                     QIndicate::Listener::Server*,
                                     const QByteArray&))
                                );
    QTest::qWait(500);

    QCOMPARE(receiver.server, mListenerServer);
    QCOMPARE(QIndicate::Decode::stringFromValue(receiver.value.toByteArray()), value);
}

void CommunicationTest::testGetServerType()
{
    showIndicatorAndGetProxies();

    PropertyReceiver receiver;

    mListener->getServerType(mListenerServer,
                             &receiver,
                             SLOT(slotServerQByteArrayPropertyReceived(
                                  QIndicate::Listener::Server*,
                                  const QByteArray&))
                             );
    QTest::qWait(500);

    QCOMPARE(receiver.server, mListenerServer);
    QCOMPARE(QIndicate::Decode::stringFromValue(receiver.value.toByteArray()), SERVER_TYPE);
}

void CommunicationTest::testGetServerCount()
{
    QSignalSpy spy(mListener, SIGNAL(serverCountChanged(QIndicate::Listener::Server*, int)));
    QCOMPARE(spy.count(), 0);

    showIndicatorAndGetProxies();
    QCOMPARE(spy.count(), 0);
    PropertyReceiver receiver;

    int count = 12;

    mServer->setCount(count);
    QTest::qWait(500);

    mListener->getServerCount(mListenerServer,
                              &receiver,
                              SLOT(slotServerIntPropertyReceived(
                                   QIndicate::Listener::Server*,
                                   int))
                              );
    QTest::qWait(500);

    QCOMPARE(receiver.server, mListenerServer);
    QCOMPARE(receiver.value.toInt(), count);
    QCOMPARE(spy.count(), 1);
    QVariantList args = spy.takeFirst();
    QCOMPARE(args[0].value<QIndicate::Listener::Server*>(), mListenerServer);
    QCOMPARE(args[1].toInt(), count);
}

void CommunicationTest::testDisplay()
{
    QSignalSpy serverDisplaySpy(mServer, SIGNAL(serverDisplay()));
    QSignalSpy indicatorDisplaySpy(mIndicator, SIGNAL(display(QIndicate::Indicator*)));

    showIndicatorAndGetProxies();

    mListener->display(mListenerServer, 0);
    QTest::qWait(500);
    QCOMPARE(serverDisplaySpy.count(), 1);

    mListener->display(mListenerServer, mListenerIndicator);
    QTest::qWait(500);
    QCOMPARE(indicatorDisplaySpy.count(), 1);
}

void CommunicationTest::testInterest()
{
    const QIndicate::Interest interest = QIndicate::InterestServerDisplay;
    showIndicatorAndGetProxies();

    QSignalSpy interestAddedSpy(mServer, SIGNAL(interestAdded(QIndicate::Interest)));
    QSignalSpy interestRemovedSpy(mServer, SIGNAL(interestRemoved(QIndicate::Interest)));

    QVERIFY(!mListener->getInterest(mListenerServer, interest));

    mListener->setInterest(mListenerServer, interest, true);
    QVERIFY(mListener->getInterest(mListenerServer, interest));
    QTest::qWait(500);
    QCOMPARE(interestAddedSpy.count(), 1);
    QCOMPARE(interestAddedSpy.takeFirst()[0].value<QIndicate::Interest>(), interest);

    mListener->setInterest(mListenerServer, interest, false);
    QVERIFY(!mListener->getInterest(mListenerServer, interest));
    QTest::qWait(500);
    QCOMPARE(interestRemovedSpy.count(), 1);
    QCOMPARE(interestRemovedSpy.takeFirst()[0].value<QIndicate::Interest>(), interest);
}

#include "communicationtest.moc"