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 | |
Shu Chen | afbcf85 | 2020-03-10 08:19:07 +0800 | [diff] [blame] | 19 | import static android.text.Spanned.SPAN_INCLUSIVE_EXCLUSIVE; |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 20 | import static android.widget.espresso.TextViewActions.clickOnTextAtIndex; |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 21 | import static android.widget.espresso.TextViewActions.dragOnText; |
| 22 | import static android.widget.espresso.TextViewAssertions.hasInsertionPointerAtIndex; |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 23 | import static android.widget.espresso.TextViewAssertions.hasSelection; |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 24 | |
| 25 | import static androidx.test.espresso.Espresso.onView; |
| 26 | import static androidx.test.espresso.action.ViewActions.replaceText; |
| 27 | import static androidx.test.espresso.matcher.ViewMatchers.withId; |
| 28 | |
Nikita Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 29 | import static com.google.common.truth.Truth.assertThat; |
| 30 | import static com.google.common.truth.Truth.assertWithMessage; |
| 31 | |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 32 | import static org.hamcrest.Matchers.emptyString; |
| 33 | import static org.hamcrest.Matchers.not; |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 34 | import static org.junit.Assert.assertFalse; |
| 35 | import static org.junit.Assert.assertTrue; |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 36 | |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 37 | import android.app.Activity; |
| 38 | import android.app.Instrumentation; |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 39 | import android.graphics.Rect; |
Nikita Dubrovsky | 256ee07 | 2020-03-20 09:51:27 -0700 | [diff] [blame] | 40 | import android.platform.test.annotations.Presubmit; |
Nikita Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 41 | import android.text.Layout; |
Shu Chen | afbcf85 | 2020-03-10 08:19:07 +0800 | [diff] [blame] | 42 | import android.text.Spannable; |
| 43 | import android.text.SpannableString; |
| 44 | import android.text.style.AbsoluteSizeSpan; |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 45 | import android.util.ArraySet; |
Nikita Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 46 | import android.util.Log; |
Nikita Dubrovsky | bd50f3b | 2020-01-11 20:14:05 -0800 | [diff] [blame] | 47 | import android.view.InputDevice; |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 48 | import android.view.MotionEvent; |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 49 | import android.view.View; |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 50 | |
| 51 | import androidx.test.InstrumentationRegistry; |
Nikita Dubrovsky | 256ee07 | 2020-03-20 09:51:27 -0700 | [diff] [blame] | 52 | import androidx.test.filters.MediumTest; |
Nikita Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 53 | import androidx.test.filters.Suppress; |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 54 | import androidx.test.rule.ActivityTestRule; |
| 55 | import androidx.test.runner.AndroidJUnit4; |
| 56 | |
| 57 | import com.android.frameworks.coretests.R; |
| 58 | |
| 59 | import com.google.common.base.Strings; |
| 60 | |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 61 | import org.junit.After; |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 62 | import org.junit.Before; |
| 63 | import org.junit.Rule; |
| 64 | import org.junit.Test; |
| 65 | import org.junit.runner.RunWith; |
| 66 | |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 67 | import java.util.Set; |
Nikita Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 68 | import java.util.concurrent.atomic.AtomicLong; |
| 69 | |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 70 | @RunWith(AndroidJUnit4.class) |
Nikita Dubrovsky | 256ee07 | 2020-03-20 09:51:27 -0700 | [diff] [blame] | 71 | @MediumTest |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 72 | public class EditorCursorDragTest { |
Nikita Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 73 | private static final String LOG_TAG = EditorCursorDragTest.class.getSimpleName(); |
| 74 | |
| 75 | private static final AtomicLong sTicker = new AtomicLong(1); |
| 76 | |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 77 | @Rule |
| 78 | public ActivityTestRule<TextViewActivity> mActivityRule = new ActivityTestRule<>( |
| 79 | TextViewActivity.class); |
| 80 | |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 81 | private Instrumentation mInstrumentation; |
| 82 | private Activity mActivity; |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 83 | private Set<MotionEvent> mMotionEvents = new ArraySet<>(); |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 84 | |
| 85 | @Before |
| 86 | public void before() throws Throwable { |
| 87 | mInstrumentation = InstrumentationRegistry.getInstrumentation(); |
| 88 | mActivity = mActivityRule.getActivity(); |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 91 | @After |
| 92 | public void after() throws Throwable { |
| 93 | for (MotionEvent event : mMotionEvents) { |
| 94 | event.recycle(); |
| 95 | } |
| 96 | mMotionEvents.clear(); |
| 97 | } |
| 98 | |
Nikita Dubrovsky | cac7080 | 2020-04-09 23:06:38 -0700 | [diff] [blame] | 99 | @Presubmit |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 100 | @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 Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 106 | // Swipe left to right to drag the cursor. The cursor should end up at the position where |
| 107 | // the finger is lifted. |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 108 | onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("llo"), text.indexOf("!"))); |
| 109 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(11)); |
| 110 | |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 111 | // Swipe right to left to drag the cursor. The cursor should end up at the position where |
| 112 | // the finger is lifted. |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 113 | 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 Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 119 | String text = "Hello world!\n\n" |
| 120 | + Strings.repeat("Bla\n\n", 200) + "Bye"; |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 121 | onView(withId(R.id.textview)).perform(replaceText(text)); |
| 122 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0)); |
| 123 | |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 124 | // Swipe left to right to drag the cursor. The cursor should end up at the position where |
| 125 | // the finger is lifted. |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 126 | onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("llo"), text.indexOf("!"))); |
| 127 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(11)); |
| 128 | |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 129 | // Swipe right to left to drag the cursor. The cursor should end up at the position where |
| 130 | // the finger is lifted. |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 131 | 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 Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 136 | 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 Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 145 | // 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 Dubrovsky | b1ad3b6 | 2020-07-01 09:51:48 -0700 | [diff] [blame^] | 148 | onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("line1"), text.indexOf("2"))); |
Nikita Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 149 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(text.indexOf("1"))); |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 150 | |
| 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 Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 157 | // 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 Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 163 | // 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 Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 166 | 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 Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 171 | 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 Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 176 | sb.append(Strings.repeat("0123456789\n", 400)).append("Last"); |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 177 | String text = sb.toString(); |
| 178 | onView(withId(R.id.textview)).perform(replaceText(text)); |
| 179 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0)); |
| 180 | |
Nikita Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 181 | // 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 Dubrovsky | b1ad3b6 | 2020-07-01 09:51:48 -0700 | [diff] [blame^] | 184 | onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("line1"), text.indexOf("2"))); |
Nikita Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 185 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(text.indexOf("1"))); |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 186 | |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 187 | // 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 Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 192 | |
Nikita Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 193 | // Tap to clear the selection. |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 194 | int index = text.indexOf("line9"); |
| 195 | onView(withId(R.id.textview)).perform(clickOnTextAtIndex(index)); |
Nikita Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 196 | onView(withId(R.id.textview)).check(hasSelection(emptyString())); |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 197 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(index)); |
Nikita Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 198 | |
| 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 Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 201 | onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("line7"), text.indexOf("1"))); |
| 202 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(index)); |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Test |
| 206 | public void testCursorDrag_vertical_whenTextViewContentsFitOnScreen() 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"; |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 214 | onView(withId(R.id.textview)).perform(replaceText(text)); |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 215 | |
| 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 Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 219 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0)); |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 220 | onView(withId(R.id.textview)).perform(dragOnText(text.indexOf("bbb"), text.indexOf("aaa"))); |
| 221 | onView(withId(R.id.textview)).check(hasSelection(not(emptyString()))); |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 222 | |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 223 | // 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 Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | @Test |
| 232 | public void testCursorDrag_vertical_whenTextViewContentsLargerThanScreen() throws Throwable { |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 233 | 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 Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 241 | onView(withId(R.id.textview)).perform(replaceText(text)); |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 242 | onView(withId(R.id.textview)).perform(clickOnTextAtIndex(text.indexOf("ddd"))); |
| 243 | int initialCursorPosition = text.indexOf("ddd"); |
Nikita Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 244 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(initialCursorPosition)); |
| 245 | |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 246 | // 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 Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 249 | |
Nikita Dubrovsky | cd36c5e | 2019-12-19 16:15:17 -0800 | [diff] [blame] | 250 | // 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 Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 253 | } |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 254 | |
| 255 | @Test |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 256 | 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 Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 266 | MotionEvent event1 = downEvent(tv, event1Time, event1Time, 5f, 10f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 267 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event1)); |
| 268 | assertFalse(editor.getInsertionController().isCursorBeingModified()); |
| 269 | assertFalse(editor.getSelectionController().isCursorBeingModified()); |
| 270 | |
| 271 | long event2Time = 1002; |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 272 | MotionEvent event2 = moveEvent(tv, event1Time, event2Time, 50f, 10f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 273 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event2)); |
| 274 | assertTrue(editor.getInsertionController().isCursorBeingModified()); |
| 275 | assertFalse(editor.getSelectionController().isCursorBeingModified()); |
| 276 | |
| 277 | long event3Time = 1003; |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 278 | MotionEvent event3 = moveEvent(tv, event1Time, event3Time, 100f, 10f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 279 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event3)); |
| 280 | assertTrue(editor.getInsertionController().isCursorBeingModified()); |
| 281 | assertFalse(editor.getSelectionController().isCursorBeingModified()); |
| 282 | |
| 283 | long event4Time = 2004; |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 284 | MotionEvent event4 = upEvent(tv, event1Time, event4Time, 100f, 10f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 285 | 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 Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 291 | MotionEvent event5 = downEvent(tv, event5Time, event5Time, 90f, 10f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 292 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event5)); |
| 293 | assertFalse(editor.getInsertionController().isCursorBeingModified()); |
| 294 | assertFalse(editor.getSelectionController().isCursorBeingModified()); |
| 295 | |
| 296 | long event6Time = 2006; |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 297 | MotionEvent event6 = upEvent(tv, event5Time, event6Time, 90f, 10f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 298 | 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 Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 304 | MotionEvent event7 = downEvent(tv, event7Time, event7Time, 90f, 10f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 305 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event7)); |
| 306 | assertFalse(editor.getInsertionController().isCursorBeingModified()); |
| 307 | assertTrue(editor.getSelectionController().isCursorBeingModified()); |
| 308 | } |
| 309 | |
| 310 | @Test |
Nikita Dubrovsky | bd50f3b | 2020-01-11 20:14:05 -0800 | [diff] [blame] | 311 | 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 Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 321 | MotionEvent event1 = mouseDownEvent(tv, event1Time, event1Time, 20f, 30f); |
Nikita Dubrovsky | bd50f3b | 2020-01-11 20:14:05 -0800 | [diff] [blame] | 322 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event1)); |
| 323 | assertFalse(editor.getInsertionController().isCursorBeingModified()); |
| 324 | assertFalse(editor.getSelectionController().isCursorBeingModified()); |
| 325 | |
| 326 | long event2Time = 1002; |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 327 | MotionEvent event2 = mouseMoveEvent(tv, event1Time, event2Time, 120f, 30f); |
Nikita Dubrovsky | bd50f3b | 2020-01-11 20:14:05 -0800 | [diff] [blame] | 328 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event2)); |
| 329 | assertFalse(editor.getInsertionController().isCursorBeingModified()); |
| 330 | assertTrue(editor.getSelectionController().isCursorBeingModified()); |
| 331 | |
| 332 | long event3Time = 1003; |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 333 | MotionEvent event3 = mouseUpEvent(tv, event1Time, event3Time, 120f, 30f); |
Nikita Dubrovsky | bd50f3b | 2020-01-11 20:14:05 -0800 | [diff] [blame] | 334 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event3)); |
| 335 | assertFalse(editor.getInsertionController().isCursorBeingModified()); |
| 336 | assertFalse(editor.getSelectionController().isCursorBeingModified()); |
| 337 | } |
| 338 | |
| 339 | @Test |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 340 | 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 Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 350 | MotionEvent event1 = downEvent(tv, event1Time, event1Time, 20f, 30f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 351 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event1)); |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 352 | assertFalse(editor.getInsertionController().isCursorBeingModified()); |
| 353 | assertFalse(editor.getSelectionController().isCursorBeingModified()); |
| 354 | |
| 355 | long event2Time = 1002; |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 356 | MotionEvent event2 = moveEvent(tv, event1Time, event2Time, 21f, 30f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 357 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event2)); |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 358 | assertFalse(editor.getInsertionController().isCursorBeingModified()); |
| 359 | assertFalse(editor.getSelectionController().isCursorBeingModified()); |
| 360 | |
| 361 | long event3Time = 1003; |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 362 | MotionEvent event3 = moveEvent(tv, event1Time, event3Time, 120f, 30f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 363 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event3)); |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 364 | assertTrue(editor.getInsertionController().isCursorBeingModified()); |
| 365 | assertFalse(editor.getSelectionController().isCursorBeingModified()); |
| 366 | |
| 367 | long event4Time = 1004; |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 368 | MotionEvent event4 = upEvent(tv, event1Time, event4Time, 120f, 30f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 369 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event4)); |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 370 | 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 Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 385 | MotionEvent event1 = downEvent(tv, event1Time, event1Time, 20f, 30f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 386 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event1)); |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 387 | assertFalse(editor.getInsertionController().isCursorBeingModified()); |
| 388 | assertFalse(editor.getSelectionController().isCursorBeingModified()); |
| 389 | |
| 390 | long event2Time = 1002; |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 391 | MotionEvent event2 = upEvent(tv, event1Time, event2Time, 20f, 30f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 392 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event2)); |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 393 | assertFalse(editor.getInsertionController().isCursorBeingModified()); |
| 394 | assertFalse(editor.getSelectionController().isCursorBeingModified()); |
| 395 | |
| 396 | long event3Time = 1003; |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 397 | MotionEvent event3 = downEvent(tv, event3Time, event3Time, 20f, 30f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 398 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event3)); |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 399 | assertFalse(editor.getInsertionController().isCursorBeingModified()); |
| 400 | assertTrue(editor.getSelectionController().isCursorBeingModified()); |
| 401 | |
| 402 | long event4Time = 1004; |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 403 | MotionEvent event4 = moveEvent(tv, event3Time, event4Time, 120f, 30f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 404 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event4)); |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 405 | assertFalse(editor.getInsertionController().isCursorBeingModified()); |
| 406 | assertTrue(editor.getSelectionController().isCursorBeingModified()); |
| 407 | |
| 408 | long event5Time = 1005; |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 409 | MotionEvent event5 = upEvent(tv, event3Time, event5Time, 120f, 30f); |
Nikita Dubrovsky | 1f78b11 | 2019-12-30 08:26:12 -0800 | [diff] [blame] | 410 | mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event5)); |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 411 | assertFalse(editor.getInsertionController().isCursorBeingModified()); |
| 412 | assertFalse(editor.getSelectionController().isCursorBeingModified()); |
| 413 | } |
| 414 | |
Nikita Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 415 | @Suppress // b/149712851 |
Nikita Dubrovsky | 99b55fa | 2020-01-12 20:57:51 -0800 | [diff] [blame] | 416 | @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 Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 422 | assertThat(editor).isNotNull(); |
Nikita Dubrovsky | 99b55fa | 2020-01-12 20:57:51 -0800 | [diff] [blame] | 423 | |
| 424 | // Simulate a tap. No error should be thrown. |
| 425 | long event1Time = 1001; |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 426 | MotionEvent event1 = downEvent(tv, event1Time, event1Time, 20f, 30f); |
Nikita Dubrovsky | 99b55fa | 2020-01-12 20:57:51 -0800 | [diff] [blame] | 427 | 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 Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 434 | @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 Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 463 | + "line3: This is the 3rd line: C\n" |
| 464 | + "line4: This is the 4th line: D\n"; |
Nikita Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 465 | 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 Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 478 | // 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 Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 480 | // 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 Dubrovsky | e8b3b31 | 2020-03-26 21:10:39 -0700 | [diff] [blame] | 497 | @Suppress // b/152574363 |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 498 | @Test |
Shu Chen | afbcf85 | 2020-03-10 08:19:07 +0800 | [diff] [blame] | 499 | 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 Chen | eb8b1ba | 2020-04-04 14:46:50 +0800 | [diff] [blame] | 531 | 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 Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 572 | 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 Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 617 | } |
| 618 | |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 619 | private void dispatchTouchEvent(View view, MotionEvent event) { |
| 620 | mInstrumentation.runOnMainSync(() -> view.dispatchTouchEvent(event)); |
Nikita Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 621 | } |
| 622 | |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 623 | 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 Dubrovsky | 21c6a96 | 2019-12-27 08:48:02 -0800 | [diff] [blame] | 627 | } |
Nikita Dubrovsky | bd50f3b | 2020-01-11 20:14:05 -0800 | [diff] [blame] | 628 | |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 629 | 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 Dubrovsky | bd50f3b | 2020-01-11 20:14:05 -0800 | [diff] [blame] | 667 | return event; |
| 668 | } |
| 669 | |
Shu Chen | eb8b1ba | 2020-04-04 14:46:50 +0800 | [diff] [blame] | 670 | 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 Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 688 | 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 Dubrovsky | bd50f3b | 2020-01-11 20:14:05 -0800 | [diff] [blame] | 691 | event.setSource(InputDevice.SOURCE_MOUSE); |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 692 | if (action != MotionEvent.ACTION_UP) { |
| 693 | event.setButtonState(MotionEvent.BUTTON_PRIMARY); |
| 694 | } |
Nikita Dubrovsky | bd50f3b | 2020-01-11 20:14:05 -0800 | [diff] [blame] | 695 | return event; |
| 696 | } |
| 697 | |
Shu Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 698 | 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 Dubrovsky | bd50f3b | 2020-01-11 20:14:05 -0800 | [diff] [blame] | 720 | } |
Nikita Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 721 | |
| 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 Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 759 | MotionEvent downEvent = downEvent(tv, downEventTime, downEventTime, |
Nikita Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 760 | 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 Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 766 | MotionEvent event = moveEvent(tv, downEventTime, eventTime, moveCoords[0], |
| 767 | moveCoords[1]); |
Nikita Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 768 | 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 Chen | 7700342 | 2020-03-05 13:38:05 +0800 | [diff] [blame] | 775 | MotionEvent upEvent = upEvent(tv, downEventTime, upEventTime, upCoords[0], upCoords[1]); |
Nikita Dubrovsky | 7c58359 | 2020-02-16 15:54:23 -0800 | [diff] [blame] | 776 | 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 Dubrovsky | 9a1369b | 2019-12-06 09:25:20 -0800 | [diff] [blame] | 807 | } |