File indexing completed on 2024-04-21 14:47:19

0001 /*
0002     SPDX-FileCopyrightText: 2017 Csaba Kertesz <csaba.kertesz@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kstars_ui_tests.h"
0008 
0009 #include "kswizard.h"
0010 #include "config-kstars.h"
0011 #include "auxiliary/kspaths.h"
0012 #include "test_kstars_startup.h"
0013 
0014 #if defined(HAVE_INDI)
0015 #include "test_ekos_wizard.h"
0016 #include "test_ekos.h"
0017 #include "test_ekos_simulator.h"
0018 #include "test_ekos_focus.h"
0019 #include "test_ekos_guide.h"
0020 #include "ekos/manager.h"
0021 #include "ekos/profileeditor.h"
0022 #include "ekos/profilewizard.h"
0023 #endif
0024 
0025 #include <KActionCollection>
0026 #include <KTipDialog>
0027 #include <KCrash/KCrash>
0028 #include <Options.h>
0029 
0030 #include <QFuture>
0031 #include <QtConcurrentRun>
0032 #include <QTest>
0033 #include <QTest>
0034 #include <QDateTime>
0035 #include <QStandardPaths>
0036 #include <QFileInfo>
0037 
0038 #include <ctime>
0039 #include <unistd.h>
0040 
0041 QSystemTrayIcon * KStarsUiTests::m_Notifier { nullptr };
0042 void KStarsUiTests::notifierBegin()
0043 {
0044     if (!m_Notifier)
0045         m_Notifier = new QSystemTrayIcon(QIcon::fromTheme("kstars_stars"), Ekos::Manager::Instance());
0046     if (m_Notifier)
0047         KStarsUiTests::m_Notifier->show();
0048 }
0049 
0050 void KStarsUiTests::notifierHide()
0051 {
0052     if (m_Notifier)
0053         m_Notifier->hide();
0054 }
0055 
0056 void KStarsUiTests::notifierMessage(QString title, QString message)
0057 {
0058     qDebug() << message.replace('\n',' ');
0059     if (m_Notifier)
0060         m_Notifier->showMessage(title, message, QIcon());
0061 }
0062 
0063 void KStarsUiTests::notifierEnd()
0064 {
0065     if (m_Notifier)
0066     {
0067         delete m_Notifier;
0068         m_Notifier = nullptr;
0069     }
0070 }
0071 
0072 // We want to launch the application before running our tests
0073 // Thus we want to explicitly call QApplication::exec(), and run our tests in parallel of the event loop
0074 // We then reimplement QTEST_MAIN(KStarsUiTests);
0075 // The same will have to be done when interacting with a modal dialog: exec() in main thread, tests in timer-based thread
0076 
0077 QT_BEGIN_NAMESPACE
0078 QTEST_ADD_GPU_BLACKLIST_SUPPORT_DEFS
0079 QT_END_NAMESPACE
0080 
0081 void prepare_tests()
0082 {
0083     // Configure our test UI
0084     QApplication::instance()->setAttribute(Qt::AA_Use96Dpi, true);
0085     QTEST_ADD_GPU_BLACKLIST_SUPPORT
0086     QTEST_SET_MAIN_SOURCE_PATH
0087     QApplication::processEvents();
0088 
0089     // Prepare our KStars configuration
0090     srand((unsigned int)time(nullptr));
0091     KSPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
0092     KSPaths::writableLocation(QStandardPaths::AppConfigLocation);
0093     KSPaths::writableLocation(QStandardPaths::CacheLocation);
0094     KCrash::initialize();
0095 
0096     // Explicitly provide the RC file from the main app resources, not the user-customized one
0097     KStars::setResourceFile(":/kxmlgui5/kstars/kstarsui.rc");
0098 }
0099 
0100 int run_wizards(int argc, char *argv[])
0101 {
0102     int failure = 0;
0103 
0104     // This cleans the test user settings, creates our instance and manages the startup wizard
0105     if (!failure)
0106     {
0107         TestKStarsStartup * ti = new TestKStarsStartup();
0108         failure |= QTest::qExec(ti);//, argc, argv);
0109         delete ti;
0110     }
0111 
0112 #if defined(HAVE_INDI)
0113     // If we test with INDI, this takes care of the Ekos startup wizard
0114     if (!failure)
0115     {
0116         TestEkosWizard * ew = new TestEkosWizard();
0117         failure |= QTest::qExec(ew);//, argc, argv);
0118         delete ew;
0119     }
0120 #endif
0121 
0122     Q_UNUSED(argc);
0123     Q_UNUSED(argv);
0124 
0125     return failure;
0126 }
0127 
0128 void execute_tests()
0129 {
0130     QCoreApplication *app = QApplication::instance();
0131 
0132     // Limit execution duration
0133     QTimer::singleShot(60 * 60 * 1000, app, &QCoreApplication::quit);
0134 
0135     app->exec();
0136 
0137     // Clean our instance up if it is still alive
0138     if( KStars::Instance() != nullptr)
0139     {
0140         KStars::Instance()->close();
0141         delete KStars::Instance();
0142     }
0143 }
0144 
0145 #if !defined(HAVE_INDI)
0146 QTEST_KSTARS_MAIN(KStarsUiTests)
0147 #endif