This source file includes following definitions.
- SendPendingAccessibilityEvents
- view
- frame
- SetUp
- SetMode
- GetLastAccEvent
- CountAccessibilityNodesSentToBrowser
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/strings/utf_string_conversions.h"
#include "content/common/frame_messages.h"
#include "content/common/view_message_enums.h"
#include "content/public/test/render_view_test.h"
#include "content/renderer/accessibility/renderer_accessibility_complete.h"
#include "content/renderer/render_view_impl.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/public/platform/WebSize.h"
#include "third_party/WebKit/public/web/WebAXObject.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebView.h"
#include "ui/accessibility/ax_node_data.h"
using blink::WebAXObject;
using blink::WebDocument;
namespace content {
class TestRendererAccessibilityComplete : public RendererAccessibilityComplete {
public:
explicit TestRendererAccessibilityComplete(RenderViewImpl* render_view)
: RendererAccessibilityComplete(render_view) {
}
void SendPendingAccessibilityEvents() {
RendererAccessibilityComplete::SendPendingAccessibilityEvents();
}
};
class RendererAccessibilityTest : public RenderViewTest {
public:
RendererAccessibilityTest() {}
RenderViewImpl* view() {
return static_cast<RenderViewImpl*>(view_);
}
RenderFrameImpl* frame() {
return static_cast<RenderFrameImpl*>(view()->GetMainRenderFrame());
}
virtual void SetUp() {
RenderViewTest::SetUp();
sink_ = &render_thread_->sink();
}
void SetMode(AccessibilityMode mode) {
view()->OnSetAccessibilityMode(mode);
}
void GetLastAccEvent(
AccessibilityHostMsg_EventParams* params) {
const IPC::Message* message =
sink_->GetUniqueMessageMatching(AccessibilityHostMsg_Events::ID);
ASSERT_TRUE(message);
Tuple1<std::vector<AccessibilityHostMsg_EventParams> > param;
AccessibilityHostMsg_Events::Read(message, ¶m);
ASSERT_GE(param.a.size(), 1U);
*params = param.a[0];
}
int CountAccessibilityNodesSentToBrowser() {
AccessibilityHostMsg_EventParams event;
GetLastAccEvent(&event);
return event.update.nodes.size();
}
protected:
IPC::TestSink* sink_;
DISALLOW_COPY_AND_ASSIGN(RendererAccessibilityTest);
};
TEST_F(RendererAccessibilityTest, EditableTextModeFocusEvents) {
SetMode(AccessibilityModeEditableTextOnly);
view()->webwidget()->resize(blink::WebSize(500, 500));
view()->webwidget()->setFocus(true);
std::string html =
"<body>"
" <input>"
" <textarea></textarea>"
" <p contentEditable>Editable</p>"
" <div tabindex=0 role=textbox>Textbox</div>"
" <button>Button</button>"
" <a href=#>Link</a>"
"</body>";
LoadHTML(html.c_str());
{
SCOPED_TRACE("Initial focus on document");
AccessibilityHostMsg_EventParams event;
GetLastAccEvent(&event);
EXPECT_EQ(event.event_type,
ui::AX_EVENT_LAYOUT_COMPLETE);
EXPECT_EQ(event.id, 1);
EXPECT_EQ(event.update.nodes.size(), 2U);
EXPECT_EQ(event.update.nodes[0].id, 1);
EXPECT_EQ(event.update.nodes[0].role,
ui::AX_ROLE_ROOT_WEB_AREA);
EXPECT_EQ(event.update.nodes[0].state,
(1U << ui::AX_STATE_READ_ONLY) |
(1U << ui::AX_STATE_FOCUSABLE) |
(1U << ui::AX_STATE_FOCUSED));
EXPECT_EQ(event.update.nodes[0].child_ids.size(), 1U);
}
{
SCOPED_TRACE("input");
sink_->ClearMessages();
ExecuteJavaScript("document.querySelector('input').focus();");
AccessibilityHostMsg_EventParams event;
GetLastAccEvent(&event);
EXPECT_EQ(event.event_type,
ui::AX_EVENT_FOCUS);
EXPECT_EQ(event.id, 3);
EXPECT_EQ(event.update.nodes[0].id, 1);
EXPECT_EQ(event.update.nodes[0].role,
ui::AX_ROLE_ROOT_WEB_AREA);
EXPECT_EQ(event.update.nodes[0].state,
(1U << ui::AX_STATE_READ_ONLY) |
(1U << ui::AX_STATE_FOCUSABLE));
EXPECT_EQ(event.update.nodes[0].child_ids.size(), 1U);
EXPECT_EQ(event.update.nodes[1].id, 3);
EXPECT_EQ(event.update.nodes[1].role,
ui::AX_ROLE_GROUP);
EXPECT_EQ(event.update.nodes[1].state,
(1U << ui::AX_STATE_FOCUSABLE) |
(1U << ui::AX_STATE_FOCUSED));
}
{
SCOPED_TRACE("textarea");
sink_->ClearMessages();
ExecuteJavaScript("document.querySelector('textarea').focus();");
AccessibilityHostMsg_EventParams event;
GetLastAccEvent(&event);
EXPECT_EQ(event.id, 4);
EXPECT_EQ(event.update.nodes[1].state,
(1U << ui::AX_STATE_FOCUSABLE) |
(1U << ui::AX_STATE_FOCUSED));
}
{
SCOPED_TRACE("contentEditable");
sink_->ClearMessages();
ExecuteJavaScript("document.querySelector('p').focus();");
AccessibilityHostMsg_EventParams event;
GetLastAccEvent(&event);
EXPECT_EQ(event.id, 5);
EXPECT_EQ(event.update.nodes[1].state,
(1U << ui::AX_STATE_FOCUSABLE) |
(1U << ui::AX_STATE_FOCUSED));
}
{
SCOPED_TRACE("role=textarea");
sink_->ClearMessages();
ExecuteJavaScript("document.querySelector('div').focus();");
AccessibilityHostMsg_EventParams event;
GetLastAccEvent(&event);
EXPECT_EQ(event.id, 6);
EXPECT_EQ(event.update.nodes[1].state,
(1U << ui::AX_STATE_FOCUSABLE) |
(1U << ui::AX_STATE_FOCUSED));
}
{
SCOPED_TRACE("button");
sink_->ClearMessages();
ExecuteJavaScript("document.querySelector('button').focus();");
AccessibilityHostMsg_EventParams event;
GetLastAccEvent(&event);
EXPECT_EQ(event.id, 7);
EXPECT_EQ(event.update.nodes[1].state,
(1U << ui::AX_STATE_FOCUSABLE) |
(1U << ui::AX_STATE_FOCUSED) |
(1U << ui::AX_STATE_READ_ONLY));
}
{
SCOPED_TRACE("link");
sink_->ClearMessages();
ExecuteJavaScript("document.querySelector('a').focus();");
AccessibilityHostMsg_EventParams event;
GetLastAccEvent(&event);
EXPECT_EQ(event.id, 8);
EXPECT_EQ(event.update.nodes[1].state,
(1U << ui::AX_STATE_FOCUSABLE) |
(1U << ui::AX_STATE_FOCUSED) |
(1U << ui::AX_STATE_READ_ONLY));
}
{
SCOPED_TRACE("Back to document.");
sink_->ClearMessages();
ExecuteJavaScript("document.activeElement.blur()");
AccessibilityHostMsg_EventParams event;
GetLastAccEvent(&event);
EXPECT_EQ(event.id, 1);
}
}
TEST_F(RendererAccessibilityTest, SendFullAccessibilityTreeOnReload) {
std::string html =
"<body>"
" <div role='group' id='A'>"
" <div role='group' id='A1'></div>"
" <div role='group' id='A2'></div>"
" </div>"
"</body>";
LoadHTML(html.c_str());
scoped_ptr<TestRendererAccessibilityComplete> accessibility(
new TestRendererAccessibilityComplete(view()));
accessibility->SendPendingAccessibilityEvents();
EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
sink_->ClearMessages();
WebDocument document = view()->GetWebView()->mainFrame()->document();
WebAXObject root_obj = document.accessibilityObject();
accessibility->HandleAXEvent(
root_obj,
ui::AX_EVENT_LAYOUT_COMPLETE);
accessibility->SendPendingAccessibilityEvents();
EXPECT_EQ(1, CountAccessibilityNodesSentToBrowser());
{
AccessibilityHostMsg_EventParams event;
GetLastAccEvent(&event);
EXPECT_EQ(root_obj.axID(), event.update.nodes[0].id);
}
LoadHTML(html.c_str());
document = view()->GetWebView()->mainFrame()->document();
root_obj = document.accessibilityObject();
sink_->ClearMessages();
accessibility->HandleAXEvent(
root_obj,
ui::AX_EVENT_LAYOUT_COMPLETE);
accessibility->SendPendingAccessibilityEvents();
EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
LoadHTML(html.c_str());
document = view()->GetWebView()->mainFrame()->document();
root_obj = document.accessibilityObject();
sink_->ClearMessages();
const WebAXObject& first_child = root_obj.childAt(0);
accessibility->HandleAXEvent(
first_child,
ui::AX_EVENT_LIVE_REGION_CHANGED);
accessibility->SendPendingAccessibilityEvents();
EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
}
#if defined(OS_ANDROID)
#define MAYBE_AccessibilityMessagesQueueWhileSwappedOut \
DISABLED_AccessibilityMessagesQueueWhileSwappedOut
#else
#define MAYBE_AccessibilityMessagesQueueWhileSwappedOut \
AccessibilityMessagesQueueWhileSwappedOut
#endif
TEST_F(RendererAccessibilityTest,
MAYBE_AccessibilityMessagesQueueWhileSwappedOut) {
std::string html =
"<body>"
" <p>Hello, world.</p>"
"</body>";
LoadHTML(html.c_str());
scoped_ptr<TestRendererAccessibilityComplete> accessibility(
new TestRendererAccessibilityComplete(view()));
accessibility->SendPendingAccessibilityEvents();
EXPECT_EQ(5, CountAccessibilityNodesSentToBrowser());
sink_->ClearMessages();
WebDocument document = view()->GetWebView()->mainFrame()->document();
WebAXObject root_obj = document.accessibilityObject();
accessibility->HandleAXEvent(
root_obj,
ui::AX_EVENT_VALUE_CHANGED);
view()->main_render_frame()->OnSwapOut();
accessibility->SendPendingAccessibilityEvents();
EXPECT_FALSE(sink_->GetUniqueMessageMatching(
AccessibilityHostMsg_Events::ID));
FrameMsg_Navigate_Params nav_params;
nav_params.url = GURL("data:text/html,<p>Hello, again.</p>");
nav_params.navigation_type = FrameMsg_Navigate_Type::NORMAL;
nav_params.transition = PAGE_TRANSITION_TYPED;
nav_params.current_history_list_length = 1;
nav_params.current_history_list_offset = 0;
nav_params.pending_history_list_offset = 1;
nav_params.page_id = -1;
frame()->OnNavigate(nav_params);
accessibility->SendPendingAccessibilityEvents();
EXPECT_TRUE(sink_->GetUniqueMessageMatching(
AccessibilityHostMsg_Events::ID));
}
TEST_F(RendererAccessibilityTest, HideAccessibilityObject) {
std::string html =
"<body>"
" <div role='group' id='A'>"
" <div role='group' id='B'>"
" <div role='group' id='C' style='visibility:visible'>"
" </div>"
" </div>"
" </div>"
"</body>";
LoadHTML(html.c_str());
scoped_ptr<TestRendererAccessibilityComplete> accessibility(
new TestRendererAccessibilityComplete(view()));
accessibility->SendPendingAccessibilityEvents();
EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
WebDocument document = view()->GetWebView()->mainFrame()->document();
WebAXObject root_obj = document.accessibilityObject();
WebAXObject node_a = root_obj.childAt(0);
WebAXObject node_b = node_a.childAt(0);
WebAXObject node_c = node_b.childAt(0);
ExecuteJavaScript(
"document.getElementById('B').style.visibility = 'hidden';");
ExecuteJavaScript("document.getElementById('B').offsetLeft;");
sink_->ClearMessages();
accessibility->HandleAXEvent(
node_a,
ui::AX_EVENT_CHILDREN_CHANGED);
accessibility->SendPendingAccessibilityEvents();
AccessibilityHostMsg_EventParams event;
GetLastAccEvent(&event);
ASSERT_EQ(2U, event.update.nodes.size());
EXPECT_EQ(node_a.axID(), event.update.node_id_to_clear);
EXPECT_EQ(node_a.axID(), event.update.nodes[0].id);
EXPECT_EQ(node_c.axID(), event.update.nodes[1].id);
EXPECT_EQ(2, CountAccessibilityNodesSentToBrowser());
}
TEST_F(RendererAccessibilityTest, ShowAccessibilityObject) {
std::string html =
"<body>"
" <div role='group' id='A'>"
" <div role='group' id='B' style='visibility:hidden'>"
" <div role='group' id='C' style='visibility:visible'>"
" </div>"
" </div>"
" </div>"
"</body>";
LoadHTML(html.c_str());
scoped_ptr<TestRendererAccessibilityComplete> accessibility(
new TestRendererAccessibilityComplete(view()));
accessibility->SendPendingAccessibilityEvents();
EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
ExecuteJavaScript(
"document.getElementById('B').style.visibility = 'visible';");
ExecuteJavaScript("document.getElementById('B').offsetLeft;");
sink_->ClearMessages();
WebDocument document = view()->GetWebView()->mainFrame()->document();
WebAXObject root_obj = document.accessibilityObject();
WebAXObject node_a = root_obj.childAt(0);
WebAXObject node_b = node_a.childAt(0);
WebAXObject node_c = node_b.childAt(0);
accessibility->HandleAXEvent(
node_a,
ui::AX_EVENT_CHILDREN_CHANGED);
accessibility->SendPendingAccessibilityEvents();
AccessibilityHostMsg_EventParams event;
GetLastAccEvent(&event);
ASSERT_EQ(3U, event.update.nodes.size());
EXPECT_EQ(node_a.axID(), event.update.node_id_to_clear);
EXPECT_EQ(node_a.axID(), event.update.nodes[0].id);
EXPECT_EQ(node_b.axID(), event.update.nodes[1].id);
EXPECT_EQ(node_c.axID(), event.update.nodes[2].id);
EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
}
TEST_F(RendererAccessibilityTest, DetachAccessibilityObject) {
std::string html =
"<body aria-label='Body'>"
"<span>1</span><span style='display:block'>2</span>"
"</body>";
LoadHTML(html.c_str());
scoped_ptr<TestRendererAccessibilityComplete> accessibility(
new TestRendererAccessibilityComplete(view()));
accessibility->SendPendingAccessibilityEvents();
EXPECT_EQ(7, CountAccessibilityNodesSentToBrowser());
WebDocument document = view()->GetWebView()->mainFrame()->document();
WebAXObject root_obj = document.accessibilityObject();
WebAXObject body = root_obj.childAt(0);
WebAXObject anonymous_block = body.childAt(0);
WebAXObject text_1 = anonymous_block.childAt(0);
WebAXObject text_2 = body.childAt(1);
ExecuteJavaScript(
"document.querySelectorAll('span')[1].style.display = 'inline';");
ExecuteJavaScript("document.body.offsetLeft;");
sink_->ClearMessages();
accessibility->HandleAXEvent(
body,
ui::AX_EVENT_CHILDREN_CHANGED);
accessibility->SendPendingAccessibilityEvents();
AccessibilityHostMsg_EventParams event;
GetLastAccEvent(&event);
ASSERT_EQ(5U, event.update.nodes.size());
EXPECT_EQ(body.axID(), event.update.nodes[0].id);
EXPECT_EQ(text_1.axID(), event.update.nodes[1].id);
}
}