File indexing completed on 2024-04-28 16:54:57

0001 /*
0002     SPDX-FileCopyrightText: 2016 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "coronatesthelper.h"
0008 #include "debug.h"
0009 
0010 #include <PlasmaQuick/AppletQuickItem>
0011 
0012 using namespace Plasma;
0013 
0014 CoronaTestHelper::CoronaTestHelper(Corona *parent)
0015     : QObject(parent)
0016     , m_corona(parent)
0017     , m_exitcode(0)
0018 {
0019     connect(m_corona, &Corona::startupCompleted, this, &CoronaTestHelper::initialize);
0020 }
0021 
0022 void CoronaTestHelper::processContainment(Plasma::Containment *containment)
0023 {
0024     foreach (Plasma::Applet *applet, containment->applets()) {
0025         processApplet(applet);
0026     }
0027     connect(containment, &Plasma::Containment::appletAdded, this, &CoronaTestHelper::processApplet);
0028 }
0029 
0030 void CoronaTestHelper::processApplet(Plasma::Applet *applet)
0031 {
0032     PlasmaQuick::AppletQuickItem *obj = applet->property("_plasma_graphicObject").value<PlasmaQuick::AppletQuickItem *>();
0033     if (applet->failedToLaunch()) {
0034         qCWarning(PLASMASHELL) << "cannot test an applet with a launch error" << applet->launchErrorMessage();
0035         qGuiApp->exit(1);
0036         return;
0037     }
0038     if (!obj) {
0039         qCWarning(PLASMASHELL) << "cannot get AppletQuickItem for applet";
0040         qGuiApp->exit(1);
0041         return;
0042     }
0043 
0044     auto testObject = obj->testItem();
0045     if (!testObject) {
0046         qCWarning(PLASMASHELL) << "no test for" << applet->title();
0047         return;
0048     }
0049     integrateTest(testObject);
0050 }
0051 
0052 void CoronaTestHelper::integrateTest(QObject *testObject)
0053 {
0054     if (m_registeredTests.contains(testObject))
0055         return;
0056 
0057     const int signal = testObject->metaObject()->indexOfSignal("done()");
0058     if (signal < 0) {
0059         qCWarning(PLASMASHELL) << "the test object should offer a 'done()' signal" << testObject;
0060         return;
0061     }
0062     if (testObject->metaObject()->indexOfProperty("failed") < 0) {
0063         qCWarning(PLASMASHELL) << "the test object should offer a 'bool failed' property" << testObject;
0064         return;
0065     }
0066 
0067     qCDebug(PLASMASHELL) << "Test registered" << testObject;
0068     m_tests.insert(testObject);
0069     m_registeredTests << testObject;
0070 
0071     connect(testObject, SIGNAL(done()), this, SLOT(testFinished()));
0072 }
0073 
0074 void CoronaTestHelper::testFinished()
0075 {
0076     QObject *testObject = sender();
0077 
0078     const bool failed = testObject->property("failed").toBool();
0079     m_exitcode += failed;
0080     m_tests.remove(testObject);
0081 
0082     qCWarning(PLASMASHELL) << "test finished" << testObject << failed << "remaining" << m_tests;
0083     if (m_tests.isEmpty()) {
0084         qGuiApp->exit(m_exitcode);
0085     }
0086 }
0087 
0088 void CoronaTestHelper::initialize()
0089 {
0090     foreach (Plasma::Containment *containment, m_corona->containments()) {
0091         processContainment(containment);
0092     }
0093     connect(m_corona, &Corona::containmentAdded, this, &CoronaTestHelper::processContainment);
0094 
0095     if (m_tests.isEmpty()) {
0096         qCWarning(PLASMASHELL) << "no tests found for the corona" << QCoreApplication::instance()->arguments();
0097         qGuiApp->exit();
0098         return;
0099     }
0100 }