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 // These tests prevent other tests from being run. crbug.com/514632
26 #if defined(ANDROID) && defined(ADDRESS_SANITIZER)
27 #define MAYBE_OneShotAccessibilityTreeSearchTest DISABLED_OneShotAccessibilityTreeSearchTets
29 #define MAYBE_OneShotAccessibilityTreeSearchTest OneShotAccessibilityTreeSearchTest
31 class MAYBE_OneShotAccessibilityTreeSearchTest
: public testing::Test
{
33 MAYBE_OneShotAccessibilityTreeSearchTest() {}
34 ~MAYBE_OneShotAccessibilityTreeSearchTest() override
{}
37 void SetUp() override
;
39 scoped_ptr
<BrowserAccessibilityManager
> tree_
;
42 DISALLOW_COPY_AND_ASSIGN(MAYBE_OneShotAccessibilityTreeSearchTest
);
45 void MAYBE_OneShotAccessibilityTreeSearchTest::SetUp() {
48 root
.SetName("Document");
49 root
.role
= ui::AX_ROLE_ROOT_WEB_AREA
;
51 root
.child_ids
.push_back(2);
52 root
.child_ids
.push_back(3);
53 root
.child_ids
.push_back(6);
55 ui::AXNodeData heading
;
57 heading
.SetName("Heading");
58 heading
.role
= ui::AX_ROLE_HEADING
;
63 list
.role
= ui::AX_ROLE_LIST
;
65 list
.child_ids
.push_back(4);
66 list
.child_ids
.push_back(5);
68 ui::AXNodeData list_item_1
;
70 list_item_1
.SetName("Autobots");
71 list_item_1
.role
= ui::AX_ROLE_LIST_ITEM
;
72 list_item_1
.state
= 0;
74 ui::AXNodeData list_item_2
;
76 list_item_2
.SetName("Decepticons");
77 list_item_2
.role
= ui::AX_ROLE_LIST_ITEM
;
78 list_item_2
.state
= 0;
80 ui::AXNodeData footer
;
82 footer
.SetName("Footer");
83 footer
.role
= ui::AX_ROLE_FOOTER
;
84 footer
.state
= 1 << ui::AX_STATE_OFFSCREEN
;
86 tree_
.reset(new TestBrowserAccessibilityManager(
87 MakeAXTreeUpdate(root
, heading
, list
, list_item_1
, list_item_2
, footer
)));
90 TEST_F(MAYBE_OneShotAccessibilityTreeSearchTest
, GetAll
) {
91 OneShotAccessibilityTreeSearch
search(tree_
.get());
92 ASSERT_EQ(6U, search
.CountMatches());
95 TEST_F(MAYBE_OneShotAccessibilityTreeSearchTest
, ForwardsWithStartNode
) {
96 OneShotAccessibilityTreeSearch
search(tree_
.get());
97 search
.SetStartNode(tree_
->GetFromID(4));
98 ASSERT_EQ(2U, search
.CountMatches());
99 EXPECT_EQ(5, search
.GetMatchAtIndex(0)->GetId());
100 EXPECT_EQ(6, search
.GetMatchAtIndex(1)->GetId());
103 TEST_F(MAYBE_OneShotAccessibilityTreeSearchTest
, BackwardsWithStartNode
) {
104 OneShotAccessibilityTreeSearch
search(tree_
.get());
105 search
.SetStartNode(tree_
->GetFromID(4));
106 search
.SetDirection(OneShotAccessibilityTreeSearch::BACKWARDS
);
107 ASSERT_EQ(3U, search
.CountMatches());
108 EXPECT_EQ(3, search
.GetMatchAtIndex(0)->GetId());
109 EXPECT_EQ(2, search
.GetMatchAtIndex(1)->GetId());
110 EXPECT_EQ(1, search
.GetMatchAtIndex(2)->GetId());
113 TEST_F(MAYBE_OneShotAccessibilityTreeSearchTest
, ResultLimitZero
) {
114 OneShotAccessibilityTreeSearch
search(tree_
.get());
115 search
.SetResultLimit(0);
116 ASSERT_EQ(0U, search
.CountMatches());
119 TEST_F(MAYBE_OneShotAccessibilityTreeSearchTest
, ResultLimitFive
) {
120 OneShotAccessibilityTreeSearch
search(tree_
.get());
121 search
.SetResultLimit(5);
122 ASSERT_EQ(5U, search
.CountMatches());
125 TEST_F(MAYBE_OneShotAccessibilityTreeSearchTest
, DescendantsOnly
) {
126 OneShotAccessibilityTreeSearch
search(tree_
.get());
127 search
.SetStartNode(tree_
->GetFromID(1));
128 search
.SetImmediateDescendantsOnly(true);
129 ASSERT_EQ(3U, search
.CountMatches());
130 EXPECT_EQ(2, search
.GetMatchAtIndex(0)->GetId());
131 EXPECT_EQ(3, search
.GetMatchAtIndex(1)->GetId());
132 EXPECT_EQ(6, search
.GetMatchAtIndex(2)->GetId());
135 TEST_F(MAYBE_OneShotAccessibilityTreeSearchTest
, VisibleOnly
) {
136 OneShotAccessibilityTreeSearch
search(tree_
.get());
137 search
.SetVisibleOnly(true);
138 ASSERT_EQ(5U, search
.CountMatches());
139 EXPECT_EQ(1, search
.GetMatchAtIndex(0)->GetId());
140 EXPECT_EQ(2, search
.GetMatchAtIndex(1)->GetId());
141 EXPECT_EQ(3, search
.GetMatchAtIndex(2)->GetId());
142 EXPECT_EQ(4, search
.GetMatchAtIndex(3)->GetId());
143 EXPECT_EQ(5, search
.GetMatchAtIndex(4)->GetId());
146 TEST_F(MAYBE_OneShotAccessibilityTreeSearchTest
, CaseInsensitiveStringMatch
) {
147 OneShotAccessibilityTreeSearch
search(tree_
.get());
148 search
.SetSearchText("eCEptiCOn");
149 ASSERT_EQ(1U, search
.CountMatches());
150 EXPECT_EQ(5, search
.GetMatchAtIndex(0)->GetId());
153 TEST_F(MAYBE_OneShotAccessibilityTreeSearchTest
, OnePredicate
) {
154 OneShotAccessibilityTreeSearch
search(tree_
.get());
155 search
.AddPredicate([](BrowserAccessibility
* start
,
156 BrowserAccessibility
* current
) {
157 return current
->GetRole() == ui::AX_ROLE_LIST_ITEM
;
159 ASSERT_EQ(2U, search
.CountMatches());
160 EXPECT_EQ(4, search
.GetMatchAtIndex(0)->GetId());
161 EXPECT_EQ(5, search
.GetMatchAtIndex(1)->GetId());
164 TEST_F(MAYBE_OneShotAccessibilityTreeSearchTest
, TwoPredicates
) {
165 OneShotAccessibilityTreeSearch
search(tree_
.get());
166 search
.AddPredicate([](BrowserAccessibility
* start
,
167 BrowserAccessibility
* current
) {
168 return (current
->GetRole() == ui::AX_ROLE_LIST
||
169 current
->GetRole() == ui::AX_ROLE_LIST_ITEM
);
171 search
.AddPredicate([](BrowserAccessibility
* start
,
172 BrowserAccessibility
* current
) {
173 return (current
->GetId() % 2 == 1);
175 ASSERT_EQ(2U, search
.CountMatches());
176 EXPECT_EQ(3, search
.GetMatchAtIndex(0)->GetId());
177 EXPECT_EQ(5, search
.GetMatchAtIndex(1)->GetId());
180 } // namespace content