1 // Copyright 2013 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 "content/test/accessibility_browser_test_utils.h"
8 #include "base/logging.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "content/browser/renderer_host/render_view_host_impl.h"
12 #include "content/port/browser/render_widget_host_view_port.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/common/url_constants.h"
15 #include "content/public/test/test_utils.h"
16 #include "content/shell/browser/shell.h"
17 #include "ui/accessibility/ax_node.h"
21 AccessibilityNotificationWaiter::AccessibilityNotificationWaiter(
23 AccessibilityMode accessibility_mode
,
24 ui::AXEvent event_type
)
26 event_to_wait_for_(event_type
),
27 loop_runner_(new MessageLoopRunner()),
29 WebContents
* web_contents
= shell_
->web_contents();
30 view_host_
= static_cast<RenderViewHostImpl
*>(
31 web_contents
->GetRenderViewHost());
32 view_host_
->SetAccessibilityCallbackForTesting(
33 base::Bind(&AccessibilityNotificationWaiter::OnAccessibilityEvent
,
34 weak_factory_
.GetWeakPtr()));
35 view_host_
->SetAccessibilityMode(accessibility_mode
);
38 AccessibilityNotificationWaiter::~AccessibilityNotificationWaiter() {
41 void AccessibilityNotificationWaiter::WaitForNotification() {
45 const ui::AXTree
& AccessibilityNotificationWaiter::GetAXTree() const {
46 return view_host_
->ax_tree_for_testing();
49 void AccessibilityNotificationWaiter::OnAccessibilityEvent(
50 ui::AXEvent event_type
) {
51 if (!IsAboutBlank() && event_to_wait_for_
== event_type
)
55 bool AccessibilityNotificationWaiter::IsAboutBlank() {
56 // Skip any accessibility notifications related to "about:blank",
57 // to avoid a possible race condition between the test beginning
58 // listening for accessibility events and "about:blank" loading.
59 const ui::AXNodeData
& root
= GetAXTree().GetRoot()->data();
60 for (size_t i
= 0; i
< root
.string_attributes
.size(); ++i
) {
61 if (root
.string_attributes
[i
].first
!= ui::AX_ATTR_DOC_URL
)
63 const std::string
& doc_url
= root
.string_attributes
[i
].second
;
64 return doc_url
== kAboutBlankURL
;
69 } // namespace content