HotKeyManagerTests.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <Tests/ActionManager/ActionManagerFixture.h>
  9. #include<QKeySequence>
  10. namespace UnitTest
  11. {
  12. TEST_F(ActionManagerFixture, AssignWidgetToActionContext)
  13. {
  14. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  15. auto outcome = m_hotKeyManagerInterface->AssignWidgetToActionContext("o3de.context.test", m_widget);
  16. EXPECT_TRUE(outcome.IsSuccess());
  17. }
  18. TEST_F(ActionManagerFixture, RemoveWidgetFromActionContext)
  19. {
  20. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  21. m_hotKeyManagerInterface->AssignWidgetToActionContext("o3de.context.test", m_widget);
  22. auto outcome = m_hotKeyManagerInterface->RemoveWidgetFromActionContext("o3de.context.test", m_widget);
  23. EXPECT_TRUE(outcome.IsSuccess());
  24. }
  25. TEST_F(ActionManagerFixture, SetHotKeyToAction)
  26. {
  27. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  28. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  29. auto outcome = m_hotKeyManagerInterface->SetActionHotKey("o3de.action.test", "Ctrl+Z");
  30. EXPECT_TRUE(outcome.IsSuccess());
  31. }
  32. TEST_F(ActionManagerFixture, SetInvalidHotKeyToAction)
  33. {
  34. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  35. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  36. auto outcome = m_hotKeyManagerInterface->SetActionHotKey("o3de.action.test", "SomeWeirdString");
  37. EXPECT_FALSE(outcome.IsSuccess());
  38. }
  39. TEST_F(ActionManagerFixture, SetHotKeyToUnregisteredAction)
  40. {
  41. auto outcome = m_hotKeyManagerInterface->SetActionHotKey("o3de.context.test", "Ctrl+Z");
  42. EXPECT_FALSE(outcome.IsSuccess());
  43. }
  44. TEST_F(ActionManagerFixture, VerifyActionHotkey)
  45. {
  46. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  47. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  48. m_hotKeyManagerInterface->SetActionHotKey("o3de.action.test", "Ctrl+Z");
  49. QAction* action = m_actionManagerInternalInterface->GetAction("o3de.action.test");
  50. EXPECT_TRUE(action->shortcut() == QKeySequence("Ctrl+Z"));
  51. }
  52. TEST_F(ActionManagerFixture, VerifyActionHotkeyTriggered)
  53. {
  54. bool actionTriggered = false;
  55. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  56. m_hotKeyManagerInterface->AssignWidgetToActionContext("o3de.context.test", m_widget);
  57. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {},
  58. [&actionTriggered]()
  59. {
  60. actionTriggered = true;
  61. }
  62. );
  63. m_hotKeyManagerInterface->SetActionHotKey("o3de.action.test", "Ctrl+Z");
  64. // Make sure to set the active window and give our m_widget focus so that the events get propogated correctly
  65. QApplication::setActiveWindow(m_defaultParentWidget);
  66. m_widget->setFocus();
  67. // Trigger a shortcut event to our widget, which should in turn trigger our action
  68. QShortcutEvent testEvent(QKeySequence("Ctrl+Z"), 0, true);
  69. QApplication::sendEvent(m_widget, &testEvent);
  70. EXPECT_TRUE(actionTriggered);
  71. QApplication::setActiveWindow(nullptr);
  72. }
  73. TEST_F(ActionManagerFixture, VerifyAmbiguousShortcutsHandled)
  74. {
  75. // Ambiguous shortcuts occur when a parent and child both have an action with the same shortcut
  76. // and the child is focused, because Qt propagates shortcut events upwards.
  77. // This test verifies that we correctly capture these ambiguous shortcuts in the child and
  78. // trigger the appropriate action.
  79. bool parentActionTriggered = false;
  80. m_actionManagerInterface->RegisterActionContext("o3de.context.parent", {});
  81. m_hotKeyManagerInterface->AssignWidgetToActionContext("o3de.context.parent", m_widget);
  82. m_actionManagerInterface->RegisterAction("o3de.context.parent", "o3de.action.parent", {},
  83. [&parentActionTriggered]()
  84. {
  85. parentActionTriggered = true;
  86. }
  87. );
  88. m_hotKeyManagerInterface->SetActionHotKey("o3de.action.parent", "Ctrl+Z");
  89. // Setup a child of our parent widget with an action that has the same shortcut
  90. QWidget* childWidget = new QWidget(m_widget);
  91. bool childActionTriggered = false;
  92. m_actionManagerInterface->RegisterActionContext("o3de.context.child", {});
  93. m_hotKeyManagerInterface->AssignWidgetToActionContext("o3de.context.child", childWidget);
  94. m_actionManagerInterface->RegisterAction("o3de.context.child", "o3de.action.child", {},
  95. [&childActionTriggered]()
  96. {
  97. childActionTriggered = true;
  98. }
  99. );
  100. m_hotKeyManagerInterface->SetActionHotKey("o3de.action.child", "Ctrl+Z");
  101. // Make sure to set the active window and give our childWidget focus so that the events get propogated correctly
  102. QApplication::setActiveWindow(m_defaultParentWidget);
  103. childWidget->setFocus();
  104. // setting Focus actually requires us to pump the event pump.
  105. QCoreApplication::processEvents();
  106. // Trigger a shortcut event to our child widget, which should in turn only trigger our child action
  107. QShortcutEvent testEvent(QKeySequence("Ctrl+Z"), 0, true);
  108. QApplication::sendEvent(childWidget, &testEvent);
  109. EXPECT_TRUE(childActionTriggered);
  110. EXPECT_FALSE(parentActionTriggered);
  111. QApplication::setActiveWindow(nullptr);
  112. }
  113. } // namespace UnitTest