DCHECK RenderText never renders more than max_lines()

Assert that if the text is homogeneous in style, font size,
weight, etc, multiline rendering should never render more than
max_lines().

Bug: 866720
Change-Id: I8b3cf652143d09c7e6d6bd1d8eb0b22714aa73a4
Reviewed-on: https://chromium-review.googlesource.com/1149409
Reviewed-by: Michael Wasserman <msw@chromium.org>
Commit-Queue: Wei-Yin Chen (陳威尹) <wychen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#578053}
diff --git a/ui/gfx/render_text.cc b/ui/gfx/render_text.cc
index 31cca78..c29ed3e 100644
--- a/ui/gfx/render_text.cc
+++ b/ui/gfx/render_text.cc
@@ -1069,6 +1069,18 @@
       cached_bounds_and_offset_valid_(false),
       strike_thickness_factor_(kLineThicknessFactor) {}
 
+bool RenderText::IsHomogeneous() const {
+  if (colors().breaks().size() > 1 || baselines().breaks().size() > 1 ||
+      font_size_overrides().breaks().size() > 1 ||
+      weights().breaks().size() > 1)
+    return false;
+  for (size_t style = 0; style < NUM_TEXT_STYLES; ++style) {
+    if (styles()[style].breaks().size() > 1)
+      return false;
+  }
+  return true;
+}
+
 SelectionModel RenderText::GetAdjacentSelectionModel(
     const SelectionModel& current,
     BreakType break_type,
diff --git a/ui/gfx/render_text.h b/ui/gfx/render_text.h
index 76c0db0..50eaca5 100644
--- a/ui/gfx/render_text.h
+++ b/ui/gfx/render_text.h
@@ -565,6 +565,9 @@
   const std::vector<BreakList<bool> >& styles() const { return styles_; }
   SkScalar strike_thickness_factor() const { return strike_thickness_factor_; }
 
+  // Whether all the BreakLists have only one break.
+  bool IsHomogeneous() const;
+
   const std::vector<internal::Line>& lines() const { return lines_; }
   void set_lines(std::vector<internal::Line>* lines) { lines_.swap(*lines); }
 
diff --git a/ui/gfx/render_text_harfbuzz.cc b/ui/gfx/render_text_harfbuzz.cc
index 4412374..b3774d7 100644
--- a/ui/gfx/render_text_harfbuzz.cc
+++ b/ui/gfx/render_text_harfbuzz.cc
@@ -1544,8 +1544,11 @@
     line_breaker.FinalizeLines(&lines, &total_size_);
     if (multiline() && max_lines()) {
       // TODO(crbug.com/866720): no more than max_lines() should be rendered.
-      // Uncomment the following DCHECK when the bug is fixed.
-      // DCHECK_LE(lines.size(), max_lines());
+      // Remove the IsHomogeneous() condition for the following DCHECK when the
+      // bug is fixed.
+      if (IsHomogeneous()) {
+        DCHECK_LE(lines.size(), max_lines());
+      }
     }
     set_lines(&lines);
   }