From 4bd21b0d9fdd4b4f204721101033ba0853a7a71f Mon Sep 17 00:00:00 2001 From: Daniel Walz Date: Tue, 8 Aug 2023 01:02:58 +0200 Subject: [PATCH] Added unit test for dynamic view hiding --- Tests/CMakeLists.txt | 129 ++++++++++++++------------- Tests/foleys_GuiTreeTests.cpp | 24 +++-- Tests/foleys_MagicBuilderTests.cpp | 121 +++++++++++++++++++++++++ Tests/foleys_MagicProcessorTests.cpp | 11 ++- 4 files changed, 204 insertions(+), 81 deletions(-) create mode 100644 Tests/foleys_MagicBuilderTests.cpp diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index b54d9af7..5964fa3f 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -11,83 +11,88 @@ test the installation process. Defaults to off. ]] -cmake_minimum_required (VERSION 3.15 FATAL_ERROR) +cmake_minimum_required(VERSION 3.15 FATAL_ERROR) -project (FoleysGUIMagicTests - VERSION ${FGM_VERSION} - DESCRIPTION "Unit tests for the foleys_gui_magic JUCE module" - HOMEPAGE_URL "https://foleysfinest.com/developer/pluginguimagic/" - LANGUAGES CXX) +project(FoleysGUIMagicTests + VERSION ${FGM_VERSION} + DESCRIPTION "Unit tests for the foleys_gui_magic JUCE module" + HOMEPAGE_URL "https://foleysfinest.com/developer/pluginguimagic/" + LANGUAGES CXX) enable_testing() # cmake-format: off # building command-line tools for iOS isn't supported by Apple's SDKs if (IOS - OR ("${CMAKE_SYSTEM_NAME}" STREQUAL iOS) - OR ("${CMAKE_SYSTEM_NAME}" STREQUAL tvOS) - OR ("${CMAKE_SYSTEM_NAME}" STREQUAL watchOS)) - - if(PROJECT_IS_TOP_LEVEL) - message (FATAL_ERROR "Building command-line tools for iOS isn't supported by Apple's SDKs; + OR ("${CMAKE_SYSTEM_NAME}" STREQUAL iOS) + OR ("${CMAKE_SYSTEM_NAME}" STREQUAL tvOS) + OR ("${CMAKE_SYSTEM_NAME}" STREQUAL watchOS)) + + if (PROJECT_IS_TOP_LEVEL) + message(FATAL_ERROR "Building command-line tools for iOS isn't supported by Apple's SDKs; the foleys_gui_magic unit test executable cannot be crosscompiled for iOS") - endif() + endif () - return () + return() endif () # cmake-format: on -option (FGM_TEST_INSTALL - "Build the tests against a system install of foleys_gui_magic, instead of the copy in this repo" - "${PROJECT_IS_TOP_LEVEL}") +option(FGM_TEST_INSTALL + "Build the tests against a system install of foleys_gui_magic, instead of the copy in this repo" + "${PROJECT_IS_TOP_LEVEL}") if (FGM_TEST_INSTALL OR PROJECT_IS_TOP_LEVEL) - set (CMAKE_FIND_PACKAGE_PREFER_CONFIG ON) + set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON) + + find_package(foleys_gui_magic "${PROJECT_VERSION}" EXACT REQUIRED CONFIG) +endif () + +include(FetchContent) + +FetchContent_Declare(Catch2 + GIT_REPOSITORY https://github.com/catchorg/Catch2.git + GIT_TAG origin/devel) + +FetchContent_MakeAvailable(Catch2) - find_package (foleys_gui_magic "${PROJECT_VERSION}" EXACT REQUIRED CONFIG) +if (catch2_SOURCE_DIR) + list(APPEND CMAKE_MODULE_PATH "${catch2_SOURCE_DIR}/extras") endif () -include (FetchContent) - -FetchContent_Declare (Catch2 - GIT_REPOSITORY https://github.com/catchorg/Catch2.git - GIT_TAG origin/devel) - -FetchContent_MakeAvailable (Catch2) - -if(catch2_SOURCE_DIR) - list (APPEND CMAKE_MODULE_PATH "${catch2_SOURCE_DIR}/extras") -endif() - -include (Catch) - -juce_add_console_app (FoleysGUIMagicTests VERSION ${FOLEYS_VERSION} - BUNDLE_ID "com.foleysfinest.foleys_gui_magic.tests") - -target_sources (FoleysGUIMagicTests PRIVATE - foleys_MagicProcessorTests.cpp - foleys_GuiTreeTests.cpp - foleys_TestProcessors.h) - -set_target_properties ( - FoleysGUIMagicTests - PROPERTIES LABELS "foleys_gui_magic;Tests" - FOLDER foleys_gui_magic - EchoString "Building the foleys_gui_magic unit test runner..." - MACOSX_BUNDLE OFF) - -target_link_libraries (FoleysGUIMagicTests PRIVATE - Catch2::Catch2WithMain - foleys::foleys_gui_magic) - -catch_discover_tests ( - FoleysGUIMagicTests - TEST_PREFIX foleys_gui_magic. - EXTRA_ARGS - --warn NoAssertions - --order rand - --verbosity high - PROPERTIES - SKIP_REGULAR_EXPRESSION "SKIPPED:" - FAIL_REGULAR_EXPRESSION "FAILED:" +include(Catch) + +juce_add_console_app(FoleysGUIMagicTests VERSION ${FOLEYS_VERSION} + BUNDLE_ID "com.foleysfinest.foleys_gui_magic.tests") + +target_sources(FoleysGUIMagicTests PRIVATE + foleys_MagicBuilderTests.cpp + foleys_MagicProcessorTests.cpp + foleys_GuiTreeTests.cpp + foleys_TestProcessors.h) + +set_target_properties( + FoleysGUIMagicTests + PROPERTIES LABELS "foleys_gui_magic;Tests" + FOLDER foleys_gui_magic + EchoString "Building the foleys_gui_magic unit test runner..." + MACOSX_BUNDLE OFF) + +target_link_libraries(FoleysGUIMagicTests PRIVATE + Catch2::Catch2WithMain + foleys::foleys_gui_magic) + +target_compile_definitions(FoleysGUIMagicTests + PUBLIC + JUCE_MODAL_LOOPS_PERMITTED=1) + +catch_discover_tests( + FoleysGUIMagicTests + TEST_PREFIX foleys_gui_magic. + EXTRA_ARGS + --warn NoAssertions + --order rand + --verbosity high + PROPERTIES + SKIP_REGULAR_EXPRESSION "SKIPPED:" + FAIL_REGULAR_EXPRESSION "FAILED:" ) diff --git a/Tests/foleys_GuiTreeTests.cpp b/Tests/foleys_GuiTreeTests.cpp index 0ca35bf1..1b90fb96 100644 --- a/Tests/foleys_GuiTreeTests.cpp +++ b/Tests/foleys_GuiTreeTests.cpp @@ -1,9 +1,9 @@ /* ============================================================================== - Copyright (c) 2022 Foleys Finest Audio - Daniel Walz + Copyright (c) 2023 Foleys Finest Audio - Daniel Walz All rights reserved. - License for non-commercial projects: + **BSD 3-Clause License** Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -16,10 +16,7 @@ may be used to endorse or promote products derived from this software without specific prior written permission. - License for commercial products: - - To sell commercial products containing this module, you are required to buy a - License from https://foleysfinest.com/developer/pluginguimagic/ + ============================================================================== THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED @@ -34,15 +31,16 @@ ============================================================================== */ -#include -#include - - #include "foleys_TestProcessors.h" +#include +#include + TEST_CASE ("GUI tree test", "[gui]") { -std::unique_ptr processor (new UnitTestProcessor()); -std::unique_ptr editor (processor->createEditor()); -REQUIRE (editor.get() != nullptr); + const juce::ScopedJuceInitialiser_GUI init; + + std::unique_ptr processor (new UnitTestProcessor()); + std::unique_ptr editor (processor->createEditor()); + REQUIRE (editor.get() != nullptr); } diff --git a/Tests/foleys_MagicBuilderTests.cpp b/Tests/foleys_MagicBuilderTests.cpp new file mode 100644 index 00000000..4968ffaf --- /dev/null +++ b/Tests/foleys_MagicBuilderTests.cpp @@ -0,0 +1,121 @@ +/* + ============================================================================== + Copyright (c) 2023 Foleys Finest Audio - Daniel Walz + All rights reserved. + + **BSD 3-Clause License** + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + 3. Neither the name of the copyright holder nor the names of its contributors + may be used to endorse or promote products derived from this software without + specific prior written permission. + + ============================================================================== + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + OF THE POSSIBILITY OF SUCH DAMAGE. + ============================================================================== + */ + +#include +#include + + +TEST_CASE ("GUI builder test", "[gui]") +{ + const juce::ScopedJuceInitialiser_GUI init; + + class TestComponent : public juce::Component + { + public: + TestComponent() + { + auto tree = juce::ValueTree::fromXml ("\n" + "\n" + "\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + ""); + + state.setGuiValueTree (tree); + + builder.registerJUCEFactories(); + builder.createGUI (*this); + } + + void resized() override { builder.updateLayout(); } + + foleys::MagicGUIState state; + foleys::MagicGUIBuilder builder { state }; + }; + + auto testComponent = std::make_unique(); + + auto value = testComponent->state.getPropertyAsValue ("HideEditor"); + value.setValue (false); + + auto flags = juce::ComponentPeer::windowAppearsOnTaskbar | juce::ComponentPeer::windowHasTitleBar; + testComponent->addToDesktop (flags); + testComponent->setVisible (true); + + testComponent->setSize (800, 600); + + auto* editorItem = testComponent->builder.findGuiItemWithId ("editor"); + REQUIRE (editorItem); + REQUIRE (editorItem->getWidth() > 200); + REQUIRE (editorItem->getHeight() > 200); + + auto* buttonItem = testComponent->builder.findGuiItemWithId ("hideEditor"); + REQUIRE (buttonItem); + + auto* button = dynamic_cast (buttonItem->getWrappedComponent()); + REQUIRE (button); + REQUIRE (button->getToggleState() == false); + + button->triggerClick(); + + juce::MessageManager::getInstance()->runDispatchLoopUntil (100); + + REQUIRE (button->getToggleState() == true); + + // Horizontal layout with grow=0 from class hidden should make it zero wide + REQUIRE (editorItem->getWidth() == 0); +} diff --git a/Tests/foleys_MagicProcessorTests.cpp b/Tests/foleys_MagicProcessorTests.cpp index e57a1655..203707c9 100644 --- a/Tests/foleys_MagicProcessorTests.cpp +++ b/Tests/foleys_MagicProcessorTests.cpp @@ -1,9 +1,9 @@ /* ============================================================================== - Copyright (c) 2022 Foleys Finest Audio - Daniel Walz + Copyright (c) 2023 Foleys Finest Audio - Daniel Walz All rights reserved. - License for non-commercial projects: + **BSD 3-Clause License** Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -16,10 +16,7 @@ may be used to endorse or promote products derived from this software without specific prior written permission. - License for commercial products: - - To sell commercial products containing this module, you are required to buy a - License from https://foleysfinest.com/developer/pluginguimagic/ + ============================================================================== THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED @@ -41,6 +38,8 @@ TEST_CASE ("MagicProcessor test", "[processor]") { + const juce::ScopedJuceInitialiser_GUI init; + std::unique_ptr processor (new UnitTestProcessor()); std::unique_ptr editor (processor->createEditor()); REQUIRE (editor.get() != nullptr);