File indexing completed on 2024-04-21 16:17:26

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Daniel Vratil <dvratil@redhat.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  *
0006  */
0007 
0008 #include <QObject>
0009 #include <QSignalSpy>
0010 #include <QtTest>
0011 
0012 #include "../src/backendmanager_p.h"
0013 #include "../src/config.h"
0014 #include "../src/configmonitor.h"
0015 #include "../src/configoperation.h"
0016 #include "../src/getconfigoperation.h"
0017 #include "../src/output.h"
0018 #include "../src/setconfigoperation.h"
0019 #include <QSignalSpy>
0020 
0021 #include "fakebackendinterface.h"
0022 
0023 class TestConfigMonitor : public QObject
0024 {
0025     Q_OBJECT
0026 public:
0027     TestConfigMonitor()
0028     {
0029     }
0030 
0031     KScreen::ConfigPtr getConfig()
0032     {
0033         auto op = new KScreen::GetConfigOperation();
0034         if (!op->exec()) {
0035             qWarning("Failed to retrieve backend: %s", qPrintable(op->errorString()));
0036             return KScreen::ConfigPtr();
0037         }
0038 
0039         return op->config();
0040     }
0041 
0042 private Q_SLOTS:
0043     void initTestCase()
0044     {
0045         qputenv("KSCREEN_LOGGING", "false");
0046         qputenv("KSCREEN_BACKEND", "Fake");
0047         // This particular test is only useful for out of process operation, so enforce that
0048         qputenv("KSCREEN_BACKEND_INPROCESS", "0");
0049         KScreen::BackendManager::instance()->shutdownBackend();
0050     }
0051 
0052     void cleanupTestCase()
0053     {
0054         KScreen::BackendManager::instance()->shutdownBackend();
0055     }
0056 
0057     void testChangeNotifyInProcess()
0058     {
0059         qputenv("KSCREEN_BACKEND_INPROCESS", "1");
0060         KScreen::BackendManager::instance()->shutdownBackend();
0061         KScreen::BackendManager::instance()->setMethod(KScreen::BackendManager::InProcess);
0062         // json file for the fake backend
0063         KScreen::BackendManager::instance()->setBackendArgs({{QStringLiteral("TEST_DATA"), TEST_DATA "singleoutput.json"}});
0064 
0065         // Prepare monitor
0066         KScreen::ConfigMonitor *monitor = KScreen::ConfigMonitor::instance();
0067         QSignalSpy spy(monitor, SIGNAL(configurationChanged()));
0068 
0069         // Get config and monitor it for changes
0070         KScreen::ConfigPtr config = getConfig();
0071         monitor->addConfig(config);
0072         QSignalSpy enabledSpy(config->outputs().first().data(), SIGNAL(isEnabledChanged()));
0073 
0074         auto output = config->outputs().first();
0075 
0076         output->setEnabled(false);
0077         auto setop = new KScreen::SetConfigOperation(config);
0078         QVERIFY(!setop->hasError());
0079         setop->exec();
0080         QTRY_VERIFY(!spy.isEmpty());
0081 
0082         QCOMPARE(spy.size(), 1);
0083         QCOMPARE(enabledSpy.size(), 1);
0084         QCOMPARE(config->output(1)->isEnabled(), false);
0085 
0086         output->setEnabled(false);
0087         auto setop2 = new KScreen::SetConfigOperation(config);
0088         QVERIFY(!setop2->hasError());
0089         setop2->exec();
0090         QTRY_VERIFY(!spy.isEmpty());
0091         QCOMPARE(spy.size(), 2);
0092     }
0093 };
0094 
0095 QTEST_MAIN(TestConfigMonitor)
0096 
0097 #include "testconfigmonitor.moc"