hterm: Don't announce page up/down at the top/bottom of the screen

Currently paging up/down when at the top/bottom of the screen can cause
a page up/down notification to be announced by the screen reader. This
change detects that case and avoids the announcement.

Bug: 822490, 646690
Change-Id: I5a6c2da6c77f84e5b1f636a9e21da2273bce9084
Reviewed-on: https://chromium-review.googlesource.com/1117730
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 e2d5f68..11aae60 100644
--- a/hterm/js/hterm_scrollport.js
+++ b/hterm/js/hterm_scrollport.js
@@ -556,6 +556,10 @@
  * position.
  */
 hterm.ScrollPort.prototype.scrollPageUp = function() {
+  if (this.getTopRowIndex() == 0) {
+    return;
+  }
+
   const i = this.getTopRowIndex();
   this.scrollRowToTop(i - this.visibleRowCount + 1);
 
@@ -567,6 +571,10 @@
  * position.
  */
 hterm.ScrollPort.prototype.scrollPageDown = function() {
+  if (this.isScrolledEnd) {
+    return;
+  }
+
   const i = this.getTopRowIndex();
   this.scrollRowToTop(i + this.visibleRowCount - 1);
 
diff --git a/hterm/js/hterm_scrollport_tests.js b/hterm/js/hterm_scrollport_tests.js
index 97bf3a5..471fa95 100644
--- a/hterm/js/hterm_scrollport_tests.js
+++ b/hterm/js/hterm_scrollport_tests.js
@@ -440,6 +440,30 @@
 });
 
 /**
+ * Test that paging up/down when at the top/bottom of the screen doesn't trigger
+ * any announcement.
+ */
+hterm.ScrollPort.Tests.addTest(
+    'page-up-down-dont-announce', function(result, cx) {
+  const doc = this.scrollPort.getDocument();
+
+  this.scrollPort.scrollRowToTop(0);
+  const mockAccessibilityReader = new MockAccessibilityReader();
+  this.scrollPort.setAccessibilityReader(mockAccessibilityReader);
+
+  const pageUp = doc.getElementById('hterm:a11y:page-up');
+  pageUp.dispatchEvent(new Event('click'));
+  result.assertEQ(mockAccessibilityReader.lastStringAnnounced, '');
+
+  this.scrollPort.scrollRowToTop(10000);
+  const pageDown = doc.getElementById('hterm:a11y:page-down');
+  pageDown.dispatchEvent(new Event('click'));
+  result.assertEQ(mockAccessibilityReader.lastStringAnnounced, '');
+
+  result.pass();
+});
+
+/**
  * Remove the scrollPort that was set up and leave the user with a full-page
  * scroll port.
  *