Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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 #include "content/browser/accessibility/accessibility_tree_formatter.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/android/jni_android.h" | |
| 10 #include "base/android/jni_string.h" | |
| 11 #include "base/files/file_path.h" | |
| 12 #include "base/json/json_writer.h" | |
| 13 #include "base/string_util.h" | |
| 14 #include "base/stringprintf.h" | |
| 15 #include "base/utf_string_conversions.h" | |
| 16 #include "content/browser/accessibility/browser_accessibility_android.h" | |
| 17 #include "content/common/accessibility_node_data.h" | |
| 18 | |
| 19 using base::StringPrintf; | |
| 20 | |
| 21 namespace content { | |
| 22 | |
| 23 const char* BOOL_ATTRIBUTES[] = { | |
|
benm (inactive)
2013/06/10 14:24:24
should these be in an anonymous namespace?
| |
| 24 "checkable", | |
| 25 "checked", | |
| 26 "clickable", | |
| 27 "disabled", | |
| 28 "editable_text", | |
| 29 "focusable", | |
| 30 "focused", | |
| 31 "invisible", | |
| 32 "password", | |
| 33 "scrollable", | |
| 34 "selected" | |
| 35 }; | |
| 36 | |
| 37 const char* STRING_ATTRIBUTES[] = { | |
| 38 "name" | |
| 39 }; | |
| 40 | |
| 41 const char* INT_ATTRIBUTES[] = { | |
| 42 "item_index", | |
| 43 "item_count" | |
| 44 }; | |
| 45 | |
| 46 void AccessibilityTreeFormatter::Initialize() { | |
| 47 } | |
| 48 | |
| 49 void AccessibilityTreeFormatter::AddProperties( | |
| 50 const BrowserAccessibility& node, DictionaryValue* dict) { | |
| 51 const BrowserAccessibilityAndroid* android_node = | |
| 52 static_cast<const BrowserAccessibilityAndroid*>(&node); | |
| 53 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 54 | |
| 55 // Class name. | |
| 56 dict->SetString("class", base::android::ConvertJavaStringToUTF8( | |
| 57 android_node->GetClassNameJNI(env, NULL))); | |
| 58 | |
| 59 // Bool attributes. | |
| 60 dict->SetBoolean("focusable", | |
| 61 android_node->IsFocusableJNI(env, NULL)); | |
| 62 dict->SetBoolean("focused", | |
| 63 android_node->IsFocusedJNI(env, NULL)); | |
| 64 dict->SetBoolean("clickable", | |
| 65 android_node->GetClickableJNI(env, NULL)); | |
| 66 dict->SetBoolean("editable_text", | |
| 67 android_node->IsEditableTextJNI(env, NULL)); | |
| 68 dict->SetBoolean("checkable", | |
| 69 android_node->IsCheckableJNI(env, NULL)); | |
| 70 dict->SetBoolean("checked", | |
| 71 android_node->IsCheckedJNI(env, NULL)); | |
| 72 dict->SetBoolean("disabled", | |
| 73 !android_node->IsEnabledJNI(env, NULL)); | |
| 74 dict->SetBoolean("scrollable", | |
| 75 android_node->IsScrollableJNI(env, NULL)); | |
| 76 dict->SetBoolean("password", | |
| 77 android_node->IsPasswordJNI(env, NULL)); | |
| 78 dict->SetBoolean("selected", | |
| 79 android_node->IsSelectedJNI(env, NULL)); | |
| 80 dict->SetBoolean("invisible", | |
| 81 !android_node->IsVisibleJNI(env, NULL)); | |
| 82 | |
| 83 // String attributes. | |
| 84 dict->SetString("name", base::android::ConvertJavaStringToUTF8( | |
| 85 android_node->GetNameJNI(env, NULL))); | |
| 86 | |
| 87 // Int attributes. | |
| 88 dict->SetInteger("item_index", | |
| 89 android_node->GetItemIndexJNI(env, NULL)); | |
| 90 dict->SetInteger("item_count", | |
| 91 android_node->GetItemCountJNI(env, NULL)); | |
| 92 } | |
| 93 | |
| 94 bool AccessibilityTreeFormatter::IncludeChildren( | |
| 95 const BrowserAccessibility& node) { | |
| 96 const BrowserAccessibilityAndroid* android_node = | |
| 97 static_cast<const BrowserAccessibilityAndroid*>(&node); | |
| 98 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 99 | |
| 100 return 0 != android_node->GetChildCountJNI(env, NULL); | |
| 101 } | |
| 102 | |
| 103 string16 AccessibilityTreeFormatter::ToString(const DictionaryValue& dict, | |
| 104 const string16& indent) { | |
| 105 string16 line; | |
| 106 | |
| 107 string16 class_value; | |
| 108 dict.GetString("class", &class_value); | |
| 109 WriteAttribute(true, UTF16ToUTF8(class_value), &line); | |
| 110 | |
| 111 for (unsigned i = 0; i < arraysize(BOOL_ATTRIBUTES); i++) { | |
| 112 const char* attribute_name = BOOL_ATTRIBUTES[i]; | |
| 113 bool value; | |
| 114 if (dict.GetBoolean(attribute_name, &value) && value) | |
| 115 WriteAttribute(true, attribute_name, &line); | |
| 116 } | |
| 117 | |
| 118 for (unsigned i = 0; i < arraysize(STRING_ATTRIBUTES); i++) { | |
| 119 const char* attribute_name = STRING_ATTRIBUTES[i]; | |
| 120 std::string value; | |
| 121 if (!dict.GetString(attribute_name, &value) || value.empty()) | |
| 122 continue; | |
| 123 WriteAttribute(true, | |
| 124 StringPrintf("%s='%s'", attribute_name, value.c_str()), | |
| 125 &line); | |
| 126 } | |
| 127 | |
| 128 for (unsigned i = 0; i < arraysize(INT_ATTRIBUTES); i++) { | |
| 129 const char* attribute_name = INT_ATTRIBUTES[i]; | |
| 130 int value; | |
| 131 if (!dict.GetInteger(attribute_name, &value) || value == 0) | |
| 132 continue; | |
| 133 WriteAttribute(true, | |
| 134 StringPrintf("%s=%d", attribute_name, value), | |
| 135 &line); | |
| 136 } | |
| 137 | |
| 138 return indent + line + ASCIIToUTF16("\n"); | |
| 139 } | |
| 140 | |
| 141 // static | |
| 142 const base::FilePath::StringType | |
| 143 AccessibilityTreeFormatter::GetActualFileSuffix() { | |
| 144 return FILE_PATH_LITERAL("-actual-android.txt"); | |
| 145 } | |
| 146 | |
| 147 // static | |
| 148 const base::FilePath::StringType | |
| 149 AccessibilityTreeFormatter::GetExpectedFileSuffix() { | |
| 150 return FILE_PATH_LITERAL("-expected-android.txt"); | |
| 151 } | |
| 152 | |
| 153 // static | |
| 154 const std::string AccessibilityTreeFormatter::GetAllowEmptyString() { | |
| 155 return "@ANDROID-ALLOW-EMPTY:"; | |
| 156 } | |
| 157 | |
| 158 // static | |
| 159 const std::string AccessibilityTreeFormatter::GetAllowString() { | |
| 160 return "@ANDROID-ALLOW:"; | |
| 161 } | |
| 162 | |
| 163 // static | |
| 164 const std::string AccessibilityTreeFormatter::GetDenyString() { | |
| 165 return "@ANDROID-DENY:"; | |
| 166 } | |
| 167 | |
| 168 } // namespace content | |
| OLD | NEW |