Add selection handle multiline dragging tests.
Bug: 25730231 Change-Id: I23b9e35d7a5ea8d03d8634ffca0e186ea76ebc99
This commit is contained in:
@@ -186,4 +186,29 @@ public class TextViewActivityTest extends ActivityInstrumentationTestCase2<TextV
|
||||
.perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('k') + 1));
|
||||
onView(withId(R.id.textview)).check(hasSelection("abcd efg hijk"));
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
public void testSelectionHandles_multiLine() throws Exception {
|
||||
final String text = "abcd\n" + "efg\n" + "hijk\n" + "lmn\n" + "opqr";
|
||||
onView(withId(R.id.textview)).perform(click());
|
||||
onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(text));
|
||||
onView(withId(R.id.textview)).perform(doubleClickOnTextAtIndex(text.indexOf('i')));
|
||||
|
||||
final TextView textView = (TextView)getActivity().findViewById(R.id.textview);
|
||||
onHandleView(com.android.internal.R.id.selection_start_handle)
|
||||
.perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('e')));
|
||||
onView(withId(R.id.textview)).check(hasSelection("efg\nhijk"));
|
||||
|
||||
onHandleView(com.android.internal.R.id.selection_start_handle)
|
||||
.perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('a')));
|
||||
onView(withId(R.id.textview)).check(hasSelection("abcd\nefg\nhijk"));
|
||||
|
||||
onHandleView(com.android.internal.R.id.selection_end_handle)
|
||||
.perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('n') + 1));
|
||||
onView(withId(R.id.textview)).check(hasSelection("abcd\nefg\nhijk\nlmn"));
|
||||
|
||||
onHandleView(com.android.internal.R.id.selection_end_handle)
|
||||
.perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('r') + 1));
|
||||
onView(withId(R.id.textview)).check(hasSelection("abcd\nefg\nhijk\nlmn\nopqr"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -252,9 +252,9 @@ public final class DragAction implements ViewAction {
|
||||
private static float[][] interpolate(float[] start, float[] end) {
|
||||
float[][] res = new float[DRAG_STEP_COUNT][2];
|
||||
|
||||
for (int i = 1; i < DRAG_STEP_COUNT + 1; i++) {
|
||||
res[i - 1][0] = start[0] + (end[0] - start[0]) * i / (DRAG_STEP_COUNT + 2f);
|
||||
res[i - 1][1] = start[1] + (end[1] - start[1]) * i / (DRAG_STEP_COUNT + 2f);
|
||||
for (int i = 0; i < DRAG_STEP_COUNT; i++) {
|
||||
res[i][0] = start[0] + (end[0] - start[0]) * i / (DRAG_STEP_COUNT - 1f);
|
||||
res[i][1] = start[1] + (end[1] - start[1]) * i / (DRAG_STEP_COUNT - 1f);
|
||||
}
|
||||
|
||||
return res;
|
||||
|
||||
@@ -184,6 +184,8 @@ public final class TextViewActions {
|
||||
* text view.
|
||||
*/
|
||||
private static final class HandleCoordinates implements CoordinatesProvider {
|
||||
// Must be larger than Editor#LINE_SLOP_MULTIPLIER_FOR_HANDLEVIEWS.
|
||||
private final static float LINE_SLOP_MULTIPLIER = 0.6f;
|
||||
private final TextView mTextView;
|
||||
private final Handle mHandleType;
|
||||
private final int mIndex;
|
||||
@@ -213,6 +215,11 @@ public final class TextViewActions {
|
||||
private float[] locateHandlePointsTextIndex(View view) {
|
||||
final int currentOffset = mHandleType == Handle.SELECTION_START ?
|
||||
mTextView.getSelectionStart() : mTextView.getSelectionEnd();
|
||||
|
||||
final Layout layout = mTextView.getLayout();
|
||||
final int currentLine = layout.getLineForOffset(currentOffset);
|
||||
final int targetLine = layout.getLineForOffset(mIndex);
|
||||
|
||||
final float[] currentCoordinates =
|
||||
(new TextCoordinates(currentOffset)).calculateCoordinates(mTextView);
|
||||
final float[] targetCoordinates =
|
||||
@@ -220,7 +227,13 @@ public final class TextViewActions {
|
||||
final Rect bounds = new Rect();
|
||||
view.getBoundsOnScreen(bounds);
|
||||
final float diffX = bounds.centerX() - currentCoordinates[0];
|
||||
final float diffY = bounds.centerY() - currentCoordinates[1];
|
||||
final float verticalOffset = bounds.height() * 0.7f;
|
||||
float diffY = bounds.top + verticalOffset - currentCoordinates[1];
|
||||
if (currentLine > targetLine) {
|
||||
diffY -= mTextView.getLineHeight() * LINE_SLOP_MULTIPLIER;
|
||||
} else if (currentLine < targetLine) {
|
||||
diffY += mTextView.getLineHeight() * LINE_SLOP_MULTIPLIER;
|
||||
}
|
||||
return new float[] {targetCoordinates[0] + diffX, targetCoordinates[1] + diffY};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user