Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.text.method.cts;
     18 
     19 import android.app.Instrumentation;
     20 import android.support.test.InstrumentationRegistry;
     21 import android.support.test.rule.ActivityTestRule;
     22 import android.text.cts.R;
     23 import android.text.method.KeyListener;
     24 import android.view.KeyEvent;
     25 import android.widget.EditText;
     26 
     27 import com.android.compatibility.common.util.PollingCheck;
     28 
     29 import org.junit.Before;
     30 import org.junit.Rule;
     31 
     32 /**
     33  * Base class for various KeyListener tests.
     34  */
     35 public abstract class KeyListenerTestCase {
     36     protected KeyListenerCtsActivity mActivity;
     37     protected Instrumentation mInstrumentation;
     38     protected EditText mTextView;
     39 
     40     @Rule
     41     public ActivityTestRule<KeyListenerCtsActivity> mActivityRule =
     42             new ActivityTestRule<>(KeyListenerCtsActivity.class);
     43 
     44     @Before
     45     public void setup() {
     46         mInstrumentation = InstrumentationRegistry.getInstrumentation();
     47         mActivity = mActivityRule.getActivity();
     48         mTextView = (EditText) mActivity.findViewById(R.id.keylistener_textview);
     49 
     50         PollingCheck.waitFor(5000, mActivity::hasWindowFocus);
     51     }
     52 
     53     /**
     54      * Synchronously sets mTextView's key listener on the UI thread.
     55      */
     56     protected void setKeyListenerSync(final KeyListener keyListener) {
     57         mInstrumentation.runOnMainSync(() -> mTextView.setKeyListener(keyListener));
     58         mInstrumentation.waitForIdleSync();
     59     }
     60 
     61     protected static KeyEvent getKey(int keycode, int metaState) {
     62         long currentTime = System.currentTimeMillis();
     63         return new KeyEvent(currentTime, currentTime, KeyEvent.ACTION_DOWN, keycode,
     64                 0 /* repeat */, metaState);
     65     }
     66 }
     67