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/+/2109458
Reviewed-by: Aaron Leventhal <aleventhal@chromium.org>
Reviewed-by: Avi 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}
diff --git a/chrome/test/data/pdf/accessibility/whitespace-expected.txt b/chrome/test/data/pdf/accessibility/whitespace-expected.txt
index 8d1c8b6..e69de29 100644
--- a/chrome/test/data/pdf/accessibility/whitespace-expected.txt
+++ b/chrome/test/data/pdf/accessibility/whitespace-expected.txt
@@ -1 +0,0 @@
- 
diff --git a/content/browser/accessibility/dump_accessibility_browsertest_base.cc b/content/browser/accessibility/dump_accessibility_browsertest_base.cc
index fb723a1..f6eed610 100644
--- a/content/browser/accessibility/dump_accessibility_browsertest_base.cc
+++ b/content/browser/accessibility/dump_accessibility_browsertest_base.cc
@@ -367,7 +367,6 @@
   }
 
   // Validate against the expectation file.
-  actual_lines.push_back(kMarkEndOfFile);
   bool matches_expectation = test_helper.ValidateAgainstExpectation(
       file_path, expected_file, actual_lines, *expected_lines);
   EXPECT_TRUE(matches_expectation);
diff --git a/content/public/test/dump_accessibility_test_helper.cc b/content/public/test/dump_accessibility_test_helper.cc
index ac1b83e..1b4aeab 100644
--- a/content/public/test/dump_accessibility_test_helper.cc
+++ b/content/public/test/dump_accessibility_test_helper.cc
@@ -19,9 +19,8 @@
 const char kCommentToken = '#';
 const char kMarkSkipFile[] = "#<skip";
 const char kSignalDiff[] = "*";
-}  // namespace
-
 const char kMarkEndOfFile[] = "<-- End-of-file -->";
+}  // namespace
 
 DumpAccessibilityTestHelper::DumpAccessibilityTestHelper(
     AccessibilityTestExpectationsLocator* test_locator)
@@ -80,10 +79,6 @@
       base::SplitString(expected_contents, "\n", base::KEEP_WHITESPACE,
                         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;
 }
 
@@ -123,6 +118,11 @@
     diff += "------\n";
     diff += base::JoinString(actual_lines, "\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;
   } else {
     LOG(INFO) << "Test output matches expectations.";
@@ -163,6 +163,20 @@
     ++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.
   return diff_lines;
 }
diff --git a/content/public/test/dump_accessibility_test_helper.h b/content/public/test/dump_accessibility_test_helper.h
index a72b9c3..c4c9763 100644
--- a/content/public/test/dump_accessibility_test_helper.h
+++ b/content/public/test/dump_accessibility_test_helper.h
@@ -5,6 +5,7 @@
 #ifndef 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"
 
 namespace base {
@@ -13,9 +14,6 @@
 
 namespace content {
 
-// Sentinal value to mark end of actual/expected results.
-extern const char kMarkEndOfFile[];
-
 class AccessibilityTestExpectationsLocator;
 
 // A helper class for writing accessibility tree dump tests.
@@ -46,6 +44,8 @@
       const std::vector<std::string>& expected_lines);
 
  private:
+  FRIEND_TEST_ALL_PREFIXES(DumpAccessibilityTestHelperTest, TestDiffLines);
+
   // Utility helper that does a comment-aware equality check.
   // Returns array of lines from expected file which are different.
   static std::vector<int> DiffLines(
diff --git a/content/public/test/dump_accessibility_test_helper_unittest.cc b/content/public/test/dump_accessibility_test_helper_unittest.cc
new file mode 100644
index 0000000..9a20e6f9
--- /dev/null
+++ b/content/public/test/dump_accessibility_test_helper_unittest.cc
@@ -0,0 +1,74 @@
+// 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
diff --git a/content/test/BUILD.gn b/content/test/BUILD.gn
index 548ce84..f47de53e 100644
--- a/content/test/BUILD.gn
+++ b/content/test/BUILD.gn
@@ -1937,6 +1937,7 @@
     "../public/common/url_utils_unittest.cc",
     "../public/test/browser_task_environment_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/permission_type_unittest.cc",
     "../public/test/referrer_unittest.cc",