CvarAdapterTests.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Console/Console.h>
  9. #include <AzCore/Console/IConsole.h>
  10. #include <AzCore/Console/IConsoleTypes.h>
  11. #include <AzCore/Interface/Interface.h>
  12. #include <AzFramework/DocumentPropertyEditor/AdapterBuilder.h>
  13. #include <AzFramework/DocumentPropertyEditor/CvarAdapter.h>
  14. #include <AzFramework/DocumentPropertyEditor/PropertyEditorNodes.h>
  15. #include <Tests/DocumentPropertyEditor/DocumentPropertyEditorFixture.h>
  16. namespace AZ::DocumentPropertyEditor::Tests
  17. {
  18. AZ_CVAR(int32_t, dpe_TestInt, 1, nullptr, AZ::ConsoleFunctorFlags::DontReplicate, "DPE test int32");
  19. AZ_CVAR(uint8_t, dpe_TestUint, 22, nullptr, AZ::ConsoleFunctorFlags::DontReplicate, "DPE test uint8");
  20. AZ_CVAR(double, dpe_TestDbl, 4.0, nullptr, AZ::ConsoleFunctorFlags::DontReplicate, "DPE test double");
  21. AZ_CVAR(CVarFixedString, dpe_TestString, "test", nullptr, AZ::ConsoleFunctorFlags::DontReplicate, "DPE test string");
  22. AZ_CVAR(bool, dpe_TestBool, false, nullptr, AZ::ConsoleFunctorFlags::DontReplicate, "DPE test bool");
  23. AZ_CVAR(AZ::Vector2, dpe_TestVec2, AZ::Vector2::CreateZero(), nullptr, AZ::ConsoleFunctorFlags::DontReplicate, "DPE test vec2");
  24. AZ_CVAR(AZ::Vector3, dpe_TestVec3, AZ::Vector3::CreateOne(), nullptr, AZ::ConsoleFunctorFlags::DontReplicate, "DPE test vec3");
  25. AZ_CVAR(AZ::Color, dpe_TestColor, AZ::Color(), nullptr, ConsoleFunctorFlags::DontReplicate, "DPE test color");
  26. class CvarAdapterDpeTests : public DocumentPropertyEditorTestFixture
  27. {
  28. public:
  29. void SetUp() override
  30. {
  31. DocumentPropertyEditorTestFixture::SetUp();
  32. m_console = AZStd::make_unique<AZ::Console>();
  33. m_console->LinkDeferredFunctors(AZ::ConsoleFunctorBase::GetDeferredHead());
  34. AZ::Interface<AZ::IConsole>::Register(m_console.get());
  35. m_adapter = AZStd::make_unique<CvarAdapter>();
  36. }
  37. void TearDown() override
  38. {
  39. m_adapter.reset();
  40. AZ::Interface<AZ::IConsole>::Unregister(m_console.get());
  41. m_console.reset();
  42. DocumentPropertyEditorTestFixture::TearDown();
  43. }
  44. Dom::Value GetEntryRow(AZStd::string_view cvarName)
  45. {
  46. Dom::Value rows = m_adapter->GetContents();
  47. for (auto it = rows.ArrayBegin(); it != rows.ArrayEnd(); ++it)
  48. {
  49. Dom::Value label = (*it)[0][Nodes::Label::Value.GetName()];
  50. if (label.GetString() == cvarName)
  51. {
  52. return *it;
  53. }
  54. }
  55. return {};
  56. }
  57. Dom::Value GetEntryValue(AZStd::string_view cvarName)
  58. {
  59. Dom::Value row = GetEntryRow(cvarName);
  60. EXPECT_FALSE(row.IsNull());
  61. return row[1][Nodes::PropertyEditor::Value.GetName()];
  62. }
  63. template <typename T>
  64. T GetTypedEntryValue(AZStd::string_view cvarName)
  65. {
  66. return Dom::Utils::ValueToType<T>(GetEntryValue(cvarName)).value();
  67. }
  68. void SetEntryValue(AZStd::string_view cvarName, Dom::Value value)
  69. {
  70. Dom::Value row = GetEntryRow(cvarName);
  71. ASSERT_FALSE(row.IsNull());
  72. auto result = Nodes::PropertyEditor::OnChanged.InvokeOnDomNode(row[1], value, Nodes::ValueChangeType::FinishedEdit);
  73. EXPECT_TRUE(result.IsSuccess());
  74. AZ_Error("CvarAdapterDpeTests", result.IsSuccess(), "%s", result.GetError().c_str());
  75. }
  76. template <typename T>
  77. void SetTypedEntryValue(AZStd::string_view cvarName, const T& value)
  78. {
  79. SetEntryValue(cvarName, Dom::Utils::ValueFromType(value));
  80. }
  81. AZStd::unique_ptr<AZ::Console> m_console;
  82. AZStd::unique_ptr<CvarAdapter> m_adapter;
  83. };
  84. TEST_F(CvarAdapterDpeTests, IntCvar)
  85. {
  86. EXPECT_EQ(static_cast<int32_t>(dpe_TestInt), GetEntryValue("dpe_TestInt").GetInt64());
  87. SetEntryValue("dpe_TestInt", Dom::Value(42));
  88. EXPECT_EQ(42, dpe_TestInt);
  89. EXPECT_EQ(42, GetEntryValue("dpe_TestInt").GetInt64());
  90. }
  91. TEST_F(CvarAdapterDpeTests, UintCvar)
  92. {
  93. EXPECT_EQ(static_cast<uint8_t>(dpe_TestUint), GetEntryValue("dpe_TestUint").GetUint64());
  94. SetEntryValue("dpe_TestUint", Dom::Value(42));
  95. EXPECT_EQ(AZ::u8(42), dpe_TestUint);
  96. EXPECT_EQ(42, GetEntryValue("dpe_TestUint").GetUint64());
  97. }
  98. TEST_F(CvarAdapterDpeTests, DoubleCvar)
  99. {
  100. EXPECT_EQ(static_cast<double>(dpe_TestDbl), GetEntryValue("dpe_TestDbl").GetDouble());
  101. SetEntryValue("dpe_TestDbl", Dom::Value(6.28));
  102. EXPECT_EQ(6.28, dpe_TestDbl);
  103. EXPECT_EQ(6.28, GetEntryValue("dpe_TestDbl").GetDouble());
  104. }
  105. TEST_F(CvarAdapterDpeTests, StringCvar)
  106. {
  107. EXPECT_EQ(static_cast<CVarFixedString>(dpe_TestString), GetEntryValue("dpe_TestString").GetString());
  108. SetEntryValue("dpe_TestString", Dom::Value("new string", true));
  109. EXPECT_EQ("new string", static_cast<CVarFixedString>(dpe_TestString));
  110. EXPECT_EQ("new string", GetEntryValue("dpe_TestString").GetString());
  111. }
  112. TEST_F(CvarAdapterDpeTests, BoolCvar)
  113. {
  114. EXPECT_EQ(static_cast<bool>(dpe_TestBool), GetEntryValue("dpe_TestBool").GetBool());
  115. SetEntryValue("dpe_TestBool", Dom::Value(true));
  116. EXPECT_EQ(true, static_cast<bool>(dpe_TestBool));
  117. EXPECT_EQ(true, GetEntryValue("dpe_TestBool").GetBool());
  118. }
  119. TEST_F(CvarAdapterDpeTests, Vec2Cvar)
  120. {
  121. AZ::Vector2 value = GetTypedEntryValue<AZ::Vector2>("dpe_TestVec2");
  122. EXPECT_EQ(static_cast<AZ::Vector2>(dpe_TestVec2), value);
  123. const AZ::Vector2 newValue(2.0, 4.5);
  124. SetTypedEntryValue("dpe_TestVec2", newValue);
  125. value = GetTypedEntryValue<AZ::Vector2>("dpe_TestVec2");
  126. EXPECT_EQ(newValue, value);
  127. EXPECT_EQ(newValue, static_cast<AZ::Vector2>(dpe_TestVec2));
  128. }
  129. TEST_F(CvarAdapterDpeTests, Vec3Cvar)
  130. {
  131. AZ::Vector3 value = GetTypedEntryValue<AZ::Vector3>("dpe_TestVec3");
  132. EXPECT_EQ(static_cast<AZ::Vector3>(dpe_TestVec3), value);
  133. const AZ::Vector3 newValue(1.0, 0.0, 2.25);
  134. SetTypedEntryValue("dpe_TestVec3", newValue);
  135. value = GetTypedEntryValue<AZ::Vector3>("dpe_TestVec3");
  136. EXPECT_EQ(newValue, value);
  137. EXPECT_EQ(newValue, static_cast<AZ::Vector3>(dpe_TestVec3));
  138. }
  139. TEST_F(CvarAdapterDpeTests, ColorCvar)
  140. {
  141. AZ::Color value = GetTypedEntryValue<AZ::Color>("dpe_TestColor");
  142. EXPECT_EQ(static_cast<AZ::Color>(dpe_TestColor), value);
  143. const AZ::Color newValue(0.0f, 1.0f, 0.5f, 0.25f);
  144. SetTypedEntryValue("dpe_TestColor", newValue);
  145. value = GetTypedEntryValue<AZ::Color>("dpe_TestColor");
  146. EXPECT_EQ(newValue, value);
  147. EXPECT_EQ(newValue, static_cast<AZ::Color>(dpe_TestColor));
  148. }
  149. } // namespace AZ::DocumentPropertyEditor::Tests