aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/squish/squishtools.cpp
blob: f6ef2e67e07541cb1d0fabbb5d1c16a92143c299 (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
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
// Copyright (C) 2022 The Qt Company Ltd
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "squishtools.h"

#include "scripthelper.h"
#include "squishmessages.h"
#include "squishoutputpane.h"
#include "squishsettings.h"
#include "squishtr.h"
#include "squishxmloutputhandler.h"

#include <QDebug> // TODO remove

#include <coreplugin/documentmanager.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icore.h>
#include <debugger/debuggerconstants.h>
#include <debugger/debuggericons.h>
#include <texteditor/textmark.h>

#include <utils/algorithm.h>
#include <utils/qtcassert.h>
#include <utils/temporaryfile.h>
#include <utils/utilsicons.h>

#include <QApplication>
#include <QDateTime>
#include <QFile>
#include <QFileSystemWatcher>
#include <QLoggingCategory>
#include <QTimer>
#include <QWindow>

static Q_LOGGING_CATEGORY(LOG, "qtc.squish.squishtools", QtWarningMsg)

using namespace Utils;

namespace Squish::Internal {

static QString runnerStateName(RunnerState state)
{
    switch (state) {
    case RunnerState::None: return "None";
    case RunnerState::Starting: return "Starting";
    case RunnerState::Running: return "Running";
    case RunnerState::RunRequested: return "RunRequested";
    case RunnerState::Interrupted: return "Interrupted";
    case RunnerState::InterruptRequested: return "InterruptedRequested";
    case RunnerState::CancelRequested: return "CancelRequested";
    case RunnerState::CancelRequestedWhileInterrupted: return "CancelRequestedWhileInterrupted";
    case RunnerState::Canceled: return "Canceled";
    case RunnerState::Finished: return "Finished";
    }
    return "ThouShallNotBeHere";
}

static QString toolsStateName(SquishTools::State state)
{
    switch (state) {
    case SquishTools::Idle: return "Idle";
    case SquishTools::ServerStarting: return "ServerStarting";
    case SquishTools::ServerStarted: return "ServerStarted";
    case SquishTools::ServerStartFailed: return "ServerStartFailed";
    case SquishTools::ServerStopped: return "ServerStopped";
    case SquishTools::ServerStopFailed: return "ServerStopFailed";
    case SquishTools::RunnerStarting: return "RunnerStarting";
    case SquishTools::RunnerStarted: return "RunnerStarted";
    case SquishTools::RunnerStartFailed: return "RunnerStartFailed";
    case SquishTools::RunnerStopped: return "RunnerStopped";
    }
    return "UnexpectedState";
}

class SquishLocationMark : public TextEditor::TextMark
{
public:
    SquishLocationMark(const FilePath &filePath, int line)
        : TextEditor::TextMark(filePath, line, {Tr::tr("Squish"), Id("Squish.LocationMark")})
    {
        setIsLocationMarker(true);
        setIcon(Debugger::Icons::LOCATION.icon());
        setPriority(HighPriority);
    }
};

// make this configurable?
static const Utils::FilePath resultsDirectory = Utils::FileUtils::homePath()
        .pathAppended(".squishQC/Test Results");
static SquishTools *s_instance = nullptr;

SquishTools::SquishTools(QObject *parent)
    : QObject(parent)
    , m_suiteConf(Utils::FilePath{})
{
    SquishOutputPane *outputPane = SquishOutputPane::instance();
    connect(this, &SquishTools::logOutputReceived,
            outputPane, &SquishOutputPane::addLogOutput, Qt::QueuedConnection);
    connect(this, &SquishTools::squishTestRunStarted,
            outputPane, &SquishOutputPane::clearOldResults);
    connect(this, &SquishTools::squishTestRunFinished,
            outputPane, &SquishOutputPane::onTestRunFinished);

    connect(&m_serverProcess, &SquishServerProcess::stateChanged,
            this, &SquishTools::onServerStateChanged);
    connect(&m_serverProcess, &SquishServerProcess::logOutputReceived,
            this, &SquishTools::logOutputReceived);
    connect(&m_serverProcess, &SquishServerProcess::portRetrieved,
            this, &SquishTools::onServerPortRetrieved);

    s_instance = this;
    m_perspective.initPerspective();
    connect(&m_perspective, &SquishPerspective::interruptRequested,
            this, [this] {
        logAndChangeRunnerState(RunnerState::InterruptRequested);
        if (m_primaryRunner && m_primaryRunner->processId() != -1)
            interruptRunner();
    });
    connect(&m_perspective, &SquishPerspective::stopRequested, this, [this] {
        bool interrupted = m_squishRunnerState == RunnerState::Interrupted;
        RunnerState state = interrupted ? RunnerState::CancelRequestedWhileInterrupted
                                        : RunnerState::CancelRequested;
        logAndChangeRunnerState(state);
        if (interrupted)
            handlePrompt();
        else if (m_primaryRunner && m_primaryRunner->processId() != -1)
            terminateRunner();
    });
    connect(&m_perspective, &SquishPerspective::stopRecordRequested,
            this, &SquishTools::stopRecorder);
    connect(&m_perspective, &SquishPerspective::runRequested,
            this, &SquishTools::onRunnerRunRequested);
    connect(&m_perspective, &SquishPerspective::inspectTriggered,
            this, &SquishTools::onInspectTriggered);
}

SquishTools::~SquishTools()
{
    if (m_locationMarker) // happens when QC is closed while Squish is executed
        delete m_locationMarker;
}

SquishTools *SquishTools::instance()
{
    QTC_CHECK(s_instance);
    return s_instance;
}

struct SquishToolsSettings
{
    SquishToolsSettings() {}

    FilePath squishPath;
    FilePath serverPath;
    FilePath runnerPath;
    FilePath processComPath;
    bool isLocalServer = true;
    bool verboseLog = false;
    bool minimizeIDE = true;
    QString serverHost = "localhost";
    int serverPort = 9999;
    FilePath licenseKeyPath;

    // populate members using current settings
    void setup()
    {
        squishPath = settings().squishPath();

        if (!squishPath.isEmpty()) {
            const FilePath squishBin = squishPath.pathAppended("bin").absoluteFilePath();
            serverPath = squishBin.pathAppended("squishserver").withExecutableSuffix();
            runnerPath = squishBin.pathAppended("squishrunner").withExecutableSuffix();
            processComPath = squishBin.pathAppended("processcomm").withExecutableSuffix();
        }

        isLocalServer = settings().local();
        serverHost = settings().serverHost();
        serverPort = settings().serverPort();
        verboseLog = settings().verbose();
        licenseKeyPath = settings().licensePath();
        minimizeIDE = settings().minimizeIDE();
    }
};

// make sure to execute setup() to populate with current settings before using it
static SquishToolsSettings toolsSettings;

void SquishTools::runTestCases(const FilePath &suitePath,
                               const QStringList &testCases)
{
    if (m_shutdownInitiated)
        return;
    if (m_state != Idle) {
        SquishMessages::toolsInUnexpectedState(m_state, Tr::tr("Refusing to run a test case."));
        return;
    }
    // create test results directory (if necessary) and return on fail
    if (!resultsDirectory.ensureWritableDir()) {
        SquishMessages::criticalMessage(
                    Tr::tr("Could not create test results folder. Canceling test run."));
        return;
    }

    m_suitePath = suitePath;
    m_suiteConf = SuiteConf::readSuiteConf(suitePath.pathAppended("suite.conf"));
    m_testCases = testCases;
    m_reportFiles.clear();

    const QString dateTimeString = QDateTime::currentDateTime().toString("yyyy-MM-ddTHH-mm-ss");
    m_currentResultsDirectory = resultsDirectory.pathAppended(dateTimeString);

    m_additionalRunnerArguments.clear();
    m_additionalRunnerArguments << "--interactive" << "--resultdir"
                                << m_currentResultsDirectory.toUserOutput();

    m_xmlOutputHandler.reset(new SquishXmlOutputHandler(this));
    connect(this, &SquishTools::resultOutputCreated,
            m_xmlOutputHandler.get(), &SquishXmlOutputHandler::outputAvailable);
    connect(m_xmlOutputHandler.get(), &SquishXmlOutputHandler::updateStatus,
            &m_perspective, &SquishPerspective::updateStatus);

    m_perspective.setPerspectiveMode(SquishPerspective::Running);
    emit squishTestRunStarted();
    setupRunnerForRun();
    startSquishServer(RunTestRequested);
}

void SquishTools::queryGlobalScripts(QueryCallback callback)
{
    m_queryCallback = callback;
    queryServer(GetGlobalScriptDirs);
}

void SquishTools::queryServerSettings(QueryCallback callback)
{
    m_queryCallback = callback;
    queryServer(ServerInfo);
}

void SquishTools::requestSetSharedFolders(const Utils::FilePaths &sharedFolders)
{
    // when sharedFolders is empty we need to pass an (explicit) empty string
    // otherwise a list of paths, for convenience we quote each path
    m_queryParameter = '"' + Utils::transform(sharedFolders, &FilePath::toUserOutput).join("\",\"") + '"';
    queryServer(SetGlobalScriptDirs);
}


void SquishTools::queryServer(RunnerQuery query)
{
    if (m_shutdownInitiated)
        return;

    if (m_state != Idle) {
        SquishMessages::toolsInUnexpectedState(m_state, Tr::tr("Refusing to execute server query."));
        return;
    }
    m_perspective.setPerspectiveMode(SquishPerspective::Querying);
    m_query = query;
    setupRunnerForQuery();
    startSquishServer(RunnerQueryRequested);
}

void SquishTools::recordTestCase(const FilePath &suitePath, const QString &testCaseName,
                                 const SuiteConf &suiteConf)
{
    if (m_shutdownInitiated)
        return;
    if (m_state != Idle) {
        SquishMessages::toolsInUnexpectedState(m_state, Tr::tr("Refusing to record a test case."));
        return;
    }

    m_suitePath = suitePath;
    m_testCases = {testCaseName};
    m_suiteConf = suiteConf;

    m_additionalRunnerArguments.clear();
    m_perspective.setPerspectiveMode(SquishPerspective::Recording);
    setupRunnerForRun();
    startSquishServer(RecordTestRequested);
}

void SquishTools::writeServerSettingsChanges(const QList<QStringList> &changes)
{
    if (m_shutdownInitiated)
        return;
    if (m_state != Idle) {
        SquishMessages::toolsInUnexpectedState(m_state, Tr::tr("Refusing to write configuration changes."));
        return;
    }
    m_serverConfigChanges = changes;
    m_perspective.setPerspectiveMode(SquishPerspective::Configuring);
    startSquishServer(ServerConfigChangeRequested);
}

void SquishTools::logAndChangeRunnerState(RunnerState to)
{
    qCInfo(LOG) << "Runner state change:" << runnerStateName(m_squishRunnerState) << ">" << runnerStateName(to);
    m_squishRunnerState = to;
}

void SquishTools::logAndChangeToolsState(SquishTools::State to)
{
    qCInfo(LOG) << "State change:" << toolsStateName(m_state) << ">" << toolsStateName(to);
    m_state = to;
}

void SquishTools::onServerStateChanged(SquishProcessState state)
{
    switch (state) {
    case Starting:
        logAndChangeToolsState(SquishTools::ServerStarting);
        break;
    case Started:
        logAndChangeToolsState(SquishTools::ServerStarted);
        break;
    case StartFailed:
        logAndChangeToolsState(SquishTools::ServerStartFailed);
        onServerStartFailed();
        break;
    case Stopped:
        logAndChangeToolsState(SquishTools::ServerStopped);
        onServerStopped();
        break;
    case StopFailed:
        logAndChangeToolsState(SquishTools::ServerStopFailed);
        onServerStopFailed();
        break;
    default:
        // Idle currently unhandled / not needed?
        break;
    }
}

void SquishTools::onServerPortRetrieved()
{
    QTC_ASSERT(m_state == ServerStarted, return);
    if (m_request == RunnerQueryRequested) {
        executeRunnerQuery();
    } else if (m_request == RunTestRequested || m_request == RecordTestRequested) {
        startSquishRunner();
    } else if (m_request == ServerConfigChangeRequested) { // nothing to do here
    } else {
        QTC_ASSERT(false, qDebug() << m_state << m_request);
    }
}

void SquishTools::onServerStopped()
{
    m_state = Idle;
    emit shutdownFinished();
    if (m_request == ServerConfigChangeRequested) {
        if (m_serverProcess.result() == ProcessResult::FinishedWithError) {
            emit configChangesFailed(m_serverProcess.error());
            return;
        }

        m_serverConfigChanges.removeFirst();
        if (!m_serverConfigChanges.isEmpty()) {
            startSquishServer(ServerConfigChangeRequested);
            return;
        }
        emit configChangesWritten();
        m_perspective.setPerspectiveMode(SquishPerspective::NoMode);
    } else if (m_request == ServerStopRequested) {
        m_request = None;
        if (m_perspective.perspectiveMode() == SquishPerspective::Running)
            emit squishTestRunFinished();

        m_perspective.setPerspectiveMode(SquishPerspective::NoMode);
        if (toolsSettings.minimizeIDE)
            restoreQtCreatorWindows();
        m_perspective.destroyControlBar();
    } else if (m_request == KillOldBeforeQueryRunner) {
        startSquishServer(RunnerQueryRequested);
    } else if (m_request == KillOldBeforeRunRunner) {
        startSquishServer(RunTestRequested);
    } else if (m_request == KillOldBeforeRecordRunner) {
        startSquishServer(RecordTestRequested);
    } else {
        QTC_ASSERT(false, qDebug() << m_request);
    }
}

void SquishTools::onServerStartFailed()
{
    m_state = Idle;
    if (m_request == RunTestRequested)
        emit squishTestRunFinished();
    m_perspective.setPerspectiveMode(SquishPerspective::NoMode);
    m_request = None;
    if (toolsSettings.minimizeIDE)
        restoreQtCreatorWindows();
    m_perspective.destroyControlBar();
}

void SquishTools::onServerStopFailed()
{
    m_serverProcess.closeProcess();
    if (toolsSettings.minimizeIDE)
        restoreQtCreatorWindows();
    m_perspective.destroyControlBar();
    m_state = Idle;
}

void SquishTools::onRunnerStateChanged(SquishProcessState state)
{
    switch (state) {
    case Starting:
        logAndChangeToolsState(SquishTools::RunnerStarting);
        break;
    case Started:
        logAndChangeToolsState(SquishTools::RunnerStarted);
        break;
    case StartFailed:
        logAndChangeToolsState(SquishTools::RunnerStartFailed);
        SquishMessages::criticalMessage(Tr::tr("Squish Runner Error"),
                                        Tr::tr("Squish runner failed to start within given timeframe."));
        onRunnerStopped();
        break;
    case Stopped:
        logAndChangeToolsState(SquishTools::RunnerStopped);
        onRunnerStopped();
    default:
        // rest not needed / handled
        break;
    }
}

void SquishTools::onRunnerStopped()
{
    if (m_request == RunnerQueryRequested) {
        m_request = ServerStopRequested;
        qCInfo(LOG) << "Stopping server from RunnerStopped (query)";
        stopSquishServer();
        return;
    } else if (m_request == RecordTestRequested) {
        if (m_secondaryRunner && m_secondaryRunner->isRunning()) {
            stopRecorder();
        } else {
            m_request = ServerStopRequested;
            qCInfo(LOG) << "Stopping server from RunnerStopped (startaut)";
            stopSquishServer();
        }
        return;
    }
    // below only normal run of test case(s)
    exitAndResetSecondaryRunner();

    if (m_testCases.isEmpty() || (m_squishRunnerState == RunnerState::Canceled)) {
        m_request = ServerStopRequested;
        qCInfo(LOG) << "Stopping server from RunnerStopped";
        stopSquishServer();
        if (QTC_GUARD(m_primaryRunner) && m_primaryRunner->lastRunHadLicenseIssues()) {
            SquishMessages::criticalMessage(Tr::tr("Could not get Squish license from server."));
            return;
        }
        QString error;
        SquishXmlOutputHandler::mergeResultFiles(m_reportFiles,
                                                 m_currentResultsDirectory,
                                                 m_suitePath.fileName(),
                                                 &error);
        if (!error.isEmpty())
            SquishMessages::criticalMessage(error);
        logrotateTestResults();
    } else {
        if (QTC_GUARD(m_primaryRunner) && m_primaryRunner->lastRunHadLicenseIssues()) {
            m_request = ServerStopRequested;
            qCInfo(LOG) << "Stopping server from RunnerStopped (multiple testcases, no license)";
            stopSquishServer();
            SquishMessages::criticalMessage(Tr::tr("Could not get Squish license from server."));
            return;
        }
        m_xmlOutputHandler->clearForNextRun();
        m_perspective.setPerspectiveMode(SquishPerspective::Running);
        logAndChangeRunnerState(RunnerState::Starting);
        startSquishRunner();
    }
}

void SquishTools::onRunnerError(SquishRunnerProcess::RunnerError error)
{
    switch (error) {
    case SquishRunnerProcess::InvalidSocket:
        // we've lost connection to the AUT - if Interrupted, try to cancel the runner
        if (m_squishRunnerState == RunnerState::Interrupted) {
            logAndChangeRunnerState(RunnerState::CancelRequestedWhileInterrupted);
            handlePrompt();
        }
        break;
    case SquishRunnerProcess::MappedAutMissing:
        SquishMessages::criticalMessage(
                    Tr::tr("Squish could not find the AUT \"%1\" to start. Make sure it has been "
                           "added as a Mapped AUT in the squishserver settings.\n"
                           "(Tools > Squish > Server Settings...)").arg(m_suiteConf.aut()));
        break;
    }
}

void SquishTools::setIdle()
{
    m_state = Idle;
    m_request = None;
    m_suitePath = {};
    m_testCases.clear();
    m_currentTestCasePath.clear();
    m_reportFiles.clear();
    m_additionalRunnerArguments.clear();
    m_perspective.setPerspectiveMode(SquishPerspective::NoMode);
    m_currentResultsDirectory.clear();
    m_lastTopLevelWindows.clear();
}

void SquishTools::startSquishServer(Request request)
{
    if (m_shutdownInitiated)
        return;
    QTC_ASSERT(m_perspective.perspectiveMode() != SquishPerspective::NoMode, return);
    m_request = request;
    if (m_serverProcess.state() != QProcess::NotRunning) {
        handleSquishServerAlreadyRunning();
        return;
    }

    toolsSettings.setup();

    const FilePath squishServer = Environment::systemEnvironment().searchInPath(
        toolsSettings.serverPath.toString());
    if (!squishServer.isExecutableFile()) {
        const QString detail = Tr::tr("\"%1\" could not be found or is not executable.\nCheck the "
                                      "settings.").arg(toolsSettings.serverPath.toUserOutput());
        SquishMessages::criticalMessage(Tr::tr("Squish Server Error"), detail);
        setIdle();
        return;
    }
    toolsSettings.serverPath = squishServer;

    if (m_request == RunTestRequested || m_request == RecordTestRequested) {
        if (toolsSettings.minimizeIDE)
            minimizeQtCreatorWindows();
        else
            m_lastTopLevelWindows.clear();
        if (m_request == RunTestRequested && QTC_GUARD(m_xmlOutputHandler))
            m_perspective.showControlBar(m_xmlOutputHandler.get());
        else
            m_perspective.showControlBar(nullptr);

        m_perspective.select();
        logAndChangeRunnerState(RunnerState::Starting);
        if (m_request == RecordTestRequested)
            m_perspective.updateStatus(Tr::tr("Recording test case"));
        else
            m_perspective.updateStatus(Tr::tr("Running test case"));
    }

    const QStringList arguments = serverArgumentsFromSettings();
    m_serverProcess.start({toolsSettings.serverPath, arguments}, squishEnvironment());
}

void SquishTools::stopSquishServer()
{
    qCDebug(LOG) << "Stopping server";
    m_serverProcess.stop();
}

void SquishTools::startSquishRunner()
{
    if (!isValidToStartRunner() || !setupRunnerPath())
        return;

    const QStringList args = runnerArgumentsFromSettings();

    if (m_request == RecordTestRequested)
        m_closeRunnerOnEndRecord = true;

    Utils::CommandLine cmdLine = {toolsSettings.runnerPath, args};
    setupAndStartSquishRunnerProcess(cmdLine);
}

void SquishTools::setupAndStartRecorder()
{
    QTC_ASSERT(m_primaryRunner && m_primaryRunner->autId() != 0, return);
    QTC_ASSERT(!m_secondaryRunner, return);

    QStringList args;
    if (!toolsSettings.isLocalServer)
        args << "--host" << toolsSettings.serverHost;
    args << "--port" << QString::number(m_serverProcess.port());
    args << "--debugLog" << "alpw"; // TODO make this configurable?
    args << "--record";
    args << "--suitedir" << m_suitePath.toUserOutput();

    Utils::TemporaryFile tmp("squishsnippetfile-XXXXXX"); // quick and dirty
    tmp.open();
    m_currentRecorderSnippetFile = Utils::FilePath::fromUserInput(tmp.fileName());
    args << "--outfile" << m_currentRecorderSnippetFile.toUserOutput();
    tmp.close();
    args << "--lang" << m_suiteConf.langParameter();
    args << "--useWaitFor" << "--recordStart";
    if (m_suiteConf.objectMapStyle() == "script")
        args << "--useScriptedObjectMap";
    args << "--autid" << QString::number(m_primaryRunner->autId());

    m_secondaryRunner = new SquishRunnerProcess(this);
    m_secondaryRunner->setupProcess(SquishRunnerProcess::Record);
    const CommandLine cmd = {toolsSettings.runnerPath, args};
    connect(m_secondaryRunner, &SquishRunnerProcess::recorderDone,
            this, &SquishTools::onRecorderFinished);
    qCDebug(LOG) << "Recorder starting:" << cmd.toUserOutput();
    if (m_suiteConf.objectMapPath().isReadableFile())
        Core::DocumentManager::expectFileChange(m_suiteConf.objectMapPath());
    m_secondaryRunner->start(cmd, squishEnvironment());
}

void SquishTools::setupAndStartInspector()
{
    QTC_ASSERT(m_primaryRunner && m_primaryRunner->autId() != 0, return);
    QTC_ASSERT(!m_secondaryRunner, return);

    QStringList args;
    if (!toolsSettings.isLocalServer)
        args << "--host" << toolsSettings.serverHost;
    args << "--port" << QString::number(m_serverProcess.port());
    args << "--debugLog" << "alpw"; // TODO make this configurable?
    args << "--inspect";
    args << "--suitedir" << m_suitePath.toUserOutput();
    args << "--autid" << QString::number(m_primaryRunner->autId());

    m_secondaryRunner = new SquishRunnerProcess(this);
    m_secondaryRunner->setupProcess(SquishRunnerProcess::Inspect);
    const CommandLine cmd = {toolsSettings.runnerPath, args};
    connect(m_secondaryRunner, &SquishRunnerProcess::logOutputReceived,
            this, &SquishTools::logOutputReceived);
    connect(m_secondaryRunner, &SquishRunnerProcess::objectPicked,
            this, &SquishTools::objectPicked);
    connect(m_secondaryRunner, &SquishRunnerProcess::updateChildren,
            this, &SquishTools::updateChildren);
    connect(m_secondaryRunner, &SquishRunnerProcess::propertiesFetched,
            this, &SquishTools::propertiesFetched);
    qCDebug(LOG) << "Inspector starting:" << cmd.toUserOutput();
    m_secondaryRunner->start(cmd, squishEnvironment());
}

void SquishTools::exitAndResetSecondaryRunner()
{
    m_perspective.resetAutId();
    if (m_secondaryRunner) {
        m_secondaryRunner->writeCommand(SquishRunnerProcess::Exit);
        m_secondaryRunner->deleteLater();
        m_secondaryRunner = nullptr;
    }
}

void SquishTools::onInspectTriggered()
{
    QTC_ASSERT(m_primaryRunner, return);
    QTC_ASSERT(m_secondaryRunner, return);
    m_secondaryRunner->writeCommand(SquishRunnerProcess::Pick);
}

void SquishTools::stopRecorder()
{
    QTC_ASSERT(m_secondaryRunner && m_secondaryRunner->isRunning(), return);
    if (m_squishRunnerState == RunnerState::Canceled) {
        qCDebug(LOG) << "Stopping recorder (exit)";
        m_secondaryRunner->writeCommand(SquishRunnerProcess::Exit);
    } else {
        qCDebug(LOG) << "Stopping recorder (endrecord)";
        m_secondaryRunner->writeCommand(SquishRunnerProcess::EndRecord);
    }
}

void SquishTools::executeRunnerQuery()
{
    if (!isValidToStartRunner() || !setupRunnerPath())
        return;

    QStringList arguments = { "--port", QString::number(m_serverProcess.port()) };
    Utils::CommandLine cmdLine = {toolsSettings.runnerPath, arguments};
    switch (m_query) {
    case ServerInfo:
        cmdLine.addArg("--info");
        cmdLine.addArg("all");
        break;
    case GetGlobalScriptDirs:
        cmdLine.addArg("--config");
        cmdLine.addArg("getGlobalScriptDirs");
        break;
    case SetGlobalScriptDirs:
        cmdLine.addArg("--config");
        cmdLine.addArg("setGlobalScriptDirs");
        cmdLine.addArgs(m_queryParameter, Utils::CommandLine::Raw);
        break;
    default:
        QTC_ASSERT(false, return);
    }

    QTC_ASSERT(m_primaryRunner, return);
    m_primaryRunner->start(cmdLine, squishEnvironment());
}

Environment SquishTools::squishEnvironment()
{
    Environment environment = Environment::systemEnvironment();
    if (!toolsSettings.licenseKeyPath.isEmpty())
        environment.set("SQUISH_LICENSEKEY_DIR", toolsSettings.licenseKeyPath.nativePath());
    environment.set("SQUISH_PREFIX", toolsSettings.squishPath.nativePath());
    return environment;
}

void SquishTools::handleQueryDone(const QString &stdOut, const QString &error)
{
    qCDebug(LOG) << "Runner finished";
    if (m_queryCallback)
        m_queryCallback(stdOut, error);
    m_queryCallback = {};
    m_queryParameter.clear();
}

void SquishTools::onRunnerFinished()
{
    qCDebug(LOG) << "Runner finished";
    if (!m_shutdownInitiated) {
        if (m_squishRunnerState == RunnerState::CancelRequested
                || m_squishRunnerState == RunnerState::CancelRequestedWhileInterrupted) {
            logAndChangeRunnerState(RunnerState::Canceled);
        } else {
            logAndChangeRunnerState(RunnerState::Finished);
        }
        if (m_request == RunTestRequested)
            m_perspective.updateStatus(Tr::tr("Test run finished."));
        else if (m_request == RecordTestRequested)
            m_perspective.updateStatus(Tr::tr("Test record finished."));
        m_perspective.setPerspectiveMode(SquishPerspective::NoMode);
    }

    if (m_resultsFileWatcher) {
        delete m_resultsFileWatcher;
        m_resultsFileWatcher = nullptr;
    }
    if (m_currentResultsXML) {
        // make sure results are being read if not done yet
        if (m_currentResultsXML->exists() && !m_currentResultsXML->isOpen())
            onResultsDirChanged(m_currentResultsXML->fileName());
        if (m_currentResultsXML->isOpen())
            m_currentResultsXML->close();
        delete m_currentResultsXML;
        m_currentResultsXML = nullptr;
    }
}

void SquishTools::onRecorderFinished()
{
    QTC_ASSERT(m_secondaryRunner, return);
    qCDebug(LOG) << "Recorder finished"; // exit code?
    m_secondaryRunner->deleteLater();
    m_secondaryRunner = nullptr;

    if (m_primaryRunner && m_primaryRunner->isRunning()) {
        if (m_closeRunnerOnEndRecord) {
            //terminateRunner();
            m_primaryRunner->writeCommand(SquishRunnerProcess::Exit); // why doesn't work anymore?
        }
    } else {
        m_request = ServerStopRequested;
        qCInfo(LOG) << "Stop Server from recorder";
        stopSquishServer();
    }

    if (!m_currentRecorderSnippetFile.exists()) {
        qCInfo(LOG) << m_currentRecorderSnippetFile.toUserOutput() << "does not exist";
        return;
    }
    qCInfo(LOG).noquote() << "\nSnippetFile content:\n--------------------\n"
                          << m_currentRecorderSnippetFile.fileContents().value_or(QByteArray())
                          << "--------------------";

    const ScriptHelper helper(m_suiteConf.language());
    const Utils::FilePath testFile = m_currentTestCasePath.pathAppended(
                "test" + m_suiteConf.scriptExtension());
    Core::DocumentManager::expectFileChange(testFile);
    bool result = helper.writeScriptFile(testFile, m_currentRecorderSnippetFile,
                                         m_suiteConf.aut(),
                                         m_suiteConf.arguments());
    qCInfo(LOG) << "Wrote recorded test case" << testFile.toUserOutput() << " " << result;
    m_currentRecorderSnippetFile.removeFile();
    m_currentRecorderSnippetFile.clear();
}

static char firstNonWhitespace(const QByteArray &text)
{
    for (int i = 0, limit = text.size(); i < limit; ++i)
        if (isspace(text.at(i)))
            continue;
        else
            return text.at(i);
    return 0;
}

static int positionAfterLastClosingTag(const QByteArray &text)
{
    QList<QByteArray> possibleEndTags;
    possibleEndTags << "</description>"
                    << "</message>"
                    << "</verification>"
                    << "</result>"
                    << "</test>"
                    << "</prolog>"
                    << "</epilog>"
                    << "</SquishReport>";

    int positionStart = text.lastIndexOf("</");
    if (positionStart == -1)
        return -1;

    int positionEnd = text.indexOf('>', positionStart);
    if (positionEnd == -1)
        return -1;

    QByteArray endTag = text.mid(positionStart, positionEnd + 1 - positionStart);
    if (possibleEndTags.contains(endTag))
        return positionEnd + 1;

    return positionAfterLastClosingTag(text.mid(0, positionStart));
}

void SquishTools::onRunnerOutput()
{
    if (m_request != RunTestRequested) // only handle test runs here, query is handled on done
        return;

    // buffer for already read, but not processed content
    static QByteArray buffer;
    const qint64 currentSize = m_currentResultsXML->size();

    if (currentSize <= m_readResultsCount)
        return;

    QByteArray output = m_currentResultsXML->read(currentSize - m_readResultsCount);
    if (output.isEmpty())
        return;

    if (!buffer.isEmpty())
        output.prepend(buffer);
    // we might read only partial written stuff - so we have to figure out how much we can
    // pass on for further processing and buffer the rest for the next reading
    const int endTag = positionAfterLastClosingTag(output);
    if (endTag < output.size()) {
        buffer = output.mid(endTag);
        output.truncate(endTag);
    } else {
        buffer.clear();
    }

    m_readResultsCount += output.size();

    if (firstNonWhitespace(output) == '<') {
        // output that must be used for the TestResultsPane
        emit resultOutputCreated(output);
    } else {
        const QList<QByteArray> lines = output.split('\n');
        for (const QByteArray &line : lines) {
            const QByteArray trimmed = line.trimmed();
            if (!trimmed.isEmpty())
                emit logOutputReceived("Runner: " + QLatin1String(trimmed));
        }
    }
}

void SquishTools::handlePrompt(const QString &fileName, int line, int column)
{
    if (m_perspective.perspectiveMode() == SquishPerspective::Recording) {
        switch (m_squishRunnerState) {
        case RunnerState::Starting:
            setupAndStartRecorder();
            onRunnerRunRequested(StepMode::Continue);
            break;
        case RunnerState::CancelRequested:
        case RunnerState::CancelRequestedWhileInterrupted:
            logAndChangeRunnerState(RunnerState::Canceled);
            stopRecorder();
            break;
        case RunnerState::Canceled:
            QTC_CHECK(false);
            break;
        default:
            break;
        }
        return;
    }

    QTC_ASSERT(m_primaryRunner, return);
    switch (m_squishRunnerState) {
    case RunnerState::Starting: {
        const Utils::Links setBPs = m_primaryRunner->setBreakpoints(m_suiteConf.scriptExtension());
        if (!setBPs.contains({Utils::FilePath::fromUserInput(fileName), line})) {
            onRunnerRunRequested(StepMode::Continue);
        } else {
            m_perspective.setPerspectiveMode(SquishPerspective::Interrupted);
            logAndChangeRunnerState(RunnerState::Interrupted);
            restoreQtCreatorWindows();
            // request local variables
            m_primaryRunner->writeCommand(SquishRunnerProcess::PrintVariables);
            const FilePath filePath = FilePath::fromUserInput(fileName);
            Core::EditorManager::openEditorAt({filePath, line, column});
            updateLocationMarker(filePath, line);
        }
        break;
    }
    case RunnerState::CancelRequested:
    case RunnerState::CancelRequestedWhileInterrupted:
        logAndChangeRunnerState(RunnerState::Canceled);
        exitAndResetSecondaryRunner();
        m_primaryRunner->writeCommand(SquishRunnerProcess::Exit);
        clearLocationMarker();
        break;
    case RunnerState::Canceled:
        QTC_CHECK(false);
        break;
    default:
        if (line != -1 && column != -1) {
            m_perspective.setPerspectiveMode(SquishPerspective::Interrupted);
            logAndChangeRunnerState(RunnerState::Interrupted);
            restoreQtCreatorWindows();
            // if we're returning from a function we might end up without a file information
            if (fileName.isEmpty()) {
                m_primaryRunner->writeCommand(SquishRunnerProcess::Next);
            } else {
                // request local variables
                m_primaryRunner->writeCommand(SquishRunnerProcess::PrintVariables);
                const FilePath filePath = FilePath::fromUserInput(fileName);
                Core::EditorManager::openEditorAt({filePath, line, column});
                updateLocationMarker(filePath, line);
                // looks like we need to start inspector while being interrupted?
                if (!m_secondaryRunner && m_primaryRunner->autId() !=0)
                    setupAndStartInspector();
            }
        } else { // it's just some output coming from the server
            if (m_squishRunnerState == RunnerState::Interrupted && !m_requestVarsTimer) {
                // FIXME: this should be easier, but when interrupted and AUT is closed
                // runner does not get notified until continued/canceled
                m_requestVarsTimer = new QTimer(this);
                m_requestVarsTimer->setSingleShot(true);
                m_requestVarsTimer->setInterval(1000);
                connect(m_requestVarsTimer, &QTimer::timeout, this, [this] {
                    m_primaryRunner->writeCommand(SquishRunnerProcess::PrintVariables);
                });
                m_requestVarsTimer->start();
            }
        }
    }
}

void SquishTools::requestExpansion(const QString &name)
{
    QTC_ASSERT(m_primaryRunner, return);
    QTC_ASSERT(m_squishRunnerState == RunnerState::Interrupted, return);
    m_primaryRunner->requestExpanded(name);
}

void SquishTools::requestExpansionForObject(const QString &value)
{
    QTC_ASSERT(m_primaryRunner, return);
    if (m_squishRunnerState != RunnerState::Interrupted)
        return;
    QTC_ASSERT(m_secondaryRunner, return);
    m_secondaryRunner->requestListObject(value);
}

void SquishTools::requestPropertiesForObject(const QString &value)
{
    QTC_ASSERT(m_primaryRunner, return);
    if (m_squishRunnerState != RunnerState::Interrupted)
        return;
    QTC_ASSERT(m_secondaryRunner, return);
    m_secondaryRunner->requestListProperties(value);
}

bool SquishTools::shutdown()
{
    QTC_ASSERT(!m_shutdownInitiated, return true);
    m_shutdownInitiated = true;
    if (m_primaryRunner && m_primaryRunner->isRunning())
        terminateRunner();
    if (m_serverProcess.isRunning())
        m_serverProcess.stop();
    return !(m_serverProcess.isRunning() || (m_primaryRunner && m_primaryRunner->isRunning()));
}

void SquishTools::onResultsDirChanged(const QString &filePath)
{
    if (!m_currentResultsXML)
        return; // runner finished before, m_currentResultsXML deleted

    if (m_currentResultsXML->exists()) {
        delete m_resultsFileWatcher;
        m_resultsFileWatcher = nullptr;
        m_readResultsCount = 0;
        if (m_currentResultsXML->open(QFile::ReadOnly)) {
            m_resultsFileWatcher = new QFileSystemWatcher;
            m_resultsFileWatcher->addPath(m_currentResultsXML->fileName());
            connect(m_resultsFileWatcher,
                    &QFileSystemWatcher::fileChanged,
                    this,
                    &SquishTools::onRunnerOutput);
            // squishrunner might have finished already, call once at least
            onRunnerOutput();
        } else {
            // TODO set a flag to process results.xml as soon the complete test run has finished
            qWarning() << "could not open results.xml although it exists" << filePath
                       << m_currentResultsXML->error() << m_currentResultsXML->errorString();
        }
    } else {
        disconnect(m_resultsFileWatcher);
        // results.xml is created as soon some output has been opened - so try again in a second
        QTimer::singleShot(1000, this, [this, filePath] { onResultsDirChanged(filePath); });
    }
}

void SquishTools::logrotateTestResults()
{
    // make this configurable?
    const int maxNumberOfTestResults = 10;
    const Utils::FilePaths existing = resultsDirectory.dirEntries({{}, QDir::Dirs | QDir::NoDotAndDotDot},
                                                                  QDir::Name);

    for (int i = 0, limit = existing.size() - maxNumberOfTestResults; i < limit; ++i) {
        if (!existing.at(i).removeRecursively())
            qWarning() << "could not remove" << existing.at(i).toUserOutput();
    }
}

void SquishTools::minimizeQtCreatorWindows()
{
    const QWindowList topLevelWindows = QApplication::topLevelWindows();
    for (QWindow *window : topLevelWindows) {
        if (window->flags() & Qt::WindowStaysOnTopHint)
            continue;
        if (window->isVisible()) {
            window->showMinimized();
            if (!m_lastTopLevelWindows.contains(window)) {
                m_lastTopLevelWindows.append(window);
                connect(window, &QWindow::destroyed, this, [this, window] {
                    m_lastTopLevelWindows.removeOne(window);
                });
            }
        }
    }
}

void SquishTools::restoreQtCreatorWindows()
{
    for (QWindow *window : std::as_const(m_lastTopLevelWindows)) {
        window->raise();
        window->requestActivate();
        window->showNormal();
    }
}

void SquishTools::updateLocationMarker(const Utils::FilePath &file, int line)
{
    if (QTC_GUARD(!m_locationMarker)) {
        m_locationMarker = new SquishLocationMark(file, line);
    } else {
        m_locationMarker->updateFilePath(file);
        m_locationMarker->move(line);
    }
}

void SquishTools::clearLocationMarker()
{
    delete m_locationMarker;
    m_locationMarker = nullptr;
}

void SquishTools::onRunnerRunRequested(StepMode step)
{
    if (m_requestVarsTimer) {
        delete m_requestVarsTimer;
        m_requestVarsTimer = nullptr;
    }
    logAndChangeRunnerState(RunnerState::RunRequested);

    QTC_ASSERT(m_primaryRunner, return);
    if (step == StepMode::Continue)
        m_primaryRunner->writeCommand(SquishRunnerProcess::Continue);
    else if (step == StepMode::StepIn)
        m_primaryRunner->writeCommand(SquishRunnerProcess::Step);
    else if (step == StepMode::StepOver)
        m_primaryRunner->writeCommand(SquishRunnerProcess::Next);
    else if (step == StepMode::StepOut)
        m_primaryRunner->writeCommand(SquishRunnerProcess::Return);

    clearLocationMarker();
    if (toolsSettings.minimizeIDE)
        minimizeQtCreatorWindows();
    // avoid overriding Recording
    if (m_perspective.perspectiveMode() == SquishPerspective::Interrupted)
        m_perspective.setPerspectiveMode(SquishPerspective::Running);

    logAndChangeRunnerState(RunnerState::Running);
}

void SquishTools::interruptRunner()
{
    qCDebug(LOG) << "Interrupting runner";
    QTC_ASSERT(m_primaryRunner, return);
    qint64 processId = m_primaryRunner->processId();
    const CommandLine cmd(toolsSettings.processComPath, {QString::number(processId), "break"});
    Process process;
    process.setCommand(cmd);
    process.start();
    process.waitForFinished();
}

void SquishTools::terminateRunner()
{
    qCDebug(LOG) << "Terminating runner";
    m_testCases.clear();
    m_currentTestCasePath.clear();
    m_perspective.updateStatus(Tr::tr("User stop initiated."));
    // should we terminate the AUT instead of the runner?!?
    QTC_ASSERT(m_primaryRunner, return);
    qint64 processId = m_primaryRunner->processId();
    const CommandLine cmd(toolsSettings.processComPath, {QString::number(processId), "terminate"});
    Process process;
    process.setCommand(cmd);
    process.start();
    process.waitForFinished();
}

void SquishTools::handleSquishServerAlreadyRunning()
{
    const QString detail = Tr::tr("There is still an old Squish server instance running.\n"
                                  "This will cause problems later on.\n\n"
                                  "If you continue, the old instance will be terminated.\n"
                                  "Do you want to continue?");
    if (SquishMessages::simpleQuestion(Tr::tr("Squish Server Already Running"), detail) != QMessageBox::Yes)
        return;

    switch (m_request) {
    case RunTestRequested:
        m_request = KillOldBeforeRunRunner;
        break;
    case RecordTestRequested:
        m_request = KillOldBeforeRecordRunner;
        break;
    case RunnerQueryRequested:
        m_request = KillOldBeforeQueryRunner;
        break;
    default:
        const QString detail = Tr::tr("Unexpected state or request while starting Squish server. "
                                      "(state: %1, request: %2)").arg(m_state).arg(m_request);
        SquishMessages::criticalMessage(detail);
    }
    stopSquishServer();
}

QStringList SquishTools::serverArgumentsFromSettings() const
{
    QStringList arguments;
    // TODO if isLocalServer is false we should start a squishserver on remote device
    if (toolsSettings.isLocalServer) {
        if (m_request != ServerConfigChangeRequested)
            arguments << "--local"; // for now - although Squish Docs say "don't use it"
    } else {
        arguments << "--port" << QString::number(toolsSettings.serverPort);
    }
    if (toolsSettings.verboseLog)
        arguments << "--verbose";

    if (m_request == ServerConfigChangeRequested && QTC_GUARD(!m_serverConfigChanges.isEmpty())) {
        arguments.append("--config");
        arguments.append(m_serverConfigChanges.first());
    }
    return arguments;
}

QStringList SquishTools::runnerArgumentsFromSettings()
{
    QStringList arguments;
    if (!toolsSettings.isLocalServer)
        arguments << "--host" << toolsSettings.serverHost;
    arguments << "--port" << QString::number(m_serverProcess.port());
    arguments << "--debugLog" << "alpw"; // TODO make this configurable?

    QTC_ASSERT(!m_testCases.isEmpty(), m_testCases.append(""));
    m_currentTestCasePath = m_suitePath / m_testCases.takeFirst();

    if (m_request == RecordTestRequested) {
        arguments << "--startapp"; // --record is triggered separately
    } else if (m_request == RunTestRequested) {
        arguments << "--testcase" << m_currentTestCasePath.toString();
        arguments << "--debug" << "--ide";
    } else {
        QTC_ASSERT(false, qDebug("Request %d", m_request));
    }

    arguments << "--suitedir" << m_suitePath.toUserOutput();

    arguments << m_additionalRunnerArguments;

    if (m_request == RecordTestRequested) {
        arguments << "--aut" << m_suiteConf.aut();
        if (!m_suiteConf.arguments().isEmpty())
            arguments << m_suiteConf.arguments().split(' ');
    }

    if (m_request == RunTestRequested) {
        const Utils::FilePath caseReportFilePath
                = m_currentResultsDirectory
                / m_suitePath.fileName() / m_currentTestCasePath.fileName() / "results.xml";
        m_reportFiles.append(caseReportFilePath);
        arguments << "--reportgen"
                  << QString::fromLatin1("xml2.2,%1").arg(caseReportFilePath.toUserOutput());

        m_currentResultsXML = new QFile(caseReportFilePath.toString());
    }
    return arguments;
}

bool SquishTools::isValidToStartRunner()
{
    if (!m_serverProcess.isRunning()) {
        const QString detail = Tr::tr("Squish server does not seem to be running.\n(state: %1, "
                                      "request: %2)\nTry again.").arg(m_state).arg(m_request);
        SquishMessages::criticalMessage(Tr::tr("No Squish Server"), detail);
        setIdle();
        return false;
    }
    if (m_serverProcess.port() == -1) {
        const QString detail = Tr::tr("Failed to get the server port.\n(state: %1, request: %2)\n"
                                      "Try again.").arg(m_state).arg(m_request);
        SquishMessages::criticalMessage(Tr::tr("No Squish Server Port"), detail);
        // setting state to ServerStartFailed will terminate/kill the current unusable server
        onServerStateChanged(StartFailed);
        return false;
    }

    if (m_primaryRunner && m_primaryRunner->state() != QProcess::NotRunning) {
        const QString detail = Tr::tr("Squish runner seems to be running already.\n(state: %1, "
                                      "request: %2)\nWait until it has finished and try again.")
                .arg(m_state).arg(m_request);
        SquishMessages::criticalMessage(Tr::tr("Squish Runner Running"), detail);
        return false;
    }
    return true;
}

bool SquishTools::setupRunnerPath()
{
    const FilePath squishRunner = Environment::systemEnvironment().searchInPath(
        toolsSettings.runnerPath.toString());
    if (!squishRunner.isExecutableFile()) {
        const QString detail = Tr::tr("\"%1\" could not be found or is not executable.\nCheck the "
                                      "settings.").arg(toolsSettings.runnerPath.toUserOutput());
        SquishMessages::criticalMessage(Tr::tr("Squish Runner Error"), detail);
        onRunnerStateChanged(Stopped);
        return false;
    }
    toolsSettings.runnerPath = squishRunner;
    return true;
}

void SquishTools::setupAndStartSquishRunnerProcess(const Utils::CommandLine &cmdLine)
{
    QTC_ASSERT(m_primaryRunner, return);
    // avoid crashes on fast re-usage of Process
    m_primaryRunner->closeProcess();

    if (m_request == RunTestRequested) {
        connect(m_primaryRunner, &SquishRunnerProcess::autIdRetrieved,
                this, &SquishTools::autIdRetrieved);
        // set up the file system watcher for being able to read the results.xml file
        m_resultsFileWatcher = new QFileSystemWatcher;
        // on 2nd run this directory exists and won't emit changes, so use the current subdirectory
        if (m_currentResultsDirectory.exists())
            m_resultsFileWatcher->addPath(m_currentResultsDirectory.pathAppended(m_suitePath.fileName()).toString());
        else
            m_resultsFileWatcher->addPath(m_currentResultsDirectory.toString());

        connect(m_resultsFileWatcher,
                &QFileSystemWatcher::directoryChanged,
                this,
                &SquishTools::onResultsDirChanged);
    }

    m_primaryRunner->setTestCasePath(m_currentTestCasePath);
    m_primaryRunner->start(cmdLine, squishEnvironment());
}

void SquishTools::setupRunnerForQuery()
{
    if (m_primaryRunner)
        delete m_primaryRunner;

    m_primaryRunner = new SquishRunnerProcess(this);
    m_primaryRunner->setupProcess(SquishRunnerProcess::QueryServer);
    connect(m_primaryRunner, &SquishRunnerProcess::queryDone,
            this, &SquishTools::handleQueryDone);
    connect(m_primaryRunner, &SquishRunnerProcess::stateChanged,
            this, &SquishTools::onRunnerStateChanged);
    connect(m_primaryRunner, &SquishRunnerProcess::logOutputReceived,
            this, &SquishTools::logOutputReceived);
}

void SquishTools::setupRunnerForRun()
{
    if (m_primaryRunner)
        delete m_primaryRunner;

    m_primaryRunner = new SquishRunnerProcess(this);
    m_primaryRunner->setupProcess(m_request == RecordTestRequested ? SquishRunnerProcess::StartAut
                                                                   : SquishRunnerProcess::Run);
    connect(m_primaryRunner, &SquishRunnerProcess::interrupted,
            this, &SquishTools::handlePrompt);
    connect(m_primaryRunner, &SquishRunnerProcess::localsUpdated,
            this, &SquishTools::localsUpdated);
    connect(m_primaryRunner, &SquishRunnerProcess::runnerFinished,
            this, &SquishTools::onRunnerFinished);
    connect(m_primaryRunner, &SquishRunnerProcess::runnerError,
            this, &SquishTools::onRunnerError);
    connect(m_primaryRunner, &SquishRunnerProcess::stateChanged,
            this, &SquishTools::onRunnerStateChanged);
    connect(m_primaryRunner, &SquishRunnerProcess::logOutputReceived,
            this, &SquishTools::logOutputReceived);
}

void setupSquishTools(QObject *guard)
{
    s_instance = new SquishTools;
    s_instance->setParent(guard);
}

} // namespace Squish::Internal