| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/accessibility/ax_tree.h" | 5 #include "ui/accessibility/ax_tree.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 | 11 |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/stringprintf.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "ui/accessibility/ax_node.h" | 15 #include "ui/accessibility/ax_node.h" |
| 15 #include "ui/accessibility/ax_serializable_tree.h" | 16 #include "ui/accessibility/ax_serializable_tree.h" |
| 16 #include "ui/accessibility/ax_tree_serializer.h" | 17 #include "ui/accessibility/ax_tree_serializer.h" |
| 17 | 18 |
| 19 using base::DoubleToString; |
| 20 using base::IntToString; |
| 21 using base::StringPrintf; |
| 22 |
| 18 namespace ui { | 23 namespace ui { |
| 19 | 24 |
| 20 namespace { | 25 namespace { |
| 21 | 26 |
| 27 std::string IntVectorToString(const std::vector<int>& items) { |
| 28 std::string str; |
| 29 for (size_t i = 0; i < items.size(); ++i) { |
| 30 if (i > 0) |
| 31 str += ","; |
| 32 str += IntToString(items[i]); |
| 33 } |
| 34 return str; |
| 35 } |
| 36 |
| 22 class FakeAXTreeDelegate : public AXTreeDelegate { | 37 class FakeAXTreeDelegate : public AXTreeDelegate { |
| 23 public: | 38 public: |
| 24 FakeAXTreeDelegate() | 39 FakeAXTreeDelegate() |
| 25 : tree_data_changed_(false), | 40 : tree_data_changed_(false), |
| 26 root_changed_(false) {} | 41 root_changed_(false) {} |
| 27 | 42 |
| 28 void OnNodeDataWillChange(AXTree* tree, | 43 void OnNodeDataWillChange(AXTree* tree, |
| 29 const AXNodeData& old_node_data, | 44 const AXNodeData& old_node_data, |
| 30 const AXNodeData& new_node_data) override {} | 45 const AXNodeData& new_node_data) override {} |
| 31 void OnTreeDataChanged(AXTree* tree) override { | 46 void OnTreeDataChanged(AXTree* tree) override { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 case SUBTREE_REPARENTED: | 89 case SUBTREE_REPARENTED: |
| 75 subtree_reparented_finished_ids_.push_back(id); | 90 subtree_reparented_finished_ids_.push_back(id); |
| 76 break; | 91 break; |
| 77 case NODE_CHANGED: | 92 case NODE_CHANGED: |
| 78 change_finished_ids_.push_back(id); | 93 change_finished_ids_.push_back(id); |
| 79 break; | 94 break; |
| 80 } | 95 } |
| 81 } | 96 } |
| 82 } | 97 } |
| 83 | 98 |
| 99 void OnRoleChanged(AXTree* tree, |
| 100 AXNode* node, |
| 101 AXRole old_role, |
| 102 AXRole new_role) override { |
| 103 attribute_change_log_.push_back(StringPrintf("Role changed from %s to %s", |
| 104 ToString(old_role).c_str(), |
| 105 ToString(new_role).c_str())); |
| 106 } |
| 107 |
| 108 void OnStateChanged(AXTree* tree, |
| 109 AXNode* node, |
| 110 AXState state, |
| 111 bool new_value) override { |
| 112 attribute_change_log_.push_back(StringPrintf("%s changed to %s", |
| 113 ToString(state).c_str(), |
| 114 new_value ? "true" : "false")); |
| 115 } |
| 116 |
| 117 void OnStringAttributeChanged(AXTree* tree, |
| 118 AXNode* node, |
| 119 AXStringAttribute attr, |
| 120 const std::string& old_value, |
| 121 const std::string& new_value) override { |
| 122 attribute_change_log_.push_back( |
| 123 StringPrintf("%s changed from %s to %s", ToString(attr).c_str(), |
| 124 old_value.c_str(), new_value.c_str())); |
| 125 } |
| 126 |
| 127 void OnIntAttributeChanged(AXTree* tree, |
| 128 AXNode* node, |
| 129 AXIntAttribute attr, |
| 130 int32_t old_value, |
| 131 int32_t new_value) override { |
| 132 attribute_change_log_.push_back(StringPrintf("%s changed from %d to %d", |
| 133 ToString(attr).c_str(), |
| 134 old_value, new_value)); |
| 135 } |
| 136 |
| 137 void OnFloatAttributeChanged(AXTree* tree, |
| 138 AXNode* node, |
| 139 AXFloatAttribute attr, |
| 140 float old_value, |
| 141 float new_value) override { |
| 142 attribute_change_log_.push_back(StringPrintf( |
| 143 "%s changed from %s to %s", ToString(attr).c_str(), |
| 144 DoubleToString(old_value).c_str(), DoubleToString(new_value).c_str())); |
| 145 } |
| 146 |
| 147 void OnBoolAttributeChanged(AXTree* tree, |
| 148 AXNode* node, |
| 149 AXBoolAttribute attr, |
| 150 bool new_value) override { |
| 151 attribute_change_log_.push_back(StringPrintf("%s changed to %s", |
| 152 ToString(attr).c_str(), |
| 153 new_value ? "true" : "false")); |
| 154 } |
| 155 |
| 156 void OnIntListAttributeChanged( |
| 157 AXTree* tree, |
| 158 AXNode* node, |
| 159 AXIntListAttribute attr, |
| 160 const std::vector<int32_t>& old_value, |
| 161 const std::vector<int32_t>& new_value) override { |
| 162 attribute_change_log_.push_back( |
| 163 StringPrintf("%s changed from %s to %s", ToString(attr).c_str(), |
| 164 IntVectorToString(old_value).c_str(), |
| 165 IntVectorToString(new_value).c_str())); |
| 166 } |
| 167 |
| 84 bool tree_data_changed() const { return tree_data_changed_; } | 168 bool tree_data_changed() const { return tree_data_changed_; } |
| 85 bool root_changed() const { return root_changed_; } | 169 bool root_changed() const { return root_changed_; } |
| 86 const std::vector<int32_t>& deleted_ids() { return deleted_ids_; } | 170 const std::vector<int32_t>& deleted_ids() { return deleted_ids_; } |
| 87 const std::vector<int32_t>& subtree_deleted_ids() { | 171 const std::vector<int32_t>& subtree_deleted_ids() { |
| 88 return subtree_deleted_ids_; | 172 return subtree_deleted_ids_; |
| 89 } | 173 } |
| 90 const std::vector<int32_t>& created_ids() { return created_ids_; } | 174 const std::vector<int32_t>& created_ids() { return created_ids_; } |
| 91 const std::vector<int32_t>& node_creation_finished_ids() { | 175 const std::vector<int32_t>& node_creation_finished_ids() { |
| 92 return node_creation_finished_ids_; | 176 return node_creation_finished_ids_; |
| 93 } | 177 } |
| 94 const std::vector<int32_t>& subtree_creation_finished_ids() { | 178 const std::vector<int32_t>& subtree_creation_finished_ids() { |
| 95 return subtree_creation_finished_ids_; | 179 return subtree_creation_finished_ids_; |
| 96 } | 180 } |
| 97 const std::vector<int32_t>& node_reparented_finished_ids() { | 181 const std::vector<int32_t>& node_reparented_finished_ids() { |
| 98 return node_reparented_finished_ids_; | 182 return node_reparented_finished_ids_; |
| 99 } | 183 } |
| 100 const std::vector<int32_t>& subtree_reparented_finished_ids() { | 184 const std::vector<int32_t>& subtree_reparented_finished_ids() { |
| 101 return subtree_reparented_finished_ids_; | 185 return subtree_reparented_finished_ids_; |
| 102 } | 186 } |
| 103 const std::vector<int32_t>& change_finished_ids() { | 187 const std::vector<int32_t>& change_finished_ids() { |
| 104 return change_finished_ids_; | 188 return change_finished_ids_; |
| 105 } | 189 } |
| 190 const std::vector<std::string>& attribute_change_log() { |
| 191 return attribute_change_log_; |
| 192 } |
| 106 | 193 |
| 107 private: | 194 private: |
| 108 bool tree_data_changed_; | 195 bool tree_data_changed_; |
| 109 bool root_changed_; | 196 bool root_changed_; |
| 110 std::vector<int32_t> deleted_ids_; | 197 std::vector<int32_t> deleted_ids_; |
| 111 std::vector<int32_t> subtree_deleted_ids_; | 198 std::vector<int32_t> subtree_deleted_ids_; |
| 112 std::vector<int32_t> created_ids_; | 199 std::vector<int32_t> created_ids_; |
| 113 std::vector<int32_t> changed_ids_; | 200 std::vector<int32_t> changed_ids_; |
| 114 std::vector<int32_t> node_creation_finished_ids_; | 201 std::vector<int32_t> node_creation_finished_ids_; |
| 115 std::vector<int32_t> subtree_creation_finished_ids_; | 202 std::vector<int32_t> subtree_creation_finished_ids_; |
| 116 std::vector<int32_t> node_reparented_finished_ids_; | 203 std::vector<int32_t> node_reparented_finished_ids_; |
| 117 std::vector<int32_t> subtree_reparented_finished_ids_; | 204 std::vector<int32_t> subtree_reparented_finished_ids_; |
| 118 std::vector<int32_t> change_finished_ids_; | 205 std::vector<int32_t> change_finished_ids_; |
| 206 std::vector<std::string> attribute_change_log_; |
| 119 }; | 207 }; |
| 120 | 208 |
| 121 } // namespace | 209 } // namespace |
| 122 | 210 |
| 123 TEST(AXTreeTest, SerializeSimpleAXTree) { | 211 TEST(AXTreeTest, SerializeSimpleAXTree) { |
| 124 AXNodeData root; | 212 AXNodeData root; |
| 125 root.id = 1; | 213 root.id = 1; |
| 126 root.role = AX_ROLE_DIALOG; | 214 root.role = AX_ROLE_DIALOG; |
| 127 root.state = 1 << AX_STATE_FOCUSABLE; | 215 root.state = 1 << AX_STATE_FOCUSABLE; |
| 128 root.location = gfx::RectF(0, 0, 800, 600); | 216 root.location = gfx::RectF(0, 0, 800, 600); |
| (...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 525 node2.id = 1; | 613 node2.id = 1; |
| 526 node2.state = 0; | 614 node2.state = 0; |
| 527 node2.child_ids.push_back(1); | 615 node2.child_ids.push_back(1); |
| 528 node2.child_ids.push_back(1); | 616 node2.child_ids.push_back(1); |
| 529 initial_state.nodes.push_back(node2); | 617 initial_state.nodes.push_back(node2); |
| 530 | 618 |
| 531 ui::AXTree tree; | 619 ui::AXTree tree; |
| 532 tree.Unserialize(initial_state); | 620 tree.Unserialize(initial_state); |
| 533 } | 621 } |
| 534 | 622 |
| 623 TEST(AXTreeTest, RoleAndStateChangeCallbacks) { |
| 624 AXTreeUpdate initial_state; |
| 625 initial_state.root_id = 1; |
| 626 initial_state.nodes.resize(1); |
| 627 initial_state.nodes[0].id = 1; |
| 628 initial_state.nodes[0].role = AX_ROLE_BUTTON; |
| 629 initial_state.nodes[0].state = 0; |
| 630 initial_state.nodes[0].AddStateFlag(AX_STATE_CHECKED); |
| 631 initial_state.nodes[0].AddStateFlag(AX_STATE_FOCUSABLE); |
| 632 AXTree tree(initial_state); |
| 633 |
| 634 FakeAXTreeDelegate fake_delegate; |
| 635 tree.SetDelegate(&fake_delegate); |
| 636 |
| 637 // Change the role and state. |
| 638 AXTreeUpdate update; |
| 639 update.root_id = 1; |
| 640 update.nodes.resize(1); |
| 641 update.nodes[0].id = 1; |
| 642 update.nodes[0].role = AX_ROLE_CHECK_BOX; |
| 643 update.nodes[0].state = 0; |
| 644 update.nodes[0].AddStateFlag(AX_STATE_FOCUSABLE); |
| 645 update.nodes[0].AddStateFlag(AX_STATE_VISITED); |
| 646 EXPECT_TRUE(tree.Unserialize(update)); |
| 647 |
| 648 const std::vector<std::string>& change_log = |
| 649 fake_delegate.attribute_change_log(); |
| 650 ASSERT_EQ(3U, change_log.size()); |
| 651 EXPECT_EQ("Role changed from button to checkBox", change_log[0]); |
| 652 EXPECT_EQ("checked changed to false", change_log[1]); |
| 653 EXPECT_EQ("visited changed to true", change_log[2]); |
| 654 |
| 655 tree.SetDelegate(NULL); |
| 656 } |
| 657 |
| 658 TEST(AXTreeTest, AttributeChangeCallbacks) { |
| 659 AXTreeUpdate initial_state; |
| 660 initial_state.root_id = 1; |
| 661 initial_state.nodes.resize(1); |
| 662 initial_state.nodes[0].id = 1; |
| 663 initial_state.nodes[0].AddStringAttribute(AX_ATTR_NAME, "N1"); |
| 664 initial_state.nodes[0].AddStringAttribute(AX_ATTR_DESCRIPTION, "D1"); |
| 665 initial_state.nodes[0].AddBoolAttribute(AX_ATTR_LIVE_ATOMIC, true); |
| 666 initial_state.nodes[0].AddBoolAttribute(AX_ATTR_LIVE_BUSY, false); |
| 667 initial_state.nodes[0].AddFloatAttribute(AX_ATTR_MIN_VALUE_FOR_RANGE, 1.0); |
| 668 initial_state.nodes[0].AddFloatAttribute(AX_ATTR_MAX_VALUE_FOR_RANGE, 10.0); |
| 669 initial_state.nodes[0].AddIntAttribute(AX_ATTR_SCROLL_X, 5); |
| 670 initial_state.nodes[0].AddIntAttribute(AX_ATTR_SCROLL_X_MIN, 1); |
| 671 AXTree tree(initial_state); |
| 672 |
| 673 FakeAXTreeDelegate fake_delegate; |
| 674 tree.SetDelegate(&fake_delegate); |
| 675 |
| 676 // Change existing attributes. |
| 677 AXTreeUpdate update0; |
| 678 update0.root_id = 1; |
| 679 update0.nodes.resize(1); |
| 680 update0.nodes[0].id = 1; |
| 681 update0.nodes[0].AddStringAttribute(AX_ATTR_NAME, "N2"); |
| 682 update0.nodes[0].AddStringAttribute(AX_ATTR_DESCRIPTION, "D2"); |
| 683 update0.nodes[0].AddBoolAttribute(AX_ATTR_LIVE_ATOMIC, false); |
| 684 update0.nodes[0].AddBoolAttribute(AX_ATTR_LIVE_BUSY, true); |
| 685 update0.nodes[0].AddFloatAttribute(AX_ATTR_MIN_VALUE_FOR_RANGE, 2.0); |
| 686 update0.nodes[0].AddFloatAttribute(AX_ATTR_MAX_VALUE_FOR_RANGE, 9.0); |
| 687 update0.nodes[0].AddIntAttribute(AX_ATTR_SCROLL_X, 6); |
| 688 update0.nodes[0].AddIntAttribute(AX_ATTR_SCROLL_X_MIN, 2); |
| 689 EXPECT_TRUE(tree.Unserialize(update0)); |
| 690 |
| 691 const std::vector<std::string>& change_log = |
| 692 fake_delegate.attribute_change_log(); |
| 693 ASSERT_EQ(8U, change_log.size()); |
| 694 EXPECT_EQ("name changed from N1 to N2", change_log[0]); |
| 695 EXPECT_EQ("description changed from D1 to D2", change_log[1]); |
| 696 EXPECT_EQ("liveAtomic changed to false", change_log[2]); |
| 697 EXPECT_EQ("liveBusy changed to true", change_log[3]); |
| 698 EXPECT_EQ("minValueForRange changed from 1 to 2", change_log[4]); |
| 699 EXPECT_EQ("maxValueForRange changed from 10 to 9", change_log[5]); |
| 700 EXPECT_EQ("scrollX changed from 5 to 6", change_log[6]); |
| 701 EXPECT_EQ("scrollXMin changed from 1 to 2", change_log[7]); |
| 702 |
| 703 FakeAXTreeDelegate fake_delegate2; |
| 704 tree.SetDelegate(&fake_delegate2); |
| 705 |
| 706 // Add and remove attributes. |
| 707 AXTreeUpdate update1; |
| 708 update1.root_id = 1; |
| 709 update1.nodes.resize(1); |
| 710 update1.nodes[0].id = 1; |
| 711 update1.nodes[0].AddStringAttribute(AX_ATTR_DESCRIPTION, "D3"); |
| 712 update1.nodes[0].AddStringAttribute(AX_ATTR_VALUE, "V3"); |
| 713 update1.nodes[0].AddBoolAttribute(AX_ATTR_MODAL, true); |
| 714 update1.nodes[0].AddFloatAttribute(AX_ATTR_VALUE_FOR_RANGE, 5.0); |
| 715 update1.nodes[0].AddFloatAttribute(AX_ATTR_MAX_VALUE_FOR_RANGE, 9.0); |
| 716 update1.nodes[0].AddIntAttribute(AX_ATTR_SCROLL_X, 7); |
| 717 update1.nodes[0].AddIntAttribute(AX_ATTR_SCROLL_X_MAX, 10); |
| 718 EXPECT_TRUE(tree.Unserialize(update1)); |
| 719 |
| 720 const std::vector<std::string>& change_log2 = |
| 721 fake_delegate2.attribute_change_log(); |
| 722 ASSERT_EQ(10U, change_log2.size()); |
| 723 EXPECT_EQ("name changed from N2 to ", change_log2[0]); |
| 724 EXPECT_EQ("description changed from D2 to D3", change_log2[1]); |
| 725 EXPECT_EQ("value changed from to V3", change_log2[2]); |
| 726 EXPECT_EQ("liveBusy changed to false", change_log2[3]); |
| 727 EXPECT_EQ("modal changed to true", change_log2[4]); |
| 728 EXPECT_EQ("minValueForRange changed from 2 to 0", change_log2[5]); |
| 729 EXPECT_EQ("valueForRange changed from 0 to 5", change_log2[6]); |
| 730 EXPECT_EQ("scrollXMin changed from 2 to 0", change_log2[7]); |
| 731 EXPECT_EQ("scrollX changed from 6 to 7", change_log2[8]); |
| 732 EXPECT_EQ("scrollXMax changed from 0 to 10", change_log2[9]); |
| 733 |
| 734 tree.SetDelegate(NULL); |
| 735 } |
| 736 |
| 737 TEST(AXTreeTest, IntListChangeCallbacks) { |
| 738 std::vector<int32_t> one; |
| 739 one.push_back(1); |
| 740 |
| 741 std::vector<int32_t> two; |
| 742 two.push_back(2); |
| 743 two.push_back(2); |
| 744 |
| 745 std::vector<int32_t> three; |
| 746 three.push_back(3); |
| 747 |
| 748 AXTreeUpdate initial_state; |
| 749 initial_state.root_id = 1; |
| 750 initial_state.nodes.resize(1); |
| 751 initial_state.nodes[0].id = 1; |
| 752 initial_state.nodes[0].AddIntListAttribute(AX_ATTR_CONTROLS_IDS, one); |
| 753 initial_state.nodes[0].AddIntListAttribute(AX_ATTR_DETAILS_IDS, two); |
| 754 AXTree tree(initial_state); |
| 755 |
| 756 FakeAXTreeDelegate fake_delegate; |
| 757 tree.SetDelegate(&fake_delegate); |
| 758 |
| 759 // Change existing attributes. |
| 760 AXTreeUpdate update0; |
| 761 update0.root_id = 1; |
| 762 update0.nodes.resize(1); |
| 763 update0.nodes[0].id = 1; |
| 764 update0.nodes[0].AddIntListAttribute(AX_ATTR_CONTROLS_IDS, two); |
| 765 update0.nodes[0].AddIntListAttribute(AX_ATTR_DETAILS_IDS, three); |
| 766 EXPECT_TRUE(tree.Unserialize(update0)); |
| 767 |
| 768 const std::vector<std::string>& change_log = |
| 769 fake_delegate.attribute_change_log(); |
| 770 ASSERT_EQ(2U, change_log.size()); |
| 771 EXPECT_EQ("controlsIds changed from 1 to 2,2", change_log[0]); |
| 772 EXPECT_EQ("detailsIds changed from 2,2 to 3", change_log[1]); |
| 773 |
| 774 FakeAXTreeDelegate fake_delegate2; |
| 775 tree.SetDelegate(&fake_delegate2); |
| 776 |
| 777 // Add and remove attributes. |
| 778 AXTreeUpdate update1; |
| 779 update1.root_id = 1; |
| 780 update1.nodes.resize(1); |
| 781 update1.nodes[0].id = 1; |
| 782 update1.nodes[0].AddIntListAttribute(AX_ATTR_DETAILS_IDS, two); |
| 783 update1.nodes[0].AddIntListAttribute(AX_ATTR_FLOWTO_IDS, three); |
| 784 EXPECT_TRUE(tree.Unserialize(update1)); |
| 785 |
| 786 const std::vector<std::string>& change_log2 = |
| 787 fake_delegate2.attribute_change_log(); |
| 788 ASSERT_EQ(3U, change_log2.size()); |
| 789 EXPECT_EQ("controlsIds changed from 2,2 to ", change_log2[0]); |
| 790 EXPECT_EQ("detailsIds changed from 3 to 2,2", change_log2[1]); |
| 791 EXPECT_EQ("flowtoIds changed from to 3", change_log2[2]); |
| 792 |
| 793 tree.SetDelegate(NULL); |
| 794 } |
| 795 |
| 535 } // namespace ui | 796 } // namespace ui |
| OLD | NEW |