diff --git a/tests/plugin_tests/chowdsp_plugin_state_test/ParamHolderTest.cpp b/tests/plugin_tests/chowdsp_plugin_state_test/ParamHolderTest.cpp index a872c6ed..fb1e629b 100644 --- a/tests/plugin_tests/chowdsp_plugin_state_test/ParamHolderTest.cpp +++ b/tests/plugin_tests/chowdsp_plugin_state_test/ParamHolderTest.cpp @@ -3,21 +3,20 @@ TEST_CASE ("ParamHolder Test", "[plugin][state]") { - SECTION ("add()") - { - chowdsp::ParamHolder params; - std::array floatParams { - chowdsp::PercentParameter::Ptr { "param3", "Param", 0.5f }, - chowdsp::PercentParameter::Ptr { "param4", "Param", 0.5f }, - }; - - chowdsp::BoolParameter::Ptr boolNested { "param1", "Param", false }; - chowdsp::ChoiceParameter::Ptr choiceNested { "param2", "Param", juce::StringArray { "One", "Two" }, 0 }; - chowdsp::ParamHolder nestedParams { ¶ms }; - nestedParams.add (boolNested, choiceNested); + std::array floatParams { + chowdsp::PercentParameter::Ptr { "param3", "Param", 0.5f }, + chowdsp::PercentParameter::Ptr { "param4", "Param", 0.5f }, + }; + chowdsp::BoolParameter::Ptr boolNested { "param1", "Param", false }; + chowdsp::ChoiceParameter::Ptr choiceNested { "param2", "Param", juce::StringArray { "One", "Two" }, 0 }; - params.add (nestedParams, floatParams); + chowdsp::ParamHolder params; + chowdsp::ParamHolder nestedParams { ¶ms }; + nestedParams.add (boolNested, choiceNested); + params.add (nestedParams, floatParams); + SECTION ("add()") + { auto allParamIDs = [¶ms] { juce::StringArray allIDs; @@ -31,4 +30,55 @@ TEST_CASE ("ParamHolder Test", "[plugin][state]") REQUIRE (allParamIDs.contains (floatParams[0]->paramID)); REQUIRE (allParamIDs.contains (floatParams[1]->paramID)); } + + SECTION ("Serialize JSON") + { + using namespace chowdsp::ParameterTypeHelpers; + setValue (0.0f, *floatParams[0]); + setValue (1.0f, *floatParams[1]); + setValue (true, *boolNested); + setValue (1, *choiceNested); + + const auto json_state = chowdsp::ParamHolder::serialize_json (params); + params.doForAllParameters ([] (auto& param, size_t) { setValue (getDefaultValue (param), param); }); + + REQUIRE (getValue (*floatParams[0]) == 0.5f); + REQUIRE (getValue (*floatParams[1]) == 0.5f); + REQUIRE (getValue (*boolNested) == false); + REQUIRE (getValue (*choiceNested) == 0); + + chowdsp::ParamHolder::deserialize_json (json_state, params); + REQUIRE (getValue (*floatParams[0]) == 0.0f); + REQUIRE (getValue (*floatParams[1]) == 1.0f); + REQUIRE (getValue (*boolNested) == true); + REQUIRE (getValue (*choiceNested) == 1); + } + + SECTION ("Serialize Bytes") + { + using namespace chowdsp::ParameterTypeHelpers; + setValue (0.0f, *floatParams[0]); + setValue (1.0f, *floatParams[1]); + setValue (true, *boolNested); + setValue (1, *choiceNested); + + chowdsp::ChainedArenaAllocator arena { 128 }; + chowdsp::ParamHolder::serialize (arena, params); + juce::MemoryBlock state {}; + chowdsp::dump_serialized_bytes (state, arena); + + params.doForAllParameters ([] (auto& param, size_t) { setValue (getDefaultValue (param), param); }); + + REQUIRE (getValue (*floatParams[0]) == 0.5f); + REQUIRE (getValue (*floatParams[1]) == 0.5f); + REQUIRE (getValue (*boolNested) == false); + REQUIRE (getValue (*choiceNested) == 0); + + nonstd::span state_data = { (const std::byte*) state.getData(), state.getSize() }; + chowdsp::ParamHolder::deserialize (state_data, params); + REQUIRE (getValue (*floatParams[0]) == 0.0f); + REQUIRE (getValue (*floatParams[1]) == 1.0f); + REQUIRE (getValue (*boolNested) == true); + REQUIRE (getValue (*choiceNested) == 1); + } } diff --git a/tests/plugin_tests/chowdsp_presets_v2_test/PresetManagerTest.cpp b/tests/plugin_tests/chowdsp_presets_v2_test/PresetManagerTest.cpp index ce81ae41..35114c4b 100644 --- a/tests/plugin_tests/chowdsp_presets_v2_test/PresetManagerTest.cpp +++ b/tests/plugin_tests/chowdsp_presets_v2_test/PresetManagerTest.cpp @@ -241,6 +241,27 @@ TEST_CASE ("Preset Manager Test", "[plugin][presets][state]") REQUIRE (presetMgr->getIsPresetDirty()); } + SECTION ("Null State JSON Test") + { + static constexpr float otherValue = 0.15f; + + chowdsp::json json_state; + { + ScopedPresetManager presetMgr {}; + + presetMgr.setFloatParam (otherValue); + REQUIRE (juce::approximatelyEqual (presetMgr.getFloatParam(), otherValue)); + + json_state = decltype (presetMgr.state)::serialize (presetMgr.state); + } + + ScopedPresetManager presetMgr {}; + decltype (presetMgr.state)::deserialize (json_state, presetMgr.state); + REQUIRE_MESSAGE (juce::approximatelyEqual (presetMgr.getFloatParam(), otherValue), "Preset state is overriding parameter state!"); + REQUIRE (presetMgr->getCurrentPreset() == nullptr); + REQUIRE (presetMgr->getIsPresetDirty()); + } + SECTION ("Preset State Test") { static constexpr float testValue = 0.05f; @@ -267,6 +288,32 @@ TEST_CASE ("Preset Manager Test", "[plugin][presets][state]") REQUIRE (presetMgr->getIsPresetDirty()); } + SECTION ("Preset State JSON Test") + { + static constexpr float testValue = 0.05f; + static constexpr float otherValue = 0.15f; + auto preset = saveUserPreset ("test.preset", testValue); + + chowdsp::json json_state; + { + ScopedPresetManager presetMgr {}; + presetMgr->addPresets ({ chowdsp::presets::Preset { preset } }); + presetMgr->loadPreset (presetMgr->getPresetTree().getRootNode().first_child->value.leaf()); + REQUIRE (juce::approximatelyEqual (presetMgr.getFloatParam(), testValue)); + + presetMgr.setFloatParam (otherValue); + REQUIRE (juce::approximatelyEqual (presetMgr.getFloatParam(), otherValue)); + + json_state = decltype (presetMgr.state)::serialize (presetMgr.state); + } + + ScopedPresetManager presetMgr {}; + decltype (presetMgr.state)::deserialize (json_state, presetMgr.state); + REQUIRE_MESSAGE (juce::approximatelyEqual (presetMgr.getFloatParam(), otherValue), "Preset state is overriding parameter state!"); + REQUIRE (*presetMgr->getCurrentPreset() == preset); + REQUIRE (presetMgr->getIsPresetDirty()); + } + SECTION ("Preset Undo/Redo Test") { static constexpr float testValue = 0.05f;