summaryrefslogtreecommitdiff
path: root/plugins/autotest/testsquishfilehandler.cpp
blob: 26a2f718b075754b479f2161baed86228dc64395 (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
/****************************************************************************
**
** 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 <QDir>
#include <QFileInfo>
#include <QMessageBox>

namespace Autotest {
namespace Internal {

static TestSquishFileHandler *m_instance = 0;

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

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(0, 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();
}

} // namespace Internal
} // namespace Autotest