Moved TransformationMethod contract tests into own test

TransformationMethodTest check that the contract between
TextView and method works (that the interface methods get
called).

At least the focusChanged one was being done in the Password
test, but it didn't actually test anything specific to the
Password method.

Also, focusChanged test doesn't rely on arrow keys to change
focus anymore since that behavior has changed (arrows don't
send focus out of textfields anymore).

Bug: 36972955
Test: TransformationMethodTest and PasswordTransformationMethodTest
Change-Id: Ie9157381675449f262bf041ae557f94065d3a471
diff --git a/tests/tests/text/src/android/text/method/cts/PasswordTransformationMethodTest.java b/tests/tests/text/src/android/text/method/cts/PasswordTransformationMethodTest.java
index 3ed0ad4..bb4ffb6 100644
--- a/tests/tests/text/src/android/text/method/cts/PasswordTransformationMethodTest.java
+++ b/tests/tests/text/src/android/text/method/cts/PasswordTransformationMethodTest.java
@@ -17,13 +17,11 @@
 package android.text.method.cts;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.Mockito.any;
-import static org.mockito.Mockito.anyBoolean;
 import static org.mockito.Mockito.anyInt;
 import static org.mockito.Mockito.atLeastOnce;
 import static org.mockito.Mockito.reset;
@@ -221,23 +219,6 @@
         assertSame(method0, method1);
     }
 
-    @Test
-    public void testOnFocusChanged() {
-        // lose focus
-        reset(mMethod);
-        assertTrue(mEditText.isFocused());
-        CtsKeyEventUtil.sendKeys(mInstrumentation, mEditText, "DPAD_DOWN");
-        assertFalse(mEditText.isFocused());
-        verify(mMethod, atLeastOnce()).onFocusChanged(any(), any(), anyBoolean(), anyInt(), any());
-
-        // gain focus
-        reset(mMethod);
-        assertFalse(mEditText.isFocused());
-        CtsKeyEventUtil.sendKeys(mInstrumentation, mEditText, "DPAD_UP");
-        assertTrue(mEditText.isFocused());
-        verify(mMethod, atLeastOnce()).onFocusChanged(any(), any(), anyBoolean(), anyInt(), any());
-    }
-
     private void savePasswordPref() {
         try {
             mPasswordPrefBackUp = System.getInt(mActivity.getContentResolver(),
diff --git a/tests/tests/text/src/android/text/method/cts/TransformationMethodTest.java b/tests/tests/text/src/android/text/method/cts/TransformationMethodTest.java
new file mode 100644
index 0000000..d67fc59
--- /dev/null
+++ b/tests/tests/text/src/android/text/method/cts/TransformationMethodTest.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.text.method.cts;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.AdditionalAnswers.returnsFirstArg;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyBoolean;
+import static org.mockito.Mockito.anyInt;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.app.Instrumentation;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.filters.MediumTest;
+import android.support.test.rule.ActivityTestRule;
+import android.support.test.runner.AndroidJUnit4;
+import android.text.method.TransformationMethod;
+import android.widget.Button;
+import android.widget.EditText;
+import android.widget.LinearLayout;
+import android.widget.LinearLayout.LayoutParams;
+
+import com.android.compatibility.common.util.PollingCheck;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Test that {@link TransformationMethod} interface gets called.
+ */
+@MediumTest
+@RunWith(AndroidJUnit4.class)
+public class TransformationMethodTest {
+    private static final int EDIT_TXT_ID = 1;
+
+    private Instrumentation mInstrumentation;
+    private CtsActivity mActivity;
+    private TransformationMethod mMethod;
+    private EditText mEditText;
+    private Button mButton;
+
+    @Rule
+    public ActivityTestRule<CtsActivity> mActivityRule = new ActivityTestRule<>(CtsActivity.class);
+
+    @Before
+    public void setup() throws Throwable {
+        mActivity = mActivityRule.getActivity();
+        PollingCheck.waitFor(1000, mActivity::hasWindowFocus);
+        mInstrumentation = InstrumentationRegistry.getInstrumentation();
+        mMethod = mock(TransformationMethod.class);
+        when(mMethod.getTransformation(any(), any())).then(returnsFirstArg());
+
+        mActivityRule.runOnUiThread(() -> {
+            mEditText = new EditTextNoIme(mActivity);
+            mEditText.setId(EDIT_TXT_ID);
+            mEditText.setTransformationMethod(mMethod);
+            mButton = new Button(mActivity);
+            mButton.setFocusableInTouchMode(true);
+            LinearLayout layout = new LinearLayout(mActivity);
+            layout.setOrientation(LinearLayout.VERTICAL);
+            layout.addView(mEditText, new LayoutParams(LayoutParams.MATCH_PARENT,
+                    LayoutParams.WRAP_CONTENT));
+            layout.addView(mButton, new LayoutParams(LayoutParams.MATCH_PARENT,
+                    LayoutParams.WRAP_CONTENT));
+            mActivity.setContentView(layout);
+            mEditText.requestFocus();
+        });
+        mInstrumentation.waitForIdleSync();
+
+        assertTrue(mEditText.isFocused());
+    }
+
+    @Test
+    public void testGetTransformation() throws Throwable {
+        reset(mMethod);
+        when(mMethod.getTransformation(any(), any())).then(returnsFirstArg());
+        mActivityRule.runOnUiThread(() -> mEditText.setText("some text"));
+        mInstrumentation.waitForIdleSync();
+        verify(mMethod, atLeastOnce()).getTransformation(any(), any());
+    }
+
+    @Test
+    public void testOnFocusChanged() throws Throwable {
+        // lose focus
+        reset(mMethod);
+        assertTrue(mEditText.isFocused());
+        mActivityRule.runOnUiThread(() -> mButton.requestFocus());
+        mInstrumentation.waitForIdleSync();
+        verify(mMethod, atLeastOnce()).onFocusChanged(any(), any(), anyBoolean(), anyInt(), any());
+
+        // gain focus
+        reset(mMethod);
+        assertFalse(mEditText.isFocused());
+        mActivityRule.runOnUiThread(() -> mEditText.requestFocus());
+        mInstrumentation.waitForIdleSync();
+        assertTrue(mEditText.isFocused());
+        verify(mMethod, atLeastOnce()).onFocusChanged(any(), any(), anyBoolean(), anyInt(), any());
+    }
+}