1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/accessibility/ax_node.h"
#include "ui/accessibility/ax_serializable_tree.h"
#include "ui/accessibility/ax_tree.h"
#include "ui/accessibility/ax_tree_serializer.h"
namespace ui {
namespace {
class FakeAXTreeDelegate : public AXTreeDelegate {
public:
void OnNodeWillBeDeleted(AXNode* node) override {
deleted_ids_.push_back(node->id());
}
void OnNodeCreated(AXNode* node) override {
created_ids_.push_back(node->id());
}
void OnNodeChanged(AXNode* node) override {
changed_ids_.push_back(node->id());
}
void OnNodeCreationFinished(AXNode* node) override {
creation_finished_ids_.push_back(node->id());
}
void OnNodeChangeFinished(AXNode* node) override {
change_finished_ids_.push_back(node->id());
}
void OnRootChanged(AXNode* new_root) override {
new_root_ids_.push_back(new_root->id());
}
const std::vector<int32>& deleted_ids() { return deleted_ids_; }
const std::vector<int32>& created_ids() { return created_ids_; }
const std::vector<int32>& creation_finished_ids() {
return creation_finished_ids_;
}
const std::vector<int32>& new_root_ids() { return new_root_ids_; }
private:
std::vector<int32> deleted_ids_;
std::vector<int32> created_ids_;
std::vector<int32> creation_finished_ids_;
std::vector<int32> changed_ids_;
std::vector<int32> change_finished_ids_;
std::vector<int32> new_root_ids_;
};
} // namespace
TEST(AXTreeTest, SerializeSimpleAXTree) {
AXNodeData root;
root.id = 1;
root.role = AX_ROLE_ROOT_WEB_AREA;
root.state = (1 << AX_STATE_FOCUSABLE) | (1 << AX_STATE_FOCUSED);
root.location = gfx::Rect(0, 0, 800, 600);
root.child_ids.push_back(2);
root.child_ids.push_back(3);
AXNodeData button;
button.id = 2;
button.role = AX_ROLE_BUTTON;
button.state = 0;
button.location = gfx::Rect(20, 20, 200, 30);
AXNodeData checkbox;
checkbox.id = 3;
checkbox.role = AX_ROLE_CHECK_BOX;
checkbox.state = 0;
checkbox.location = gfx::Rect(20, 50, 200, 30);
AXTreeUpdate initial_state;
initial_state.nodes.push_back(root);
initial_state.nodes.push_back(button);
initial_state.nodes.push_back(checkbox);
AXSerializableTree src_tree(initial_state);
scoped_ptr<AXTreeSource<const AXNode*> > tree_source(
src_tree.CreateTreeSource());
AXTreeSerializer<const AXNode*> serializer(tree_source.get());
AXTreeUpdate update;
serializer.SerializeChanges(src_tree.GetRoot(), &update);
AXTree dst_tree;
ASSERT_TRUE(dst_tree.Unserialize(update));
const AXNode* root_node = dst_tree.GetRoot();
ASSERT_TRUE(root_node != NULL);
EXPECT_EQ(root.id, root_node->id());
EXPECT_EQ(root.role, root_node->data().role);
ASSERT_EQ(2, root_node->child_count());
const AXNode* button_node = root_node->ChildAtIndex(0);
EXPECT_EQ(button.id, button_node->id());
EXPECT_EQ(button.role, button_node->data().role);
const AXNode* checkbox_node = root_node->ChildAtIndex(1);
EXPECT_EQ(checkbox.id, checkbox_node->id());
EXPECT_EQ(checkbox.role, checkbox_node->data().role);
EXPECT_EQ(
"id=1 rootWebArea FOCUSABLE FOCUSED (0, 0)-(800, 600) child_ids=2,3\n"
" id=2 button (20, 20)-(200, 30)\n"
" id=3 checkBox (20, 50)-(200, 30)\n",
dst_tree.ToString());
}
TEST(AXTreeTest, SerializeAXTreeUpdate) {
AXNodeData list;
list.id = 3;
list.role = AX_ROLE_LIST;
list.state = 0;
list.child_ids.push_back(4);
list.child_ids.push_back(5);
list.child_ids.push_back(6);
AXNodeData list_item_2;
list_item_2.id = 5;
list_item_2.role = AX_ROLE_LIST_ITEM;
list_item_2.state = 0;
AXNodeData list_item_3;
list_item_3.id = 6;
list_item_3.role = AX_ROLE_LIST_ITEM;
list_item_3.state = 0;
AXNodeData button;
button.id = 7;
button.role = AX_ROLE_BUTTON;
button.state = 0;
AXTreeUpdate update;
update.nodes.push_back(list);
update.nodes.push_back(list_item_2);
update.nodes.push_back(list_item_3);
update.nodes.push_back(button);
EXPECT_EQ(
"id=3 list (0, 0)-(0, 0) child_ids=4,5,6\n"
" id=5 listItem (0, 0)-(0, 0)\n"
" id=6 listItem (0, 0)-(0, 0)\n"
"id=7 button (0, 0)-(0, 0)\n",
update.ToString());
}
TEST(AXTreeTest, DeleteUnknownSubtreeFails) {
AXNodeData root;
root.id = 1;
root.role = AX_ROLE_ROOT_WEB_AREA;
AXTreeUpdate initial_state;
initial_state.nodes.push_back(root);
AXTree tree(initial_state);
// This should fail because we're asking it to delete
// a subtree rooted at id=2, which doesn't exist.
AXTreeUpdate update;
update.node_id_to_clear = 2;
update.nodes.resize(1);
update.nodes[0].id = 1;
update.nodes[0].id = AX_ROLE_ROOT_WEB_AREA;
EXPECT_FALSE(tree.Unserialize(update));
ASSERT_EQ("Bad node_id_to_clear: 2", tree.error());
}
TEST(AXTreeTest, LeaveOrphanedDeletedSubtreeFails) {
AXTreeUpdate initial_state;
initial_state.nodes.resize(3);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
initial_state.nodes[0].child_ids.push_back(2);
initial_state.nodes[0].child_ids.push_back(3);
initial_state.nodes[1].id = 2;
initial_state.nodes[2].id = 3;
AXTree tree(initial_state);
// This should fail because we delete a subtree rooted at id=2
// but never update it.
AXTreeUpdate update;
update.node_id_to_clear = 2;
update.nodes.resize(1);
update.nodes[0].id = 3;
EXPECT_FALSE(tree.Unserialize(update));
ASSERT_EQ("Nodes left pending by the update: 2", tree.error());
}
TEST(AXTreeTest, LeaveOrphanedNewChildFails) {
AXTreeUpdate initial_state;
initial_state.nodes.resize(1);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
AXTree tree(initial_state);
// This should fail because we add a new child to the root node
// but never update it.
AXTreeUpdate update;
update.nodes.resize(1);
update.nodes[0].id = 1;
update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
update.nodes[0].child_ids.push_back(2);
EXPECT_FALSE(tree.Unserialize(update));
ASSERT_EQ("Nodes left pending by the update: 2", tree.error());
}
TEST(AXTreeTest, DuplicateChildIdFails) {
AXTreeUpdate initial_state;
initial_state.nodes.resize(1);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
AXTree tree(initial_state);
// This should fail because a child id appears twice.
AXTreeUpdate update;
update.nodes.resize(2);
update.nodes[0].id = 1;
update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
update.nodes[0].child_ids.push_back(2);
update.nodes[0].child_ids.push_back(2);
update.nodes[1].id = 2;
EXPECT_FALSE(tree.Unserialize(update));
ASSERT_EQ("Node 1 has duplicate child id 2", tree.error());
}
TEST(AXTreeTest, InvalidReparentingFails) {
AXTreeUpdate initial_state;
initial_state.nodes.resize(3);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
initial_state.nodes[0].child_ids.push_back(2);
initial_state.nodes[1].id = 2;
initial_state.nodes[1].child_ids.push_back(3);
initial_state.nodes[2].id = 3;
AXTree tree(initial_state);
// This should fail because node 3 is reparented from node 2 to node 1
// without deleting node 1's subtree first.
AXTreeUpdate update;
update.nodes.resize(3);
update.nodes[0].id = 1;
update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
update.nodes[0].child_ids.push_back(3);
update.nodes[0].child_ids.push_back(2);
update.nodes[1].id = 2;
update.nodes[2].id = 3;
EXPECT_FALSE(tree.Unserialize(update));
ASSERT_EQ("Node 3 reparented from 2 to 1", tree.error());
}
TEST(AXTreeTest, TreeDelegateIsCalled) {
AXTreeUpdate initial_state;
initial_state.nodes.resize(1);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
AXTree tree(initial_state);
AXTreeUpdate update;
update.node_id_to_clear = 1;
update.nodes.resize(2);
update.nodes[0].id = 2;
update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA;
update.nodes[0].child_ids.push_back(3);
update.nodes[1].id = 3;
FakeAXTreeDelegate fake_delegate;
tree.SetDelegate(&fake_delegate);
EXPECT_TRUE(tree.Unserialize(update));
ASSERT_EQ(1U, fake_delegate.deleted_ids().size());
EXPECT_EQ(1, fake_delegate.deleted_ids()[0]);
ASSERT_EQ(2U, fake_delegate.created_ids().size());
EXPECT_EQ(2, fake_delegate.created_ids()[0]);
EXPECT_EQ(3, fake_delegate.created_ids()[1]);
ASSERT_EQ(2U, fake_delegate.creation_finished_ids().size());
EXPECT_EQ(2, fake_delegate.creation_finished_ids()[0]);
EXPECT_EQ(3, fake_delegate.creation_finished_ids()[1]);
ASSERT_EQ(1U, fake_delegate.new_root_ids().size());
EXPECT_EQ(2, fake_delegate.new_root_ids()[0]);
tree.SetDelegate(NULL);
}
} // namespace ui
|