summaryrefslogtreecommitdiff
path: root/mpris2/playerinterfacetest.cpp
blob: 278fa81adf0182a071625a9ab6c76a7d99eb59f1 (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
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/*
 * Copyright 2011  Alex Merry <dev@randomguy3.me.uk>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "playerinterfacetest.h"

#include <QtDBus>

#define MPRIS2_PLAYER_IFACE "org.mpris.MediaPlayer2.Player"

using namespace Mpris2;

PlayerInterfaceTest::PlayerInterfaceTest(const QString& service, QObject* parent)
    : InterfaceTest(MPRIS2_PLAYER_IFACE, service, parent)
{
    m_pos = -1;
    m_currentRate = 0.0;
    propsNotUpdated << "Position";
}

PlayerInterfaceTest::~PlayerInterfaceTest()
{
}

void PlayerInterfaceTest::connectSignals()
{
    QDBusConnection::sessionBus().connect(
            iface->service(),
            iface->path(),
            iface->interface(),
            "Seeked", /* signature, */
            this,
            SLOT( _m_seeked(qint64,QDBusMessage)));
}

void PlayerInterfaceTest::checkUpdatedProperty(const QString& propName)
{
    if (propName == "CanControl") {
        checkPropValid("CanControl", QVariant::Bool);
    } else if (propName == "CanGoNext") {
        checkControlProp("CanGoNext");
    } else if (propName == "CanGoPrevious") {
        checkControlProp("CanGoPrevious");
    } else if (propName == "CanPlay") {
        checkControlProp("CanPlay");
    } else if (propName == "CanPause") {
        checkControlProp("CanPause");
    } else if (propName == "CanSeek") {
        checkControlProp("CanSeek");
    } else if (propName == "Shuffle") {
        checkPropValid("Shuffle", QVariant::Bool);
    } else if (propName == "Volume") {
        checkVolume();
    } else if (propName == "PlaybackStatus") {
        checkPlaybackStatus();
    } else if (propName == "LoopStatus") {
        checkLoopStatus();
    } else if (propName == "MinimumRate") {
        checkMinimumRate();
    } else if (propName == "MaximumRate") {
        checkMaximumRate();
    } else if (propName == "Rate") {
        checkPropValid("Rate", QVariant::Double);
    } else if (propName == "Metadata") {
        checkMetadata();
    }
}

void PlayerInterfaceTest::checkProps(const QVariantMap& oldProps)
{
    // position is time-sensitive; check it first
    checkPosition(oldProps);
    checkPredictedPosition();

    checkPropValid("CanControl", QVariant::Bool, oldProps);
    checkControlProp("CanGoNext", oldProps);
    checkControlProp("CanGoPrevious", oldProps);
    checkControlProp("CanPlay", oldProps);
    checkControlProp("CanPause", oldProps);
    checkControlProp("CanSeek", oldProps);
    if (properties().contains("Shuffle")) {
        checkPropValid("Shuffle", QVariant::Bool, oldProps);
    } else {
        emit interfaceInfo(Property, "Shuffle",
                           "Optional property not implemented");
    }
    checkVolume(oldProps);
    checkPlaybackStatus(oldProps);
    if (properties().contains("LoopStatus")) {
        checkLoopStatus(oldProps);
    } else {
        emit interfaceInfo(Property, "LoopStatus",
                           "Optional property not implemented");
    }
    checkMinimumRate(oldProps);
    checkMaximumRate(oldProps);
    checkPropValid("Rate", QVariant::Double, oldProps);
    checkMetadata(oldProps);

    checkConsistency();
}

void PlayerInterfaceTest::checkControlProp(const QString& propName, const QVariantMap& oldProps)
{
    if (!checkPropValid(propName, QVariant::Bool, oldProps))
        return;
    bool canControl = properties().value("CanControl").toBool();
    if (!canControl && properties().value(propName).toBool()) {
        emit interfaceError(Property, propName,
                            "Value is true when CanControl is false");
    }
}

void PlayerInterfaceTest::checkVolume(const QVariantMap& oldProps)
{
    if (!checkPropValid("Volume", QVariant::Double, oldProps))
        return;
    if (properties().value("Volume").toDouble() < 0.0) {
        emit interfaceError(Property, "Volume",
                            "Volume cannot be negative");
    }
    if (properties().value("Volume").toDouble() > 1.0) {
        emit interfaceWarning(Property, "Volume",
                            "Volume is greater than 1.0");
    }
}

void PlayerInterfaceTest::checkLoopStatus(const QVariantMap& oldProps)
{
    if (!properties().contains("LoopStatus"))
        return;
    if (!checkPropValid("LoopStatus", QVariant::String, oldProps))
        return;
    QString loopStatus = properties().value("LoopStatus").toString();
    if (loopStatus != "None" &&
        loopStatus != "Track" &&
        loopStatus != "Playlist")
    {
        emit interfaceError(Property, "LoopStatus",
                            "Invalid value: '" + loopStatus + "'");
    }
}

void PlayerInterfaceTest::checkPlaybackStatus(const QVariantMap& oldProps)
{
    if (!checkPropValid("PlaybackStatus", QVariant::String, oldProps))
        return;
    QString playbackStatus = properties().value("PlaybackStatus").toString();
    if (playbackStatus != "Playing" &&
        playbackStatus != "Paused" &&
        playbackStatus != "Stopped")
    {
        emit interfaceError(Property, "PlaybackStatus",
                            "Invalid value: '" + playbackStatus + "'");
    }
}

void PlayerInterfaceTest::checkMaximumRate(const QVariantMap& oldProps)
{
    if (!checkPropValid("MaximumRate", QVariant::Double, oldProps))
        return;
    double maxRate = properties().value("MaximumRate").toDouble();
    if (maxRate < 1.0) {
        emit interfaceWarning(Property, "MaximumRate",
                              "Maximum rate should not be less than 1.0");
    }
}

void PlayerInterfaceTest::checkMinimumRate(const QVariantMap& oldProps)
{
    if (!checkPropValid("MinimumRate", QVariant::Double, oldProps))
        return;
    double minRate = properties().value("MinimumRate").toDouble();
    if (minRate > 1.0) {
        emit interfaceWarning(Property, "MinimumRate",
                              "Minimum rate should not be greater than 1.0");
    }
    if (minRate < 0.0) {
        emit interfaceInfo(Property, "MinimumRate",
                           "Minimum rate is negative");
    }
}

void PlayerInterfaceTest::checkRate(const QVariantMap& oldProps)
{
    if (!checkPropValid("Rate", QVariant::Double, oldProps))
        return;
    double rate = properties().value("Rate").toDouble();
    if (qFuzzyCompare(rate, 0.0)) {
        emit interfaceError(Property, "Rate",
                            "Rate must not be 0.0");
    }
}

static bool compare(const QVariantMap& one, const QVariantMap& other)
{
    if (one.size() != other.size())
        return false;

    QVariantMap::const_iterator it1 = one.begin();
    QVariantMap::const_iterator it2 = other.begin();

    while (it1 != one.end()) {
        if (it1.value().userType() != it2.value().userType())
            return false;
        if (!(it1.value() == it2.value())) {
            if (it1.value().userType() == qMetaTypeId<QDBusObjectPath>()) {
                if (!(it1.value().value<QDBusObjectPath>() == it2.value().value<QDBusObjectPath>()))
                    return false;
            }
        }
        if (qMapLessThanKey(it1.key(), it2.key()) || qMapLessThanKey(it2.key(), it1.key()))
            return false;
        ++it2;
        ++it1;
    }
    return true;
}

void PlayerInterfaceTest::checkMetadata(const QVariantMap& oldProps)
{
    if (!props.contains("Metadata")) {
        emit interfaceError(Property, "Metadata", "Property Metadata is missing");
        return;
    }
    if (props["Metadata"].type() != QVariant::Map &&
        !props.value("Metadata").canConvert<QDBusArgument>())
    {
        const char * gotTypeCh = QDBusMetaType::typeToSignature(props["Metadata"].userType());
        QString gotType = gotTypeCh ? QString::fromAscii(gotTypeCh) : "<unknown>";
        emit interfaceError(Property, "Metadata", "Property Metadata has type " + gotType + ", but should be type a{sv}");
        return;
    }
    QVariantMap metadata;
    if (props["Metadata"].type() == QVariant::Map) {
        metadata = props["Metadata"].toMap();
    } else {
        QDBusArgument arg = props["Metadata"].value<QDBusArgument>();
        arg >> metadata;
        // replace the entry in the properties array
        props["Metadata"] = metadata;
    }

    if (oldProps.contains("Metadata") &&
        oldProps.value("Metadata").canConvert(QVariant::Map))
    {
        QVariantMap oldMetadata = oldProps.value("Metadata").toMap();

        // custom compare fn as we're expecting a QDBusObjectPath entry
        if (!compare(metadata, oldMetadata)) {
            outOfDateProperties["Metadata"] = props["Metadata"];
            props["Metadata"] = oldProps["Metadata"];
            return;
        } else {
            // same as before; don't re-run checks
            return;
        }
    }
    if (metadata.isEmpty()) {
        emit interfaceInfo(Property, "Metadata",
                           "No metadata provided");
        return;
    }

    if (!metadata.contains("mpris:trackid")) {
        emit interfaceError(Property, "Metadata",
                            "No mpris:trackid entry for the current track");
    } else if (metadata.value("mpris:trackid").userType() != qMetaTypeId<QDBusObjectPath>()) {
        emit interfaceError(Property, "Metadata",
                            "mpris:trackid entry was not sent as a D-Bus object path (D-Bus type 'o')");
    } else if (metadata.value("mpris:trackid").value<QDBusObjectPath>().path().isEmpty()) {
        emit interfaceError(Property, "Metadata",
                            "mpris:trackid entry is an empty path");
    }

    checkMetadataEntry(metadata, "mpris:length", QVariant::LongLong);

    if (checkMetadataEntry(metadata, "mpris:artUrl", QVariant::Url)) {
        QString artUrl = metadata.value("mpris:artUrl").toString();
        QUrl asUrl(artUrl, QUrl::StrictMode);
        if (asUrl.scheme() != "file" && asUrl.scheme() != "http" && asUrl.scheme() != "https") {
            emit interfaceInfo(Property, "Metadata",
                                "mpris:artUrl has a scheme (" + asUrl.scheme() + ") which not all clients may recognise");
        } else {
            if (asUrl.scheme() == "file") {
                if (!QFile::exists(asUrl.toLocalFile())) {
                    emit interfaceInfo(Property, "Metadata",
                                        "mpris:artUrl references a file that does not exist");
                }
            }
            // TODO: check network files
        }
    }

    Q_FOREACH( QString key, metadata.keys() ) {
        if (!key.startsWith("xesam:")) {
            if (key != "mpris:trackid" &&
                key != "mpris:length" &&
                key != "mpris:artUrl")
            {
                emit interfaceWarning(Property, "Metadata",
                                      "Unrecognised entry " + key);
            }
        }
    }

    checkMetadataEntry(metadata, "xesam:album", QVariant::String);
    checkMetadataEntry(metadata, "xesam:albumArtist", QVariant::StringList);
    checkMetadataEntry(metadata, "xesam:artist", QVariant::StringList);
    checkMetadataEntry(metadata, "xesam:asText", QVariant::String);
    checkMetadataEntry(metadata, "xesam:audioBpm", QVariant::Int);
    checkMetadataEntry(metadata, "xesam:autoRating", QVariant::Double);
    checkMetadataEntry(metadata, "xesam:comment", QVariant::StringList);
    checkMetadataEntry(metadata, "xesam:composer", QVariant::StringList);
    checkMetadataEntry(metadata, "xesam:contentCreator", QVariant::DateTime);
    checkMetadataEntry(metadata, "xesam:discNumber", QVariant::Int);
    checkMetadataEntry(metadata, "xesam:firstUsed", QVariant::DateTime);
    checkMetadataEntry(metadata, "xesam:genre", QVariant::StringList);
    checkMetadataEntry(metadata, "xesam:lastUsed", QVariant::DateTime);
    checkMetadataEntry(metadata, "xesam:lyricist", QVariant::StringList);
    checkMetadataEntry(metadata, "xesam:title", QVariant::String);
    checkMetadataEntry(metadata, "xesam:trackNumber", QVariant::Int);
    checkMetadataEntry(metadata, "xesam:url", QVariant::Url);
    checkMetadataEntry(metadata, "xesam:useCount", QVariant::Int);
    checkMetadataEntry(metadata, "xesam:userRating", QVariant::Double);
}

bool PlayerInterfaceTest::checkMetadataEntry(const QVariantMap& metadata, const QString& entry, QVariant::Type expType)
{
    if (metadata.contains(entry)) {
        QVariant value = metadata.value(entry);

        bool propertyTypeError = false;
        bool propertyTypeWarning = false;
        QVariant::Type realExpectedType = expType;
        if (expType == QVariant::DateTime || expType == QVariant::Url) {
            realExpectedType = QVariant::String;
        }

        // be lax about integers
        if (realExpectedType == QVariant::Int) {
            if (value.type() == QVariant::UInt ||
                value.type() == QVariant::LongLong ||
                value.type() == QVariant::ULongLong)
            {
                propertyTypeWarning = true;
            } else if (value.type() != QVariant::Int) {
                propertyTypeError = true;
            }
        } else if (realExpectedType == QVariant::UInt || realExpectedType == QVariant::LongLong) {
            if (value.type() == QVariant::ULongLong) {
                propertyTypeWarning = true;
            } else if (value.type() != realExpectedType) {
                propertyTypeError = true;
            }
        } else if (value.type() != realExpectedType) {
            propertyTypeError = true;
        }

        if (propertyTypeError || propertyTypeWarning) {
            const char * gotTypeCh = QDBusMetaType::typeToSignature(value.userType());
            QString gotType = gotTypeCh ? QString::fromAscii(gotTypeCh) : "<unknown>";
            const char * expTypeCh = QDBusMetaType::typeToSignature(realExpectedType);
            QString expType = expTypeCh ? QString::fromAscii(expTypeCh) : "<unknown>";
            if (propertyTypeError) {
                emit interfaceError(Property, "Metadata",
                                    entry + " entry is of type '" + gotType + "' but should have been of type '" + expType + "'");
                return false;
            } else {
                emit interfaceWarning(Property, "Metadata",
                                      entry + " entry is of type '" + gotType + "' but should have been of type '" + expType + "'");
                return true;
            }
        }

        // extra checks for special types
        if (expType == QVariant::DateTime) {
            QDateTime dtValue = QDateTime::fromString(value.toString(), Qt::ISODate);
            if (!dtValue.isValid()) {
                emit interfaceError(Property, "Metadata",
                                    entry + " entry does not contain a valid date/time string (value was " + value.toString() + ")");
                return false;
            }
        } else if (expType == QVariant::Url) {
            if (value.toString().isEmpty()) {
                return false;
            } else {
                QUrl asUrl(value.toString(), QUrl::StrictMode);
                if (!asUrl.isValid()) {
                    emit interfaceError(Property, "Metadata",
                                        entry + " entry is not a valid URL");
                    return false;
                }
            }
        }
        return true;
    }
    return false;
}

void PlayerInterfaceTest::checkPosition(const QVariantMap& oldProps)
{
    // don't pass in oldProps: Position doesn't get updated
    // automatically
    if (!checkPropValid("Position", QVariant::LongLong))
        return;

    qint64 position = properties().value("Position").toLongLong();
    if (position < 0) {
        emit interfaceError(Property, "Position",
                            "Negative position value");
    }
}

void PlayerInterfaceTest::checkConsistency(const QVariantMap& oldProps)
{
    checkRateConsistency();
    checkPositionConsistency();
}

void PlayerInterfaceTest::checkPositionConsistency(const QVariantMap& oldProps)
{
    if (!properties().contains("Position") ||
        !properties().contains("Metadata") ||
        !properties().value("Metadata").canConvert<QDBusArgument>()
    ) {
        return;
    }

    qint64 position = properties().value("Position").toLongLong();

    QVariantMap metadata;
    properties().value("Metadata").value<QDBusArgument>() >> metadata;
    if (!metadata.contains("mpris:length")) {
        return;
    }
    qint64 length = metadata.value("mpris:length").toLongLong();

    // drop out if the length and position have not changed
    if (oldProps.contains("Position") &&
        oldProps.value("Position") == position &&
        oldProps.contains("Metadata") &&
        oldProps.value("Metadata").canConvert<QDBusArgument>())
    {
        QVariantMap oldMetadata;
        oldProps.value("Metadata").value<QDBusArgument>() >> oldMetadata;
        if (oldMetadata.contains("mpris:length")) {
            if (length == oldMetadata.value("mpris:length").toLongLong()) {
                return;
            }
        }
    }

    if (position > length) {
        emit interfaceError(Property, "Position",
                            "Position is greater than the track length");
    }
}

void PlayerInterfaceTest::checkRateConsistency(const QVariantMap& oldProps)
{
    if (properties().value("MaximumRate") != oldProps.value("MaximumRate") ||
        properties().value("MinimumRate") != oldProps.value("MinimumRate"))
    {
        if (properties().contains("MaximumRate") && properties().contains("MinimumRate")) {
            double minRate = properties().value("MinimumRate").toDouble();
            double maxRate = properties().value("MaximumRate").toDouble();
            if (minRate > maxRate) {
                emit interfaceError(Property, "MinimumRate",
                                    "Minimum rate must not be greater than maximum rate");
            }
        }
    }
    if (properties().value("Rate") != oldProps.value("Rate") ||
        properties().value("MaximumRate") != oldProps.value("MaximumRate") ||
        properties().value("MinimumRate") != oldProps.value("MinimumRate"))
    {
        if (properties().contains("Rate")) {
            double rate = properties().value("Rate").toDouble();
            if (properties().contains("MaximumRate")) {
                double maxRate = properties().value("MaximumRate").toDouble();
                if (rate > maxRate) {
                    emit interfaceError(Property, "Rate",
                                        "Rate must not be greater than maximum rate");
                }
            }
            if (properties().contains("MinimumRate")) {
                double minRate = properties().value("MinimumRate").toDouble();
                if (minRate > rate) {
                    emit interfaceError(Property, "Rate",
                                        "Rate must not be less than minimum rate");
                }
            }
        }
    }
}

void PlayerInterfaceTest::updateCurrentRate()
{
    QString playbackStatus = properties().value("PlaybackStatus").toString();
    if (playbackStatus == "Playing") {
        m_currentRate = properties().value("Rate").toDouble();
    } else {
        m_currentRate = 0.0;
    }
}

void PlayerInterfaceTest::_m_seeked(qint64 position, const QDBusMessage& message)
{
    emit interfaceInfo(Signal, "Seeked", "Got Seeked(" + QString::number(position) + ") signal");
    m_pos = position;
    m_posLastUpdated = QTime::currentTime();
    props["Position"] = position;
    checkPosition();
    emit Seeked(position);
}

qint64 PlayerInterfaceTest::predictedPosition()
{
    qint64 elapsed = (qint64)m_posLastUpdated.elapsed() * 1000;
    return m_pos + (m_currentRate * elapsed);
}

void PlayerInterfaceTest::checkPredictedPosition()
{
    qint64 position = properties().value("Position").toLongLong();

    // if this is the initial fetch
    if (m_pos == -1) {
        m_pos = position;
        m_posLastUpdated = QTime::currentTime();
        updateCurrentRate();
        return;
    }

    qint64 predictedPos = predictedPosition();
    m_pos = position;
    m_posLastUpdated = QTime::currentTime();
    updateCurrentRate();

    qint64 diffMillis = (position - predictedPos) / 1000;
    // allow 1s of error
    const qint64 allowanceMillis = 1000;
    if (diffMillis > allowanceMillis ||
        diffMillis < -allowanceMillis)
    {
        qreal diffSecs = ((qreal)diffMillis) / 1000.0;
        qreal predictedPosSecs = ((qreal)(predictedPos/1000)) / 1000.0;
        qreal positionSecs = ((qreal)(position/1000)) / 1000.0;
        if (diffMillis > 0) {
            emit interfaceWarning(Property, "Position",
                                  "Position (" +
                                  QString::number(positionSecs, 'f', 2) +
                                  "s) is " +
                                  QString::number(diffSecs, 'f', 2) +
                                  "s ahead of what was predicted from Rate (" +
                                  QString::number(predictedPosSecs, 'f', 2) +
                                  "s)");
        } else {
            emit interfaceWarning(Property, "Position",
                                  "Position (" +
                                  QString::number(positionSecs, 'f', 2) +
                                  "s) is " +
                                  QString::number(-diffSecs, 'f', 2) +
                                  "s ahead of what was predicted from Rate (" +
                                  QString::number(predictedPosSecs, 'f', 2) +
                                  "s)");
        }
    }
}

void PlayerInterfaceTest::testNext()
{
    QDBusReply<void> reply = iface->call("Next");
    if (!reply.isValid()) {
        emit interfaceError(Method, "Next", "Call to Next failed with error " + reply.error().message());
    } else {
        if (!props["CanGoNext"].toBool()) {
            emit interfaceInfo(Method, "Next", "Next called, but CanGoNext is false, so this should have no effect");
            // TODO: check to see that the track does not change in the next second or so
        } else {
            emit interfaceInfo(Method, "Next", "Next called; the media player should now move to the next track");
            // TODO: check to see if the track changes
            // TODO: check to make sure the PlaybackStatus does not change
        }
    }
}

void PlayerInterfaceTest::testPrevious()
{
    QDBusReply<void> reply = iface->call("Previous");
    if (!reply.isValid()) {
        emit interfaceError(Method, "Previous", "Call to Previous failed with error " + reply.error().message());
    } else {
        if (!props["CanGoNext"].toBool()) {
            emit interfaceInfo(Method, "Previous", "Previous called, but CanGoPrevious is false, so this should have no effect");
            // TODO: check to see that the track does not change in the next second or so
        } else {
            emit interfaceInfo(Method, "Previous", "Previous called; the media player should now move to the next track");
            // TODO: check to see if the track changes
            // TODO: check to make sure the PlaybackStatus does not change
        }
    }
}

void PlayerInterfaceTest::testPause()
{
    QDBusReply<void> reply = iface->call("Pause");
    if (!reply.isValid()) {
        emit interfaceError(Method, "Pause", "Call to Pause failed with error " + reply.error().message());
    } else {
        if (!props["CanPause"].toBool()) {
            emit interfaceInfo(Method, "Pause", "Pause called, but CanPause is false, so this should have no effect");
            // TODO: check to see that the PlaybackStatus does not change
        } else if (props["PlaybackStatus"] == "Paused") {
            emit interfaceInfo(Method, "Pause", "Pause called, but already paused, so this should have no effect");
            // TODO: check to see that the PlaybackStatus does not change
        } else {
            emit interfaceInfo(Method, "Pause", "Pause called; the media player should now be paused");
            // TODO: check to see if the PlaybackStatus changes to Paused
        }
    }
}

void PlayerInterfaceTest::testPlayPause()
{
    QDBusReply<void> reply = iface->call("PlayPause");
    if (!reply.isValid()) {
        emit interfaceError(Method, "PlayPause", "Call to PlayPause failed with error " + reply.error().message());
    } else {
        if (!props["CanPause"].toBool()) {
            emit interfaceInfo(Method, "PlayPause", "PlayPause called, but CanPause is false, so this should have no effect");
            // TODO: check to see that the PlaybackStatus does not change
        } else if (props["PlaybackStatus"] == "Playing") {
            emit interfaceInfo(Method, "PlayPause", "PlayPause called; the media player should now be paused");
            // TODO: check to see if the PlaybackStatus changes to Paused
        } else {
            emit interfaceInfo(Method, "PlayPause", "PlayPause called; the media player should now be playing");
            // TODO: check to see if the PlaybackStatus changes to Playing
        }
    }
}

void PlayerInterfaceTest::testPlay()
{
    QDBusReply<void> reply = iface->call("Play");
    if (!reply.isValid()) {
        emit interfaceError(Method, "Play", "Call to Play failed with error " + reply.error().message());
    } else {
        if (!props["CanPlay"].toBool()) {
            emit interfaceInfo(Method, "Play", "Play called, but CanPlay is false, so this should have no effect");
            // TODO: check to see that the PlaybackStatus does not change
        } else if (props["PlaybackStatus"] == "Playing") {
            emit interfaceInfo(Method, "Play", "Play called, but already playing, so this should have no effect");
            // TODO: check to see that the PlaybackStatus does not change
        } else {
            emit interfaceInfo(Method, "Play", "Play called; the media player should start playback");
            // TODO: check to see if the PlaybackStatus changes to Playing
        }
    }
}

void PlayerInterfaceTest::testStop()
{
    QDBusReply<void> reply = iface->call("Stop");
    if (!reply.isValid()) {
        emit interfaceError(Method, "Stop", "Call to Stop failed with error " + reply.error().message());
    } else {
        if (!props["CanControl"].toBool()) {
            emit interfaceInfo(Method, "Stop", "Stop called, but CanControl is false, so this should have no effect");
            // TODO: check to see that the PlaybackStatus does not change
        } else if (props["PlaybackStatus"] == "Stopped") {
            emit interfaceInfo(Method, "Stop", "Stop called, but already stopped, so this should have no effect");
            // TODO: check to see that the PlaybackStatus does not change
        } else {
            emit interfaceInfo(Method, "Stop", "Stop called; the media player should now stop");
            // TODO: check to see if the PlaybackStatus changes to Stopped
        }
    }
}

void PlayerInterfaceTest::testSeek(qint64 offset)
{
    QDBusReply<void> reply = iface->call("Seek", QVariant::fromValue<qint64>(offset));
    if (!reply.isValid()) {
        emit interfaceError(Method, "Seek", "Call to Seek failed with error " + reply.error().message());
    } else {
        if (!props["CanSeek"].toBool()) {
            emit interfaceInfo(Method, "Seek", "Seek called, but CanSeek is false, so this should have no effect");
            // TODO: check to see that Seeked is not emitted; PlaybackStatus does not change
        } else {
            qint64 newPos = predictedPosition() + offset;
            if (newPos < 0.0) {
                emit interfaceInfo(Method, "Seek", "Seek called with a value that moved beyond the start of the track; the media player should be at the start of the track");
                // TODO: check to see that Seeked is emitted with value 0 (fuzzy)
            } else if (props.contains("mpris:length") &&
                       props["mpris:length"].toLongLong() <= newPos) {
                if (!props["CanGoNext"].toBool()) {
                    emit interfaceInfo(Method, "Seek", "Seek called with a value that would move beyond the end of the track, but CanGoNext is false, so playback should stop");
                    // TODO: check to see that PlaybackStatus changes to Stopped
                } else {
                    emit interfaceInfo(Method, "Seek", "Seek called with a value that would move beyond the end of the track; the media player should now move to the next track");
                    // TODO: check to see if the track changes
                    // TODO: check to make sure the PlaybackStatus does not change
                }
            } else {
                emit interfaceInfo(Method, "Seek", "Seek called; the media player should seek to " +
                        QString::number(newPos));
                // TODO: check that Seeked is emitted with the relevant value (fuzzy)
            }
        }
    }
}

void PlayerInterfaceTest::testSetPosition(const QDBusObjectPath& trackId, qint64 position)
{
    QDBusReply<void> reply = iface->call("SetPosition",
                                         QVariant::fromValue<QDBusObjectPath>(trackId),
                                         QVariant::fromValue<qint64>(position));
    if (!reply.isValid()) {
        emit interfaceError(Method, "SetPosition", "Call to SetPosition failed with error " + reply.error().message());
    } else {
        if (!props["CanSeek"].toBool()) {
            emit interfaceInfo(Method, "SetPosition", "SetPosition called, but CanSeek is false, so this should have no effect");
            // TODO: check to see that Seeked is not emitted; PlaybackStatus does not change
        } else {
            if (trackId.path() != props["Metadata"].toMap()["mpris:trackid"].value<QDBusObjectPath>().path()) {
                emit interfaceInfo(Method, "SetPosition", "SetPosition called with the wrong trackid ('" +
                                   trackId.path() +
                                   "'; expecting '" +
                                   props["mpris:trackid"].value<QDBusObjectPath>().path() +
                                   "'); nothing should happen");
                // TODO: check to see that Seeked is not emitted; PlaybackStatus does not change
            } else if (position < 0.0) {
                emit interfaceInfo(Method, "SetPosition", "SetPosition called with a negative value; the media player should be at the start of the track");
                // TODO: check to see that Seeked is emitted with value 0
            } else if (props.contains("mpris:length") &&
                       props["mpris:length"].toLongLong() <= position) {
                if (!props["CanGoNext"].toBool()) {
                    emit interfaceInfo(Method, "SetPosition", "SetPosition called with a value that would move beyond the end of the track, but CanGoNext is false, so playback should stop");
                    // TODO: check to see that PlaybackStatus changes to Stopped
                } else {
                    emit interfaceInfo(Method, "SetPosition", "SetPosition called with a value that would move beyond the end of the track; the media player should now move to the next track");
                    // TODO: check to see if the track changes
                    // TODO: check to make sure the PlaybackStatus does not change
                }
            } else {
                emit interfaceInfo(Method, "SetPosition", "SetPosition called; the media player should seek to the new position");
                // TODO: check that Seeked is emitted with the relevant value
            }
        }
    }
}

void PlayerInterfaceTest::testOpenUri(const QString& uri)
{
    QDBusReply<void> reply = iface->call("OpenUri",
                                         QVariant::fromValue<QString>(uri));
    if (!reply.isValid()) {
        emit interfaceInfo(Method, "OpenUri", "Call to OpenUri failed with error " + reply.error().message());
        // TODO: check SupportedUriSchemes - if empty, this method may be unimplemented
        //       if uri scheme is not in the list of schemes or not a valid uri, argument
        //       error may be returned
    } else {
        emit interfaceInfo(Method, "OpenUri", "Call to OpenUri did not return an error");
        // ?
    }
}