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 // Helper to count the number of nodes in a tree.
30 size_t GetSize(AXAuraObjWrapper
* tree
) {
33 std::vector
<AXAuraObjWrapper
*> out_children
;
34 tree
->GetChildren(&out_children
);
36 for (size_t i
= 0; i
< out_children
.size(); ++i
)
37 count
+= GetSize(out_children
[i
]);
42 class AXTreeSourceAuraTest
: public ash::test::AshTestBase
{
44 AXTreeSourceAuraTest() {}
45 ~AXTreeSourceAuraTest() override
{}
47 void SetUp() override
{
50 widget_
= new Widget();
51 Widget::InitParams
init_params(Widget::InitParams::TYPE_POPUP
);
52 init_params
.parent
= CurrentContext();
53 widget_
->Init(init_params
);
55 content_
= new View();
56 widget_
->SetContentsView(content_
);
58 textfield_
= new Textfield();
59 textfield_
->SetText(base::ASCIIToUTF16("Value"));
60 content_
->AddChildView(textfield_
);
66 Textfield
* textfield_
;
69 DISALLOW_COPY_AND_ASSIGN(AXTreeSourceAuraTest
);
72 TEST_F(AXTreeSourceAuraTest
, Accessors
) {
73 AXTreeSourceAura ax_tree
;
74 ASSERT_TRUE(ax_tree
.GetRoot());
76 // ID's should start at 1 and there should be a root.
77 ASSERT_EQ(1, ax_tree
.GetRoot()->GetID());
79 // Grab the content view directly from cache to avoid walking down the tree.
80 AXAuraObjWrapper
* content
=
81 AXAuraObjCache::GetInstance()->GetOrCreate(content_
);
82 std::vector
<AXAuraObjWrapper
*> content_children
;
83 ax_tree
.GetChildren(content
, &content_children
);
84 ASSERT_EQ(1U, content_children
.size());
86 // Walk down to the text field and assert it is what we expect.
87 AXAuraObjWrapper
* textfield
= content_children
[0];
88 AXAuraObjWrapper
* cached_textfield
=
89 AXAuraObjCache::GetInstance()->GetOrCreate(textfield_
);
90 ASSERT_EQ(cached_textfield
, textfield
);
91 std::vector
<AXAuraObjWrapper
*> textfield_children
;
92 ax_tree
.GetChildren(textfield
, &textfield_children
);
93 ASSERT_EQ(0U, textfield_children
.size());
95 ASSERT_EQ(content
, textfield
->GetParent());
97 // Try walking up the tree to the root.
98 AXAuraObjWrapper
* test_root
= NULL
;
99 for (AXAuraObjWrapper
* root_finder
= ax_tree
.GetParent(content
); root_finder
;
100 root_finder
= ax_tree
.GetParent(root_finder
))
101 test_root
= root_finder
;
102 ASSERT_EQ(ax_tree
.GetRoot(), test_root
);
105 TEST_F(AXTreeSourceAuraTest
, DoDefault
) {
106 AXTreeSourceAura ax_tree
;
108 // Grab a wrapper to |DoDefault| (click).
109 AXAuraObjWrapper
* textfield_wrapper
=
110 AXAuraObjCache::GetInstance()->GetOrCreate(textfield_
);
112 // Click and verify focus.
113 ASSERT_FALSE(textfield_
->HasFocus());
114 textfield_wrapper
->DoDefault();
115 ASSERT_TRUE(textfield_
->HasFocus());
118 TEST_F(AXTreeSourceAuraTest
, Focus
) {
119 AXTreeSourceAura ax_tree
;
121 // Grab a wrapper to focus.
122 AXAuraObjWrapper
* textfield_wrapper
=
123 AXAuraObjCache::GetInstance()->GetOrCreate(textfield_
);
126 ASSERT_FALSE(textfield_
->HasFocus());
127 textfield_wrapper
->Focus();
128 ASSERT_TRUE(textfield_
->HasFocus());
131 TEST_F(AXTreeSourceAuraTest
, Serialize
) {
132 AXTreeSourceAura ax_tree
;
133 ui::AXTreeSerializer
<AXAuraObjWrapper
*> ax_serializer(&ax_tree
);
134 ui::AXTreeUpdate out_update
;
136 // This is the initial serialization.
137 ax_serializer
.SerializeChanges(ax_tree
.GetRoot(), &out_update
);
139 // The update should just be the desktop node since no events have been fired
140 // on any controls, so no windows have been cached.
141 ASSERT_EQ(1U, out_update
.nodes
.size());
143 // Try removing some child views and re-adding which should fire some events.
144 content_
->RemoveAllChildViews(false /* delete_children */);
145 content_
->AddChildView(textfield_
);
147 // Grab the textfield since serialization only walks up the tree (not down
149 AXAuraObjWrapper
* textfield_wrapper
=
150 AXAuraObjCache::GetInstance()->GetOrCreate(textfield_
);
152 // Now, re-serialize.
153 ui::AXTreeUpdate out_update2
;
154 ax_serializer
.SerializeChanges(textfield_wrapper
, &out_update2
);
156 size_t node_count
= out_update2
.nodes
.size();
158 // We should have far more updates this time around.
159 ASSERT_GE(node_count
, 10U);
161 ASSERT_EQ(textfield_wrapper
->GetID(), out_update2
.nodes
[node_count
- 1].id
);
162 ASSERT_EQ(ui::AX_ROLE_TEXT_FIELD
, out_update2
.nodes
[node_count
- 1].role
);