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