Commit c7d0001d authored by Dominic Mazzoni's avatar Dominic Mazzoni Committed by Commit Bot

Fix accessibility dump tree tests to not need an end-of-file sentinel.

The DumpAccessibilityTree tests generate an output text file and diff
the results against an expected file. Previously, the way that the
algorithm handled files of different lengths was by adding an
end-of-file sentinel to the end. This resulted in that sentinel
getting checked in, which was just confusing.

Fix this by modifying the DiffLines helper function to properly compare
files that don't have the same number of lines. Add full unit tests for
DiffLines so we can be confident it works.

Continue to LOG the end-of-file sentinel, because it's needed by
running rebase_dump_accessibility_tree_test.py to parse actual output
files from remote logs.

Bug: none
Change-Id: If1bcef063822fd5786acaeff9133c05d5fc5b065
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2109458Reviewed-by: default avatarAaron Leventhal <aleventhal@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Commit-Queue: Avi Drissman <avi@chromium.org>
Auto-Submit: Dominic Mazzoni <dmazzoni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#751791}
parent 884a90ea
...@@ -367,7 +367,6 @@ void DumpAccessibilityTestBase::RunTestForPlatform( ...@@ -367,7 +367,6 @@ void DumpAccessibilityTestBase::RunTestForPlatform(
} }
// Validate against the expectation file. // Validate against the expectation file.
actual_lines.push_back(kMarkEndOfFile);
bool matches_expectation = test_helper.ValidateAgainstExpectation( bool matches_expectation = test_helper.ValidateAgainstExpectation(
file_path, expected_file, actual_lines, *expected_lines); file_path, expected_file, actual_lines, *expected_lines);
EXPECT_TRUE(matches_expectation); EXPECT_TRUE(matches_expectation);
......
...@@ -19,9 +19,8 @@ namespace { ...@@ -19,9 +19,8 @@ namespace {
const char kCommentToken = '#'; const char kCommentToken = '#';
const char kMarkSkipFile[] = "#<skip"; const char kMarkSkipFile[] = "#<skip";
const char kSignalDiff[] = "*"; const char kSignalDiff[] = "*";
} // namespace
const char kMarkEndOfFile[] = "<-- End-of-file -->"; const char kMarkEndOfFile[] = "<-- End-of-file -->";
} // namespace
DumpAccessibilityTestHelper::DumpAccessibilityTestHelper( DumpAccessibilityTestHelper::DumpAccessibilityTestHelper(
AccessibilityTestExpectationsLocator* test_locator) AccessibilityTestExpectationsLocator* test_locator)
...@@ -80,10 +79,6 @@ DumpAccessibilityTestHelper::LoadExpectationFile( ...@@ -80,10 +79,6 @@ DumpAccessibilityTestHelper::LoadExpectationFile(
base::SplitString(expected_contents, "\n", base::KEEP_WHITESPACE, base::SplitString(expected_contents, "\n", base::KEEP_WHITESPACE,
base::SPLIT_WANT_NONEMPTY); base::SPLIT_WANT_NONEMPTY);
// Marking the end of the file with a line of text ensures that
// file length differences are found.
expected_lines.push_back(kMarkEndOfFile);
return expected_lines; return expected_lines;
} }
...@@ -123,6 +118,11 @@ bool DumpAccessibilityTestHelper::ValidateAgainstExpectation( ...@@ -123,6 +118,11 @@ bool DumpAccessibilityTestHelper::ValidateAgainstExpectation(
diff += "------\n"; diff += "------\n";
diff += base::JoinString(actual_lines, "\n"); diff += base::JoinString(actual_lines, "\n");
diff += "\n"; diff += "\n";
// This is used by rebase_dump_accessibility_tree_test.py to signify
// the end of the file when parsing the actual output from remote logs.
diff += kMarkEndOfFile;
diff += "\n";
LOG(ERROR) << "Diff:\n" << diff; LOG(ERROR) << "Diff:\n" << diff;
} else { } else {
LOG(INFO) << "Test output matches expectations."; LOG(INFO) << "Test output matches expectations.";
...@@ -163,6 +163,20 @@ std::vector<int> DumpAccessibilityTestHelper::DiffLines( ...@@ -163,6 +163,20 @@ std::vector<int> DumpAccessibilityTestHelper::DiffLines(
++j; ++j;
} }
// Report a failure if there are additional expected lines or
// actual lines.
if (i < actual_lines_count) {
diff_lines.push_back(j);
} else {
while (j < expected_lines_count) {
if (expected_lines[j].size() > 0 &&
expected_lines[j][0] != kCommentToken) {
diff_lines.push_back(j);
}
j++;
}
}
// Actual file has been fully checked. // Actual file has been fully checked.
return diff_lines; return diff_lines;
} }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef CONTENT_PUBLIC_TEST_DUMP_ACCESSIBILITY_TEST_HELPER_H_ #ifndef CONTENT_PUBLIC_TEST_DUMP_ACCESSIBILITY_TEST_HELPER_H_
#define CONTENT_PUBLIC_TEST_DUMP_ACCESSIBILITY_TEST_HELPER_H_ #define CONTENT_PUBLIC_TEST_DUMP_ACCESSIBILITY_TEST_HELPER_H_
#include "base/gtest_prod_util.h"
#include "base/optional.h" #include "base/optional.h"
namespace base { namespace base {
...@@ -13,9 +14,6 @@ class FilePath; ...@@ -13,9 +14,6 @@ class FilePath;
namespace content { namespace content {
// Sentinal value to mark end of actual/expected results.
extern const char kMarkEndOfFile[];
class AccessibilityTestExpectationsLocator; class AccessibilityTestExpectationsLocator;
// A helper class for writing accessibility tree dump tests. // A helper class for writing accessibility tree dump tests.
...@@ -46,6 +44,8 @@ class DumpAccessibilityTestHelper { ...@@ -46,6 +44,8 @@ class DumpAccessibilityTestHelper {
const std::vector<std::string>& expected_lines); const std::vector<std::string>& expected_lines);
private: private:
FRIEND_TEST_ALL_PREFIXES(DumpAccessibilityTestHelperTest, TestDiffLines);
// Utility helper that does a comment-aware equality check. // Utility helper that does a comment-aware equality check.
// Returns array of lines from expected file which are different. // Returns array of lines from expected file which are different.
static std::vector<int> DiffLines( static std::vector<int> DiffLines(
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/public/test/dump_accessibility_test_helper.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::ElementsAre;
namespace content {
TEST(DumpAccessibilityTestHelperTest, TestDiffLines) {
// Files with the same lines should have an empty diff.
EXPECT_THAT(DumpAccessibilityTestHelper::DiffLines({"broccoli", "turnip"},
{"broccoli", "turnip"}),
ElementsAre());
// Empty files should have an empty diff.
EXPECT_THAT(DumpAccessibilityTestHelper::DiffLines({}, {}), ElementsAre());
// If one line differs, that line number should be returned as the diff.
EXPECT_THAT(DumpAccessibilityTestHelper::DiffLines({"broccoli", "turnip"},
{"watermelon", "turnip"}),
ElementsAre(0));
EXPECT_THAT(DumpAccessibilityTestHelper::DiffLines({"broccoli", "turnip"},
{"broccoli", "rutabaga"}),
ElementsAre(1));
// Multiple lines can differ.
EXPECT_THAT(DumpAccessibilityTestHelper::DiffLines(
{"broccoli", "turnip", "carrot", "squash"},
{"broccoli", "aspartame", "carrot", "toothbrush"}),
ElementsAre(1, 3));
// Blank lines in the expected file are allowed.
EXPECT_THAT(DumpAccessibilityTestHelper::DiffLines(
{"", "broccoli", "turnip", "carrot", "", "squash"},
{"broccoli", "turnip", "carrot", "squash"}),
ElementsAre());
// Comments in the expected file are allowed.
EXPECT_THAT(
DumpAccessibilityTestHelper::DiffLines(
{"#Vegetables:", "broccoli", "turnip", "carrot", "", "squash"},
{"broccoli", "turnip", "carrot", "squash"}),
ElementsAre());
// Comments or blank lines in the actual file are NOT allowed, that's a diff.
EXPECT_THAT(
DumpAccessibilityTestHelper::DiffLines(
{"broccoli", "turnip", "carrot", "squash"},
{"#vegetables", "broccoli", "turnip", "carrot", "", "squash"}),
ElementsAre(0, 1, 2, 3, 4));
// If the expected file has an extra line, that's a diff.
EXPECT_THAT(DumpAccessibilityTestHelper::DiffLines(
{"broccoli", "turnip", "cow"}, {"broccoli", "turnip"}),
ElementsAre(2));
// If the actual file has an extra line, that's a diff.
EXPECT_THAT(DumpAccessibilityTestHelper::DiffLines(
{"broccoli", "turnip"}, {"broccoli", "turnip", "horse"}),
ElementsAre(2));
// If the expected file has an extra blank line, that's okay.
EXPECT_THAT(DumpAccessibilityTestHelper::DiffLines({"broccoli", "turnip", ""},
{"broccoli", "turnip"}),
ElementsAre());
}
} // namespace content
...@@ -1937,6 +1937,7 @@ test("content_unittests") { ...@@ -1937,6 +1937,7 @@ test("content_unittests") {
"../public/common/url_utils_unittest.cc", "../public/common/url_utils_unittest.cc",
"../public/test/browser_task_environment_unittest.cc", "../public/test/browser_task_environment_unittest.cc",
"../public/test/devtools_permission_overrides_unittest.cc", "../public/test/devtools_permission_overrides_unittest.cc",
"../public/test/dump_accessibility_test_helper_unittest.cc",
"../public/test/no_renderer_crashes_assertion_unittest.cc", "../public/test/no_renderer_crashes_assertion_unittest.cc",
"../public/test/permission_type_unittest.cc", "../public/test/permission_type_unittest.cc",
"../public/test/referrer_unittest.cc", "../public/test/referrer_unittest.cc",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment