1 // Copyright 2014 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.
7 #include "ash/test/ash_test_base.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/ui/aura/accessibility/ax_tree_source_aura.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/accessibility/ax_enums.h"
12 #include "ui/accessibility/ax_node.h"
13 #include "ui/accessibility/ax_serializable_tree.h"
14 #include "ui/accessibility/ax_tree_serializer.h"
15 #include "ui/accessibility/ax_tree_update.h"
16 #include "ui/aura/window.h"
17 #include "ui/views/accessibility/ax_aura_obj_cache.h"
18 #include "ui/views/accessibility/ax_aura_obj_wrapper.h"
19 #include "ui/views/controls/textfield/textfield.h"
20 #include "ui/views/test/views_test_base.h"
21 #include "ui/views/widget/widget.h"
23 using views::AXAuraObjCache
;
24 using views::AXAuraObjWrapper
;
25 using views::Textfield
;
29 using AuraAXTreeSerializer
=
30 ui::AXTreeSerializer
<views::AXAuraObjWrapper
*, ui::AXNodeData
>;
32 // Helper to count the number of nodes in a tree.
33 size_t GetSize(AXAuraObjWrapper
* tree
) {
36 std::vector
<AXAuraObjWrapper
*> out_children
;
37 tree
->GetChildren(&out_children
);
39 for (size_t i
= 0; i
< out_children
.size(); ++i
)
40 count
+= GetSize(out_children
[i
]);
45 class AXTreeSourceAuraTest
: public ash::test::AshTestBase
{
47 AXTreeSourceAuraTest() {}
48 ~AXTreeSourceAuraTest() override
{}
50 void SetUp() override
{
53 widget_
= new Widget();
54 Widget::InitParams
init_params(Widget::InitParams::TYPE_POPUP
);
55 init_params
.parent
= CurrentContext();
56 widget_
->Init(init_params
);
58 content_
= new View();
59 widget_
->SetContentsView(content_
);
61 textfield_
= new Textfield();
62 textfield_
->SetText(base::ASCIIToUTF16("Value"));
63 content_
->AddChildView(textfield_
);
69 Textfield
* textfield_
;
72 DISALLOW_COPY_AND_ASSIGN(AXTreeSourceAuraTest
);
75 TEST_F(AXTreeSourceAuraTest
, Accessors
) {
76 AXTreeSourceAura ax_tree
;
77 ASSERT_TRUE(ax_tree
.GetRoot());
79 // ID's should start at 1 and there should be a root.
80 ASSERT_EQ(1, ax_tree
.GetRoot()->GetID());
82 // Grab the content view directly from cache to avoid walking down the tree.
83 AXAuraObjWrapper
* content
=
84 AXAuraObjCache::GetInstance()->GetOrCreate(content_
);
85 std::vector
<AXAuraObjWrapper
*> content_children
;
86 ax_tree
.GetChildren(content
, &content_children
);
87 ASSERT_EQ(1U, content_children
.size());
89 // Walk down to the text field and assert it is what we expect.
90 AXAuraObjWrapper
* textfield
= content_children
[0];
91 AXAuraObjWrapper
* cached_textfield
=
92 AXAuraObjCache::GetInstance()->GetOrCreate(textfield_
);
93 ASSERT_EQ(cached_textfield
, textfield
);
94 std::vector
<AXAuraObjWrapper
*> textfield_children
;
95 ax_tree
.GetChildren(textfield
, &textfield_children
);
96 ASSERT_EQ(0U, textfield_children
.size());
98 ASSERT_EQ(content
, textfield
->GetParent());
100 // Try walking up the tree to the root.
101 AXAuraObjWrapper
* test_root
= NULL
;
102 for (AXAuraObjWrapper
* root_finder
= ax_tree
.GetParent(content
); root_finder
;
103 root_finder
= ax_tree
.GetParent(root_finder
))
104 test_root
= root_finder
;
105 ASSERT_EQ(ax_tree
.GetRoot(), test_root
);
108 TEST_F(AXTreeSourceAuraTest
, DoDefault
) {
109 AXTreeSourceAura ax_tree
;
111 // Grab a wrapper to |DoDefault| (click).
112 AXAuraObjWrapper
* textfield_wrapper
=
113 AXAuraObjCache::GetInstance()->GetOrCreate(textfield_
);
115 // Click and verify focus.
116 ASSERT_FALSE(textfield_
->HasFocus());
117 textfield_wrapper
->DoDefault();
118 ASSERT_TRUE(textfield_
->HasFocus());
121 TEST_F(AXTreeSourceAuraTest
, Focus
) {
122 AXTreeSourceAura ax_tree
;
124 // Grab a wrapper to focus.
125 AXAuraObjWrapper
* textfield_wrapper
=
126 AXAuraObjCache::GetInstance()->GetOrCreate(textfield_
);
129 ASSERT_FALSE(textfield_
->HasFocus());
130 textfield_wrapper
->Focus();
131 ASSERT_TRUE(textfield_
->HasFocus());
134 TEST_F(AXTreeSourceAuraTest
, Serialize
) {
135 AXTreeSourceAura ax_tree
;
136 AuraAXTreeSerializer
ax_serializer(&ax_tree
);
137 ui::AXTreeUpdate
<ui::AXNodeData
> out_update
;
139 // This is the initial serialization.
140 ax_serializer
.SerializeChanges(ax_tree
.GetRoot(), &out_update
);
142 // The update should just be the desktop node since no events have been fired
143 // on any controls, so no windows have been cached.
144 ASSERT_EQ(1U, out_update
.nodes
.size());
146 // Try removing some child views and re-adding which should fire some events.
147 content_
->RemoveAllChildViews(false /* delete_children */);
148 content_
->AddChildView(textfield_
);
150 // Grab the textfield since serialization only walks up the tree (not down
152 AXAuraObjWrapper
* textfield_wrapper
=
153 AXAuraObjCache::GetInstance()->GetOrCreate(textfield_
);
155 // Now, re-serialize.
156 ui::AXTreeUpdate
<ui::AXNodeData
> out_update2
;
157 ax_serializer
.SerializeChanges(textfield_wrapper
, &out_update2
);
159 size_t node_count
= out_update2
.nodes
.size();
161 // We should have far more updates this time around.
162 ASSERT_GE(node_count
, 10U);
164 ASSERT_EQ(textfield_wrapper
->GetID(), out_update2
.nodes
[node_count
- 1].id
);
165 ASSERT_EQ(ui::AX_ROLE_TEXT_FIELD
, out_update2
.nodes
[node_count
- 1].role
);