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(
18 const SimpleAXTreeUpdate
& initial_tree
)
19 : BrowserAccessibilityManager(initial_tree
,
21 new BrowserAccessibilityFactory()) {}
26 // These tests prevent other tests from being run. crbug.com/514632
27 #if defined(ANDROID) && defined(ADDRESS_SANITIZER)
28 #define MAYBE_OneShotAccessibilityTreeSearchTest DISABLED_OneShotAccessibilityTreeSearchTets
30 #define MAYBE_OneShotAccessibilityTreeSearchTest OneShotAccessibilityTreeSearchTest
32 class MAYBE_OneShotAccessibilityTreeSearchTest
: public testing::Test
{
34 MAYBE_OneShotAccessibilityTreeSearchTest() {}
35 ~MAYBE_OneShotAccessibilityTreeSearchTest() override
{}
38 void SetUp() override
;
40 scoped_ptr
<BrowserAccessibilityManager
> tree_
;
43 DISALLOW_COPY_AND_ASSIGN(MAYBE_OneShotAccessibilityTreeSearchTest
);
46 void MAYBE_OneShotAccessibilityTreeSearchTest::SetUp() {
49 root
.SetName("Document");
50 root
.role
= ui::AX_ROLE_ROOT_WEB_AREA
;
52 root
.child_ids
.push_back(2);
53 root
.child_ids
.push_back(3);
54 root
.child_ids
.push_back(6);
56 ui::AXNodeData heading
;
58 heading
.SetName("Heading");
59 heading
.role
= ui::AX_ROLE_HEADING
;
64 list
.role
= ui::AX_ROLE_LIST
;
66 list
.child_ids
.push_back(4);
67 list
.child_ids
.push_back(5);
69 ui::AXNodeData list_item_1
;
71 list_item_1
.SetName("Autobots");
72 list_item_1
.role
= ui::AX_ROLE_LIST_ITEM
;
73 list_item_1
.state
= 0;
75 ui::AXNodeData list_item_2
;
77 list_item_2
.SetName("Decepticons");
78 list_item_2
.role
= ui::AX_ROLE_LIST_ITEM
;
79 list_item_2
.state
= 0;
81 ui::AXNodeData footer
;
83 footer
.SetName("Footer");
84 footer
.role
= ui::AX_ROLE_FOOTER
;
85 footer
.state
= 1 << ui::AX_STATE_OFFSCREEN
;
87 tree_
.reset(new TestBrowserAccessibilityManager(
88 MakeAXTreeUpdate(root
, heading
, list
, list_item_1
, list_item_2
, footer
)));
91 TEST_F(MAYBE_OneShotAccessibilityTreeSearchTest
, GetAll
) {
92 OneShotAccessibilityTreeSearch
search(tree_
.get());
93 ASSERT_EQ(6U, search
.CountMatches());
96 TEST_F(MAYBE_OneShotAccessibilityTreeSearchTest
, ForwardsWithStartNode
) {
97 OneShotAccessibilityTreeSearch
search(tree_
.get());
98 search
.SetStartNode(tree_
->GetFromID(4));
99 ASSERT_EQ(2U, search
.CountMatches());
100 EXPECT_EQ(5, search
.GetMatchAtIndex(0)->GetId());
101 EXPECT_EQ(6, search
.GetMatchAtIndex(1)->GetId());
104 TEST_F(MAYBE_OneShotAccessibilityTreeSearchTest
, BackwardsWithStartNode
) {
105 OneShotAccessibilityTreeSearch
search(tree_
.get());
106 search
.SetStartNode(tree_
->GetFromID(4));
107 search
.SetDirection(OneShotAccessibilityTreeSearch::BACKWARDS
);
108 ASSERT_EQ(3U, search
.CountMatches());
109 EXPECT_EQ(3, search
.GetMatchAtIndex(0)->GetId());
110 EXPECT_EQ(2, search
.GetMatchAtIndex(1)->GetId());
111 EXPECT_EQ(1, search
.GetMatchAtIndex(2)->GetId());
114 TEST_F(MAYBE_OneShotAccessibilityTreeSearchTest
, ResultLimitZero
) {
115 OneShotAccessibilityTreeSearch
search(tree_
.get());
116 search
.SetResultLimit(0);
117 ASSERT_EQ(0U, search
.CountMatches());
120 TEST_F(MAYBE_OneShotAccessibilityTreeSearchTest
, ResultLimitFive
) {
121 OneShotAccessibilityTreeSearch
search(tree_
.get());
122 search
.SetResultLimit(5);
123 ASSERT_EQ(5U, search
.CountMatches());
126 TEST_F(MAYBE_OneShotAccessibilityTreeSearchTest
, DescendantsOnly
) {
127 OneShotAccessibilityTreeSearch
search(tree_
.get());
128 search
.SetStartNode(tree_
->GetFromID(1));
129 search
.SetImmediateDescendantsOnly(true);
130 ASSERT_EQ(3U, search
.CountMatches());
131 EXPECT_EQ(2, search
.GetMatchAtIndex(0)->GetId());
132 EXPECT_EQ(3, search
.GetMatchAtIndex(1)->GetId());
133 EXPECT_EQ(6, search
.GetMatchAtIndex(2)->GetId());
136 TEST_F(MAYBE_OneShotAccessibilityTreeSearchTest
, VisibleOnly
) {
137 OneShotAccessibilityTreeSearch
search(tree_
.get());
138 search
.SetVisibleOnly(true);
139 ASSERT_EQ(5U, search
.CountMatches());
140 EXPECT_EQ(1, search
.GetMatchAtIndex(0)->GetId());
141 EXPECT_EQ(2, search
.GetMatchAtIndex(1)->GetId());
142 EXPECT_EQ(3, search
.GetMatchAtIndex(2)->GetId());
143 EXPECT_EQ(4, search
.GetMatchAtIndex(3)->GetId());
144 EXPECT_EQ(5, search
.GetMatchAtIndex(4)->GetId());
147 TEST_F(MAYBE_OneShotAccessibilityTreeSearchTest
, CaseInsensitiveStringMatch
) {
148 OneShotAccessibilityTreeSearch
search(tree_
.get());
149 search
.SetSearchText("eCEptiCOn");
150 ASSERT_EQ(1U, search
.CountMatches());
151 EXPECT_EQ(5, search
.GetMatchAtIndex(0)->GetId());
154 TEST_F(MAYBE_OneShotAccessibilityTreeSearchTest
, OnePredicate
) {
155 OneShotAccessibilityTreeSearch
search(tree_
.get());
156 search
.AddPredicate([](BrowserAccessibility
* start
,
157 BrowserAccessibility
* current
) {
158 return current
->GetRole() == ui::AX_ROLE_LIST_ITEM
;
160 ASSERT_EQ(2U, search
.CountMatches());
161 EXPECT_EQ(4, search
.GetMatchAtIndex(0)->GetId());
162 EXPECT_EQ(5, search
.GetMatchAtIndex(1)->GetId());
165 TEST_F(MAYBE_OneShotAccessibilityTreeSearchTest
, TwoPredicates
) {
166 OneShotAccessibilityTreeSearch
search(tree_
.get());
167 search
.AddPredicate([](BrowserAccessibility
* start
,
168 BrowserAccessibility
* current
) {
169 return (current
->GetRole() == ui::AX_ROLE_LIST
||
170 current
->GetRole() == ui::AX_ROLE_LIST_ITEM
);
172 search
.AddPredicate([](BrowserAccessibility
* start
,
173 BrowserAccessibility
* current
) {
174 return (current
->GetId() % 2 == 1);
176 ASSERT_EQ(2U, search
.CountMatches());
177 EXPECT_EQ(3, search
.GetMatchAtIndex(0)->GetId());
178 EXPECT_EQ(5, search
.GetMatchAtIndex(1)->GetId());
181 } // namespace content