release-request-160c4b31-7fa0-4e2b-aabe-85380836a1ce-for-git_oc-release-4129081 snap-temp-L15300000077039010

Change-Id: Ia5755f4037c646ef8c80b842881e0ed0d49f82ee
diff --git a/libraries/app-helpers/handheld/src/android/platform/test/helpers/handheld/AbstractChromeHelper.java b/libraries/app-helpers/handheld/src/android/platform/test/helpers/handheld/IChromeHelper.java
similarity index 92%
rename from libraries/app-helpers/handheld/src/android/platform/test/helpers/handheld/AbstractChromeHelper.java
rename to libraries/app-helpers/handheld/src/android/platform/test/helpers/handheld/IChromeHelper.java
index 7589d22..9362fec 100644
--- a/libraries/app-helpers/handheld/src/android/platform/test/helpers/handheld/AbstractChromeHelper.java
+++ b/libraries/app-helpers/handheld/src/android/platform/test/helpers/handheld/IChromeHelper.java
@@ -19,12 +19,7 @@
 import android.app.Instrumentation;
 import android.support.test.uiautomator.Direction;
 
-public abstract class AbstractChromeHelper extends AbstractStandardAppHelper {
-
-    public AbstractChromeHelper(Instrumentation instr) {
-        super(instr);
-    }
-
+public interface IChromeHelper extends IStandardAppHelper {
     /**
      * Setup expectations: Chrome is open and on a standard page, i.e. a tab is open.
      *
diff --git a/libraries/launcher-helper/src/android/support/test/launcherhelper/TvLauncherStrategy.java b/libraries/launcher-helper/src/android/support/test/launcherhelper/TvLauncherStrategy.java
index c11af46..610fdbb 100644
--- a/libraries/launcher-helper/src/android/support/test/launcherhelper/TvLauncherStrategy.java
+++ b/libraries/launcher-helper/src/android/support/test/launcherhelper/TvLauncherStrategy.java
@@ -191,13 +191,25 @@
     }
 
     /**
+     * Returns a {@link BySelector} describing a given favorite app
+     */
+    public BySelector getFavoriteAppSelector(String appName) {
+        return By.res(getSupportedLauncherPackage(), "favorite_app_banner").text(appName);
+    }
+
+    /**
+     * Returns a {@link BySelector} describing a given app in Apps View
+     */
+    public BySelector getAppInAppsViewSelector(String appName) {
+        return By.res(getSupportedLauncherPackage(), "app_title").text(appName);
+    }
+
+    /**
      * {@inheritDoc}
      */
     @Override
     public long launch(String appName, String packageName) {
-        BySelector appSelector = By.res(getSupportedLauncherPackage(),
-                "app_banner_image").desc(appName);
-        return launchApp(this, appSelector, packageName, isGame(packageName));
+        return launchApp(this, appName, packageName, isGame(packageName));
     }
 
     /**
@@ -322,7 +334,26 @@
         return expected;
     }
 
-    protected long launchApp(ILauncherStrategy launcherStrategy, BySelector appSelector,
+    /**
+     * Select the given app in All Apps activity in zigzag manner.
+     * When the All Apps opens, the focus is always at the top left.
+     * Search from left to right, and down to the next row, from right to left, and
+     * down to the next row like a zigzag pattern until it founds a given app.
+     */
+    public UiObject2 selectAppInAllAppsZigZag(BySelector appSelector, String packageName) {
+        Direction direction = Direction.RIGHT;
+        UiObject2 app = select(appSelector, direction, UI_TRANSITION_WAIT_TIME);
+        while (app == null && move(Direction.DOWN)) {
+            direction = Direction.reverse(direction);
+            app = select(appSelector, direction, UI_TRANSITION_WAIT_TIME);
+        }
+        if (app != null) {
+            Log.i(LOG_TAG, String.format("The app %s is selected", packageName));
+        }
+        return app;
+    }
+
+    protected long launchApp(ILauncherStrategy launcherStrategy, String appName,
             String packageName, boolean isGame) {
         unlockDeviceIfAsleep();
 
@@ -335,13 +366,17 @@
         launcherStrategy.open();
         selectAppsRow();
 
-        // Search for the app in the Apps row first.
+        // Search for the app in the Favorite Apps row first.
         // If not exists, open the 'All Apps' and search for the app there
         UiObject2 app = null;
-        if (mDevice.hasObject(appSelector)) {
-            app = selectBidirect(By.focused(true).hasDescendant(appSelector), Direction.RIGHT);
+        BySelector favAppSelector = getFavoriteAppSelector(appName);
+        if (mDevice.hasObject(favAppSelector)) {
+            app = selectBidirect(By.focused(true).hasDescendant(favAppSelector), Direction.RIGHT);
         } else {
-            app = selectAppInAllApps(appSelector, packageName);
+            openAllApps(true);
+            // Find app in Apps View in zigzag mode with app selector for Apps View
+            // because the app title no longer appears until focused.
+            app = selectAppInAllAppsZigZag(getAppInAppsViewSelector(appName), packageName);
         }
         if (app == null) {
             throw new RuntimeException(
@@ -479,6 +514,36 @@
         return object;
     }
 
+    /**
+     * Simulate a move pressing a key code.
+     * Return true if a focus is shifted on TV UI, otherwise false.
+     */
+    public boolean move(Direction direction) {
+        int keyCode = KeyEvent.KEYCODE_UNKNOWN;
+        switch (direction) {
+            case LEFT:
+                keyCode = KeyEvent.KEYCODE_DPAD_LEFT;
+                break;
+            case RIGHT:
+                keyCode = KeyEvent.KEYCODE_DPAD_RIGHT;
+                break;
+            case UP:
+                keyCode = KeyEvent.KEYCODE_DPAD_UP;
+                break;
+            case DOWN:
+                keyCode = KeyEvent.KEYCODE_DPAD_DOWN;
+                break;
+            default:
+                throw new RuntimeException(String.format("This direction %s is not supported.",
+                    direction));
+        }
+        UiObject2 focus = mDevice.wait(Until.findObject(By.focused(true)),
+                UI_TRANSITION_WAIT_TIME);
+        mDPadUtil.pressKeyCodeAndWait(keyCode);
+        return !focus.equals(mDevice.wait(Until.findObject(By.focused(true)),
+                UI_TRANSITION_WAIT_TIME));
+    }
+
 
     // Unsupported methods
 
diff --git a/tests/jank/uibench/src/com/android/uibench/janktests/UiBenchTextJankTests.java b/tests/jank/uibench/src/com/android/uibench/janktests/UiBenchTextJankTests.java
index 64b786e..1e8f8d7 100644
--- a/tests/jank/uibench/src/com/android/uibench/janktests/UiBenchTextJankTests.java
+++ b/tests/jank/uibench/src/com/android/uibench/janktests/UiBenchTextJankTests.java
@@ -60,11 +60,12 @@
     }
 
     // Measure jank metrics for EditText Typing
-    @JankTest(beforeTest = "openEditTextTyping", expectedFrames = EXPECTED_FRAMES)
-    @GfxMonitor(processName = PACKAGE_NAME)
-    public void testEditTextTyping() {
-        SystemClock.sleep(UiBenchJankTestsHelper.FULL_TEST_DURATION);
-    }
+    // Reenable the test after b/62917134 is fixed
+    // @JankTest(beforeTest = "openEditTextTyping", expectedFrames = EXPECTED_FRAMES)
+    // @GfxMonitor(processName = PACKAGE_NAME)
+    // public void testEditTextTyping() {
+    //    SystemClock.sleep(UiBenchJankTestsHelper.FULL_TEST_DURATION);
+    //}
 
     // Open Layout Cache High Hitrate
     public void openLayoutCacheHighHitrate() {