File indexing completed on 2024-12-15 05:06:15

0001 /*
0002     SPDX-FileCopyrightText: 2023 Fushan Wen <qydwhotmail@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #define QT_FORCE_ASSERTS 1
0008 
0009 #include <QProcess>
0010 #include <QSignalSpy>
0011 #include <QTest>
0012 
0013 #include "mpris2model.h"
0014 #include "mprisinterface.h"
0015 
0016 class FetchInitialPlayerTest : public QObject
0017 {
0018     Q_OBJECT
0019 
0020 private Q_SLOTS:
0021     void cleanup();
0022 
0023     /**
0024      * Make sure if a player is already running before the media controller widget is added,
0025      * the widget can still provide player information.
0026      * @see plasma-workspace!9c5726ab353487580297c83ebe4ae67d14cd8830
0027      */
0028     void test_playerBeforeModel();
0029 
0030 private:
0031     QProcess *m_playerProcess = nullptr;
0032 };
0033 
0034 void FetchInitialPlayerTest::cleanup()
0035 {
0036     if (m_playerProcess) {
0037         MprisInterface::stopPlayer(m_playerProcess);
0038         m_playerProcess = nullptr;
0039     }
0040 }
0041 
0042 void FetchInitialPlayerTest::test_playerBeforeModel()
0043 {
0044     m_playerProcess = MprisInterface::startPlayer(this);
0045 
0046     std::unique_ptr<Mpris2Model> model1 = std::make_unique<Mpris2Model>();
0047     QSignalSpy rowsInsertedSpy1(model1.get(), &QAbstractItemModel::rowsInserted);
0048     if (model1->rowCount() == 0) {
0049         QVERIFY(rowsInsertedSpy1.wait());
0050     }
0051 
0052     // Because the filter model is shared in the same process, the bug is only testable when a second Mpris2Model is created.
0053     std::unique_ptr<Mpris2Model> model2 = std::make_unique<Mpris2Model>();
0054     QVERIFY(model2->rowCount() > 0);
0055     QVERIFY(model2->currentPlayer());
0056 }
0057 
0058 QTEST_GUILESS_MAIN(FetchInitialPlayerTest)
0059 
0060 #include "fetchinitialplayertest.moc"