SettingsTests.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/Serialization/SerializeContext.h>
  9. #include <AzCore/Serialization/Json/RegistrationContext.h>
  10. #include <AzCore/Settings/SettingsRegistryImpl.h>
  11. #include <AzCore/Serialization/Json/JsonSystemComponent.h>
  12. #include <AzCore/UnitTest/TestTypes.h>
  13. #include <AzTest/Utils.h>
  14. #include <Settings.h>
  15. namespace O3DE::ProjectManager
  16. {
  17. class SettingsTests
  18. : public ::UnitTest::LeakDetectionFixture
  19. {
  20. public:
  21. ~SettingsTests() override = default;
  22. void SetUp() override
  23. {
  24. UnitTest::LeakDetectionFixture::SetUp();
  25. m_registry = AZStd::make_unique<AZ::SettingsRegistryImpl>();
  26. // Store off the old global settings registry to restore after each test
  27. m_oldSettingsRegistry = AZ::SettingsRegistry::Get();
  28. if (m_oldSettingsRegistry != nullptr)
  29. {
  30. AZ::SettingsRegistry::Unregister(m_oldSettingsRegistry);
  31. }
  32. AZ::SettingsRegistry::Register(m_registry.get());
  33. m_serializeContext = AZStd::make_unique<AZ::SerializeContext>();
  34. m_registrationContext = AZStd::make_unique<AZ::JsonRegistrationContext>();
  35. m_registry->SetContext(m_serializeContext.get());
  36. m_registry->SetContext(m_registrationContext.get());
  37. AZ::JsonSystemComponent::Reflect(m_registrationContext.get());
  38. m_serializeContext->RegisterGenericType<AZStd::set<AZStd::string>>();
  39. m_settings = AZStd::make_unique<Settings>(/*saveToDisk*/ false);
  40. m_projectInfo.m_path = "Z:/ProjectTestPath";
  41. }
  42. void TearDown() override
  43. {
  44. m_settings.reset();
  45. m_registrationContext->EnableRemoveReflection();
  46. AZ::JsonSystemComponent::Reflect(m_registrationContext.get());
  47. m_registrationContext->DisableRemoveReflection();
  48. m_registrationContext.reset();
  49. m_serializeContext.reset();
  50. // Restore the old global settings registry
  51. AZ::SettingsRegistry::Unregister(m_registry.get());
  52. if (m_oldSettingsRegistry != nullptr)
  53. {
  54. AZ::SettingsRegistry::Register(m_oldSettingsRegistry);
  55. m_oldSettingsRegistry = nullptr;
  56. }
  57. m_registry.reset();
  58. UnitTest::LeakDetectionFixture::TearDown();
  59. }
  60. protected:
  61. AZStd::unique_ptr<Settings> m_settings;
  62. const QString m_settingsPath = "/Testing/TestKey";
  63. const QString m_newSettingsPath = "/Testing/NewTestKey";
  64. ProjectInfo m_projectInfo;
  65. private:
  66. AZ::SettingsRegistryInterface* m_oldSettingsRegistry = nullptr;
  67. AZStd::unique_ptr<AZ::SettingsRegistryImpl> m_registry;
  68. AZStd::unique_ptr<AZ::SerializeContext> m_serializeContext;
  69. AZStd::unique_ptr<AZ::JsonRegistrationContext> m_registrationContext;
  70. };
  71. TEST_F(SettingsTests, Settings_GetUnsetPathBool_ReturnsFalse)
  72. {
  73. bool settingsResult = false;
  74. EXPECT_FALSE(m_settings->Get(settingsResult, m_settingsPath));
  75. EXPECT_FALSE(settingsResult);
  76. }
  77. TEST_F(SettingsTests, Settings_SetAndGetValueBool_Success)
  78. {
  79. bool settingsResult = false;
  80. EXPECT_FALSE(m_settings->Get(settingsResult, m_settingsPath));
  81. EXPECT_TRUE(m_settings->Set(m_settingsPath, true));
  82. settingsResult = false;
  83. EXPECT_TRUE(m_settings->Get(settingsResult, m_settingsPath));
  84. EXPECT_TRUE(settingsResult);
  85. }
  86. TEST_F(SettingsTests, Settings_GetUnsetPathString_ReturnsFalse)
  87. {
  88. QString settingsResult;
  89. EXPECT_FALSE(m_settings->Get(settingsResult, m_settingsPath));
  90. EXPECT_TRUE(settingsResult.isEmpty());
  91. }
  92. TEST_F(SettingsTests, Settings_SetAndGetValueString_Success)
  93. {
  94. QString settingsResult;
  95. EXPECT_FALSE(m_settings->Get(settingsResult, m_settingsPath));
  96. QString settingsValue = "TestValue";
  97. EXPECT_TRUE(m_settings->Set(m_settingsPath, settingsValue));
  98. EXPECT_TRUE(m_settings->Get(settingsResult, m_settingsPath));
  99. EXPECT_TRUE(settingsResult == settingsValue);
  100. }
  101. TEST_F(SettingsTests, Settings_CopyStringRemoveOriginal_SuccessAndRemovesOriginal)
  102. {
  103. QString settingsResult;
  104. EXPECT_FALSE(m_settings->Get(settingsResult, m_newSettingsPath));
  105. QString settingsValue = "TestValue";
  106. EXPECT_TRUE(m_settings->Set(m_settingsPath, settingsValue));
  107. EXPECT_TRUE(m_settings->Copy(m_settingsPath, m_newSettingsPath, /*removeOrig*/ true));
  108. // Check that old path value is removed
  109. EXPECT_FALSE(m_settings->Get(settingsResult, m_settingsPath));
  110. EXPECT_TRUE(m_settings->Get(settingsResult, m_newSettingsPath));
  111. EXPECT_TRUE(settingsResult == settingsValue);
  112. }
  113. TEST_F(SettingsTests, Settings_RemoveProjectManagerKey_RemovesKey)
  114. {
  115. QString settingsResult;
  116. EXPECT_FALSE(m_settings->Get(settingsResult, m_settingsPath));
  117. QString settingsValue = "TestValue";
  118. EXPECT_TRUE(m_settings->Set(m_settingsPath, settingsValue));
  119. EXPECT_TRUE(m_settings->Get(settingsResult, m_settingsPath));
  120. EXPECT_TRUE(m_settings->Remove(m_settingsPath));
  121. EXPECT_FALSE(m_settings->Get(settingsResult, m_settingsPath));
  122. }
  123. TEST_F(SettingsTests, Settings_GetUnsetBuildPath_ReturnsFalse)
  124. {
  125. bool buildResult = true;
  126. EXPECT_FALSE(m_settings->GetProjectBuiltSuccessfully(buildResult, m_projectInfo));
  127. EXPECT_FALSE(buildResult);
  128. }
  129. TEST_F(SettingsTests, Settings_SetProjectBuiltSuccessfully_ReturnsTrue)
  130. {
  131. EXPECT_TRUE(m_settings->SetProjectBuiltSuccessfully(m_projectInfo, true));
  132. bool buildResult = false;
  133. EXPECT_TRUE(m_settings->GetProjectBuiltSuccessfully(buildResult, m_projectInfo));
  134. EXPECT_TRUE(buildResult);
  135. }
  136. TEST_F(SettingsTests, Settings_SetProjectBuiltUnsuccessfully_ReturnsFalse)
  137. {
  138. EXPECT_TRUE(m_settings->SetProjectBuiltSuccessfully(m_projectInfo, false));
  139. bool buildResult = false;
  140. EXPECT_TRUE(m_settings->GetProjectBuiltSuccessfully(buildResult, m_projectInfo));
  141. EXPECT_FALSE(buildResult);
  142. }
  143. }