[Explore Sites] Fix render tests for other builders.

Some tablets failed because scrolling to a position did not guarantee
that position would be the first visible, since the catalog is not
big enough on a tablet.

Also switches scrollToPosition -> scrollToPositionWithOffset to match
behavior of moving a tile to the top.

Bug: 953254
Change-Id: Id7671a73e4ce16cc834ac66daa25369205ed4eee
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1570189
Reviewed-by: Cathy Li <chili@chromium.org>
Commit-Queue: Justin DeWitt <dewittj@chromium.org>
Cr-Commit-Position: refs/heads/master@{#652173}
diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/explore_sites/ExploreSitesPageTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/explore_sites/ExploreSitesPageTest.java
index a89efdd..eb5ce7a 100644
--- a/chrome/android/javatests/src/org/chromium/chrome/browser/explore_sites/ExploreSitesPageTest.java
+++ b/chrome/android/javatests/src/org/chromium/chrome/browser/explore_sites/ExploreSitesPageTest.java
@@ -5,19 +5,25 @@
 package org.chromium.chrome.browser.explore_sites;
 
 import static android.support.test.espresso.Espresso.onView;
+import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
+import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
 
+import static org.hamcrest.Matchers.allOf;
 import static org.hamcrest.Matchers.instanceOf;
 
 import android.annotation.TargetApi;
 import android.os.Build;
 import android.os.SystemClock;
 import android.support.test.InstrumentationRegistry;
+import android.support.test.espresso.UiController;
+import android.support.test.espresso.ViewAction;
 import android.support.test.espresso.contrib.RecyclerViewActions;
 import android.support.test.filters.SmallTest;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
 import android.view.View;
 
+import org.hamcrest.Matcher;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Assume;
@@ -27,7 +33,6 @@
 import org.junit.runner.RunWith;
 
 import org.chromium.base.test.util.CommandLineFlags;
-import org.chromium.base.test.util.DisabledTest;
 import org.chromium.base.test.util.Feature;
 import org.chromium.chrome.R;
 import org.chromium.chrome.browser.ChromeActivity;
@@ -57,6 +62,34 @@
 public class ExploreSitesPageTest {
     // clang-format on
 
+    private static final class ScrollToPositionWithOffsetViewAction implements ViewAction {
+        private final int mPosition;
+        private final int mOffset;
+
+        private ScrollToPositionWithOffsetViewAction(int position, int offset) {
+            this.mPosition = position;
+            mOffset = offset;
+        }
+
+        @SuppressWarnings("unchecked")
+        @Override
+        public Matcher<View> getConstraints() {
+            return allOf(isAssignableFrom(RecyclerView.class), isDisplayed());
+        }
+
+        @Override
+        public String getDescription() {
+            return "scroll RecyclerView to mPosition: " + mPosition;
+        }
+
+        @Override
+        public void perform(UiController uiController, View view) {
+            RecyclerView recyclerView = (RecyclerView) view;
+            ((LinearLayoutManager) recyclerView.getLayoutManager())
+                    .scrollToPositionWithOffset(mPosition, mOffset);
+        }
+    }
+
     ArrayList<ExploreSitesCategory> getTestingCatalog() {
         final ArrayList<ExploreSitesCategory> categoryList = new ArrayList<>();
         for (int i = 0; i < 5; i++) {
@@ -112,16 +145,19 @@
                 .findFirstCompletelyVisibleItemPosition();
     }
 
+    private void scrollToPosition(int scrollPosition) {
+        onView(instanceOf(RecyclerView.class))
+                .perform(new ScrollToPositionWithOffsetViewAction(scrollPosition, 0));
+    }
+
     @Test
     @SmallTest
-    @DisabledTest
     @Feature({"ExploreSites", "RenderTest"})
     public void testScrolledLayout_withBack() throws Exception {
-        final int scrollPosition = 2;
-        onView(instanceOf(RecyclerView.class))
-                .perform(RecyclerViewActions.scrollToPosition(scrollPosition));
+        scrollToPosition(2);
+
         mRenderTestRule.render(mRecyclerView, "recycler_layout");
-        Assert.assertEquals(scrollPosition, getFirstVisiblePosition());
+        final int scrollPosition = getFirstVisiblePosition();
         // TODO(https://crbug.com/938519): Remove this sleep in favor of actually waiting for the
         // scroll bar to disappear.
         SystemClock.sleep(3000);
diff --git a/chrome/test/data/android/render_tests/ExploreSitesPageTest.recycler_layout.Nexus_5-19.png.sha1 b/chrome/test/data/android/render_tests/ExploreSitesPageTest.recycler_layout.Nexus_5-19.png.sha1
index 44d81ba..ff1bb65 100644
--- a/chrome/test/data/android/render_tests/ExploreSitesPageTest.recycler_layout.Nexus_5-19.png.sha1
+++ b/chrome/test/data/android/render_tests/ExploreSitesPageTest.recycler_layout.Nexus_5-19.png.sha1
@@ -1 +1 @@
-d60de483ccbe01a433798e31df3f9f08dc387480
\ No newline at end of file
+8d1722f71ac07ab22c7c126b00e18deff42827f1
\ No newline at end of file
diff --git a/chrome/test/data/android/render_tests/ExploreSitesPageTest.recycler_layout_back.Nexus_5-19.png.sha1 b/chrome/test/data/android/render_tests/ExploreSitesPageTest.recycler_layout_back.Nexus_5-19.png.sha1
index d6523ec..d7f7cc78 100644
--- a/chrome/test/data/android/render_tests/ExploreSitesPageTest.recycler_layout_back.Nexus_5-19.png.sha1
+++ b/chrome/test/data/android/render_tests/ExploreSitesPageTest.recycler_layout_back.Nexus_5-19.png.sha1
@@ -1 +1 @@
-33ecdb2b25aac53cc826f38e281a11cf6eefdb8e
\ No newline at end of file
+56c1615c2cae305efe6fdc0d73d660608d5470f5
\ No newline at end of file