ActionManagerTests.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. namespace UnitTest
  11. {
  12. TEST_F(ActionManagerFixture, RegisterActionContext)
  13. {
  14. auto outcome =
  15. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  16. EXPECT_TRUE(outcome.IsSuccess());
  17. }
  18. TEST_F(ActionManagerFixture, VerifyActionContextIsRegistered)
  19. {
  20. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  21. EXPECT_TRUE(m_actionManagerInterface->IsActionContextRegistered("o3de.context.test"));
  22. }
  23. TEST_F(ActionManagerFixture, RegisterActionToUnregisteredContext)
  24. {
  25. auto outcome = m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  26. EXPECT_FALSE(outcome.IsSuccess());
  27. }
  28. TEST_F(ActionManagerFixture, RegisterAction)
  29. {
  30. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  31. auto outcome = m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  32. EXPECT_TRUE(outcome.IsSuccess());
  33. }
  34. TEST_F(ActionManagerFixture, RegisterActionTwice)
  35. {
  36. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  37. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  38. auto outcome = m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  39. EXPECT_FALSE(outcome.IsSuccess());
  40. }
  41. TEST_F(ActionManagerFixture, VerifyActionIsRegistered)
  42. {
  43. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  44. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  45. EXPECT_TRUE(m_actionManagerInterface->IsActionRegistered("o3de.action.test"));
  46. }
  47. TEST_F(ActionManagerFixture, RegisterCheckableActionToUnregisteredContext)
  48. {
  49. auto outcome = m_actionManagerInterface->RegisterCheckableAction("o3de.context.test", "o3de.action.test", {}, []{}, []()->bool{ return true; });
  50. EXPECT_FALSE(outcome.IsSuccess());
  51. }
  52. TEST_F(ActionManagerFixture, RegisterCheckableAction)
  53. {
  54. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  55. auto outcome = m_actionManagerInterface->RegisterCheckableAction("o3de.context.test", "o3de.action.test", {}, []{}, []()->bool{ return true; });
  56. EXPECT_TRUE(outcome.IsSuccess());
  57. }
  58. TEST_F(ActionManagerFixture, RegisterCheckableActionTwice)
  59. {
  60. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  61. m_actionManagerInterface->RegisterCheckableAction("o3de.context.test", "o3de.action.test", {}, []{}, []()->bool{ return true; });
  62. auto outcome = m_actionManagerInterface->RegisterCheckableAction("o3de.context.test", "o3de.action.test", {}, []{}, []()->bool{ return true; });
  63. EXPECT_FALSE(outcome.IsSuccess());
  64. }
  65. TEST_F(ActionManagerFixture, GetUnregisteredAction)
  66. {
  67. QAction* action = m_actionManagerInternalInterface->GetAction("o3de.action.test");
  68. EXPECT_EQ(action, nullptr);
  69. }
  70. TEST_F(ActionManagerFixture, GetAction)
  71. {
  72. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  73. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  74. QAction* action = m_actionManagerInternalInterface->GetAction("o3de.action.test");
  75. EXPECT_TRUE(action != nullptr);
  76. }
  77. TEST_F(ActionManagerFixture, GetAndTriggerAction)
  78. {
  79. bool actionTriggered = false;
  80. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  81. m_actionManagerInterface->RegisterAction(
  82. "o3de.context.test", "o3de.action.test", {},
  83. [&actionTriggered]()
  84. {
  85. actionTriggered = true;
  86. }
  87. );
  88. QAction* action = m_actionManagerInternalInterface->GetAction("o3de.action.test");
  89. action->trigger();
  90. EXPECT_TRUE(actionTriggered);
  91. }
  92. TEST_F(ActionManagerFixture, GetActionName)
  93. {
  94. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  95. AzToolsFramework::ActionProperties actionProperties;
  96. actionProperties.m_name = "Test Name";
  97. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", actionProperties, []{});
  98. auto outcome = m_actionManagerInterface->GetActionName("o3de.action.test");
  99. EXPECT_TRUE(outcome.IsSuccess());
  100. EXPECT_THAT(outcome.GetValue().c_str(), ::testing::StrEq("Test Name"));
  101. }
  102. TEST_F(ActionManagerFixture, SetActionName)
  103. {
  104. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  105. AzToolsFramework::ActionProperties actionProperties;
  106. actionProperties.m_name = "Wrong Name";
  107. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", actionProperties, []{});
  108. auto setOutcome = m_actionManagerInterface->SetActionName("o3de.action.test", "Correct Name");
  109. EXPECT_TRUE(setOutcome.IsSuccess());
  110. auto getOutcome = m_actionManagerInterface->GetActionName("o3de.action.test");
  111. EXPECT_THAT(getOutcome.GetValue().c_str(), ::testing::StrEq("Correct Name"));
  112. }
  113. TEST_F(ActionManagerFixture, GetActionDescription)
  114. {
  115. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  116. AzToolsFramework::ActionProperties actionProperties;
  117. actionProperties.m_description = "Test Description";
  118. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", actionProperties, []{});
  119. auto outcome = m_actionManagerInterface->GetActionDescription("o3de.action.test");
  120. EXPECT_TRUE(outcome.IsSuccess());
  121. EXPECT_THAT(outcome.GetValue().c_str(), ::testing::StrEq("Test Description"));
  122. }
  123. TEST_F(ActionManagerFixture, SetActionDescription)
  124. {
  125. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  126. AzToolsFramework::ActionProperties actionProperties;
  127. actionProperties.m_description = "Wrong Description";
  128. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", actionProperties, []{});
  129. auto setOutcome = m_actionManagerInterface->SetActionDescription("o3de.action.test", "Correct Description");
  130. EXPECT_TRUE(setOutcome.IsSuccess());
  131. auto getOutcome = m_actionManagerInterface->GetActionDescription("o3de.action.test");
  132. EXPECT_THAT(getOutcome.GetValue().c_str(), ::testing::StrEq("Correct Description"));
  133. }
  134. TEST_F(ActionManagerFixture, GetActionCategory)
  135. {
  136. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  137. AzToolsFramework::ActionProperties actionProperties;
  138. actionProperties.m_category = "Test Category";
  139. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", actionProperties, []{});
  140. auto outcome = m_actionManagerInterface->GetActionCategory("o3de.action.test");
  141. EXPECT_TRUE(outcome.IsSuccess());
  142. EXPECT_THAT(outcome.GetValue().c_str(), ::testing::StrEq("Test Category"));
  143. }
  144. TEST_F(ActionManagerFixture, SetActionCategory)
  145. {
  146. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  147. AzToolsFramework::ActionProperties actionProperties;
  148. actionProperties.m_category = "Wrong Category";
  149. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", actionProperties, []{});
  150. auto setOutcome = m_actionManagerInterface->SetActionCategory("o3de.action.test", "Correct Category");
  151. EXPECT_TRUE(setOutcome.IsSuccess());
  152. auto getOutcome = m_actionManagerInterface->GetActionCategory("o3de.action.test");
  153. EXPECT_THAT(getOutcome.GetValue().c_str(), ::testing::StrEq("Correct Category"));
  154. }
  155. TEST_F(ActionManagerFixture, VerifyIncorrectIconPath)
  156. {
  157. // Since we don't want to have the unit tests depend on a resource file, this will only test the case where an incorrect path is set.
  158. // When a path that does not point to a resource is passed, the icon path string is cleared and the icon will be null.
  159. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  160. AzToolsFramework::ActionProperties actionProperties;
  161. actionProperties.m_iconPath = ":/Some/Incorrect/Path.svg";
  162. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", actionProperties, []{});
  163. auto outcome = m_actionManagerInterface->GetActionIconPath("o3de.action.test");
  164. EXPECT_TRUE(outcome.IsSuccess());
  165. EXPECT_TRUE(outcome.GetValue().empty());
  166. QAction* action = m_actionManagerInternalInterface->GetAction("o3de.action.test");
  167. EXPECT_TRUE(action->icon().isNull());
  168. }
  169. TEST_F(ActionManagerFixture, TriggerUnregisteredAction)
  170. {
  171. auto outcome = m_actionManagerInterface->TriggerAction("o3de.action.test");
  172. EXPECT_FALSE(outcome.IsSuccess());
  173. }
  174. TEST_F(ActionManagerFixture, TriggerAction)
  175. {
  176. bool actionTriggered = false;
  177. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  178. m_actionManagerInterface->RegisterAction(
  179. "o3de.context.test", "o3de.action.test", {},
  180. [&actionTriggered]()
  181. {
  182. actionTriggered = true;
  183. }
  184. );
  185. auto outcome = m_actionManagerInterface->TriggerAction("o3de.action.test");
  186. EXPECT_TRUE(outcome.IsSuccess());
  187. EXPECT_TRUE(actionTriggered);
  188. }
  189. TEST_F(ActionManagerFixture, TriggerCheckableAction)
  190. {
  191. // Verify that triggering a checkable action automatically calls the update callback to refresh the checkable state.
  192. bool actionToggle = false;
  193. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  194. m_actionManagerInterface->RegisterCheckableAction(
  195. "o3de.context.test", "o3de.action.checkableTest", {},
  196. [&]()
  197. {
  198. actionToggle = !actionToggle;
  199. },
  200. [&]() -> bool
  201. {
  202. return actionToggle;
  203. }
  204. );
  205. auto outcome = m_actionManagerInterface->TriggerAction("o3de.action.checkableTest");
  206. EXPECT_TRUE(outcome.IsSuccess());
  207. EXPECT_TRUE(actionToggle);
  208. // Note that we don't expose an API to directly query the checked state of an action.
  209. // This is because the checkable state is just a UI indicator. Operations relying on the
  210. // checked state of an action should instead rely on the value of the property they visualize.
  211. // In this example, logic should not rely on the checked state of o3de.action.checkableTest,
  212. // but on the value of actionToggle itself (just like the update callback does).
  213. QAction* action = m_actionManagerInternalInterface->GetAction("o3de.action.checkableTest");
  214. EXPECT_TRUE(action->isChecked());
  215. }
  216. TEST_F(ActionManagerFixture, InstallEnabledStateCallback)
  217. {
  218. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  219. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  220. auto outcome = m_actionManagerInterface->InstallEnabledStateCallback("o3de.action.test", []() { return false; });
  221. EXPECT_TRUE(outcome.IsSuccess());
  222. }
  223. TEST_F(ActionManagerFixture, VerifyEnabledStateCallback)
  224. {
  225. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  226. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  227. {
  228. auto enabledOutcome = m_actionManagerInterface->IsActionEnabled("o3de.action.test");
  229. EXPECT_TRUE(enabledOutcome.IsSuccess());
  230. EXPECT_TRUE(enabledOutcome.GetValue());
  231. }
  232. m_actionManagerInterface->InstallEnabledStateCallback("o3de.action.test", []() { return false; });
  233. {
  234. auto enabledOutcome = m_actionManagerInterface->IsActionEnabled("o3de.action.test");
  235. EXPECT_TRUE(enabledOutcome.IsSuccess());
  236. EXPECT_FALSE(enabledOutcome.GetValue());
  237. }
  238. }
  239. TEST_F(ActionManagerFixture, VerifyEnabledStateCallbackUpdate)
  240. {
  241. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  242. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  243. bool enabledState = false;
  244. m_actionManagerInterface->InstallEnabledStateCallback("o3de.action.test", [&]() {
  245. return enabledState;
  246. });
  247. {
  248. auto enabledOutcome = m_actionManagerInterface->IsActionEnabled("o3de.action.test");
  249. EXPECT_TRUE(enabledOutcome.IsSuccess());
  250. EXPECT_FALSE(enabledOutcome.GetValue());
  251. }
  252. enabledState = true;
  253. {
  254. auto enabledOutcome = m_actionManagerInterface->IsActionEnabled("o3de.action.test");
  255. EXPECT_TRUE(enabledOutcome.IsSuccess());
  256. EXPECT_FALSE(enabledOutcome.GetValue());
  257. }
  258. m_actionManagerInterface->UpdateAction("o3de.action.test");
  259. {
  260. auto enabledOutcome = m_actionManagerInterface->IsActionEnabled("o3de.action.test");
  261. EXPECT_TRUE(enabledOutcome.IsSuccess());
  262. EXPECT_TRUE(enabledOutcome.GetValue());
  263. }
  264. }
  265. TEST_F(ActionManagerFixture, InstallMultipleEnabledStateCallbacks)
  266. {
  267. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  268. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  269. m_actionManagerInterface->InstallEnabledStateCallback("o3de.action.test", []() { return false; });
  270. auto outcome = m_actionManagerInterface->InstallEnabledStateCallback("o3de.action.test", []() { return false; });
  271. EXPECT_TRUE(outcome.IsSuccess());
  272. }
  273. TEST_F(ActionManagerFixture, VerifyEnabledStateCallbacks)
  274. {
  275. // Results of Enabled State Callbacks are chained with AND.
  276. // So all callbacks need to return TRUE for an Action to be enabled.
  277. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  278. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  279. m_actionManagerInterface->InstallEnabledStateCallback("o3de.action.test", []() { return true; });
  280. {
  281. auto enabledOutcome = m_actionManagerInterface->IsActionEnabled("o3de.action.test");
  282. EXPECT_TRUE(enabledOutcome.IsSuccess());
  283. EXPECT_TRUE(enabledOutcome.GetValue());
  284. }
  285. m_actionManagerInterface->InstallEnabledStateCallback("o3de.action.test", []() { return false; });
  286. {
  287. auto enabledOutcome = m_actionManagerInterface->IsActionEnabled("o3de.action.test");
  288. EXPECT_TRUE(enabledOutcome.IsSuccess());
  289. EXPECT_FALSE(enabledOutcome.GetValue());
  290. }
  291. }
  292. TEST_F(ActionManagerFixture, UpdateUnregisteredAction)
  293. {
  294. auto outcome = m_actionManagerInterface->UpdateAction("o3de.action.test");
  295. EXPECT_FALSE(outcome.IsSuccess());
  296. }
  297. TEST_F(ActionManagerFixture, UpdateCheckableAction)
  298. {
  299. // Verify the ability to update the checked state of a Checkable Action.
  300. bool actionToggle = false;
  301. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  302. m_actionManagerInterface->RegisterCheckableAction(
  303. "o3de.context.test", "o3de.action.checkableTest", {},
  304. [&]()
  305. {
  306. actionToggle = !actionToggle;
  307. },
  308. [&]() -> bool
  309. {
  310. return actionToggle;
  311. }
  312. );
  313. QAction* action = m_actionManagerInternalInterface->GetAction("o3de.action.checkableTest");
  314. EXPECT_FALSE(action->isChecked());
  315. // When the property driving the action's state is changed outside the Action Manager system,
  316. // the caller should ensure the actions relying on it are updated accordingly.
  317. actionToggle = true;
  318. EXPECT_FALSE(action->isChecked());
  319. auto outcome = m_actionManagerInterface->UpdateAction("o3de.action.checkableTest");
  320. EXPECT_TRUE(outcome.IsSuccess());
  321. EXPECT_TRUE(action->isChecked());
  322. }
  323. TEST_F(ActionManagerFixture, RegisterWidgetAction)
  324. {
  325. auto outcome = m_actionManagerInterface->RegisterWidgetAction(
  326. "o3de.widgetAction.test", {},
  327. []() -> QWidget*
  328. {
  329. return nullptr;
  330. }
  331. );
  332. EXPECT_TRUE(outcome.IsSuccess());
  333. }
  334. TEST_F(ActionManagerFixture, RegisterWidgetActionTwice)
  335. {
  336. m_actionManagerInterface->RegisterWidgetAction(
  337. "o3de.widgetAction.test", {},
  338. []() -> QWidget*
  339. {
  340. return nullptr;
  341. }
  342. );
  343. auto outcome = m_actionManagerInterface->RegisterWidgetAction(
  344. "o3de.widgetAction.test", {},
  345. []() -> QWidget*
  346. {
  347. return nullptr;
  348. }
  349. );
  350. EXPECT_FALSE(outcome.IsSuccess());
  351. }
  352. TEST_F(ActionManagerFixture, VerifyWidgetActionIsRegistered)
  353. {
  354. m_actionManagerInterface->RegisterWidgetAction(
  355. "o3de.widgetAction.test",
  356. {},
  357. []() -> QWidget*
  358. {
  359. return nullptr;
  360. }
  361. );
  362. EXPECT_TRUE(m_actionManagerInterface->IsWidgetActionRegistered("o3de.widgetAction.test"));
  363. }
  364. TEST_F(ActionManagerFixture, GetWidgetActionName)
  365. {
  366. AzToolsFramework::WidgetActionProperties widgetActionProperties;
  367. widgetActionProperties.m_name = "Test Widget";
  368. m_actionManagerInterface->RegisterWidgetAction(
  369. "o3de.widgetAction.test",
  370. widgetActionProperties,
  371. []() -> QWidget*
  372. {
  373. return nullptr;
  374. }
  375. );
  376. auto outcome = m_actionManagerInterface->GetWidgetActionName("o3de.widgetAction.test");
  377. EXPECT_TRUE(outcome.IsSuccess());
  378. EXPECT_THAT(outcome.GetValue().c_str(), ::testing::StrEq("Test Widget"));
  379. }
  380. TEST_F(ActionManagerFixture, SetWidgetActionName)
  381. {
  382. AzToolsFramework::WidgetActionProperties widgetActionProperties;
  383. widgetActionProperties.m_name = "Wrong Widget Name";
  384. m_actionManagerInterface->RegisterWidgetAction(
  385. "o3de.widgetAction.test",
  386. widgetActionProperties,
  387. []() -> QWidget*
  388. {
  389. return nullptr;
  390. }
  391. );
  392. auto setOutcome = m_actionManagerInterface->SetWidgetActionName("o3de.widgetAction.test", "Correct Widget Name");
  393. EXPECT_TRUE(setOutcome.IsSuccess());
  394. auto getOutcome = m_actionManagerInterface->GetWidgetActionName("o3de.widgetAction.test");
  395. EXPECT_THAT(getOutcome.GetValue().c_str(), ::testing::StrEq("Correct Widget Name"));
  396. }
  397. TEST_F(ActionManagerFixture, GetWidgetActionCategory)
  398. {
  399. AzToolsFramework::WidgetActionProperties widgetActionProperties;
  400. widgetActionProperties.m_category = "Test Widget Category";
  401. m_actionManagerInterface->RegisterWidgetAction(
  402. "o3de.widgetAction.test",
  403. widgetActionProperties,
  404. []() -> QWidget*
  405. {
  406. return nullptr;
  407. }
  408. );
  409. auto outcome = m_actionManagerInterface->GetWidgetActionCategory("o3de.widgetAction.test");
  410. EXPECT_TRUE(outcome.IsSuccess());
  411. EXPECT_THAT(outcome.GetValue().c_str(), ::testing::StrEq("Test Widget Category"));
  412. }
  413. TEST_F(ActionManagerFixture, SetWidgetActionCategory)
  414. {
  415. AzToolsFramework::WidgetActionProperties widgetActionProperties;
  416. widgetActionProperties.m_category = "Wrong Widget Category";
  417. m_actionManagerInterface->RegisterWidgetAction(
  418. "o3de.widgetAction.test",
  419. widgetActionProperties,
  420. []() -> QWidget*
  421. {
  422. return nullptr;
  423. }
  424. );
  425. auto setOutcome = m_actionManagerInterface->SetWidgetActionCategory("o3de.widgetAction.test", "Correct Widget Category");
  426. EXPECT_TRUE(setOutcome.IsSuccess());
  427. auto getOutcome = m_actionManagerInterface->GetWidgetActionCategory("o3de.widgetAction.test");
  428. EXPECT_THAT(getOutcome.GetValue().c_str(), ::testing::StrEq("Correct Widget Category"));
  429. }
  430. TEST_F(ActionManagerFixture, RegisterActionUpdater)
  431. {
  432. auto outcome = m_actionManagerInterface->RegisterActionUpdater("o3de.updater.onTestChange");
  433. EXPECT_TRUE(outcome.IsSuccess());
  434. }
  435. TEST_F(ActionManagerFixture, AddUnregisteredActionToUpdater)
  436. {
  437. m_actionManagerInterface->RegisterActionUpdater("o3de.updater.onTestChange");
  438. auto outcome = m_actionManagerInterface->AddActionToUpdater("o3de.updater.onTestChange", "o3de.action.test");
  439. EXPECT_FALSE(outcome.IsSuccess());
  440. }
  441. TEST_F(ActionManagerFixture, AddActionToUnregisteredUpdater)
  442. {
  443. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  444. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  445. auto outcome = m_actionManagerInterface->AddActionToUpdater("o3de.updater.onTestChange", "o3de.action.test");
  446. EXPECT_FALSE(outcome.IsSuccess());
  447. }
  448. TEST_F(ActionManagerFixture, AddActionToUpdater)
  449. {
  450. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  451. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  452. m_actionManagerInterface->RegisterActionUpdater("o3de.updater.onTestChange");
  453. auto outcome = m_actionManagerInterface->AddActionToUpdater("o3de.updater.onTestChange", "o3de.action.test");
  454. EXPECT_TRUE(outcome.IsSuccess());
  455. }
  456. TEST_F(ActionManagerFixture, TriggerActionUpdater)
  457. {
  458. // Action Updaters are meant to be triggered when a specific event happens.
  459. // This will mostly be achieved by handling a notification bus and calling the
  460. // "TriggerActionUpdater" function for the updater identifier there.
  461. // This test only verifies that the underlying function works, but does not
  462. // represent the expected setup for using the system.
  463. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  464. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  465. m_actionManagerInterface->RegisterActionUpdater("o3de.updater.onTestChange");
  466. m_actionManagerInterface->AddActionToUpdater("o3de.updater.onTestChange", "o3de.action.test");
  467. bool enabledState = false;
  468. m_actionManagerInterface->InstallEnabledStateCallback("o3de.action.test", [&]() {
  469. return enabledState;
  470. });
  471. {
  472. auto enabledOutcome = m_actionManagerInterface->IsActionEnabled("o3de.action.test");
  473. EXPECT_TRUE(enabledOutcome.IsSuccess());
  474. EXPECT_FALSE(enabledOutcome.GetValue());
  475. }
  476. enabledState = true;
  477. {
  478. auto enabledOutcome = m_actionManagerInterface->IsActionEnabled("o3de.action.test");
  479. EXPECT_TRUE(enabledOutcome.IsSuccess());
  480. EXPECT_FALSE(enabledOutcome.GetValue());
  481. }
  482. m_actionManagerInterface->TriggerActionUpdater("o3de.updater.onTestChange");
  483. {
  484. auto enabledOutcome = m_actionManagerInterface->IsActionEnabled("o3de.action.test");
  485. EXPECT_TRUE(enabledOutcome.IsSuccess());
  486. EXPECT_TRUE(enabledOutcome.GetValue());
  487. }
  488. }
  489. TEST_F(ActionManagerFixture, SetUnregisteredActionContextModeOnAction)
  490. {
  491. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  492. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  493. auto outcome = m_actionManagerInterface->AssignModeToAction("o3de.context.mode.test", "o3de.action.test");
  494. EXPECT_FALSE(outcome.IsSuccess());
  495. }
  496. TEST_F(ActionManagerFixture, SetActionContextModeOnAction)
  497. {
  498. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  499. m_actionManagerInterface->RegisterActionContextMode("o3de.context.test", "o3de.context.mode.test");
  500. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  501. {
  502. auto outcome = m_actionManagerInterface->IsActionEnabled("o3de.action.test");
  503. EXPECT_TRUE(outcome.GetValue());
  504. }
  505. // Set Action to the "o3de.context.mode.test" mode and verify it's no longer enabled (since "o3de.context.test" is set to "default").
  506. m_actionManagerInterface->AssignModeToAction("o3de.context.mode.test", "o3de.action.test");
  507. {
  508. auto outcome = m_actionManagerInterface->IsActionEnabled("o3de.action.test");
  509. EXPECT_FALSE(outcome.GetValue());
  510. }
  511. }
  512. TEST_F(ActionManagerFixture, SetActionContextDefaultModeOnAction)
  513. {
  514. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  515. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  516. {
  517. auto outcome = m_actionManagerInterface->IsActionEnabled("o3de.action.test");
  518. EXPECT_TRUE(outcome.GetValue());
  519. }
  520. // Set Action to the "default" mode and verify it's still enabled (since "o3de.context.test" is set to "default").
  521. m_actionManagerInterface->AssignModeToAction("default", "o3de.action.test");
  522. {
  523. auto outcome = m_actionManagerInterface->IsActionEnabled("o3de.action.test");
  524. EXPECT_TRUE(outcome.GetValue());
  525. }
  526. }
  527. TEST_F(ActionManagerFixture, ChangeModeAndVerifyActionWithNoSetMode)
  528. {
  529. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  530. m_actionManagerInterface->RegisterActionContextMode("o3de.context.test", "o3de.context.mode.test");
  531. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  532. {
  533. auto outcome = m_actionManagerInterface->IsActionEnabled("o3de.action.test");
  534. EXPECT_TRUE(outcome.GetValue());
  535. }
  536. // Set the Action Context Mode to "o3de.context.mode.test" and verify that the action is still enabled.
  537. m_actionManagerInterface->SetActiveActionContextMode("o3de.context.test", "o3de.context.mode.test");
  538. {
  539. auto outcome = m_actionManagerInterface->IsActionEnabled("o3de.action.test");
  540. EXPECT_TRUE(outcome.GetValue());
  541. }
  542. }
  543. TEST_F(ActionManagerFixture, ChangeModeAndVerifyActionSetToDefaultMode)
  544. {
  545. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  546. m_actionManagerInterface->RegisterActionContextMode("o3de.context.test", "o3de.context.mode.test");
  547. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test", {}, []{});
  548. {
  549. auto outcome = m_actionManagerInterface->IsActionEnabled("o3de.action.test");
  550. EXPECT_TRUE(outcome.GetValue());
  551. }
  552. // Set Action to the "default" mode and verify it's still enabled (since "o3de.context.test" is set to "default").
  553. m_actionManagerInterface->AssignModeToAction("default", "o3de.action.test");
  554. {
  555. auto outcome = m_actionManagerInterface->IsActionEnabled("o3de.action.test");
  556. EXPECT_TRUE(outcome.GetValue());
  557. }
  558. // Set the Action Context Mode to "o3de.context.mode.test" and verify that the action is now disabled.
  559. m_actionManagerInterface->SetActiveActionContextMode("o3de.context.test", "o3de.context.mode.test");
  560. {
  561. auto outcome = m_actionManagerInterface->IsActionEnabled("o3de.action.test");
  562. EXPECT_FALSE(outcome.GetValue());
  563. }
  564. }
  565. TEST_F(ActionManagerFixture, ModeSwitchingTest)
  566. {
  567. m_actionManagerInterface->RegisterActionContext("o3de.context.test", {});
  568. m_actionManagerInterface->RegisterActionContextMode("o3de.context.test", "o3de.context.mode.test1");
  569. m_actionManagerInterface->RegisterActionContextMode("o3de.context.test", "o3de.context.mode.test2");
  570. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.testDefault", {}, []{});
  571. m_actionManagerInterface->AssignModeToAction("default", "o3de.action.testDefault");
  572. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test1", {}, []{});
  573. m_actionManagerInterface->AssignModeToAction("o3de.context.mode.test1", "o3de.action.test1");
  574. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.test2", {}, []{});
  575. m_actionManagerInterface->AssignModeToAction("o3de.context.mode.test2", "o3de.action.test2");
  576. m_actionManagerInterface->RegisterAction("o3de.context.test", "o3de.action.testAll", {}, []{});
  577. // At the beginning, "o3de.context.test" is set to mode "default".
  578. EXPECT_TRUE(m_actionManagerInterface->IsActionEnabled("o3de.action.testDefault").GetValue());
  579. EXPECT_FALSE(m_actionManagerInterface->IsActionEnabled("o3de.action.test1").GetValue());
  580. EXPECT_FALSE(m_actionManagerInterface->IsActionEnabled("o3de.action.test2").GetValue());
  581. EXPECT_TRUE(m_actionManagerInterface->IsActionEnabled("o3de.action.testAll").GetValue());
  582. // Set the Action Context Mode to "o3de.context.mode.test1" and verify that the actions are updated accordingly.
  583. m_actionManagerInterface->SetActiveActionContextMode("o3de.context.test", "o3de.context.mode.test1");
  584. EXPECT_FALSE(m_actionManagerInterface->IsActionEnabled("o3de.action.testDefault").GetValue());
  585. EXPECT_TRUE(m_actionManagerInterface->IsActionEnabled("o3de.action.test1").GetValue());
  586. EXPECT_FALSE(m_actionManagerInterface->IsActionEnabled("o3de.action.test2").GetValue());
  587. EXPECT_TRUE(m_actionManagerInterface->IsActionEnabled("o3de.action.testAll").GetValue());
  588. // Set the Action Context Mode to "o3de.context.mode.test2" and verify that the actions are updated accordingly.
  589. m_actionManagerInterface->SetActiveActionContextMode("o3de.context.test", "o3de.context.mode.test2");
  590. EXPECT_FALSE(m_actionManagerInterface->IsActionEnabled("o3de.action.testDefault").GetValue());
  591. EXPECT_FALSE(m_actionManagerInterface->IsActionEnabled("o3de.action.test1").GetValue());
  592. EXPECT_TRUE(m_actionManagerInterface->IsActionEnabled("o3de.action.test2").GetValue());
  593. EXPECT_TRUE(m_actionManagerInterface->IsActionEnabled("o3de.action.testAll").GetValue());
  594. // Set the Action Context Mode back to "default" and verify that the actions are updated accordingly.
  595. m_actionManagerInterface->SetActiveActionContextMode("o3de.context.test", "default");
  596. EXPECT_TRUE(m_actionManagerInterface->IsActionEnabled("o3de.action.testDefault").GetValue());
  597. EXPECT_FALSE(m_actionManagerInterface->IsActionEnabled("o3de.action.test1").GetValue());
  598. EXPECT_FALSE(m_actionManagerInterface->IsActionEnabled("o3de.action.test2").GetValue());
  599. EXPECT_TRUE(m_actionManagerInterface->IsActionEnabled("o3de.action.testAll").GetValue());
  600. }
  601. } // namespace UnitTest