OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 /* |
| 6 * Copyright (C) 2010 Google Inc. All rights reserved. |
| 7 * |
| 8 * Redistribution and use in source and binary forms, with or without |
| 9 * modification, are permitted provided that the following conditions are |
| 10 * met: |
| 11 * |
| 12 * * Redistributions of source code must retain the above copyright |
| 13 * notice, this list of conditions and the following disclaimer. |
| 14 * * Redistributions in binary form must reproduce the above |
| 15 * copyright notice, this list of conditions and the following disclaimer |
| 16 * in the documentation and/or other materials provided with the |
| 17 * distribution. |
| 18 * * Neither the name of Google Inc. nor the names of its |
| 19 * contributors may be used to endorse or promote products derived from |
| 20 * this software without specific prior written permission. |
| 21 * |
| 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 */ |
| 34 |
| 35 #include "content/test/layout_tests/runner/AccessibilityController.h" |
| 36 |
| 37 #include "content/public/test/layout_tests/WebTestDelegate.h" |
| 38 #include "third_party/WebKit/public/platform/WebCString.h" |
| 39 #include "third_party/WebKit/public/web/WebAXObject.h" |
| 40 #include "third_party/WebKit/public/web/WebElement.h" |
| 41 #include "third_party/WebKit/public/web/WebFrame.h" |
| 42 #include "third_party/WebKit/public/web/WebNode.h" |
| 43 #include "third_party/WebKit/public/web/WebView.h" |
| 44 |
| 45 using namespace blink; |
| 46 |
| 47 namespace WebTestRunner { |
| 48 |
| 49 AccessibilityController::AccessibilityController() |
| 50 : m_logAccessibilityEvents(false) |
| 51 { |
| 52 |
| 53 bindMethod("logAccessibilityEvents", &AccessibilityController::logAccessibil
ityEventsCallback); |
| 54 bindMethod("addNotificationListener", &AccessibilityController::addNotificat
ionListenerCallback); |
| 55 bindMethod("removeNotificationListener", &AccessibilityController::removeNot
ificationListenerCallback); |
| 56 |
| 57 bindProperty("focusedElement", &AccessibilityController::focusedElementGette
rCallback); |
| 58 bindProperty("rootElement", &AccessibilityController::rootElementGetterCallb
ack); |
| 59 |
| 60 bindMethod("accessibleElementById", &AccessibilityController::accessibleElem
entByIdGetterCallback); |
| 61 |
| 62 bindFallbackMethod(&AccessibilityController::fallbackCallback); |
| 63 } |
| 64 |
| 65 void AccessibilityController::bindToJavascript(WebFrame* frame, const WebString&
classname) |
| 66 { |
| 67 WebAXObject::enableAccessibility(); |
| 68 WebAXObject::enableInlineTextBoxAccessibility(); |
| 69 CppBoundClass::bindToJavascript(frame, classname); |
| 70 } |
| 71 |
| 72 void AccessibilityController::reset() |
| 73 { |
| 74 m_rootElement = WebAXObject(); |
| 75 m_focusedElement = WebAXObject(); |
| 76 m_elements.clear(); |
| 77 m_notificationCallbacks.clear(); |
| 78 |
| 79 m_logAccessibilityEvents = false; |
| 80 } |
| 81 |
| 82 void AccessibilityController::setFocusedElement(const WebAXObject& focusedElemen
t) |
| 83 { |
| 84 m_focusedElement = focusedElement; |
| 85 } |
| 86 |
| 87 WebAXObjectProxy* AccessibilityController::getFocusedElement() |
| 88 { |
| 89 if (m_focusedElement.isNull()) |
| 90 m_focusedElement = m_webView->accessibilityObject(); |
| 91 return m_elements.getOrCreate(m_focusedElement); |
| 92 } |
| 93 |
| 94 WebAXObjectProxy* AccessibilityController::getRootElement() |
| 95 { |
| 96 if (m_rootElement.isNull()) |
| 97 m_rootElement = m_webView->accessibilityObject(); |
| 98 return m_elements.createRoot(m_rootElement); |
| 99 } |
| 100 |
| 101 WebAXObjectProxy* AccessibilityController::findAccessibleElementByIdRecursive(co
nst WebAXObject& obj, const WebString& id) |
| 102 { |
| 103 if (obj.isNull() || obj.isDetached()) |
| 104 return 0; |
| 105 |
| 106 WebNode node = obj.node(); |
| 107 if (!node.isNull() && node.isElementNode()) { |
| 108 WebElement element = node.to<WebElement>(); |
| 109 element.getAttribute("id"); |
| 110 if (element.getAttribute("id") == id) |
| 111 return m_elements.getOrCreate(obj); |
| 112 } |
| 113 |
| 114 unsigned childCount = obj.childCount(); |
| 115 for (unsigned i = 0; i < childCount; i++) { |
| 116 if (WebAXObjectProxy* result = findAccessibleElementByIdRecursive(obj.ch
ildAt(i), id)) |
| 117 return result; |
| 118 } |
| 119 |
| 120 return 0; |
| 121 } |
| 122 |
| 123 WebAXObjectProxy* AccessibilityController::getAccessibleElementById(const std::s
tring& id) |
| 124 { |
| 125 if (m_rootElement.isNull()) |
| 126 m_rootElement = m_webView->accessibilityObject(); |
| 127 |
| 128 if (!m_rootElement.updateBackingStoreAndCheckValidity()) |
| 129 return 0; |
| 130 |
| 131 return findAccessibleElementByIdRecursive(m_rootElement, WebString::fromUTF8
(id.c_str())); |
| 132 } |
| 133 |
| 134 bool AccessibilityController::shouldLogAccessibilityEvents() |
| 135 { |
| 136 return m_logAccessibilityEvents; |
| 137 } |
| 138 |
| 139 void AccessibilityController::notificationReceived(const blink::WebAXObject& tar
get, const char* notificationName) |
| 140 { |
| 141 // Call notification listeners on the element. |
| 142 WebAXObjectProxy* element = m_elements.getOrCreate(target); |
| 143 element->notificationReceived(notificationName); |
| 144 |
| 145 // Call global notification listeners. |
| 146 size_t callbackCount = m_notificationCallbacks.size(); |
| 147 for (size_t i = 0; i < callbackCount; i++) { |
| 148 CppVariant arguments[2]; |
| 149 arguments[0].set(*element->getAsCppVariant()); |
| 150 arguments[1].set(notificationName); |
| 151 CppVariant invokeResult; |
| 152 m_notificationCallbacks[i].invokeDefault(arguments, 2, invokeResult); |
| 153 } |
| 154 } |
| 155 |
| 156 void AccessibilityController::logAccessibilityEventsCallback(const CppArgumentLi
st&, CppVariant* result) |
| 157 { |
| 158 m_logAccessibilityEvents = true; |
| 159 result->setNull(); |
| 160 } |
| 161 |
| 162 void AccessibilityController::addNotificationListenerCallback(const CppArgumentL
ist& arguments, CppVariant* result) |
| 163 { |
| 164 if (arguments.size() < 1 || !arguments[0].isObject()) { |
| 165 result->setNull(); |
| 166 return; |
| 167 } |
| 168 |
| 169 m_notificationCallbacks.push_back(arguments[0]); |
| 170 result->setNull(); |
| 171 } |
| 172 |
| 173 void AccessibilityController::removeNotificationListenerCallback(const CppArgume
ntList&, CppVariant* result) |
| 174 { |
| 175 // FIXME: Implement this. |
| 176 result->setNull(); |
| 177 } |
| 178 |
| 179 void AccessibilityController::focusedElementGetterCallback(CppVariant* result) |
| 180 { |
| 181 result->set(*(getFocusedElement()->getAsCppVariant())); |
| 182 } |
| 183 |
| 184 void AccessibilityController::rootElementGetterCallback(CppVariant* result) |
| 185 { |
| 186 result->set(*(getRootElement()->getAsCppVariant())); |
| 187 } |
| 188 |
| 189 void AccessibilityController::accessibleElementByIdGetterCallback(const CppArgum
entList& arguments, CppVariant* result) |
| 190 { |
| 191 result->setNull(); |
| 192 |
| 193 if (arguments.size() < 1 || !arguments[0].isString()) |
| 194 return; |
| 195 |
| 196 std::string id = arguments[0].toString(); |
| 197 WebAXObjectProxy* foundElement = getAccessibleElementById(id); |
| 198 if (!foundElement) |
| 199 return; |
| 200 |
| 201 result->set(*(foundElement->getAsCppVariant())); |
| 202 } |
| 203 |
| 204 void AccessibilityController::fallbackCallback(const CppArgumentList&, CppVarian
t* result) |
| 205 { |
| 206 m_delegate->printMessage("CONSOLE MESSAGE: JavaScript ERROR: unknown method
called on AccessibilityController\n"); |
| 207 result->setNull(); |
| 208 } |
| 209 |
| 210 } |
OLD | NEW |