hterm: Fix announcement of current scrolled percentage

Currently the scroll percentage can be computed to NaN when the number
of rows onscreen is the same as the total number of rows. This fixes
the divide by zero issue.

Bug: 822490, 646690
Change-Id: I65afb5cbf6271be5d6b6772bf2afe8f2711e1013
Reviewed-on: https://chromium-review.googlesource.com/1116212
Tested-by: Raymes Khoury <raymes@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/hterm/js/hterm_scrollport.js b/hterm/js/hterm_scrollport.js
index 11aae60..b27ae67 100644
--- a/hterm/js/hterm_scrollport.js
+++ b/hterm/js/hterm_scrollport.js
@@ -889,7 +889,7 @@
   const bottomRow = this.getBottomRowIndex(topRow);
 
   let percentScrolled = 100 * topRow /
-      (this.rowProvider_.getRowCount() - this.visibleRowCount);
+      Math.max(1, this.rowProvider_.getRowCount() - this.visibleRowCount);
   percentScrolled = Math.min(100, Math.round(percentScrolled));
   let currentScreenContent = hterm.msg('ANNOUNCE_CURRENT_SCREEN_HEADER',
                                        [percentScrolled],
diff --git a/hterm/js/hterm_scrollport_tests.js b/hterm/js/hterm_scrollport_tests.js
index 471fa95..966896c 100644
--- a/hterm/js/hterm_scrollport_tests.js
+++ b/hterm/js/hterm_scrollport_tests.js
@@ -464,6 +464,32 @@
 });
 
 /**
+ * Test that the percentage scrolled is always listed as 0% when all rows are
+ * visible.
+ */
+hterm.ScrollPort.Tests.addTest(
+    'page-down-all-rows-visible', function(result, cx) {
+  const doc = this.scrollPort.getDocument();
+
+  // Resize the scrollport to show all rows.
+  doc.body.firstChild.style.height =
+      this.scrollPort.characterSize.height * this.totalRowCount + 1 + 'px';
+  this.scrollPort.resize();
+
+  this.scrollPort.scrollRowToTop(0);
+  const mockAccessibilityReader = new MockAccessibilityReader();
+  this.scrollPort.setAccessibilityReader(mockAccessibilityReader);
+
+  const pageDown = doc.getElementById('hterm:a11y:page-down');
+  pageDown.dispatchEvent(new Event('click'));
+  result.assertEQ(
+      mockAccessibilityReader.lastStringAnnounced.split('\n')[0],
+      '0% scrolled,');
+
+  result.pass();
+});
+
+/**
  * Remove the scrollPort that was set up and leave the user with a full-page
  * scroll port.
  *