summaryrefslogtreecommitdiff
path: root/plugins/autotest/testtreeview.cpp
blob: d16e43c60399093b8f136cace331a6bd13991dae (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
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.digia.com
**
** This file is part of the Qt Creator Enterprise Auto Test Add-on.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.digia.com
**
****************************************************************************/

#include "autotestconstants.h"
#include "testcodeparser.h"
#include "testrunner.h"
#include "testtreeitem.h"
#include "testtreemodel.h"
#include "testtreeview.h"

#include <coreplugin/icore.h>

#include <coreplugin/find/itemviewfind.h>

#include <cpptools/cppmodelmanager.h>

#include <projectexplorer/project.h>
#include <projectexplorer/session.h>

#include <texteditor/texteditor.h>

#include <QToolButton>
#include <QVBoxLayout>

namespace Autotest {
namespace Internal {

TestTreeViewWidget::TestTreeViewWidget(QWidget *parent) :
    QWidget(parent)
{
    setWindowTitle(tr("Tests"));
    m_model = TestTreeModel::instance();
    m_view = new TestTreeView(this);
    m_view->setModel(m_model);

    QVBoxLayout *layout = new QVBoxLayout;
    layout->setMargin(0);
    layout->setSpacing(0);
    layout->addWidget(Core::ItemViewFind::createSearchableWrapper(m_view));
    setLayout(layout);

    TestCodeParser *parser = m_model->parser();
    ProjectExplorer::SessionManager *sm = ProjectExplorer::SessionManager::instance();
    connect(sm, &ProjectExplorer::SessionManager::startupProjectChanged,
            parser, &TestCodeParser::updateTestTree);

    CppTools::CppModelManager *cppMM = CppTools::CppModelManager::instance();
    connect(cppMM, &CppTools::CppModelManager::documentUpdated,
            parser, &TestCodeParser::onDocumentUpdated, Qt::QueuedConnection);

    connect(cppMM, &CppTools::CppModelManager::aboutToRemoveFiles,
            parser, &TestCodeParser::removeFiles, Qt::QueuedConnection);

    connect(m_view, &TestTreeView::activated, this, &TestTreeViewWidget::onItemActivated);
}

void TestTreeViewWidget::contextMenuEvent(QContextMenuEvent *event)
{
    bool enabled = !TestRunner::instance()->isTestRunning();
    bool hasTests = m_model->hasTests();
    QMenu menu;
    QAction *runAll = new QAction(tr("Run All Tests"), &menu);
    QAction *runSelected = new QAction(tr("Run Selected Tests"), &menu);
    QAction *selectAll = new QAction(tr("Select All"), &menu);
    QAction *deselectAll = new QAction(tr("Deselect All"), &menu);
    // TODO remove?
    QAction *rescan = new QAction(tr("Rescan"), &menu);

    connect(runAll, &QAction::triggered, this, &TestTreeViewWidget::onRunAllTriggered);
    connect(runSelected, &QAction::triggered, this, &TestTreeViewWidget::onRunSelectedTriggered);
    connect(selectAll, &QAction::triggered, m_view, &TestTreeView::selectAll);
    connect(deselectAll, &QAction::triggered, m_view, &TestTreeView::deselectAll);
    connect(rescan, &QAction::triggered,
            TestTreeModel::instance()->parser(), &TestCodeParser::updateTestTree);

    runAll->setEnabled(enabled && hasTests);
    runSelected->setEnabled(enabled && hasTests);
    selectAll->setEnabled(enabled && hasTests);
    deselectAll->setEnabled(enabled && hasTests);
    rescan->setEnabled(enabled);

    menu.addAction(runAll);
    menu.addAction(runSelected);
    menu.addSeparator();
    menu.addAction(selectAll);
    menu.addAction(deselectAll);
    menu.addSeparator();
    menu.addAction(rescan);

    menu.exec(mapToGlobal(event->pos()));
}

QList<QToolButton *> TestTreeViewWidget::createToolButtons()
{
    QList<QToolButton *> list;

    m_sortAlphabetically = true;
    m_sort = new QToolButton(this);
    m_sort->setIcon((QIcon(QLatin1String(":/images/leafsort.png"))));
    m_sort->setToolTip(tr("Sort Naturally (not implemented yet)"));

    QToolButton *expand = new QToolButton(this);
    expand->setIcon(QIcon(QLatin1String(":/images/expand.png")));
    expand->setToolTip(tr("Expand All"));

    QToolButton *collapse = new QToolButton(this);
    collapse->setIcon(QIcon(QLatin1String(":/images/collapse.png")));
    collapse->setToolTip(tr("Collapse All"));

    connect(expand, &QToolButton::clicked, m_view, &TestTreeView::expandAll);
    connect(collapse, &QToolButton::clicked, m_view, &TestTreeView::collapseAll);
    connect(m_sort, &QToolButton::clicked, this, &TestTreeViewWidget::onSortClicked);

    list << m_sort << expand << collapse;
    return list;
}

void TestTreeViewWidget::onItemActivated(const QModelIndex &index)
{
    const TextEditor::TextEditorWidget::Link link
            = index.data(LinkRole).value<TextEditor::TextEditorWidget::Link>();
    if (link.hasValidTarget()) {
        Core::EditorManager::openEditorAt(link.targetFileName,
                                          link.targetLine,
                                          link.targetColumn);
    }
}

void TestTreeViewWidget::onRunAllTriggered()
{
    TestRunner *runner = TestRunner::instance();
    runner->setSelectedTests(m_model->getAllTestCases());
    runner->runTests();
}

void TestTreeViewWidget::onRunSelectedTriggered()
{
    TestRunner *runner = TestRunner::instance();
    runner->setSelectedTests(m_model->getSelectedTests());
    runner->runTests();
}

void TestTreeViewWidget::onSortClicked()
{
    if (m_sortAlphabetically) {
        m_sort->setIcon((QIcon(QLatin1String(":/images/sort.png"))));
        m_sort->setToolTip(tr("Sort Alphabetically"));
    } else {
        m_sort->setIcon((QIcon(QLatin1String(":/images/leafsort.png"))));
        m_sort->setToolTip(tr("Sort Naturally (not implemented yet)"));
    }
    // TODO trigger the sorting change..
    m_sortAlphabetically = !m_sortAlphabetically;
}

TestViewFactory::TestViewFactory()
{
    setDisplayName(tr("Tests"));
    setId(Autotest::Constants::AUTOTEST_ID);
    setPriority(666);
}

Core::NavigationView TestViewFactory::createWidget()
{
    TestTreeViewWidget *treeViewWidget = new TestTreeViewWidget;
    Core::NavigationView view;
    view.widget = treeViewWidget;
    view.dockToolBarWidgets = treeViewWidget->createToolButtons();
    return view;
}

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

void TestTreeView::selectAll()
{
    selectOrDeselectAll(Qt::Checked);
}

void TestTreeView::deselectAll()
{
    selectOrDeselectAll(Qt::Unchecked);
}

// this avoids the re-evaluation of parent nodes when modifying the child nodes (setData())
void TestTreeView::selectOrDeselectAll(const Qt::CheckState checkState)
{
    TestTreeModel *model = TestTreeModel::instance();
    QModelIndex autoTestsIndex = model->index(0, 0, rootIndex());
    if (!autoTestsIndex.isValid())
        return;
    int count = model->rowCount(autoTestsIndex);
    QModelIndex last;
    for (int i = 0; i < count; ++i) {
        QModelIndex classesIndex = model->index(i, 0, autoTestsIndex);
        int funcCount = model->rowCount(classesIndex);
        TestTreeItem *item = static_cast<TestTreeItem *>(classesIndex.internalPointer());
        if (item) {
            item->setChecked(checkState);
            if (!item->childCount())
                last = classesIndex;
        }
        for (int j = 0; j < funcCount; ++j) {
            last = model->index(j, 0, classesIndex);
            TestTreeItem *item = static_cast<TestTreeItem *>(last.internalPointer());
            if (item)
                item->setChecked(checkState);
        }
    }
    emit dataChanged(autoTestsIndex, last);
}

} // namespace Internal
} // namespace Autotest