blob: a9f251e5c3a77abc140917f4bd81bba6f2263c76 [file] [log] [blame]
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -08001/*
2 * Copyright (C) 2019 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 android.widget;
18
Shu Chenafbcf852020-03-10 08:19:07 +080019import static android.text.Spanned.SPAN_INCLUSIVE_EXCLUSIVE;
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -080020import static android.widget.espresso.TextViewActions.clickOnTextAtIndex;
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -080021import static android.widget.espresso.TextViewActions.dragOnText;
22import static android.widget.espresso.TextViewAssertions.hasInsertionPointerAtIndex;
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -080023import static android.widget.espresso.TextViewAssertions.hasSelection;
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -080024
25import static androidx.test.espresso.Espresso.onView;
26import static androidx.test.espresso.action.ViewActions.replaceText;
27import static androidx.test.espresso.matcher.ViewMatchers.withId;
28
Nikita Dubrovsky7c583592020-02-16 15:54:23 -080029import static com.google.common.truth.Truth.assertThat;
30import static com.google.common.truth.Truth.assertWithMessage;
31
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -080032import static org.hamcrest.Matchers.emptyString;
33import static org.hamcrest.Matchers.not;
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -080034import static org.junit.Assert.assertFalse;
35import static org.junit.Assert.assertTrue;
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -080036
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -080037import android.app.Activity;
38import android.app.Instrumentation;
Shu Chen77003422020-03-05 13:38:05 +080039import android.graphics.Rect;
Nikita Dubrovsky256ee072020-03-20 09:51:27 -070040import android.platform.test.annotations.Presubmit;
Nikita Dubrovsky7c583592020-02-16 15:54:23 -080041import android.text.Layout;
Shu Chenafbcf852020-03-10 08:19:07 +080042import android.text.Spannable;
43import android.text.SpannableString;
44import android.text.style.AbsoluteSizeSpan;
Shu Chen77003422020-03-05 13:38:05 +080045import android.util.ArraySet;
Nikita Dubrovsky7c583592020-02-16 15:54:23 -080046import android.util.Log;
Nikita Dubrovskybd50f3b2020-01-11 20:14:05 -080047import android.view.InputDevice;
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -080048import android.view.MotionEvent;
Shu Chen77003422020-03-05 13:38:05 +080049import android.view.View;
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -080050
51import androidx.test.InstrumentationRegistry;
Nikita Dubrovsky256ee072020-03-20 09:51:27 -070052import androidx.test.filters.MediumTest;
Nikita Dubrovsky7c583592020-02-16 15:54:23 -080053import androidx.test.filters.Suppress;
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -080054import androidx.test.rule.ActivityTestRule;
55import androidx.test.runner.AndroidJUnit4;
56
57import com.android.frameworks.coretests.R;
58
59import com.google.common.base.Strings;
60
Shu Chen77003422020-03-05 13:38:05 +080061import org.junit.After;
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -080062import org.junit.Before;
63import org.junit.Rule;
64import org.junit.Test;
65import org.junit.runner.RunWith;
66
Shu Chen77003422020-03-05 13:38:05 +080067import java.util.Set;
Nikita Dubrovsky7c583592020-02-16 15:54:23 -080068import java.util.concurrent.atomic.AtomicLong;
69
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -080070@RunWith(AndroidJUnit4.class)
Nikita Dubrovsky256ee072020-03-20 09:51:27 -070071@MediumTest
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -080072public class EditorCursorDragTest {
Nikita Dubrovsky7c583592020-02-16 15:54:23 -080073 private static final String LOG_TAG = EditorCursorDragTest.class.getSimpleName();
74
75 private static final AtomicLong sTicker = new AtomicLong(1);
76
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -080077 @Rule
78 public ActivityTestRule<TextViewActivity> mActivityRule = new ActivityTestRule<>(
79 TextViewActivity.class);
80
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -080081 private Instrumentation mInstrumentation;
82 private Activity mActivity;
Shu Chen77003422020-03-05 13:38:05 +080083 private Set<MotionEvent> mMotionEvents = new ArraySet<>();
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -080084
85 @Before
86 public void before() throws Throwable {
87 mInstrumentation = InstrumentationRegistry.getInstrumentation();
88 mActivity = mActivityRule.getActivity();
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -080089 }
90
Shu Chen77003422020-03-05 13:38:05 +080091 @After
92 public void after() throws Throwable {
93 for (MotionEvent event : mMotionEvents) {
94 event.recycle();
95 }
96 mMotionEvents.clear();
97 }
98
Nikita Dubrovskycac70802020-04-09 23:06:38 -070099 @Presubmit
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800100 @Test
101 public void testCursorDrag_horizontal_whenTextViewContentsFitOnScreen() throws Throwable {
102 String text = "Hello world!";
103 onView(withId(R.id.textview)).perform(replaceText(text));
104 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
105
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800106 // Swipe left to right to drag the cursor. The cursor should end up at the position where
107 // the finger is lifted.
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800108 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("llo"), text.indexOf("!")));
109 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(11));
110
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800111 // Swipe right to left to drag the cursor. The cursor should end up at the position where
112 // the finger is lifted.
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800113 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("!"), text.indexOf("llo")));
114 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(2));
115 }
116
117 @Test
118 public void testCursorDrag_horizontal_whenTextViewContentsLargerThanScreen() throws Throwable {
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800119 String text = "Hello world!\n\n"
120 + Strings.repeat("Bla\n\n", 200) + "Bye";
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800121 onView(withId(R.id.textview)).perform(replaceText(text));
122 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
123
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800124 // Swipe left to right to drag the cursor. The cursor should end up at the position where
125 // the finger is lifted.
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800126 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("llo"), text.indexOf("!")));
127 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(11));
128
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800129 // Swipe right to left to drag the cursor. The cursor should end up at the position where
130 // the finger is lifted.
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800131 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("!"), text.indexOf("llo")));
132 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(2));
133 }
134
135 @Test
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800136 public void testCursorDrag_diagonal_whenTextViewContentsFitOnScreen() throws Throwable {
137 StringBuilder sb = new StringBuilder();
138 for (int i = 1; i <= 9; i++) {
139 sb.append("line").append(i).append("\n");
140 }
141 String text = sb.toString();
142 onView(withId(R.id.textview)).perform(replaceText(text));
143 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
144
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800145 // Swipe along a diagonal path. This should drag the cursor. Because we snap the finger to
146 // the handle as the touch moves downwards (and because we have some slop to avoid jumping
147 // across lines), the cursor position will end up higher than the finger position.
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800148 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("line1"), text.indexOf("3")));
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800149 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(text.indexOf("1")));
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800150
151 // Swipe right-down along a very steep diagonal path. This should not drag the cursor.
152 // Normally this would trigger a scroll, but since the full view fits on the screen there
153 // is nothing to scroll and the gesture will trigger a selection drag.
154 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("line1"), text.indexOf("7")));
155 onView(withId(R.id.textview)).check(hasSelection(not(emptyString())));
156
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800157 // Tap to clear the selection.
158 int index = text.indexOf("line9");
159 onView(withId(R.id.textview)).perform(clickOnTextAtIndex(index));
160 onView(withId(R.id.textview)).check(hasSelection(emptyString()));
161 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(index));
162
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800163 // Swipe right-up along a very steep diagonal path. This should not drag the cursor.
164 // Normally this would trigger a scroll, but since the full view fits on the screen there
165 // is nothing to scroll and the gesture will trigger a selection drag.
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800166 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("line7"), text.indexOf("1")));
167 onView(withId(R.id.textview)).check(hasSelection(not(emptyString())));
168 }
169
170 @Test
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800171 public void testCursorDrag_diagonal_whenTextViewContentsLargerThanScreen() throws Throwable {
172 StringBuilder sb = new StringBuilder();
173 for (int i = 1; i <= 9; i++) {
174 sb.append("line").append(i).append("\n");
175 }
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800176 sb.append(Strings.repeat("0123456789\n", 400)).append("Last");
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800177 String text = sb.toString();
178 onView(withId(R.id.textview)).perform(replaceText(text));
179 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
180
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800181 // Swipe along a diagonal path. This should drag the cursor. Because we snap the finger to
182 // the handle as the touch moves downwards (and because we have some slop to avoid jumping
183 // across lines), the cursor position will end up higher than the finger position.
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800184 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("line1"), text.indexOf("3")));
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800185 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(text.indexOf("1")));
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800186
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800187 // Swipe right-down along a very steep diagonal path. This should not drag the cursor.
188 // Normally this would trigger a scroll up, but since the view is already at the top there
189 // is nothing to scroll and the gesture will trigger a selection drag.
190 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("line1"), text.indexOf("7")));
191 onView(withId(R.id.textview)).check(hasSelection(not(emptyString())));
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800192
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800193 // Tap to clear the selection.
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800194 int index = text.indexOf("line9");
195 onView(withId(R.id.textview)).perform(clickOnTextAtIndex(index));
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800196 onView(withId(R.id.textview)).check(hasSelection(emptyString()));
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800197 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(index));
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800198
199 // Swipe right-up along a very steep diagonal path. This should not drag the cursor. This
200 // will trigger a downward scroll and the cursor position will not change.
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800201 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("line7"), text.indexOf("1")));
202 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(index));
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800203 }
204
205 @Test
206 public void testCursorDrag_vertical_whenTextViewContentsFitOnScreen() throws Throwable {
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800207 String text = "012345_aaa\n"
208 + "0123456789\n"
209 + "012345_bbb\n"
210 + "0123456789\n"
211 + "012345_ccc\n"
212 + "0123456789\n"
213 + "012345_ddd";
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800214 onView(withId(R.id.textview)).perform(replaceText(text));
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800215
216 // Swipe up vertically. This should not drag the cursor. Since there's also nothing to
217 // scroll, the gesture will trigger a selection drag.
218 onView(withId(R.id.textview)).perform(clickOnTextAtIndex(0));
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800219 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800220 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("bbb"), text.indexOf("aaa")));
221 onView(withId(R.id.textview)).check(hasSelection(not(emptyString())));
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800222
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800223 // Swipe down vertically. This should not drag the cursor. Since there's also nothing to
224 // scroll, the gesture will trigger a selection drag.
225 onView(withId(R.id.textview)).perform(clickOnTextAtIndex(0));
226 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
227 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("ccc"), text.indexOf("ddd")));
228 onView(withId(R.id.textview)).check(hasSelection(not(emptyString())));
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800229 }
230
231 @Test
232 public void testCursorDrag_vertical_whenTextViewContentsLargerThanScreen() throws Throwable {
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800233 String text = "012345_aaa\n"
234 + "0123456789\n"
235 + "012345_bbb\n"
236 + "0123456789\n"
237 + "012345_ccc\n"
238 + "0123456789\n"
239 + "012345_ddd\n"
240 + Strings.repeat("0123456789\n", 400) + "012345_zzz";
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800241 onView(withId(R.id.textview)).perform(replaceText(text));
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800242 onView(withId(R.id.textview)).perform(clickOnTextAtIndex(text.indexOf("ddd")));
243 int initialCursorPosition = text.indexOf("ddd");
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800244 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(initialCursorPosition));
245
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800246 // Swipe up vertically. This should trigger a downward scroll.
247 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("bbb"), text.indexOf("aaa")));
248 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(initialCursorPosition));
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800249
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800250 // Swipe down vertically. This should trigger an upward scroll.
251 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("ccc"), text.indexOf("ddd")));
252 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(initialCursorPosition));
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800253 }
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800254
255 @Test
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800256 public void testEditor_onTouchEvent_quickTapAfterDrag() throws Throwable {
257 String text = "Hi world!";
258 onView(withId(R.id.textview)).perform(replaceText(text));
259 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
260
261 TextView tv = mActivity.findViewById(R.id.textview);
262 Editor editor = tv.getEditorForTesting();
263
264 // Simulate a tap-and-drag gesture.
265 long event1Time = 1001;
Shu Chen77003422020-03-05 13:38:05 +0800266 MotionEvent event1 = downEvent(tv, event1Time, event1Time, 5f, 10f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800267 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event1));
268 assertFalse(editor.getInsertionController().isCursorBeingModified());
269 assertFalse(editor.getSelectionController().isCursorBeingModified());
270
271 long event2Time = 1002;
Shu Chen77003422020-03-05 13:38:05 +0800272 MotionEvent event2 = moveEvent(tv, event1Time, event2Time, 50f, 10f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800273 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event2));
274 assertTrue(editor.getInsertionController().isCursorBeingModified());
275 assertFalse(editor.getSelectionController().isCursorBeingModified());
276
277 long event3Time = 1003;
Shu Chen77003422020-03-05 13:38:05 +0800278 MotionEvent event3 = moveEvent(tv, event1Time, event3Time, 100f, 10f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800279 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event3));
280 assertTrue(editor.getInsertionController().isCursorBeingModified());
281 assertFalse(editor.getSelectionController().isCursorBeingModified());
282
283 long event4Time = 2004;
Shu Chen77003422020-03-05 13:38:05 +0800284 MotionEvent event4 = upEvent(tv, event1Time, event4Time, 100f, 10f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800285 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event4));
286 assertFalse(editor.getInsertionController().isCursorBeingModified());
287 assertFalse(editor.getSelectionController().isCursorBeingModified());
288
289 // Simulate a quick tap after the drag, near the location where the drag ended.
290 long event5Time = 2005;
Shu Chen77003422020-03-05 13:38:05 +0800291 MotionEvent event5 = downEvent(tv, event5Time, event5Time, 90f, 10f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800292 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event5));
293 assertFalse(editor.getInsertionController().isCursorBeingModified());
294 assertFalse(editor.getSelectionController().isCursorBeingModified());
295
296 long event6Time = 2006;
Shu Chen77003422020-03-05 13:38:05 +0800297 MotionEvent event6 = upEvent(tv, event5Time, event6Time, 90f, 10f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800298 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event6));
299 assertFalse(editor.getInsertionController().isCursorBeingModified());
300 assertFalse(editor.getSelectionController().isCursorBeingModified());
301
302 // Simulate another quick tap in the same location; now selection should be triggered.
303 long event7Time = 2007;
Shu Chen77003422020-03-05 13:38:05 +0800304 MotionEvent event7 = downEvent(tv, event7Time, event7Time, 90f, 10f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800305 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event7));
306 assertFalse(editor.getInsertionController().isCursorBeingModified());
307 assertTrue(editor.getSelectionController().isCursorBeingModified());
308 }
309
310 @Test
Nikita Dubrovskybd50f3b2020-01-11 20:14:05 -0800311 public void testEditor_onTouchEvent_mouseDrag() throws Throwable {
312 String text = "testEditor_onTouchEvent_mouseDrag";
313 onView(withId(R.id.textview)).perform(replaceText(text));
314 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
315
316 TextView tv = mActivity.findViewById(R.id.textview);
317 Editor editor = tv.getEditorForTesting();
318
319 // Simulate a mouse click and drag. This should NOT trigger a cursor drag.
320 long event1Time = 1001;
Shu Chen77003422020-03-05 13:38:05 +0800321 MotionEvent event1 = mouseDownEvent(tv, event1Time, event1Time, 20f, 30f);
Nikita Dubrovskybd50f3b2020-01-11 20:14:05 -0800322 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event1));
323 assertFalse(editor.getInsertionController().isCursorBeingModified());
324 assertFalse(editor.getSelectionController().isCursorBeingModified());
325
326 long event2Time = 1002;
Shu Chen77003422020-03-05 13:38:05 +0800327 MotionEvent event2 = mouseMoveEvent(tv, event1Time, event2Time, 120f, 30f);
Nikita Dubrovskybd50f3b2020-01-11 20:14:05 -0800328 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event2));
329 assertFalse(editor.getInsertionController().isCursorBeingModified());
330 assertTrue(editor.getSelectionController().isCursorBeingModified());
331
332 long event3Time = 1003;
Shu Chen77003422020-03-05 13:38:05 +0800333 MotionEvent event3 = mouseUpEvent(tv, event1Time, event3Time, 120f, 30f);
Nikita Dubrovskybd50f3b2020-01-11 20:14:05 -0800334 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event3));
335 assertFalse(editor.getInsertionController().isCursorBeingModified());
336 assertFalse(editor.getSelectionController().isCursorBeingModified());
337 }
338
339 @Test
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800340 public void testEditor_onTouchEvent_cursorDrag() throws Throwable {
341 String text = "testEditor_onTouchEvent_cursorDrag";
342 onView(withId(R.id.textview)).perform(replaceText(text));
343 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
344
345 TextView tv = mActivity.findViewById(R.id.textview);
346 Editor editor = tv.getEditorForTesting();
347
348 // Simulate a tap-and-drag gesture. This should trigger a cursor drag.
349 long event1Time = 1001;
Shu Chen77003422020-03-05 13:38:05 +0800350 MotionEvent event1 = downEvent(tv, event1Time, event1Time, 20f, 30f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800351 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event1));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800352 assertFalse(editor.getInsertionController().isCursorBeingModified());
353 assertFalse(editor.getSelectionController().isCursorBeingModified());
354
355 long event2Time = 1002;
Shu Chen77003422020-03-05 13:38:05 +0800356 MotionEvent event2 = moveEvent(tv, event1Time, event2Time, 21f, 30f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800357 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event2));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800358 assertFalse(editor.getInsertionController().isCursorBeingModified());
359 assertFalse(editor.getSelectionController().isCursorBeingModified());
360
361 long event3Time = 1003;
Shu Chen77003422020-03-05 13:38:05 +0800362 MotionEvent event3 = moveEvent(tv, event1Time, event3Time, 120f, 30f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800363 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event3));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800364 assertTrue(editor.getInsertionController().isCursorBeingModified());
365 assertFalse(editor.getSelectionController().isCursorBeingModified());
366
367 long event4Time = 1004;
Shu Chen77003422020-03-05 13:38:05 +0800368 MotionEvent event4 = upEvent(tv, event1Time, event4Time, 120f, 30f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800369 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event4));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800370 assertFalse(editor.getInsertionController().isCursorBeingModified());
371 assertFalse(editor.getSelectionController().isCursorBeingModified());
372 }
373
374 @Test
375 public void testEditor_onTouchEvent_selectionDrag() throws Throwable {
376 String text = "testEditor_onTouchEvent_selectionDrag";
377 onView(withId(R.id.textview)).perform(replaceText(text));
378 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
379
380 TextView tv = mActivity.findViewById(R.id.textview);
381 Editor editor = tv.getEditorForTesting();
382
383 // Simulate a double-tap followed by a drag. This should trigger a selection drag.
384 long event1Time = 1001;
Shu Chen77003422020-03-05 13:38:05 +0800385 MotionEvent event1 = downEvent(tv, event1Time, event1Time, 20f, 30f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800386 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event1));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800387 assertFalse(editor.getInsertionController().isCursorBeingModified());
388 assertFalse(editor.getSelectionController().isCursorBeingModified());
389
390 long event2Time = 1002;
Shu Chen77003422020-03-05 13:38:05 +0800391 MotionEvent event2 = upEvent(tv, event1Time, event2Time, 20f, 30f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800392 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event2));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800393 assertFalse(editor.getInsertionController().isCursorBeingModified());
394 assertFalse(editor.getSelectionController().isCursorBeingModified());
395
396 long event3Time = 1003;
Shu Chen77003422020-03-05 13:38:05 +0800397 MotionEvent event3 = downEvent(tv, event3Time, event3Time, 20f, 30f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800398 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event3));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800399 assertFalse(editor.getInsertionController().isCursorBeingModified());
400 assertTrue(editor.getSelectionController().isCursorBeingModified());
401
402 long event4Time = 1004;
Shu Chen77003422020-03-05 13:38:05 +0800403 MotionEvent event4 = moveEvent(tv, event3Time, event4Time, 120f, 30f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800404 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event4));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800405 assertFalse(editor.getInsertionController().isCursorBeingModified());
406 assertTrue(editor.getSelectionController().isCursorBeingModified());
407
408 long event5Time = 1005;
Shu Chen77003422020-03-05 13:38:05 +0800409 MotionEvent event5 = upEvent(tv, event3Time, event5Time, 120f, 30f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800410 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event5));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800411 assertFalse(editor.getInsertionController().isCursorBeingModified());
412 assertFalse(editor.getSelectionController().isCursorBeingModified());
413 }
414
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800415 @Suppress // b/149712851
Nikita Dubrovsky99b55fa2020-01-12 20:57:51 -0800416 @Test // Reproduces b/147366705
417 public void testCursorDrag_nonSelectableTextView() throws Throwable {
418 String text = "Hello world!";
419 TextView tv = mActivity.findViewById(R.id.nonselectable_textview);
420 tv.setText(text);
421 Editor editor = tv.getEditorForTesting();
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800422 assertThat(editor).isNotNull();
Nikita Dubrovsky99b55fa2020-01-12 20:57:51 -0800423
424 // Simulate a tap. No error should be thrown.
425 long event1Time = 1001;
Shu Chen77003422020-03-05 13:38:05 +0800426 MotionEvent event1 = downEvent(tv, event1Time, event1Time, 20f, 30f);
Nikita Dubrovsky99b55fa2020-01-12 20:57:51 -0800427 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event1));
428
429 // Swipe left to right. No error should be thrown.
430 onView(withId(R.id.nonselectable_textview)).perform(
431 dragOnText(text.indexOf("llo"), text.indexOf("!")));
432 }
433
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800434 @Test
435 public void testCursorDrag_slop() throws Throwable {
436 String text = "line1: This is the 1st line: A\n"
437 + "line2: This is the 2nd line: B\n"
438 + "line3: This is the 3rd line: C\n";
439 onView(withId(R.id.textview)).perform(replaceText(text));
440 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
441 TextView tv = mActivity.findViewById(R.id.textview);
442
443 // Simulate a drag where the finger moves slightly up and down (above and below the original
444 // line where the drag started). The cursor should just move along the original line without
445 // jumping up or down across lines.
446 MotionEventInfo[] events = new MotionEventInfo[]{
447 // Start dragging along the second line
448 motionEventInfo(text.indexOf("line2"), 1.0f),
449 motionEventInfo(text.indexOf("This is the 2nd"), 1.0f),
450 // Move to the bottom of the first line; cursor should remain on second line
451 motionEventInfo(text.indexOf("he 1st"), 0.0f, text.indexOf("he 2nd")),
452 // Move to the top of the third line; cursor should remain on second line
453 motionEventInfo(text.indexOf("e: C"), 1.0f, text.indexOf("e: B")),
454 motionEventInfo(text.indexOf("B"), 0.0f)
455 };
456 simulateDrag(tv, events, true);
457 }
458
459 @Test
460 public void testCursorDrag_snapToHandle() throws Throwable {
461 String text = "line1: This is the 1st line: A\n"
462 + "line2: This is the 2nd line: B\n"
Shu Chen77003422020-03-05 13:38:05 +0800463 + "line3: This is the 3rd line: C\n"
464 + "line4: This is the 4th line: D\n";
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800465 onView(withId(R.id.textview)).perform(replaceText(text));
466 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
467 TextView tv = mActivity.findViewById(R.id.textview);
468
469 // When the drag motion moves downward, we delay jumping to the lower line to allow the
470 // user's touch to snap to the cursor's handle. Once the finger is over the handle, we
471 // position the cursor above the user's actual touch (offset such that the finger remains
472 // over the handle rather than on top of the cursor vertical bar). This improves the
473 // visibility of the cursor and the text underneath.
474 MotionEventInfo[] events = new MotionEventInfo[]{
475 // Start dragging along the first line
476 motionEventInfo(text.indexOf("line1"), 1.0f),
477 motionEventInfo(text.indexOf("This is the 1st"), 1.0f),
Shu Chen77003422020-03-05 13:38:05 +0800478 // Move to the middle of the fourth line; cursor should end up on second line
479 motionEventInfo(text.indexOf("he 4th"), 0.5f, text.indexOf("he 2nd")),
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800480 // Move to the middle of the second line; cursor should end up on the first line
481 motionEventInfo(text.indexOf("he 2nd"), 0.5f, text.indexOf("he 1st"))
482 };
483 simulateDrag(tv, events, true);
484
485 // If the drag motion hasn't moved downward (ie, we haven't had a chance to snap to the
486 // handle), we position the cursor directly at the touch position.
487 events = new MotionEventInfo[]{
488 // Start dragging along the third line
489 motionEventInfo(text.indexOf("line3"), 1.0f),
490 motionEventInfo(text.indexOf("This is the 3rd"), 1.0f),
491 // Move to the middle of the second line; cursor should end up on the second line
492 motionEventInfo(text.indexOf("he 2nd"), 0.5f, text.indexOf("he 2nd")),
493 };
494 simulateDrag(tv, events, true);
495 }
496
Nikita Dubrovskye8b3b312020-03-26 21:10:39 -0700497 @Suppress // b/152574363
Shu Chen77003422020-03-05 13:38:05 +0800498 @Test
Shu Chenafbcf852020-03-10 08:19:07 +0800499 public void testLineChangeSlop() throws Throwable {
500 TextView tv = mActivity.findViewById(R.id.textview);
501 Spannable s = new SpannableString("a\nb\nc");
502 s.setSpan(new AbsoluteSizeSpan(10), 2, 4, SPAN_INCLUSIVE_EXCLUSIVE);
503 s.setSpan(new AbsoluteSizeSpan(32), 4, 5, SPAN_INCLUSIVE_EXCLUSIVE);
504 mInstrumentation.runOnMainSync(() -> tv.setText(s));
505
506 Layout layout = tv.getLayout();
507 Editor editor = tv.getEditorForTesting();
508 final float verticalOffset = tv.getExtendedPaddingTop();
509 editor.setLineChangeSlopMinMaxForTesting(30, 65);
510 // Hit top part of upper line, jump to upper line.
511 assertThat(editor.getCurrentLineAdjustedForSlop(layout, 1, 5 + verticalOffset))
512 .isEqualTo(0);
513 // Hit bottom part of upper line, stay at current line.
514 assertThat(editor.getCurrentLineAdjustedForSlop(layout, 1, 40 + verticalOffset))
515 .isEqualTo(1);
516 // Hit current line, stay at current line.
517 assertThat(editor.getCurrentLineAdjustedForSlop(layout, 1, 70 + verticalOffset))
518 .isEqualTo(1);
519 // Hit top part of lower line, stay at current line.
520 assertThat(editor.getCurrentLineAdjustedForSlop(layout, 1, 85 + verticalOffset))
521 .isEqualTo(1);
522 // Hit bottom part of lower line, jump to lower line.
523 assertThat(editor.getCurrentLineAdjustedForSlop(layout, 1, 110 + verticalOffset))
524 .isEqualTo(2);
525 // Hit lower line of lower line, jump to target line.
526 assertThat(editor.getCurrentLineAdjustedForSlop(layout, 0, 110 + verticalOffset))
527 .isEqualTo(2);
528 }
529
530 @Test
Shu Chen77003422020-03-05 13:38:05 +0800531 public void testCursorDrag_snapDistance() throws Throwable {
532 String text = "line1: This is the 1st line: A\n"
533 + "line2: This is the 2nd line: B\n"
534 + "line3: This is the 3rd line: C\n";
535 onView(withId(R.id.textview)).perform(replaceText(text));
536 TextView tv = mActivity.findViewById(R.id.textview);
537 Editor editor = tv.getEditorForTesting();
538 final int startIndex = text.indexOf("he 2nd");
539 Layout layout = tv.getLayout();
540 final float cursorStartX = layout.getPrimaryHorizontal(startIndex) + tv.getTotalPaddingLeft();
541 final float cursorStartY = layout.getLineTop(1) + tv.getTotalPaddingTop();
542 final float dragHandleStartX = 20;
543 final float dragHandleStartY = 20;
544
545 // Drag the handle from the 2nd line to the 3rd line.
546 tapAtPoint(tv, cursorStartX, cursorStartY);
547 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(startIndex));
548 View handleView = editor.getInsertionController().getHandle();
549 final int rawYOfHandleDrag = dragDownUntilLineChange(
550 handleView, dragHandleStartX, dragHandleStartY, tv.getSelectionStart());
551
552 // Drag the cursor from the 2nd line to the 3rd line.
553 tapAtPoint(tv, cursorStartX, cursorStartY);
554 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(startIndex));
555 final int rawYOfCursorDrag =
556 dragDownUntilLineChange(tv, cursorStartX, cursorStartY, tv.getSelectionStart());
557
558 // Drag the handle with touch through from the 2nd line to the 3rd line.
559 tv.getEditorForTesting().setFlagInsertionHandleGesturesEnabled(true);
560 tapAtPoint(tv, cursorStartX, cursorStartY);
561 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(startIndex));
562 handleView = editor.getInsertionController().getHandle();
563 int rawYOfHandleDragWithTouchThrough =
564 dragDownUntilLineChange(handleView, dragHandleStartX, dragHandleStartY, tv.getSelectionStart());
565
566 String msg = String.format(
567 "rawYOfHandleDrag: %d, rawYOfCursorDrag: %d, rawYOfHandleDragWithTouchThrough: %d",
568 rawYOfHandleDrag, rawYOfCursorDrag, rawYOfHandleDragWithTouchThrough);
569 final int max = Math.max(
570 rawYOfCursorDrag, Math.max(rawYOfHandleDrag, rawYOfHandleDragWithTouchThrough));
571 final int min = Math.min(
572 rawYOfCursorDrag, Math.min(rawYOfHandleDrag, rawYOfHandleDragWithTouchThrough));
573 // The drag step is 5 pixels in dragDownUntilLineChange().
574 // The difference among the 3 raw Y values should be no bigger than the drag step.
575 assertWithMessage(msg).that(max - min).isLessThan(6);
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800576 }
577
Shu Chen77003422020-03-05 13:38:05 +0800578 private void dispatchTouchEvent(View view, MotionEvent event) {
579 mInstrumentation.runOnMainSync(() -> view.dispatchTouchEvent(event));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800580 }
581
Shu Chen77003422020-03-05 13:38:05 +0800582 private void tapAtPoint(TextView tv, final float x, final float y) {
583 long downTime = sTicker.addAndGet(10_000);
584 dispatchTouchEvent(tv, downEvent(tv, downTime, downTime, x, y));
585 dispatchTouchEvent(tv, upEvent(tv, downTime, downTime + 1, x, y));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800586 }
Nikita Dubrovskybd50f3b2020-01-11 20:14:05 -0800587
Shu Chen77003422020-03-05 13:38:05 +0800588 private int dragDownUntilLineChange(View view, final float startX, final float startY,
589 final int startOffset) {
590 TextView tv = mActivity.findViewById(R.id.textview);
591 final int startLine = tv.getLayout().getLineForOffset(startOffset);
592
593 int rawY = 0;
594 long downTime = sTicker.addAndGet(10_000);
595 long eventTime = downTime;
596 // Move horizontally first to initiate the cursor drag.
597 dispatchTouchEvent(view, downEvent(view, downTime, eventTime++, startX, startY));
598 dispatchTouchEvent(view, moveEvent(view, downTime, eventTime++, startX + 50, startY));
599 dispatchTouchEvent(view, moveEvent(view, downTime, eventTime++, startX, startY));
600 // Move downwards 5 pixels at a time until a line change occurs.
601 for (int i = 0; i < 200; i++) {
602 MotionEvent ev = moveEvent(view, downTime, eventTime++, startX, startY + i * 5);
603 rawY = (int) ev.getRawY();
604 dispatchTouchEvent(view, ev);
605 if (tv.getLayout().getLineForOffset(tv.getSelectionStart()) > startLine) {
606 break;
607 }
608 }
609 String msg = String.format("The cursor didn't jump from %d!", startOffset);
610 assertWithMessage(msg).that(
611 tv.getLayout().getLineForOffset(tv.getSelectionStart())).isGreaterThan(startLine);
612 dispatchTouchEvent(view, upEvent(view, downTime, eventTime, startX, startY));
613 return rawY;
614 }
615
616 private MotionEvent obtainTouchEvent(
617 View view, int action, long downTime, long eventTime, float x, float y) {
618 Rect r = new Rect();
619 view.getBoundsOnScreen(r);
620 float rawX = x + r.left;
621 float rawY = y + r.top;
622 MotionEvent event =
623 MotionEvent.obtain(downTime, eventTime, action, rawX, rawY, 0);
624 view.toLocalMotionEvent(event);
625 mMotionEvents.add(event);
Nikita Dubrovskybd50f3b2020-01-11 20:14:05 -0800626 return event;
627 }
628
Shu Chen77003422020-03-05 13:38:05 +0800629 private MotionEvent obtainMouseEvent(
630 View view, int action, long downTime, long eventTime, float x, float y) {
631 MotionEvent event = obtainTouchEvent(view, action, downTime, eventTime, x, y);
Nikita Dubrovskybd50f3b2020-01-11 20:14:05 -0800632 event.setSource(InputDevice.SOURCE_MOUSE);
Shu Chen77003422020-03-05 13:38:05 +0800633 if (action != MotionEvent.ACTION_UP) {
634 event.setButtonState(MotionEvent.BUTTON_PRIMARY);
635 }
Nikita Dubrovskybd50f3b2020-01-11 20:14:05 -0800636 return event;
637 }
638
Shu Chen77003422020-03-05 13:38:05 +0800639 private MotionEvent downEvent(View view, long downTime, long eventTime, float x, float y) {
640 return obtainTouchEvent(view, MotionEvent.ACTION_DOWN, downTime, eventTime, x, y);
641 }
642
643 private MotionEvent moveEvent(View view, long downTime, long eventTime, float x, float y) {
644 return obtainTouchEvent(view, MotionEvent.ACTION_MOVE, downTime, eventTime, x, y);
645 }
646
647 private MotionEvent upEvent(View view, long downTime, long eventTime, float x, float y) {
648 return obtainTouchEvent(view, MotionEvent.ACTION_UP, downTime, eventTime, x, y);
649 }
650
651 private MotionEvent mouseDownEvent(View view, long downTime, long eventTime, float x, float y) {
652 return obtainMouseEvent(view, MotionEvent.ACTION_DOWN, downTime, eventTime, x, y);
653 }
654
655 private MotionEvent mouseMoveEvent(View view, long downTime, long eventTime, float x, float y) {
656 return obtainMouseEvent(view, MotionEvent.ACTION_MOVE, downTime, eventTime, x, y);
657 }
658
659 private MotionEvent mouseUpEvent(View view, long downTime, long eventTime, float x, float y) {
660 return obtainMouseEvent(view, MotionEvent.ACTION_UP, downTime, eventTime, x, y);
Nikita Dubrovskybd50f3b2020-01-11 20:14:05 -0800661 }
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800662
663 public static MotionEventInfo motionEventInfo(int index, float ratioToLineTop) {
664 return new MotionEventInfo(index, ratioToLineTop, index);
665 }
666
667 public static MotionEventInfo motionEventInfo(int index, float ratioToLineTop,
668 int expectedCursorIndex) {
669 return new MotionEventInfo(index, ratioToLineTop, expectedCursorIndex);
670 }
671
672 private static class MotionEventInfo {
673 public final int index;
674 public final float ratioToLineTop; // 0.0 = bottom of line, 0.5 = middle of line, etc
675 public final int expectedCursorIndex;
676
677 private MotionEventInfo(int index, float ratioToLineTop, int expectedCursorIndex) {
678 this.index = index;
679 this.ratioToLineTop = ratioToLineTop;
680 this.expectedCursorIndex = expectedCursorIndex;
681 }
682
683 public float[] getCoordinates(TextView textView) {
684 Layout layout = textView.getLayout();
685 int line = layout.getLineForOffset(index);
686 float x = layout.getPrimaryHorizontal(index) + textView.getTotalPaddingLeft();
687 int bottom = layout.getLineBottom(line);
688 int top = layout.getLineTop(line);
689 float y = bottom - ((bottom - top) * ratioToLineTop) + textView.getTotalPaddingTop();
690 return new float[]{x, y};
691 }
692 }
693
694 private void simulateDrag(TextView tv, MotionEventInfo[] events, boolean runAssertions)
695 throws Exception {
696 Editor editor = tv.getEditorForTesting();
697
698 float[] downCoords = events[0].getCoordinates(tv);
699 long downEventTime = sTicker.addAndGet(10_000);
Shu Chen77003422020-03-05 13:38:05 +0800700 MotionEvent downEvent = downEvent(tv, downEventTime, downEventTime,
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800701 downCoords[0], downCoords[1]);
702 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(downEvent));
703
704 for (int i = 1; i < events.length; i++) {
705 float[] moveCoords = events[i].getCoordinates(tv);
706 long eventTime = downEventTime + i;
Shu Chen77003422020-03-05 13:38:05 +0800707 MotionEvent event = moveEvent(tv, downEventTime, eventTime, moveCoords[0],
708 moveCoords[1]);
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800709 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event));
710 assertCursorPosition(tv, events[i].expectedCursorIndex, runAssertions);
711 }
712
713 MotionEventInfo lastEvent = events[events.length - 1];
714 float[] upCoords = lastEvent.getCoordinates(tv);
715 long upEventTime = downEventTime + events.length;
Shu Chen77003422020-03-05 13:38:05 +0800716 MotionEvent upEvent = upEvent(tv, downEventTime, upEventTime, upCoords[0], upCoords[1]);
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800717 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(upEvent));
718 }
719
720 private static void assertCursorPosition(TextView tv, int expectedPosition,
721 boolean runAssertions) {
722 String textAfterExpectedPos = getTextAfterIndex(tv, expectedPosition, 15);
723 String textAfterActualPos = getTextAfterIndex(tv, tv.getSelectionStart(), 15);
724 String msg = "Expected cursor at " + expectedPosition + ", just before \""
725 + textAfterExpectedPos + "\". Cursor is at " + tv.getSelectionStart()
726 + ", just before \"" + textAfterActualPos + "\".";
727 Log.d(LOG_TAG, msg);
728 if (runAssertions) {
729 assertWithMessage(msg).that(tv.getSelectionStart()).isEqualTo(expectedPosition);
730 assertThat(tv.getSelectionEnd()).isEqualTo(expectedPosition);
731 }
732 }
733
734 private static String getTextAfterIndex(TextView tv, int position, int maxLength) {
735 int end = Math.min(position + maxLength, tv.getText().length());
736 try {
737 String afterPosition = tv.getText().subSequence(position, end).toString();
738 if (afterPosition.indexOf('\n') > 0) {
739 afterPosition = afterPosition.substring(0, afterPosition.indexOf('\n'));
740 }
741 return afterPosition;
742 } catch (StringIndexOutOfBoundsException e) {
743 Log.d(LOG_TAG, "Invalid target position: position=" + position + ", length="
744 + tv.getText().length() + ", end=" + end);
745 return "";
746 }
747 }
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800748}