PrefabEditorEntityNotificationTests.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <AzFramework/Entity/EntityOwnershipServiceBus.h>
  9. #include <AzToolsFramework/Entity/EditorEntityContextBus.h>
  10. #include <AzToolsFramework/Entity/EditorEntityHelpers.h>
  11. #include <Prefab/PrefabTestFixture.h>
  12. namespace UnitTest
  13. {
  14. class EditorEntityContextNotificationOrderingDetector
  15. : private AzToolsFramework::EditorEntityContextNotificationBus::Handler
  16. , private AzFramework::EntityOwnershipServiceNotificationBus::Handler
  17. {
  18. public:
  19. void Connect();
  20. void Disconnect();
  21. // EditorEntityContextNotificationBus overrides ...
  22. void OnPrepareForContextReset() override;
  23. void OnContextReset() override;
  24. bool m_prepareForContextReset = false;
  25. bool m_contextReset = false;
  26. };
  27. void EditorEntityContextNotificationOrderingDetector::Connect()
  28. {
  29. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusConnect();
  30. AzFramework::EntityOwnershipServiceNotificationBus::Handler::BusDisconnect();
  31. }
  32. void EditorEntityContextNotificationOrderingDetector::Disconnect()
  33. {
  34. AzFramework::EntityOwnershipServiceNotificationBus::Handler::BusDisconnect();
  35. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusDisconnect();
  36. }
  37. void EditorEntityContextNotificationOrderingDetector::OnPrepareForContextReset()
  38. {
  39. EXPECT_THAT(m_contextReset, ::testing::IsFalse());
  40. EXPECT_THAT(m_prepareForContextReset, ::testing::IsFalse());
  41. m_prepareForContextReset = true;
  42. }
  43. void EditorEntityContextNotificationOrderingDetector::OnContextReset()
  44. {
  45. EXPECT_THAT(m_contextReset, ::testing::IsFalse());
  46. EXPECT_THAT(m_prepareForContextReset, ::testing::IsTrue());
  47. m_contextReset = true;
  48. }
  49. class EditorEntityContextNotificationFixture : public LeakDetectionFixture
  50. {
  51. public:
  52. EditorEntityContextNotificationOrderingDetector m_editorEntityContextNotificationOrderingDetector;
  53. };
  54. TEST_F(EditorEntityContextNotificationFixture, EditorEntityContextNotificationsReceivedInCorrectOrder)
  55. {
  56. auto app = AZStd::make_unique<ToolsTestApplication>("DummyApplication");
  57. AZ::ComponentApplication::StartupParameters startupParameters;
  58. startupParameters.m_loadAssetCatalog = false;
  59. app->Start(AzFramework::Application::Descriptor(), startupParameters);
  60. // without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
  61. // shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
  62. // in the unit tests.
  63. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  64. m_editorEntityContextNotificationOrderingDetector.Connect();
  65. app->Stop();
  66. app.reset();
  67. EXPECT_THAT(m_editorEntityContextNotificationOrderingDetector.m_prepareForContextReset, ::testing::IsTrue());
  68. EXPECT_THAT(m_editorEntityContextNotificationOrderingDetector.m_contextReset, ::testing::IsTrue());
  69. m_editorEntityContextNotificationOrderingDetector.Disconnect();
  70. }
  71. } // namespace UnitTest