diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index bafe66d2c..12dd57b69 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -14,6 +14,7 @@ set(TEST_SOURCES Outline.cpp Sprite.cpp Signal.cpp + SignalManager.cpp String.cpp SvgImage.cpp Text.cpp diff --git a/tests/SignalManager.cpp b/tests/SignalManager.cpp new file mode 100644 index 000000000..c898d66e6 --- /dev/null +++ b/tests/SignalManager.cpp @@ -0,0 +1,85 @@ +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// +// TGUI - Texus' Graphical User Interface +// Copyright (C) 2012-2019 Bruno Van de Velde (vdv_b@tgui.eu) +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +#include "Tests.hpp" +#include +#include + + +TEST_CASE("[SignalManager]") +{ + tgui::Widget::Ptr widget = tgui::Button::create(); + + SECTION("connect") + { + class MySignalManager : public tgui::SignalManager { + public: + + virtual void connect(std::string signalName, tgui::Widget* widget){ + last_signal_name = signalName; + last_widget = widget; + ++connected; + } + + virtual void remove(std::string signalName, tgui::Widget* widget){ + last_signal_name = signalName; + last_widget = widget; + --connected; + } + + std::string last_signal_name; + tgui::Widget* last_widget; + int connected = 0; + }; + + std::shared_ptr m = std::make_shared(); + tgui::SignalManager::setDefaultSignalManager(m); + REQUIRE(tgui::SignalManager::getSignalManager().get() == m.get()); + widget->setCallbackID("w1"); + REQUIRE(m->last_signal_name == "w1"); + REQUIRE(m->last_widget == widget.get()); + REQUIRE(m->connected == 1); + widget->setCallbackID("w2"); + REQUIRE(m->connected == 1); + { + tgui::Button::Ptr b = std::dynamic_pointer_cast(widget); + auto widget2 = tgui::Button::copy(b); + REQUIRE(m->connected == 2); + } + REQUIRE(m->connected == 1); + { + tgui::Button::Ptr widget2 = tgui::Button::create(); + widget2->setCallbackID("w3"); + REQUIRE(m->connected == 2); + *widget = std::move(*widget2); + REQUIRE(m->connected == 1); + } + REQUIRE(m->connected == 1); + widget->setCallbackID(""); + REQUIRE(m->connected == 0); + widget->setCallbackID("w1"); + widget = nullptr; + REQUIRE(m->connected == 0); + } +}