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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
|
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used 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. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <QtCore/qtimer.h>
#include <QtMultimedia/qmediaobject.h>
#include <QtMultimedia/qmediaservice.h>
#include <QtMultimedia/qmetadatacontrol.h>
class tst_QMediaObject : public QObject
{
Q_OBJECT
private slots:
void propertyWatch();
void notifySignals_data();
void notifySignals();
void notifyInterval_data();
void notifyInterval();
void nullMetaDataControl();
void isMetaDataAvailable();
void isWritable();
void metaDataChanged();
void metaData_data();
void metaData();
void setMetaData_data();
void setMetaData();
void extendedMetaData_data() { metaData_data(); }
void extendedMetaData();
void setExtendedMetaData_data() { extendedMetaData_data(); }
void setExtendedMetaData();
private:
void setupNotifyTests();
};
class QtTestMetaDataProvider : public QMetaDataControl
{
Q_OBJECT
public:
QtTestMetaDataProvider(QObject *parent = 0)
: QMetaDataControl(parent)
, m_available(false)
, m_writable(false)
{
}
bool isMetaDataAvailable() const { return m_available; }
void setMetaDataAvailable(bool available) {
if (m_available != available)
emit metaDataAvailableChanged(m_available = available);
}
QList<QtMultimedia::MetaData> availableMetaData() const { return m_data.keys(); }
bool isWritable() const { return m_writable; }
void setWritable(bool writable) { emit writableChanged(m_writable = writable); }
QVariant metaData(QtMultimedia::MetaData key) const { return m_data.value(key); }
void setMetaData(QtMultimedia::MetaData key, const QVariant &value) {
m_data.insert(key, value); }
QVariant extendedMetaData(const QString &key) const { return m_extendedData.value(key); }
void setExtendedMetaData(const QString &key, const QVariant &value) {
m_extendedData.insert(key, value); }
QStringList availableExtendedMetaData() const { return m_extendedData.keys(); }
using QMetaDataControl::metaDataChanged;
void populateMetaData()
{
m_available = true;
}
bool m_available;
bool m_writable;
QMap<QtMultimedia::MetaData, QVariant> m_data;
QMap<QString, QVariant> m_extendedData;
};
class QtTestMetaDataService : public QMediaService
{
Q_OBJECT
public:
QtTestMetaDataService(QObject *parent = 0):QMediaService(parent), hasMetaData(true)
{
}
QMediaControl *control(const char *iid) const
{
if (hasMetaData && qstrcmp(iid, QMetaDataControl_iid) == 0)
return const_cast<QtTestMetaDataProvider *>(&metaData);
else
return 0;
}
QtTestMetaDataProvider metaData;
bool hasMetaData;
};
class QtTestMediaObject : public QMediaObject
{
Q_OBJECT
Q_PROPERTY(int a READ a WRITE setA NOTIFY aChanged)
Q_PROPERTY(int b READ b WRITE setB NOTIFY bChanged)
Q_PROPERTY(int c READ c WRITE setC NOTIFY cChanged)
Q_PROPERTY(int d READ d WRITE setD)
public:
QtTestMediaObject(QMediaService *service = 0): QMediaObject(0, service), m_a(0), m_b(0), m_c(0), m_d(0) {}
using QMediaObject::addPropertyWatch;
using QMediaObject::removePropertyWatch;
int a() const { return m_a; }
void setA(int a) { m_a = a; }
int b() const { return m_b; }
void setB(int b) { m_b = b; }
int c() const { return m_c; }
void setC(int c) { m_c = c; }
int d() const { return m_d; }
void setD(int d) { m_d = d; }
Q_SIGNALS:
void aChanged(int a);
void bChanged(int b);
void cChanged(int c);
private:
int m_a;
int m_b;
int m_c;
int m_d;
};
void tst_QMediaObject::propertyWatch()
{
QtTestMediaObject object;
object.setNotifyInterval(0);
QEventLoop loop;
connect(&object, SIGNAL(aChanged(int)), &QTestEventLoop::instance(), SLOT(exitLoop()));
connect(&object, SIGNAL(bChanged(int)), &QTestEventLoop::instance(), SLOT(exitLoop()));
connect(&object, SIGNAL(cChanged(int)), &QTestEventLoop::instance(), SLOT(exitLoop()));
QSignalSpy aSpy(&object, SIGNAL(aChanged(int)));
QSignalSpy bSpy(&object, SIGNAL(bChanged(int)));
QSignalSpy cSpy(&object, SIGNAL(cChanged(int)));
QTestEventLoop::instance().enterLoop(1);
QCOMPARE(aSpy.count(), 0);
QCOMPARE(bSpy.count(), 0);
QCOMPARE(cSpy.count(), 0);
int aCount = 0;
int bCount = 0;
int cCount = 0;
object.addPropertyWatch("a");
QTestEventLoop::instance().enterLoop(1);
QVERIFY(aSpy.count() > aCount);
QCOMPARE(bSpy.count(), 0);
QCOMPARE(cSpy.count(), 0);
QCOMPARE(aSpy.last().value(0).toInt(), 0);
aCount = aSpy.count();
object.setA(54);
object.setB(342);
object.setC(233);
QTestEventLoop::instance().enterLoop(1);
QVERIFY(aSpy.count() > aCount);
QCOMPARE(bSpy.count(), 0);
QCOMPARE(cSpy.count(), 0);
QCOMPARE(aSpy.last().value(0).toInt(), 54);
aCount = aSpy.count();
object.addPropertyWatch("b");
object.addPropertyWatch("d");
object.removePropertyWatch("e");
object.setA(43);
object.setB(235);
object.setC(90);
QTestEventLoop::instance().enterLoop(1);
QVERIFY(aSpy.count() > aCount);
QVERIFY(bSpy.count() > bCount);
QCOMPARE(cSpy.count(), 0);
QCOMPARE(aSpy.last().value(0).toInt(), 43);
QCOMPARE(bSpy.last().value(0).toInt(), 235);
aCount = aSpy.count();
bCount = bSpy.count();
object.removePropertyWatch("a");
object.addPropertyWatch("c");
object.addPropertyWatch("e");
QTestEventLoop::instance().enterLoop(1);
QCOMPARE(aSpy.count(), aCount);
QVERIFY(bSpy.count() > bCount);
QVERIFY(cSpy.count() > cCount);
QCOMPARE(bSpy.last().value(0).toInt(), 235);
QCOMPARE(cSpy.last().value(0).toInt(), 90);
bCount = bSpy.count();
cCount = cSpy.count();
object.setA(435);
object.setC(9845);
QTestEventLoop::instance().enterLoop(1);
QCOMPARE(aSpy.count(), aCount);
QVERIFY(bSpy.count() > bCount);
QVERIFY(cSpy.count() > cCount);
QCOMPARE(bSpy.last().value(0).toInt(), 235);
QCOMPARE(cSpy.last().value(0).toInt(), 9845);
bCount = bSpy.count();
cCount = cSpy.count();
object.setA(8432);
object.setB(324);
object.setC(443);
object.removePropertyWatch("c");
object.removePropertyWatch("d");
QTestEventLoop::instance().enterLoop(1);
QCOMPARE(aSpy.count(), aCount);
QVERIFY(bSpy.count() > bCount);
QCOMPARE(cSpy.count(), cCount);
QCOMPARE(bSpy.last().value(0).toInt(), 324);
QCOMPARE(cSpy.last().value(0).toInt(), 9845);
bCount = bSpy.count();
object.removePropertyWatch("b");
QTestEventLoop::instance().enterLoop(1);
QCOMPARE(aSpy.count(), aCount);
QCOMPARE(bSpy.count(), bCount);
QCOMPARE(cSpy.count(), cCount);
}
void tst_QMediaObject::setupNotifyTests()
{
QTest::addColumn<int>("interval");
QTest::addColumn<int>("count");
QTest::newRow("single 750ms")
<< 750
<< 1;
QTest::newRow("single 600ms")
<< 600
<< 1;
QTest::newRow("x3 300ms")
<< 300
<< 3;
QTest::newRow("x5 180ms")
<< 180
<< 5;
}
void tst_QMediaObject::notifySignals_data()
{
setupNotifyTests();
}
void tst_QMediaObject::notifySignals()
{
QFETCH(int, interval);
QFETCH(int, count);
QtTestMediaObject object;
object.setNotifyInterval(interval);
object.addPropertyWatch("a");
QSignalSpy spy(&object, SIGNAL(aChanged(int)));
QTestEventLoop::instance().enterLoop(1);
QCOMPARE(spy.count(), count);
}
void tst_QMediaObject::notifyInterval_data()
{
setupNotifyTests();
}
void tst_QMediaObject::notifyInterval()
{
QFETCH(int, interval);
QtTestMediaObject object;
QSignalSpy spy(&object, SIGNAL(notifyIntervalChanged(int)));
object.setNotifyInterval(interval);
QCOMPARE(object.notifyInterval(), interval);
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.last().value(0).toInt(), interval);
object.setNotifyInterval(interval);
QCOMPARE(object.notifyInterval(), interval);
QCOMPARE(spy.count(), 1);
}
void tst_QMediaObject::nullMetaDataControl()
{
const QString titleKey(QLatin1String("Title"));
const QString title(QLatin1String("Host of Seraphim"));
QtTestMetaDataService service;
service.hasMetaData = false;
QtTestMediaObject object(&service);
QSignalSpy spy(&object, SIGNAL(metaDataChanged()));
QCOMPARE(object.isMetaDataAvailable(), false);
QCOMPARE(object.isMetaDataWritable(), false);
object.setMetaData(QtMultimedia::Title, title);
object.setExtendedMetaData(titleKey, title);
QCOMPARE(object.metaData(QtMultimedia::Title).toString(), QString());
QCOMPARE(object.extendedMetaData(titleKey).toString(), QString());
QCOMPARE(object.availableMetaData(), QList<QtMultimedia::MetaData>());
QCOMPARE(object.availableExtendedMetaData(), QStringList());
QCOMPARE(spy.count(), 0);
}
void tst_QMediaObject::isMetaDataAvailable()
{
QtTestMetaDataService service;
service.metaData.setMetaDataAvailable(false);
QtTestMediaObject object(&service);
QCOMPARE(object.isMetaDataAvailable(), false);
QSignalSpy spy(&object, SIGNAL(metaDataAvailableChanged(bool)));
service.metaData.setMetaDataAvailable(true);
QCOMPARE(object.isMetaDataAvailable(), true);
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.at(0).at(0).toBool(), true);
service.metaData.setMetaDataAvailable(false);
QCOMPARE(object.isMetaDataAvailable(), false);
QCOMPARE(spy.count(), 2);
QCOMPARE(spy.at(1).at(0).toBool(), false);
}
void tst_QMediaObject::isWritable()
{
QtTestMetaDataService service;
service.metaData.setWritable(false);
QtTestMediaObject object(&service);
QSignalSpy spy(&object, SIGNAL(metaDataWritableChanged(bool)));
QCOMPARE(object.isMetaDataWritable(), false);
service.metaData.setWritable(true);
QCOMPARE(object.isMetaDataWritable(), true);
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.at(0).at(0).toBool(), true);
service.metaData.setWritable(false);
QCOMPARE(object.isMetaDataWritable(), false);
QCOMPARE(spy.count(), 2);
QCOMPARE(spy.at(1).at(0).toBool(), false);
}
void tst_QMediaObject::metaDataChanged()
{
QtTestMetaDataService service;
QtTestMediaObject object(&service);
QSignalSpy spy(&object, SIGNAL(metaDataChanged()));
service.metaData.metaDataChanged();
QCOMPARE(spy.count(), 1);
service.metaData.metaDataChanged();
QCOMPARE(spy.count(), 2);
}
void tst_QMediaObject::metaData_data()
{
QTest::addColumn<QString>("artist");
QTest::addColumn<QString>("title");
QTest::addColumn<QString>("genre");
QTest::newRow("")
<< QString::fromLatin1("Dead Can Dance")
<< QString::fromLatin1("Host of Seraphim")
<< QString::fromLatin1("Awesome");
}
void tst_QMediaObject::metaData()
{
QFETCH(QString, artist);
QFETCH(QString, title);
QFETCH(QString, genre);
QtTestMetaDataService service;
service.metaData.populateMetaData();
QtTestMediaObject object(&service);
QVERIFY(object.availableMetaData().isEmpty());
service.metaData.m_data.insert(QtMultimedia::AlbumArtist, artist);
service.metaData.m_data.insert(QtMultimedia::Title, title);
service.metaData.m_data.insert(QtMultimedia::Genre, genre);
QCOMPARE(object.metaData(QtMultimedia::AlbumArtist).toString(), artist);
QCOMPARE(object.metaData(QtMultimedia::Title).toString(), title);
QList<QtMultimedia::MetaData> metaDataKeys = object.availableMetaData();
QCOMPARE(metaDataKeys.size(), 3);
QVERIFY(metaDataKeys.contains(QtMultimedia::AlbumArtist));
QVERIFY(metaDataKeys.contains(QtMultimedia::Title));
QVERIFY(metaDataKeys.contains(QtMultimedia::Genre));
}
void tst_QMediaObject::setMetaData_data()
{
QTest::addColumn<QString>("title");
QTest::newRow("")
<< QString::fromLatin1("In the Kingdom of the Blind the One eyed are Kings");
}
void tst_QMediaObject::setMetaData()
{
QFETCH(QString, title);
QtTestMetaDataService service;
service.metaData.populateMetaData();
QtTestMediaObject object(&service);
object.setMetaData(QtMultimedia::Title, title);
QCOMPARE(object.metaData(QtMultimedia::Title).toString(), title);
QCOMPARE(service.metaData.m_data.value(QtMultimedia::Title).toString(), title);
}
void tst_QMediaObject::extendedMetaData()
{
QFETCH(QString, artist);
QFETCH(QString, title);
QFETCH(QString, genre);
QtTestMetaDataService service;
QtTestMediaObject object(&service);
QVERIFY(object.availableExtendedMetaData().isEmpty());
service.metaData.m_extendedData.insert(QLatin1String("Artist"), artist);
service.metaData.m_extendedData.insert(QLatin1String("Title"), title);
service.metaData.m_extendedData.insert(QLatin1String("Genre"), genre);
QCOMPARE(object.extendedMetaData(QLatin1String("Artist")).toString(), artist);
QCOMPARE(object.extendedMetaData(QLatin1String("Title")).toString(), title);
QStringList extendedKeys = object.availableExtendedMetaData();
QCOMPARE(extendedKeys.size(), 3);
QVERIFY(extendedKeys.contains(QLatin1String("Artist")));
QVERIFY(extendedKeys.contains(QLatin1String("Title")));
QVERIFY(extendedKeys.contains(QLatin1String("Genre")));
}
void tst_QMediaObject::setExtendedMetaData()
{
QtTestMetaDataService service;
service.metaData.populateMetaData();
QtTestMediaObject object(&service);
QString title(QLatin1String("In the Kingdom of the Blind the One eyed are Kings"));
object.setExtendedMetaData(QLatin1String("Title"), title);
QCOMPARE(object.extendedMetaData(QLatin1String("Title")).toString(), title);
QCOMPARE(service.metaData.m_extendedData.value(QLatin1String("Title")).toString(), title);
}
QTEST_MAIN(tst_QMediaObject)
#include "tst_qmediaobject.moc"
|