File: wizard.cpp

package info (click to toggle)
syncthingtray 1.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,804 kB
  • sloc: cpp: 31,085; xml: 1,694; java: 570; sh: 81; javascript: 53; makefile: 25
file content (538 lines) | stat: -rw-r--r-- 25,138 bytes parent folder | download
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#include "../settings/wizard.h"
#include "../misc/syncthinglauncher.h"
#include "../settings/settings.h"
#include "../settings/setupdetection.h"

// use meta-data of syncthingtray application here
#include "resources/../../tray/resources/config.h"

#include <qtutilities/misc/compat.h>

#include <QtTest/QtTest>

#include <QApplication>
#include <QCheckBox>
#include <QCommandLinkButton>
#include <QDebug>
#include <QEventLoop>
#include <QLabel>
#include <QLocale>
#include <QMessageBox>
#include <QProgressBar>
#include <QRadioButton>
#include <QRegularExpression>
#include <QTemporaryDir>
#include <QTextBrowser>

using namespace QtGui;

class WizardTests : public QObject {
    Q_OBJECT

private Q_SLOTS:
    void initTestCase();
    void cleanupTestCase();
    void cleanup();
    void testShowingSettings();
    void testConfiguringLauncher();
    void testConfiguringCurrentlyRunningSyncthing();

private:
    bool confirmMessageBox();
    void configureSyncthingArgs(SetupDetection &setupDetection) const;

    QTemporaryDir m_homeDir;
    QEventLoop m_eventLoop;
    QString m_syncthingPath;
    QString m_syncthingPort;
    QString m_syncthingGuiAddress;
    Data::SyncthingLauncher m_launcher;
    Data::SyncthingConnection m_connection;
    QByteArray m_syncthingLog;
};

/*!
 * \brief Ensure prestine environment with English as language.
 * \remarks Relies on setting HOME; likely breaks on Windows.
 */
void WizardTests::initTestCase()
{
    // ensure all text is English as checks rely on it
    QLocale::setDefault(QLocale::English);

    // assume first launch
    auto &settings = Settings::values();
    settings.fakeFirstLaunch = true;

    // use an empty dir as HOME to simulate a prestine setup
    const auto homePath = m_homeDir.path();
    qDebug() << QStringLiteral("HOME dir: ") + homePath;
    qputenv("LIB_SYNCTHING_CONNECTOR_SYNCTHING_CONFIG_DIR", homePath.toLocal8Bit());
    QVERIFY(m_homeDir.isValid());

    // create a config file for Syncthing Tray in the working dir so it'll be picked up instead of the user's config file
    auto testConfigFile = QFile(QStringLiteral(PROJECT_NAME ".ini"));
    QVERIFY(testConfigFile.open(QFile::WriteOnly | QFile::Truncate));
    testConfigFile.close();

    // assert there's no connection setting present initially
    settings.connection.primary.label = QStringLiteral("testconfig");
    QCOMPARE(settings.connection.primary.syncthingUrl, QString());
    QCOMPARE(settings.connection.primary.apiKey, QByteArray());
    QCOMPARE(settings.connection.secondary.size(), 0);

    // read syncthing executable path from env so it must not necassarily in PATH for this test to run
    const auto syncthingPathFromEnv = qgetenv("SYNCTHING_PATH");
    m_syncthingPath = syncthingPathFromEnv.isEmpty() ? QStringLiteral("syncthing") : QString::fromLocal8Bit(syncthingPathFromEnv);

    // read syncthing port from env so it can be customized should the default port already be used otherwise
    // notes: This is passed via "--gui-address=http://127.0.0.1:$port" so the Syncthing test instance will not interfere with the actual Syncthing setup.
    //        The config file will still contain "127.0.0.1:8384" (or a random port if 8384 is already used by another application), though.
    const auto syncthingPortFromEnv = qEnvironmentVariableIntValue("SYNCTHING_PORT");
    m_syncthingPort = !syncthingPortFromEnv ? QStringLiteral("4001") : QString::number(syncthingPortFromEnv);
    m_syncthingGuiAddress = QStringLiteral("http://127.0.0.1:") + m_syncthingPort;

    // gather Syncthing's output
    m_launcher.setEmittingOutput(true);
    connect(&m_launcher, &Data::SyncthingLauncher::outputAvailable, this, [this](const QByteArray &output) { m_syncthingLog += output; });
}

void WizardTests::cleanupTestCase()
{
    auto syncthingConfig = QFile(m_homeDir.path() + QStringLiteral("/config.xml"));
    qDebug() << "Syncthing config: ";
    !syncthingConfig.open(QFile::ReadOnly) ? (qDebug() << "unable to open") : (qDebug().noquote() << syncthingConfig.readAll());

    if (m_launcher.isRunning()) {
        qDebug() << "terminating Syncthing";
        m_launcher.terminate();
    }
    qDebug().noquote() << "Syncthing log during testrun:\n" << m_syncthingLog;
}

/*!
 * \brief Cleanup the global instance (if used by the test).
 */
void WizardTests::cleanup()
{
    if (Wizard::hasInstance()) {
        delete Wizard::instance();
    }
}

/*!
 * \brief Tests showing settings dialog from wizard's welcome page.
 */
void WizardTests::testShowingSettings()
{
    // obtain global wizard instance and do some basic checks
    auto settingsDlgRequested = 0;
    auto *wizardDlg = Wizard::instance();
    auto c = connect(wizardDlg, &Wizard::settingsDialogRequested, [&] { ++settingsDlgRequested; });
    QVERIFY(!wizardDlg->setupDetection().hasConfig());

    // show wizard and request settings though welcome page
    wizardDlg->show();
    auto *welcomePage = qobject_cast<WelcomeWizardPage *>(wizardDlg->currentPage());
    QVERIFY(welcomePage != nullptr);
    auto *settingsButton = welcomePage->findChild<QCommandLinkButton *>(QStringLiteral("showSettingsCommand"));
    QVERIFY(settingsButton != nullptr);
    QCOMPARE(settingsDlgRequested, 0);
    settingsButton->click();
    m_eventLoop.processEvents();
    QCOMPARE(settingsDlgRequested, 1);

    // other tests might trigger settingsDialogRequested() so better disconnect lambda
    disconnect(c);
}

/*!
 * \brief Tests configuring the built-in launcher to launch an external Syncthing binary.
 */
void WizardTests::testConfiguringLauncher()
{
    // mock the autostart path; it is supposed to be preserved
    QVERIFY(qputenv(PROJECT_VARNAME_UPPER "_AUTOSTART_PATH_MOCK", "fake-autostart-path"));

    // pretend libsyncthing / systemd is already enabled
    // note: Should be unset as we're selecting to use an external binary.
    auto &settings = Settings::values();
    settings.launcher.useLibSyncthing = true;
#ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
    settings.systemd.considerForReconnect = true;
    settings.systemd.showButton = true;
#endif

    // init new wizard and do some basic checks
    auto wizardDlg = Wizard();
    QVERIFY(!wizardDlg.setupDetection().hasConfig());

    // show wizard and proceed with guided setup
    wizardDlg.show();
    auto *const welcomePage = qobject_cast<WelcomeWizardPage *>(wizardDlg.currentPage());
    QVERIFY(welcomePage != nullptr);
    auto *const startWizardButton = welcomePage->findChild<QCommandLinkButton *>(QStringLiteral("startWizardCommand"));
    QVERIFY(startWizardButton != nullptr);
    startWizardButton->click();
    auto *const detectionPage = qobject_cast<DetectionWizardPage *>(wizardDlg.currentPage());
    QVERIFY(detectionPage != nullptr);

    // confirm there's no Syncthing config yet and wait for main config page to show
    auto confirmMsgBox = std::function<void(void)>();
    confirmMsgBox = [&] {
        if (qobject_cast<MainConfigWizardPage *>(wizardDlg.currentPage())) {
            m_eventLoop.quit();
        } else {
            confirmMessageBox();
            QTimer::singleShot(0, this, confirmMsgBox);
        }
    };
    QTimer::singleShot(0, this, confirmMsgBox);
    m_eventLoop.exec();

    // configure external launcher
    auto *const mainConfigPage = qobject_cast<MainConfigWizardPage *>(wizardDlg.currentPage());
    QVERIFY(mainConfigPage != nullptr);
    auto &setupDetection = wizardDlg.setupDetection();
    const auto &configuredAutostartPath = setupDetection.autostartConfiguredPath;
    QVERIFY(configuredAutostartPath.has_value());
    QCOMPARE(configuredAutostartPath.value(), QStringLiteral("fake-autostart-path"));
    QVERIFY(!setupDetection.hasConfig());
    // -> print debug output in certain launcher error cases to get the full picture if any of the subsequent checks fail
    if (setupDetection.launcherError.has_value()) {
        qDebug() << "a launcher error occurred: " << setupDetection.launcherError.value();
    }
    if (!setupDetection.launcherError.has_value() || setupDetection.launcherExitCode.value_or(-1) != 0) {
        qDebug() << "launcher output: " << setupDetection.launcherOutput;
    }
    if (setupDetection.launcherExitCode.has_value()) {
        qDebug() << "launcher exit code: " << setupDetection.launcherExitCode.value();
    }
#ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
    qDebug() << "user service state: " << setupDetection.userService.unitFileState();
    qDebug() << "system service state: " << setupDetection.systemService.unitFileState();
#endif
    if (setupDetection.timedOut) {
        qDebug() << "timeout of " << setupDetection.timeout.interval() << " ms has been exceeded (normal if systemd units not available)";
    }
    // -> verify whether the launcher setup detection is in the expected state before checking UI itself
    QVERIFY(setupDetection.launcherExitCode.has_value());
    QCOMPARE(setupDetection.launcherExitCode.value(), 0);
    QVERIFY(!setupDetection.launcherError.has_value());
    // -> check UI, select the external launcher and continue
    auto *const cfgCurrentlyRunningRadioButton = mainConfigPage->findChild<QRadioButton *>(QStringLiteral("cfgCurrentlyRunningRadioButton"));
    QVERIFY(cfgCurrentlyRunningRadioButton != nullptr);
    QVERIFY(cfgCurrentlyRunningRadioButton->isHidden());
    auto *const cfgLauncherExternalRadioButton = mainConfigPage->findChild<QRadioButton *>(QStringLiteral("cfgLauncherExternalRadioButton"));
    QVERIFY(cfgLauncherExternalRadioButton != nullptr);
    QVERIFY(!cfgLauncherExternalRadioButton->isHidden());
    cfgLauncherExternalRadioButton->setChecked(true);
    wizardDlg.next();

    // keep autostart setting as-is
    auto *const autostartPage = qobject_cast<AutostartWizardPage *>(wizardDlg.currentPage());
    QVERIFY(autostartPage != nullptr);
    auto *const keepExistingCheckBox = autostartPage->findChild<QCheckBox *>(QStringLiteral("keepExistingCheckBox"));
    QVERIFY(keepExistingCheckBox != nullptr);
    QVERIFY(keepExistingCheckBox->isVisible());
    keepExistingCheckBox->setChecked(true);
    wizardDlg.next();

    // apply settings
    auto *const applyPage = qobject_cast<ApplyWizardPage *>(wizardDlg.currentPage());
    QVERIFY(applyPage != nullptr);
    auto *const summaryTextBrowser = applyPage->findChild<QTextBrowser *>(QStringLiteral("summaryTextBrowser"));
    QVERIFY(summaryTextBrowser != nullptr);
    const auto summary = summaryTextBrowser->toPlainText();
    QVERIFY(summary.contains(QStringLiteral("Start Syncthing via Syncthing Tray's launcher")));
    QVERIFY(summary.contains(QStringLiteral("executable from PATH as separate process")));
    QVERIFY(summary.contains(QStringLiteral("Keep autostart disabled")) || summary.contains(QStringLiteral("Preserve existing autostart entry")));
    wizardDlg.next();

    // check results
    auto *finalPage = qobject_cast<FinalWizardPage *>(wizardDlg.currentPage());
    QVERIFY(finalPage != nullptr);
    auto *progressBar = finalPage->findChild<QProgressBar *>(QStringLiteral("progressBar"));
    auto *label = finalPage->findChild<QLabel *>(QStringLiteral("label"));
    QVERIFY(progressBar != nullptr);
    QVERIFY(label != nullptr);

    // wait until configuration has been applied
    auto waitForCfg = std::function<void(void)>();
    waitForCfg = [&] {
        if (!label->isHidden() && progressBar->isHidden()) {
            m_eventLoop.quit();
        } else {
            QTimer::singleShot(0, this, waitForCfg);
        }
    };
    QTimer::singleShot(0, this, waitForCfg);
    m_eventLoop.exec();
    QVERIFY(label->text().contains(QStringLiteral("The internal launcher has not been initialized.")));
    QVERIFY(label->text().contains(QStringLiteral("You may try to head back")));
    configureSyncthingArgs(wizardDlg.setupDetection());

    // try again with launcher being initialized
    wizardDlg.back();
    m_eventLoop.processEvents();
    Data::SyncthingLauncher::setMainInstance(&m_launcher);
    wizardDlg.next();

    // apply changes and wait for results on final wizard page
    qDebug() << "waiting for Syncthing to write config file";
    finalPage = nullptr;
    progressBar = nullptr;
    label = nullptr;
    waitForCfg = [&] {
        if (!finalPage && (finalPage = qobject_cast<FinalWizardPage *>(wizardDlg.currentPage()))) {
            progressBar = finalPage->findChild<QProgressBar *>(QStringLiteral("progressBar"));
            label = finalPage->findChild<QLabel *>(QStringLiteral("label"));
            QVERIFY(progressBar != nullptr);
            QVERIFY(label != nullptr);
        }
        if (label && progressBar && !label->isHidden() && progressBar->isHidden()) {
            m_eventLoop.quit();
        } else {
            QTimer::singleShot(0, this, waitForCfg);
        }
    };
    // -> assign handler to connect to Syncthing with applied settings
    connect(&wizardDlg, &Wizard::settingsChanged, this, [&] {
        m_connection.applySettings(settings.connection.primary);
        m_connection.setSyncthingUrl(QStringLiteral("http://127.0.0.1:") + m_syncthingPort);
        m_connection.reconnect();
        qDebug() << "configured URL: " << m_connection.syncthingUrl();
        qDebug() << "configured API key: " << m_connection.apiKey();
        QTimer::singleShot(0, this, waitForCfg);
    });
    // -> assign handler to re-try connecting to Syncthing or handle a successful connection
    connect(&m_connection, &Data::SyncthingConnection::statusChanged, &wizardDlg,
        [this, &wizardDlg, connectAttempts = 10](Data::SyncthingStatus status) mutable {
            switch (status) {
            case Data::SyncthingStatus::Disconnected:
                // try to connect up to 10 times while Syncthing is still running; otherwise consider test failed
                if (--connectAttempts > 0 && m_launcher.isRunning()) {
                    qDebug() << "attempting to connect to Syncthing again ( remaining attempts:" << connectAttempts << ')';
                    m_connection.reconnectLater(1000);
                } else {
                    qWarning() << "giving up connecting to Syncthing";
                    qDebug() << (m_launcher.isRunning() ? "Syncthing is still running" : "Syncthing is not running (anymore)");
                    wizardDlg.handleConfigurationApplied(QStringLiteral("Unable to connect to Syncthing"), &m_connection);
                }
                return;
            case Data::SyncthingStatus::Reconnecting:
                return;
            case Data::SyncthingStatus::Idle:
            case Data::SyncthingStatus::Scanning:
            case Data::SyncthingStatus::Synchronizing:
            case Data::SyncthingStatus::RemoteNotInSync:
            case Data::SyncthingStatus::NoRemoteConnected:
            case Data::SyncthingStatus::Paused:
                qDebug() << "connected to Syncthing: " << m_connection.statusText();
            default:;
            }
            // consider connection to Syncthing successful
            wizardDlg.handleConfigurationApplied(QString(), &m_connection);
        });
    // -> invoke the whole procedure
    wizardDlg.next();
    m_eventLoop.exec();
    QVERIFY(finalPage != nullptr);
    QVERIFY(label->text().contains(QStringLiteral("changed successfully")));
    QVERIFY(label->text().contains(QStringLiteral("open Syncthing")));

    // verify settings
    QVERIFY(settings.launcher.autostartEnabled);
    QVERIFY(settings.launcher.considerForReconnect);
    QVERIFY(settings.launcher.showButton);
    QVERIFY(!settings.launcher.syncthingPath.isEmpty());
    QVERIFY(!settings.launcher.syncthingArgs.isEmpty());
    QVERIFY(!settings.launcher.useLibSyncthing);
#ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
    QVERIFY(!settings.systemd.considerForReconnect);
    QVERIFY(!settings.systemd.showButton);
#endif
    // cannot verify the port from Syncthing's config, see comment in setup function
    //QCOMPARE(settings.connection.primary.syncthingUrl, QStringLiteral("http://127.0.0.1:8384"));
    QVERIFY(!settings.connection.primary.syncthingUrl.isEmpty());
    QVERIFY(!settings.connection.primary.apiKey.isEmpty());
    QCOMPARE(settings.connection.secondary.size(), 0);
    QCOMPARE(qEnvironmentVariable(PROJECT_VARNAME_UPPER "_AUTOSTART_PATH_MOCK"), QStringLiteral("fake-autostart-path"));
}

/*!
 * \brief Test configuring for the currently running Syncthing instance.
 * \remarks The running instance should have been started by WizardTests::testConfiguringLauncher().
 */
void WizardTests::testConfiguringCurrentlyRunningSyncthing()
{
    // mock the autostart path; it is supposed to be changed
    QVERIFY(qputenv(PROJECT_VARNAME_UPPER "_AUTOSTART_PATH_MOCK", "fake-autostart-path"));

    // change port in config file
    auto wizardDlg = Wizard();
    auto &setupDetection = wizardDlg.setupDetection();
    setupDetection.determinePaths();
    auto configFile = QFile(setupDetection.configFilePath);
    QVERIFY(configFile.open(QFile::ReadOnly));
    auto config = QString::fromUtf8(configFile.readAll());
    configFile.close();
    config.replace(QRegularExpression("<address>127.0.0.1:.*</address>"),
        QStringLiteral("<address>127.0.0.1:") % m_syncthingPort % QStringLiteral("</address>"));
    const auto configData = config.toUtf8();
    QVERIFY(configFile.open(QFile::WriteOnly | QFile::Truncate));
    QCOMPARE(configFile.write(configData), configData.size());
    configFile.close();

    // show wizard and proceed with guided setup
    wizardDlg.show();
    auto *const welcomePage = qobject_cast<WelcomeWizardPage *>(wizardDlg.currentPage());
    QVERIFY(welcomePage != nullptr);
    auto *const startWizardButton = welcomePage->findChild<QCommandLinkButton *>(QStringLiteral("startWizardCommand"));
    QVERIFY(startWizardButton != nullptr);
    startWizardButton->click();
    auto *const detectionPage = qobject_cast<DetectionWizardPage *>(wizardDlg.currentPage());
    QVERIFY(detectionPage != nullptr);

    // wait for main config page to show
    auto awaitDetection = std::function<void(void)>();
    awaitDetection = [&] {
        if (qobject_cast<MainConfigWizardPage *>(wizardDlg.currentPage())) {
            m_eventLoop.quit();
        } else {
            QVERIFY(!confirmMessageBox()); // message box should NOT show up
            QTimer::singleShot(0, this, awaitDetection);
        }
    };
    QTimer::singleShot(0, this, awaitDetection);
    m_eventLoop.exec();

    // configure external launcher
    const auto &url = setupDetection.connection.syncthingUrl();
    const auto &apiKey = setupDetection.connection.apiKey();
    qDebug() << "configured URL: " << url;
    qDebug() << "configured API key: " << apiKey;
    QVERIFY(!url.isEmpty());
    QVERIFY(!apiKey.isEmpty());
    auto *const mainConfigPage = qobject_cast<MainConfigWizardPage *>(wizardDlg.currentPage());
    QVERIFY(mainConfigPage != nullptr);
    QVERIFY(setupDetection.hasConfig());
    auto *const invalidConfigLabel = mainConfigPage->findChild<QLabel *>(QStringLiteral("invalidConfigLabel"));
    QVERIFY(invalidConfigLabel != nullptr);
    if (!invalidConfigLabel->isHidden()) {
        qDebug() << "invalid config label shown: " << invalidConfigLabel->text();
    }
    QVERIFY(invalidConfigLabel->isHidden());
    auto *const cfgLauncherExternalRadioButton = mainConfigPage->findChild<QRadioButton *>(QStringLiteral("cfgLauncherExternalRadioButton"));
    QVERIFY(cfgLauncherExternalRadioButton != nullptr);
    QVERIFY(cfgLauncherExternalRadioButton->isHidden());
    auto *const cfgLauncherBuiltInRadioButton = mainConfigPage->findChild<QRadioButton *>(QStringLiteral("cfgLauncherBuiltInRadioButton"));
    QVERIFY(cfgLauncherBuiltInRadioButton != nullptr);
    QVERIFY(cfgLauncherBuiltInRadioButton->isHidden());
    auto *const cfgSystemdUserUnitRadioButton = mainConfigPage->findChild<QRadioButton *>(QStringLiteral("cfgSystemdUserUnitRadioButton"));
    QVERIFY(cfgSystemdUserUnitRadioButton != nullptr);
    QVERIFY(cfgSystemdUserUnitRadioButton->isHidden());
    auto *const cfgSystemdSystemUnitRadioButton = mainConfigPage->findChild<QRadioButton *>(QStringLiteral("cfgSystemdSystemUnitRadioButton"));
    QVERIFY(cfgSystemdSystemUnitRadioButton != nullptr);
    QVERIFY(cfgSystemdSystemUnitRadioButton->isHidden());
    auto *const cfgCurrentlyRunningRadioButton = mainConfigPage->findChild<QRadioButton *>(QStringLiteral("cfgCurrentlyRunningRadioButton"));
    QVERIFY(cfgCurrentlyRunningRadioButton != nullptr);
    QVERIFY(!cfgCurrentlyRunningRadioButton->isHidden());
    QVERIFY(cfgCurrentlyRunningRadioButton->isChecked());
    auto *const cfgNoneRadioButton = mainConfigPage->findChild<QRadioButton *>(QStringLiteral("cfgNoneRadioButton"));
    QVERIFY(cfgNoneRadioButton != nullptr);
    QVERIFY(!cfgNoneRadioButton->isHidden());
    QVERIFY(!cfgNoneRadioButton->isChecked());
    wizardDlg.next();

    // override existing autostart setting
    auto *const autostartPage = qobject_cast<AutostartWizardPage *>(wizardDlg.currentPage());
    QVERIFY(autostartPage != nullptr);
    auto *const keepExistingCheckBox = autostartPage->findChild<QCheckBox *>(QStringLiteral("keepExistingCheckBox"));
    QVERIFY(keepExistingCheckBox != nullptr);
    QVERIFY(keepExistingCheckBox->isVisible());
    keepExistingCheckBox->setChecked(false);
    wizardDlg.next();
    configureSyncthingArgs(setupDetection);

    // apply settings
    auto *const applyPage = qobject_cast<ApplyWizardPage *>(wizardDlg.currentPage());
    QVERIFY(applyPage != nullptr);
    auto *const summaryTextBrowser = applyPage->findChild<QTextBrowser *>(QStringLiteral("summaryTextBrowser"));
    QVERIFY(summaryTextBrowser != nullptr);
    const auto summary = summaryTextBrowser->toPlainText();
    QVERIFY(summary.contains(QStringLiteral("Configure Syncthing Tray to use the currently running Syncthing instance")));
    QVERIFY(summary.contains(QStringLiteral("Do not change how Syncthing is launched")));
    QVERIFY(summary.contains(QStringLiteral("Keep autostart")));

    // apply changes and wait for results on final wizard page
    auto &settings = Settings::values();
    settings.connection.primary.syncthingUrl = QStringLiteral("foobar"); // ensure this value will change so we always get a backup
    auto waitForCfg = std::function<void(void)>();
    FinalWizardPage *finalPage = nullptr;
    QProgressBar *progressBar = nullptr;
    QLabel *label = nullptr;
    waitForCfg = [&] {
        if (!finalPage && (finalPage = qobject_cast<FinalWizardPage *>(wizardDlg.currentPage()))) {
            progressBar = finalPage->findChild<QProgressBar *>(QStringLiteral("progressBar"));
            label = finalPage->findChild<QLabel *>(QStringLiteral("label"));
            QVERIFY(progressBar != nullptr);
            QVERIFY(label != nullptr);
        }
        if (label && progressBar && !label->isHidden() && progressBar->isHidden()) {
            m_eventLoop.quit();
        } else {
            QTimer::singleShot(0, this, waitForCfg);
        }
    };
    connect(&wizardDlg, &Wizard::settingsChanged, this, [&] {
        wizardDlg.handleConfigurationApplied();
        QTimer::singleShot(0, this, waitForCfg);
    });
    wizardDlg.next();
    m_eventLoop.exec();
    QVERIFY(finalPage != nullptr);
    QVERIFY(label->text().contains(QStringLiteral("changed successfully")));
    QVERIFY(label->text().contains(QStringLiteral("open Syncthing")));

    // verify settings
    QVERIFY(settings.launcher.autostartEnabled);
    QVERIFY(settings.launcher.considerForReconnect);
    QVERIFY(settings.launcher.showButton);
    QVERIFY(!settings.launcher.syncthingPath.isEmpty());
    QVERIFY(!settings.launcher.syncthingArgs.isEmpty());
    QVERIFY(!settings.launcher.useLibSyncthing);
    QCOMPARE(settings.connection.primary.syncthingUrl, m_syncthingGuiAddress);
    QVERIFY(!settings.connection.primary.apiKey.isEmpty());
    QCOMPARE(settings.connection.secondary.size(), 1);
    QCOMPARE(settings.connection.secondary[0].label, QStringLiteral("Backup of testconfig (created by wizard)"));
    QCOMPARE(qEnvironmentVariable(PROJECT_VARNAME_UPPER "_AUTOSTART_PATH_MOCK"), setupDetection.autostartSupposedPath);
}

bool WizardTests::confirmMessageBox()
{
    const auto allToplevelWidgets = QApplication::topLevelWidgets();
    for (auto *const w : allToplevelWidgets) {
        if (auto *const mb = qobject_cast<QMessageBox *>(w)) {
            QTest::keyClick(mb, Qt::Key_Enter);
            return true;
        }
    }
    return false;
}

/*!
 * \brief Overrides launcher settings so tests can use a custom Syncthing binary and a custom port.
 */
void WizardTests::configureSyncthingArgs(SetupDetection &setupDetection) const
{
    setupDetection.launcherSettings.syncthingPath = m_syncthingPath;
    setupDetection.defaultSyncthingArgs.append(QStringLiteral(" --gui-address="));
    setupDetection.defaultSyncthingArgs.append(m_syncthingGuiAddress);
    setupDetection.defaultSyncthingArgs.append(QStringLiteral(" --home='"));
    setupDetection.defaultSyncthingArgs.append(m_homeDir.path());
    setupDetection.defaultSyncthingArgs.append(QChar('\''));
}

QTEST_MAIN(WizardTests)
#include "wizard.moc"