1 // Copyright (c) 2012 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.
5 #ifndef CONTENT_BROWSER_ACCESSIBILITY_ACCESSIBILITY_TREE_FORMATTER_H_
6 #define CONTENT_BROWSER_ACCESSIBILITY_ACCESSIBILITY_TREE_FORMATTER_H_
10 #include "base/files/file_path.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "content/browser/accessibility/browser_accessibility.h"
15 #include "content/common/content_export.h"
21 // A utility class for formatting platform-specific accessibility information,
22 // for use in testing, debugging, and developer tools.
23 // This is extended by a subclass for each platform where accessibility is
25 class CONTENT_EXPORT AccessibilityTreeFormatter
{
27 explicit AccessibilityTreeFormatter(BrowserAccessibility
* root
);
28 virtual ~AccessibilityTreeFormatter();
30 // A single filter specification. See GetAllowString() and GetDenyString()
31 // for more information.
38 base::string16 match_str
;
41 Filter(base::string16 match_str
, Type type
)
42 : match_str(match_str
), type(type
) {}
45 static AccessibilityTreeFormatter
* Create(WebContents
* wc
);
47 static bool MatchesFilters(
48 const std::vector
<Filter
>& filters
,
49 const base::string16
& text
,
52 // Populates the given DictionaryValue with the accessibility tree.
53 // The dictionary contains a key/value pair for each attribute of the node,
54 // plus a "children" attribute containing a list of all child nodes.
56 // "AXName": "node", /* actual attributes will vary by platform */
57 // "position": { /* some attributes may be dictionaries */
61 // /* ... more attributes of |node| */
62 // "children": [ { /* list of children created recursively */
63 // "AXName": "child node 1",
64 // /* ... more attributes */
67 // "AXName": "child name 2",
68 // /* ... more attributes */
72 scoped_ptr
<base::DictionaryValue
> BuildAccessibilityTree();
74 // Dumps a BrowserAccessibility tree into a string.
75 void FormatAccessibilityTree(base::string16
* contents
);
77 // Set regular expression filters that apply to each component of every
78 // line before it's output.
79 void SetFilters(const std::vector
<Filter
>& filters
);
81 // If true, the internal accessibility id of each node will be included
83 void set_show_ids(bool show_ids
) { show_ids_
= show_ids
; }
85 // Suffix of the expectation file corresponding to html file.
87 // HTML test: test-file.html
88 // Expected: test-file-expected-mac.txt.
89 // Auto-generated: test-file-actual-mac.txt
90 static const base::FilePath::StringType
GetActualFileSuffix();
91 static const base::FilePath::StringType
GetExpectedFileSuffix();
93 // A platform-specific string that indicates a given line in a file
94 // is an allow-empty, allow or deny filter. Example:
96 // GetAllowEmptyString() -> "@MAC-ALLOW-EMPTY:"
97 // GetAllowString() -> "@MAC-ALLOW:"
98 // GetDenyString() -> "@MAC-DENY:"
101 // @MAC-ALLOW-EMPTY:description*
102 // @MAC-ALLOW:roleDescription*
103 // @MAC-DENY:subrole*
106 static const std::string
GetAllowEmptyString();
107 static const std::string
GetAllowString();
108 static const std::string
GetDenyString();
111 void RecursiveFormatAccessibilityTree(const BrowserAccessibility
& node
,
112 base::string16
* contents
,
114 void RecursiveBuildAccessibilityTree(const BrowserAccessibility
& node
,
115 base::DictionaryValue
* tree_node
);
116 void RecursiveFormatAccessibilityTree(const base::DictionaryValue
& tree_node
,
117 base::string16
* contents
,
120 // Overridden by each platform to add the required attributes for each node
121 // into the given dict.
122 void AddProperties(const BrowserAccessibility
& node
,
123 base::DictionaryValue
* dict
);
125 base::string16
FormatCoordinates(const char* name
,
128 const base::DictionaryValue
& value
);
130 // Returns a platform specific representation of a BrowserAccessibility.
131 base::string16
ToString(const base::DictionaryValue
& node
);
135 bool MatchesFilters(const base::string16
& text
, bool default_result
) const;
137 // Writes the given attribute string out to |line| if it matches the filters.
138 void WriteAttribute(bool include_by_default
,
139 const base::string16
& attr
,
140 base::string16
* line
);
141 void WriteAttribute(bool include_by_default
,
142 const std::string
& attr
,
143 base::string16
* line
);
145 BrowserAccessibility
* root_
;
147 // Filters used when formatting the accessibility tree as text.
148 std::vector
<Filter
> filters_
;
150 // Whether or not node ids should be included in the dump.
153 DISALLOW_COPY_AND_ASSIGN(AccessibilityTreeFormatter
);
156 } // namespace content
158 #endif // CONTENT_BROWSER_ACCESSIBILITY_ACCESSIBILITY_TREE_FORMATTER_H_