Merge "Fix broken Backspace/ForwardDelete tests" into oc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
2880a4b41f
@@ -16,10 +16,13 @@
|
||||
|
||||
package android.text.method;
|
||||
|
||||
import android.support.test.InstrumentationRegistry;
|
||||
import android.support.test.filters.SmallTest;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
import android.text.InputType;
|
||||
import android.util.KeyUtils;
|
||||
import android.view.KeyEvent;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView.BufferType;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -33,13 +36,21 @@ import org.junit.runner.RunWith;
|
||||
*/
|
||||
@SmallTest
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class BackspaceTest extends KeyListenerTestCase {
|
||||
public class BackspaceTest {
|
||||
private EditText mTextView;
|
||||
|
||||
private static final BaseKeyListener mKeyListener = new BaseKeyListener() {
|
||||
public int getInputType() {
|
||||
return InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL;
|
||||
}
|
||||
};
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mTextView = new EditText(InstrumentationRegistry.getInstrumentation().getContext());
|
||||
}
|
||||
|
||||
|
||||
// Sync the state to the TextView and call onKeyDown with KEYCODE_DEL key event.
|
||||
// Then update the state to the result of TextView.
|
||||
private void backspace(final EditorState state, int modifiers) {
|
||||
@@ -47,7 +58,8 @@ public class BackspaceTest extends KeyListenerTestCase {
|
||||
mTextView.setKeyListener(mKeyListener);
|
||||
mTextView.setSelection(state.mSelectionStart, state.mSelectionEnd);
|
||||
|
||||
final KeyEvent keyEvent = getKey(KeyEvent.KEYCODE_DEL, modifiers);
|
||||
final KeyEvent keyEvent = KeyUtils.generateKeyEvent(
|
||||
KeyEvent.KEYCODE_DEL, KeyEvent.ACTION_DOWN, modifiers);
|
||||
mTextView.onKeyDown(keyEvent.getKeyCode(), keyEvent);
|
||||
|
||||
state.mText = mTextView.getText();
|
||||
|
||||
@@ -16,10 +16,13 @@
|
||||
|
||||
package android.text.method;
|
||||
|
||||
import android.support.test.InstrumentationRegistry;
|
||||
import android.support.test.filters.SmallTest;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
import android.text.InputType;
|
||||
import android.util.KeyUtils;
|
||||
import android.view.KeyEvent;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView.BufferType;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -34,13 +37,20 @@ import org.junit.runner.RunWith;
|
||||
*/
|
||||
@SmallTest
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class ForwardDeleteTest extends KeyListenerTestCase {
|
||||
public class ForwardDeleteTest {
|
||||
private EditText mTextView;
|
||||
|
||||
private static final BaseKeyListener mKeyListener = new BaseKeyListener() {
|
||||
public int getInputType() {
|
||||
return InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL;
|
||||
}
|
||||
};
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mTextView = new EditText(InstrumentationRegistry.getInstrumentation().getContext());
|
||||
}
|
||||
|
||||
// Sync the state to the TextView and call onKeyDown with KEYCODE_FORWARD_DEL key event.
|
||||
// Then update the state to the result of TextView.
|
||||
private void forwardDelete(final EditorState state, int modifiers) {
|
||||
@@ -48,7 +58,8 @@ public class ForwardDeleteTest extends KeyListenerTestCase {
|
||||
mTextView.setKeyListener(mKeyListener);
|
||||
mTextView.setSelection(state.mSelectionStart, state.mSelectionEnd);
|
||||
|
||||
final KeyEvent keyEvent = getKey(KeyEvent.KEYCODE_FORWARD_DEL, modifiers);
|
||||
final KeyEvent keyEvent = KeyUtils.generateKeyEvent(
|
||||
KeyEvent.KEYCODE_FORWARD_DEL, KeyEvent.ACTION_DOWN, modifiers);
|
||||
mTextView.onKeyDown(keyEvent.getKeyCode(), keyEvent);
|
||||
|
||||
state.mText = mTextView.getText();
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package android.text.method;
|
||||
|
||||
import android.app.Instrumentation;
|
||||
import android.support.test.InstrumentationRegistry;
|
||||
import android.view.KeyEvent;
|
||||
import android.widget.EditText;
|
||||
|
||||
public abstract class KeyListenerTestCase {
|
||||
protected Instrumentation mInstrumentation;
|
||||
protected EditText mTextView;
|
||||
|
||||
public KeyListenerTestCase() {
|
||||
}
|
||||
|
||||
protected void setup() {
|
||||
mInstrumentation = InstrumentationRegistry.getInstrumentation();
|
||||
mTextView = new EditText(mInstrumentation.getContext());
|
||||
}
|
||||
|
||||
protected static KeyEvent getKey(int keycode, int metaState) {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
return new KeyEvent(currentTime, currentTime, KeyEvent.ACTION_DOWN, keycode,
|
||||
0 /* repeat */, metaState);
|
||||
}
|
||||
}
|
||||
@@ -85,4 +85,19 @@ public class KeyUtils {
|
||||
}
|
||||
inst.sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a {@link KeyEvent}.
|
||||
*
|
||||
* @param keycode The integer keycode for the event to be generated.
|
||||
* @param keyEventAction The integer {@link KeyEvent} action code.
|
||||
* @param metaState Flags indicating which meta keys are currently pressed.
|
||||
*
|
||||
* @return a new {@link KeyEvent} for current time.
|
||||
*/
|
||||
public static KeyEvent generateKeyEvent(int keycode, int keyEventAction, int metaState) {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
return new KeyEvent(currentTime, currentTime, keyEventAction, keycode,
|
||||
0 /* repeat */, metaState);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user