Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / test / accessibility_browser_test_utils.cc
blobae514aec4af7ebb37fc4bfd07c70ce2763a0ea7a
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 : shell_(shell),
25 event_to_wait_for_(ui::AX_EVENT_NONE),
26 loop_runner_(new MessageLoopRunner()),
27 weak_factory_(this),
28 event_target_id_(0) {
29 WebContents* web_contents = shell_->web_contents();
30 frame_host_ = static_cast<RenderFrameHostImpl*>(
31 web_contents->GetMainFrame());
32 frame_host_->SetAccessibilityCallbackForTesting(
33 base::Bind(&AccessibilityNotificationWaiter::OnAccessibilityEvent,
34 weak_factory_.GetWeakPtr()));
37 AccessibilityNotificationWaiter::AccessibilityNotificationWaiter(
38 Shell* shell,
39 AccessibilityMode accessibility_mode,
40 ui::AXEvent event_type)
41 : shell_(shell),
42 event_to_wait_for_(event_type),
43 loop_runner_(new MessageLoopRunner()),
44 weak_factory_(this),
45 event_target_id_(0) {
46 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
47 shell_->web_contents());
48 frame_host_ = static_cast<RenderFrameHostImpl*>(
49 web_contents->GetMainFrame());
50 frame_host_->SetAccessibilityCallbackForTesting(
51 base::Bind(&AccessibilityNotificationWaiter::OnAccessibilityEvent,
52 weak_factory_.GetWeakPtr()));
53 web_contents->AddAccessibilityMode(accessibility_mode);
56 AccessibilityNotificationWaiter::~AccessibilityNotificationWaiter() {
59 void AccessibilityNotificationWaiter::WaitForNotification() {
60 loop_runner_->Run();
62 // Each loop runner can only be called once. Create a new one in case
63 // the caller wants to call this again to wait for the next notification.
64 loop_runner_ = new MessageLoopRunner();
67 const ui::AXTree& AccessibilityNotificationWaiter::GetAXTree() const {
68 return *frame_host_->GetAXTreeForTesting();
71 void AccessibilityNotificationWaiter::OnAccessibilityEvent(
72 ui::AXEvent event_type, int event_target_id) {
73 if (!IsAboutBlank() && (event_to_wait_for_ == ui::AX_EVENT_NONE ||
74 event_to_wait_for_ == event_type)) {
75 event_target_id_ = event_target_id;
76 loop_runner_->Quit();
80 bool AccessibilityNotificationWaiter::IsAboutBlank() {
81 // Skip any accessibility notifications related to "about:blank",
82 // to avoid a possible race condition between the test beginning
83 // listening for accessibility events and "about:blank" loading.
84 const ui::AXNodeData& root = GetAXTree().GetRoot()->data();
85 for (size_t i = 0; i < root.string_attributes.size(); ++i) {
86 if (root.string_attributes[i].first != ui::AX_ATTR_DOC_URL)
87 continue;
88 const std::string& doc_url = root.string_attributes[i].second;
89 return doc_url == url::kAboutBlankURL;
91 return false;
94 } // namespace content