summaryrefslogtreecommitdiff
path: root/plugins/autotest/testsquishfilehandler.cpp
blob: b69e5e254405e66d15468e311b08213c3e7a6f70 (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
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd
** All rights reserved.
** For any questions to The Qt Company, please use contact form at
** http://www.qt.io/contact-us
**
** 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 The Qt Company.
**
** If you have questions regarding the use of this file, please use
** contact form at http://www.qt.io/contact-us
**
****************************************************************************/

#include "opensquishsuitesdialog.h"
#include "testsquishfilehandler.h"
#include "testsquishutils.h"
#include "testsquishtools.h"

#include <coreplugin/icore.h>

#include <utils/qtcassert.h>

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

namespace Autotest {
namespace Internal {

static TestSquishFileHandler *m_instance = 0;

TestSquishFileHandler::TestSquishFileHandler(QObject *parent)
    : QObject(parent),
      m_squishTools(new TestSquishTools)
{
    m_instance = this;
}

TestSquishFileHandler::~TestSquishFileHandler()
{
    delete m_squishTools;
}

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

    return m_instance;
}

TestTreeItem createTestTreeItem(const QString &name, const QString &filePath,
                                const QStringList &cases)
{
    TestTreeItem item(name, filePath, TestTreeItem::SQUISH_SUITE);
    foreach (const QString &testCase, cases) {
        TestTreeItem *child = new TestTreeItem(QFileInfo(testCase).dir().dirName(), testCase,
                                               TestTreeItem::SQUISH_TESTCASE, &item);
        item.appendChild(child);
    }
    return item;
}

void TestSquishFileHandler::openTestSuites()
{
    OpenSquishSuitesDialog dialog;
    dialog.exec();
    foreach (const QString &suite, dialog.chosenSuites()) {
        const QDir suiteDir(suite);
        const QStringList cases = TestSquishUtils::validTestCases(suite);
        const QFileInfo suiteConf(suiteDir, QLatin1String("suite.conf"));
        if (m_suites.contains(suiteDir.dirName())) {
            if (QMessageBox::question(Core::ICore::dialogParent(), tr("Suite Already Open"),
                                      tr("A test suite with the name \"%1\" is already open. "
                                         "The opened test suite will be closed and replaced "
                                         "with the new one.").arg(suiteDir.dirName()),
                                      QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Cancel)
                    == QMessageBox::Cancel) {
                continue;
            } else {
                TestTreeItem item = createTestTreeItem(suiteDir.dirName(),
                                                       suiteConf.absoluteFilePath(), cases);
                // TODO update file watcher
                m_suites.insert(suiteDir.dirName(), suiteConf.absoluteFilePath());
                emit testTreeItemModified(item, TestTreeModel::SquishTest,
                                      m_suites.value(suiteDir.dirName()));
            }
        } else {
            TestTreeItem item = createTestTreeItem(suiteDir.dirName(),
                                                   suiteConf.absoluteFilePath(), cases);
            // TODO add file watcher
            m_suites.insert(suiteDir.dirName(), suiteConf.absoluteFilePath());
            emit testTreeItemCreated(item, TestTreeModel::SquishTest);
        }
    }
}

void TestSquishFileHandler::closeTestSuite(const QString &suite)
{
    const QString suiteConf = m_suites.value(suite);
    if (suiteConf.isEmpty())
        return;

    // TODO close respective editors if there are any
    // TODO remove file watcher
    emit testTreeItemsRemoved(suiteConf, TestTreeModel::SquishTest);
    m_suites.remove(suite);
}

void TestSquishFileHandler::closeAllTestSuites()
{
    // TODO close respective editors if there are any
    // TODO remove file watcher
    foreach (const QString &suiteConf, m_suites.values())
        emit testTreeItemsRemoved(suiteConf, TestTreeModel::SquishTest);
    m_suites.clear();
}

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

    if (m_squishTools->state() != TestSquishTools::Idle)
        return;
    const QDir suitePath = QFileInfo(m_suites.value(suiteName)).absoluteDir();
    if (!suitePath.exists() || !suitePath.isReadable()) {
        QMessageBox::critical(Core::ICore::dialogParent(), tr("Test Suite Path Not Accessible"),
                              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;
    }

    m_squishTools->runTestCases(suitePath.absolutePath(), QStringList() << testCaseName);
}

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

    if (m_squishTools->state() != TestSquishTools::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("Test Suite Path Not Accessible"),
                              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 = TestTreeModel::instance()->getSelectedSquishTestCases(suiteConf);
    m_squishTools->runTestCases(suitePath.absolutePath(), testCases);
}

} // namespace Internal
} // namespace Autotest