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