summaryrefslogtreecommitdiffstats
path: root/tests/main/tst_main.cpp
blob: dcba815e0cbdcbad50df08bdb3aa2c1e1dca7cf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/****************************************************************************
**
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Pelagicore Application Manager.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company.  For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtCore>
#include <QtTest>
#include <QDir>
#include <QString>
#include <QStringList>

#include "applicationinstaller.h"
#include "applicationmanager.h"
#include "logging.h"
#include "main.h"
#include <QtAppManMain/defaultconfiguration.h>


QT_USE_NAMESPACE_AM

class tst_Main : public QObject
{
    Q_OBJECT

public:
    tst_Main();
    ~tst_Main();

private slots:
    void initTestCase();
    void init();
    void cleanup();
    void installAndRemoveUpdateForBuiltIn();
    void updateForBuiltInAlreadyInstalled();
    void loadDatabaseWithUpdatedBuiltInApp();
    void nonExistentMainQmlFile();

private:
    void cleanUpInstallationDir();
    void installPackage(const QString &path);
    void removePackage(const QString &id);
    void initMain();
    void destroyMain();
    void copyRecursively(const QString &sourceDir, const QString &destDir);
    int argc;
    char **argv;
    Main *main{nullptr};
    DefaultConfiguration *config{nullptr};
    bool m_verbose = false;
};

tst_Main::tst_Main()
{
    argc = 3;
    argv = new char*[3];
    argv[0] = new char[255];
    sprintf(argv[0], "tst_update-builtin-app");
    argv[1] = new char[255];
    sprintf(argv[1], "--dbus");
    argv[2] = new char[255];
    sprintf(argv[2], "none");
}

tst_Main::~tst_Main()
{
    for (int i = 0; i < argc; ++i)
        delete []argv[i];
    delete []argv;
}

void tst_Main::initTestCase()
{
    if (!QDir(qL1S(AM_TESTDATA_DIR "/packages")).exists())
        QSKIP("No test packages available in the data/ directory");

    m_verbose = qEnvironmentVariableIsSet("VERBOSE_TEST");
    qInfo() << "Verbose mode is" << (m_verbose ? "on" : "off") << "(changed by (un)setting $VERBOSE_TEST)";
}

void tst_Main::copyRecursively(const QString &sourcePath, const QString &destPath)
{
    QDir sourceDir(sourcePath);
    QDir destDir(destPath);

    QStringList subdirNames = sourceDir.entryList(QDir::NoDotAndDotDot | QDir::AllDirs);
    for (auto subdirName : subdirNames) {
        destDir.mkdir(subdirName);
        copyRecursively(sourceDir.filePath(subdirName), destDir.filePath(subdirName));
    }

    QStringList fileNames = sourceDir.entryList(QDir::Files);
    for (auto fileName : fileNames)
        QFile::copy(sourceDir.filePath(fileName), destDir.filePath(fileName));
}

void tst_Main::cleanUpInstallationDir()
{
    {
        QDir testTmpDir("/tmp/am-test-main");
        if (testTmpDir.exists())
            testTmpDir.removeRecursively();
    }
    QDir tmpDir("/tmp");
    tmpDir.mkdir("am-test-main");
}

void tst_Main::init()
{
    cleanUpInstallationDir();
}

void tst_Main::initMain()
{
    main = new Main(argc, argv);

    QStringList deploymentWarnings;

    QString amConfigPath = QFINDTESTDATA("am-config.yaml");
    auto pathList = QStringList(amConfigPath);

    config = new DefaultConfiguration(pathList, QString());
    config->parse(&deploymentWarnings);

    main->setup(config, deploymentWarnings);
    if (m_verbose)
        Logging::setFilterRules(Logging::filterRules() += qSL("am.installer.debug=true"));

    ApplicationInstaller::instance()->setAllowInstallationOfUnsignedPackages(true);
}

void tst_Main::destroyMain()
{
    if (main) {
        main->shutDown();
        main->exec();
        delete main;
        main = nullptr;
    }
}

void tst_Main::cleanup()
{
    destroyMain();

    delete config;
    config = nullptr;
}

void tst_Main::installPackage(const QString &pkgPath)
{
    auto appInstaller = ApplicationInstaller::instance();

    bool installationFinished = false;

    connect(appInstaller, &ApplicationInstaller::taskRequestingInstallationAcknowledge,
            this, [this, appInstaller](const QString &taskId, const QVariantMap &,
                                       const QVariantMap &, const QVariantMap &) {
            appInstaller->acknowledgePackageInstallation(taskId);
    });

    connect(appInstaller, &ApplicationInstaller::taskFinished,
            this, [this, &installationFinished](const QString &) {
            installationFinished = true;
    });

    appInstaller->startPackageInstallation(qSL("internal-0"), QUrl::fromLocalFile(pkgPath));

    QTRY_COMPARE(installationFinished, true);
}

void tst_Main::removePackage(const QString &id)
{
    auto appInstaller = ApplicationInstaller::instance();

    bool removalFinished = false;

    connect(appInstaller, &ApplicationInstaller::taskFinished,
            this, [this, &removalFinished](const QString &) {
            removalFinished = true;
    });

    appInstaller->removePackage(id, false /* keepDocuments */);

    QTRY_COMPARE(removalFinished, true);
}

/*
  Install an application with the same id of an existing, builtin, one.
  Then remove it.
  At all stages ApplicationManager should contain the same Application
  object. All that changes is the contents of that Application:
  from original, to updated, and then back to original.
 */
void tst_Main::installAndRemoveUpdateForBuiltIn()
{
    initMain();

    auto appMan = ApplicationManager::instance();
    QCOMPARE(appMan->count(), 1);

    auto app = appMan->application(0);
    QCOMPARE(app->name(qSL("en")), qSL("Hello Red"));

    installPackage(qL1S(AM_TESTDATA_DIR "packages/hello-world.red.appkg"));

    QCOMPARE(appMan->count(), 1);
    // it must still be the same Application instance as before the installation
    QCOMPARE(appMan->application(0), app);
    // but with different contents
    QCOMPARE(app->name(qSL("en")), qSL("Hello Updated Red"));

    removePackage(qSL("hello-world.red"));

    // After removal of the updated version all data in Application should be as before the
    // installation took place.
    QCOMPARE(appMan->count(), 1);
    QCOMPARE(appMan->application(0), app);
    QCOMPARE(app->name(qSL("en")), qSL("Hello Red"));
}

/*
   Situation: there's already an update installed for a built-in application and there's no database
   file yet (or a database rebuild has been requested, in which case any existing database file is
   ignored).

   Check that ApplicationManager ends up with only one ApplicationObject as both entries
   (the built-in and the installed one) share the same applicaion id. And check also that
   this ApplicationObject is showing data from the installed update.
 */
void tst_Main::updateForBuiltInAlreadyInstalled()
{
    copyRecursively(QFINDTESTDATA("dir-with-update-already-installed"), "/tmp/am-test-main");

    initMain();

    auto appMan = ApplicationManager::instance();
    QCOMPARE(appMan->count(), 1);

    auto app = appMan->application(0);
    QCOMPARE(app->name(qSL("en")), qSL("Hello Updated Red"));
}

/*
   Install an update for a built-in app and quit Main. A database will be generated.

   Then, on next iteration of Main, Applications will be created from that database.
   Check that ApplicationManager shows only one, updated, built-in app.
 */
void tst_Main::loadDatabaseWithUpdatedBuiltInApp()
{
    QCOMPARE(QFile::exists("/tmp/am-test-main/apps.db"), false);

    initMain();
    installPackage(qL1S(AM_TESTDATA_DIR "packages/hello-world.red.appkg"));
    destroyMain();

    QCOMPARE(QFile::exists("/tmp/am-test-main/apps.db"), true);

    initMain();

    auto appMan = ApplicationManager::instance();
    QCOMPARE(appMan->count(), 1);

    auto app = appMan->application(0);
    QCOMPARE(app->name(qSL("en")), qSL("Hello Updated Red"));
}

/*
   When the "main QML file" parameter contains a relative filepath like "foo/bar.qml",
   Main should treat it as a local file (instead of a remote url) and complain that it
   doesn't exist.
 */
void tst_Main::nonExistentMainQmlFile()
{
    QStringList arguments;
    arguments << "tst_update-builtin-app";
    arguments << "--dbus";
    arguments << "none";
    arguments << "foo/bar.qml";

    QString errorMsg;

    main = new Main(argc, argv);

    config = new DefaultConfiguration;
    config->parseWithArguments(arguments);

    try {
        main->setup(config);
    } catch (const std::exception &e) {
        errorMsg.append(e.what());
    }

    QVERIFY(errorMsg.startsWith("no/invalid main QML file specified:"));
    QVERIFY(errorMsg.contains("bar.qml"));

    delete config;
    config = nullptr;
    delete main;
    main = nullptr;
}

QTEST_APPLESS_MAIN(tst_Main)

#include "tst_main.moc"