| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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. | |
| 4 | |
| 5 #include <string> | |
| 6 #include <vector> | |
| 7 | |
| 8 #include "base/utf_string_conversions.h" | |
| 9 #include "chrome/browser/browser_window.h" | |
| 10 #include "chrome/browser/renderer_host/render_view_host.h" | |
| 11 #include "chrome/browser/renderer_host/render_widget_host.h" | |
| 12 #include "chrome/browser/renderer_host/render_widget_host_view.h" | |
| 13 #include "chrome/browser/tab_contents/tab_contents.h" | |
| 14 #include "chrome/browser/ui/browser.h" | |
| 15 #include "chrome/common/render_messages.h" | |
| 16 #include "chrome/common/notification_type.h" | |
| 17 #include "chrome/test/in_process_browser_test.h" | |
| 18 #include "chrome/test/ui_test_utils.h" | |
| 19 | |
| 20 #if defined(OS_WIN) | |
| 21 #include <atlbase.h> | |
| 22 #include <atlcom.h> | |
| 23 #endif | |
| 24 | |
| 25 using webkit_glue::WebAccessibility; | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 class RendererAccessibilityBrowserTest : public InProcessBrowserTest { | |
| 30 public: | |
| 31 RendererAccessibilityBrowserTest() {} | |
| 32 | |
| 33 // Tell the renderer to send an accessibility tree, then wait for the | |
| 34 // notification that it's been received. | |
| 35 const WebAccessibility& GetWebAccessibilityTree() { | |
| 36 RenderWidgetHostView* host_view = | |
| 37 browser()->GetSelectedTabContents()->GetRenderWidgetHostView(); | |
| 38 RenderWidgetHost* host = host_view->GetRenderWidgetHost(); | |
| 39 RenderViewHost* view_host = static_cast<RenderViewHost*>(host); | |
| 40 view_host->set_save_accessibility_tree_for_testing(true); | |
| 41 view_host->EnableRendererAccessibility(); | |
| 42 ui_test_utils::WaitForNotification( | |
| 43 NotificationType::RENDER_VIEW_HOST_ACCESSIBILITY_TREE_UPDATED); | |
| 44 return view_host->accessibility_tree(); | |
| 45 } | |
| 46 | |
| 47 // InProcessBrowserTest | |
| 48 void SetUpInProcessBrowserTestFixture(); | |
| 49 void TearDownInProcessBrowserTestFixture(); | |
| 50 | |
| 51 protected: | |
| 52 std::string GetAttr(const WebAccessibility& node, | |
| 53 const WebAccessibility::Attribute attr); | |
| 54 }; | |
| 55 | |
| 56 void RendererAccessibilityBrowserTest::SetUpInProcessBrowserTestFixture() { | |
| 57 #if defined(OS_WIN) | |
| 58 // ATL needs a pointer to a COM module. | |
| 59 static CComModule module; | |
| 60 _pAtlModule = &module; | |
| 61 | |
| 62 // Make sure COM is initialized for this thread; it's safe to call twice. | |
| 63 ::CoInitialize(NULL); | |
| 64 #endif | |
| 65 } | |
| 66 | |
| 67 void RendererAccessibilityBrowserTest::TearDownInProcessBrowserTestFixture() { | |
| 68 #if defined(OS_WIN) | |
| 69 ::CoUninitialize(); | |
| 70 #endif | |
| 71 } | |
| 72 | |
| 73 // Convenience method to get the value of a particular WebAccessibility | |
| 74 // node attribute as a UTF-8 const char*. | |
| 75 std::string RendererAccessibilityBrowserTest::GetAttr( | |
| 76 const WebAccessibility& node, const WebAccessibility::Attribute attr) { | |
| 77 std::map<int32, string16>::const_iterator iter = node.attributes.find(attr); | |
| 78 if (iter != node.attributes.end()) | |
| 79 return UTF16ToUTF8(iter->second); | |
| 80 else | |
| 81 return ""; | |
| 82 } | |
| 83 | |
| 84 IN_PROC_BROWSER_TEST_F(RendererAccessibilityBrowserTest, | |
| 85 CrossPlatformWebpageAccessibility) { | |
| 86 // Create a data url and load it. | |
| 87 const char url_str[] = | |
| 88 "data:text/html," | |
| 89 "<!doctype html>" | |
| 90 "<html><head><title>Accessibility Test</title></head>" | |
| 91 "<body><input type='button' value='push' /><input type='checkbox' />" | |
| 92 "</body></html>"; | |
| 93 GURL url(url_str); | |
| 94 browser()->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED); | |
| 95 const WebAccessibility& tree = GetWebAccessibilityTree(); | |
| 96 | |
| 97 // Check properties of the root element of the tree. | |
| 98 EXPECT_STREQ(url_str, GetAttr(tree, WebAccessibility::ATTR_DOC_URL).c_str()); | |
| 99 EXPECT_STREQ( | |
| 100 "Accessibility Test", | |
| 101 GetAttr(tree, WebAccessibility::ATTR_DOC_TITLE).c_str()); | |
| 102 EXPECT_STREQ( | |
| 103 "html", GetAttr(tree, WebAccessibility::ATTR_DOC_DOCTYPE).c_str()); | |
| 104 EXPECT_STREQ( | |
| 105 "text/html", GetAttr(tree, WebAccessibility::ATTR_DOC_MIMETYPE).c_str()); | |
| 106 EXPECT_STREQ("Accessibility Test", UTF16ToUTF8(tree.name).c_str()); | |
| 107 EXPECT_EQ(WebAccessibility::ROLE_WEB_AREA, tree.role); | |
| 108 | |
| 109 // Check properites of the BODY element. | |
| 110 ASSERT_EQ(1U, tree.children.size()); | |
| 111 const WebAccessibility& body = tree.children[0]; | |
| 112 EXPECT_EQ(WebAccessibility::ROLE_GROUP, body.role); | |
| 113 EXPECT_STREQ("body", GetAttr(body, WebAccessibility::ATTR_HTML_TAG).c_str()); | |
| 114 EXPECT_STREQ("block", GetAttr(body, WebAccessibility::ATTR_DISPLAY).c_str()); | |
| 115 | |
| 116 // Check properties of the two children of the BODY element. | |
| 117 ASSERT_EQ(2U, body.children.size()); | |
| 118 | |
| 119 const WebAccessibility& button = body.children[0]; | |
| 120 EXPECT_EQ(WebAccessibility::ROLE_BUTTON, button.role); | |
| 121 EXPECT_STREQ( | |
| 122 "input", GetAttr(button, WebAccessibility::ATTR_HTML_TAG).c_str()); | |
| 123 EXPECT_STREQ("push", UTF16ToUTF8(button.name).c_str()); | |
| 124 EXPECT_STREQ( | |
| 125 "inline-block", GetAttr(button, WebAccessibility::ATTR_DISPLAY).c_str()); | |
| 126 ASSERT_EQ(2U, button.html_attributes.size()); | |
| 127 EXPECT_STREQ("type", UTF16ToUTF8(button.html_attributes[0].first).c_str()); | |
| 128 EXPECT_STREQ("button", UTF16ToUTF8(button.html_attributes[0].second).c_str()); | |
| 129 EXPECT_STREQ("value", UTF16ToUTF8(button.html_attributes[1].first).c_str()); | |
| 130 EXPECT_STREQ("push", UTF16ToUTF8(button.html_attributes[1].second).c_str()); | |
| 131 | |
| 132 const WebAccessibility& checkbox = body.children[1]; | |
| 133 EXPECT_EQ(WebAccessibility::ROLE_CHECKBOX, checkbox.role); | |
| 134 EXPECT_STREQ( | |
| 135 "input", GetAttr(checkbox, WebAccessibility::ATTR_HTML_TAG).c_str()); | |
| 136 EXPECT_STREQ( | |
| 137 "inline-block", | |
| 138 GetAttr(checkbox, WebAccessibility::ATTR_DISPLAY).c_str()); | |
| 139 ASSERT_EQ(1U, checkbox.html_attributes.size()); | |
| 140 EXPECT_STREQ( | |
| 141 "type", UTF16ToUTF8(checkbox.html_attributes[0].first).c_str()); | |
| 142 EXPECT_STREQ( | |
| 143 "checkbox", UTF16ToUTF8(checkbox.html_attributes[0].second).c_str()); | |
| 144 } | |
| 145 | |
| 146 IN_PROC_BROWSER_TEST_F(RendererAccessibilityBrowserTest, | |
| 147 CrossPlatformUnselectedEditableTextAccessibility) { | |
| 148 // Create a data url and load it. | |
| 149 const char url_str[] = | |
| 150 "data:text/html," | |
| 151 "<!doctype html>" | |
| 152 "<body>" | |
| 153 "<input value=\"Hello, world.\"/>" | |
| 154 "</body></html>"; | |
| 155 GURL url(url_str); | |
| 156 browser()->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED); | |
| 157 | |
| 158 const WebAccessibility& tree = GetWebAccessibilityTree(); | |
| 159 ASSERT_EQ(1U, tree.children.size()); | |
| 160 const WebAccessibility& body = tree.children[0]; | |
| 161 ASSERT_EQ(1U, body.children.size()); | |
| 162 const WebAccessibility& text = body.children[0]; | |
| 163 EXPECT_EQ(WebAccessibility::ROLE_TEXT_FIELD, text.role); | |
| 164 EXPECT_STREQ( | |
| 165 "input", GetAttr(text, WebAccessibility::ATTR_HTML_TAG).c_str()); | |
| 166 EXPECT_STREQ( | |
| 167 "0", GetAttr(text, WebAccessibility::ATTR_TEXT_SEL_START).c_str()); | |
| 168 EXPECT_STREQ( | |
| 169 "0", GetAttr(text, WebAccessibility::ATTR_TEXT_SEL_END).c_str()); | |
| 170 EXPECT_STREQ("Hello, world.", UTF16ToUTF8(text.value).c_str()); | |
| 171 | |
| 172 // TODO(dmazzoni): as soon as more accessibility code is cross-platform, | |
| 173 // this code should test that the accessible info is dynamically updated | |
| 174 // if the selection or value changes. | |
| 175 } | |
| 176 | |
| 177 IN_PROC_BROWSER_TEST_F(RendererAccessibilityBrowserTest, | |
| 178 CrossPlatformSelectedEditableTextAccessibility) { | |
| 179 // Create a data url and load it. | |
| 180 const char url_str[] = | |
| 181 "data:text/html," | |
| 182 "<!doctype html>" | |
| 183 "<body onload=\"document.body.children[0].select();\">" | |
| 184 "<input value=\"Hello, world.\"/>" | |
| 185 "</body></html>"; | |
| 186 GURL url(url_str); | |
| 187 browser()->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED); | |
| 188 | |
| 189 const WebAccessibility& tree = GetWebAccessibilityTree(); | |
| 190 ASSERT_EQ(1U, tree.children.size()); | |
| 191 const WebAccessibility& body = tree.children[0]; | |
| 192 ASSERT_EQ(1U, body.children.size()); | |
| 193 const WebAccessibility& text = body.children[0]; | |
| 194 EXPECT_EQ(WebAccessibility::ROLE_TEXT_FIELD, text.role); | |
| 195 EXPECT_STREQ( | |
| 196 "input", GetAttr(text, WebAccessibility::ATTR_HTML_TAG).c_str()); | |
| 197 EXPECT_STREQ( | |
| 198 "0", GetAttr(text, WebAccessibility::ATTR_TEXT_SEL_START).c_str()); | |
| 199 EXPECT_STREQ( | |
| 200 "13", GetAttr(text, WebAccessibility::ATTR_TEXT_SEL_END).c_str()); | |
| 201 EXPECT_STREQ("Hello, world.", UTF16ToUTF8(text.value).c_str()); | |
| 202 } | |
| 203 | |
| 204 } // namespace | |
| OLD | NEW |