blob: b36f601332b51638fdb7e9fcd3c6a07964d428d6 [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
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -080019import static android.widget.espresso.TextViewActions.clickOnTextAtIndex;
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -080020import static android.widget.espresso.TextViewActions.dragOnText;
21import static android.widget.espresso.TextViewAssertions.hasInsertionPointerAtIndex;
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -080022import static android.widget.espresso.TextViewAssertions.hasSelection;
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -080023
24import static androidx.test.espresso.Espresso.onView;
25import static androidx.test.espresso.action.ViewActions.replaceText;
26import static androidx.test.espresso.matcher.ViewMatchers.withId;
27
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -080028import static org.hamcrest.Matchers.emptyString;
29import static org.hamcrest.Matchers.not;
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -080030import static org.junit.Assert.assertFalse;
31import static org.junit.Assert.assertTrue;
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -080032
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -080033import android.app.Activity;
34import android.app.Instrumentation;
Nikita Dubrovskybd50f3b2020-01-11 20:14:05 -080035import android.view.InputDevice;
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -080036import android.view.MotionEvent;
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -080037
38import androidx.test.InstrumentationRegistry;
39import androidx.test.filters.SmallTest;
40import androidx.test.rule.ActivityTestRule;
41import androidx.test.runner.AndroidJUnit4;
42
43import com.android.frameworks.coretests.R;
44
45import com.google.common.base.Strings;
46
47import org.junit.After;
48import org.junit.Before;
49import org.junit.Rule;
50import org.junit.Test;
51import org.junit.runner.RunWith;
52
53@RunWith(AndroidJUnit4.class)
54@SmallTest
55public class EditorCursorDragTest {
56 @Rule
57 public ActivityTestRule<TextViewActivity> mActivityRule = new ActivityTestRule<>(
58 TextViewActivity.class);
59
60 private boolean mOriginalFlagValue;
61 private Instrumentation mInstrumentation;
62 private Activity mActivity;
63
64 @Before
65 public void before() throws Throwable {
66 mInstrumentation = InstrumentationRegistry.getInstrumentation();
67 mActivity = mActivityRule.getActivity();
68 mOriginalFlagValue = Editor.FLAG_ENABLE_CURSOR_DRAG;
69 Editor.FLAG_ENABLE_CURSOR_DRAG = true;
70 }
71 @After
72 public void after() throws Throwable {
73 Editor.FLAG_ENABLE_CURSOR_DRAG = mOriginalFlagValue;
74 }
75
76 @Test
77 public void testCursorDrag_horizontal_whenTextViewContentsFitOnScreen() throws Throwable {
78 String text = "Hello world!";
79 onView(withId(R.id.textview)).perform(replaceText(text));
80 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
81
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -080082 // Swipe left to right to drag the cursor. The cursor should end up at the position where
83 // the finger is lifted.
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -080084 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("llo"), text.indexOf("!")));
85 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(11));
86
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -080087 // Swipe right to left to drag the cursor. The cursor should end up at the position where
88 // the finger is lifted.
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -080089 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("!"), text.indexOf("llo")));
90 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(2));
91 }
92
93 @Test
94 public void testCursorDrag_horizontal_whenTextViewContentsLargerThanScreen() throws Throwable {
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -080095 String text = "Hello world!\n\n"
96 + Strings.repeat("Bla\n\n", 200) + "Bye";
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -080097 onView(withId(R.id.textview)).perform(replaceText(text));
98 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
99
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800100 // Swipe left to right to drag the cursor. The cursor should end up at the position where
101 // the finger is lifted.
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800102 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("llo"), text.indexOf("!")));
103 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(11));
104
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800105 // Swipe right to left to drag the cursor. The cursor should end up at the position where
106 // the finger is lifted.
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800107 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("!"), text.indexOf("llo")));
108 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(2));
109 }
110
111 @Test
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800112 public void testCursorDrag_diagonal_whenTextViewContentsFitOnScreen() throws Throwable {
113 StringBuilder sb = new StringBuilder();
114 for (int i = 1; i <= 9; i++) {
115 sb.append("line").append(i).append("\n");
116 }
117 String text = sb.toString();
118 onView(withId(R.id.textview)).perform(replaceText(text));
119 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
120
121 // Swipe along a diagonal path. This should drag the cursor.
122 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("line1"), text.indexOf("2")));
123 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(text.indexOf("2")));
124
125 // Swipe along a steeper diagonal path. This should still drag the cursor.
126 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("line1"), text.indexOf("3")));
127 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(text.indexOf("3")));
128
129 // Swipe right-down along a very steep diagonal path. This should not drag the cursor.
130 // Normally this would trigger a scroll, but since the full view fits on the screen there
131 // is nothing to scroll and the gesture will trigger a selection drag.
132 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("line1"), text.indexOf("7")));
133 onView(withId(R.id.textview)).check(hasSelection(not(emptyString())));
134
135 // Swipe right-up along a very steep diagonal path. This should not drag the cursor.
136 // Normally this would trigger a scroll, but since the full view fits on the screen there
137 // is nothing to scroll and the gesture will trigger a selection drag.
138 int index = text.indexOf("line9");
139 onView(withId(R.id.textview)).perform(clickOnTextAtIndex(index));
140 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(index));
141 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("line7"), text.indexOf("1")));
142 onView(withId(R.id.textview)).check(hasSelection(not(emptyString())));
143 }
144
145 @Test
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800146 public void testCursorDrag_diagonal_whenTextViewContentsLargerThanScreen() throws Throwable {
147 StringBuilder sb = new StringBuilder();
148 for (int i = 1; i <= 9; i++) {
149 sb.append("line").append(i).append("\n");
150 }
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800151 sb.append(Strings.repeat("0123456789\n", 400)).append("Last");
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800152 String text = sb.toString();
153 onView(withId(R.id.textview)).perform(replaceText(text));
154 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
155
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800156 // Swipe along a diagonal path. This should drag the cursor.
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800157 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("line1"), text.indexOf("2")));
158 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(text.indexOf("2")));
159
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800160 // Swipe along a steeper diagonal path. This should still drag the cursor.
161 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("line1"), text.indexOf("3")));
162 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(text.indexOf("3")));
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800163
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800164 // Swipe right-down along a very steep diagonal path. This should not drag the cursor.
165 // Normally this would trigger a scroll up, but since the view is already at the top there
166 // is nothing to scroll and the gesture will trigger a selection drag.
167 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("line1"), text.indexOf("7")));
168 onView(withId(R.id.textview)).check(hasSelection(not(emptyString())));
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800169
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800170 // Swipe right-up along a very steep diagonal path. This should not drag the cursor. This
171 // will trigger a downward scroll and the cursor position will not change.
172 int index = text.indexOf("line9");
173 onView(withId(R.id.textview)).perform(clickOnTextAtIndex(index));
174 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(index));
175 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("line7"), text.indexOf("1")));
176 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(index));
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800177 }
178
179 @Test
180 public void testCursorDrag_vertical_whenTextViewContentsFitOnScreen() throws Throwable {
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800181 String text = "012345_aaa\n"
182 + "0123456789\n"
183 + "012345_bbb\n"
184 + "0123456789\n"
185 + "012345_ccc\n"
186 + "0123456789\n"
187 + "012345_ddd";
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800188 onView(withId(R.id.textview)).perform(replaceText(text));
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800189
190 // Swipe up vertically. This should not drag the cursor. Since there's also nothing to
191 // scroll, the gesture will trigger a selection drag.
192 onView(withId(R.id.textview)).perform(clickOnTextAtIndex(0));
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800193 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800194 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("bbb"), text.indexOf("aaa")));
195 onView(withId(R.id.textview)).check(hasSelection(not(emptyString())));
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800196
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800197 // Swipe down vertically. This should not drag the cursor. Since there's also nothing to
198 // scroll, the gesture will trigger a selection drag.
199 onView(withId(R.id.textview)).perform(clickOnTextAtIndex(0));
200 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
201 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("ccc"), text.indexOf("ddd")));
202 onView(withId(R.id.textview)).check(hasSelection(not(emptyString())));
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800203 }
204
205 @Test
206 public void testCursorDrag_vertical_whenTextViewContentsLargerThanScreen() 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\n"
214 + Strings.repeat("0123456789\n", 400) + "012345_zzz";
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800215 onView(withId(R.id.textview)).perform(replaceText(text));
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800216 onView(withId(R.id.textview)).perform(clickOnTextAtIndex(text.indexOf("ddd")));
217 int initialCursorPosition = text.indexOf("ddd");
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800218 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(initialCursorPosition));
219
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800220 // Swipe up vertically. This should trigger a downward scroll.
221 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("bbb"), text.indexOf("aaa")));
222 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(initialCursorPosition));
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800223
Nikita Dubrovskycd36c5e2019-12-19 16:15:17 -0800224 // Swipe down vertically. This should trigger an upward scroll.
225 onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("ccc"), text.indexOf("ddd")));
226 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(initialCursorPosition));
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800227 }
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800228
229 @Test
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800230 public void testEditor_onTouchEvent_quickTapAfterDrag() throws Throwable {
231 String text = "Hi world!";
232 onView(withId(R.id.textview)).perform(replaceText(text));
233 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
234
235 TextView tv = mActivity.findViewById(R.id.textview);
236 Editor editor = tv.getEditorForTesting();
237
238 // Simulate a tap-and-drag gesture.
239 long event1Time = 1001;
240 MotionEvent event1 = downEvent(event1Time, event1Time, 5f, 10f);
241 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event1));
242 assertFalse(editor.getInsertionController().isCursorBeingModified());
243 assertFalse(editor.getSelectionController().isCursorBeingModified());
244
245 long event2Time = 1002;
246 MotionEvent event2 = moveEvent(event1Time, event2Time, 50f, 10f);
247 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event2));
248 assertTrue(editor.getInsertionController().isCursorBeingModified());
249 assertFalse(editor.getSelectionController().isCursorBeingModified());
250
251 long event3Time = 1003;
252 MotionEvent event3 = moveEvent(event1Time, event3Time, 100f, 10f);
253 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event3));
254 assertTrue(editor.getInsertionController().isCursorBeingModified());
255 assertFalse(editor.getSelectionController().isCursorBeingModified());
256
257 long event4Time = 2004;
258 MotionEvent event4 = upEvent(event1Time, event4Time, 100f, 10f);
259 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event4));
260 assertFalse(editor.getInsertionController().isCursorBeingModified());
261 assertFalse(editor.getSelectionController().isCursorBeingModified());
262
263 // Simulate a quick tap after the drag, near the location where the drag ended.
264 long event5Time = 2005;
265 MotionEvent event5 = downEvent(event5Time, event5Time, 90f, 10f);
266 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event5));
267 assertFalse(editor.getInsertionController().isCursorBeingModified());
268 assertFalse(editor.getSelectionController().isCursorBeingModified());
269
270 long event6Time = 2006;
271 MotionEvent event6 = upEvent(event5Time, event6Time, 90f, 10f);
272 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event6));
273 assertFalse(editor.getInsertionController().isCursorBeingModified());
274 assertFalse(editor.getSelectionController().isCursorBeingModified());
275
276 // Simulate another quick tap in the same location; now selection should be triggered.
277 long event7Time = 2007;
278 MotionEvent event7 = downEvent(event7Time, event7Time, 90f, 10f);
279 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event7));
280 assertFalse(editor.getInsertionController().isCursorBeingModified());
281 assertTrue(editor.getSelectionController().isCursorBeingModified());
282 }
283
284 @Test
Nikita Dubrovskybd50f3b2020-01-11 20:14:05 -0800285 public void testEditor_onTouchEvent_mouseDrag() throws Throwable {
286 String text = "testEditor_onTouchEvent_mouseDrag";
287 onView(withId(R.id.textview)).perform(replaceText(text));
288 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
289
290 TextView tv = mActivity.findViewById(R.id.textview);
291 Editor editor = tv.getEditorForTesting();
292
293 // Simulate a mouse click and drag. This should NOT trigger a cursor drag.
294 long event1Time = 1001;
295 MotionEvent event1 = mouseDownEvent(event1Time, event1Time, 20f, 30f);
296 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event1));
297 assertFalse(editor.getInsertionController().isCursorBeingModified());
298 assertFalse(editor.getSelectionController().isCursorBeingModified());
299
300 long event2Time = 1002;
301 MotionEvent event2 = mouseMoveEvent(event1Time, event2Time, 120f, 30f);
302 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event2));
303 assertFalse(editor.getInsertionController().isCursorBeingModified());
304 assertTrue(editor.getSelectionController().isCursorBeingModified());
305
306 long event3Time = 1003;
307 MotionEvent event3 = mouseUpEvent(event1Time, event3Time, 120f, 30f);
308 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event3));
309 assertFalse(editor.getInsertionController().isCursorBeingModified());
310 assertFalse(editor.getSelectionController().isCursorBeingModified());
311 }
312
313 @Test
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800314 public void testEditor_onTouchEvent_cursorDrag() throws Throwable {
315 String text = "testEditor_onTouchEvent_cursorDrag";
316 onView(withId(R.id.textview)).perform(replaceText(text));
317 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
318
319 TextView tv = mActivity.findViewById(R.id.textview);
320 Editor editor = tv.getEditorForTesting();
321
322 // Simulate a tap-and-drag gesture. This should trigger a cursor drag.
323 long event1Time = 1001;
324 MotionEvent event1 = downEvent(event1Time, event1Time, 20f, 30f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800325 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event1));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800326 assertFalse(editor.getInsertionController().isCursorBeingModified());
327 assertFalse(editor.getSelectionController().isCursorBeingModified());
328
329 long event2Time = 1002;
330 MotionEvent event2 = moveEvent(event1Time, event2Time, 21f, 30f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800331 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event2));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800332 assertFalse(editor.getInsertionController().isCursorBeingModified());
333 assertFalse(editor.getSelectionController().isCursorBeingModified());
334
335 long event3Time = 1003;
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800336 MotionEvent event3 = moveEvent(event1Time, event3Time, 120f, 30f);
337 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event3));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800338 assertTrue(editor.getInsertionController().isCursorBeingModified());
339 assertFalse(editor.getSelectionController().isCursorBeingModified());
340
341 long event4Time = 1004;
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800342 MotionEvent event4 = upEvent(event1Time, event4Time, 120f, 30f);
343 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event4));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800344 assertFalse(editor.getInsertionController().isCursorBeingModified());
345 assertFalse(editor.getSelectionController().isCursorBeingModified());
346 }
347
348 @Test
349 public void testEditor_onTouchEvent_selectionDrag() throws Throwable {
350 String text = "testEditor_onTouchEvent_selectionDrag";
351 onView(withId(R.id.textview)).perform(replaceText(text));
352 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));
353
354 TextView tv = mActivity.findViewById(R.id.textview);
355 Editor editor = tv.getEditorForTesting();
356
357 // Simulate a double-tap followed by a drag. This should trigger a selection drag.
358 long event1Time = 1001;
359 MotionEvent event1 = downEvent(event1Time, event1Time, 20f, 30f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800360 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event1));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800361 assertFalse(editor.getInsertionController().isCursorBeingModified());
362 assertFalse(editor.getSelectionController().isCursorBeingModified());
363
364 long event2Time = 1002;
365 MotionEvent event2 = upEvent(event1Time, event2Time, 20f, 30f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800366 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event2));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800367 assertFalse(editor.getInsertionController().isCursorBeingModified());
368 assertFalse(editor.getSelectionController().isCursorBeingModified());
369
370 long event3Time = 1003;
371 MotionEvent event3 = downEvent(event3Time, event3Time, 20f, 30f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800372 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event3));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800373 assertFalse(editor.getInsertionController().isCursorBeingModified());
374 assertTrue(editor.getSelectionController().isCursorBeingModified());
375
376 long event4Time = 1004;
377 MotionEvent event4 = moveEvent(event3Time, event4Time, 120f, 30f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800378 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event4));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800379 assertFalse(editor.getInsertionController().isCursorBeingModified());
380 assertTrue(editor.getSelectionController().isCursorBeingModified());
381
382 long event5Time = 1005;
383 MotionEvent event5 = upEvent(event3Time, event5Time, 120f, 30f);
Nikita Dubrovsky1f78b112019-12-30 08:26:12 -0800384 mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event5));
Nikita Dubrovsky21c6a962019-12-27 08:48:02 -0800385 assertFalse(editor.getInsertionController().isCursorBeingModified());
386 assertFalse(editor.getSelectionController().isCursorBeingModified());
387 }
388
389 private static MotionEvent downEvent(long downTime, long eventTime, float x, float y) {
390 return MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, 0);
391 }
392
393 private static MotionEvent upEvent(long downTime, long eventTime, float x, float y) {
394 return MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0);
395 }
396
397 private static MotionEvent moveEvent(long downTime, long eventTime, float x, float y) {
398 return MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, x, y, 0);
399 }
Nikita Dubrovskybd50f3b2020-01-11 20:14:05 -0800400
401 private static MotionEvent mouseDownEvent(long downTime, long eventTime, float x, float y) {
402 MotionEvent event = downEvent(downTime, eventTime, x, y);
403 event.setSource(InputDevice.SOURCE_MOUSE);
404 event.setButtonState(MotionEvent.BUTTON_PRIMARY);
405 return event;
406 }
407
408 private static MotionEvent mouseUpEvent(long downTime, long eventTime, float x, float y) {
409 MotionEvent event = upEvent(downTime, eventTime, x, y);
410 event.setSource(InputDevice.SOURCE_MOUSE);
411 event.setButtonState(0);
412 return event;
413 }
414
415 private static MotionEvent mouseMoveEvent(long downTime, long eventTime, float x, float y) {
416 MotionEvent event = moveEvent(downTime, eventTime, x, y);
417 event.setSource(InputDevice.SOURCE_MOUSE);
418 event.setButtonState(MotionEvent.BUTTON_PRIMARY);
419 return event;
420 }
Nikita Dubrovsky9a1369b2019-12-06 09:25:20 -0800421}