Files
c-evo-evo/common/Forms/src/FormTests.cpp
T

163 lines
5.1 KiB
C++

// This file is part of c-evo-evo.
//
// This software is governed by the CeCILL license under French law and
// abiding by the rules of distribution of free software. You can use,
// modify and/or redistribute the software under the terms of the CeCILL
// license as circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy,
// modify and redistribute granted by the license, users are provided only
// with a limited warranty and the software's author, the holder of the
// economic rights, and the successive licensors have only limited
// liability.
//
// In this respect, the user's attention is drawn to the risks associated
// with loading, using, modifying and/or developing or reproducing the
// software by the user in light of its specific status of free software,
// that may mean that it is complicated to manipulate, and that also
// therefore means that it is reserved for developers and experienced
// professionals having in-depth computer knowledge. Users are therefore
// encouraged to load and test the software's suitability as regards their
// requirements in conditions enabling the security of their systems and/or
// data to be ensured and, more generally, to use and operate it in the
// same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
#include "Cevo/FormTests.h"
#include "Cevo/FormTest.h"
#include "ui_FormTests.h"
#include <QMenu>
namespace Cevo {
namespace {
TestCase *selectedTestCase(QTreeWidget *tree)
{
QTreeWidgetItem *item = tree->currentItem();
if (!item)
return nullptr;
return static_cast<TestCase *>(item->data(0, Qt::UserRole).value<void *>());
}
} // namespace
FormTests::FormTests(QWidget *parent) : FormBase(parent), ui(new Ui::FormTests)
{
ui->setupUi(this);
connect(ui->buttonRun, &QPushButton::clicked, this, &FormTests::onRunClicked);
// Matches the original .lfm's ButtonClose OnClick=ButtonRunClick quirk.
connect(ui->buttonClose, &QPushButton::clicked, this, &FormTests::onRunClicked);
connect(ui->listViewTestCases, &QTreeWidget::itemSelectionChanged, this, &FormTests::onSelectionChanged);
connect(ui->listViewTestCases, &QTreeWidget::itemDoubleClicked, this, &FormTests::onShowSelected);
connect(ui->listViewTestCases, &QTreeWidget::customContextMenuRequested, this, [this](const QPoint &pos) {
QMenu menu(this);
QAction *show = menu.addAction(tr("Show"), this, &FormTests::onShowSelected);
QAction *run = menu.addAction(tr("Run"), this, &FormTests::onRunSelected);
const bool hasSelection = ui->listViewTestCases->currentItem() != nullptr;
show->setEnabled(hasSelection);
run->setEnabled(hasSelection);
menu.exec(ui->listViewTestCases->viewport()->mapToGlobal(pos));
});
}
FormTests::~FormTests()
{
delete ui;
}
void FormTests::setTestCases(TestCases *value)
{
if (m_testCases == value)
return;
m_testCases = value;
reloadList();
updateInterface();
}
void FormTests::showEvent(QShowEvent *event)
{
FormBase::showEvent(event);
reloadList();
updateInterface();
}
void FormTests::reloadList()
{
ui->listViewTestCases->clear();
if (!m_testCases)
return;
for (int i = 0; i < m_testCases->count(); ++i) {
TestCase *testCase = m_testCases->at(i);
auto *item = new QTreeWidgetItem(ui->listViewTestCases);
item->setText(0, testCase->name);
item->setText(1, testResultText(testCase->testResult));
item->setData(0, Qt::UserRole, QVariant::fromValue(static_cast<void *>(testCase)));
}
}
void FormTests::updateInterface()
{
const bool selected = ui->listViewTestCases->currentItem() != nullptr;
ui->buttonRun->setEnabled(m_testCases != nullptr);
Q_UNUSED(selected);
int passed = 0, failed = 0;
if (m_testCases) {
for (int i = 0; i < m_testCases->count(); ++i) {
switch (m_testCases->at(i)->testResult) {
case TestResult::Passed:
++passed;
break;
case TestResult::Failed:
++failed;
break;
default:
break;
}
}
}
ui->labelResult->setText(tr("Passed") + ' ' + QString::number(passed) + ", " + tr("Failed") + ' ' +
QString::number(failed));
}
void FormTests::onRunClicked()
{
if (!m_testCases)
return;
for (int i = 0; i < m_testCases->count(); ++i)
m_testCases->at(i)->run();
reloadList();
updateInterface();
}
void FormTests::onSelectionChanged()
{
updateInterface();
}
void FormTests::onShowSelected()
{
TestCase *testCase = selectedTestCase(ui->listViewTestCases);
if (!testCase)
return;
FormTest dialog(this);
dialog.setTestCase(testCase);
dialog.exec();
reloadList();
}
void FormTests::onRunSelected()
{
TestCase *testCase = selectedTestCase(ui->listViewTestCases);
if (!testCase)
return;
testCase->run();
reloadList();
updateInterface();
}
} // namespace Cevo