aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/squish/squishtesttreeview.cpp
blob: 225fe1c86a365604ded791ec648fcb2eda9bc614 (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
// Copyright (C) 2022 The Qt Company Ltd
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "squishtesttreeview.h"

#include "squishconstants.h"
#include "squishfilehandler.h"
#include "squishplugin.h"
#include "squishsettings.h"
#include "squishtesttreemodel.h"
#include "suiteconf.h"

#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icontext.h>
#include <coreplugin/icore.h>

#include <utils/algorithm.h>
#include <utils/fancylineedit.h>
#include <utils/qtcassert.h>

#include <QRegularExpression>

namespace Squish {
namespace Internal {

SquishTestTreeView::SquishTestTreeView(QWidget *parent)
    : Utils::NavigationTreeView(parent)
    , m_context(new Core::IContext(this))
{
    setExpandsOnDoubleClick(false);
    m_context->setWidget(this);
    m_context->setContext(Core::Context(Constants::SQUISH_CONTEXT));
    Core::ICore::addContextObject(m_context);
}

void SquishTestTreeView::resizeEvent(QResizeEvent *event)
{
    // override derived behavior of Utils::NavigationTreeView as we have more than 1 column
    Utils::TreeView::resizeEvent(event);
}

void SquishTestTreeView::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        const QModelIndex index = indexAt(event->pos());
        if (index.isValid() && index.column() > 0 && index.column() < 3) {
            int type = index.data(TypeRole).toInt();
            if (type == SquishTestTreeItem::SquishSuite
                || type == SquishTestTreeItem::SquishTestCase) {
                m_lastMousePressedIndex = index;
            }
        }
    }
    QTreeView::mousePressEvent(event);
}

void SquishTestTreeView::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        const QModelIndex index = indexAt(event->pos());
        if (index.isValid() && index == m_lastMousePressedIndex) {
            int type = index.data(TypeRole).toInt();
            if (type == SquishTestTreeItem::SquishSuite) {
                if (index.column() == 1)
                    emit runTestSuite(index.data(DisplayNameRole).toString());
                else if (index.column() == 2)
                    emit openObjectsMap(index.data(DisplayNameRole).toString());
            } else {
                const QModelIndex &suiteIndex = index.parent();
                if (suiteIndex.isValid()) {
                    if (index.column() == 1) {
                        emit runTestCase(suiteIndex.data(DisplayNameRole).toString(),
                                         index.data(DisplayNameRole).toString());
                    } else if (index.column() == 2) {
                        emit recordTestCase(suiteIndex.data(DisplayNameRole).toString(),
                                            index.data(DisplayNameRole).toString());
                    }
                }
            }
        }
    }
    QTreeView::mouseReleaseEvent(event);
}

/****************************** SquishTestTreeItemDelegate *************************************/

SquishTestTreeItemDelegate::SquishTestTreeItemDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{}

void SquishTestTreeItemDelegate::paint(QPainter *painter,
                                       const QStyleOptionViewItem &option,
                                       const QModelIndex &idx) const
{
    QStyleOptionViewItem opt = option;
    initStyleOption(&opt, idx);

    // elide first column if necessary
    if (idx.column() == 0)
        opt.textElideMode = Qt::ElideMiddle;

    // display disabled items as enabled
    if (idx.flags() == Qt::NoItemFlags)
        opt.palette.setColor(QPalette::Text, opt.palette.color(QPalette::Active, QPalette::Text));

    QStyledItemDelegate::paint(painter, opt, idx);
}

QSize SquishTestTreeItemDelegate::sizeHint(const QStyleOptionViewItem &option,
                                           const QModelIndex &idx) const
{
    QStyleOptionViewItem opt = option;
    initStyleOption(&opt, idx);

    // elide first column if necessary
    if (idx.column() == 0)
        opt.textElideMode = Qt::ElideMiddle;
    return QStyledItemDelegate::sizeHint(opt, idx);
}

QWidget *SquishTestTreeItemDelegate::createEditor(QWidget *parent,
                                                  const QStyleOptionViewItem &option,
                                                  const QModelIndex &index) const
{
    Q_UNUSED(option)
    QTC_ASSERT(parent, return nullptr);
    QTC_ASSERT(index.isValid(), return nullptr);
    auto model = static_cast<const SquishTestTreeSortModel *>(index.model());
    QTC_ASSERT(model, return nullptr);
    auto srcModel = static_cast<SquishTestTreeModel *>(model->sourceModel());

    const SquishTestTreeItem *suite = srcModel->itemForIndex(model->mapToSource(index.parent()));
    SquishTestTreeItem *testCaseItem = srcModel->itemForIndex(model->mapToSource(index));
    const SuiteConf suiteConf = SuiteConf::readSuiteConf(suite->filePath());
    const QStringList inUse = suiteConf.usedTestCases();
    Utils::FancyLineEdit *editor = new Utils::FancyLineEdit(parent);
    editor->setValidationFunction([inUse](Utils::FancyLineEdit *edit, QString *) {
        static const QRegularExpression validFileName("^[-a-zA-Z0-9_$. ]+$");
        QString testName = edit->text();
        if (!testName.startsWith("tst_"))
            testName.prepend("tst_");
        return validFileName.match(testName).hasMatch() && !inUse.contains(testName);
    });

    connect(this, &QStyledItemDelegate::closeEditor,
            editor, [srcModel, testCaseItem](QWidget *, EndEditHint hint) {
        QTC_ASSERT(srcModel, return);
        QTC_ASSERT(testCaseItem, return);
        if (hint != QAbstractItemDelegate::RevertModelCache)
            return;
        srcModel->destroyItem(testCaseItem);
    });
    return editor;
}

void SquishTestTreeItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QTC_ASSERT(editor, return);
    QTC_ASSERT(index.isValid(), return);

    static_cast<Utils::FancyLineEdit *>(editor)->setText(index.data().toString());
}

static bool copyScriptTemplates(const SuiteConf &suiteConf, const Utils::FilePath &destination)
{
    const SquishSettings *s = SquishPlugin::squishSettings();
    QTC_ASSERT(s, return false);
    // copy template files
    const Utils::FilePath squishPath = s->squishPath.filePath();

    bool ok = destination.ensureWritableDir();
    QTC_ASSERT(ok, return false);

    const bool scripted = suiteConf.objectMapStyle() == "script";
    const QString extension = suiteConf.scriptExtension();
    const QString testStr = scripted ? QString("script_som_template") : QString("script_template");

    const Utils::FilePath scripts = s->scriptsPath(suiteConf.language());
    const Utils::FilePath test = scripts.pathAppended(testStr + extension);
    const Utils::FilePath testFile = destination.pathAppended("test" + extension);
    QTC_ASSERT(!testFile.exists(), return false);
    ok = test.copyFile(testFile);
    QTC_ASSERT(ok, return false);

    if (scripted)
        ok = suiteConf.ensureObjectMapExists();
    return ok;
}

void SquishTestTreeItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                              const QModelIndex &index) const
{
    QTC_ASSERT(editor, return);
    QTC_ASSERT(model, return);
    QTC_ASSERT(index.isValid(), return);

    auto sortModel = static_cast<SquishTestTreeSortModel *>(model);
    auto sourceModel = static_cast<SquishTestTreeModel *>(sortModel->sourceModel());
    auto lineEdit = static_cast<Utils::FancyLineEdit *>(editor);
    auto removeFormerlyAdded = [sortModel, sourceModel, &index](){
        auto item = sourceModel->itemForIndex(sortModel->mapToSource(index));
        QTC_ASSERT(item, return);
        sourceModel->destroyItem(item);
    };

    if (!lineEdit->isValid()) {
        // remove the formerly added again
        removeFormerlyAdded();
        return;
    }

    QString chosenName = lineEdit->text();
    if (!chosenName.startsWith("tst_"))
        chosenName.prepend("tst_");

    const QModelIndex parent = index.parent();
    SquishTestTreeItem *suiteItem = sourceModel->itemForIndex(sortModel->mapToSource(parent));
    const auto suiteConfPath = suiteItem->filePath();
    SuiteConf suiteConf = SuiteConf::readSuiteConf(suiteConfPath);
    const Utils::FilePath destination = suiteConfPath.parentDir().pathAppended(chosenName);
    bool ok = copyScriptTemplates(suiteConf, destination);
    QTC_ASSERT(ok, removeFormerlyAdded(); return);

    suiteConf.addTestCase(chosenName);
    ok = suiteConf.write();
    QTC_ASSERT(ok, removeFormerlyAdded(); return);
    SquishFileHandler::instance()->openTestSuite(suiteConfPath, true);

    Core::EditorManager::openEditor(destination.pathAppended("test" + suiteConf.scriptExtension()));
}

} // namespace Internal
} // namespace Squish