Disable Keyboard IME on TextEdit Jank Test

Keyboard will be brught up during textEdit jank test,
which result in not enough frames captured. Modified test apk so
as to disable keyboard IME when setup then reenable it when
teardown

bug: 27897448
Change-Id: If794ea47ccd31a98deb9202c1432265131b7726e
diff --git a/tests/jank/uibench_wear/src/com/android/wearable/uibench/janktests/UiBenchJankTestsHelper.java b/tests/jank/uibench_wear/src/com/android/wearable/uibench/janktests/UiBenchJankTestsHelper.java
index a1f4902..3c48b7a 100644
--- a/tests/jank/uibench_wear/src/com/android/wearable/uibench/janktests/UiBenchJankTestsHelper.java
+++ b/tests/jank/uibench_wear/src/com/android/wearable/uibench/janktests/UiBenchJankTestsHelper.java
@@ -17,6 +17,7 @@
 package com.android.wearable.uibench.janktests;
 
 import android.R;
+import android.util.Log;
 import android.content.Context;
 import android.content.Intent;
 import android.os.SystemClock;
@@ -26,6 +27,8 @@
 import android.support.test.uiautomator.UiObjectNotFoundException;
 import android.support.test.uiautomator.Until;
 
+import java.io.IOException;
+
 import junit.framework.Assert;
 
 /**
@@ -40,12 +43,15 @@
     public static final int EXPECTED_FRAMES = 100;
     public static final int CW_FLING_RATE = 5000;
 
+    public static final String LOG_TAG = "UiBenchJankTestsHelper";
     public static final String RES_PACKAGE_NAME = "android";
     public static final String PACKAGE_NAME = "com.android.test.uibench";
     public static final String ROOT_NAME = "root";
     public static final String LAUNCHER_VIEW_NAME = "launcher_view";
     public static final String TEXT_OBJECT_NAME = "text1";
     public static final String UIBENCH_OBJECT_NAME = "UiBench";
+    public static final String KEYBOARD_SERVICE_NAME =
+            "com.google.android.wearable.input.latin/.WearIME";
 
     private static UiBenchJankTestsHelper mInstance;
     private UiDevice mDevice;
@@ -145,4 +151,18 @@
             40); // slow speed
     }
 
+    // Helper method to turn on/off keyboard IME
+    public void enableKeyboardIME(Boolean turnOn) {
+        try {
+            String cmd = null;
+            if (turnOn) {
+                cmd = "ime enable %s";
+            } else {
+                cmd = "ime disable %s";
+            }
+            mDevice.executeShellCommand(String.format(cmd, KEYBOARD_SERVICE_NAME));
+        } catch (IOException e) {
+            Log.w(LOG_TAG, String.format("Exception when toggling keyboard. %s", e.toString()));
+        }
+    }
 }
diff --git a/tests/jank/uibench_wear/src/com/android/wearable/uibench/janktests/UiBenchTextJankTests.java b/tests/jank/uibench_wear/src/com/android/wearable/uibench/janktests/UiBenchTextJankTests.java
index afabd0d..60b6f3d 100644
--- a/tests/jank/uibench_wear/src/com/android/wearable/uibench/janktests/UiBenchTextJankTests.java
+++ b/tests/jank/uibench_wear/src/com/android/wearable/uibench/janktests/UiBenchTextJankTests.java
@@ -56,34 +56,18 @@
         mDevice.setOrientationNatural();
         mHelper = UiBenchJankTestsHelper.getInstance(mDevice,
              this.getInstrumentation().getContext());
+        mHelper.enableKeyboardIME(false);
     }
 
     @Override
     protected void tearDown() throws Exception {
         mDevice.unfreezeRotation();
+        mHelper.enableKeyboardIME(true);
         super.tearDown();
     }
 
-    // TODO(kneas): After b/27897448 is fixed, remove method or TODO
-    public void forceDeviceHome() throws RemoteException {
-        // Put device to sleep to go back home
-        if (VERSION.SDK_INT >= 20) {
-            mDevice.pressKeyCode(KeyEvent.KEYCODE_SLEEP);
-        } else {
-            mDevice.sleep();
-        }
-        SystemClock.sleep(mHelper.LONG_TIMEOUT);
-        mDevice.wakeUp();
-    }
-
     // Open Text Components
     public void openTextComponents(String componentName) throws RemoteException {
-        // TODO(kneas): Remove if statement after b/27897448 is fixed
-        // Needed in case the EditTextTyping tests fails, leaving it in the test with the keyboard
-        // open
-        if (mDevice.getProductName().equals("nemo")) {
-            forceDeviceHome();
-        }
         mHelper.launchUiBench();
         mHelper.openTextInList("Text");
         mHelper.openTextInList(componentName);
@@ -95,8 +79,7 @@
     }
 
     // Measure jank metrics for EditText Typing
-    // TODO(kneas): Change afterTest to "goBackHome" after b/27897448 is fixed
-    @JankTest(beforeTest="openEditTextTyping", afterTest="goBackHomeEditText",
+    @JankTest(beforeTest="openEditTextTyping", afterTest="goBackHome",
         expectedFrames=EXPECTED_FRAMES)
     @GfxMonitor(processName=PACKAGE_NAME)
     public void testEditTextTyping() {
@@ -172,24 +155,7 @@
     // Ensuring that we head back to the first screen before launching the app again
     public void goBackHome(Bundle metrics) throws UiObjectNotFoundException {
             mHelper.goBackHome();
-           super.afterTest(metrics);
+            super.afterTest(metrics);
     }
 
-    // Workaround for b/27897448 until investigation is complete
-    // TODO(kneas): Remove once b/27897448 is fixed
-    public void goBackHomeEditText(Bundle metrics)
-            throws RemoteException, UiObjectNotFoundException {
-        if (mDevice.getProductName() == "nemo") {
-            forceDeviceHome();
-            super.afterTest(metrics);
-            // Relaunch the app. Ideally we're still in EditText, but it's no longer typing
-            // goBackHome will now be able to use the back button, since the keyboard is hidden
-            SystemClock.sleep(mHelper.SHORT_TIMEOUT + mHelper.SHORT_TIMEOUT);
-            mHelper.launchUiBench();
-            mHelper.goBackHome();
-        }
-        else {
-            goBackHome(metrics);
-        }
-    }
 }