ax_node_data_unittest.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/accessibility/ax_node_data.h"
  5. #include <set>
  6. #include <string>
  7. #include <unordered_set>
  8. #include <utility>
  9. #include "base/containers/contains.h"
  10. #include "base/test/gtest_util.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. #include "ui/accessibility/ax_enum_util.h"
  13. #include "ui/accessibility/ax_enums.mojom.h"
  14. #include "ui/accessibility/ax_role_properties.h"
  15. #include "ui/accessibility/ax_text_attributes.h"
  16. namespace ui {
  17. TEST(AXNodeDataTest, GetAndSetCheckedState) {
  18. AXNodeData root;
  19. EXPECT_EQ(ax::mojom::CheckedState::kNone, root.GetCheckedState());
  20. EXPECT_FALSE(root.HasIntAttribute(ax::mojom::IntAttribute::kCheckedState));
  21. root.SetCheckedState(ax::mojom::CheckedState::kMixed);
  22. EXPECT_EQ(ax::mojom::CheckedState::kMixed, root.GetCheckedState());
  23. EXPECT_TRUE(root.HasIntAttribute(ax::mojom::IntAttribute::kCheckedState));
  24. root.SetCheckedState(ax::mojom::CheckedState::kFalse);
  25. EXPECT_EQ(ax::mojom::CheckedState::kFalse, root.GetCheckedState());
  26. EXPECT_TRUE(root.HasIntAttribute(ax::mojom::IntAttribute::kCheckedState));
  27. root.SetCheckedState(ax::mojom::CheckedState::kNone);
  28. EXPECT_EQ(ax::mojom::CheckedState::kNone, root.GetCheckedState());
  29. EXPECT_FALSE(root.HasIntAttribute(ax::mojom::IntAttribute::kCheckedState));
  30. }
  31. TEST(AXNodeDataTest, TextAttributes) {
  32. AXNodeData node_1;
  33. node_1.AddFloatAttribute(ax::mojom::FloatAttribute::kFontSize, 1.5);
  34. AXNodeData node_2;
  35. node_2.AddFloatAttribute(ax::mojom::FloatAttribute::kFontSize, 1.5);
  36. EXPECT_TRUE(node_1.GetTextAttributes() == node_2.GetTextAttributes());
  37. node_2.AddIntAttribute(ax::mojom::IntAttribute::kColor, 100);
  38. EXPECT_TRUE(node_1.GetTextAttributes() != node_2.GetTextAttributes());
  39. node_1.AddIntAttribute(ax::mojom::IntAttribute::kColor, 100);
  40. EXPECT_TRUE(node_1.GetTextAttributes() == node_2.GetTextAttributes());
  41. node_2.RemoveIntAttribute(ax::mojom::IntAttribute::kColor);
  42. EXPECT_TRUE(node_1.GetTextAttributes() != node_2.GetTextAttributes());
  43. node_2.AddIntAttribute(ax::mojom::IntAttribute::kColor, 100);
  44. EXPECT_TRUE(node_1.GetTextAttributes() == node_2.GetTextAttributes());
  45. node_1.AddStringAttribute(ax::mojom::StringAttribute::kFontFamily,
  46. "test font");
  47. EXPECT_TRUE(node_1.GetTextAttributes() != node_2.GetTextAttributes());
  48. node_2.AddStringAttribute(ax::mojom::StringAttribute::kFontFamily,
  49. "test font");
  50. EXPECT_TRUE(node_1.GetTextAttributes() == node_2.GetTextAttributes());
  51. node_2.RemoveStringAttribute(ax::mojom::StringAttribute::kFontFamily);
  52. EXPECT_TRUE(node_1.GetTextAttributes() != node_2.GetTextAttributes());
  53. node_2.AddStringAttribute(ax::mojom::StringAttribute::kFontFamily,
  54. "test font");
  55. EXPECT_TRUE(node_1.GetTextAttributes() == node_2.GetTextAttributes());
  56. node_2.AddStringAttribute(ax::mojom::StringAttribute::kFontFamily,
  57. "different font");
  58. EXPECT_TRUE(node_1.GetTextAttributes() != node_2.GetTextAttributes());
  59. std::string tooltip;
  60. node_2.AddStringAttribute(ax::mojom::StringAttribute::kTooltip,
  61. "test tooltip");
  62. EXPECT_TRUE(node_2.GetStringAttribute(ax::mojom::StringAttribute::kTooltip,
  63. &tooltip));
  64. EXPECT_EQ(tooltip, "test tooltip");
  65. AXTextAttributes node1_attributes = node_1.GetTextAttributes();
  66. AXTextAttributes moved_attributes = std::move(node1_attributes);
  67. EXPECT_TRUE(node1_attributes != moved_attributes);
  68. EXPECT_TRUE(moved_attributes == node_1.GetTextAttributes());
  69. }
  70. TEST(AXNodeDataTest, IsButtonPressed) {
  71. // A non-button element with CheckedState::kTrue should not return true for
  72. // IsButtonPressed.
  73. AXNodeData non_button_pressed;
  74. non_button_pressed.role = ax::mojom::Role::kGenericContainer;
  75. non_button_pressed.SetCheckedState(ax::mojom::CheckedState::kTrue);
  76. EXPECT_FALSE(IsButton(non_button_pressed.role));
  77. EXPECT_FALSE(non_button_pressed.IsButtonPressed());
  78. // A button element with CheckedState::kTrue should return true for
  79. // IsButtonPressed.
  80. AXNodeData button_pressed;
  81. button_pressed.role = ax::mojom::Role::kButton;
  82. button_pressed.SetCheckedState(ax::mojom::CheckedState::kTrue);
  83. EXPECT_TRUE(IsButton(button_pressed.role));
  84. EXPECT_TRUE(button_pressed.IsButtonPressed());
  85. button_pressed.role = ax::mojom::Role::kPopUpButton;
  86. EXPECT_TRUE(IsButton(button_pressed.role));
  87. EXPECT_TRUE(button_pressed.IsButtonPressed());
  88. button_pressed.role = ax::mojom::Role::kToggleButton;
  89. EXPECT_TRUE(IsButton(button_pressed.role));
  90. EXPECT_TRUE(button_pressed.IsButtonPressed());
  91. // A button element does not have CheckedState::kTrue should return false for
  92. // IsButtonPressed.
  93. AXNodeData button_not_pressed;
  94. button_not_pressed.role = ax::mojom::Role::kButton;
  95. button_not_pressed.SetCheckedState(ax::mojom::CheckedState::kNone);
  96. EXPECT_TRUE(IsButton(button_not_pressed.role));
  97. EXPECT_FALSE(button_not_pressed.IsButtonPressed());
  98. button_not_pressed.role = ax::mojom::Role::kPopUpButton;
  99. button_not_pressed.SetCheckedState(ax::mojom::CheckedState::kFalse);
  100. EXPECT_TRUE(IsButton(button_not_pressed.role));
  101. EXPECT_FALSE(button_not_pressed.IsButtonPressed());
  102. button_not_pressed.role = ax::mojom::Role::kToggleButton;
  103. button_not_pressed.SetCheckedState(ax::mojom::CheckedState::kMixed);
  104. EXPECT_TRUE(IsButton(button_not_pressed.role));
  105. EXPECT_FALSE(button_not_pressed.IsButtonPressed());
  106. }
  107. TEST(AXNodeDataTest, IsClickable) {
  108. // Test for ax node data attribute with a custom default action verb.
  109. AXNodeData data_default_action_verb;
  110. for (int action_verb_idx =
  111. static_cast<int>(ax::mojom::DefaultActionVerb::kMinValue);
  112. action_verb_idx <=
  113. static_cast<int>(ax::mojom::DefaultActionVerb::kMaxValue);
  114. action_verb_idx++) {
  115. data_default_action_verb.SetDefaultActionVerb(
  116. static_cast<ax::mojom::DefaultActionVerb>(action_verb_idx));
  117. bool is_clickable = data_default_action_verb.IsClickable();
  118. SCOPED_TRACE(testing::Message()
  119. << "ax::mojom::DefaultActionVerb="
  120. << ToString(data_default_action_verb.GetDefaultActionVerb())
  121. << ", Actual: isClickable=" << is_clickable
  122. << ", Expected: isClickable=" << !is_clickable);
  123. if (data_default_action_verb.GetDefaultActionVerb() ==
  124. ax::mojom::DefaultActionVerb::kClickAncestor ||
  125. data_default_action_verb.GetDefaultActionVerb() ==
  126. ax::mojom::DefaultActionVerb::kNone)
  127. EXPECT_FALSE(is_clickable);
  128. else
  129. EXPECT_TRUE(is_clickable);
  130. }
  131. // Test for iterating through all roles and validate if a role is clickable.
  132. std::set<ax::mojom::Role> roles_expected_is_clickable = {
  133. ax::mojom::Role::kButton,
  134. ax::mojom::Role::kCheckBox,
  135. ax::mojom::Role::kColorWell,
  136. ax::mojom::Role::kComboBoxMenuButton,
  137. ax::mojom::Role::kDate,
  138. ax::mojom::Role::kDateTime,
  139. ax::mojom::Role::kDisclosureTriangle,
  140. ax::mojom::Role::kDocBackLink,
  141. ax::mojom::Role::kDocBiblioRef,
  142. ax::mojom::Role::kDocGlossRef,
  143. ax::mojom::Role::kDocNoteRef,
  144. ax::mojom::Role::kImeCandidate,
  145. ax::mojom::Role::kInputTime,
  146. ax::mojom::Role::kLink,
  147. ax::mojom::Role::kListBox,
  148. ax::mojom::Role::kListBoxOption,
  149. ax::mojom::Role::kMenuItem,
  150. ax::mojom::Role::kMenuItemCheckBox,
  151. ax::mojom::Role::kMenuItemRadio,
  152. ax::mojom::Role::kMenuListOption,
  153. ax::mojom::Role::kPdfActionableHighlight,
  154. ax::mojom::Role::kPopUpButton,
  155. ax::mojom::Role::kPortal,
  156. ax::mojom::Role::kRadioButton,
  157. ax::mojom::Role::kSearchBox,
  158. ax::mojom::Role::kSpinButton,
  159. ax::mojom::Role::kSwitch,
  160. ax::mojom::Role::kTab,
  161. ax::mojom::Role::kTextField,
  162. ax::mojom::Role::kTextFieldWithComboBox,
  163. ax::mojom::Role::kToggleButton};
  164. AXNodeData data;
  165. for (int role_idx = static_cast<int>(ax::mojom::Role::kMinValue);
  166. role_idx <= static_cast<int>(ax::mojom::Role::kMaxValue); role_idx++) {
  167. data.role = static_cast<ax::mojom::Role>(role_idx);
  168. bool is_clickable = data.IsClickable();
  169. SCOPED_TRACE(testing::Message()
  170. << "ax::mojom::Role=" << ToString(data.role)
  171. << ", Actual: isClickable=" << is_clickable
  172. << ", Expected: isClickable=" << !is_clickable);
  173. EXPECT_EQ(base::Contains(roles_expected_is_clickable, data.role),
  174. is_clickable);
  175. }
  176. }
  177. TEST(AXNodeDataTest, IsInvocable) {
  178. // Test for iterating through all roles and validate if a role is invocable.
  179. // A role is invocable if it is clickable and supports neither expand collapse
  180. // nor toggle. A link should always be invocable, regardless of whether it is
  181. // clickable or supports expand/collapse or toggle.
  182. AXNodeData data;
  183. for (int role_idx = static_cast<int>(ax::mojom::Role::kMinValue);
  184. role_idx <= static_cast<int>(ax::mojom::Role::kMaxValue); role_idx++) {
  185. data.role = static_cast<ax::mojom::Role>(role_idx);
  186. bool is_activatable = data.IsActivatable();
  187. const bool supports_expand_collapse = data.SupportsExpandCollapse();
  188. const bool supports_toggle = ui::SupportsToggle(data.role);
  189. const bool is_clickable = data.IsClickable();
  190. const bool is_invocable = data.IsInvocable();
  191. SCOPED_TRACE(testing::Message()
  192. << "ax::mojom::Role=" << ToString(data.role)
  193. << ", isClickable=" << is_clickable << ", isActivatable="
  194. << is_activatable << ", supportsToggle=" << supports_toggle
  195. << ", supportsExpandCollapse=" << supports_expand_collapse
  196. << ", Actual: isInvocable=" << is_invocable
  197. << ", Expected: isInvocable=" << !is_invocable);
  198. if (ui::IsLink(data.role) ||
  199. (is_clickable && !is_activatable && !supports_toggle &&
  200. !supports_expand_collapse)) {
  201. EXPECT_TRUE(is_invocable);
  202. } else {
  203. EXPECT_FALSE(is_invocable);
  204. }
  205. }
  206. }
  207. TEST(AXNodeDataTest, IsMenuButton) {
  208. // A non button element should return false for IsMenuButton.
  209. AXNodeData non_button;
  210. non_button.role = ax::mojom::Role::kGenericContainer;
  211. EXPECT_FALSE(IsButton(non_button.role));
  212. EXPECT_FALSE(non_button.IsMenuButton());
  213. // Only button element with HasPopup::kMenu should return true for
  214. // IsMenuButton. All other ax::mojom::HasPopup types should return false.
  215. AXNodeData button_with_popup;
  216. button_with_popup.role = ax::mojom::Role::kButton;
  217. for (int has_popup_idx = static_cast<int>(ax::mojom::HasPopup::kMinValue);
  218. has_popup_idx <= static_cast<int>(ax::mojom::HasPopup::kMaxValue);
  219. has_popup_idx++) {
  220. button_with_popup.SetHasPopup(
  221. static_cast<ax::mojom::HasPopup>(has_popup_idx));
  222. bool is_menu_button = button_with_popup.IsMenuButton();
  223. SCOPED_TRACE(testing::Message()
  224. << "ax::mojom::Role=" << ToString(button_with_popup.role)
  225. << ", hasPopup=" << button_with_popup.GetHasPopup()
  226. << ", Actual: isMenuButton=" << is_menu_button
  227. << ", Expected: isMenuButton=" << !is_menu_button);
  228. if (IsButton(button_with_popup.role) &&
  229. button_with_popup.GetHasPopup() == ax::mojom::HasPopup::kMenu)
  230. EXPECT_TRUE(is_menu_button);
  231. else
  232. EXPECT_FALSE(is_menu_button);
  233. }
  234. }
  235. TEST(AXNodeDataTest, SupportsExpandCollapse) {
  236. // Test for iterating through all hasPopup attributes and validate if a
  237. // hasPopup attribute supports expand collapse.
  238. AXNodeData data_has_popup;
  239. for (int has_popup_idx = static_cast<int>(ax::mojom::HasPopup::kMinValue);
  240. has_popup_idx <= static_cast<int>(ax::mojom::HasPopup::kMaxValue);
  241. has_popup_idx++) {
  242. data_has_popup.SetHasPopup(static_cast<ax::mojom::HasPopup>(has_popup_idx));
  243. bool supports_expand_collapse = data_has_popup.SupportsExpandCollapse();
  244. SCOPED_TRACE(testing::Message() << "ax::mojom::HasPopup="
  245. << ToString(data_has_popup.GetHasPopup())
  246. << ", Actual: supportsExpandCollapse="
  247. << supports_expand_collapse
  248. << ", Expected: supportsExpandCollapse="
  249. << !supports_expand_collapse);
  250. if (data_has_popup.GetHasPopup() == ax::mojom::HasPopup::kFalse)
  251. EXPECT_FALSE(supports_expand_collapse);
  252. else
  253. EXPECT_TRUE(supports_expand_collapse);
  254. }
  255. // Test for iterating through all states and validate if a state supports
  256. // expand collapse.
  257. AXNodeData data_state;
  258. for (int state_idx = static_cast<int>(ax::mojom::State::kMinValue);
  259. state_idx <= static_cast<int>(ax::mojom::State::kMaxValue);
  260. state_idx++) {
  261. ax::mojom::State state = static_cast<ax::mojom::State>(state_idx);
  262. // skipping kNone here because AXNodeData::AddState, RemoveState forbids
  263. // kNone to be added/removed and would fail DCHECK.
  264. if (state == ax::mojom::State::kNone)
  265. continue;
  266. data_state.AddState(state);
  267. bool supports_expand_collapse = data_state.SupportsExpandCollapse();
  268. SCOPED_TRACE(testing::Message() << "ax::mojom::State=" << ToString(state)
  269. << ", Actual: supportsExpandCollapse="
  270. << supports_expand_collapse
  271. << ", Expected: supportsExpandCollapse="
  272. << !supports_expand_collapse);
  273. if (data_state.HasState(ax::mojom::State::kExpanded) ||
  274. data_state.HasState(ax::mojom::State::kCollapsed))
  275. EXPECT_TRUE(supports_expand_collapse);
  276. else
  277. EXPECT_FALSE(supports_expand_collapse);
  278. data_state.RemoveState(state);
  279. }
  280. // Test for iterating through all roles and validate if a role supports expand
  281. // collapse.
  282. AXNodeData data;
  283. std::unordered_set<ax::mojom::Role> roles_expected_supports_expand_collapse =
  284. {ax::mojom::Role::kComboBoxGrouping, ax::mojom::Role::kComboBoxMenuButton,
  285. ax::mojom::Role::kDisclosureTriangle,
  286. ax::mojom::Role::kTextFieldWithComboBox, ax::mojom::Role::kTreeItem};
  287. for (int role_idx = static_cast<int>(ax::mojom::Role::kMinValue);
  288. role_idx <= static_cast<int>(ax::mojom::Role::kMaxValue); role_idx++) {
  289. data.role = static_cast<ax::mojom::Role>(role_idx);
  290. bool supports_expand_collapse = data.SupportsExpandCollapse();
  291. SCOPED_TRACE(testing::Message() << "ax::mojom::Role=" << ToString(data.role)
  292. << ", Actual: supportsExpandCollapse="
  293. << supports_expand_collapse
  294. << ", Expected: supportsExpandCollapse="
  295. << !supports_expand_collapse);
  296. if (roles_expected_supports_expand_collapse.find(data.role) !=
  297. roles_expected_supports_expand_collapse.end())
  298. EXPECT_TRUE(supports_expand_collapse);
  299. else
  300. EXPECT_FALSE(supports_expand_collapse);
  301. }
  302. }
  303. TEST(AXNodeDataTest, SetName) {
  304. AXNodeData data;
  305. // SetName should not be called on a role of kNone. That role is used for
  306. // presentational objects which should not be included in the accessibility
  307. // tree. This is enforced by a DCHECK.
  308. data.role = ax::mojom::Role::kNone;
  309. EXPECT_DCHECK_DEATH(data.SetName("role is presentational"));
  310. // For roles other than text, setting the name should result in the NameFrom
  311. // source being kAttribute.
  312. data.role = ax::mojom::Role::kButton;
  313. data.SetName("foo");
  314. EXPECT_EQ("foo", data.GetStringAttribute(ax::mojom::StringAttribute::kName));
  315. EXPECT_EQ(data.GetNameFrom(), ax::mojom::NameFrom::kAttribute);
  316. // TODO(accessibility): The static text role should have a NameFrom source of
  317. // kContents. But nothing clears the NameFrom if the role of an existing
  318. // object changes because currently there is no AXNodeData::SetRole method.
  319. data.role = ax::mojom::Role::kStaticText;
  320. data.SetName("bar");
  321. EXPECT_EQ("bar", data.GetStringAttribute(ax::mojom::StringAttribute::kName));
  322. EXPECT_EQ(data.GetNameFrom(), ax::mojom::NameFrom::kAttribute);
  323. data.RemoveIntAttribute(ax::mojom::IntAttribute::kNameFrom);
  324. data.SetName("baz");
  325. EXPECT_EQ("baz", data.GetStringAttribute(ax::mojom::StringAttribute::kName));
  326. EXPECT_EQ(data.GetNameFrom(), ax::mojom::NameFrom::kContents);
  327. data.SetNameExplicitlyEmpty();
  328. EXPECT_EQ("", data.GetStringAttribute(ax::mojom::StringAttribute::kName));
  329. EXPECT_EQ(data.GetNameFrom(), ax::mojom::NameFrom::kAttributeExplicitlyEmpty);
  330. data.SetName("foo");
  331. EXPECT_EQ("foo", data.GetStringAttribute(ax::mojom::StringAttribute::kName));
  332. EXPECT_EQ(data.GetNameFrom(), ax::mojom::NameFrom::kContents);
  333. }
  334. TEST(AXNodeDataTest, BitFieldsSanityCheck) {
  335. EXPECT_LT(static_cast<size_t>(ax::mojom::State::kMaxValue),
  336. sizeof(AXNodeData::state) * 8);
  337. EXPECT_LT(static_cast<size_t>(ax::mojom::Action::kMaxValue),
  338. sizeof(AXNodeData::actions) * 8);
  339. }
  340. } // namespace ui