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 #pragma once 0008 0009 #include <QProcess> 0010 #include <QSignalSpy> 0011 0012 namespace MprisInterface 0013 { 0014 QProcess *startPlayer(QObject *parent) 0015 { 0016 auto process = new QProcess(parent); 0017 process->setProgram(QStringLiteral("python3")); 0018 process->setArguments({QFINDTESTDATA(QStringLiteral("../../appiumtests/utils/mediaplayer.py")), // 0019 QFINDTESTDATA(QStringLiteral("../../appiumtests/resources/player_b.json"))}); 0020 QSignalSpy startedSpy(process, &QProcess::started); 0021 QSignalSpy readyReadSpy(process, &QProcess::readyReadStandardOutput); 0022 process->setReadChannel(QProcess::StandardOutput); 0023 process->start(QIODeviceBase::ReadOnly); 0024 if (process->state() != QProcess::Running) { 0025 Q_ASSERT(startedSpy.wait()); 0026 } 0027 0028 bool registered = false; 0029 for (int i = 0; i < 10; ++i) { 0030 if (process->isReadable() && process->readAllStandardError().contains("MPRIS registered")) { 0031 registered = true; 0032 break; 0033 } 0034 readyReadSpy.wait(); 0035 } 0036 0037 qDebug() << process->state(); 0038 qDebug() << process->readAllStandardError(); 0039 Q_ASSERT(registered); 0040 0041 return process; 0042 } 0043 0044 void stopPlayer(QProcess *process) 0045 { 0046 QSignalSpy finishedSpy(process, &QProcess::finished); 0047 process->terminate(); 0048 if (process->state() == QProcess::Running) { 0049 QVERIFY(finishedSpy.wait()); 0050 } 0051 delete process; 0052 } 0053 0054 }