Home | History | Annotate | Download | only in samples
      1 /*
      2  * Copyright (C) 2007 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 com.android.imftest.samples;
     18 
     19 import android.app.Activity;
     20 import android.app.KeyguardManager;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.res.Configuration;
     24 import android.os.SystemClock;
     25 import android.test.InstrumentationTestCase;
     26 import android.view.KeyEvent;
     27 import android.view.View;
     28 import android.view.WindowManager;
     29 import android.view.inputmethod.InputMethodManager;
     30 
     31 import com.android.imftest.R;
     32 
     33 public abstract class ImfBaseTestCase<T extends Activity> extends InstrumentationTestCase {
     34 
     35     /*
     36      * The amount of time we are willing to wait for the IME to appear after a user action
     37      * before we give up and fail the test.
     38      */
     39     public final long WAIT_FOR_IME = 5000;
     40 
     41     /*
     42      * Unfortunately there is now way for us to know how tall the IME is,
     43      * so we have to hard code a minimum and maximum value.
     44      */
     45     public final int IME_MIN_HEIGHT = 150;
     46     public final int IME_MAX_HEIGHT = 300;
     47 
     48     protected InputMethodManager mImm;
     49     protected T mTargetActivity;
     50     protected boolean mExpectAutoPop;
     51     private Class<T> mTargetActivityClass;
     52 
     53     public ImfBaseTestCase(Class<T> activityClass) {
     54         mTargetActivityClass = activityClass;
     55     }
     56 
     57     @Override
     58     public void setUp() throws Exception {
     59         super.setUp();
     60         final String packageName = getInstrumentation().getTargetContext().getPackageName();
     61         Intent intent = new Intent(Intent.ACTION_MAIN);
     62         intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
     63         mTargetActivity = launchActivityWithIntent(packageName, mTargetActivityClass, intent);
     64         // expect ime to auto pop up if device has no hard keyboard
     65         int keyboardType = mTargetActivity.getResources().getConfiguration().keyboard;
     66         mExpectAutoPop = (keyboardType  == Configuration.KEYBOARD_NOKEYS ||
     67                 keyboardType == Configuration.KEYBOARD_UNDEFINED);
     68 
     69         mImm = InputMethodManager.getInstance();
     70 
     71         KeyguardManager keyguardManager =
     72             (KeyguardManager) getInstrumentation().getContext().getSystemService(
     73                     Context.KEYGUARD_SERVICE);
     74         keyguardManager.newKeyguardLock("imftest").disableKeyguard();
     75     }
     76 
     77     // Utility test methods
     78     public void verifyEditTextAdjustment(final View editText, int rootViewHeight) {
     79 
     80         int[] origLocation = new int[2];
     81         int[] newLocation = new int[2];
     82 
     83         // Tell the keyboard to go away.
     84         mImm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
     85 
     86         // Bring the target EditText into focus.
     87         mTargetActivity.runOnUiThread(new Runnable() {
     88             public void run() {
     89                 editText.requestFocus();
     90             }
     91         });
     92 
     93         // Get the original location of the EditText.
     94         editText.getLocationOnScreen(origLocation);
     95 
     96         // Tap the EditText to bring up the IME.
     97         sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
     98 
     99         // Wait until the EditText pops above the IME or until we hit the timeout.
    100         editText.getLocationOnScreen(newLocation);
    101         long timeoutTime = SystemClock.uptimeMillis() + WAIT_FOR_IME;
    102         while (newLocation[1] > rootViewHeight - IME_MIN_HEIGHT && SystemClock.uptimeMillis() < timeoutTime) {
    103             editText.getLocationOnScreen(newLocation);
    104             pause(100);
    105         }
    106 
    107         assertTrue(newLocation[1] <= rootViewHeight - IME_MIN_HEIGHT);
    108 
    109         // Tell the keyboard to go away.
    110         mImm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    111     }
    112 
    113     public void destructiveCheckImeInitialState(View rootView, View servedView) {
    114         int windowSoftInputMode = mTargetActivity.getWindow().getAttributes().softInputMode;
    115         int adjustMode = windowSoftInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST;
    116         if (mExpectAutoPop && adjustMode == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) {
    117             assertTrue(destructiveCheckImeUp(rootView, servedView));
    118         } else {
    119             assertFalse(destructiveCheckImeUp(rootView, servedView));
    120         }
    121     }
    122 
    123     public boolean destructiveCheckImeUp(View rootView, View servedView) {
    124         int origHeight;
    125         int newHeight;
    126 
    127         origHeight = rootView.getHeight();
    128 
    129         // Tell the keyboard to go away.
    130         mImm.hideSoftInputFromWindow(servedView.getWindowToken(), 0);
    131 
    132         // Give it five seconds to adjust
    133         newHeight = rootView.getHeight();
    134         long timeoutTime = SystemClock.uptimeMillis() + WAIT_FOR_IME;
    135         while (Math.abs(newHeight - origHeight) < IME_MIN_HEIGHT && SystemClock.uptimeMillis() < timeoutTime) {
    136             newHeight = rootView.getHeight();
    137         }
    138 
    139         return (Math.abs(origHeight - newHeight) >= IME_MIN_HEIGHT);
    140     }
    141 
    142     void pause(int millis) {
    143         try {
    144             Thread.sleep(millis);
    145         } catch (InterruptedException e) {
    146         }
    147     }
    148 
    149 }
    150