1 /*
2  * main.cpp
3  * Copyright (C) 2017  Belledonne Communications, Grenoble, France
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  *  Created on: July 17, 2017
20  *      Author: Ronan Abhamon
21  */
22 
23 #include <QTest>
24 #include <QTimer>
25 
26 #include "../app/AppController.hpp"
27 #include "../utils/Utils.hpp"
28 
29 #include "assistant-view/AssistantViewTest.hpp"
30 #include "main-view/MainViewTest.hpp"
31 #include "self-test/SelfTest.hpp"
32 
33 // =============================================================================
34 
initializeTests()35 static QHash<QString, QObject *> initializeTests () {
36   QHash<QString, QObject *> hash;
37   hash["assistant-view"] = new AssistantViewTest();
38   hash["main-view"] = new MainViewTest();
39   return hash;
40 }
41 
42 // -----------------------------------------------------------------------------
43 
main(int argc,char * argv[])44 int main (int argc, char *argv[]) {
45   int fakeArgc = 1;
46   AppController controller(fakeArgc, argv);
47   App *app = controller.getApp();
48   if (app->isSecondary())
49     qFatal("Unable to run test with secondary app.");
50 
51   int testsRet = 0;
52 
53   const QHash<QString, QObject *> tests = initializeTests();
54 
55   QObject *test = nullptr;
56   if (argc > 1) {
57     if (!strcmp(argv[1], "self-test"))
58       // Execute only self-test.
59       QTimer::singleShot(0, [app, &testsRet] {
60         testsRet = QTest::qExec(new SelfTest(app));
61         QCoreApplication::quit();
62       });
63     else {
64       // Execute only one test.
65       const QString testName = ::Utils::coreStringToAppString(argv[1]);
66       test = tests[testName];
67       if (!test) {
68         qWarning() << QStringLiteral("Unable to run invalid test: `%1`.").arg(testName);
69         return EXIT_FAILURE;
70       }
71 
72       QTimer::singleShot(0, [app, &testsRet, test, argc, argv] {
73         testsRet = QTest::qExec(new SelfTest(app));
74         if (!testsRet)
75           QTest::qExec(test, argc - 1, argv + 1);
76         QCoreApplication::quit();
77       });
78     }
79   } else
80     // Execute all tests.
81     QTimer::singleShot(0, [app, &testsRet, &tests] {
82       testsRet = QTest::qExec(new SelfTest(app));
83       if (!testsRet)
84         for (const auto &test : tests) {
85           testsRet |= QTest::qExec(test);
86         }
87 
88       QCoreApplication::quit();
89     });
90 
91   app->initContentApp();
92   int ret = app->exec();
93 
94   for (auto &test : tests)
95     delete test;
96 
97   if (testsRet)
98     qWarning() << QStringLiteral("One or many tests are failed. :(");
99   else
100     qInfo() << QStringLiteral("Tests seems OK. :)");
101 
102   return testsRet || ret;
103 }
104