ToolBarManagerTests.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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 <AzToolsFramework/API/ToolsApplicationAPI.h>
  10. #include <QToolBar>
  11. namespace UnitTest
  12. {
  13. TEST_F(ActionManagerFixture, RegisterToolBar)
  14. {
  15. auto outcome = m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  16. EXPECT_TRUE(outcome.IsSuccess());
  17. }
  18. TEST_F(ActionManagerFixture, RegisterToolBarTwice)
  19. {
  20. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  21. auto outcome = m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  22. EXPECT_FALSE(outcome.IsSuccess());
  23. }
  24. TEST_F(ActionManagerFixture, RegisterToolBarArea)
  25. {
  26. auto outcome = m_toolBarManagerInterface->RegisterToolBarArea("o3de.toolbararea.test", m_mainWindow, Qt::ToolBarArea::TopToolBarArea);
  27. EXPECT_TRUE(outcome.IsSuccess());
  28. }
  29. TEST_F(ActionManagerFixture, RegisterToolBarAreaTwice)
  30. {
  31. m_toolBarManagerInterface->RegisterToolBarArea("o3de.toolbararea.test", m_mainWindow, Qt::ToolBarArea::TopToolBarArea);
  32. auto outcome = m_toolBarManagerInterface->RegisterToolBarArea("o3de.toolbararea.test", m_mainWindow, Qt::ToolBarArea::TopToolBarArea);
  33. EXPECT_FALSE(outcome.IsSuccess());
  34. }
  35. TEST_F(ActionManagerFixture, AddActionToUnregisteredToolBar)
  36. {
  37. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  38. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  39. auto outcome = m_toolBarManagerInterface->AddActionToToolBar("o3de.toolbar.test", "o3de.action.test", 42);
  40. EXPECT_FALSE(outcome.IsSuccess());
  41. }
  42. TEST_F(ActionManagerFixture, AddActionToToolBar)
  43. {
  44. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  45. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  46. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  47. auto outcome = m_toolBarManagerInterface->AddActionToToolBar("o3de.toolbar.test", "o3de.action.test", 42);
  48. EXPECT_TRUE(outcome.IsSuccess());
  49. }
  50. TEST_F(ActionManagerFixture, AddActionToToolBarTwice)
  51. {
  52. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  53. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  54. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  55. m_toolBarManagerInterface->AddActionToToolBar("o3de.toolbar.test", "o3de.action.test", 42);
  56. auto outcome = m_toolBarManagerInterface->AddActionToToolBar("o3de.toolbar.test", "o3de.action.test", 42);
  57. EXPECT_FALSE(outcome.IsSuccess());
  58. }
  59. TEST_F(ActionManagerFixture, AddActionsToToolBar)
  60. {
  61. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  62. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  63. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test2", {}, []{});
  64. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  65. AZStd::vector<AZStd::pair<AZStd::string, int>> actions;
  66. actions.push_back(AZStd::make_pair("o3de.action.test", 42));
  67. actions.push_back(AZStd::make_pair("o3de.action.test2", 1));
  68. auto outcome = m_toolBarManagerInterface->AddActionsToToolBar("o3de.toolbar.test", actions);
  69. EXPECT_TRUE(outcome.IsSuccess());
  70. }
  71. TEST_F(ActionManagerFixture, RemoveActionFromToolBar)
  72. {
  73. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  74. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  75. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  76. m_toolBarManagerInterface->AddActionToToolBar("o3de.toolbar.test", "o3de.action.test", 42);
  77. auto outcome = m_toolBarManagerInterface->RemoveActionFromToolBar("o3de.toolbar.test", "o3de.action.test");
  78. EXPECT_TRUE(outcome.IsSuccess());
  79. }
  80. TEST_F(ActionManagerFixture, RemoveMissingActionFromToolBar)
  81. {
  82. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  83. auto outcome = m_toolBarManagerInterface->RemoveActionFromToolBar("o3de.toolbar.test", "o3de.action.test");
  84. EXPECT_FALSE(outcome.IsSuccess());
  85. }
  86. TEST_F(ActionManagerFixture, RemoveActionsFromToolBar)
  87. {
  88. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  89. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  90. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test2", {}, []{});
  91. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  92. AZStd::vector<AZStd::pair<AZStd::string, int>> actions;
  93. actions.push_back(AZStd::make_pair("o3de.action.test", 42));
  94. actions.push_back(AZStd::make_pair("o3de.action.test2", 1));
  95. m_toolBarManagerInterface->AddActionsToToolBar("o3de.toolbar.test", actions);
  96. auto outcome = m_toolBarManagerInterface->RemoveActionsFromToolBar("o3de.toolbar.test", { "o3de.action.test", "o3de.action.test2" });
  97. EXPECT_TRUE(outcome.IsSuccess());
  98. }
  99. TEST_F(ActionManagerFixture, RemoveMissingActionsFromToolBar)
  100. {
  101. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  102. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  103. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test2", {}, []{});
  104. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  105. AZStd::vector<AZStd::pair<AZStd::string, int>> actions;
  106. actions.push_back(AZStd::make_pair("o3de.action.test", 42));
  107. m_toolBarManagerInterface->AddActionsToToolBar("o3de.toolbar.test", actions);
  108. auto outcome = m_toolBarManagerInterface->RemoveActionsFromToolBar("o3de.toolbar.test", { "o3de.action.test", "o3de.action.test2" });
  109. EXPECT_FALSE(outcome.IsSuccess());
  110. }
  111. TEST_F(ActionManagerFixture, GetUnregisteredToolBar)
  112. {
  113. QToolBar* toolBar = m_toolBarManagerInterface->GenerateToolBar("o3de.toolbar.test");
  114. EXPECT_TRUE(toolBar == nullptr);
  115. }
  116. TEST_F(ActionManagerFixture, GenerateToolBar)
  117. {
  118. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  119. QToolBar* toolBar = m_toolBarManagerInterface->GenerateToolBar("o3de.toolbar.test");
  120. EXPECT_TRUE(toolBar != nullptr);
  121. }
  122. TEST_F(ActionManagerFixture, VerifyActionInToolBar)
  123. {
  124. // Register ToolBar, get it and verify it's empty.
  125. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  126. QToolBar* toolBar = m_toolBarManagerInterface->GenerateToolBar("o3de.toolbar.test");
  127. EXPECT_EQ(toolBar->actions().size(), 0);
  128. // Register a new action and add it to the ToolBar.
  129. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  130. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  131. auto outcome = m_toolBarManagerInterface->AddActionToToolBar("o3de.toolbar.test", "o3de.action.test", 42);
  132. // Manually trigger ToolBar refresh - Editor will call this once per tick.
  133. m_toolBarManagerInternalInterface->RefreshToolBars();
  134. // Verify the action is now in the ToolBar.
  135. EXPECT_EQ(toolBar->actions().size(), 1);
  136. }
  137. TEST_F(ActionManagerFixture, VerifyActionOrderInToolBar)
  138. {
  139. // Register ToolBar, get it and verify it's empty.
  140. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  141. QToolBar* toolBar = m_toolBarManagerInterface->GenerateToolBar("o3de.toolbar.test");
  142. EXPECT_EQ(toolBar->actions().size(), 0);
  143. // Register a new action and add it to the ToolBar.
  144. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  145. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test1", {}, []{});
  146. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test2", {}, []{});
  147. m_toolBarManagerInterface->AddActionToToolBar("o3de.toolbar.test", "o3de.action.test2", 42);
  148. m_toolBarManagerInterface->AddActionToToolBar("o3de.toolbar.test", "o3de.action.test1", 1);
  149. // Manually trigger ToolBar refresh - Editor will call this once per tick.
  150. m_toolBarManagerInternalInterface->RefreshToolBars();
  151. // Verify the actions are now in the ToolBar.
  152. EXPECT_EQ(toolBar->actions().size(), 2);
  153. // Verify the order is correct.
  154. QAction* test1 = m_actionManagerInternalInterface->GetAction("o3de.action.test1");
  155. QAction* test2 = m_actionManagerInternalInterface->GetAction("o3de.action.test2");
  156. const auto& actions = toolBar->actions();
  157. EXPECT_EQ(actions[0], test1);
  158. EXPECT_EQ(actions[1], test2);
  159. }
  160. TEST_F(ActionManagerFixture, VerifyActionOrderInToolBarWithCollision)
  161. {
  162. // Register ToolBar, get it and verify it's empty.
  163. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  164. QToolBar* toolBar = m_toolBarManagerInterface->GenerateToolBar("o3de.toolbar.test");
  165. EXPECT_EQ(toolBar->actions().size(), 0);
  166. // Register a new action and add it to the ToolBar.
  167. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  168. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test1", {}, []{});
  169. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test2", {}, []{});
  170. m_toolBarManagerInterface->AddActionToToolBar("o3de.toolbar.test", "o3de.action.test2", 42);
  171. m_toolBarManagerInterface->AddActionToToolBar("o3de.toolbar.test", "o3de.action.test1", 42);
  172. // Manually trigger ToolBar refresh - Editor will call this once per tick.
  173. m_toolBarManagerInternalInterface->RefreshToolBars();
  174. // Verify the actions are now in the ToolBar.
  175. EXPECT_EQ(toolBar->actions().size(), 2);
  176. // Verify the order is correct (when a collision happens, items should be in order of addition).
  177. QAction* test1 = m_actionManagerInternalInterface->GetAction("o3de.action.test1");
  178. QAction* test2 = m_actionManagerInternalInterface->GetAction("o3de.action.test2");
  179. const auto& actions = toolBar->actions();
  180. EXPECT_EQ(actions[0], test2);
  181. EXPECT_EQ(actions[1], test1);
  182. }
  183. TEST_F(ActionManagerFixture, VerifySeparatorInToolBar)
  184. {
  185. // Register ToolBar, get it and verify it's empty.
  186. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  187. QToolBar* toolBar = m_toolBarManagerInterface->GenerateToolBar("o3de.toolbar.test");
  188. EXPECT_EQ(toolBar->actions().size(), 0);
  189. // Add a separator to the ToolBar.
  190. m_toolBarManagerInterface->AddSeparatorToToolBar("o3de.toolbar.test", 42);
  191. // Manually trigger ToolBar refresh - Editor will call this once per tick.
  192. m_toolBarManagerInternalInterface->RefreshToolBars();
  193. // Verify the separator is now in the ToolBar.
  194. const auto& actions = toolBar->actions();
  195. EXPECT_EQ(actions.size(), 1);
  196. EXPECT_TRUE(actions[0]->isSeparator());
  197. }
  198. TEST_F(ActionManagerFixture, AddUnregisteredWidgetInToolBar)
  199. {
  200. // Register ToolBar.
  201. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  202. // Try to add a nullptr widget.
  203. auto outcome = m_toolBarManagerInterface->AddWidgetToToolBar("o3de.toolbar.test", "someUnregisteredWidgetIdentifier", 42);
  204. EXPECT_FALSE(outcome.IsSuccess());
  205. }
  206. TEST_F(ActionManagerFixture, VerifyWidgetInToolBar)
  207. {
  208. // Register ToolBar and widget action.
  209. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  210. QWidget* widget = new QWidget();
  211. m_actionManagerInterface->RegisterWidgetAction(
  212. "o3de.widgetAction.test",
  213. {},
  214. [widget]()
  215. {
  216. // Note: the WidgetAction generator function should create a new widget every time it's called.
  217. // This implementation is technically incorrect, but it allows us to test the correct behavior.
  218. return widget;
  219. }
  220. );
  221. // Add the widget to the ToolBar.
  222. m_toolBarManagerInterface->AddWidgetToToolBar("o3de.toolbar.test", "o3de.widgetAction.test", 42);
  223. // Manually trigger ToolBar refresh - Editor will call this once per tick.
  224. m_toolBarManagerInternalInterface->RefreshToolBars();
  225. // Verify the separator is now in the ToolBar.
  226. QToolBar* toolBar = m_toolBarManagerInterface->GenerateToolBar("o3de.toolbar.test");
  227. const auto& actions = toolBar->actions();
  228. EXPECT_EQ(actions.size(), 1);
  229. QWidgetAction* widgetAction = qobject_cast<QWidgetAction*>(actions[0]);
  230. EXPECT_TRUE(widgetAction != nullptr);
  231. EXPECT_TRUE(widgetAction->defaultWidget() == widget);
  232. }
  233. TEST_F(ActionManagerFixture, VerifyComplexToolBar)
  234. {
  235. // Combine multiple actions and separators.
  236. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  237. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  238. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test1", {}, []{});
  239. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test2", {}, []{});
  240. // Create a ToolBar with this setup. Order of addition is intentionally scrambled to verify sortKeys.
  241. // - Test 1 Action
  242. // - Separator
  243. // - Test 2 Action
  244. m_toolBarManagerInterface->AddActionToToolBar("o3de.toolbar.test", "o3de.action.test2", 15);
  245. m_toolBarManagerInterface->AddActionToToolBar("o3de.toolbar.test", "o3de.action.test1", 1);
  246. m_toolBarManagerInterface->AddSeparatorToToolBar("o3de.toolbar.test", 10);
  247. // Verify the actions are now in the ToolBar in the expected order.
  248. QToolBar* toolBar = m_toolBarManagerInterface->GenerateToolBar("o3de.toolbar.test");
  249. QAction* test1 = m_actionManagerInternalInterface->GetAction("o3de.action.test1");
  250. QAction* test2 = m_actionManagerInternalInterface->GetAction("o3de.action.test2");
  251. // Manually trigger ToolBar refresh - Editor will call this once per tick.
  252. m_toolBarManagerInternalInterface->RefreshToolBars();
  253. // Note: separators are still QActions in the context of the ToolBar.
  254. const auto& actions = toolBar->actions();
  255. EXPECT_EQ(actions.size(), 3);
  256. // Verify the order is correct.
  257. EXPECT_EQ(actions[0], test1);
  258. EXPECT_TRUE(actions[1]->isSeparator());
  259. EXPECT_EQ(actions[2], test2);
  260. }
  261. TEST_F(ActionManagerFixture, AddToolBarToUnregisteredToolBarArea)
  262. {
  263. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  264. auto outcome = m_toolBarManagerInterface->AddToolBarToToolBarArea("o3de.toolbararea.test", "o3de.toolbar.test", 42);
  265. EXPECT_FALSE(outcome.IsSuccess());
  266. }
  267. TEST_F(ActionManagerFixture, AddToolBarToToolBarArea)
  268. {
  269. m_toolBarManagerInterface->RegisterToolBarArea("o3de.toolbararea.test", m_mainWindow, Qt::ToolBarArea::TopToolBarArea);
  270. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  271. auto outcome = m_toolBarManagerInterface->AddToolBarToToolBarArea("o3de.toolbararea.test", "o3de.toolbar.test", 42);
  272. EXPECT_TRUE(outcome.IsSuccess());
  273. }
  274. TEST_F(ActionManagerFixture, AddToolBarToToolBarAreaTwice)
  275. {
  276. m_toolBarManagerInterface->RegisterToolBarArea("o3de.toolbararea.test", m_mainWindow, Qt::ToolBarArea::TopToolBarArea);
  277. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  278. m_toolBarManagerInterface->AddToolBarToToolBarArea("o3de.toolbararea.test", "o3de.toolbar.test", 42);
  279. auto outcome = m_toolBarManagerInterface->AddToolBarToToolBarArea("o3de.toolbararea.test", "o3de.toolbar.test", 42);
  280. EXPECT_FALSE(outcome.IsSuccess());
  281. }
  282. TEST_F(ActionManagerFixture, VerifyToolBarInToolBarArea)
  283. {
  284. const char* TestToolBarName = "Test ToolBar";
  285. m_toolBarManagerInterface->RegisterToolBarArea("o3de.toolbararea.test", m_mainWindow, Qt::ToolBarArea::TopToolBarArea);
  286. AzToolsFramework::ToolBarProperties toolBarProperties;
  287. toolBarProperties.m_name = TestToolBarName;
  288. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", toolBarProperties);
  289. // Add the ToolBar to the toolbar area.
  290. m_toolBarManagerInterface->AddToolBarToToolBarArea("o3de.toolbararea.test", "o3de.toolbar.test", 42);
  291. // Manually trigger ToolBar refresh - Editor will call this once per tick.
  292. m_toolBarManagerInternalInterface->RefreshToolBarAreas();
  293. // Verify the ToolBar is now in the ToolBar Area.
  294. auto toolBars = m_mainWindow->findChildren<QToolBar*>("");
  295. EXPECT_EQ(toolBars.size(), 1);
  296. EXPECT_EQ(toolBars[0]->windowTitle(), TestToolBarName);
  297. EXPECT_EQ(m_mainWindow->toolBarArea(toolBars[0]), Qt::ToolBarArea::TopToolBarArea);
  298. }
  299. TEST_F(ActionManagerFixture, GetSortKeyOfActionInToolBar)
  300. {
  301. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  302. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  303. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  304. // Add the action to the ToolBar.
  305. m_toolBarManagerInterface->AddActionToToolBar("o3de.toolbar.test", "o3de.action.test", 42);
  306. // Verify the API returns the correct sort key.
  307. auto outcome = m_toolBarManagerInterface->GetSortKeyOfActionInToolBar("o3de.toolbar.test", "o3de.action.test");
  308. EXPECT_TRUE(outcome.IsSuccess());
  309. EXPECT_EQ(outcome.GetValue(), 42);
  310. }
  311. TEST_F(ActionManagerFixture, GetSortKeyOfUnregisteredActionInToolBar)
  312. {
  313. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  314. // Verify the API fails as the action is not registered.
  315. auto outcome = m_toolBarManagerInterface->GetSortKeyOfActionInToolBar("o3de.toolbar.test", "o3de.action.test");
  316. EXPECT_FALSE(outcome.IsSuccess());
  317. }
  318. TEST_F(ActionManagerFixture, GetSortKeyOfActionNotInToolBar)
  319. {
  320. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  321. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  322. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  323. // Verify the API fails as the action is registered but was not added to the ToolBar.
  324. auto outcome = m_toolBarManagerInterface->GetSortKeyOfActionInToolBar("o3de.toolbar.test", "o3de.action.test");
  325. EXPECT_FALSE(outcome.IsSuccess());
  326. }
  327. TEST_F(ActionManagerFixture, GetSortKeyOfWidgetInToolBar)
  328. {
  329. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  330. m_actionManagerInterface->RegisterWidgetAction(
  331. "o3de.widgetAction.test",
  332. {},
  333. []() -> QWidget*
  334. {
  335. return nullptr;
  336. }
  337. );
  338. // Add the widget to the ToolBar.
  339. m_toolBarManagerInterface->AddWidgetToToolBar("o3de.toolbar.test", "o3de.widgetAction.test", 42);
  340. // Verify the API returns the correct sort key.
  341. auto outcome = m_toolBarManagerInterface->GetSortKeyOfWidgetInToolBar("o3de.toolbar.test", "o3de.widgetAction.test");
  342. EXPECT_TRUE(outcome.IsSuccess());
  343. EXPECT_EQ(outcome.GetValue(), 42);
  344. }
  345. TEST_F(ActionManagerFixture, GetSortKeyOfUnregisteredWidgetInToolBar)
  346. {
  347. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  348. // Verify the API fails as the widget is not registered.
  349. auto outcome = m_toolBarManagerInterface->GetSortKeyOfWidgetInToolBar("o3de.toolbar.test", "o3de.widgetAction.test");
  350. EXPECT_FALSE(outcome.IsSuccess());
  351. }
  352. TEST_F(ActionManagerFixture, GetSortKeyOfWidgetNotInToolBar)
  353. {
  354. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  355. m_actionManagerInterface->RegisterWidgetAction(
  356. "o3de.widgetAction.test",
  357. {},
  358. []() -> QWidget*
  359. {
  360. return nullptr;
  361. }
  362. );
  363. // Verify the API fails as the widget is registered but was not added to the ToolBar.
  364. auto outcome = m_toolBarManagerInterface->GetSortKeyOfWidgetInToolBar("o3de.toolbar.test", "o3de.widgetAction.test");
  365. EXPECT_FALSE(outcome.IsSuccess());
  366. }
  367. TEST_F(ActionManagerFixture, VerifyToolBarVisibilityHideWhenDisabled)
  368. {
  369. // Register toolbar, get it and verify it's empty.
  370. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  371. QToolBar* toolBar = m_toolBarManagerInterface->GenerateToolBar("o3de.toolbar.test");
  372. EXPECT_EQ(toolBar->actions().size(), 0);
  373. // Register a new action and add it to the ToolBar. Have ToolBarVisibility set to HideWhenDisabled.
  374. AzToolsFramework::ActionProperties actionProperties;
  375. actionProperties.m_toolBarVisibility = AzToolsFramework::ActionVisibility::HideWhenDisabled;
  376. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  377. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", actionProperties, []{});
  378. m_toolBarManagerInterface->AddActionToToolBar("o3de.toolbar.test", "o3de.action.test", 42);
  379. // Add enabled state callback.
  380. bool enabledState = true;
  381. m_actionManagerInterface->InstallEnabledStateCallback(
  382. "o3de.action.test",
  383. [&]()
  384. {
  385. return enabledState;
  386. }
  387. );
  388. // Manually trigger ToolBar refresh - Editor will call this once per tick.
  389. m_toolBarManagerInternalInterface->RefreshToolBars();
  390. // Verify the action is now in the ToolBar.
  391. EXPECT_EQ(toolBar->actions().size(), 1);
  392. // Set the action as disabled.
  393. enabledState = false;
  394. m_actionManagerInterface->UpdateAction("o3de.action.test");
  395. // Manually trigger ToolBar refresh - Editor will call this once per tick.
  396. m_toolBarManagerInternalInterface->RefreshToolBars();
  397. // Verify the action is no longer in the ToolBar.
  398. EXPECT_EQ(toolBar->actions().size(), 0);
  399. }
  400. TEST_F(ActionManagerFixture, VerifyDefaultToolBarVisibility)
  401. {
  402. // Register ToolBar, get it and verify it's empty.
  403. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  404. QToolBar* toolBar = m_toolBarManagerInterface->GenerateToolBar("o3de.toolbar.test");
  405. EXPECT_EQ(toolBar->actions().size(), 0);
  406. // Register a new action and add it to the menu. ToolBarVisibility is set to OnlyInActiveMode by default.
  407. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  408. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  409. m_toolBarManagerInterface->AddActionToToolBar("o3de.toolbar.test", "o3de.action.test", 42);
  410. // Add enabled state callback.
  411. bool enabledState = true;
  412. m_actionManagerInterface->InstallEnabledStateCallback(
  413. "o3de.action.test",
  414. [&]()
  415. {
  416. return enabledState;
  417. }
  418. );
  419. // Manually trigger ToolBar refresh - Editor will call this once per tick.
  420. m_toolBarManagerInternalInterface->RefreshToolBars();
  421. // Verify the action is now in the ToolBar.
  422. EXPECT_EQ(toolBar->actions().size(), 1);
  423. // Set the action as disabled.
  424. enabledState = false;
  425. m_actionManagerInterface->UpdateAction("o3de.action.test");
  426. // Manually trigger ToolBar refresh - Editor will call this once per tick.
  427. m_toolBarManagerInternalInterface->RefreshToolBars();
  428. // Verify the action is still in the ToolBar.
  429. EXPECT_EQ(toolBar->actions().size(), 1);
  430. }
  431. TEST_F(ActionManagerFixture, VerifyToolBarVisibilityAlwaysShowWhenChangingMode)
  432. {
  433. // Register ToolBar, get it and verify it's empty.
  434. m_toolBarManagerInterface->RegisterToolBar("o3de.toolbar.test", {});
  435. QToolBar* toolBar = m_toolBarManagerInterface->GenerateToolBar("o3de.toolbar.test");
  436. EXPECT_EQ(toolBar->actions().size(), 0);
  437. // Register a new action and add it to the default mode. Set ToolBarVisibility to AlwaysShow.
  438. AzToolsFramework::ActionProperties actionProperties;
  439. actionProperties.m_toolBarVisibility = AzToolsFramework::ActionVisibility::AlwaysShow;
  440. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  441. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", actionProperties, []{});
  442. m_actionManagerInterface->AssignModeToAction(AzToolsFramework::DefaultActionContextModeIdentifier, "o3de.action.test");
  443. // Add the action to the ToolBar.
  444. m_toolBarManagerInterface->AddActionToToolBar("o3de.toolbar.test", "o3de.action.test", 42);
  445. // Manually trigger ToolBar refresh - Editor will call this once per tick.
  446. m_toolBarManagerInternalInterface->RefreshToolBars();
  447. // Verify the action is now in the ToolBar.
  448. EXPECT_EQ(toolBar->actions().size(), 1);
  449. // Register a new mode and switch to it.
  450. m_actionManagerInterface->RegisterActionContextMode("o3de.context.test", "testMode");
  451. m_actionManagerInterface->SetActiveActionContextMode("o3de.context.test", "testMode");
  452. // Manually trigger ToolBar refresh - Editor will call this once per tick.
  453. m_toolBarManagerInternalInterface->RefreshToolBars();
  454. // Verify the action is still in the ToolBar.
  455. EXPECT_EQ(toolBar->actions().size(), 1);
  456. }
  457. } // namespace UnitTest