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/memory/scoped_ptr.h"
6 #include "content/browser/accessibility/browser_accessibility.h"
7 #include "content/browser/accessibility/browser_accessibility_manager.h"
8 #include "content/browser/accessibility/one_shot_accessibility_tree_search.h"
9 #include "testing/gtest/include/gtest/gtest.h"
15 class TestBrowserAccessibilityManager
: public BrowserAccessibilityManager
{
17 TestBrowserAccessibilityManager(const ui::AXTreeUpdate
& initial_tree
)
18 : BrowserAccessibilityManager(initial_tree
,
20 new BrowserAccessibilityFactory()) {}
25 class OneShotAccessibilityTreeSearchTest
: public testing::Test
{
27 OneShotAccessibilityTreeSearchTest() {}
28 ~OneShotAccessibilityTreeSearchTest() override
{}
31 void SetUp() override
;
33 scoped_ptr
<BrowserAccessibilityManager
> tree_
;
36 DISALLOW_COPY_AND_ASSIGN(OneShotAccessibilityTreeSearchTest
);
39 void OneShotAccessibilityTreeSearchTest::SetUp() {
42 root
.SetName("Document");
43 root
.role
= ui::AX_ROLE_ROOT_WEB_AREA
;
45 root
.child_ids
.push_back(2);
46 root
.child_ids
.push_back(3);
47 root
.child_ids
.push_back(6);
49 ui::AXNodeData heading
;
51 heading
.SetName("Heading");
52 heading
.role
= ui::AX_ROLE_HEADING
;
57 list
.role
= ui::AX_ROLE_LIST
;
59 list
.child_ids
.push_back(4);
60 list
.child_ids
.push_back(5);
62 ui::AXNodeData list_item_1
;
64 list_item_1
.SetName("Autobots");
65 list_item_1
.role
= ui::AX_ROLE_LIST_ITEM
;
66 list_item_1
.state
= 0;
68 ui::AXNodeData list_item_2
;
70 list_item_2
.SetName("Decepticons");
71 list_item_2
.role
= ui::AX_ROLE_LIST_ITEM
;
72 list_item_2
.state
= 0;
74 ui::AXNodeData footer
;
76 footer
.SetName("Footer");
77 footer
.role
= ui::AX_ROLE_FOOTER
;
78 footer
.state
= 1 << ui::AX_STATE_OFFSCREEN
;
80 tree_
.reset(new TestBrowserAccessibilityManager(
81 MakeAXTreeUpdate(root
, heading
, list
, list_item_1
, list_item_2
, footer
)));
84 TEST_F(OneShotAccessibilityTreeSearchTest
, GetAll
) {
85 OneShotAccessibilityTreeSearch
search(tree_
.get());
86 ASSERT_EQ(6U, search
.CountMatches());
89 TEST_F(OneShotAccessibilityTreeSearchTest
, ForwardsWithStartNode
) {
90 OneShotAccessibilityTreeSearch
search(tree_
.get());
91 search
.SetStartNode(tree_
->GetFromID(4));
92 ASSERT_EQ(2U, search
.CountMatches());
93 EXPECT_EQ(5, search
.GetMatchAtIndex(0)->GetId());
94 EXPECT_EQ(6, search
.GetMatchAtIndex(1)->GetId());
97 TEST_F(OneShotAccessibilityTreeSearchTest
, BackwardsWithStartNode
) {
98 OneShotAccessibilityTreeSearch
search(tree_
.get());
99 search
.SetStartNode(tree_
->GetFromID(4));
100 search
.SetDirection(OneShotAccessibilityTreeSearch::BACKWARDS
);
101 ASSERT_EQ(3U, search
.CountMatches());
102 EXPECT_EQ(3, search
.GetMatchAtIndex(0)->GetId());
103 EXPECT_EQ(2, search
.GetMatchAtIndex(1)->GetId());
104 EXPECT_EQ(1, search
.GetMatchAtIndex(2)->GetId());
107 TEST_F(OneShotAccessibilityTreeSearchTest
, ResultLimitZero
) {
108 OneShotAccessibilityTreeSearch
search(tree_
.get());
109 search
.SetResultLimit(0);
110 ASSERT_EQ(0U, search
.CountMatches());
113 TEST_F(OneShotAccessibilityTreeSearchTest
, ResultLimitFive
) {
114 OneShotAccessibilityTreeSearch
search(tree_
.get());
115 search
.SetResultLimit(5);
116 ASSERT_EQ(5U, search
.CountMatches());
119 TEST_F(OneShotAccessibilityTreeSearchTest
, DescendantsOnly
) {
120 OneShotAccessibilityTreeSearch
search(tree_
.get());
121 search
.SetStartNode(tree_
->GetFromID(1));
122 search
.SetImmediateDescendantsOnly(true);
123 ASSERT_EQ(3U, search
.CountMatches());
124 EXPECT_EQ(2, search
.GetMatchAtIndex(0)->GetId());
125 EXPECT_EQ(3, search
.GetMatchAtIndex(1)->GetId());
126 EXPECT_EQ(6, search
.GetMatchAtIndex(2)->GetId());
129 TEST_F(OneShotAccessibilityTreeSearchTest
, VisibleOnly
) {
130 OneShotAccessibilityTreeSearch
search(tree_
.get());
131 search
.SetVisibleOnly(true);
132 ASSERT_EQ(5U, search
.CountMatches());
133 EXPECT_EQ(1, search
.GetMatchAtIndex(0)->GetId());
134 EXPECT_EQ(2, search
.GetMatchAtIndex(1)->GetId());
135 EXPECT_EQ(3, search
.GetMatchAtIndex(2)->GetId());
136 EXPECT_EQ(4, search
.GetMatchAtIndex(3)->GetId());
137 EXPECT_EQ(5, search
.GetMatchAtIndex(4)->GetId());
140 TEST_F(OneShotAccessibilityTreeSearchTest
, CaseInsensitiveStringMatch
) {
141 OneShotAccessibilityTreeSearch
search(tree_
.get());
142 search
.SetSearchText("eCEptiCOn");
143 ASSERT_EQ(1U, search
.CountMatches());
144 EXPECT_EQ(5, search
.GetMatchAtIndex(0)->GetId());
147 TEST_F(OneShotAccessibilityTreeSearchTest
, OnePredicate
) {
148 OneShotAccessibilityTreeSearch
search(tree_
.get());
149 search
.AddPredicate([](BrowserAccessibility
* start
,
150 BrowserAccessibility
* current
) {
151 return current
->GetRole() == ui::AX_ROLE_LIST_ITEM
;
153 ASSERT_EQ(2U, search
.CountMatches());
154 EXPECT_EQ(4, search
.GetMatchAtIndex(0)->GetId());
155 EXPECT_EQ(5, search
.GetMatchAtIndex(1)->GetId());
158 TEST_F(OneShotAccessibilityTreeSearchTest
, TwoPredicates
) {
159 OneShotAccessibilityTreeSearch
search(tree_
.get());
160 search
.AddPredicate([](BrowserAccessibility
* start
,
161 BrowserAccessibility
* current
) {
162 return (current
->GetRole() == ui::AX_ROLE_LIST
||
163 current
->GetRole() == ui::AX_ROLE_LIST_ITEM
);
165 search
.AddPredicate([](BrowserAccessibility
* start
,
166 BrowserAccessibility
* current
) {
167 return (current
->GetId() % 2 == 1);
169 ASSERT_EQ(2U, search
.CountMatches());
170 EXPECT_EQ(3, search
.GetMatchAtIndex(0)->GetId());
171 EXPECT_EQ(5, search
.GetMatchAtIndex(1)->GetId());
174 } // namespace content