DeviceAttributeSystemComponentTests.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/UnitTest/TestTypes.h>
  9. #include <AzCore/std/any.h>
  10. #include <AzCore/std/smart_ptr/make_shared.h>
  11. #include <AzCore/std/smart_ptr/shared_ptr.h>
  12. #include <AzCore/std/smart_ptr/unique_ptr.h>
  13. #include <AzCore/std/string/string.h>
  14. #include <AzCore/Settings/SettingsRegistry.h>
  15. #include <AzCore/Settings/SettingsRegistryImpl.h>
  16. #include <AzFramework/Device/DeviceAttributesSystemComponent.h>
  17. #include "DeviceAttribute/TestDeviceAttribute.h"
  18. namespace UnitTest
  19. {
  20. class DeviceAttributesSystemComponentTestFixture
  21. : public LeakDetectionFixture
  22. {
  23. public:
  24. DeviceAttributesSystemComponentTestFixture()
  25. : LeakDetectionFixture()
  26. {
  27. }
  28. protected:
  29. void SetUp() override
  30. {
  31. m_settingsRegistry = AZStd::make_unique<AZ::SettingsRegistryImpl>();
  32. AZ::SettingsRegistry::Register(m_settingsRegistry.get());
  33. m_deviceAttributesSystemComponent = AZStd::make_unique<AzFramework::DeviceAttributesSystemComponent>();
  34. m_deviceAttributesSystemComponent->Activate();
  35. }
  36. void TearDown() override
  37. {
  38. m_deviceAttributesSystemComponent->Deactivate();
  39. m_deviceAttributesSystemComponent.reset();
  40. AZ::SettingsRegistry::Unregister(m_settingsRegistry.get());
  41. m_settingsRegistry.reset();
  42. }
  43. AZStd::unique_ptr<AzFramework::DeviceAttributesSystemComponent> m_deviceAttributesSystemComponent;
  44. AZStd::unique_ptr<AZ::SettingsRegistryInterface> m_settingsRegistry;
  45. };
  46. TEST_F(DeviceAttributesSystemComponentTestFixture, DeviceAttributesSystem_Can_Register_Device_Attributes)
  47. {
  48. AZStd::string name { "TestAttribute" };
  49. AZStd::string description { "Description" };
  50. AZStd::string valueString{ "ABC123" };
  51. AZStd::any value{ valueString };
  52. TestDeviceAttribute::EvalFunc eval = [](AZStd::string_view rule)
  53. {
  54. // simple string comparison evaluator
  55. return rule == "True";
  56. };
  57. auto deviceAttribute = AZStd::make_shared<TestDeviceAttribute>(name, description, value, eval);
  58. // when the device attribute registrar exists
  59. auto registrar = AzFramework::DeviceAttributeRegistrar::Get();
  60. ASSERT_NE(nullptr, registrar);
  61. // expect a device attribute can be registered
  62. EXPECT_TRUE(registrar->RegisterDeviceAttribute(deviceAttribute));
  63. // expect the registered attribute can be found
  64. auto foundAttribute = registrar->FindDeviceAttribute(name);
  65. EXPECT_NE(nullptr, foundAttribute);
  66. auto foundValue = foundAttribute->GetValue();
  67. EXPECT_EQ(azrtti_typeid<AZStd::string>(), foundValue.type());
  68. EXPECT_EQ(valueString, AZStd::any_cast<AZStd::string>(foundValue));
  69. EXPECT_EQ(description, foundAttribute->GetDescription());
  70. EXPECT_EQ(name, foundAttribute->GetName());
  71. EXPECT_TRUE(foundAttribute->Evaluate("True"));
  72. EXPECT_FALSE(foundAttribute->Evaluate("Not true"));
  73. // when another device attribute with the same attribute name is registered
  74. auto deviceAttributeDuplicate = AZStd::make_shared<TestDeviceAttribute>(name, description, value, eval);
  75. // expect failure because attribute names must be unique
  76. EXPECT_FALSE(registrar->RegisterDeviceAttribute(deviceAttributeDuplicate));
  77. // expect device attribute can be removed
  78. EXPECT_TRUE(registrar->UnregisterDeviceAttribute(deviceAttribute->GetName()));
  79. // expect the alternate device attribute can be registered now
  80. EXPECT_TRUE(registrar->RegisterDeviceAttribute(deviceAttributeDuplicate));
  81. // expect alternate device attribute can be removed
  82. EXPECT_TRUE(registrar->UnregisterDeviceAttribute(deviceAttributeDuplicate->GetName()));
  83. // expect alternate attribute is not found after removal
  84. EXPECT_EQ(nullptr, registrar->FindDeviceAttribute(deviceAttributeDuplicate->GetName()));
  85. }
  86. TEST_F(DeviceAttributesSystemComponentTestFixture, DeviceAttributesSystem_Registers_Common_Device_Attributes)
  87. {
  88. // when the device attributes system component is activated
  89. // expect a device attribute registrar exists
  90. auto registrar = AzFramework::DeviceAttributeRegistrar::Get();
  91. ASSERT_NE(registrar, nullptr);
  92. // expect common attributes are registered
  93. auto deviceModel = registrar->FindDeviceAttribute("DeviceModel");
  94. EXPECT_NE(nullptr, deviceModel);
  95. auto deviceModelValue = deviceModel->GetValue();
  96. ASSERT_TRUE(deviceModelValue.is<AZStd::string>());
  97. EXPECT_FALSE(AZStd::any_cast<AZStd::string>(deviceModelValue).empty());
  98. auto ram = registrar->FindDeviceAttribute("RAM");
  99. EXPECT_NE(nullptr, ram);
  100. auto ramValue = ram->GetValue();
  101. ASSERT_TRUE(ramValue.is<float>());
  102. EXPECT_GT(AZStd::any_cast<float>(ramValue), 0);
  103. }
  104. } // namespace UnitTest