aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/squish/squishfilehandler.cpp
blob: ab172967c4891a9963713f9d02f7ffc4f1c780ac (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
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator Squish plugin.
**
** Commercial License Usage
** Licensees holding valid commercial Qt 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.
**
****************************************************************************/

#include "squishfilehandler.h"

#include "opensquishsuitesdialog.h"
#include "squishconstants.h"
#include "squishtesttreemodel.h"
#include "squishtools.h"
#include "squishutils.h"
#include "squishtr.h"

#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icore.h>
#include <utils/qtcassert.h>

#include <QDir>
#include <QFileDialog>
#include <QFileInfo>
#include <QMessageBox>

namespace Squish {
namespace Internal {

static SquishFileHandler *m_instance = nullptr;

SquishFileHandler::SquishFileHandler(QObject *parent)
    : QObject(parent)
{
    m_instance = this;
}

SquishFileHandler *SquishFileHandler::instance()
{
    if (!m_instance)
        m_instance = new SquishFileHandler;

    return m_instance;
}

SquishTestTreeItem *createTestTreeItem(const QString &name,
                                       const QString &filePath,
                                       const QStringList &cases)
{
    SquishTestTreeItem *item = new SquishTestTreeItem(name, SquishTestTreeItem::SquishSuite);
    item->setFilePath(filePath);
    for (const QString &testCase : cases) {
        SquishTestTreeItem *child = new SquishTestTreeItem(QFileInfo(testCase).dir().dirName(),
                                                           SquishTestTreeItem::SquishTestCase);
        child->setFilePath(testCase);
        item->appendChild(child);
    }
    return item;
}

void SquishFileHandler::modifySuiteItem(const QString &suiteName,
                                        const QString &filePath,
                                        const QStringList &cases)
{
    SquishTestTreeItem *item = createTestTreeItem(suiteName, filePath, cases);
    // TODO update file watcher
    m_suites.insert(suiteName, filePath);
    emit suiteTreeItemModified(item, suiteName);
}

void SquishFileHandler::openTestSuites()
{
    OpenSquishSuitesDialog dialog;
    dialog.exec();
    QMessageBox::StandardButton replaceSuite = QMessageBox::NoButton;
    const QStringList chosenSuites = dialog.chosenSuites();
    for (const QString &suite : chosenSuites) {
        const QDir suiteDir(suite);
        const QString suiteName = suiteDir.dirName();
        const QStringList cases = SquishUtils::validTestCases(suite);
        const QFileInfo suiteConf(suiteDir, "suite.conf");
        if (m_suites.contains(suiteName)) {
            if (replaceSuite == QMessageBox::YesToAll) {
                modifySuiteItem(suiteName, suiteConf.absoluteFilePath(), cases);
            } else if (replaceSuite != QMessageBox::NoToAll) {
                replaceSuite
                    = QMessageBox::question(Core::ICore::dialogParent(),
                                            Tr::tr("Suite Already Open"),
                                            Tr::tr("A test suite with the name \"%1\" is already open."
                                                   "\nClose the opened test suite and replace it "
                                                   "with the new one?")
                                                .arg(suiteName),
                                            QMessageBox::Yes | QMessageBox::YesToAll
                                                | QMessageBox::No | QMessageBox::NoToAll,
                                            QMessageBox::No);
                if (replaceSuite == QMessageBox::YesToAll || replaceSuite == QMessageBox::Yes)
                    modifySuiteItem(suiteName, suiteConf.absoluteFilePath(), cases);
            }
        } else {
            SquishTestTreeItem *item = createTestTreeItem(suiteName,
                                                          suiteConf.absoluteFilePath(),
                                                          cases);
            // TODO add file watcher
            m_suites.insert(suiteName, suiteConf.absoluteFilePath());
            emit testTreeItemCreated(item);
        }
    }
    emit suitesOpened();
}

void SquishFileHandler::closeTestSuite(const QString &suiteName)
{
    if (!m_suites.contains(suiteName))
        return;

    // TODO close respective editors if there are any
    // TODO remove file watcher
    m_suites.remove(suiteName);
    emit suiteTreeItemRemoved(suiteName);
}

void SquishFileHandler::closeAllTestSuites()
{
    // TODO close respective editors if there are any
    // TODO remove file watcher
    const QStringList &suiteNames = m_suites.keys();
    m_suites.clear();
    for (const QString &suiteName : suiteNames)
        emit suiteTreeItemRemoved(suiteName);
}

void SquishFileHandler::runTestCase(const QString &suiteName, const QString &testCaseName)
{
    QTC_ASSERT(!suiteName.isEmpty() && !testCaseName.isEmpty(), return );

    if (SquishTools::instance()->state() != SquishTools::Idle)
        return;

    const QDir suitePath = QFileInfo(m_suites.value(suiteName)).absoluteDir();
    if (!suitePath.exists() || !suitePath.isReadable()) {
        QMessageBox::critical(Core::ICore::dialogParent(),
                              Tr::tr("Test Suite Path Not Accessible"),
                              Tr::tr("The path \"%1\" does not exist or is not accessible.\n"
                                     "Refusing to run test case \"%2\".")
                                  .arg(QDir::toNativeSeparators(suitePath.absolutePath()))
                                  .arg(testCaseName));
        return;
    }

    SquishTools::instance()->runTestCases(suitePath.absolutePath(), QStringList(testCaseName));
}

void SquishFileHandler::runTestSuite(const QString &suiteName)
{
    QTC_ASSERT(!suiteName.isEmpty(), return );

    if (SquishTools::instance()->state() != SquishTools::Idle)
        return;

    const QString suiteConf = m_suites.value(suiteName);
    const QDir suitePath = QFileInfo(suiteConf).absoluteDir();
    if (!suitePath.exists() || !suitePath.isReadable()) {
        QMessageBox::critical(Core::ICore::dialogParent(),
                              Tr::tr("Test Suite Path Not Accessible"),
                              Tr::tr("The path \"%1\" does not exist or is not accessible.\n"
                                     "Refusing to run test cases.")
                                  .arg(QDir::toNativeSeparators(suitePath.absolutePath())));
        return;
    }

    QStringList testCases = SquishTestTreeModel::instance()->getSelectedSquishTestCases(suiteConf);
    SquishTools::instance()->runTestCases(suitePath.absolutePath(), testCases);
}

void addAllEntriesRecursively(SquishTestTreeItem *item)
{
    QDir folder(item->filePath());

    const QFileInfoList entries = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);
    for (const QFileInfo &info : entries) {
        const QString &path = info.absoluteFilePath();
        // TODO improve this later? Squish refuses directories containing Squish test suites
        const bool isDir = info.isDir();
        if (!info.isFile() && !isDir)
            continue;

        SquishTestTreeItem *child
            = new SquishTestTreeItem(info.fileName(),
                                     isDir ? SquishTestTreeItem::SquishSharedFolder
                                           : SquishTestTreeItem::SquishSharedFile);
        child->setFilePath(path);

        if (info.isDir())
            addAllEntriesRecursively(child);

        item->appendChild(child);
    }
}

void SquishFileHandler::addSharedFolder()
{
    const QString &chosen = QFileDialog::getExistingDirectory(Core::ICore::dialogParent(),
                                                              Tr::tr("Select Global Script Folder"));
    if (chosen.isEmpty())
        return;

    if (m_sharedFolders.contains(chosen))
        return;

    m_sharedFolders.append(chosen);
    SquishTestTreeItem *item = new SquishTestTreeItem(chosen,
                                                      SquishTestTreeItem::SquishSharedFolder);
    item->setFilePath(chosen);
    addAllEntriesRecursively(item);
    emit testTreeItemCreated(item);
}

bool SquishFileHandler::removeSharedFolder(const QString &folder)
{
    if (m_sharedFolders.contains(folder))
        return m_sharedFolders.removeOne(folder);

    return false;
}

void SquishFileHandler::removeAllSharedFolders()
{
    m_sharedFolders.clear();
}

void SquishFileHandler::openObjectsMap(const QString &suiteName)
{
    QTC_ASSERT(!suiteName.isEmpty(), return );

    const Utils::FilePath objectsMapPath = Utils::FilePath::fromString(
        SquishUtils::objectsMapPath(m_suites.value(suiteName)));
    if (!objectsMapPath.isEmpty() && objectsMapPath.exists()) {
        if (!Core::EditorManager::openEditor(objectsMapPath, Constants::OBJECTSMAP_EDITOR_ID)) {
            QMessageBox::critical(Core::ICore::dialogParent(),
                                  Tr::tr("Error"),
                                  Tr::tr("Failed to open objects.map file at \"%1\".")
                                      .arg(objectsMapPath.toUserOutput()));
        }
    }
}

} // namespace Internal
} // namespace Squish