1 // Copyright 2019 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 #ifndef CONTENT_PUBLIC_TEST_DUMP_ACCESSIBILITY_TEST_HELPER_H_
6 #define CONTENT_PUBLIC_TEST_DUMP_ACCESSIBILITY_TEST_HELPER_H_
7 
8 #include "base/files/file_path.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/optional.h"
11 
12 namespace base {
13 class FilePath;
14 }
15 
16 namespace ui {
17 struct AXNodeFilter;
18 struct AXPropertyFilter;
19 }  // namespace ui
20 
21 namespace content {
22 
23 // A helper class for writing accessibility tree dump tests.
24 class DumpAccessibilityTestHelper {
25  public:
26   explicit DumpAccessibilityTestHelper(const char* expectation_type);
27   ~DumpAccessibilityTestHelper() = default;
28 
29   // Returns a path to an expectation file for the current platform. If no
30   // suitable expectation file can be found, logs an error message and returns
31   // an empty path.
32   base::FilePath GetExpectationFilePath(const base::FilePath& test_file_path);
33 
34   // Parses property filter directive, if the line is a valid property filter
35   // directive, then a new property filter is created and appneded to the list,
36   // true is returned, otherwise false.
37   bool ParsePropertyFilter(const std::string& line,
38                            std::vector<ui::AXPropertyFilter>* filters) const;
39 
40   // Parses node filter directive, if the line is a valid node filter directive
41   // then a new node filter is created and appneded to the list, true is
42   // returned, otherwise false.
43   bool ParseNodeFilter(const std::string& line,
44                        std::vector<ui::AXNodeFilter>* filters) const;
45 
46   struct Directive {
47     enum Type {
48       // No directive.
49       kNone,
50 
51       // Instructs to not wait for document load for url defined by the
52       // directive.
53       kNoLoadExpected,
54 
55       // Delays a test unitl a string defined by the directive is present
56       // in the dump.
57       kWaitFor,
58 
59       // Delays a test until a string returned by a script defined by the
60       // directive is present in the dump.
61       kExecuteAndWaitFor,
62 
63       // Indicates event recording should continue at least until a specific
64       // event has been received.
65       kRunUntil,
66 
67       // Invokes default action on an accessible object defined by the
68       // directive.
69       kDefaultActionOn,
70     } type;
71 
72     std::string value;
73   };
74 
75   // Parses directives from the given line.
76   Directive ParseDirective(const std::string& line) const;
77 
78   // Loads the given expectation file and returns the contents. An expectation
79   // file may be empty, in which case an empty vector is returned.
80   // Returns nullopt if the file contains a skip marker.
81   static base::Optional<std::vector<std::string>> LoadExpectationFile(
82       const base::FilePath& expected_file);
83 
84   // Compares the given actual dump against the given expectation and generates
85   // a new expectation file if switches::kGenerateAccessibilityTestExpectations
86   // has been set. Returns true if the result matches the expectation.
87   static bool ValidateAgainstExpectation(
88       const base::FilePath& test_file_path,
89       const base::FilePath& expected_file,
90       const std::vector<std::string>& actual_lines,
91       const std::vector<std::string>& expected_lines);
92 
93  private:
94   // Suffix of the expectation file corresponding to html file.
95   // Overridden by each platform subclass.
96   // Example:
97   // HTML test:      test-file.html
98   // Expected:       test-file-expected-mac.txt.
99   base::FilePath::StringType GetExpectedFileSuffix() const;
100 
101   // Some Platforms expect different outputs depending on the version.
102   // Most test outputs are identical but this allows a version specific
103   // expected file to be used.
104   base::FilePath::StringType GetVersionSpecificExpectedFileSuffix() const;
105 
106   FRIEND_TEST_ALL_PREFIXES(DumpAccessibilityTestHelperTest, TestDiffLines);
107 
108   // Utility helper that does a comment-aware equality check.
109   // Returns array of lines from expected file which are different.
110   static std::vector<int> DiffLines(
111       const std::vector<std::string>& expected_lines,
112       const std::vector<std::string>& actual_lines);
113 
114   std::string expectation_type_;
115 };
116 
117 }  // namespace content
118 
119 #endif  // CONTENT_PUBLIC_TEST_DUMP_ACCESSIBILITY_TEST_HELPER_H_
120