blob: 838f60069f2709fabf3a9916ec534a935be0358b [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 Dubrovskyb1ad3b62020-07-01 09:51:48 -0700148 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("line1"), text.indexOf("2")));
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 Dubrovskyb1ad3b62020-07-01 09:51:48 -0700184 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("line1"), text.indexOf("2")));
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 Cheneb8b1ba2020-04-04 14:46:50 +0800531 public void testCursorDrag_multiTouch() throws Throwable {
532 String text = "line1: This is the 1st line: A";
533 onView(withId(R.id.textview)).perform(replaceText(text));
534 TextView tv = mActivity.findViewById(R.id.textview);
535 Editor editor = tv.getEditorForTesting();
536 final int startIndex = text.indexOf("1st line");
537 Layout layout = tv.getLayout();
538 final float cursorStartX =
539 layout.getPrimaryHorizontal(startIndex) + tv.getTotalPaddingLeft();
540 final float cursorStartY = layout.getLineTop(1) + tv.getTotalPaddingTop();
541
542 // Taps to show the insertion handle.
543 tapAtPoint(tv, cursorStartX, cursorStartY);
544 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(startIndex));
545 View handleView = editor.getInsertionController().getHandle();
546
547 // Taps & holds the insertion handle.
548 long handleDownTime = sTicker.addAndGet(10_000);
549 long eventTime = handleDownTime;
550 dispatchTouchEvent(handleView, downEvent(handleView, handleDownTime, eventTime++, 0, 0));
551
552 // Tries to Drag the cursor, with the pointer id > 0 (meaning the 2nd finger).
553 long cursorDownTime = eventTime++;
554 dispatchTouchEvent(tv, obtainTouchEventWithPointerId(
555 tv, MotionEvent.ACTION_DOWN, cursorDownTime, eventTime++, 1,
556 cursorStartX - 50, cursorStartY));
557 dispatchTouchEvent(tv, obtainTouchEventWithPointerId(
558 tv, MotionEvent.ACTION_MOVE, cursorDownTime, eventTime++, 1,
559 cursorStartX - 100, cursorStartY));
560 dispatchTouchEvent(tv, obtainTouchEventWithPointerId(
561 tv, MotionEvent.ACTION_UP, cursorDownTime, eventTime++, 1,
562 cursorStartX - 100, cursorStartY));
563
564 // Checks the cursor drag doesn't work while the handle is being hold.
565 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(startIndex));
566
567 // Finger up on the insertion handle.
568 dispatchTouchEvent(handleView, upEvent(handleView, handleDownTime, eventTime, 0, 0));
569 }
570
571 @Test
Shu Chen77003422020-03-05 13:38:05 +0800572 public void testCursorDrag_snapDistance() throws Throwable {
573 String text = "line1: This is the 1st line: A\n"
574 + "line2: This is the 2nd line: B\n"
575 + "line3: This is the 3rd line: C\n";
576 onView(withId(R.id.textview)).perform(replaceText(text));
577 TextView tv = mActivity.findViewById(R.id.textview);
578 Editor editor = tv.getEditorForTesting();
579 final int startIndex = text.indexOf("he 2nd");
580 Layout layout = tv.getLayout();
581 final float cursorStartX = layout.getPrimaryHorizontal(startIndex) + tv.getTotalPaddingLeft();
582 final float cursorStartY = layout.getLineTop(1) + tv.getTotalPaddingTop();
583 final float dragHandleStartX = 20;
584 final float dragHandleStartY = 20;
585
586 // Drag the handle from the 2nd line to the 3rd line.
587 tapAtPoint(tv, cursorStartX, cursorStartY);
588 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(startIndex));
589 View handleView = editor.getInsertionController().getHandle();
590 final int rawYOfHandleDrag = dragDownUntilLineChange(
591 handleView, dragHandleStartX, dragHandleStartY, tv.getSelectionStart());
592
593 // Drag the cursor from the 2nd line to the 3rd line.
594 tapAtPoint(tv, cursorStartX, cursorStartY);
595 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(startIndex));
596 final int rawYOfCursorDrag =
597 dragDownUntilLineChange(tv, cursorStartX, cursorStartY, tv.getSelectionStart());
598
599 // Drag the handle with touch through from the 2nd line to the 3rd line.
600 tv.getEditorForTesting().setFlagInsertionHandleGesturesEnabled(true);
601 tapAtPoint(tv, cursorStartX, cursorStartY);
602 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(startIndex));
603 handleView = editor.getInsertionController().getHandle();
604 int rawYOfHandleDragWithTouchThrough =
605 dragDownUntilLineChange(handleView, dragHandleStartX, dragHandleStartY, tv.getSelectionStart());
606
607 String msg = String.format(
608 "rawYOfHandleDrag: %d, rawYOfCursorDrag: %d, rawYOfHandleDragWithTouchThrough: %d",
609 rawYOfHandleDrag, rawYOfCursorDrag, rawYOfHandleDragWithTouchThrough);
610 final int max = Math.max(
611 rawYOfCursorDrag, Math.max(rawYOfHandleDrag, rawYOfHandleDragWithTouchThrough));
612 final int min = Math.min(
613 rawYOfCursorDrag, Math.min(rawYOfHandleDrag, rawYOfHandleDragWithTouchThrough));
614 // The drag step is 5 pixels in dragDownUntilLineChange().
615 // The difference among the 3 raw Y values should be no bigger than the drag step.
616 assertWithMessage(msg).that(max - min).isLessThan(6);
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800617 }
618
Shu Chen77003422020-03-05 13:38:05 +0800619 private void dispatchTouchEvent(View view, MotionEvent event) {
620 mInstrumentation.runOnMainSync(() -> view.dispatchTouchEvent(event));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800621 }
622
Shu Chen77003422020-03-05 13:38:05 +0800623 private void tapAtPoint(TextView tv, final float x, final float y) {
624 long downTime = sTicker.addAndGet(10_000);
625 dispatchTouchEvent(tv, downEvent(tv, downTime, downTime, x, y));
626 dispatchTouchEvent(tv, upEvent(tv, downTime, downTime + 1, x, y));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800627 }
Nikita Dubrovskybd50f3b2020-01-11 20:14:05 -0800628
Shu Chen77003422020-03-05 13:38:05 +0800629 private int dragDownUntilLineChange(View view, final float startX, final float startY,
630 final int startOffset) {
631 TextView tv = mActivity.findViewById(R.id.textview);
632 final int startLine = tv.getLayout().getLineForOffset(startOffset);
633
634 int rawY = 0;
635 long downTime = sTicker.addAndGet(10_000);
636 long eventTime = downTime;
637 // Move horizontally first to initiate the cursor drag.
638 dispatchTouchEvent(view, downEvent(view, downTime, eventTime++, startX, startY));
639 dispatchTouchEvent(view, moveEvent(view, downTime, eventTime++, startX + 50, startY));
640 dispatchTouchEvent(view, moveEvent(view, downTime, eventTime++, startX, startY));
641 // Move downwards 5 pixels at a time until a line change occurs.
642 for (int i = 0; i < 200; i++) {
643 MotionEvent ev = moveEvent(view, downTime, eventTime++, startX, startY + i * 5);
644 rawY = (int) ev.getRawY();
645 dispatchTouchEvent(view, ev);
646 if (tv.getLayout().getLineForOffset(tv.getSelectionStart()) > startLine) {
647 break;
648 }
649 }
650 String msg = String.format("The cursor didn't jump from %d!", startOffset);
651 assertWithMessage(msg).that(
652 tv.getLayout().getLineForOffset(tv.getSelectionStart())).isGreaterThan(startLine);
653 dispatchTouchEvent(view, upEvent(view, downTime, eventTime, startX, startY));
654 return rawY;
655 }
656
657 private MotionEvent obtainTouchEvent(
658 View view, int action, long downTime, long eventTime, float x, float y) {
659 Rect r = new Rect();
660 view.getBoundsOnScreen(r);
661 float rawX = x + r.left;
662 float rawY = y + r.top;
663 MotionEvent event =
664 MotionEvent.obtain(downTime, eventTime, action, rawX, rawY, 0);
665 view.toLocalMotionEvent(event);
666 mMotionEvents.add(event);
Nikita Dubrovskybd50f3b2020-01-11 20:14:05 -0800667 return event;
668 }
669
Shu Cheneb8b1ba2020-04-04 14:46:50 +0800670 private MotionEvent obtainTouchEventWithPointerId(
671 View view, int action, long downTime, long eventTime, int pointerId, float x, float y) {
672 Rect r = new Rect();
673 view.getBoundsOnScreen(r);
674 float rawX = x + r.left;
675 float rawY = y + r.top;
676 MotionEvent.PointerCoords coordinates = new MotionEvent.PointerCoords();
677 coordinates.x = rawX;
678 coordinates.y = rawY;
679 MotionEvent event = MotionEvent.obtain(
680 downTime, eventTime, action, 1, new int[] {pointerId},
681 new MotionEvent.PointerCoords[] {coordinates},
682 0, 1f, 1f, 0, 0, 0, 0);
683 view.toLocalMotionEvent(event);
684 mMotionEvents.add(event);
685 return event;
686 }
687
Shu Chen77003422020-03-05 13:38:05 +0800688 private MotionEvent obtainMouseEvent(
689 View view, int action, long downTime, long eventTime, float x, float y) {
690 MotionEvent event = obtainTouchEvent(view, action, downTime, eventTime, x, y);
Nikita Dubrovskybd50f3b2020-01-11 20:14:05 -0800691 event.setSource(InputDevice.SOURCE_MOUSE);
Shu Chen77003422020-03-05 13:38:05 +0800692 if (action != MotionEvent.ACTION_UP) {
693 event.setButtonState(MotionEvent.BUTTON_PRIMARY);
694 }
Nikita Dubrovskybd50f3b2020-01-11 20:14:05 -0800695 return event;
696 }
697
Shu Chen77003422020-03-05 13:38:05 +0800698 private MotionEvent downEvent(View view, long downTime, long eventTime, float x, float y) {
699 return obtainTouchEvent(view, MotionEvent.ACTION_DOWN, downTime, eventTime, x, y);
700 }
701
702 private MotionEvent moveEvent(View view, long downTime, long eventTime, float x, float y) {
703 return obtainTouchEvent(view, MotionEvent.ACTION_MOVE, downTime, eventTime, x, y);
704 }
705
706 private MotionEvent upEvent(View view, long downTime, long eventTime, float x, float y) {
707 return obtainTouchEvent(view, MotionEvent.ACTION_UP, downTime, eventTime, x, y);
708 }
709
710 private MotionEvent mouseDownEvent(View view, long downTime, long eventTime, float x, float y) {
711 return obtainMouseEvent(view, MotionEvent.ACTION_DOWN, downTime, eventTime, x, y);
712 }
713
714 private MotionEvent mouseMoveEvent(View view, long downTime, long eventTime, float x, float y) {
715 return obtainMouseEvent(view, MotionEvent.ACTION_MOVE, downTime, eventTime, x, y);
716 }
717
718 private MotionEvent mouseUpEvent(View view, long downTime, long eventTime, float x, float y) {
719 return obtainMouseEvent(view, MotionEvent.ACTION_UP, downTime, eventTime, x, y);
Nikita Dubrovskybd50f3b2020-01-11 20:14:05 -0800720 }
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800721
722 public static MotionEventInfo motionEventInfo(int index, float ratioToLineTop) {
723 return new MotionEventInfo(index, ratioToLineTop, index);
724 }
725
726 public static MotionEventInfo motionEventInfo(int index, float ratioToLineTop,
727 int expectedCursorIndex) {
728 return new MotionEventInfo(index, ratioToLineTop, expectedCursorIndex);
729 }
730
731 private static class MotionEventInfo {
732 public final int index;
733 public final float ratioToLineTop; // 0.0 = bottom of line, 0.5 = middle of line, etc
734 public final int expectedCursorIndex;
735
736 private MotionEventInfo(int index, float ratioToLineTop, int expectedCursorIndex) {
737 this.index = index;
738 this.ratioToLineTop = ratioToLineTop;
739 this.expectedCursorIndex = expectedCursorIndex;
740 }
741
742 public float[] getCoordinates(TextView textView) {
743 Layout layout = textView.getLayout();
744 int line = layout.getLineForOffset(index);
745 float x = layout.getPrimaryHorizontal(index) + textView.getTotalPaddingLeft();
746 int bottom = layout.getLineBottom(line);
747 int top = layout.getLineTop(line);
748 float y = bottom - ((bottom - top) * ratioToLineTop) + textView.getTotalPaddingTop();
749 return new float[]{x, y};
750 }
751 }
752
753 private void simulateDrag(TextView tv, MotionEventInfo[] events, boolean runAssertions)
754 throws Exception {
755 Editor editor = tv.getEditorForTesting();
756
757 float[] downCoords = events[0].getCoordinates(tv);
758 long downEventTime = sTicker.addAndGet(10_000);
Shu Chen77003422020-03-05 13:38:05 +0800759 MotionEvent downEvent = downEvent(tv, downEventTime, downEventTime,
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800760 downCoords[0], downCoords[1]);
761 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(downEvent));
762
763 for (int i = 1; i < events.length; i++) {
764 float[] moveCoords = events[i].getCoordinates(tv);
765 long eventTime = downEventTime + i;
Shu Chen77003422020-03-05 13:38:05 +0800766 MotionEvent event = moveEvent(tv, downEventTime, eventTime, moveCoords[0],
767 moveCoords[1]);
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800768 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event));
769 assertCursorPosition(tv, events[i].expectedCursorIndex, runAssertions);
770 }
771
772 MotionEventInfo lastEvent = events[events.length - 1];
773 float[] upCoords = lastEvent.getCoordinates(tv);
774 long upEventTime = downEventTime + events.length;
Shu Chen77003422020-03-05 13:38:05 +0800775 MotionEvent upEvent = upEvent(tv, downEventTime, upEventTime, upCoords[0], upCoords[1]);
Nikita Dubrovsky7c583592020-02-16 15:54:23 -0800776 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(upEvent));
777 }
778
779 private static void assertCursorPosition(TextView tv, int expectedPosition,
780 boolean runAssertions) {
781 String textAfterExpectedPos = getTextAfterIndex(tv, expectedPosition, 15);
782 String textAfterActualPos = getTextAfterIndex(tv, tv.getSelectionStart(), 15);
783 String msg = "Expected cursor at " + expectedPosition + ", just before \""
784 + textAfterExpectedPos + "\". Cursor is at " + tv.getSelectionStart()
785 + ", just before \"" + textAfterActualPos + "\".";
786 Log.d(LOG_TAG, msg);
787 if (runAssertions) {
788 assertWithMessage(msg).that(tv.getSelectionStart()).isEqualTo(expectedPosition);
789 assertThat(tv.getSelectionEnd()).isEqualTo(expectedPosition);
790 }
791 }
792
793 private static String getTextAfterIndex(TextView tv, int position, int maxLength) {
794 int end = Math.min(position + maxLength, tv.getText().length());
795 try {
796 String afterPosition = tv.getText().subSequence(position, end).toString();
797 if (afterPosition.indexOf('\n') > 0) {
798 afterPosition = afterPosition.substring(0, afterPosition.indexOf('\n'));
799 }
800 return afterPosition;
801 } catch (StringIndexOutOfBoundsException e) {
802 Log.d(LOG_TAG, "Invalid target position: position=" + position + ", length="
803 + tv.getText().length() + ", end=" + end);
804 return "";
805 }
806 }
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800807}