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