ScriptAutomationTests.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 <ScriptAutomation/ScriptAutomationBus.h>
  9. #include <ScriptAutomationApplicationFixture.h>
  10. #include <AzCore/RTTI/BehaviorContext.h>
  11. #include <AzCore/UnitTest/UnitTest.h>
  12. namespace UnitTest
  13. {
  14. TEST_F(ScriptAutomationApplicationFixture, GetAutomationContext_FromScriptAutomationInterface_HasCoreMethods)
  15. {
  16. CreateApplication({ });
  17. auto automationSystem = AZ::ScriptAutomation::ScriptAutomationInterface::Get();
  18. ASSERT_TRUE(automationSystem);
  19. auto behaviorContext = automationSystem->GetAutomationContext();
  20. ASSERT_TRUE(behaviorContext);
  21. EXPECT_TRUE(behaviorContext->m_methods.find("Print") != behaviorContext->m_methods.end());
  22. EXPECT_TRUE(behaviorContext->m_methods.find("Warning") != behaviorContext->m_methods.end());
  23. EXPECT_TRUE(behaviorContext->m_methods.find("Error") != behaviorContext->m_methods.end());
  24. EXPECT_TRUE(behaviorContext->m_methods.find("ExecuteConsoleCommand") != behaviorContext->m_methods.end());
  25. EXPECT_TRUE(behaviorContext->m_methods.find("IdleFrames") != behaviorContext->m_methods.end());
  26. EXPECT_TRUE(behaviorContext->m_methods.find("IdleSeconds") != behaviorContext->m_methods.end());
  27. }
  28. class TrackedAutomationFixture
  29. : public ScriptAutomationApplicationFixture
  30. , public AZ::Debug::TraceMessageBus::Handler
  31. {
  32. public:
  33. void SetUp() override
  34. {
  35. ScriptAutomationApplicationFixture::SetUp();
  36. AZ::Debug::TraceMessageBus::Handler::BusConnect();
  37. }
  38. void TearDown() override
  39. {
  40. AZ::Debug::TraceMessageBus::Handler::BusDisconnect();
  41. m_automationWarnings.set_capacity(0);
  42. m_automationLogs.set_capacity(0);
  43. ScriptAutomationApplicationFixture::TearDown();
  44. }
  45. protected:
  46. // AZ::Debug::TraceMessageBus implementation
  47. bool OnError(const char* window, const char* message) override
  48. {
  49. AZ_UNUSED(window);
  50. AZ_UNUSED(message);
  51. // Do nothing. Errors will cause the test to fail. If we suppress errors,
  52. // this function won't get called. We test errors by suppressing them and
  53. // then counting how many errors were suppressed.
  54. return false;
  55. }
  56. bool OnWarning(const char* window, const char* message) override
  57. {
  58. if (azstricmp(window, "ScriptAutomation") == 0)
  59. {
  60. m_automationWarnings.push_back(message);
  61. }
  62. return false;
  63. }
  64. bool OnPrintf(const char* window, const char* message) override
  65. {
  66. if (azstricmp(window, "ScriptAutomation") == 0)
  67. {
  68. m_automationLogs.push_back(message);
  69. }
  70. return false;
  71. }
  72. AZStd::vector<AZStd::string> m_automationWarnings;
  73. AZStd::vector<AZStd::string> m_automationLogs;
  74. };
  75. TEST_F(TrackedAutomationFixture, ScriptCommandLineArgument_UsesPrintMethods_AllOperationsLogged)
  76. {
  77. const char* scriptPath = "@gemroot:ScriptAutomation@/Code/Tests/Scripts/print_test.lua";
  78. auto application = CreateApplication(scriptPath);
  79. // We expect our "Hello World" error message to be printed by the test script.
  80. // There should be exactly one error message.
  81. AZ_TEST_START_TRACE_SUPPRESSION;
  82. application->RunMainLoop();
  83. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  84. AZStd::string executeScriptLog = AZStd::string::format("Running script '%s'...\n", scriptPath);
  85. const char* scriptLog = "Script: Hello World\n";
  86. ASSERT_EQ(m_automationLogs.size(), 2);
  87. EXPECT_EQ(m_automationLogs[0], executeScriptLog);
  88. EXPECT_EQ(m_automationLogs[1], scriptLog);
  89. ASSERT_EQ(m_automationWarnings.size(), 1);
  90. EXPECT_EQ(m_automationWarnings[0], scriptLog);
  91. }
  92. TEST_F(TrackedAutomationFixture, ScriptCommandLineArgument_UsesIdleFramesMethod_AllOperationsLogged)
  93. {
  94. auto application = CreateApplication("@gemroot:ScriptAutomation@/Code/Tests/Scripts/idle_five_frames_test.lua");
  95. application->RunMainLoop();
  96. ASSERT_EQ(m_automationLogs.size(), 3);
  97. // first log is the "running script ..." line
  98. EXPECT_EQ(m_automationLogs[1], "Script: Going to idle for 5 frames\n");
  99. EXPECT_EQ(m_automationLogs[2], "Script: Idled for 5 frames\n");
  100. }
  101. TEST_F(TrackedAutomationFixture, ScriptCommandLineArgument_UsesIdleSecondsMethod_AllOperationsLogged)
  102. {
  103. auto application = CreateApplication("@gemroot:ScriptAutomation@/Code/Tests/Scripts/idle_one_second_test.lua");
  104. application->RunMainLoop();
  105. ASSERT_EQ(m_automationLogs.size(), 3);
  106. // first log is the "running script ..." line
  107. EXPECT_EQ(m_automationLogs[1], "Script: Going to idle for 1 second(s)\n");
  108. EXPECT_EQ(m_automationLogs[2], "Script: Idled for 1 second(s)\n");
  109. }
  110. } // namespace UnitTest
  111. AZ_UNIT_TEST_HOOK(DEFAULT_UNIT_TEST_ENV);