test_CryEditPythonBindings.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "EditorDefs.h"
  9. #include <AzTest/AzTest.h>
  10. #include <Util/EditorUtils.h>
  11. #include <AzCore/base.h>
  12. #include <AzCore/Memory/SystemAllocator.h>
  13. #include <AzCore/Debug/TraceMessageBus.h>
  14. #include <AzCore/UnitTest/TestTypes.h>
  15. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  16. #include <AzToolsFramework/Application/ToolsApplication.h>
  17. #include <AzCore/Component/TickBus.h>
  18. #include <MainWindow.h>
  19. #include <AzCore/RTTI/BehaviorContext.h>
  20. #include <AzCore/UserSettings/UserSettingsComponent.h>
  21. #include "CryEdit.h"
  22. namespace CryEditPythonBindingsUnitTests
  23. {
  24. class CryEditPythonBindingsFixture
  25. : public ::UnitTest::LeakDetectionFixture
  26. {
  27. public:
  28. AzToolsFramework::ToolsApplication m_app;
  29. void SetUp() override
  30. {
  31. AzFramework::Application::Descriptor appDesc;
  32. AZ::ComponentApplication::StartupParameters startupParameters;
  33. startupParameters.m_loadSettingsRegistry = false;
  34. m_app.Start(appDesc, startupParameters);
  35. // Without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
  36. // shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
  37. // in the unit tests.
  38. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  39. m_app.RegisterComponentDescriptor(AzToolsFramework::CryEditPythonHandler::CreateDescriptor());
  40. }
  41. void TearDown() override
  42. {
  43. m_app.Stop();
  44. }
  45. };
  46. TEST_F(CryEditPythonBindingsFixture, CryEditCommands_ApiExists)
  47. {
  48. AZ::BehaviorContext* behaviorContext = m_app.GetBehaviorContext();
  49. ASSERT_TRUE(behaviorContext);
  50. EXPECT_TRUE(behaviorContext->m_methods.find("open_level") != behaviorContext->m_methods.end());
  51. EXPECT_TRUE(behaviorContext->m_methods.find("open_level_no_prompt") != behaviorContext->m_methods.end());
  52. EXPECT_TRUE(behaviorContext->m_methods.find("create_level") != behaviorContext->m_methods.end());
  53. EXPECT_TRUE(behaviorContext->m_methods.find("create_level_no_prompt") != behaviorContext->m_methods.end());
  54. EXPECT_TRUE(behaviorContext->m_methods.find("get_game_folder") != behaviorContext->m_methods.end());
  55. EXPECT_TRUE(behaviorContext->m_methods.find("get_current_level_name") != behaviorContext->m_methods.end());
  56. EXPECT_TRUE(behaviorContext->m_methods.find("get_current_level_path") != behaviorContext->m_methods.end());
  57. EXPECT_TRUE(behaviorContext->m_methods.find("load_all_plugins") != behaviorContext->m_methods.end());
  58. EXPECT_TRUE(behaviorContext->m_methods.find("get_current_view_position") != behaviorContext->m_methods.end());
  59. EXPECT_TRUE(behaviorContext->m_methods.find("get_current_view_rotation") != behaviorContext->m_methods.end());
  60. EXPECT_TRUE(behaviorContext->m_methods.find("set_current_view_position") != behaviorContext->m_methods.end());
  61. EXPECT_TRUE(behaviorContext->m_methods.find("set_current_view_rotation") != behaviorContext->m_methods.end());
  62. EXPECT_TRUE(behaviorContext->m_methods.find("export_to_engine") != behaviorContext->m_methods.end());
  63. EXPECT_TRUE(behaviorContext->m_methods.find("get_config_platform") != behaviorContext->m_methods.end());
  64. EXPECT_TRUE(behaviorContext->m_methods.find("set_result_to_success") != behaviorContext->m_methods.end());
  65. EXPECT_TRUE(behaviorContext->m_methods.find("set_result_to_failure") != behaviorContext->m_methods.end());
  66. EXPECT_TRUE(behaviorContext->m_methods.find("idle_enable") != behaviorContext->m_methods.end());
  67. EXPECT_TRUE(behaviorContext->m_methods.find("is_idle_enabled") != behaviorContext->m_methods.end());
  68. EXPECT_TRUE(behaviorContext->m_methods.find("idle_is_enabled") != behaviorContext->m_methods.end());
  69. EXPECT_TRUE(behaviorContext->m_methods.find("idle_wait") != behaviorContext->m_methods.end());
  70. EXPECT_TRUE(behaviorContext->m_methods.find("idle_wait_frames") != behaviorContext->m_methods.end());
  71. EXPECT_TRUE(behaviorContext->m_methods.find("launch_lua_editor") != behaviorContext->m_methods.end());
  72. }
  73. TEST_F(CryEditPythonBindingsFixture, CryEditCheckoutDialogCommands_ApiExists)
  74. {
  75. AZ::BehaviorContext* behaviorContext = m_app.GetBehaviorContext();
  76. ASSERT_TRUE(behaviorContext);
  77. EXPECT_TRUE(behaviorContext->m_methods.find("enable_for_all") != behaviorContext->m_methods.end());
  78. }
  79. TEST_F(CryEditPythonBindingsFixture, CryEditPython_IdleWaitFramesWorks)
  80. {
  81. AZ::BehaviorContext* behaviorContext = nullptr;
  82. AZ::ComponentApplicationBus::BroadcastResult(behaviorContext, &AZ::ComponentApplicationBus::Events::GetBehaviorContext);
  83. ASSERT_TRUE(behaviorContext);
  84. unsigned int numTicks = 0;
  85. QEventLoop loop;
  86. QTimer timer;
  87. loop.connect(&timer, &QTimer::timeout, [&numTicks]()
  88. {
  89. AZ::TickBus::Broadcast(&AZ::TickEvents::OnTick, 0.01f, AZ::ScriptTimePoint(AZStd::chrono::steady_clock::now()));
  90. ++numTicks;
  91. });
  92. timer.start(100);
  93. const unsigned int framesToWait = 5;
  94. AZStd::array<AZ::BehaviorArgument, 1> args;
  95. args[0].Set(&framesToWait);
  96. behaviorContext->m_methods.find("idle_wait_frames")->second->Call(args.begin(), static_cast<unsigned int>(args.size()));
  97. loop.disconnect(&timer);
  98. timer.stop();
  99. EXPECT_EQ(numTicks, framesToWait);
  100. }
  101. }