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/browser/renderer_host/render_widget_host_view_base.h"
13 #include "content/common/view_message_enums.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/common/url_constants.h"
16 #include "content/public/test/test_utils.h"
17 #include "content/shell/browser/shell.h"
18 #include "ui/accessibility/ax_node.h"
22 AccessibilityNotificationWaiter::AccessibilityNotificationWaiter(Shell
* shell
)
24 event_to_wait_for_(ui::AX_EVENT_NONE
),
25 loop_runner_(new MessageLoopRunner()),
28 WebContents
* web_contents
= shell_
->web_contents();
29 view_host_
= static_cast<RenderViewHostImpl
*>(
30 web_contents
->GetRenderViewHost());
31 view_host_
->SetAccessibilityCallbackForTesting(
32 base::Bind(&AccessibilityNotificationWaiter::OnAccessibilityEvent
,
33 weak_factory_
.GetWeakPtr()));
36 AccessibilityNotificationWaiter::AccessibilityNotificationWaiter(
38 AccessibilityMode accessibility_mode
,
39 ui::AXEvent event_type
)
41 event_to_wait_for_(event_type
),
42 loop_runner_(new MessageLoopRunner()),
45 WebContents
* web_contents
= shell_
->web_contents();
46 view_host_
= static_cast<RenderViewHostImpl
*>(
47 web_contents
->GetRenderViewHost());
48 view_host_
->SetAccessibilityCallbackForTesting(
49 base::Bind(&AccessibilityNotificationWaiter::OnAccessibilityEvent
,
50 weak_factory_
.GetWeakPtr()));
51 view_host_
->AddAccessibilityMode(accessibility_mode
);
54 AccessibilityNotificationWaiter::~AccessibilityNotificationWaiter() {
57 void AccessibilityNotificationWaiter::WaitForNotification() {
61 const ui::AXTree
& AccessibilityNotificationWaiter::GetAXTree() const {
62 return view_host_
->ax_tree_for_testing();
65 void AccessibilityNotificationWaiter::OnAccessibilityEvent(
66 ui::AXEvent event_type
, int event_target_id
) {
67 if (!IsAboutBlank() && (event_to_wait_for_
== ui::AX_EVENT_NONE
||
68 event_to_wait_for_
== event_type
)) {
69 event_target_id_
= event_target_id
;
74 bool AccessibilityNotificationWaiter::IsAboutBlank() {
75 // Skip any accessibility notifications related to "about:blank",
76 // to avoid a possible race condition between the test beginning
77 // listening for accessibility events and "about:blank" loading.
78 const ui::AXNodeData
& root
= GetAXTree().GetRoot()->data();
79 for (size_t i
= 0; i
< root
.string_attributes
.size(); ++i
) {
80 if (root
.string_attributes
[i
].first
!= ui::AX_ATTR_DOC_URL
)
82 const std::string
& doc_url
= root
.string_attributes
[i
].second
;
83 return doc_url
== kAboutBlankURL
;
88 } // namespace content