diff options
author | Kyle Edwards <kyle.edwards@kitware.com> | 2020-09-29 13:51:13 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2020-09-29 13:51:22 (GMT) |
commit | af048185b1b4c20ddda2998e448c81449b803944 (patch) | |
tree | 622ad3408eefc424c832c269c75ca0ef80268f21 /Tests/CMakeGUI | |
parent | 39677de5e209445c8cbc5957c1e79088d5d2a03a (diff) | |
parent | 85f5009d2786349a6576d19bf6b605f825775b44 (diff) | |
download | CMake-af048185b1b4c20ddda2998e448c81449b803944.zip CMake-af048185b1b4c20ddda2998e448c81449b803944.tar.gz CMake-af048185b1b4c20ddda2998e448c81449b803944.tar.bz2 |
Merge topic 'cmake-gui-environment'
85f5009d27 CMake GUI: Add environment editor
d6c051c126 Tests: Add some basic configure tests for the CMake GUI
7cd95d9996 Tests: Add CatchShow helper for CMake GUI tests
Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !5270
Diffstat (limited to 'Tests/CMakeGUI')
-rw-r--r-- | Tests/CMakeGUI/CMakeGUITest.cmake | 9 | ||||
-rw-r--r-- | Tests/CMakeGUI/CMakeGUITest.cxx | 104 | ||||
-rw-r--r-- | Tests/CMakeGUI/CMakeGUITest.h | 5 | ||||
-rw-r--r-- | Tests/CMakeGUI/CMakeLists.txt | 29 | ||||
-rw-r--r-- | Tests/CMakeGUI/CatchShow.cxx | 25 | ||||
-rw-r--r-- | Tests/CMakeGUI/CatchShow.h | 41 | ||||
-rw-r--r-- | Tests/CMakeGUI/CatchShowTest.cxx | 49 | ||||
-rw-r--r-- | Tests/CMakeGUI/CatchShowTest.h | 15 | ||||
-rw-r--r-- | Tests/CMakeGUI/EnvironmentDialogTest.cxx | 142 | ||||
-rw-r--r-- | Tests/CMakeGUI/EnvironmentDialogTest.h | 15 | ||||
-rw-r--r-- | Tests/CMakeGUI/environment/CMakeLists.txt.in | 18 | ||||
-rw-r--r-- | Tests/CMakeGUI/simpleConfigure-fail/CMakeLists.txt.in | 5 | ||||
-rw-r--r-- | Tests/CMakeGUI/simpleConfigure-success/CMakeLists.txt.in | 4 |
13 files changed, 459 insertions, 2 deletions
diff --git a/Tests/CMakeGUI/CMakeGUITest.cmake b/Tests/CMakeGUI/CMakeGUITest.cmake index 3d8c27e..b60ec35 100644 --- a/Tests/CMakeGUI/CMakeGUITest.cmake +++ b/Tests/CMakeGUI/CMakeGUITest.cmake @@ -109,3 +109,12 @@ run_cmake_gui_test(sourceBinaryArgs:noExistConfigExists ARGS "${CMakeGUITest_BINARY_DIR}/sourceBinaryArgs-noExistConfigExists/noexist" ) + +run_cmake_gui_test(simpleConfigure:success) +run_cmake_gui_test(simpleConfigure:fail) + +unset(ENV{ADDED_VARIABLE}) +set(ENV{KEPT_VARIABLE} "Kept variable") +set(ENV{CHANGED_VARIABLE} "This variable will be changed") +set(ENV{REMOVED_VARIABLE} "Removed variable") +run_cmake_gui_test(environment) diff --git a/Tests/CMakeGUI/CMakeGUITest.cxx b/Tests/CMakeGUI/CMakeGUITest.cxx index a7a5d17..80ea08d 100644 --- a/Tests/CMakeGUI/CMakeGUITest.cxx +++ b/Tests/CMakeGUI/CMakeGUITest.cxx @@ -2,8 +2,10 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "CMakeGUITest.h" +#include "QCMake.h" #include <QApplication> #include <QEventLoop> +#include <QMessageBox> #include <QSettings> #include <QString> #include <QStringList> @@ -13,6 +15,9 @@ #include "CMakeSetupDialog.h" +#include "CatchShow.h" +#include "FirstConfigure.h" + namespace { void loopSleep(int msecs = 100) { @@ -28,6 +33,44 @@ CMakeGUITest::CMakeGUITest(CMakeSetupDialog* window, QObject* parent) { } +void CMakeGUITest::tryConfigure(int expectedResult, int timeout) +{ + auto* cmake = this->m_window->findChild<QCMakeThread*>()->cmakeInstance(); + + bool done = false; + CatchShow catchConfigure; + catchConfigure.setCallback<FirstConfigure>([&done](FirstConfigure* dialog) { + if (done) { + return; + } + done = true; + + dialog->findChild<StartCompilerSetup*>()->setCurrentGenerator( + CMAKE_GENERATOR); + dialog->accept(); + }); + + CatchShow catchMessages; + catchMessages.setCallback<QMessageBox>([](QMessageBox* box) { + if (box->text().contains("Build directory does not exist")) { + box->accept(); + } + + if (box->text().contains("Error in configuration process")) { + box->accept(); + } + }); + + QSignalSpy configureDoneSpy(cmake, &QCMake::configureDone); + QVERIFY(configureDoneSpy.isValid()); + QMetaObject::invokeMethod( + this->m_window, [this]() { this->m_window->ConfigureButton->click(); }, + Qt::QueuedConnection); + QVERIFY(configureDoneSpy.wait(timeout)); + + QCOMPARE(configureDoneSpy, { { expectedResult } }); +} + void CMakeGUITest::sourceBinaryArgs() { QFETCH(QString, sourceDir); @@ -68,6 +111,67 @@ void CMakeGUITest::sourceBinaryArgs_data() << CMakeGUITest_BINARY_DIR "/sourceBinaryArgs-noExistConfigExists/build"; } +void CMakeGUITest::simpleConfigure() +{ + QFETCH(QString, sourceDir); + QFETCH(QString, binaryDir); + QFETCH(int, expectedResult); + + this->m_window->SourceDirectory->setText(sourceDir); + this->m_window->BinaryDirectory->setCurrentText(binaryDir); + + // Wait a bit for everything to update + loopSleep(); + + this->tryConfigure(expectedResult, 1000); +} + +void CMakeGUITest::simpleConfigure_data() +{ + QTest::addColumn<QString>("sourceDir"); + QTest::addColumn<QString>("binaryDir"); + QTest::addColumn<int>("expectedResult"); + + QTest::newRow("success") << CMakeGUITest_BINARY_DIR + "/simpleConfigure-success/src" + << CMakeGUITest_BINARY_DIR + "/simpleConfigure-success/build" + << 0; + QTest::newRow("fail") << CMakeGUITest_BINARY_DIR "/simpleConfigure-fail/src" + << CMakeGUITest_BINARY_DIR + "/simpleConfigure-fail/build" + << -1; +} + +void CMakeGUITest::environment() +{ + auto* cmake = this->m_window->findChild<QCMakeThread*>()->cmakeInstance(); + + this->m_window->SourceDirectory->setText(CMakeGUITest_BINARY_DIR + "/environment/src"); + this->m_window->BinaryDirectory->setCurrentText(CMakeGUITest_BINARY_DIR + "/environment/build"); + + // We are already testing EnvironmentDialog, so just trust that it's + // connected correctly and modify the environment directly. + auto env = cmake->environment(); + env.insert("ADDED_VARIABLE", "Added variable"); + env.insert("CHANGED_VARIABLE", "Changed variable"); + env.remove("REMOVED_VARIABLE"); + cmake->setEnvironment(env); + + // Wait a bit for everything to update + loopSleep(); + + this->tryConfigure(); + + auto penv = QProcessEnvironment::systemEnvironment(); + QVERIFY(!penv.contains("ADDED_VARIABLE")); + QCOMPARE(penv.value("KEPT_VARIABLE"), "Kept variable"); + QCOMPARE(penv.value("CHANGED_VARIABLE"), "This variable will be changed"); + QCOMPARE(penv.value("REMOVED_VARIABLE"), "Removed variable"); +} + void SetupDefaultQSettings() { QSettings::setDefaultFormat(QSettings::IniFormat); diff --git a/Tests/CMakeGUI/CMakeGUITest.h b/Tests/CMakeGUI/CMakeGUITest.h index 55a885b..891cf62 100644 --- a/Tests/CMakeGUI/CMakeGUITest.h +++ b/Tests/CMakeGUI/CMakeGUITest.h @@ -15,7 +15,12 @@ public: private: CMakeSetupDialog* m_window = nullptr; + void tryConfigure(int expectedResult = 0, int timeout = 60000); + private slots: void sourceBinaryArgs(); void sourceBinaryArgs_data(); + void simpleConfigure(); + void simpleConfigure_data(); + void environment(); }; diff --git a/Tests/CMakeGUI/CMakeLists.txt b/Tests/CMakeGUI/CMakeLists.txt index 2a2ee1a..c6bc88a 100644 --- a/Tests/CMakeGUI/CMakeLists.txt +++ b/Tests/CMakeGUI/CMakeLists.txt @@ -10,13 +10,24 @@ include_directories( set(MOC_SRCS) qt5_wrap_cpp(MOC_SRCS + CatchShow.h + ) +add_library(CMakeGUITestLib STATIC ${MOC_SRCS} + CatchShow.cxx + CatchShow.h + ) +target_link_libraries(CMakeGUITestLib Qt5::Core Qt5::Gui Qt5::Widgets) + +set(MOC_SRCS) +qt5_wrap_cpp(MOC_SRCS CMakeGUITest.h ) add_executable(CMakeGUITest CMakeGUITest.cxx ${MOC_SRCS}) -target_link_libraries(CMakeGUITest CMakeGUIMainLib Qt5::Core Qt5::Test Qt5::Widgets) +target_link_libraries(CMakeGUITest CMakeGUIMainLib CMakeGUITestLib Qt5::Core Qt5::Test Qt5::Widgets) target_compile_definitions(CMakeGUITest PRIVATE "CMakeGUITest_SOURCE_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}\"" "CMakeGUITest_BINARY_DIR=\"${CMAKE_CURRENT_BINARY_DIR}\"" + "CMAKE_GENERATOR=\"${CMAKE_GENERATOR}\"" ) add_test(NAME CMakeGUI COMMAND ${CMAKE_CMAKE_COMMAND} @@ -35,11 +46,25 @@ function(add_cmake_gui_lib_test name) ${_t_MOC_SOURCES} ) add_executable(${name} ${_t_SOURCES} ${MOC_SRCS}) - target_link_libraries(${name} CMakeGUILib Qt5::Core Qt5::Test Qt5::Widgets) + target_link_libraries(${name} CMakeGUILib CMakeGUITestLib Qt5::Core Qt5::Test Qt5::Widgets) add_test(NAME "CMakeGUILib.${name}" COMMAND ${name}) endfunction() +add_cmake_gui_lib_test(CatchShow + SOURCES + CatchShowTest.cxx + CatchShowTest.h + MOC_SOURCES + CatchShowTest.h + ) +add_cmake_gui_lib_test(EnvironmentDialog + SOURCES + EnvironmentDialogTest.cxx + EnvironmentDialogTest.h + MOC_SOURCES + EnvironmentDialogTest.h + ) add_cmake_gui_lib_test(QCMakeCacheModel SOURCES QCMakeCacheModelTest.cxx diff --git a/Tests/CMakeGUI/CatchShow.cxx b/Tests/CMakeGUI/CatchShow.cxx new file mode 100644 index 0000000..aee2d9d --- /dev/null +++ b/Tests/CMakeGUI/CatchShow.cxx @@ -0,0 +1,25 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#include "CatchShow.h" + +#include <QCoreApplication> + +CatchShow::CatchShow(QObject* parent) + : QObject(parent) +{ + QCoreApplication::instance()->installEventFilter(this); +} + +bool CatchShow::eventFilter(QObject* obj, QEvent* event) +{ + if (this->m_callback && event->type() == QEvent::Show) { + this->m_callback(obj); + } + + return this->QObject::eventFilter(obj, event); +} + +int CatchShow::count() const +{ + return this->m_count; +} diff --git a/Tests/CMakeGUI/CatchShow.h b/Tests/CMakeGUI/CatchShow.h new file mode 100644 index 0000000..0254c15 --- /dev/null +++ b/Tests/CMakeGUI/CatchShow.h @@ -0,0 +1,41 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#pragma once + +#include <functional> +#include <memory> + +#include <QObject> +#include <QWidget> + +class CatchShow : public QObject +{ + Q_OBJECT +public: + CatchShow(QObject* parent = nullptr); + + template <typename T, typename F> + void setCallback(F&& func); + bool eventFilter(QObject* obj, QEvent* event) override; + int count() const; + +private: + std::function<void(QObject* obj)> m_callback; + int m_count = 0; +}; + +template <typename T, typename F> +void CatchShow::setCallback(F&& func) +{ + this->m_callback = [this, func](QObject* obj) { + auto* d = qobject_cast<T*>(obj); + if (d) { + QMetaObject::invokeMethod(obj, + [this, func, d]() { + ++this->m_count; + func(d); + }, + Qt::QueuedConnection); + } + }; +} diff --git a/Tests/CMakeGUI/CatchShowTest.cxx b/Tests/CMakeGUI/CatchShowTest.cxx new file mode 100644 index 0000000..acea8ea --- /dev/null +++ b/Tests/CMakeGUI/CatchShowTest.cxx @@ -0,0 +1,49 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#include "CatchShowTest.h" + +#include <QMessageBox> +#include <QtTest> + +#include "CatchShow.h" + +CatchShowTest::CatchShowTest(QObject* parent) + : QObject(parent) +{ +} + +void CatchShowTest::catchShow() +{ + bool have = false; + CatchShow catcher; + catcher.setCallback<QMessageBox>([&have](QMessageBox* box) { + have = true; + box->accept(); + }); + + QCOMPARE(catcher.count(), 0); + QCOMPARE(have, false); + + { + QDialog dialog; + dialog.show(); + QCOMPARE(catcher.count(), 0); + QCOMPARE(have, false); + } + + { + have = false; + QMessageBox::critical(nullptr, "Error", "This is an error"); + QCOMPARE(catcher.count(), 1); + QCOMPARE(have, true); + } + + { + have = false; + QMessageBox::information(nullptr, "Info", "This is information"); + QCOMPARE(catcher.count(), 2); + QCOMPARE(have, true); + } +} + +QTEST_MAIN(CatchShowTest) diff --git a/Tests/CMakeGUI/CatchShowTest.h b/Tests/CMakeGUI/CatchShowTest.h new file mode 100644 index 0000000..6da2163 --- /dev/null +++ b/Tests/CMakeGUI/CatchShowTest.h @@ -0,0 +1,15 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#pragma once + +#include <QObject> + +class CatchShowTest : public QObject +{ + Q_OBJECT +public: + CatchShowTest(QObject* parent = nullptr); + +private slots: + void catchShow(); +}; diff --git a/Tests/CMakeGUI/EnvironmentDialogTest.cxx b/Tests/CMakeGUI/EnvironmentDialogTest.cxx new file mode 100644 index 0000000..9ec4996 --- /dev/null +++ b/Tests/CMakeGUI/EnvironmentDialogTest.cxx @@ -0,0 +1,142 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#include "EnvironmentDialogTest.h" + +#include <QDialogButtonBox> +#include <QMessageBox> +#include <QObject> +#include <QPushButton> +#include <QString> +#include <QtTest> + +#include "CatchShow.h" +#include "EnvironmentDialog.h" + +EnvironmentDialogTest::EnvironmentDialogTest(QObject* parent) + : QObject(parent) +{ +} + +void EnvironmentDialogTest::environmentDialog() +{ + CatchShow catcher; + catcher.setCallback<QMessageBox>([](QMessageBox* box) { box->accept(); }); + + QProcessEnvironment env; + env.insert("DELETED_VARIABLE_1", "Deleted variable 1"); + env.insert("DELETED_VARIABLE_2", "Deleted variable 2"); + env.insert("KEPT_VARIABLE", "Kept variable"); + env.insert("CHANGED_VARIABLE", "This will be changed"); + + EnvironmentDialog dialog(env); + + { + QStringList expected{ + "CHANGED_VARIABLE=This will be changed", + "DELETED_VARIABLE_1=Deleted variable 1", + "DELETED_VARIABLE_2=Deleted variable 2", + "KEPT_VARIABLE=Kept variable", + }; + QCOMPARE(dialog.environment().toStringList(), expected); + QCOMPARE(catcher.count(), 0); + } + + { + CatchShow catcher2; + bool done = false; + catcher2.setCallback<QDialog>([&catcher, &done](QDialog* box) { + if (done) { + return; + } + done = true; + + auto name = box->findChild<QLineEdit*>("name"); + auto value = box->findChild<QLineEdit*>("value"); + auto acceptReject = box->findChild<QDialogButtonBox*>(); + + name->setText(""); + value->setText(""); + acceptReject->button(QDialogButtonBox::Ok)->click(); + QCOMPARE(catcher.count(), 1); + + name->setText("KEPT_VARIABLE"); + value->setText(""); + acceptReject->button(QDialogButtonBox::Ok)->click(); + QCOMPARE(catcher.count(), 2); + + name->setText("ADDED_VARIABLE"); + value->setText("Added variable"); + acceptReject->button(QDialogButtonBox::Ok)->click(); + QCOMPARE(catcher.count(), 2); + }); + dialog.AddEntry->click(); + + QStringList expected{ + "ADDED_VARIABLE=Added variable", + "CHANGED_VARIABLE=This will be changed", + "DELETED_VARIABLE_1=Deleted variable 1", + "DELETED_VARIABLE_2=Deleted variable 2", + "KEPT_VARIABLE=Kept variable", + }; + QCOMPARE(dialog.environment().toStringList(), expected); + QCOMPARE(catcher.count(), 2); + QVERIFY(done); + } + + { + CatchShow catcher2; + bool done = false; + catcher2.setCallback<QDialog>([&done](QDialog* box) { + if (done) { + return; + } + done = true; + + auto name = box->findChild<QLineEdit*>("name"); + auto value = box->findChild<QLineEdit*>("value"); + auto acceptReject = box->findChild<QDialogButtonBox*>(); + + name->setText("DISCARDED_VARIABLE"); + value->setText("Discarded variable"); + acceptReject->button(QDialogButtonBox::Cancel)->click(); + }); + dialog.AddEntry->click(); + + QStringList expected{ + "ADDED_VARIABLE=Added variable", + "CHANGED_VARIABLE=This will be changed", + "DELETED_VARIABLE_1=Deleted variable 1", + "DELETED_VARIABLE_2=Deleted variable 2", + "KEPT_VARIABLE=Kept variable", + }; + QCOMPARE(dialog.environment().toStringList(), expected); + QCOMPARE(catcher.count(), 2); + QVERIFY(done); + } + + { + auto* model = dialog.Environment->model(); + auto* selectionModel = dialog.Environment->selectionModel(); + for (int i = 0; i < model->rowCount(); ++i) { + auto index1 = model->index(i, 0); + auto index2 = model->buddy(index1); + auto name = model->data(index1, Qt::DisplayRole).toString(); + if (name == "DELETED_VARIABLE_1" || name == "DELETED_VARIABLE_2") { + selectionModel->select(index1, QItemSelectionModel::Select); + selectionModel->select(index2, QItemSelectionModel::Select); + } else if (name == "CHANGED_VARIABLE") { + model->setData(index2, "Changed variable", Qt::DisplayRole); + } + } + dialog.RemoveEntry->click(); + + QStringList expected{ + "ADDED_VARIABLE=Added variable", + "CHANGED_VARIABLE=Changed variable", + "KEPT_VARIABLE=Kept variable", + }; + QCOMPARE(dialog.environment().toStringList(), expected); + } +} + +QTEST_MAIN(EnvironmentDialogTest) diff --git a/Tests/CMakeGUI/EnvironmentDialogTest.h b/Tests/CMakeGUI/EnvironmentDialogTest.h new file mode 100644 index 0000000..bcba2c5 --- /dev/null +++ b/Tests/CMakeGUI/EnvironmentDialogTest.h @@ -0,0 +1,15 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#pragma once + +#include <QObject> + +class EnvironmentDialogTest : public QObject +{ + Q_OBJECT +public: + EnvironmentDialogTest(QObject* parent = nullptr); + +private slots: + void environmentDialog(); +}; diff --git a/Tests/CMakeGUI/environment/CMakeLists.txt.in b/Tests/CMakeGUI/environment/CMakeLists.txt.in new file mode 100644 index 0000000..1eeeb85 --- /dev/null +++ b/Tests/CMakeGUI/environment/CMakeLists.txt.in @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.18) +project(environment NONE) + +if(NOT "$ENV{KEPT_VARIABLE}" STREQUAL "Kept variable") + message(SEND_ERROR "KEPT_VARIABLE is \"$ENV{KEPT_VARIABLE}\", should be \"Kept variable\"") +endif() + +if(NOT "$ENV{ADDED_VARIABLE}" STREQUAL "Added variable") + message(SEND_ERROR "ADDED_VARIABLE is \"$ENV{ADDED_VARIABLE}\", should be \"Added variable\"") +endif() + +if(NOT "$ENV{CHANGED_VARIABLE}" STREQUAL "Changed variable") + message(SEND_ERROR "CHANGED_VARIABLE is \"$ENV{CHANGED_VARIABLE}\", should be \"Changed variable\"") +endif() + +if(DEFINED ENV{REMOVED_VARIABLE}) + message(SEND_ERROR "REMOVED_VARIABLE should not be defined") +endif() diff --git a/Tests/CMakeGUI/simpleConfigure-fail/CMakeLists.txt.in b/Tests/CMakeGUI/simpleConfigure-fail/CMakeLists.txt.in new file mode 100644 index 0000000..dc55064 --- /dev/null +++ b/Tests/CMakeGUI/simpleConfigure-fail/CMakeLists.txt.in @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.18) +project(simpleConfigure-fail NONE) + +message(STATUS "This is a failed configure") +message(FATAL_ERROR "Error") diff --git a/Tests/CMakeGUI/simpleConfigure-success/CMakeLists.txt.in b/Tests/CMakeGUI/simpleConfigure-success/CMakeLists.txt.in new file mode 100644 index 0000000..fc42c00 --- /dev/null +++ b/Tests/CMakeGUI/simpleConfigure-success/CMakeLists.txt.in @@ -0,0 +1,4 @@ +cmake_minimum_required(VERSION 3.18) +project(simpleConfigure-success NONE) + +message(STATUS "This is a successful configure") |