1 /*
2     SPDX-FileCopyrightText: 2017 Csaba Kertesz <csaba.kertesz@gmail.com>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "kstars_ui_tests.h"
8 
9 #include "kswizard.h"
10 #include "config-kstars.h"
11 #include "auxiliary/kspaths.h"
12 #include "test_kstars_startup.h"
13 
14 #if defined(HAVE_INDI)
15 #include "test_ekos_wizard.h"
16 #include "test_ekos.h"
17 #include "test_ekos_simulator.h"
18 #include "test_ekos_focus.h"
19 #include "test_ekos_guide.h"
20 #include "ekos/manager.h"
21 #include "ekos/profileeditor.h"
22 #include "ekos/profilewizard.h"
23 #endif
24 
25 #include <KActionCollection>
26 #include <KTipDialog>
27 #include <KCrash/KCrash>
28 #include <Options.h>
29 
30 #include <QFuture>
31 #include <QtConcurrentRun>
32 #include <QtTest>
33 #include <QTest>
34 #include <QDateTime>
35 #include <QStandardPaths>
36 #include <QFileInfo>
37 
38 #include <ctime>
39 #include <unistd.h>
40 
41 QSystemTrayIcon * KStarsUiTests::m_Notifier { nullptr };
notifierBegin()42 void KStarsUiTests::notifierBegin()
43 {
44     if (!m_Notifier)
45         m_Notifier = new QSystemTrayIcon(QIcon::fromTheme("kstars_stars"), Ekos::Manager::Instance());
46     if (m_Notifier)
47         KStarsUiTests::m_Notifier->show();
48 }
49 
notifierHide()50 void KStarsUiTests::notifierHide()
51 {
52     if (m_Notifier)
53         m_Notifier->hide();
54 }
55 
notifierMessage(QString title,QString message)56 void KStarsUiTests::notifierMessage(QString title, QString message)
57 {
58     qDebug() << message.replace('\n',' ');
59     if (m_Notifier)
60         m_Notifier->showMessage(title, message, QIcon());
61 }
62 
notifierEnd()63 void KStarsUiTests::notifierEnd()
64 {
65     if (m_Notifier)
66     {
67         delete m_Notifier;
68         m_Notifier = nullptr;
69     }
70 }
71 
72 // We want to launch the application before running our tests
73 // Thus we want to explicitly call QApplication::exec(), and run our tests in parallel of the event loop
74 // We then reimplement QTEST_MAIN(KStarsUiTests);
75 // The same will have to be done when interacting with a modal dialog: exec() in main thread, tests in timer-based thread
76 
77 QT_BEGIN_NAMESPACE
78 QTEST_ADD_GPU_BLACKLIST_SUPPORT_DEFS
79 QT_END_NAMESPACE
80 
prepare_tests()81 void prepare_tests()
82 {
83     // Configure our test UI
84     QApplication::instance()->setAttribute(Qt::AA_Use96Dpi, true);
85     QTEST_ADD_GPU_BLACKLIST_SUPPORT
86     QTEST_SET_MAIN_SOURCE_PATH
87     QApplication::processEvents();
88 
89     // Prepare our KStars configuration
90     srand((unsigned int)time(nullptr));
91     KSPaths::writableLocation(QStandardPaths::AppDataLocation);
92     KSPaths::writableLocation(QStandardPaths::AppConfigLocation);
93     KSPaths::writableLocation(QStandardPaths::CacheLocation);
94     KCrash::initialize();
95 
96     // Explicitly provide the RC file from the main app resources, not the user-customized one
97     KStars::setResourceFile(":/kxmlgui5/kstars/kstarsui.rc");
98 }
99 
run_wizards(int argc,char * argv[])100 int run_wizards(int argc, char *argv[])
101 {
102     int failure = 0;
103 
104     // This cleans the test user settings, creates our instance and manages the startup wizard
105     if (!failure)
106     {
107         TestKStarsStartup * ti = new TestKStarsStartup();
108         failure |= QTest::qExec(ti);//, argc, argv);
109         delete ti;
110     }
111 
112 #if defined(HAVE_INDI)
113     // If we test with INDI, this takes care of the Ekos startup wizard
114     if (!failure)
115     {
116         TestEkosWizard * ew = new TestEkosWizard();
117         failure |= QTest::qExec(ew);//, argc, argv);
118         delete ew;
119     }
120 #endif
121 
122     Q_UNUSED(argc);
123     Q_UNUSED(argv);
124 
125     return failure;
126 }
127 
execute_tests()128 void execute_tests()
129 {
130     QCoreApplication *app = QApplication::instance();
131 
132     // Limit execution duration
133     QTimer::singleShot(60 * 60 * 1000, app, &QCoreApplication::quit);
134 
135     app->exec();
136 
137     // Clean our instance up if it is still alive
138     if( KStars::Instance() != nullptr)
139     {
140         KStars::Instance()->close();
141         delete KStars::Instance();
142     }
143 }
144 
145 #if !defined(HAVE_INDI)
146 QTEST_KSTARS_MAIN(KStarsUiTests)
147 #endif
148