ThumbnailerTests.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 <AzTest/AzTest.h>
  9. #include <AzCore/UnitTest/TestTypes.h>
  10. #include <AzToolsFramework/Thumbnails/ThumbnailerComponent.h>
  11. #include <AzCore/UserSettings/UserSettingsComponent.h>
  12. #include <AzToolsFramework/Application/ToolsApplication.h>
  13. #include <AzToolsFramework/Entity/EditorEntityContextComponent.h>
  14. #include <AzToolsFramework/Entity/EditorEntityHelpers.h>
  15. #include <AzToolsFramework/UnitTest/ToolsTestApplication.h>
  16. namespace UnitTest
  17. {
  18. using namespace AzToolsFramework::Thumbnailer;
  19. class ThumbnailerTests
  20. : public ::testing::Test
  21. {
  22. protected:
  23. void SetUp() override
  24. {
  25. AZ::ComponentApplication::StartupParameters startupParameters;
  26. startupParameters.m_loadSettingsRegistry = false;
  27. m_app.Start(m_descriptor, startupParameters);
  28. // Without this, the user settings component would sometimes attempt to save
  29. // changes on shutdown. In some cases this would cause a crash while the unit test
  30. // was running, because the environment wasn't setup for it to save these settings.
  31. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  32. AZStd::string entityName("test");
  33. AZ::EntityId testEntityId;
  34. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(
  35. testEntityId,
  36. &AzToolsFramework::EditorEntityContextRequestBus::Events::CreateNewEditorEntity,
  37. entityName.c_str());
  38. m_testEntity = AzToolsFramework::GetEntityById(testEntityId);
  39. ASSERT_TRUE(m_testEntity);
  40. AZ::Component* thumbnailerComponent = nullptr;
  41. AZ::ComponentDescriptorBus::EventResult(thumbnailerComponent, azrtti_typeid<AzToolsFramework::Thumbnailer::ThumbnailerComponent>(), &AZ::ComponentDescriptorBus::Events::CreateComponent);
  42. ASSERT_TRUE(thumbnailerComponent);
  43. if (m_testEntity->GetState() == AZ::Entity::State::Active)
  44. {
  45. m_testEntity->Deactivate();
  46. }
  47. ASSERT_TRUE(m_testEntity->AddComponent(thumbnailerComponent));
  48. m_testEntity->Activate();
  49. }
  50. void TearDown() override
  51. {
  52. AzToolsFramework::EditorEntityContextRequestBus::Broadcast(
  53. &AzToolsFramework::EditorEntityContextRequestBus::Events::DestroyEditorEntity,
  54. m_testEntity->GetId());
  55. m_app.Stop();
  56. }
  57. ToolsTestApplication m_app{ "ThumbnailerTests" };
  58. AZ::ComponentApplication::Descriptor m_descriptor;
  59. AZ::Entity* m_testEntity = nullptr;
  60. };
  61. } // namespace UnitTest