Backport uibench changes from master

bug: 31061998
initial commit: 09c34b8bfe8e

Change-Id: I342a5aa506dbe397cc6f939996dd06e2fcb38b9d
diff --git a/tests/jank/uibench/src/com/android/uibench/janktests/UiBenchJankTests.java b/tests/jank/uibench/src/com/android/uibench/janktests/UiBenchJankTests.java
index 7990f7d..8b58b67 100644
--- a/tests/jank/uibench/src/com/android/uibench/janktests/UiBenchJankTests.java
+++ b/tests/jank/uibench/src/com/android/uibench/janktests/UiBenchJankTests.java
@@ -122,11 +122,7 @@
 
     // Open Trivial listview from General
     public void openTrivialListView() {
-        mHelper.launchActivity("TrivialListActivity",
-                "General/Trivial ListView");
-        mHelper.mContents = mDevice.wait(Until.findObject(
-                By.res("android", "content")), mHelper.TIMEOUT);
-        Assert.assertNotNull("Trivial ListView isn't found in General", mHelper.mContents);
+        mHelper.launchActivityAndAssert("TrivialListActivity", "General/Trivial ListView");
     }
 
     // Test trivialListView fling
@@ -138,12 +134,8 @@
 
     // Open Trivial Recycler List View from General
     public void openTrivialRecyclerListView() {
-        mHelper.launchActivity("TrivialRecyclerViewActivity",
+        mHelper.launchActivityAndAssert("TrivialRecyclerViewActivity",
                 "General/Trivial Recycler ListView");
-        mHelper.mContents = mDevice.wait(Until.findObject(
-                By.res("android", "content")), mHelper.TIMEOUT);
-        Assert.assertNotNull("Trivial Recycler ListView isn't found in General",
-                mHelper.mContents);
     }
 
     // Test trivialRecyclerListView fling
@@ -155,12 +147,8 @@
 
     // Open Inflation Listview contents
     public void openInflatingListView() {
-        mHelper.launchActivity("InflatingListActivity",
+        mHelper.launchActivityAndAssert("InflatingListActivity",
                 "Inflation/Inflating ListView");
-        mHelper.mContents = mDevice.wait(Until.findObject(
-                By.res("android", "content")), mHelper.TIMEOUT);
-        Assert.assertNotNull("Inflating ListView isn't found in Inflation",
-                mHelper.mContents);
     }
 
     // Test Inflating List View fling
@@ -169,5 +157,4 @@
     public void testInflatingListViewFling() {
         mHelper.flingUpDown(mHelper.mContents, mHelper.SHORT_TIMEOUT, 2);
     }
-
 }
diff --git a/tests/jank/uibench/src/com/android/uibench/janktests/UiBenchJankTestsHelper.java b/tests/jank/uibench/src/com/android/uibench/janktests/UiBenchJankTestsHelper.java
index aca7d1f..a5e2af4 100644
--- a/tests/jank/uibench/src/com/android/uibench/janktests/UiBenchJankTestsHelper.java
+++ b/tests/jank/uibench/src/com/android/uibench/janktests/UiBenchJankTestsHelper.java
@@ -75,6 +75,12 @@
                 expectedTextCmp);
     }
 
+    public void launchActivityAndAssert(String activityName, String verifyText) {
+        launchActivity(activityName, verifyText);
+        mContents = mDevice.wait(Until.findObject(By.res("android", "content")), TIMEOUT);
+        Assert.assertNotNull(activityName + " isn't found", mContents);
+    }
+
     /**
      * To perform the fling down and up on given content for flingCount number
      * of times
@@ -83,11 +89,15 @@
      * @param flingCount
      */
     public void flingUpDown(UiObject2 content, long timeout, int flingCount) {
+        flingUpDown(content, timeout, flingCount, false);
+    }
+
+    public void flingUpDown(UiObject2 content, long timeout, int flingCount, boolean reverse) {
         for (int count = 0; count < flingCount; count++) {
             SystemClock.sleep(timeout);
-            content.fling(Direction.DOWN);
+            content.fling(reverse ? Direction.UP : Direction.DOWN);
             SystemClock.sleep(timeout);
-            content.fling(Direction.UP);
+            content.fling(reverse ? Direction.DOWN : Direction.UP);
         }
     }