Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / accessibility / platform / test_ax_node_wrapper.cc
blob77af2a2573f003d882a6301f5b6abead301b16a1
1 // Copyright 2015 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.
5 #include "base/containers/hash_tables.h"
6 #include "ui/accessibility/platform/test_ax_node_wrapper.h"
8 namespace ui {
10 namespace {
12 // A global map from AXNodes to TestAXNodeWrappers.
13 base::hash_map<AXNode*, TestAXNodeWrapper*> g_node_to_wrapper_map;
15 // A global coordinate offset.
16 gfx::Vector2d g_offset;
18 // A simple implementation of AXTreeDelegate to catch when AXNodes are
19 // deleted so we can delete their wrappers.
20 class TestAXTreeDelegate : public AXTreeDelegate {
21 void OnNodeWillBeDeleted(AXTree* tree, AXNode* node) override {
22 auto iter = g_node_to_wrapper_map.find(node);
23 if (iter != g_node_to_wrapper_map.end()) {
24 TestAXNodeWrapper* wrapper = iter->second;
25 delete wrapper;
26 g_node_to_wrapper_map.erase(iter->first);
29 void OnSubtreeWillBeDeleted(AXTree* tree, AXNode* node) override {}
30 void OnNodeCreated(AXTree* tree, AXNode* node) override {}
31 void OnNodeChanged(AXTree* tree, AXNode* node) override {}
32 void OnAtomicUpdateFinished(AXTree* tree,
33 bool root_changed,
34 const std::vector<Change>& changes) override {}
37 TestAXTreeDelegate g_ax_tree_delegate;
39 } // namespace
41 // static
42 TestAXNodeWrapper* TestAXNodeWrapper::GetOrCreate(AXTree* tree, AXNode* node) {
43 // Just return NULL if |node| is NULL; this makes test code simpler because
44 // now we don't have to null-check AXNode* every time we call GetOrCreate.
45 if (!node)
46 return nullptr;
48 tree->SetDelegate(&g_ax_tree_delegate);
49 auto iter = g_node_to_wrapper_map.find(node);
50 if (iter != g_node_to_wrapper_map.end())
51 return iter->second;
52 TestAXNodeWrapper* wrapper = new TestAXNodeWrapper(tree, node);
53 g_node_to_wrapper_map[node] = wrapper;
54 return wrapper;
57 // static
58 void TestAXNodeWrapper::SetGlobalCoordinateOffset(const gfx::Vector2d& offset) {
59 g_offset = offset;
62 TestAXNodeWrapper::~TestAXNodeWrapper() {
63 platform_node_->Destroy();
66 const AXNodeData& TestAXNodeWrapper::GetData() {
67 return node_->data();
70 gfx::NativeViewAccessible TestAXNodeWrapper::GetParent() {
71 TestAXNodeWrapper* parent_wrapper = GetOrCreate(tree_, node_->parent());
72 return parent_wrapper ?
73 parent_wrapper->ax_platform_node()->GetNativeViewAccessible() :
74 nullptr;
77 int TestAXNodeWrapper::GetChildCount() {
78 return node_->child_count();
81 gfx::NativeViewAccessible TestAXNodeWrapper::ChildAtIndex(int index) {
82 CHECK_GE(index, 0);
83 CHECK_LT(index, GetChildCount());
84 TestAXNodeWrapper* child_wrapper =
85 GetOrCreate(tree_, node_->children()[index]);
86 return child_wrapper ?
87 child_wrapper->ax_platform_node()->GetNativeViewAccessible() :
88 nullptr;
91 gfx::Vector2d TestAXNodeWrapper::GetGlobalCoordinateOffset() {
92 return g_offset;
95 gfx::NativeViewAccessible TestAXNodeWrapper::HitTestSync(int x, int y) {
96 return nullptr;
99 gfx::NativeViewAccessible TestAXNodeWrapper::GetFocus() {
100 return nullptr;
103 gfx::AcceleratedWidget
104 TestAXNodeWrapper::GetTargetForNativeAccessibilityEvent() {
105 return gfx::kNullAcceleratedWidget;
108 void TestAXNodeWrapper::DoDefaultAction() {
111 bool TestAXNodeWrapper::SetStringValue(const base::string16& new_value) {
112 return false;
115 TestAXNodeWrapper::TestAXNodeWrapper(AXTree* tree, AXNode* node)
116 : tree_(tree),
117 node_(node),
118 platform_node_(AXPlatformNode::Create(this)) {
121 } // namespace ui