| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/browser.h" | 9 #include "chrome/browser/browser.h" |
| 10 #include "chrome/browser/browser_window.h" | 10 #include "chrome/browser/browser_window.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #endif | 23 #endif |
| 24 | 24 |
| 25 using webkit_glue::WebAccessibility; | 25 using webkit_glue::WebAccessibility; |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 class RendererAccessibilityBrowserTest : public InProcessBrowserTest { | 29 class RendererAccessibilityBrowserTest : public InProcessBrowserTest { |
| 30 public: | 30 public: |
| 31 RendererAccessibilityBrowserTest() {} | 31 RendererAccessibilityBrowserTest() {} |
| 32 | 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 |
| 33 // InProcessBrowserTest | 47 // InProcessBrowserTest |
| 34 void SetUpInProcessBrowserTestFixture(); | 48 void SetUpInProcessBrowserTestFixture(); |
| 35 void TearDownInProcessBrowserTestFixture(); | 49 void TearDownInProcessBrowserTestFixture(); |
| 36 | 50 |
| 37 protected: | 51 protected: |
| 38 std::string GetAttr(const WebAccessibility& node, | 52 std::string GetAttr(const WebAccessibility& node, |
| 39 const WebAccessibility::Attribute attr); | 53 const WebAccessibility::Attribute attr); |
| 40 }; | 54 }; |
| 41 | 55 |
| 42 void RendererAccessibilityBrowserTest::SetUpInProcessBrowserTestFixture() { | 56 void RendererAccessibilityBrowserTest::SetUpInProcessBrowserTestFixture() { |
| 43 #if defined(OS_WIN) | 57 #if defined(OS_WIN) |
| 44 // ATL needs a pointer to a COM module. | 58 // ATL needs a pointer to a COM module. |
| 45 static CComModule module; | 59 static CComModule module; |
| 46 _pAtlModule = &module; | 60 _pAtlModule = &module; |
| 47 | 61 |
| 48 // Make sure COM is initialized for this thread; it's safe to call twice. | 62 // Make sure COM is initialized for this thread; it's safe to call twice. |
| 49 ::CoInitialize(NULL); | 63 ::CoInitialize(NULL); |
| 50 #endif | 64 #endif |
| 51 } | 65 } |
| 52 | 66 |
| 53 void RendererAccessibilityBrowserTest::TearDownInProcessBrowserTestFixture() { | 67 void RendererAccessibilityBrowserTest::TearDownInProcessBrowserTestFixture() { |
| 54 #if defined(OS_WIN) | 68 #if defined(OS_WIN) |
| 55 ::CoUninitialize(); | 69 ::CoUninitialize(); |
| 56 #endif | 70 #endif |
| 57 } | 71 } |
| 72 |
| 58 // Convenience method to get the value of a particular WebAccessibility | 73 // Convenience method to get the value of a particular WebAccessibility |
| 59 // node attribute as a UTF-8 const char*. | 74 // node attribute as a UTF-8 const char*. |
| 60 std::string RendererAccessibilityBrowserTest::GetAttr( | 75 std::string RendererAccessibilityBrowserTest::GetAttr( |
| 61 const WebAccessibility& node, const WebAccessibility::Attribute attr) { | 76 const WebAccessibility& node, const WebAccessibility::Attribute attr) { |
| 62 std::map<int32, string16>::const_iterator iter = node.attributes.find(attr); | 77 std::map<int32, string16>::const_iterator iter = node.attributes.find(attr); |
| 63 if (iter != node.attributes.end()) | 78 if (iter != node.attributes.end()) |
| 64 return UTF16ToUTF8(iter->second); | 79 return UTF16ToUTF8(iter->second); |
| 65 else | 80 else |
| 66 return ""; | 81 return ""; |
| 67 } | 82 } |
| 68 | 83 |
| 69 IN_PROC_BROWSER_TEST_F(RendererAccessibilityBrowserTest, | 84 IN_PROC_BROWSER_TEST_F(RendererAccessibilityBrowserTest, |
| 70 TestCrossPlatformAccessibilityTree) { | 85 CrossPlatformWebpageAccessibility) { |
| 71 // Create a data url and load it. | 86 // Create a data url and load it. |
| 72 const char url_str[] = | 87 const char url_str[] = |
| 73 "data:text/html," | 88 "data:text/html," |
| 74 "<!doctype html>" | 89 "<!doctype html>" |
| 75 "<html><head><title>Accessibility Test</title></head>" | 90 "<html><head><title>Accessibility Test</title></head>" |
| 76 "<body><input type='button' value='push' /><input type='checkbox' />" | 91 "<body><input type='button' value='push' /><input type='checkbox' />" |
| 77 "</body></html>"; | 92 "</body></html>"; |
| 78 GURL url(url_str); | 93 GURL url(url_str); |
| 79 browser()->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED); | 94 browser()->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED); |
| 80 | 95 const WebAccessibility& tree = GetWebAccessibilityTree(); |
| 81 // Tell the renderer to send an accessibility tree, then wait for the | |
| 82 // notification that it's been received. | |
| 83 RenderWidgetHostView* host_view = | |
| 84 browser()->GetSelectedTabContents()->GetRenderWidgetHostView(); | |
| 85 RenderWidgetHost* host = host_view->GetRenderWidgetHost(); | |
| 86 RenderViewHost* view_host = static_cast<RenderViewHost*>(host); | |
| 87 view_host->set_save_accessibility_tree_for_testing(true); | |
| 88 view_host->EnableRendererAccessibility(); | |
| 89 ui_test_utils::WaitForNotification( | |
| 90 NotificationType::RENDER_VIEW_HOST_ACCESSIBILITY_TREE_UPDATED); | |
| 91 | 96 |
| 92 // Check properties of the root element of the tree. | 97 // Check properties of the root element of the tree. |
| 93 const WebAccessibility& tree = view_host->accessibility_tree(); | |
| 94 EXPECT_STREQ(url_str, GetAttr(tree, WebAccessibility::ATTR_DOC_URL).c_str()); | 98 EXPECT_STREQ(url_str, GetAttr(tree, WebAccessibility::ATTR_DOC_URL).c_str()); |
| 95 EXPECT_STREQ( | 99 EXPECT_STREQ( |
| 96 "Accessibility Test", | 100 "Accessibility Test", |
| 97 GetAttr(tree, WebAccessibility::ATTR_DOC_TITLE).c_str()); | 101 GetAttr(tree, WebAccessibility::ATTR_DOC_TITLE).c_str()); |
| 98 EXPECT_STREQ( | 102 EXPECT_STREQ( |
| 99 "html", GetAttr(tree, WebAccessibility::ATTR_DOC_DOCTYPE).c_str()); | 103 "html", GetAttr(tree, WebAccessibility::ATTR_DOC_DOCTYPE).c_str()); |
| 100 EXPECT_STREQ( | 104 EXPECT_STREQ( |
| 101 "text/html", GetAttr(tree, WebAccessibility::ATTR_DOC_MIMETYPE).c_str()); | 105 "text/html", GetAttr(tree, WebAccessibility::ATTR_DOC_MIMETYPE).c_str()); |
| 102 EXPECT_STREQ("Accessibility Test", UTF16ToUTF8(tree.name).c_str()); | 106 EXPECT_STREQ("Accessibility Test", UTF16ToUTF8(tree.name).c_str()); |
| 103 EXPECT_EQ(WebAccessibility::ROLE_WEB_AREA, tree.role); | 107 EXPECT_EQ(WebAccessibility::ROLE_WEB_AREA, tree.role); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 132 EXPECT_STREQ( | 136 EXPECT_STREQ( |
| 133 "inline-block", | 137 "inline-block", |
| 134 GetAttr(checkbox, WebAccessibility::ATTR_DISPLAY).c_str()); | 138 GetAttr(checkbox, WebAccessibility::ATTR_DISPLAY).c_str()); |
| 135 ASSERT_EQ(1U, checkbox.html_attributes.size()); | 139 ASSERT_EQ(1U, checkbox.html_attributes.size()); |
| 136 EXPECT_STREQ( | 140 EXPECT_STREQ( |
| 137 "type", UTF16ToUTF8(checkbox.html_attributes[0].first).c_str()); | 141 "type", UTF16ToUTF8(checkbox.html_attributes[0].first).c_str()); |
| 138 EXPECT_STREQ( | 142 EXPECT_STREQ( |
| 139 "checkbox", UTF16ToUTF8(checkbox.html_attributes[0].second).c_str()); | 143 "checkbox", UTF16ToUTF8(checkbox.html_attributes[0].second).c_str()); |
| 140 } | 144 } |
| 141 | 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 |
| 142 } // namespace | 204 } // namespace |
| OLD | NEW |