[NTP Client] Fix the calculation of the height of the bottom spacer

Switch to using the bottom of the last content item instead of the
top of the bottom spacer. Because the bottom spacer is not visible
when we do the calculation, its properties are not up to date,
and it makes the calculations return wrong results.

BUG=655514

Review-Url: https://codereview.chromium.org/2415663006
Cr-Commit-Position: refs/heads/master@{#425313}
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapter.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapter.java
index cc83745a..87ab56f1 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapter.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapter.java
@@ -374,6 +374,10 @@
         return getGroupPositionOffset(mBottomSpacer);
     }
 
+    public int getLastContentItemPosition() {
+        return getGroupPositionOffset(hasAllBeenDismissed() ? mAllDismissed : mFooter);
+    }
+
     public int getSuggestionPosition(SnippetArticle article) {
         List<NewTabPageItem> items = getItems();
         for (int i = 0; i < items.size(); i++) {
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageRecyclerView.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageRecyclerView.java
index b0c46c3..34442a7 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageRecyclerView.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageRecyclerView.java
@@ -195,11 +195,11 @@
             return 0;
         }
 
-        ViewHolder spacer = findBottomSpacer();
+        ViewHolder lastContentItem = findLastContentItem();
         ViewHolder aboveTheFold = findViewHolderForAdapterPosition(aboveTheFoldPosition);
 
         int bottomSpacing = getHeight() - mToolbarHeight;
-        if (spacer == null || aboveTheFold == null) {
+        if (lastContentItem == null || aboveTheFold == null) {
             // This can happen in several cases, where some elements are not visible and the
             // RecyclerView didn't already attach them. We handle it by just adding space to make
             // sure that we never run out and force the UI to jump around and get stuck in a
@@ -214,9 +214,10 @@
 
             Log.w(TAG, "The RecyclerView items are not attached, can't determine the content "
                             + "height: snap=%s, spacer=%s. Using full height: %d ",
-                    aboveTheFold, spacer, bottomSpacing);
+                    aboveTheFold, lastContentItem, bottomSpacing);
         } else {
-            int contentHeight = spacer.itemView.getTop() - aboveTheFold.itemView.getBottom();
+            int contentHeight =
+                    lastContentItem.itemView.getBottom() - aboveTheFold.itemView.getBottom();
             bottomSpacing -= contentHeight - mCompensationHeight;
         }
 
@@ -323,6 +324,13 @@
         return findViewHolderForAdapterPosition(position);
     }
 
+    private ViewHolder findLastContentItem() {
+        int position = getNewTabPageAdapter().getLastContentItemPosition();
+        if (position == RecyclerView.NO_POSITION) return null;
+
+        return findViewHolderForAdapterPosition(position);
+    }
+
     /**
      * Finds the above the fold view.
      * @return The view for above the fold or null, if it is not present.