1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
content / renderer / accessibility / annotations / ax_image_annotator_browsertest.cc [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/renderer/accessibility/annotations/ax_image_annotator.h"
#include "base/test/scoped_feature_list.h"
#include "content/renderer/accessibility/annotations/ax_annotators_manager.h"
#include "content/renderer/accessibility/render_accessibility_impl_test.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "third_party/blink/public/web/web_element.h"
#include "third_party/blink/public/web/web_local_frame.h"
namespace content {
using blink::WebAXObject;
using blink::WebDocument;
using testing::ElementsAre;
class TestAXImageAnnotator : public AXImageAnnotator {
public:
TestAXImageAnnotator(RenderAccessibilityImpl* const render_accessibility)
: AXImageAnnotator(render_accessibility) {}
TestAXImageAnnotator(const TestAXImageAnnotator&) = delete;
TestAXImageAnnotator& operator=(const TestAXImageAnnotator&) = delete;
~TestAXImageAnnotator() override = default;
private:
std::string GenerateImageSourceId(
const blink::WebAXObject& image) const override {
std::string image_id;
if (image.IsDetached() || image.IsNull() || image.GetNode().IsNull() ||
image.GetNode().To<blink::WebElement>().IsNull()) {
ADD_FAILURE() << "Unable to retrieve the image src.";
return image_id;
}
image_id =
image.GetNode().To<blink::WebElement>().GetAttribute("SRC").Utf8();
return image_id;
}
};
class MockImageAnnotationService : public image_annotation::mojom::Annotator {
public:
MockImageAnnotationService() = default;
MockImageAnnotationService(const MockImageAnnotationService&) = delete;
MockImageAnnotationService& operator=(const MockImageAnnotationService&) =
delete;
~MockImageAnnotationService() override = default;
mojo::PendingRemote<image_annotation::mojom::Annotator> GetRemote() {
mojo::PendingRemote<image_annotation::mojom::Annotator> remote;
receivers_.Add(this, remote.InitWithNewPipeAndPassReceiver());
return remote;
}
void AnnotateImage(
const std::string& image_id,
const std::string& /* description_language_tag */,
mojo::PendingRemote<image_annotation::mojom::ImageProcessor>
image_processor,
AnnotateImageCallback callback) override {
image_ids_.push_back(image_id);
image_processors_.push_back(
mojo::Remote<image_annotation::mojom::ImageProcessor>(
std::move(image_processor)));
image_processors_.back().set_disconnect_handler(
base::BindOnce(&MockImageAnnotationService::ResetImageProcessor,
base::Unretained(this), image_processors_.size() - 1));
callbacks_.push_back(std::move(callback));
}
// Tests should not delete entries in these lists.
std::vector<std::string> image_ids_;
std::vector<mojo::Remote<image_annotation::mojom::ImageProcessor>>
image_processors_;
std::vector<AnnotateImageCallback> callbacks_;
private:
void ResetImageProcessor(const size_t index) {
image_processors_[index].reset();
}
mojo::ReceiverSet<image_annotation::mojom::Annotator> receivers_;
};
class AXImageAnnotatorTest : public RenderAccessibilityImplTest {
public:
AXImageAnnotatorTest() = default;
AXImageAnnotatorTest(const AXImageAnnotatorTest&) = delete;
AXImageAnnotatorTest& operator=(const AXImageAnnotatorTest&) = delete;
~AXImageAnnotatorTest() override = default;
protected:
void SetUp() override {
RenderAccessibilityImplTest::SetUp();
// TODO(nektar): Add the ability to test the AX action that labels images
// only once.
ui::AXMode mode = ui::kAXModeComplete;
mode.set_mode(ui::AXMode::kLabelImages, true);
SetMode(mode);
auto annotator =
std::make_unique<TestAXImageAnnotator>(GetRenderAccessibilityImpl());
annotator->BindAnnotatorForTesting(mock_annotator_service().GetRemote());
GetRenderAccessibilityImpl()
->ax_annotators_manager_for_testing()
->AddAnnotatorForTesting(std::move(annotator));
AXImageAnnotator::IgnoreProtocolChecksForTesting();
task_environment_.RunUntilIdle();
}
void TearDown() override {
GetRenderAccessibilityImpl()
->ax_annotators_manager_for_testing()
->ClearAnnotatorsForTesting();
task_environment_.RunUntilIdle();
RenderAccessibilityImplTest::TearDown();
}
MockImageAnnotationService& mock_annotator_service() {
return mock_annotator_service_;
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
MockImageAnnotationService mock_annotator_service_;
};
// TODO(crbug.com/1477047, fuchsia:132924): Reenable test on Fuchsia once
// post-lifecycle serialization is turned on.
#if BUILDFLAG(IS_FUCHSIA)
#define MAYBE_OnImageAdded DISABLED_OnImageAdded
#else
#define MAYBE_OnImageAdded OnImageAdded
#endif
TEST_F(AXImageAnnotatorTest, MAYBE_OnImageAdded) {
LoadHTMLAndRefreshAccessibilityTree(R"HTML(
<body>
<p>Test document</p>
<img id="A" src="test1.jpg"
style="width: 200px; height: 150px;">
<img id="B" src="test2.jpg"
style="visibility: hidden; width: 200px; height: 150px;">
</body>
)HTML");
// Every time we call a method on a Mojo interface, a message is posted to the
// current task queue. We need to ask the queue to drain itself before we
// check test expectations.
task_environment_.RunUntilIdle();
EXPECT_THAT(mock_annotator_service().image_ids_, ElementsAre("test1.jpg"));
ASSERT_EQ(1u, mock_annotator_service().image_processors_.size());
EXPECT_TRUE(mock_annotator_service().image_processors_[0].is_bound());
EXPECT_EQ(1u, mock_annotator_service().callbacks_.size());
WebDocument document = GetMainFrame()->GetDocument();
WebAXObject root_obj = WebAXObject::FromWebDocument(document);
ASSERT_FALSE(root_obj.IsNull());
// Show node "B".
ExecuteJavaScriptForTests(
"document.getElementById('B').style.visibility = 'visible';");
SendPendingAccessibilityEvents();
ClearHandledUpdates();
// This should update the annotations of all images on the page, including the
// already visible one.
MarkSubtreeDirty(root_obj);
SendPendingAccessibilityEvents();
EXPECT_THAT(mock_annotator_service().image_ids_,
ElementsAre("test1.jpg", "test2.jpg", "test1.jpg", "test2.jpg"));
ASSERT_EQ(4u, mock_annotator_service().image_processors_.size());
EXPECT_TRUE(mock_annotator_service().image_processors_[0].is_bound());
EXPECT_TRUE(mock_annotator_service().image_processors_[1].is_bound());
EXPECT_TRUE(mock_annotator_service().image_processors_[2].is_bound());
EXPECT_TRUE(mock_annotator_service().image_processors_[3].is_bound());
EXPECT_EQ(4u, mock_annotator_service().callbacks_.size());
}
TEST_F(AXImageAnnotatorTest, OnImageUpdated) {
LoadHTMLAndRefreshAccessibilityTree(R"HTML(
<body>
<p>Test document</p>
<img id="A" src="test1.jpg"
style="width: 200px; height: 150px;">
</body>
)HTML");
// Every time we call a method on a Mojo interface, a message is posted to the
// current task queue. We need to ask the queue to drain itself before we
// check test expectations.
task_environment_.RunUntilIdle();
EXPECT_THAT(mock_annotator_service().image_ids_, ElementsAre("test1.jpg"));
ASSERT_EQ(1u, mock_annotator_service().image_processors_.size());
EXPECT_TRUE(mock_annotator_service().image_processors_[0].is_bound());
EXPECT_EQ(1u, mock_annotator_service().callbacks_.size());
ClearHandledUpdates();
WebDocument document = GetMainFrame()->GetDocument();
WebAXObject root_obj = WebAXObject::FromWebDocument(document);
ASSERT_FALSE(root_obj.IsNull());
// This should update the annotations of all images on the page.
MarkSubtreeDirty(root_obj);
SendPendingAccessibilityEvents();
EXPECT_THAT(mock_annotator_service().image_ids_,
ElementsAre("test1.jpg", "test1.jpg"));
ASSERT_EQ(2u, mock_annotator_service().image_processors_.size());
EXPECT_TRUE(mock_annotator_service().image_processors_[0].is_bound());
EXPECT_TRUE(mock_annotator_service().image_processors_[1].is_bound());
EXPECT_EQ(2u, mock_annotator_service().callbacks_.size());
// Update node "A".
ExecuteJavaScriptForTests("document.querySelector('img').src = 'test2.jpg';");
SendPendingAccessibilityEvents();
ClearHandledUpdates();
// This should update the annotations of all images on the page, including the
// now updated image src.
MarkSubtreeDirty(root_obj);
SendPendingAccessibilityEvents();
EXPECT_THAT(mock_annotator_service().image_ids_,
ElementsAre("test1.jpg", "test1.jpg", "test2.jpg"));
ASSERT_EQ(3u, mock_annotator_service().image_processors_.size());
EXPECT_TRUE(mock_annotator_service().image_processors_[0].is_bound());
EXPECT_TRUE(mock_annotator_service().image_processors_[1].is_bound());
EXPECT_TRUE(mock_annotator_service().image_processors_[2].is_bound());
EXPECT_EQ(3u, mock_annotator_service().callbacks_.size());
}
} // namespace content