dump_accessibility_tree_browsertest.cc revision 558790d6acca3451cf3a6b497803a5f07d0bec58
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.
4
5#include <set>
6#include <string>
7#include <vector>
8
9#include "base/file_util.h"
10#include "base/logging.h"
11#include "base/path_service.h"
12#include "base/strings/string16.h"
13#include "base/strings/string_split.h"
14#include "base/strings/string_util.h"
15#include "base/strings/utf_string_conversions.h"
16#include "content/browser/accessibility/accessibility_tree_formatter.h"
17#include "content/browser/accessibility/browser_accessibility.h"
18#include "content/browser/accessibility/browser_accessibility_manager.h"
19#include "content/browser/renderer_host/render_view_host_impl.h"
20#include "content/port/browser/render_widget_host_view_port.h"
21#include "content/public/browser/web_contents.h"
22#include "content/public/common/content_paths.h"
23#include "content/shell/shell.h"
24#include "content/test/accessibility_browser_test_utils.h"
25#include "content/test/content_browser_test.h"
26#include "content/test/content_browser_test_utils.h"
27#include "testing/gtest/include/gtest/gtest.h"
28
29// TODO(dmazzoni): Disabled accessibility tests on Win64. crbug.com/179717
30#if defined(OS_WIN) && defined(ARCH_CPU_X86_64)
31#define MAYBE(x) DISABLED_##x
32#else
33#define MAYBE(x) x
34#endif
35
36namespace content {
37
38namespace {
39
40const char kCommentToken = '#';
41const char kMarkSkipFile[] = "#<skip";
42const char kMarkEndOfFile[] = "<-- End-of-file -->";
43const char kSignalDiff[] = "*";
44
45}  // namespace
46
47typedef AccessibilityTreeFormatter::Filter Filter;
48
49// This test takes a snapshot of the platform BrowserAccessibility tree and
50// tests it against an expected baseline.
51//
52// The flow of the test is as outlined below.
53// 1. Load an html file from chrome/test/data/accessibility.
54// 2. Read the expectation.
55// 3. Browse to the page and serialize the platform specific tree into a human
56//    readable string.
57// 4. Perform a comparison between actual and expected and fail if they do not
58//    exactly match.
59class DumpAccessibilityTreeTest : public ContentBrowserTest {
60 public:
61  // Utility helper that does a comment aware equality check.
62  // Returns array of lines from expected file which are different.
63  std::vector<int> DiffLines(std::vector<std::string>& expected_lines,
64                             std::vector<std::string>& actual_lines) {
65    int actual_lines_count = actual_lines.size();
66    int expected_lines_count = expected_lines.size();
67    std::vector<int> diff_lines;
68    int i = 0, j = 0;
69    while (i < actual_lines_count && j < expected_lines_count) {
70      if (expected_lines[j].size() == 0 ||
71          expected_lines[j][0] == kCommentToken) {
72        // Skip comment lines and blank lines in expected output.
73        ++j;
74        continue;
75      }
76
77      if (actual_lines[i] != expected_lines[j])
78        diff_lines.push_back(j);
79      ++i;
80      ++j;
81    }
82
83    // Actual file has been fully checked.
84    return diff_lines;
85  }
86
87  void AddDefaultFilters(std::vector<Filter>* filters) {
88    filters->push_back(Filter(ASCIIToUTF16("FOCUSABLE"), Filter::ALLOW));
89    filters->push_back(Filter(ASCIIToUTF16("READONLY"), Filter::ALLOW));
90    filters->push_back(Filter(ASCIIToUTF16("*=''"), Filter::DENY));
91  }
92
93  void ParseFilters(const std::string& test_html,
94                    std::vector<Filter>* filters) {
95    std::vector<std::string> lines;
96    base::SplitString(test_html, '\n', &lines);
97    for (std::vector<std::string>::const_iterator iter = lines.begin();
98         iter != lines.end();
99         ++iter) {
100      const std::string& line = *iter;
101      const std::string& allow_empty_str =
102          AccessibilityTreeFormatter::GetAllowEmptyString();
103      const std::string& allow_str =
104          AccessibilityTreeFormatter::GetAllowString();
105      const std::string& deny_str =
106          AccessibilityTreeFormatter::GetDenyString();
107      if (StartsWithASCII(line, allow_empty_str, true)) {
108        filters->push_back(
109          Filter(UTF8ToUTF16(line.substr(allow_empty_str.size())),
110                 Filter::ALLOW_EMPTY));
111      } else if (StartsWithASCII(line, allow_str, true)) {
112        filters->push_back(Filter(UTF8ToUTF16(line.substr(allow_str.size())),
113                                  Filter::ALLOW));
114      } else if (StartsWithASCII(line, deny_str, true)) {
115        filters->push_back(Filter(UTF8ToUTF16(line.substr(deny_str.size())),
116                                  Filter::DENY));
117      }
118    }
119  }
120
121  void RunTest(const base::FilePath::CharType* file_path);
122};
123
124void DumpAccessibilityTreeTest::RunTest(
125    const base::FilePath::CharType* file_path) {
126  NavigateToURL(shell(), GURL("about:blank"));
127
128  // Setup test paths.
129  base::FilePath dir_test_data;
130  ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &dir_test_data));
131  base::FilePath test_path(
132      dir_test_data.Append(FILE_PATH_LITERAL("accessibility")));
133  ASSERT_TRUE(base::PathExists(test_path))
134      << test_path.LossyDisplayName();
135
136  base::FilePath html_file = test_path.Append(base::FilePath(file_path));
137  // Output the test path to help anyone who encounters a failure and needs
138  // to know where to look.
139  printf("Testing: %s\n", html_file.MaybeAsASCII().c_str());
140
141  std::string html_contents;
142  file_util::ReadFileToString(html_file, &html_contents);
143
144  // Read the expected file.
145  std::string expected_contents_raw;
146  base::FilePath expected_file =
147    base::FilePath(html_file.RemoveExtension().value() +
148                   AccessibilityTreeFormatter::GetExpectedFileSuffix());
149  file_util::ReadFileToString(expected_file, &expected_contents_raw);
150
151  // Tolerate Windows-style line endings (\r\n) in the expected file:
152  // normalize by deleting all \r from the file (if any) to leave only \n.
153  std::string expected_contents;
154  RemoveChars(expected_contents_raw, "\r", &expected_contents);
155
156  if (!expected_contents.compare(0, strlen(kMarkSkipFile), kMarkSkipFile)) {
157    printf("Skipping this test on this platform.\n");
158    return;
159  }
160
161  // Load the page.
162  string16 html_contents16;
163  html_contents16 = UTF8ToUTF16(html_contents);
164  GURL url = GetTestUrl("accessibility",
165                        html_file.BaseName().MaybeAsASCII().c_str());
166  AccessibilityNotificationWaiter waiter(
167      shell(), AccessibilityModeComplete,
168      AccessibilityNotificationLoadComplete);
169  NavigateToURL(shell(), url);
170  waiter.WaitForNotification();
171
172  RenderWidgetHostViewPort* host_view = RenderWidgetHostViewPort::FromRWHV(
173      shell()->web_contents()->GetRenderWidgetHostView());
174  AccessibilityTreeFormatter formatter(
175      host_view->GetBrowserAccessibilityManager()->GetRoot());
176
177  // Parse filters in the test file.
178  std::vector<Filter> filters;
179  AddDefaultFilters(&filters);
180  ParseFilters(html_contents, &filters);
181  formatter.SetFilters(filters);
182
183  // Perform a diff (or write the initial baseline).
184  string16 actual_contents_utf16;
185  formatter.FormatAccessibilityTree(&actual_contents_utf16);
186  std::string actual_contents = UTF16ToUTF8(actual_contents_utf16);
187  std::vector<std::string> actual_lines, expected_lines;
188  Tokenize(actual_contents, "\n", &actual_lines);
189  Tokenize(expected_contents, "\n", &expected_lines);
190  // Marking the end of the file with a line of text ensures that
191  // file length differences are found.
192  expected_lines.push_back(kMarkEndOfFile);
193  actual_lines.push_back(kMarkEndOfFile);
194
195  std::vector<int> diff_lines = DiffLines(expected_lines, actual_lines);
196  bool is_different = diff_lines.size() > 0;
197  EXPECT_FALSE(is_different);
198  if (is_different) {
199    // Mark the expected lines which did not match actual output with a *.
200    printf("* Line Expected\n");
201    printf("- ---- --------\n");
202    for (int line = 0, diff_index = 0;
203         line < static_cast<int>(expected_lines.size());
204         ++line) {
205      bool is_diff = false;
206      if (diff_index < static_cast<int>(diff_lines.size()) &&
207          diff_lines[diff_index] == line) {
208        is_diff = true;
209        ++ diff_index;
210      }
211      printf("%1s %4d %s\n", is_diff? kSignalDiff : "", line + 1,
212             expected_lines[line].c_str());
213    }
214    printf("\nActual\n");
215    printf("------\n");
216    printf("%s\n", actual_contents.c_str());
217  }
218
219  if (!base::PathExists(expected_file)) {
220    base::FilePath actual_file =
221        base::FilePath(html_file.RemoveExtension().value() +
222                       AccessibilityTreeFormatter::GetActualFileSuffix());
223
224    EXPECT_TRUE(file_util::WriteFile(
225        actual_file, actual_contents.c_str(), actual_contents.size()));
226
227    ADD_FAILURE() << "No expectation found. Create it by doing:\n"
228                  << "mv " << actual_file.LossyDisplayName() << " "
229                  << expected_file.LossyDisplayName();
230  }
231}
232
233IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityA) {
234  RunTest(FILE_PATH_LITERAL("a.html"));
235}
236
237IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityAddress) {
238  RunTest(FILE_PATH_LITERAL("address.html"));
239}
240
241IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityAName) {
242  RunTest(FILE_PATH_LITERAL("a-name.html"));
243}
244
245IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityAOnclick) {
246  RunTest(FILE_PATH_LITERAL("a-onclick.html"));
247}
248
249IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest,
250                       AccessibilityAriaApplication) {
251  RunTest(FILE_PATH_LITERAL("aria-application.html"));
252}
253
254IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest,
255                       AccessibilityAriaAutocomplete) {
256  RunTest(FILE_PATH_LITERAL("aria-autocomplete.html"));
257}
258
259IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityAriaCombobox) {
260  RunTest(FILE_PATH_LITERAL("aria-combobox.html"));
261}
262
263IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityAriaInvalid) {
264  RunTest(FILE_PATH_LITERAL("aria-invalid.html"));
265}
266
267IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityAriaLevel) {
268  RunTest(FILE_PATH_LITERAL("aria-level.html"));
269}
270
271IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityAriaMenu) {
272  RunTest(FILE_PATH_LITERAL("aria-menu.html"));
273}
274
275IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest,
276                       AccessibilityAriaMenuitemradio) {
277  RunTest(FILE_PATH_LITERAL("aria-menuitemradio.html"));
278}
279
280IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest,
281                       AccessibilityAriaPressed) {
282  RunTest(FILE_PATH_LITERAL("aria-pressed.html"));
283}
284
285IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest,
286                       AccessibilityAriaProgressbar) {
287  RunTest(FILE_PATH_LITERAL("aria-progressbar.html"));
288}
289
290IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest,
291                       AccessibilityAriaToolbar) {
292  RunTest(FILE_PATH_LITERAL("toolbar.html"));
293}
294
295IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest,
296                       AccessibilityAriaValueMin) {
297  RunTest(FILE_PATH_LITERAL("aria-valuemin.html"));
298}
299
300IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest,
301                       AccessibilityAriaValueMax) {
302  RunTest(FILE_PATH_LITERAL("aria-valuemax.html"));
303}
304
305IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityArticle) {
306  RunTest(FILE_PATH_LITERAL("article.html"));
307}
308
309IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityAWithImg) {
310  RunTest(FILE_PATH_LITERAL("a-with-img.html"));
311}
312
313IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityBdo) {
314  RunTest(FILE_PATH_LITERAL("bdo.html"));
315}
316
317IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityBR) {
318  RunTest(FILE_PATH_LITERAL("br.html"));
319}
320
321IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityButtonNameCalc) {
322  RunTest(FILE_PATH_LITERAL("button-name-calc.html"));
323}
324
325IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityCanvas) {
326  RunTest(FILE_PATH_LITERAL("canvas.html"));
327}
328
329IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest,
330                       AccessibilityCheckboxNameCalc) {
331  RunTest(FILE_PATH_LITERAL("checkbox-name-calc.html"));
332}
333
334IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityDiv) {
335  RunTest(FILE_PATH_LITERAL("div.html"));
336}
337
338IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityDl) {
339  RunTest(FILE_PATH_LITERAL("dl.html"));
340}
341
342IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest,
343                       AccessibilityContenteditableDescendants) {
344  RunTest(FILE_PATH_LITERAL("contenteditable-descendants.html"));
345}
346
347IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityEm) {
348  RunTest(FILE_PATH_LITERAL("em.html"));
349}
350
351IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityFooter) {
352  RunTest(FILE_PATH_LITERAL("footer.html"));
353}
354
355IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityForm) {
356  RunTest(FILE_PATH_LITERAL("form.html"));
357}
358
359IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityHeading) {
360  RunTest(FILE_PATH_LITERAL("heading.html"));
361}
362
363IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityHR) {
364  RunTest(FILE_PATH_LITERAL("hr.html"));
365}
366
367// crbug.com/179717 and crbug.com/224659
368IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest,
369                       DISABLED_AccessibilityIframeCoordinates) {
370  RunTest(FILE_PATH_LITERAL("iframe-coordinates.html"));
371}
372
373IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityInputButton) {
374  RunTest(FILE_PATH_LITERAL("input-button.html"));
375}
376
377IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest,
378                       AccessibilityInputButtonInMenu) {
379  RunTest(FILE_PATH_LITERAL("input-button-in-menu.html"));
380}
381
382IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityInputColor) {
383  RunTest(FILE_PATH_LITERAL("input-color.html"));
384}
385
386IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest,
387                       AccessibilityInputImageButtonInMenu) {
388  RunTest(FILE_PATH_LITERAL("input-image-button-in-menu.html"));
389}
390
391IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityInputRange) {
392  RunTest(FILE_PATH_LITERAL("input-range.html"));
393}
394
395IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest,
396                       AccessibilityInputTextNameCalc) {
397  RunTest(FILE_PATH_LITERAL("input-text-name-calc.html"));
398}
399
400IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityLabel) {
401  RunTest(FILE_PATH_LITERAL("label.html"));
402}
403
404IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityListMarkers) {
405  RunTest(FILE_PATH_LITERAL("list-markers.html"));
406}
407
408IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityP) {
409  RunTest(FILE_PATH_LITERAL("p.html"));
410}
411
412IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilitySelect) {
413  RunTest(FILE_PATH_LITERAL("select.html"));
414}
415
416IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilitySpan) {
417  RunTest(FILE_PATH_LITERAL("span.html"));
418}
419
420IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilitySpinButton) {
421  RunTest(FILE_PATH_LITERAL("spinbutton.html"));
422}
423
424// TODO(dmazzoni): Rebaseline this test after Blink rolls past r155083.
425// See http://crbug.com/265619
426IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, DISABLED_AccessibilitySvg) {
427  RunTest(FILE_PATH_LITERAL("svg.html"));
428}
429
430IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityTab) {
431  RunTest(FILE_PATH_LITERAL("tab.html"));
432}
433
434IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityTableSimple) {
435  RunTest(FILE_PATH_LITERAL("table-simple.html"));
436}
437
438IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityTableSpans) {
439  RunTest(FILE_PATH_LITERAL("table-spans.html"));
440}
441
442IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest,
443                       AccessibilityToggleButton) {
444  RunTest(FILE_PATH_LITERAL("togglebutton.html"));
445}
446
447IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityUl) {
448  RunTest(FILE_PATH_LITERAL("ul.html"));
449}
450
451IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityWbr) {
452  RunTest(FILE_PATH_LITERAL("wbr.html"));
453}
454
455}  // namespace content
456