Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.widget; |
| 18 | |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 19 | import static android.widget.espresso.TextViewActions.clickOnTextAtIndex; |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 20 | import static android.widget.espresso.TextViewActions.dragOnText; |
| 21 | import static android.widget.espresso.TextViewAssertions.hasInsertionPointerAtIndex; |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 22 | import static android.widget.espresso.TextViewAssertions.hasSelection; |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 23 | |
| 24 | import static androidx.test.espresso.Espresso.onView; |
| 25 | import static androidx.test.espresso.action.ViewActions.replaceText; |
| 26 | import static androidx.test.espresso.matcher.ViewMatchers.withId; |
| 27 | |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 28 | import static org.hamcrest.Matchers.emptyString; |
| 29 | import static org.hamcrest.Matchers.not; |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 30 | import static org.junit.Assert.assertFalse; |
| 31 | import static org.junit.Assert.assertTrue; |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 32 | |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 33 | import android.app.Activity; |
| 34 | import android.app.Instrumentation; |
Nikita Dubrovsky | bd50f3b | 2020-01-11 20:14:05 -0800 | [diff] [blame^] | 35 | import android.view.InputDevice; |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 36 | import android.view.MotionEvent; |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 37 | |
| 38 | import androidx.test.InstrumentationRegistry; |
| 39 | import androidx.test.filters.SmallTest; |
| 40 | import androidx.test.rule.ActivityTestRule; |
| 41 | import androidx.test.runner.AndroidJUnit4; |
| 42 | |
| 43 | import com.android.frameworks.coretests.R; |
| 44 | |
| 45 | import com.google.common.base.Strings; |
| 46 | |
| 47 | import org.junit.After; |
| 48 | import org.junit.Before; |
| 49 | import org.junit.Rule; |
| 50 | import org.junit.Test; |
| 51 | import org.junit.runner.RunWith; |
| 52 | |
| 53 | @RunWith(AndroidJUnit4.class) |
| 54 | @SmallTest |
| 55 | public 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 Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 82 | // Swipe left to right to drag the cursor. The cursor should end up at the position where |
| 83 | // the finger is lifted. |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 84 | onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("llo"), text.indexOf("!"))); |
| 85 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(11)); |
| 86 | |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 87 | // Swipe right to left to drag the cursor. The cursor should end up at the position where |
| 88 | // the finger is lifted. |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 89 | 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 Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 95 | String text = "Hello world!\n\n" |
| 96 | + Strings.repeat("Bla\n\n", 200) + "Bye"; |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 97 | onView(withId(R.id.textview)).perform(replaceText(text)); |
| 98 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0)); |
| 99 | |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 100 | // Swipe left to right to drag the cursor. The cursor should end up at the position where |
| 101 | // the finger is lifted. |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 102 | onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("llo"), text.indexOf("!"))); |
| 103 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(11)); |
| 104 | |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 105 | // Swipe right to left to drag the cursor. The cursor should end up at the position where |
| 106 | // the finger is lifted. |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 107 | 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 Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 112 | 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 Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 146 | 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 Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 151 | sb.append(Strings.repeat("0123456789\n", 400)).append("Last"); |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 152 | String text = sb.toString(); |
| 153 | onView(withId(R.id.textview)).perform(replaceText(text)); |
| 154 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0)); |
| 155 | |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 156 | // Swipe along a diagonal path. This should drag the cursor. |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 157 | 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 Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 160 | // 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 Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 163 | |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 164 | // 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 Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 169 | |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 170 | // 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 Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Test |
| 180 | public void testCursorDrag_vertical_whenTextViewContentsFitOnScreen() throws Throwable { |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 181 | 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 Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 188 | onView(withId(R.id.textview)).perform(replaceText(text)); |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 189 | |
| 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 Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 193 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0)); |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 194 | onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("bbb"), text.indexOf("aaa"))); |
| 195 | onView(withId(R.id.textview)).check(hasSelection(not(emptyString()))); |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 196 | |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 197 | // 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 Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Test |
| 206 | public void testCursorDrag_vertical_whenTextViewContentsLargerThanScreen() throws Throwable { |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 207 | 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 Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 215 | onView(withId(R.id.textview)).perform(replaceText(text)); |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 216 | onView(withId(R.id.textview)).perform(clickOnTextAtIndex(text.indexOf("ddd"))); |
| 217 | int initialCursorPosition = text.indexOf("ddd"); |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 218 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(initialCursorPosition)); |
| 219 | |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 220 | // 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 Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 223 | |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 224 | // 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 Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 227 | } |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 228 | |
| 229 | @Test |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 230 | 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 Dubrovsky | bd50f3b | 2020-01-11 20:14:05 -0800 | [diff] [blame^] | 285 | 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 Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 314 | 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 Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 325 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event1)); |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 326 | assertFalse(editor.getInsertionController().isCursorBeingModified()); |
| 327 | assertFalse(editor.getSelectionController().isCursorBeingModified()); |
| 328 | |
| 329 | long event2Time = 1002; |
| 330 | MotionEvent event2 = moveEvent(event1Time, event2Time, 21f, 30f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 331 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event2)); |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 332 | assertFalse(editor.getInsertionController().isCursorBeingModified()); |
| 333 | assertFalse(editor.getSelectionController().isCursorBeingModified()); |
| 334 | |
| 335 | long event3Time = 1003; |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 336 | MotionEvent event3 = moveEvent(event1Time, event3Time, 120f, 30f); |
| 337 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event3)); |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 338 | assertTrue(editor.getInsertionController().isCursorBeingModified()); |
| 339 | assertFalse(editor.getSelectionController().isCursorBeingModified()); |
| 340 | |
| 341 | long event4Time = 1004; |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 342 | MotionEvent event4 = upEvent(event1Time, event4Time, 120f, 30f); |
| 343 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event4)); |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 344 | 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 Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 360 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event1)); |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 361 | assertFalse(editor.getInsertionController().isCursorBeingModified()); |
| 362 | assertFalse(editor.getSelectionController().isCursorBeingModified()); |
| 363 | |
| 364 | long event2Time = 1002; |
| 365 | MotionEvent event2 = upEvent(event1Time, event2Time, 20f, 30f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 366 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event2)); |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 367 | assertFalse(editor.getInsertionController().isCursorBeingModified()); |
| 368 | assertFalse(editor.getSelectionController().isCursorBeingModified()); |
| 369 | |
| 370 | long event3Time = 1003; |
| 371 | MotionEvent event3 = downEvent(event3Time, event3Time, 20f, 30f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 372 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event3)); |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 373 | assertFalse(editor.getInsertionController().isCursorBeingModified()); |
| 374 | assertTrue(editor.getSelectionController().isCursorBeingModified()); |
| 375 | |
| 376 | long event4Time = 1004; |
| 377 | MotionEvent event4 = moveEvent(event3Time, event4Time, 120f, 30f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 378 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event4)); |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 379 | assertFalse(editor.getInsertionController().isCursorBeingModified()); |
| 380 | assertTrue(editor.getSelectionController().isCursorBeingModified()); |
| 381 | |
| 382 | long event5Time = 1005; |
| 383 | MotionEvent event5 = upEvent(event3Time, event5Time, 120f, 30f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 384 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event5)); |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 385 | 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 Dubrovsky | bd50f3b | 2020-01-11 20:14:05 -0800 | [diff] [blame^] | 400 | |
| 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 Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 421 | } |