Further cleanup of some assets
[chromium-blink-merge.git] / content / test / accessibility_browser_test_utils.cc
blob58ff89c05ec312f71e2cbb041dffd7835fcbef8e
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"
7 #include "base/bind.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/frame_host/render_frame_host_impl.h"
12 #include "content/browser/renderer_host/render_widget_host_view_base.h"
13 #include "content/browser/web_contents/web_contents_impl.h"
14 #include "content/common/view_message_enums.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/common/url_constants.h"
17 #include "content/public/test/test_utils.h"
18 #include "content/shell/browser/shell.h"
19 #include "ui/accessibility/ax_node.h"
21 namespace content {
23 AccessibilityNotificationWaiter::AccessibilityNotificationWaiter(Shell* shell)
24 : event_to_wait_for_(ui::AX_EVENT_NONE),
25 loop_runner_(new MessageLoopRunner()),
26 event_target_id_(0),
27 weak_factory_(this) {
28 WebContents* web_contents = shell->web_contents();
29 frame_host_ = static_cast<RenderFrameHostImpl*>(
30 web_contents->GetMainFrame());
31 frame_host_->SetAccessibilityCallbackForTesting(
32 base::Bind(&AccessibilityNotificationWaiter::OnAccessibilityEvent,
33 weak_factory_.GetWeakPtr()));
36 AccessibilityNotificationWaiter::AccessibilityNotificationWaiter(
37 Shell* shell,
38 AccessibilityMode accessibility_mode,
39 ui::AXEvent event_type)
40 : event_to_wait_for_(event_type),
41 loop_runner_(new MessageLoopRunner()),
42 event_target_id_(0),
43 weak_factory_(this) {
44 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
45 shell->web_contents());
46 frame_host_ = static_cast<RenderFrameHostImpl*>(
47 web_contents->GetMainFrame());
48 frame_host_->SetAccessibilityCallbackForTesting(
49 base::Bind(&AccessibilityNotificationWaiter::OnAccessibilityEvent,
50 weak_factory_.GetWeakPtr()));
51 web_contents->AddAccessibilityMode(accessibility_mode);
54 AccessibilityNotificationWaiter::AccessibilityNotificationWaiter(
55 RenderFrameHostImpl* frame_host,
56 ui::AXEvent event_type)
57 : frame_host_(frame_host),
58 event_to_wait_for_(event_type),
59 loop_runner_(new MessageLoopRunner()),
60 event_target_id_(0),
61 weak_factory_(this) {
62 frame_host_->SetAccessibilityCallbackForTesting(
63 base::Bind(&AccessibilityNotificationWaiter::OnAccessibilityEvent,
64 weak_factory_.GetWeakPtr()));
67 AccessibilityNotificationWaiter::~AccessibilityNotificationWaiter() {
70 void AccessibilityNotificationWaiter::ListenToAdditionalFrame(
71 RenderFrameHostImpl* frame_host) {
72 frame_host->SetAccessibilityCallbackForTesting(
73 base::Bind(&AccessibilityNotificationWaiter::OnAccessibilityEvent,
74 weak_factory_.GetWeakPtr()));
77 void AccessibilityNotificationWaiter::WaitForNotification() {
78 loop_runner_->Run();
80 // Each loop runner can only be called once. Create a new one in case
81 // the caller wants to call this again to wait for the next notification.
82 loop_runner_ = new MessageLoopRunner();
85 const ui::AXTree& AccessibilityNotificationWaiter::GetAXTree() const {
86 return *frame_host_->GetAXTreeForTesting();
89 void AccessibilityNotificationWaiter::OnAccessibilityEvent(
90 ui::AXEvent event_type, int event_target_id) {
91 if (!IsAboutBlank() && (event_to_wait_for_ == ui::AX_EVENT_NONE ||
92 event_to_wait_for_ == event_type)) {
93 event_target_id_ = event_target_id;
94 loop_runner_->Quit();
98 bool AccessibilityNotificationWaiter::IsAboutBlank() {
99 // Skip any accessibility notifications related to "about:blank",
100 // to avoid a possible race condition between the test beginning
101 // listening for accessibility events and "about:blank" loading.
102 const ui::AXNodeData& root = GetAXTree().root()->data();
103 for (size_t i = 0; i < root.string_attributes.size(); ++i) {
104 if (root.string_attributes[i].first != ui::AX_ATTR_DOC_URL)
105 continue;
106 const std::string& doc_url = root.string_attributes[i].second;
107 return doc_url == url::kAboutBlankURL;
109 return false;
112 } // namespace content