ViewBookmarkTests.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/Settings/SettingsRegistryImpl.h>
  9. #include <AzTest/AzTest.h>
  10. #include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
  11. #include <AzToolsFramework/Viewport/LocalViewBookmarkComponent.h>
  12. #include <AzToolsFramework/Viewport/LocalViewBookmarkLoader.h>
  13. #include <Tests/Prefab/PrefabTestFixture.h>
  14. namespace UnitTest
  15. {
  16. struct LocalPersistentSettingsRegistry
  17. {
  18. AZStd::vector<char> m_buffer;
  19. };
  20. // fixture for testing the view bookmark save and load functionality
  21. class ViewBookmarkTestFixture : public PrefabTestFixture
  22. {
  23. public:
  24. void SetUpEditorFixtureImpl() override
  25. {
  26. PrefabTestFixture::SetUpEditorFixtureImpl();
  27. m_oldSettingsRegistry = AZ::SettingsRegistry::Get();
  28. if (m_oldSettingsRegistry)
  29. {
  30. AZ::SettingsRegistry::Unregister(m_oldSettingsRegistry);
  31. }
  32. m_settingsRegistry = AZStd::make_unique<AZ::SettingsRegistryImpl>();
  33. AZ::SettingsRegistry::Register(m_settingsRegistry.get());
  34. AZ::Entity* entity = nullptr;
  35. m_rootEntityId = UnitTest::CreateDefaultEditorEntity("Root", &entity);
  36. AzToolsFramework::ViewBookmarkPersistInterface* bookmarkPersistInterface =
  37. AZ::Interface<AzToolsFramework::ViewBookmarkPersistInterface>::Get();
  38. auto persistentSetReg = AZStd::make_shared<LocalPersistentSettingsRegistry>();
  39. bookmarkPersistInterface->OverrideStreamWriteFn(
  40. [persistentSetReg](
  41. const AZ::IO::PathView&, AZStd::string_view stringBuffer,
  42. AZStd::function<bool(AZ::IO::GenericStream&, const AZStd::string&)> write)
  43. {
  44. persistentSetReg->m_buffer.resize(stringBuffer.size() + 1);
  45. AZ::IO::MemoryStream memoryStream(persistentSetReg->m_buffer.data(), persistentSetReg->m_buffer.size(), 0);
  46. const bool saved = write(memoryStream, stringBuffer);
  47. EXPECT_THAT(saved, ::testing::IsTrue());
  48. return saved;
  49. });
  50. bookmarkPersistInterface->OverrideStreamReadFn(
  51. [persistentSetReg](const AZ::IO::PathView&)
  52. {
  53. return persistentSetReg->m_buffer;
  54. });
  55. bookmarkPersistInterface->OverrideFileExistsFn(
  56. [exists = false](const AZ::IO::PathView&) mutable
  57. {
  58. // initially does not exist and is then created
  59. return AZStd::exchange(exists, true);
  60. });
  61. }
  62. void TearDownEditorFixtureImpl() override
  63. {
  64. AZ::SettingsRegistry::Unregister(m_settingsRegistry.get());
  65. if (m_oldSettingsRegistry)
  66. {
  67. AZ::SettingsRegistry::Register(m_oldSettingsRegistry);
  68. m_oldSettingsRegistry = nullptr;
  69. }
  70. m_settingsRegistry.reset();
  71. PrefabTestFixture::TearDownEditorFixtureImpl();
  72. }
  73. AZStd::unique_ptr<AZ::SettingsRegistryInterface> m_settingsRegistry;
  74. AZ::SettingsRegistryInterface* m_oldSettingsRegistry = nullptr;
  75. AZ::EntityId m_rootEntityId;
  76. };
  77. TEST_F(ViewBookmarkTestFixture, ViewBookmarkInterfaceIsInstantiatedAsPartOfToolsApplication)
  78. {
  79. using ::testing::NotNull;
  80. AzToolsFramework::ViewBookmarkInterface* bookmarkInterface = AZ::Interface<AzToolsFramework::ViewBookmarkInterface>::Get();
  81. EXPECT_THAT(bookmarkInterface, NotNull());
  82. }
  83. TEST_F(ViewBookmarkTestFixture, ViewBookmarkLastKnownLocationIsNotFoundWithNoLevel)
  84. {
  85. using ::testing::IsFalse;
  86. AzToolsFramework::ViewBookmarkInterface* bookmarkInterface = AZ::Interface<AzToolsFramework::ViewBookmarkInterface>::Get();
  87. const AZStd::optional<AzToolsFramework::ViewBookmark> lastKnownLocationBookmark = bookmarkInterface->LoadLastKnownLocation();
  88. EXPECT_THAT(lastKnownLocationBookmark.has_value(), IsFalse());
  89. }
  90. TEST_F(ViewBookmarkTestFixture, ViewBookmarkLastKnownLocationCanBeStoredAndRetrieved)
  91. {
  92. using ::testing::IsFalse;
  93. AzToolsFramework::ViewBookmarkInterface* bookmarkInterface = AZ::Interface<AzToolsFramework::ViewBookmarkInterface>::Get();
  94. const auto cameraPosition = AZ::Vector3(0.0f, 20.0f, 12.0f);
  95. const auto expectedCameraRotationXDegrees = -35.0f;
  96. const auto expectedCameraRotationZDegrees = 90.0f;
  97. const AzFramework::CameraState cameraState = AzFramework::CreateDefaultCamera(
  98. AZ::Transform::CreateFromMatrix3x3AndTranslation(
  99. AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(expectedCameraRotationZDegrees)) *
  100. AZ::Matrix3x3::CreateRotationX(AZ::DegToRad(expectedCameraRotationXDegrees)),
  101. cameraPosition),
  102. AzFramework::ScreenSize(1280, 720));
  103. AzToolsFramework::StoreViewBookmarkLastKnownLocationFromCameraState(cameraState);
  104. const AZStd::optional<AzToolsFramework::ViewBookmark> lastKnownLocationBookmark = bookmarkInterface->LoadLastKnownLocation();
  105. EXPECT_THAT(lastKnownLocationBookmark->m_position, IsClose(cameraPosition));
  106. EXPECT_THAT(
  107. lastKnownLocationBookmark->m_rotation,
  108. IsClose(AZ::Vector3(expectedCameraRotationXDegrees, 0.0f, expectedCameraRotationZDegrees)));
  109. }
  110. TEST_F(ViewBookmarkTestFixture, ViewBookmarkCanBeStoredAndRetrievedAtIndex)
  111. {
  112. using ::testing::IsFalse;
  113. AzToolsFramework::ViewBookmarkInterface* bookmarkInterface = AZ::Interface<AzToolsFramework::ViewBookmarkInterface>::Get();
  114. const auto index = 4;
  115. const auto cameraPosition = AZ::Vector3(13.0f, 20.0f, 70.0f);
  116. const auto expectedCameraRotationXDegrees = 75.0f;
  117. const auto expectedCameraRotationZDegrees = 64.0f;
  118. const AzFramework::CameraState cameraState = AzFramework::CreateDefaultCamera(
  119. AZ::Transform::CreateFromMatrix3x3AndTranslation(
  120. AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(expectedCameraRotationZDegrees)) *
  121. AZ::Matrix3x3::CreateRotationX(AZ::DegToRad(expectedCameraRotationXDegrees)),
  122. cameraPosition),
  123. AzFramework::ScreenSize(1280, 720));
  124. AzToolsFramework::StoreViewBookmarkFromCameraStateAtIndex(index, cameraState);
  125. const AZStd::optional<AzToolsFramework::ViewBookmark> lastKnownLocationBookmark = bookmarkInterface->LoadBookmarkAtIndex(4);
  126. #if AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  127. // Expected: (X: 75, Y: 0, Z: 64)
  128. // Actual: (X: 74.9989, Y: -0, Z: 64)
  129. // Delta: 0.0011 0, 0
  130. constexpr float cameraPositionTolerance = 0.0012f;
  131. EXPECT_THAT(lastKnownLocationBookmark->m_position, IsCloseTolerance(cameraPosition, cameraPositionTolerance));
  132. #else
  133. EXPECT_THAT(lastKnownLocationBookmark->m_position, IsClose(cameraPosition));
  134. #endif // AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  135. #if AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  136. // Expected: (X: 75, Y: 0, Z: 64)
  137. // Actual: (X: 74.9989, Y: -0, Z: 64)
  138. // Delta: 0.0011 0, 0
  139. constexpr float cameraRotationTolerance = 0.0012f;
  140. EXPECT_THAT(
  141. lastKnownLocationBookmark->m_rotation,
  142. IsCloseTolerance(AZ::Vector3(expectedCameraRotationXDegrees, 0.0f, expectedCameraRotationZDegrees), cameraRotationTolerance));
  143. #else
  144. EXPECT_THAT(
  145. lastKnownLocationBookmark->m_rotation,
  146. IsClose(AZ::Vector3(expectedCameraRotationXDegrees, 0.0f, expectedCameraRotationZDegrees)));
  147. #endif // AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  148. }
  149. } // namespace UnitTest