aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/autotest/testtreeitem.cpp
blob: 39dfe5b65da8b6301b9d0ac2d5f05f1c5d47b7a5 (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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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 "autotestconstants.h"
#include "itestparser.h"
#include "testconfiguration.h"
#include "testtreeitem.h"

#include <cplusplus/Icons.h>
#include <cpptools/cppmodelmanager.h>
#include <cpptools/cpptoolsreuse.h>
#include <texteditor/texteditor.h>
#include <utils/utilsicons.h>

#include <QIcon>

namespace Autotest {
namespace Internal {

TestTreeItem::TestTreeItem(const QString &name, const QString &filePath, Type type)
    : m_name(name),
      m_filePath(filePath),
      m_type(type)
{
    switch (m_type) {
    case Root:
    case GroupNode:
    case TestCase:
    case TestFunctionOrSet:
        m_checked = Qt::Checked;
        break;
    default:
        m_checked = Qt::Unchecked;
        break;
    }
}

static QIcon testTreeIcon(TestTreeItem::Type type)
{
    static QIcon icons[] = {
        QIcon(),
        Utils::Icons::OPENFILE.icon(),
        Utils::CodeModelIcon::iconForType(Utils::CodeModelIcon::Class),
        Utils::CodeModelIcon::iconForType(Utils::CodeModelIcon::SlotPrivate),
        QIcon(":/autotest/images/data.png")
    };

    if (int(type) >= int(sizeof icons / sizeof *icons))
        return icons[3];
    return icons[type];
}

QVariant TestTreeItem::data(int /*column*/, int role) const
{
    switch (role) {
    case Qt::DisplayRole:
        if (m_type == Root && childCount() == 0)
            return QCoreApplication::translate("TestTreeItem", "%1 (none)").arg(m_name);
        else
            return m_name;
    case Qt::ToolTipRole:
        return m_filePath;
    case Qt::DecorationRole:
        return testTreeIcon(m_type);
    case Qt::CheckStateRole:
        return QVariant();
    case LinkRole: {
        if (m_type == GroupNode)
            return QVariant();
        QVariant itemLink;
        itemLink.setValue(Utils::Link(m_filePath, int(m_line), int(m_column)));
        return itemLink;
    }
    case ItalicRole:
        return false;
    case TypeRole:
        return m_type;
    case EnabledRole:
        return true;
    }
    return QVariant();
}

bool TestTreeItem::setData(int /*column*/, const QVariant &data, int role)
{
    if (role == Qt::CheckStateRole) {
        Qt::CheckState old = m_checked;
        m_checked = Qt::CheckState(data.toInt());
        return m_checked != old;
    }
    return false;
}

Qt::ItemFlags TestTreeItem::flags(int /*column*/) const
{
    static const Qt::ItemFlags defaultFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    switch (m_type) {
    case Root:
    case GroupNode:
        return Qt::ItemIsEnabled | Qt::ItemIsAutoTristate | Qt::ItemIsUserCheckable;
    case TestCase:
        return defaultFlags | Qt::ItemIsAutoTristate | Qt::ItemIsUserCheckable;
    case TestFunctionOrSet:
        return defaultFlags | Qt::ItemIsUserCheckable;
    default:
        return defaultFlags;
    }
}

bool TestTreeItem::modifyTestCaseContent(const TestParseResult *result)
{
    bool hasBeenModified = modifyName(result->name);
    hasBeenModified |= modifyLineAndColumn(result);
    return hasBeenModified;
}

bool TestTreeItem::modifyTestFunctionContent(const TestParseResult *result)
{
    bool hasBeenModified = modifyFilePath(result->fileName);
    hasBeenModified |= modifyLineAndColumn(result);
    return hasBeenModified;
}

bool TestTreeItem::modifyDataTagContent(const TestParseResult *result)
{

    bool hasBeenModified = modifyTestFunctionContent(result);
    hasBeenModified |= modifyName(result->name);
    return hasBeenModified;
}

bool TestTreeItem::modifyLineAndColumn(const TestParseResult *result)
{
    bool hasBeenModified = false;
    if (m_line != result->line) {
        m_line = result->line;
        hasBeenModified = true;
    }
    if (m_column != result->column) {
        m_column = result->column;
        hasBeenModified = true;
    }
    return hasBeenModified;
}

Qt::CheckState TestTreeItem::checked() const
{
    switch (m_type) {
    case Root:
    case GroupNode:
    case TestCase:
    case TestFunctionOrSet:
    case TestDataTag:
        return m_checked;
    default:
        return Qt::Unchecked;
    }
}

void TestTreeItem::markForRemoval(bool mark)
{
    m_status = mark ? MarkedForRemoval : Cleared;
}

void TestTreeItem::markForRemovalRecursively(bool mark)
{
    markForRemoval(mark);
    for (int row = 0, count = childCount(); row < count; ++row)
        childAt(row)->markForRemovalRecursively(mark);
}

void TestTreeItem::markForRemovalRecursively(const QString &filePath)
{
    bool mark = m_filePath == filePath;
    forFirstLevelChildren([&mark, &filePath](TestTreeItem *child) {
        child->markForRemovalRecursively(filePath);
        mark &= child->markedForRemoval();
    });
    markForRemoval(mark);
}

TestTreeItem *TestTreeItem::parentItem() const
{
    return static_cast<TestTreeItem *>(parent());
}

TestTreeItem *TestTreeItem::findChildByName(const QString &name)
{
    return findFirstLevelChild([name](const TestTreeItem *other) { return other->name() == name; });
}

TestTreeItem *TestTreeItem::findChildByFile(const QString &filePath)
{
    return findFirstLevelChild([filePath](const TestTreeItem *other) {
        return other->filePath() == filePath;
    });
}

TestTreeItem *TestTreeItem::findChildByFileAndType(const QString &filePath, Type tType)
{
    return findFirstLevelChild([filePath, tType](const TestTreeItem *other) {
        return other->type() == tType && other->filePath() == filePath;
    });
}

TestTreeItem *TestTreeItem::findChildByNameAndFile(const QString &name, const QString &filePath)
{
    return findFirstLevelChild([name, filePath](const TestTreeItem *other) {
        return other->filePath() == filePath && other->name() == name;
    });
}

TestConfiguration *TestTreeItem::asConfiguration(TestRunMode mode) const
{
    switch (mode) {
    case TestRunMode::Run:
    case TestRunMode::RunWithoutDeploy:
        return testConfiguration();
    case TestRunMode::Debug:
    case TestRunMode::DebugWithoutDeploy:
        return debugConfiguration();
    }
    return nullptr;
}

QList<TestConfiguration *> TestTreeItem::getAllTestConfigurations() const
{
    return QList<TestConfiguration *>();
}

QList<TestConfiguration *> TestTreeItem::getSelectedTestConfigurations() const
{
    return QList<TestConfiguration *>();
}

QList<TestConfiguration *> TestTreeItem::getTestConfigurationsForFile(const Utils::FileName &) const
{
    return QList<TestConfiguration *>();
}

bool TestTreeItem::lessThan(const TestTreeItem *other, SortMode mode) const
{
    const QString &lhs = data(0, Qt::DisplayRole).toString();
    const QString &rhs = other->data(0, Qt::DisplayRole).toString();

    switch (mode) {
    case Alphabetically:
        if (lhs == rhs)
            return index().row() > other->index().row();
        return lhs > rhs;
    case Naturally: {
        if (m_type == GroupNode && other->type() == GroupNode)
            return m_filePath > other->filePath();

        const Utils::Link &leftLink = data(0, LinkRole).value<Utils::Link>();
        const Utils::Link &rightLink = other->data(0, LinkRole).value<Utils::Link>();
        if (leftLink.targetFileName == rightLink.targetFileName) {
            return leftLink.targetLine == rightLink.targetLine
                    ? leftLink.targetColumn > rightLink.targetColumn
                    : leftLink.targetLine > rightLink.targetLine;
        }
        return leftLink.targetFileName > rightLink.targetFileName;
    }
    }
    return true;
}

bool TestTreeItem::isGroupNodeFor(const TestTreeItem *other) const
{
    QTC_ASSERT(other, return false);
    if (type() != TestTreeItem::GroupNode)
        return false;

    // for now there's only the possibility to have 'Folder' nodes
    return QFileInfo(other->filePath()).absolutePath() == filePath();
}

QSet<QString> TestTreeItem::internalTargets() const
{
    auto cppMM = CppTools::CppModelManager::instance();
    const QList<CppTools::ProjectPart::Ptr> projectParts = cppMM->projectPart(m_filePath);
    // if we have no project parts it's most likely a header with declarations only and CMake based
    if (projectParts.isEmpty())
        return TestTreeItem::dependingInternalTargets(cppMM, m_filePath);
    QSet<QString> targets;
    for (const CppTools::ProjectPart::Ptr &part : projectParts) {
        targets.insert(part->buildSystemTarget);
        if (part->buildTargetType != CppTools::ProjectPart::Executable)
            targets.unite(TestTreeItem::dependingInternalTargets(cppMM, m_filePath));
    }
    return targets;
}

void TestTreeItem::copyBasicDataFrom(const TestTreeItem *other)
{
    if (!other)
        return;
    m_name = other->m_name;
    m_filePath = other->m_filePath;
    m_type = other->m_type;
    m_checked = other->m_checked;
    m_line = other->m_line;
    m_column = other->m_column;
    m_proFile = other->m_proFile;
    m_status = other->m_status;
}

inline bool TestTreeItem::modifyFilePath(const QString &filePath)
{
    if (m_filePath != filePath) {
        m_filePath = filePath;
        return true;
    }
    return false;
}

inline bool TestTreeItem::modifyName(const QString &name)
{
    if (m_name != name) {
        m_name = name;
        return true;
    }
    return false;
}

/*
 * try to find build system target that depends on the given file - if the file is no header
 * try to find the corresponding header and use this instead to find the respective target
 */
QSet<QString> TestTreeItem::dependingInternalTargets(CppTools::CppModelManager *cppMM,
                                                     const QString &file)
{
    QSet<QString> result;
    QTC_ASSERT(cppMM, return result);
    const CPlusPlus::Snapshot snapshot = cppMM->snapshot();
    QTC_ASSERT(snapshot.contains(file), return result);
    bool wasHeader;
    const QString correspondingFile
            = CppTools::correspondingHeaderOrSource(file, &wasHeader, CppTools::CacheUsage::ReadOnly);
    const Utils::FileNameList dependingFiles = snapshot.filesDependingOn(
                wasHeader ? file : correspondingFile);
    for (const Utils::FileName &fn : dependingFiles) {
        for (const CppTools::ProjectPart::Ptr &part : cppMM->projectPart(fn))
            result.insert(part->buildSystemTarget);
    }
    return result;
}

} // namespace Internal
} // namespace Autotest