|
| 1 | +/* |
| 2 | +============================================================================== |
| 3 | + Copyright (c) 2022 Foleys Finest Audio - Daniel Walz |
| 4 | + All rights reserved. |
| 5 | +
|
| 6 | + License for non-commercial projects: |
| 7 | +
|
| 8 | + Redistribution and use in source and binary forms, with or without modification, |
| 9 | + are permitted provided that the following conditions are met: |
| 10 | + 1. Redistributions of source code must retain the above copyright notice, this |
| 11 | + list of conditions and the following disclaimer. |
| 12 | + 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + this list of conditions and the following disclaimer in the documentation |
| 14 | + and/or other materials provided with the distribution. |
| 15 | + 3. Neither the name of the copyright holder nor the names of its contributors |
| 16 | + may be used to endorse or promote products derived from this software without |
| 17 | + specific prior written permission. |
| 18 | +
|
| 19 | + License for commercial products: |
| 20 | +
|
| 21 | + To sell commercial products containing this module, you are required to buy a |
| 22 | + License from https://foleysfinest.com/developer/pluginguimagic/ |
| 23 | +
|
| 24 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 25 | + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 26 | + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 27 | + IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 28 | + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 29 | + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 30 | + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 31 | + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 32 | + OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 33 | + OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | +============================================================================== |
| 35 | +*/ |
| 36 | + |
| 37 | +#include <catch2/catch_test_macros.hpp> |
| 38 | +#include <foleys_gui_magic/foleys_gui_magic.h> |
| 39 | + |
| 40 | + |
| 41 | +TEST_CASE ("GUI builder test", "[gui]") |
| 42 | +{ |
| 43 | + const juce::ScopedJuceInitialiser_GUI init; |
| 44 | + |
| 45 | + class TestComponent : public juce::Component |
| 46 | + { |
| 47 | + public: |
| 48 | + TestComponent() |
| 49 | + { |
| 50 | + auto tree = juce::ValueTree::fromXml ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 51 | + "\n" |
| 52 | + "<magic>\n" |
| 53 | + " <Styles>\n" |
| 54 | + " <Style name=\"default\">\n" |
| 55 | + " <Nodes/>\n" |
| 56 | + " <Classes>\n" |
| 57 | + " <hidden flex-grow=\"0\" active=\"HideEditor\">\n" |
| 58 | + " <media max-height=\"100000\" max-width=\"100000\"/>\n" |
| 59 | + " </hidden>\n" |
| 60 | + " </Classes>\n" |
| 61 | + " <Palettes>\n" |
| 62 | + " <default/>\n" |
| 63 | + " </Palettes>\n" |
| 64 | + " </Style>\n" |
| 65 | + " </Styles>\n" |
| 66 | + " <View>\n" |
| 67 | + " <View flex-direction=\"column\" caption=\"Hide Test\">\n" |
| 68 | + " <View>\n" |
| 69 | + " <View/>\n" |
| 70 | + " <ToggleButton id=\"hideEditor\" text=\"Hide Editor View\" property=\"HideEditor\"/>\n" |
| 71 | + " <View/>\n" |
| 72 | + " </View>\n" |
| 73 | + " <View>\n" |
| 74 | + " <View id=\"editor\" caption=\"Editor View\" class=\"hidden\"/>\n" |
| 75 | + " <View caption=\"Performance View\"/>\n" |
| 76 | + " </View>\n" |
| 77 | + " </View>\n" |
| 78 | + " </View>\n" |
| 79 | + "</magic>"); |
| 80 | + |
| 81 | + state.setGuiValueTree (tree); |
| 82 | + |
| 83 | + builder.registerJUCEFactories(); |
| 84 | + builder.createGUI (*this); |
| 85 | + } |
| 86 | + |
| 87 | + void resized() override { builder.updateLayout(); } |
| 88 | + |
| 89 | + foleys::MagicGUIState state; |
| 90 | + foleys::MagicGUIBuilder builder { state }; |
| 91 | + }; |
| 92 | + |
| 93 | + auto testComponent = std::make_unique<TestComponent>(); |
| 94 | + |
| 95 | + auto value = testComponent->state.getPropertyAsValue ("HideEditor"); |
| 96 | + value.setValue (false); |
| 97 | + |
| 98 | + auto flags = juce::ComponentPeer::windowAppearsOnTaskbar | juce::ComponentPeer::windowHasTitleBar; |
| 99 | + testComponent->addToDesktop (flags); |
| 100 | + testComponent->setVisible (true); |
| 101 | + |
| 102 | + testComponent->setSize (800, 600); |
| 103 | + |
| 104 | + auto* editorItem = testComponent->builder.findGuiItemWithId ("editor"); |
| 105 | + REQUIRE (editorItem); |
| 106 | + REQUIRE (editorItem->getWidth() > 200); |
| 107 | + REQUIRE (editorItem->getHeight() > 200); |
| 108 | + |
| 109 | + auto* buttonItem = testComponent->builder.findGuiItemWithId ("hideEditor"); |
| 110 | + REQUIRE (buttonItem); |
| 111 | + |
| 112 | + auto* button = dynamic_cast<juce::ToggleButton*> (buttonItem->getWrappedComponent()); |
| 113 | + REQUIRE (button); |
| 114 | + REQUIRE (button->getToggleState() == false); |
| 115 | + |
| 116 | + button->triggerClick(); |
| 117 | + |
| 118 | + juce::MessageManager::getInstance()->runDispatchLoopUntil (100); |
| 119 | + |
| 120 | + REQUIRE (bool (value.getValue()) == true); |
| 121 | + |
| 122 | + // Horizontal layout with grow=0 from class hidden should make it zero wide |
| 123 | + REQUIRE (editorItem->getWidth() == 0); |
| 124 | +} |
0 commit comments