| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.inputmethod.stresstest; |
| 18 |
|
| 19 |
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; |
| 20 |
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
| 21 |
import static android.view.WindowInsetsAnimation.Callback.DISPATCH_MODE_STOP; |
| 22 |
|
| 23 |
import static com.android.inputmethod.stresstest.ImeStressTestUtil.isImeShown; |
| 24 |
import static com.android.inputmethod.stresstest.ImeStressTestUtil.waitOnMainUntil; |
| 25 |
|
| 26 |
import android.app.Activity; |
| 27 |
import android.app.Instrumentation; |
| 28 |
import android.content.Intent; |
| 29 |
import android.os.Bundle; |
| 30 |
import android.os.SystemClock; |
| 31 |
import android.platform.test.annotations.RootPermissionTest; |
| 32 |
import android.platform.test.rule.UnlockScreenRule; |
| 33 |
import android.util.Log; |
| 34 |
import android.view.WindowInsets; |
| 35 |
import android.view.WindowInsetsAnimation; |
| 36 |
import android.view.inputmethod.InputMethodManager; |
| 37 |
import android.widget.EditText; |
| 38 |
import android.widget.LinearLayout; |
| 39 |
|
| 40 |
import androidx.annotation.Nullable; |
| 41 |
import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 42 |
import androidx.test.platform.app.InstrumentationRegistry; |
| 43 |
|
| 44 |
import org.junit.Before; |
| 45 |
import org.junit.Rule; |
| 46 |
import org.junit.Test; |
| 47 |
import org.junit.runner.RunWith; |
| 48 |
|
| 49 |
import java.util.ArrayList; |
| 50 |
import java.util.List; |
| 51 |
|
| 52 |
@RootPermissionTest |
| 53 |
@RunWith(AndroidJUnit4.class) |
| 54 |
public final class ImeOpenCloseStressTest { |
| 55 |
|
| 56 |
private static final String TAG = "ImeOpenCloseStressTest"; |
| 57 |
private static final int NUM_TEST_ITERATIONS = 10; |
| 58 |
|
| 59 |
@Rule |
| 60 |
public UnlockScreenRule mUnlockScreenRule = new UnlockScreenRule(); |
| 61 |
|
| 62 |
@Rule |
| 63 |
public ScreenCaptureRule mScreenCaptureRule = |
| 64 |
new ScreenCaptureRule("/sdcard/InputMethodStressTest"); |
| 65 |
private Instrumentation mInstrumentation; |
| 66 |
|
| 67 |
@Before |
| 68 |
public void setUp() { |
| 69 |
mInstrumentation = InstrumentationRegistry.getInstrumentation(); |
| 70 |
} |
| 71 |
|
| 72 |
@Test |
| 73 |
public void testShowHide_waitingVisibilityChange() { |
| 74 |
TestActivity activity = TestActivity.start(); |
| 75 |
EditText editText = activity.getEditText(); |
| 76 |
waitOnMainUntil("activity should gain focus", editText::hasWindowFocus); |
| 77 |
for (int i = 0; i < NUM_TEST_ITERATIONS; i++) { |
| 78 |
String msgPrefix = "Iteration #" + i + " "; |
| 79 |
Log.i(TAG, msgPrefix + "start"); |
| 80 |
mInstrumentation.runOnMainSync(activity::showIme); |
| 81 |
waitOnMainUntil(msgPrefix + "IME should be visible", () -> isImeShown(editText)); |
| 82 |
mInstrumentation.runOnMainSync(activity::hideIme); |
| 83 |
waitOnMainUntil(msgPrefix + "IME should be hidden", () -> !isImeShown(editText)); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
@Test |
| 88 |
public void testShowHide_waitingAnimationEnd() { |
| 89 |
TestActivity activity = TestActivity.start(); |
| 90 |
activity.enableAnimationMonitoring(); |
| 91 |
EditText editText = activity.getEditText(); |
| 92 |
waitOnMainUntil("activity should gain focus", editText::hasWindowFocus); |
| 93 |
for (int i = 0; i < NUM_TEST_ITERATIONS; i++) { |
| 94 |
String msgPrefix = "Iteration #" + i + " "; |
| 95 |
Log.i(TAG, msgPrefix + "start"); |
| 96 |
mInstrumentation.runOnMainSync(activity::showIme); |
| 97 |
waitOnMainUntil(msgPrefix + "IME should be visible", |
| 98 |
() -> !activity.isAnimating() && isImeShown(editText)); |
| 99 |
mInstrumentation.runOnMainSync(activity::hideIme); |
| 100 |
waitOnMainUntil(msgPrefix + "IME should be hidden", |
| 101 |
() -> !activity.isAnimating() && !isImeShown(editText)); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
@Test |
| 106 |
public void testShowHide_intervalAfterHide() { |
| 107 |
|
| 108 |
TestActivity activity = TestActivity.start(); |
| 109 |
EditText editText = activity.getEditText(); |
| 110 |
|
| 111 |
List<Integer> intervals = new ArrayList<>(); |
| 112 |
for (int i = 10; i < 100; i += 10) intervals.add(i); |
| 113 |
for (int i = 100; i < 1000; i += 50) intervals.add(i); |
| 114 |
waitOnMainUntil("activity should gain focus", editText::hasWindowFocus); |
| 115 |
for (int intervalMillis : intervals) { |
| 116 |
String msgPrefix = "Interval = " + intervalMillis + " "; |
| 117 |
Log.i(TAG, msgPrefix + " start"); |
| 118 |
mInstrumentation.runOnMainSync(activity::hideIme); |
| 119 |
SystemClock.sleep(intervalMillis); |
| 120 |
mInstrumentation.runOnMainSync(activity::showIme); |
| 121 |
waitOnMainUntil(msgPrefix + "IME should be visible", |
| 122 |
() -> isImeShown(editText)); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
@Test |
| 127 |
public void testShowHideInSameFrame() { |
| 128 |
TestActivity activity = TestActivity.start(); |
| 129 |
activity.enableAnimationMonitoring(); |
| 130 |
EditText editText = activity.getEditText(); |
| 131 |
waitOnMainUntil("activity should gain focus", editText::hasWindowFocus); |
| 132 |
|
| 133 |
|
| 134 |
mInstrumentation.runOnMainSync(() -> { |
| 135 |
Log.i(TAG, "Calling showIme() and hideIme()"); |
| 136 |
activity.showIme(); |
| 137 |
activity.hideIme(); |
| 138 |
}); |
| 139 |
|
| 140 |
SystemClock.sleep(1000); |
| 141 |
mInstrumentation.waitForIdleSync(); |
| 142 |
waitOnMainUntil("IME should be invisible after show/hide", () -> !isImeShown(editText)); |
| 143 |
|
| 144 |
mInstrumentation.runOnMainSync(activity::showIme); |
| 145 |
waitOnMainUntil("IME should be visible", |
| 146 |
() -> !activity.isAnimating() && isImeShown(editText)); |
| 147 |
mInstrumentation.waitForIdleSync(); |
| 148 |
|
| 149 |
|
| 150 |
mInstrumentation.runOnMainSync(() -> { |
| 151 |
Log.i(TAG, "Calling hideIme() and showIme()"); |
| 152 |
activity.hideIme(); |
| 153 |
activity.showIme(); |
| 154 |
}); |
| 155 |
|
| 156 |
SystemClock.sleep(1000); |
| 157 |
mInstrumentation.waitForIdleSync(); |
| 158 |
waitOnMainUntil("IME should be visible after hide/show", |
| 159 |
() -> !activity.isAnimating() && isImeShown(editText)); |
| 160 |
} |
| 161 |
|
| 162 |
public static class TestActivity extends Activity { |
| 163 |
|
| 164 |
private EditText mEditText; |
| 165 |
private boolean mIsAnimating; |
| 166 |
|
| 167 |
private final WindowInsetsAnimation.Callback mWindowInsetsAnimationCallback = |
| 168 |
new WindowInsetsAnimation.Callback(DISPATCH_MODE_STOP) { |
| 169 |
@Override |
| 170 |
public WindowInsetsAnimation.Bounds onStart(WindowInsetsAnimation animation, |
| 171 |
WindowInsetsAnimation.Bounds bounds) { |
| 172 |
mIsAnimating = true; |
| 173 |
return super.onStart(animation, bounds); |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public void onEnd(WindowInsetsAnimation animation) { |
| 178 |
super.onEnd(animation); |
| 179 |
mIsAnimating = false; |
| 180 |
} |
| 181 |
|
| 182 |
@Override |
| 183 |
public WindowInsets onProgress(WindowInsets insets, |
| 184 |
List<WindowInsetsAnimation> runningAnimations) { |
| 185 |
return insets; |
| 186 |
} |
| 187 |
}; |
| 188 |
|
| 189 |
public static TestActivity start() { |
| 190 |
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); |
| 191 |
Intent intent = new Intent() |
| 192 |
.setAction(Intent.ACTION_MAIN) |
| 193 |
.setClass(instrumentation.getContext(), TestActivity.class) |
| 194 |
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| 195 |
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 196 |
return (TestActivity) instrumentation.startActivitySync(intent); |
| 197 |
} |
| 198 |
|
| 199 |
@Override |
| 200 |
protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 201 |
super.onCreate(savedInstanceState); |
| 202 |
LinearLayout rootView = new LinearLayout(this); |
| 203 |
rootView.setOrientation(LinearLayout.VERTICAL); |
| 204 |
mEditText = new EditText(this); |
| 205 |
rootView.addView(mEditText, new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)); |
| 206 |
setContentView(rootView); |
| 207 |
} |
| 208 |
|
| 209 |
public EditText getEditText() { |
| 210 |
return mEditText; |
| 211 |
} |
| 212 |
|
| 213 |
public void showIme() { |
| 214 |
Log.i(TAG, "TestActivity.showIme"); |
| 215 |
mEditText.requestFocus(); |
| 216 |
InputMethodManager imm = getSystemService(InputMethodManager.class); |
| 217 |
imm.showSoftInput(mEditText, 0); |
| 218 |
} |
| 219 |
|
| 220 |
public void hideIme() { |
| 221 |
Log.i(TAG, "TestActivity.hideIme"); |
| 222 |
InputMethodManager imm = getSystemService(InputMethodManager.class); |
| 223 |
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0); |
| 224 |
} |
| 225 |
|
| 226 |
public void enableAnimationMonitoring() { |
| 227 |
|
| 228 |
|
| 229 |
InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { |
| 230 |
getWindow().setDecorFitsSystemWindows(false); |
| 231 |
mEditText.setWindowInsetsAnimationCallback(mWindowInsetsAnimationCallback); |
| 232 |
}); |
| 233 |
} |
| 234 |
|
| 235 |
public boolean isAnimating() { |
| 236 |
return mIsAnimating; |
| 237 |
} |
| 238 |
} |
| 239 |
} |