Abodunrinwa Toki | 223c839 | 2015-07-24 20:55:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 19 | import static android.support.test.espresso.Espresso.onView; |
| 20 | import static android.support.test.espresso.action.ViewActions.click; |
Abodunrinwa Toki | c5b54ba | 2016-10-25 10:21:36 +0100 | [diff] [blame] | 21 | import static android.support.test.espresso.action.ViewActions.longClick; |
Abodunrinwa Toki | 90cdfe0 | 2015-09-15 21:15:56 +0100 | [diff] [blame] | 22 | import static android.support.test.espresso.action.ViewActions.pressKey; |
Keisuke Kuroyanagi | 2645414 | 2015-12-02 15:04:57 -0800 | [diff] [blame] | 23 | import static android.support.test.espresso.action.ViewActions.replaceText; |
Abodunrinwa Toki | 223c839 | 2015-07-24 20:55:59 -0700 | [diff] [blame] | 24 | import static android.support.test.espresso.assertion.ViewAssertions.matches; |
Abodunrinwa Toki | fc6e25e | 2015-11-30 19:45:32 +0000 | [diff] [blame] | 25 | import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; |
Abodunrinwa Toki | 223c839 | 2015-07-24 20:55:59 -0700 | [diff] [blame] | 26 | import static android.support.test.espresso.matcher.ViewMatchers.withId; |
| 27 | import static android.support.test.espresso.matcher.ViewMatchers.withText; |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 28 | import static android.widget.espresso.CustomViewActions.longPressAtRelativeCoordinates; |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 29 | import static android.widget.espresso.DragHandleUtils.onHandleView; |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 30 | import static android.widget.espresso.FloatingToolbarEspressoUtils |
| 31 | .assertFloatingToolbarContainsItem; |
| 32 | import static android.widget.espresso.FloatingToolbarEspressoUtils |
| 33 | .assertFloatingToolbarDoesNotContainItem; |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 34 | import static android.widget.espresso.FloatingToolbarEspressoUtils.assertFloatingToolbarIsDisplayed; |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 35 | import static android.widget.espresso.FloatingToolbarEspressoUtils.assertFloatingToolbarItemIndex; |
| 36 | import static android.widget.espresso.FloatingToolbarEspressoUtils.clickFloatingToolbarItem; |
| 37 | import static android.widget.espresso.FloatingToolbarEspressoUtils.sleepForFloatingToolbarPopup; |
| 38 | import static android.widget.espresso.TextViewActions.Handle; |
| 39 | import static android.widget.espresso.TextViewActions.clickOnTextAtIndex; |
| 40 | import static android.widget.espresso.TextViewActions.doubleClickOnTextAtIndex; |
| 41 | import static android.widget.espresso.TextViewActions.doubleTapAndDragOnText; |
| 42 | import static android.widget.espresso.TextViewActions.dragHandle; |
| 43 | import static android.widget.espresso.TextViewActions.longPressAndDragOnText; |
| 44 | import static android.widget.espresso.TextViewActions.longPressOnTextAtIndex; |
| 45 | import static android.widget.espresso.TextViewAssertions.doesNotHaveStyledText; |
| 46 | import static android.widget.espresso.TextViewAssertions.hasInsertionPointerAtIndex; |
| 47 | import static android.widget.espresso.TextViewAssertions.hasSelection; |
| 48 | |
| 49 | import static junit.framework.Assert.assertFalse; |
| 50 | import static junit.framework.Assert.assertTrue; |
| 51 | |
Abodunrinwa Toki | c5b54ba | 2016-10-25 10:21:36 +0100 | [diff] [blame] | 52 | import static org.hamcrest.Matchers.anyOf; |
| 53 | import static org.hamcrest.Matchers.is; |
Abodunrinwa Toki | 6264cbd | 2017-11-06 15:12:52 +0000 | [diff] [blame] | 54 | import static org.mockito.Matchers.any; |
| 55 | import static org.mockito.Mockito.mock; |
| 56 | import static org.mockito.Mockito.never; |
| 57 | import static org.mockito.Mockito.verify; |
| 58 | import static org.mockito.Mockito.when; |
Abodunrinwa Toki | 223c839 | 2015-07-24 20:55:59 -0700 | [diff] [blame] | 59 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 60 | import android.app.Activity; |
| 61 | import android.app.Instrumentation; |
Abodunrinwa Toki | ea6cb12 | 2017-04-28 22:14:13 +0100 | [diff] [blame] | 62 | import android.content.ClipData; |
| 63 | import android.content.ClipboardManager; |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 64 | import android.support.test.InstrumentationRegistry; |
| 65 | import android.support.test.espresso.action.EspressoKey; |
| 66 | import android.support.test.filters.MediumTest; |
| 67 | import android.support.test.rule.ActivityTestRule; |
| 68 | import android.support.test.runner.AndroidJUnit4; |
Richard Ledley | 5f2f820 | 2018-02-05 14:55:47 +0000 | [diff] [blame] | 69 | import android.support.test.uiautomator.UiDevice; |
Abodunrinwa Toki | 89550e8 | 2017-10-31 03:46:46 +0000 | [diff] [blame] | 70 | import android.test.suitebuilder.annotation.Suppress; |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 71 | import android.text.InputType; |
| 72 | import android.text.Selection; |
| 73 | import android.text.Spannable; |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 74 | import android.text.SpannableString; |
| 75 | import android.text.method.LinkMovementMethod; |
Abodunrinwa Toki | 6eecdc9 | 2017-04-11 05:15:25 +0100 | [diff] [blame] | 76 | import android.view.ActionMode; |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 77 | import android.view.KeyEvent; |
Abodunrinwa Toki | 6eecdc9 | 2017-04-11 05:15:25 +0100 | [diff] [blame] | 78 | import android.view.Menu; |
| 79 | import android.view.MenuItem; |
| 80 | import android.view.textclassifier.TextClassificationManager; |
| 81 | import android.view.textclassifier.TextClassifier; |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 82 | import android.view.textclassifier.TextLinks; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 83 | import android.view.textclassifier.TextLinksParams; |
Abodunrinwa Toki | 9479d3e | 2016-03-09 19:41:41 +0000 | [diff] [blame] | 84 | import android.widget.espresso.CustomViewActions.RelativeCoordinatesProvider; |
Abodunrinwa Toki | 223c839 | 2015-07-24 20:55:59 -0700 | [diff] [blame] | 85 | |
Abodunrinwa Toki | c5b54ba | 2016-10-25 10:21:36 +0100 | [diff] [blame] | 86 | import com.android.frameworks.coretests.R; |
Keisuke Kuroyanagi | e27e085 | 2016-01-08 19:05:27 +0900 | [diff] [blame] | 87 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 88 | import org.junit.Before; |
| 89 | import org.junit.Rule; |
| 90 | import org.junit.Test; |
| 91 | import org.junit.runner.RunWith; |
| 92 | |
Abodunrinwa Toki | 223c839 | 2015-07-24 20:55:59 -0700 | [diff] [blame] | 93 | /** |
| 94 | * Tests the TextView widget from an Activity |
| 95 | */ |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 96 | @RunWith(AndroidJUnit4.class) |
Abodunrinwa Toki | c5b54ba | 2016-10-25 10:21:36 +0100 | [diff] [blame] | 97 | @MediumTest |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 98 | public class TextViewActivityTest { |
Abodunrinwa Toki | 223c839 | 2015-07-24 20:55:59 -0700 | [diff] [blame] | 99 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 100 | @Rule |
| 101 | public ActivityTestRule<TextViewActivity> mActivityRule = new ActivityTestRule<>( |
| 102 | TextViewActivity.class); |
Abodunrinwa Toki | 223c839 | 2015-07-24 20:55:59 -0700 | [diff] [blame] | 103 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 104 | private Activity mActivity; |
| 105 | private Instrumentation mInstrumentation; |
| 106 | |
| 107 | @Before |
| 108 | public void setUp() { |
| 109 | mActivity = mActivityRule.getActivity(); |
| 110 | mInstrumentation = InstrumentationRegistry.getInstrumentation(); |
| 111 | mActivity.getSystemService(TextClassificationManager.class) |
Abodunrinwa Toki | 6eecdc9 | 2017-04-11 05:15:25 +0100 | [diff] [blame] | 112 | .setTextClassifier(TextClassifier.NO_OP); |
Abodunrinwa Toki | c9e924e | 2015-10-08 12:01:35 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 115 | @Test |
| 116 | public void testTypedTextIsOnScreen() { |
Abodunrinwa Toki | 223c839 | 2015-07-24 20:55:59 -0700 | [diff] [blame] | 117 | final String helloWorld = "Hello world!"; |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 118 | // We use replaceText instead of typeTextIntoFocusedView to input text to avoid |
| 119 | // unintentional interactions with software keyboard. |
| 120 | onView(withId(R.id.textview)).perform(replaceText(helloWorld)); |
Abodunrinwa Toki | 223c839 | 2015-07-24 20:55:59 -0700 | [diff] [blame] | 121 | |
| 122 | onView(withId(R.id.textview)).check(matches(withText(helloWorld))); |
| 123 | } |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 124 | @Test |
| 125 | public void testPositionCursorAtTextAtIndex() { |
Abodunrinwa Toki | 90cdfe0 | 2015-09-15 21:15:56 +0100 | [diff] [blame] | 126 | final String helloWorld = "Hello world!"; |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 127 | onView(withId(R.id.textview)).perform(replaceText(helloWorld)); |
Abodunrinwa Toki | 90cdfe0 | 2015-09-15 21:15:56 +0100 | [diff] [blame] | 128 | onView(withId(R.id.textview)).perform(clickOnTextAtIndex(helloWorld.indexOf("world"))); |
| 129 | |
| 130 | // Delete text at specified index and see if we got the right one. |
| 131 | onView(withId(R.id.textview)).perform(pressKey(KeyEvent.KEYCODE_FORWARD_DEL)); |
| 132 | onView(withId(R.id.textview)).check(matches(withText("Hello orld!"))); |
| 133 | } |
Abodunrinwa Toki | ca4aaf3 | 2015-09-22 20:31:21 +0100 | [diff] [blame] | 134 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 135 | @Test |
| 136 | public void testPositionCursorAtTextAtIndex_arabic() { |
Keisuke Kuroyanagi | e27e085 | 2016-01-08 19:05:27 +0900 | [diff] [blame] | 137 | // Arabic text. The expected cursorable boundary is |
| 138 | // | \u0623 \u064F | \u067A | \u0633 \u0652 | |
| 139 | final String text = "\u0623\u064F\u067A\u0633\u0652"; |
Keisuke Kuroyanagi | e27e085 | 2016-01-08 19:05:27 +0900 | [diff] [blame] | 140 | onView(withId(R.id.textview)).perform(replaceText(text)); |
| 141 | |
| 142 | onView(withId(R.id.textview)).perform(clickOnTextAtIndex(0)); |
| 143 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0)); |
| 144 | onView(withId(R.id.textview)).perform(clickOnTextAtIndex(1)); |
| 145 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(anyOf(is(0), is(2)))); |
| 146 | onView(withId(R.id.textview)).perform(clickOnTextAtIndex(2)); |
| 147 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(2)); |
| 148 | onView(withId(R.id.textview)).perform(clickOnTextAtIndex(3)); |
| 149 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(3)); |
| 150 | onView(withId(R.id.textview)).perform(clickOnTextAtIndex(4)); |
| 151 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(anyOf(is(3), is(5)))); |
| 152 | onView(withId(R.id.textview)).perform(clickOnTextAtIndex(5)); |
| 153 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(5)); |
| 154 | } |
| 155 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 156 | @Test |
| 157 | public void testPositionCursorAtTextAtIndex_devanagari() { |
Keisuke Kuroyanagi | e27e085 | 2016-01-08 19:05:27 +0900 | [diff] [blame] | 158 | // Devanagari text. The expected cursorable boundary is | \u0915 \u093E | |
| 159 | final String text = "\u0915\u093E"; |
Keisuke Kuroyanagi | e27e085 | 2016-01-08 19:05:27 +0900 | [diff] [blame] | 160 | onView(withId(R.id.textview)).perform(replaceText(text)); |
| 161 | |
| 162 | onView(withId(R.id.textview)).perform(clickOnTextAtIndex(0)); |
| 163 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0)); |
| 164 | onView(withId(R.id.textview)).perform(clickOnTextAtIndex(1)); |
| 165 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(anyOf(is(0), is(2)))); |
| 166 | onView(withId(R.id.textview)).perform(clickOnTextAtIndex(2)); |
| 167 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(2)); |
| 168 | } |
| 169 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 170 | @Test |
| 171 | public void testLongPressToSelect() { |
Abodunrinwa Toki | 8a5e1ae | 2015-09-28 21:59:04 +0100 | [diff] [blame] | 172 | final String helloWorld = "Hello Kirk!"; |
| 173 | onView(withId(R.id.textview)).perform(click()); |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 174 | onView(withId(R.id.textview)).perform(replaceText(helloWorld)); |
Abodunrinwa Toki | 8a5e1ae | 2015-09-28 21:59:04 +0100 | [diff] [blame] | 175 | onView(withId(R.id.textview)).perform( |
| 176 | longPressOnTextAtIndex(helloWorld.indexOf("Kirk"))); |
| 177 | |
| 178 | onView(withId(R.id.textview)).check(hasSelection("Kirk")); |
| 179 | } |
| 180 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 181 | @Test |
| 182 | public void testLongPressEmptySpace() { |
Abodunrinwa Toki | 8a5e1ae | 2015-09-28 21:59:04 +0100 | [diff] [blame] | 183 | final String helloWorld = "Hello big round sun!"; |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 184 | onView(withId(R.id.textview)).perform(replaceText(helloWorld)); |
Abodunrinwa Toki | 8a5e1ae | 2015-09-28 21:59:04 +0100 | [diff] [blame] | 185 | // Move cursor somewhere else |
| 186 | onView(withId(R.id.textview)).perform(clickOnTextAtIndex(helloWorld.indexOf("big"))); |
| 187 | // Long-press at end of line. |
Abodunrinwa Toki | 9479d3e | 2016-03-09 19:41:41 +0000 | [diff] [blame] | 188 | onView(withId(R.id.textview)).perform(longPressAtRelativeCoordinates( |
| 189 | RelativeCoordinatesProvider.HorizontalReference.RIGHT, -5, |
| 190 | RelativeCoordinatesProvider.VerticalReference.CENTER, 0)); |
Abodunrinwa Toki | 8a5e1ae | 2015-09-28 21:59:04 +0100 | [diff] [blame] | 191 | |
| 192 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(helloWorld.length())); |
| 193 | } |
| 194 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 195 | @Test |
| 196 | public void testLongPressAndDragToSelect() { |
Abodunrinwa Toki | ca4aaf3 | 2015-09-22 20:31:21 +0100 | [diff] [blame] | 197 | final String helloWorld = "Hello little handsome boy!"; |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 198 | onView(withId(R.id.textview)).perform(replaceText(helloWorld)); |
Abodunrinwa Toki | ca4aaf3 | 2015-09-22 20:31:21 +0100 | [diff] [blame] | 199 | onView(withId(R.id.textview)).perform( |
| 200 | longPressAndDragOnText(helloWorld.indexOf("little"), helloWorld.indexOf(" boy!"))); |
| 201 | |
| 202 | onView(withId(R.id.textview)).check(hasSelection("little handsome")); |
| 203 | } |
| 204 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 205 | @Test |
| 206 | public void testLongPressAndDragToSelect_emoji() { |
Keisuke Kuroyanagi | 133dfc0 | 2016-07-21 18:07:23 +0900 | [diff] [blame] | 207 | final String text = "\uD83D\uDE00\uD83D\uDE01\uD83D\uDE02\uD83D\uDE03"; |
Keisuke Kuroyanagi | 133dfc0 | 2016-07-21 18:07:23 +0900 | [diff] [blame] | 208 | onView(withId(R.id.textview)).perform(replaceText(text)); |
| 209 | |
| 210 | onView(withId(R.id.textview)).perform(longPressAndDragOnText(4, 6)); |
| 211 | onView(withId(R.id.textview)).check(hasSelection("\uD83D\uDE02")); |
| 212 | |
| 213 | onView(withId(R.id.textview)).perform(click()); |
| 214 | |
| 215 | onView(withId(R.id.textview)).perform(longPressAndDragOnText(4, 2)); |
| 216 | onView(withId(R.id.textview)).check(hasSelection("\uD83D\uDE01")); |
| 217 | } |
| 218 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 219 | @Test |
| 220 | public void testDragAndDrop() { |
Keisuke Kuroyanagi | 4307aeb | 2015-12-07 11:16:39 -0800 | [diff] [blame] | 221 | final String text = "abc def ghi."; |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 222 | onView(withId(R.id.textview)).perform(replaceText(text)); |
Keisuke Kuroyanagi | 4307aeb | 2015-12-07 11:16:39 -0800 | [diff] [blame] | 223 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(text.indexOf("e"))); |
| 224 | |
| 225 | onView(withId(R.id.textview)).perform( |
| 226 | longPressAndDragOnText(text.indexOf("e"), text.length())); |
| 227 | |
| 228 | onView(withId(R.id.textview)).check(matches(withText("abc ghi.def"))); |
| 229 | onView(withId(R.id.textview)).check(hasSelection("")); |
Keisuke Kuroyanagi | 4307aeb | 2015-12-07 11:16:39 -0800 | [diff] [blame] | 230 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex("abc ghi.def".length())); |
Keisuke Kuroyanagi | fae4578 | 2016-02-24 18:53:00 -0800 | [diff] [blame] | 231 | |
| 232 | // Test undo returns to the original state. |
| 233 | onView(withId(R.id.textview)).perform(pressKey( |
| 234 | (new EspressoKey.Builder()).withCtrlPressed(true).withKeyCode(KeyEvent.KEYCODE_Z) |
| 235 | .build())); |
| 236 | onView(withId(R.id.textview)).check(matches(withText(text))); |
Keisuke Kuroyanagi | 4307aeb | 2015-12-07 11:16:39 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 239 | @Test |
| 240 | public void testDoubleTapToSelect() { |
Abodunrinwa Toki | 8a5e1ae | 2015-09-28 21:59:04 +0100 | [diff] [blame] | 241 | final String helloWorld = "Hello SuetYi!"; |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 242 | onView(withId(R.id.textview)).perform(replaceText(helloWorld)); |
| 243 | |
Abodunrinwa Toki | 8a5e1ae | 2015-09-28 21:59:04 +0100 | [diff] [blame] | 244 | onView(withId(R.id.textview)).perform( |
| 245 | doubleClickOnTextAtIndex(helloWorld.indexOf("SuetYi"))); |
| 246 | |
| 247 | onView(withId(R.id.textview)).check(hasSelection("SuetYi")); |
| 248 | } |
| 249 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 250 | @Test |
| 251 | public void testDoubleTapAndDragToSelect() { |
| 252 | final String helloWorld = "Hello young beautiful person!"; |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 253 | onView(withId(R.id.textview)).perform(replaceText(helloWorld)); |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 254 | onView(withId(R.id.textview)).perform(doubleTapAndDragOnText(helloWorld.indexOf("young"), |
| 255 | helloWorld.indexOf(" person!"))); |
Abodunrinwa Toki | ca4aaf3 | 2015-09-22 20:31:21 +0100 | [diff] [blame] | 256 | |
| 257 | onView(withId(R.id.textview)).check(hasSelection("young beautiful")); |
| 258 | } |
Abodunrinwa Toki | 8a5e1ae | 2015-09-28 21:59:04 +0100 | [diff] [blame] | 259 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 260 | @Test |
| 261 | public void testDoubleTapAndDragToSelect_multiLine() { |
Sara Kato | 68c433d | 2016-08-09 14:40:45 +0900 | [diff] [blame] | 262 | final String helloWorld = "abcd\n" + "efg\n" + "hijklm\n" + "nop"; |
Sara Kato | 68c433d | 2016-08-09 14:40:45 +0900 | [diff] [blame] | 263 | onView(withId(R.id.textview)).perform(replaceText(helloWorld)); |
| 264 | onView(withId(R.id.textview)).perform( |
| 265 | doubleTapAndDragOnText(helloWorld.indexOf("m"), helloWorld.indexOf("a"))); |
| 266 | onView(withId(R.id.textview)).check(hasSelection("abcd\nefg\nhijklm")); |
| 267 | } |
| 268 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 269 | @Test |
| 270 | public void testSelectBackwordsByTouch() { |
Abodunrinwa Toki | 8a5e1ae | 2015-09-28 21:59:04 +0100 | [diff] [blame] | 271 | final String helloWorld = "Hello king of the Jungle!"; |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 272 | onView(withId(R.id.textview)).perform(replaceText(helloWorld)); |
Abodunrinwa Toki | 8a5e1ae | 2015-09-28 21:59:04 +0100 | [diff] [blame] | 273 | onView(withId(R.id.textview)).perform( |
| 274 | doubleTapAndDragOnText(helloWorld.indexOf(" Jungle!"), helloWorld.indexOf("king"))); |
| 275 | |
| 276 | onView(withId(R.id.textview)).check(hasSelection("king of the")); |
| 277 | } |
| 278 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 279 | @Test |
| 280 | public void testToolbarAppearsAfterSelection() { |
Abodunrinwa Toki | 8a5e1ae | 2015-09-28 21:59:04 +0100 | [diff] [blame] | 281 | final String text = "Toolbar appears after selection."; |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 282 | onView(withId(R.id.textview)).perform(replaceText(text)); |
Abodunrinwa Toki | 8a5e1ae | 2015-09-28 21:59:04 +0100 | [diff] [blame] | 283 | onView(withId(R.id.textview)).perform( |
| 284 | longPressOnTextAtIndex(text.indexOf("appears"))); |
| 285 | |
Keisuke Kuroyanagi | b0dd77c | 2015-12-02 13:27:01 -0800 | [diff] [blame] | 286 | sleepForFloatingToolbarPopup(); |
| 287 | assertFloatingToolbarIsDisplayed(); |
Keisuke Kuroyanagi | b0dd77c | 2015-12-02 13:27:01 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 290 | @Test |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 291 | public void testToolbarAppearsAfterSelection_withFirstStringLtrAlgorithmAndRtlHint() |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 292 | throws Throwable { |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 293 | // after the hint layout change, the floating toolbar was not visible in the case below |
| 294 | // this test tests that the floating toolbar is displayed on the screen and is visible to |
| 295 | // user. |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 296 | mActivityRule.runOnUiThread(() -> { |
| 297 | final TextView textView = mActivity.findViewById(R.id.textview); |
| 298 | textView.setTextDirection(TextView.TEXT_DIRECTION_FIRST_STRONG_LTR); |
| 299 | textView.setInputType(InputType.TYPE_CLASS_TEXT); |
| 300 | textView.setSingleLine(true); |
| 301 | textView.setHint("الروبوت"); |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 302 | }); |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 303 | mInstrumentation.waitForIdleSync(); |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 304 | |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 305 | onView(withId(R.id.textview)).perform(replaceText("test")); |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 306 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(1)); |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 307 | clickFloatingToolbarItem(mActivity.getString(com.android.internal.R.string.cut)); |
Siyamed Sinir | 987ec65 | 2016-02-17 19:44:41 -0800 | [diff] [blame] | 308 | onView(withId(R.id.textview)).perform(longClick()); |
| 309 | sleepForFloatingToolbarPopup(); |
| 310 | |
| 311 | assertFloatingToolbarIsDisplayed(); |
| 312 | } |
| 313 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 314 | @Test |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 315 | public void testToolbarAppearsAfterLinkClicked() throws Throwable { |
Richard Ledley | 5f2f820 | 2018-02-05 14:55:47 +0000 | [diff] [blame] | 316 | TextLinks.TextLink textLink = addLinkifiedTextToTextView(R.id.textview); |
| 317 | int position = (textLink.getStart() + textLink.getEnd()) / 2; |
| 318 | onView(withId(R.id.textview)).perform(clickOnTextAtIndex(position)); |
| 319 | sleepForFloatingToolbarPopup(); |
| 320 | assertFloatingToolbarIsDisplayed(); |
Richard Ledley | 724eff9 | 2017-12-21 10:11:34 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | @Test |
| 324 | public void testToolbarAppearsAfterLinkClickedNonselectable() throws Throwable { |
Abodunrinwa Toki | 29cb768 | 2018-04-11 21:24:20 +0100 | [diff] [blame] | 325 | final TextView textView = mActivity.findViewById(R.id.nonselectable_textview); |
| 326 | final TextLinks.TextLink textLink = addLinkifiedTextToTextView(R.id.nonselectable_textview); |
| 327 | final int position = (textLink.getStart() + textLink.getEnd()) / 2; |
| 328 | |
Richard Ledley | 5f2f820 | 2018-02-05 14:55:47 +0000 | [diff] [blame] | 329 | onView(withId(R.id.nonselectable_textview)).perform(clickOnTextAtIndex(position)); |
| 330 | sleepForFloatingToolbarPopup(); |
| 331 | assertFloatingToolbarIsDisplayed(); |
Abodunrinwa Toki | 29cb768 | 2018-04-11 21:24:20 +0100 | [diff] [blame] | 332 | assertTrue(textView.hasSelection()); |
| 333 | |
| 334 | // toggle |
| 335 | onView(withId(R.id.nonselectable_textview)).perform(clickOnTextAtIndex(position)); |
| 336 | sleepForFloatingToolbarPopup(); |
| 337 | assertFalse(textView.hasSelection()); |
| 338 | |
| 339 | onView(withId(R.id.nonselectable_textview)).perform(clickOnTextAtIndex(position)); |
| 340 | sleepForFloatingToolbarPopup(); |
| 341 | assertFloatingToolbarIsDisplayed(); |
| 342 | assertTrue(textView.hasSelection()); |
| 343 | |
| 344 | // click outside |
| 345 | onView(withId(R.id.nonselectable_textview)).perform(clickOnTextAtIndex(0)); |
| 346 | sleepForFloatingToolbarPopup(); |
| 347 | assertFalse(textView.hasSelection()); |
Richard Ledley | 724eff9 | 2017-12-21 10:11:34 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Richard Ledley | 5f2f820 | 2018-02-05 14:55:47 +0000 | [diff] [blame] | 350 | @Test |
| 351 | public void testSelectionRemovedWhenNonselectableTextLosesFocus() throws Throwable { |
Abodunrinwa Toki | 29cb768 | 2018-04-11 21:24:20 +0100 | [diff] [blame] | 352 | final TextLinks.TextLink textLink = addLinkifiedTextToTextView(R.id.nonselectable_textview); |
| 353 | final int position = (textLink.getStart() + textLink.getEnd()) / 2; |
| 354 | final TextView textView = mActivity.findViewById(R.id.nonselectable_textview); |
| 355 | mActivityRule.runOnUiThread(() -> textView.setFocusableInTouchMode(true)); |
Richard Ledley | 5f2f820 | 2018-02-05 14:55:47 +0000 | [diff] [blame] | 356 | |
Abodunrinwa Toki | 29cb768 | 2018-04-11 21:24:20 +0100 | [diff] [blame] | 357 | onView(withId(R.id.nonselectable_textview)).perform(clickOnTextAtIndex(position)); |
Richard Ledley | 5f2f820 | 2018-02-05 14:55:47 +0000 | [diff] [blame] | 358 | sleepForFloatingToolbarPopup(); |
| 359 | assertFloatingToolbarIsDisplayed(); |
Abodunrinwa Toki | 29cb768 | 2018-04-11 21:24:20 +0100 | [diff] [blame] | 360 | assertTrue(textView.hasSelection()); |
Richard Ledley | 5f2f820 | 2018-02-05 14:55:47 +0000 | [diff] [blame] | 361 | |
Abodunrinwa Toki | 29cb768 | 2018-04-11 21:24:20 +0100 | [diff] [blame] | 362 | mActivityRule.runOnUiThread(() -> textView.clearFocus()); |
| 363 | mInstrumentation.waitForIdleSync(); |
Richard Ledley | 5f2f820 | 2018-02-05 14:55:47 +0000 | [diff] [blame] | 364 | sleepForFloatingToolbarPopup(); |
Richard Ledley | 5f2f820 | 2018-02-05 14:55:47 +0000 | [diff] [blame] | 365 | |
Abodunrinwa Toki | 29cb768 | 2018-04-11 21:24:20 +0100 | [diff] [blame] | 366 | assertFalse(textView.hasSelection()); |
Richard Ledley | 5f2f820 | 2018-02-05 14:55:47 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | @Test |
| 370 | public void testSelectionRemovedFromNonselectableTextWhenWindowLosesFocus() throws Throwable { |
| 371 | TextLinks.TextLink textLink = addLinkifiedTextToTextView(R.id.nonselectable_textview); |
| 372 | int nonselectablePosition = (textLink.getStart() + textLink.getEnd()) / 2; |
| 373 | TextView nonselectableTextView = mActivity.findViewById(R.id.nonselectable_textview); |
| 374 | |
| 375 | onView(withId(R.id.nonselectable_textview)) |
| 376 | .perform(clickOnTextAtIndex(nonselectablePosition)); |
| 377 | sleepForFloatingToolbarPopup(); |
| 378 | assertFloatingToolbarIsDisplayed(); |
| 379 | assertTrue(nonselectableTextView.hasSelection()); |
| 380 | |
| 381 | UiDevice device = UiDevice.getInstance(mInstrumentation); |
| 382 | device.openNotification(); |
| 383 | Thread.sleep(2000); |
| 384 | device.pressBack(); |
| 385 | Thread.sleep(2000); |
| 386 | |
| 387 | assertFalse(nonselectableTextView.hasSelection()); |
| 388 | } |
| 389 | |
| 390 | private TextLinks.TextLink addLinkifiedTextToTextView(int id) throws Throwable { |
Richard Ledley | 724eff9 | 2017-12-21 10:11:34 +0000 | [diff] [blame] | 391 | TextView textView = mActivity.findViewById(id); |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 392 | useSystemDefaultTextClassifier(); |
| 393 | TextClassificationManager textClassificationManager = |
| 394 | mActivity.getSystemService(TextClassificationManager.class); |
| 395 | TextClassifier textClassifier = textClassificationManager.getTextClassifier(); |
Abodunrinwa Toki | fe20cdd | 2017-12-12 02:31:25 +0000 | [diff] [blame] | 396 | Spannable content = new SpannableString("Call me at +19148277737"); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 397 | TextLinks.Request request = new TextLinks.Request.Builder(content).build(); |
| 398 | TextLinks links = textClassifier.generateLinks(request); |
| 399 | TextLinksParams applyParams = new TextLinksParams.Builder() |
| 400 | .setApplyStrategy(TextLinks.APPLY_STRATEGY_REPLACE) |
| 401 | .build(); |
| 402 | applyParams.apply(content, links); |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 403 | |
| 404 | mActivityRule.runOnUiThread(() -> { |
| 405 | textView.setText(content); |
| 406 | textView.setMovementMethod(LinkMovementMethod.getInstance()); |
| 407 | }); |
| 408 | mInstrumentation.waitForIdleSync(); |
| 409 | |
| 410 | // Wait for the UI thread to refresh |
| 411 | Thread.sleep(1000); |
| 412 | |
Richard Ledley | 5f2f820 | 2018-02-05 14:55:47 +0000 | [diff] [blame] | 413 | return links.getLinks().iterator().next(); |
Richard Ledley | 26b8722 | 2017-11-30 10:54:08 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | @Test |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 417 | public void testToolbarAndInsertionHandle() { |
Keisuke Kuroyanagi | b0dd77c | 2015-12-02 13:27:01 -0800 | [diff] [blame] | 418 | final String text = "text"; |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 419 | onView(withId(R.id.textview)).perform(replaceText(text)); |
Keisuke Kuroyanagi | b0dd77c | 2015-12-02 13:27:01 -0800 | [diff] [blame] | 420 | onView(withId(R.id.textview)).perform(clickOnTextAtIndex(text.length())); |
Keisuke Kuroyanagi | b0dd77c | 2015-12-02 13:27:01 -0800 | [diff] [blame] | 421 | |
| 422 | onHandleView(com.android.internal.R.id.insertion_handle).perform(click()); |
| 423 | sleepForFloatingToolbarPopup(); |
| 424 | assertFloatingToolbarIsDisplayed(); |
| 425 | |
| 426 | assertFloatingToolbarContainsItem( |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 427 | mActivity.getString(com.android.internal.R.string.selectAll)); |
Keisuke Kuroyanagi | b0dd77c | 2015-12-02 13:27:01 -0800 | [diff] [blame] | 428 | assertFloatingToolbarDoesNotContainItem( |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 429 | mActivity.getString(com.android.internal.R.string.copy)); |
Keisuke Kuroyanagi | b0dd77c | 2015-12-02 13:27:01 -0800 | [diff] [blame] | 430 | assertFloatingToolbarDoesNotContainItem( |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 431 | mActivity.getString(com.android.internal.R.string.cut)); |
Keisuke Kuroyanagi | b0dd77c | 2015-12-02 13:27:01 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 434 | @Test |
| 435 | public void testToolbarAndSelectionHandle() { |
Keisuke Kuroyanagi | b0dd77c | 2015-12-02 13:27:01 -0800 | [diff] [blame] | 436 | final String text = "abcd efg hijk"; |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 437 | onView(withId(R.id.textview)).perform(replaceText(text)); |
Keisuke Kuroyanagi | b0dd77c | 2015-12-02 13:27:01 -0800 | [diff] [blame] | 438 | |
| 439 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(text.indexOf("f"))); |
| 440 | sleepForFloatingToolbarPopup(); |
| 441 | assertFloatingToolbarIsDisplayed(); |
| 442 | |
| 443 | assertFloatingToolbarContainsItem( |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 444 | mActivity.getString(com.android.internal.R.string.selectAll)); |
Keisuke Kuroyanagi | b0dd77c | 2015-12-02 13:27:01 -0800 | [diff] [blame] | 445 | assertFloatingToolbarContainsItem( |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 446 | mActivity.getString(com.android.internal.R.string.copy)); |
Keisuke Kuroyanagi | b0dd77c | 2015-12-02 13:27:01 -0800 | [diff] [blame] | 447 | assertFloatingToolbarContainsItem( |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 448 | mActivity.getString(com.android.internal.R.string.cut)); |
Keisuke Kuroyanagi | b0dd77c | 2015-12-02 13:27:01 -0800 | [diff] [blame] | 449 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 450 | final TextView textView = mActivity.findViewById(R.id.textview); |
Keisuke Kuroyanagi | b0dd77c | 2015-12-02 13:27:01 -0800 | [diff] [blame] | 451 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 452 | .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('a'))); |
| 453 | sleepForFloatingToolbarPopup(); |
| 454 | assertFloatingToolbarIsDisplayed(); |
| 455 | |
| 456 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 457 | .perform(dragHandle(textView, Handle.SELECTION_END, text.length())); |
| 458 | sleepForFloatingToolbarPopup(); |
| 459 | assertFloatingToolbarIsDisplayed(); |
| 460 | |
| 461 | assertFloatingToolbarDoesNotContainItem( |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 462 | mActivity.getString(com.android.internal.R.string.selectAll)); |
Keisuke Kuroyanagi | b0dd77c | 2015-12-02 13:27:01 -0800 | [diff] [blame] | 463 | assertFloatingToolbarContainsItem( |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 464 | mActivity.getString(com.android.internal.R.string.copy)); |
Keisuke Kuroyanagi | b0dd77c | 2015-12-02 13:27:01 -0800 | [diff] [blame] | 465 | assertFloatingToolbarContainsItem( |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 466 | mActivity.getString(com.android.internal.R.string.cut)); |
Abodunrinwa Toki | 8a5e1ae | 2015-09-28 21:59:04 +0100 | [diff] [blame] | 467 | } |
Keisuke Kuroyanagi | da79ee6 | 2015-11-25 16:15:15 +0900 | [diff] [blame] | 468 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 469 | @Test |
| 470 | public void testInsertionHandle() { |
Keisuke Kuroyanagi | e8b3dd4 | 2015-11-26 14:19:27 +0900 | [diff] [blame] | 471 | final String text = "abcd efg hijk "; |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 472 | onView(withId(R.id.textview)).perform(replaceText(text)); |
Keisuke Kuroyanagi | e8b3dd4 | 2015-11-26 14:19:27 +0900 | [diff] [blame] | 473 | |
| 474 | onView(withId(R.id.textview)).perform(clickOnTextAtIndex(text.length())); |
| 475 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(text.length())); |
| 476 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 477 | final TextView textView = mActivity.findViewById(R.id.textview); |
Keisuke Kuroyanagi | e8b3dd4 | 2015-11-26 14:19:27 +0900 | [diff] [blame] | 478 | |
| 479 | onHandleView(com.android.internal.R.id.insertion_handle) |
| 480 | .perform(dragHandle(textView, Handle.INSERTION, text.indexOf('a'))); |
| 481 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(text.indexOf("a"))); |
| 482 | |
| 483 | onHandleView(com.android.internal.R.id.insertion_handle) |
| 484 | .perform(dragHandle(textView, Handle.INSERTION, text.indexOf('f'))); |
| 485 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(text.indexOf("f"))); |
| 486 | } |
| 487 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 488 | @Test |
| 489 | public void testInsertionHandle_multiLine() { |
Keisuke Kuroyanagi | e8b3dd4 | 2015-11-26 14:19:27 +0900 | [diff] [blame] | 490 | final String text = "abcd\n" + "efg\n" + "hijk\n"; |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 491 | onView(withId(R.id.textview)).perform(replaceText(text)); |
Keisuke Kuroyanagi | e8b3dd4 | 2015-11-26 14:19:27 +0900 | [diff] [blame] | 492 | |
| 493 | onView(withId(R.id.textview)).perform(clickOnTextAtIndex(text.length())); |
| 494 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(text.length())); |
| 495 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 496 | final TextView textView = mActivity.findViewById(R.id.textview); |
Keisuke Kuroyanagi | e8b3dd4 | 2015-11-26 14:19:27 +0900 | [diff] [blame] | 497 | |
| 498 | onHandleView(com.android.internal.R.id.insertion_handle) |
| 499 | .perform(dragHandle(textView, Handle.INSERTION, text.indexOf('a'))); |
| 500 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(text.indexOf("a"))); |
| 501 | |
| 502 | onHandleView(com.android.internal.R.id.insertion_handle) |
| 503 | .perform(dragHandle(textView, Handle.INSERTION, text.indexOf('f'))); |
| 504 | onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(text.indexOf("f"))); |
| 505 | } |
| 506 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 507 | @Test |
| 508 | public void testSelectionHandles() { |
Keisuke Kuroyanagi | da79ee6 | 2015-11-25 16:15:15 +0900 | [diff] [blame] | 509 | final String text = "abcd efg hijk lmn"; |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 510 | onView(withId(R.id.textview)).perform(replaceText(text)); |
Abodunrinwa Toki | fc6e25e | 2015-11-30 19:45:32 +0000 | [diff] [blame] | 511 | |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 512 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(text.indexOf('f'))); |
Keisuke Kuroyanagi | da79ee6 | 2015-11-25 16:15:15 +0900 | [diff] [blame] | 513 | |
Abodunrinwa Toki | fc6e25e | 2015-11-30 19:45:32 +0000 | [diff] [blame] | 514 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 515 | .check(matches(isDisplayed())); |
| 516 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 517 | .check(matches(isDisplayed())); |
| 518 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 519 | final TextView textView = mActivity.findViewById(R.id.textview); |
Keisuke Kuroyanagi | da79ee6 | 2015-11-25 16:15:15 +0900 | [diff] [blame] | 520 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 521 | .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('a'))); |
| 522 | onView(withId(R.id.textview)).check(hasSelection("abcd efg")); |
| 523 | |
| 524 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 525 | .perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('k') + 1)); |
| 526 | onView(withId(R.id.textview)).check(hasSelection("abcd efg hijk")); |
| 527 | } |
Keisuke Kuroyanagi | c24c2bb | 2015-11-24 18:48:28 +0900 | [diff] [blame] | 528 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 529 | @Test |
| 530 | public void testSelectionHandles_bidi() { |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 531 | final String text = "abc \u0621\u0622\u0623 def"; |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 532 | onView(withId(R.id.textview)).perform(replaceText(text)); |
| 533 | |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 534 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(text.indexOf('\u0622'))); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 535 | |
| 536 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 537 | .check(matches(isDisplayed())); |
| 538 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 539 | .check(matches(isDisplayed())); |
| 540 | |
| 541 | onView(withId(R.id.textview)).check(hasSelection("\u0621\u0622\u0623")); |
| 542 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 543 | final TextView textView = mActivity.findViewById(R.id.textview); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 544 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 545 | .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('f'))); |
| 546 | onView(withId(R.id.textview)).check(hasSelection("\u0621\u0622\u0623")); |
| 547 | |
| 548 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 549 | .perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('a'))); |
| 550 | onView(withId(R.id.textview)).check(hasSelection("\u0621\u0622\u0623")); |
| 551 | |
| 552 | onHandleView(com.android.internal.R.id.selection_start_handle) |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 553 | .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('\u0623'), |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 554 | false)); |
| 555 | onView(withId(R.id.textview)).check(hasSelection("\u0623")); |
| 556 | |
| 557 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 558 | .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('\u0621'), |
| 559 | false)); |
| 560 | onView(withId(R.id.textview)).check(hasSelection("\u0621\u0622\u0623")); |
| 561 | |
| 562 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 563 | .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('a'))); |
| 564 | onView(withId(R.id.textview)).check(hasSelection("abc \u0621\u0622\u0623")); |
| 565 | |
| 566 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 567 | .perform(dragHandle(textView, Handle.SELECTION_END, text.length())); |
| 568 | onView(withId(R.id.textview)).check(hasSelection("abc \u0621\u0622\u0623 def")); |
| 569 | } |
| 570 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 571 | @Test |
| 572 | public void testSelectionHandles_multiLine() { |
Keisuke Kuroyanagi | c24c2bb | 2015-11-24 18:48:28 +0900 | [diff] [blame] | 573 | final String text = "abcd\n" + "efg\n" + "hijk\n" + "lmn\n" + "opqr"; |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 574 | onView(withId(R.id.textview)).perform(replaceText(text)); |
| 575 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(text.indexOf('i'))); |
Keisuke Kuroyanagi | c24c2bb | 2015-11-24 18:48:28 +0900 | [diff] [blame] | 576 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 577 | final TextView textView = mActivity.findViewById(R.id.textview); |
Keisuke Kuroyanagi | c24c2bb | 2015-11-24 18:48:28 +0900 | [diff] [blame] | 578 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 579 | .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('e'))); |
| 580 | onView(withId(R.id.textview)).check(hasSelection("efg\nhijk")); |
| 581 | |
| 582 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 583 | .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('a'))); |
| 584 | onView(withId(R.id.textview)).check(hasSelection("abcd\nefg\nhijk")); |
| 585 | |
| 586 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 587 | .perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('n') + 1)); |
| 588 | onView(withId(R.id.textview)).check(hasSelection("abcd\nefg\nhijk\nlmn")); |
| 589 | |
| 590 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 591 | .perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('r') + 1)); |
| 592 | onView(withId(R.id.textview)).check(hasSelection("abcd\nefg\nhijk\nlmn\nopqr")); |
| 593 | } |
Keisuke Kuroyanagi | cb6e842 | 2015-11-24 18:51:48 +0900 | [diff] [blame] | 594 | |
Abodunrinwa Toki | 89550e8 | 2017-10-31 03:46:46 +0000 | [diff] [blame] | 595 | @Suppress // Consistently failing. |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 596 | @Test |
| 597 | public void testSelectionHandles_multiLine_rtl() { |
Keisuke Kuroyanagi | 2645414 | 2015-12-02 15:04:57 -0800 | [diff] [blame] | 598 | // Arabic text. |
| 599 | final String text = "\u062A\u062B\u062C\n" + "\u062D\u062E\u062F\n" |
| 600 | + "\u0630\u0631\u0632\n" + "\u0633\u0634\u0635\n" + "\u0636\u0637\u0638\n" |
| 601 | + "\u0639\u063A\u063B"; |
Keisuke Kuroyanagi | 2645414 | 2015-12-02 15:04:57 -0800 | [diff] [blame] | 602 | onView(withId(R.id.textview)).perform(replaceText(text)); |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 603 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(text.indexOf('\u0634'))); |
Keisuke Kuroyanagi | 2645414 | 2015-12-02 15:04:57 -0800 | [diff] [blame] | 604 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 605 | final TextView textView = mActivity.findViewById(R.id.textview); |
Keisuke Kuroyanagi | 2645414 | 2015-12-02 15:04:57 -0800 | [diff] [blame] | 606 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 607 | .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('\u062E'))); |
| 608 | onView(withId(R.id.textview)).check(hasSelection( |
| 609 | text.substring(text.indexOf('\u062D'), text.indexOf('\u0635') + 1))); |
| 610 | |
| 611 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 612 | .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('\u062A'))); |
| 613 | onView(withId(R.id.textview)).check(hasSelection( |
| 614 | text.substring(text.indexOf('\u062A'), text.indexOf('\u0635') + 1))); |
| 615 | |
| 616 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 617 | .perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('\u0638'))); |
| 618 | onView(withId(R.id.textview)).check(hasSelection( |
| 619 | text.substring(text.indexOf('\u062A'), text.indexOf('\u0638') + 1))); |
| 620 | |
| 621 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 622 | .perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('\u063B'))); |
| 623 | onView(withId(R.id.textview)).check(hasSelection(text)); |
| 624 | } |
| 625 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 626 | @Test |
| 627 | public void testSelectionHandles_doesNotPassAnotherHandle() { |
Keisuke Kuroyanagi | cb6e842 | 2015-11-24 18:51:48 +0900 | [diff] [blame] | 628 | final String text = "abcd efg hijk lmn"; |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 629 | onView(withId(R.id.textview)).perform(replaceText(text)); |
| 630 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(text.indexOf('f'))); |
Keisuke Kuroyanagi | cb6e842 | 2015-11-24 18:51:48 +0900 | [diff] [blame] | 631 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 632 | final TextView textView = mActivity.findViewById(R.id.textview); |
Keisuke Kuroyanagi | cb6e842 | 2015-11-24 18:51:48 +0900 | [diff] [blame] | 633 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 634 | .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('l'))); |
| 635 | onView(withId(R.id.textview)).check(hasSelection("g")); |
| 636 | |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 637 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(text.indexOf('f'))); |
Keisuke Kuroyanagi | cb6e842 | 2015-11-24 18:51:48 +0900 | [diff] [blame] | 638 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 639 | .perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('a'))); |
| 640 | onView(withId(R.id.textview)).check(hasSelection("e")); |
| 641 | } |
| 642 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 643 | @Test |
| 644 | public void testSelectionHandles_doesNotPassAnotherHandle_multiLine() { |
Keisuke Kuroyanagi | cb6e842 | 2015-11-24 18:51:48 +0900 | [diff] [blame] | 645 | final String text = "abcd\n" + "efg\n" + "hijk\n" + "lmn\n" + "opqr"; |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 646 | onView(withId(R.id.textview)).perform(replaceText(text)); |
| 647 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(text.indexOf('i'))); |
Keisuke Kuroyanagi | cb6e842 | 2015-11-24 18:51:48 +0900 | [diff] [blame] | 648 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 649 | final TextView textView = mActivity.findViewById(R.id.textview); |
Keisuke Kuroyanagi | cb6e842 | 2015-11-24 18:51:48 +0900 | [diff] [blame] | 650 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 651 | .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('r') + 1)); |
| 652 | onView(withId(R.id.textview)).check(hasSelection("k")); |
| 653 | |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 654 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(text.indexOf('i'))); |
Keisuke Kuroyanagi | cb6e842 | 2015-11-24 18:51:48 +0900 | [diff] [blame] | 655 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 656 | .perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('a'))); |
| 657 | onView(withId(R.id.textview)).check(hasSelection("h")); |
| 658 | } |
Keisuke Kuroyanagi | ec913c0 | 2015-11-24 18:56:20 +0900 | [diff] [blame] | 659 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 660 | @Test |
| 661 | public void testSelectionHandles_snapToWordBoundary() { |
Keisuke Kuroyanagi | ec913c0 | 2015-11-24 18:56:20 +0900 | [diff] [blame] | 662 | final String text = "abcd efg hijk lmn opqr"; |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 663 | onView(withId(R.id.textview)).perform(replaceText(text)); |
| 664 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(text.indexOf('i'))); |
Keisuke Kuroyanagi | ec913c0 | 2015-11-24 18:56:20 +0900 | [diff] [blame] | 665 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 666 | final TextView textView = mActivity.findViewById(R.id.textview); |
Keisuke Kuroyanagi | ec913c0 | 2015-11-24 18:56:20 +0900 | [diff] [blame] | 667 | |
| 668 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 669 | .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('f'))); |
| 670 | onView(withId(R.id.textview)).check(hasSelection("efg hijk")); |
| 671 | |
| 672 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 673 | .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('d') + 1)); |
| 674 | onView(withId(R.id.textview)).check(hasSelection("efg hijk")); |
| 675 | |
| 676 | |
| 677 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 678 | .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('c'))); |
| 679 | onView(withId(R.id.textview)).check(hasSelection("abcd efg hijk")); |
| 680 | |
| 681 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 682 | .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('d'))); |
| 683 | onView(withId(R.id.textview)).check(hasSelection("d efg hijk")); |
| 684 | |
| 685 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 686 | .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('b'))); |
| 687 | onView(withId(R.id.textview)).check(hasSelection("bcd efg hijk")); |
| 688 | |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 689 | onView(withId(R.id.textview)).perform(click()); |
| 690 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(text.indexOf('i'))); |
Keisuke Kuroyanagi | ec913c0 | 2015-11-24 18:56:20 +0900 | [diff] [blame] | 691 | |
| 692 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 693 | .perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('n'))); |
| 694 | onView(withId(R.id.textview)).check(hasSelection("hijk lmn")); |
| 695 | |
| 696 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 697 | .perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('o'))); |
| 698 | onView(withId(R.id.textview)).check(hasSelection("hijk lmn")); |
| 699 | |
| 700 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 701 | .perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('q'))); |
| 702 | onView(withId(R.id.textview)).check(hasSelection("hijk lmn opqr")); |
| 703 | |
| 704 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 705 | .perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('p'))); |
| 706 | onView(withId(R.id.textview)).check(hasSelection("hijk lmn o")); |
| 707 | |
| 708 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 709 | .perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('r'))); |
| 710 | onView(withId(R.id.textview)).check(hasSelection("hijk lmn opq")); |
| 711 | } |
| 712 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 713 | @Test |
| 714 | public void testSelectionHandles_snapToWordBoundary_multiLine() { |
Keisuke Kuroyanagi | ec913c0 | 2015-11-24 18:56:20 +0900 | [diff] [blame] | 715 | final String text = "abcd efg\n" + "hijk lmn\n" + "opqr stu"; |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 716 | onView(withId(R.id.textview)).perform(replaceText(text)); |
| 717 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(text.indexOf('m'))); |
Keisuke Kuroyanagi | ec913c0 | 2015-11-24 18:56:20 +0900 | [diff] [blame] | 718 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 719 | final TextView textView = mActivity.findViewById(R.id.textview); |
Keisuke Kuroyanagi | ec913c0 | 2015-11-24 18:56:20 +0900 | [diff] [blame] | 720 | |
| 721 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 722 | .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('c'))); |
| 723 | onView(withId(R.id.textview)).check(hasSelection("abcd efg\nhijk lmn")); |
| 724 | |
| 725 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 726 | .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('g'))); |
| 727 | onView(withId(R.id.textview)).check(hasSelection("g\nhijk lmn")); |
| 728 | |
| 729 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 730 | .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('m'))); |
| 731 | onView(withId(R.id.textview)).check(hasSelection("lmn")); |
| 732 | |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 733 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(text.indexOf('i'))); |
Keisuke Kuroyanagi | ec913c0 | 2015-11-24 18:56:20 +0900 | [diff] [blame] | 734 | |
| 735 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 736 | .perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('u'))); |
| 737 | onView(withId(R.id.textview)).check(hasSelection("hijk lmn\nopqr stu")); |
| 738 | |
| 739 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 740 | .perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('p'))); |
| 741 | onView(withId(R.id.textview)).check(hasSelection("hijk lmn\no")); |
| 742 | |
| 743 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 744 | .perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('i'))); |
| 745 | onView(withId(R.id.textview)).check(hasSelection("hijk")); |
| 746 | } |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 747 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 748 | @Test |
Abodunrinwa Toki | deb2f49 | 2017-11-06 18:55:17 +0000 | [diff] [blame] | 749 | public void testSelectionHandles_visibleEvenWithEmptyMenu() { |
| 750 | ((TextView) mActivity.findViewById(R.id.textview)).setCustomSelectionActionModeCallback( |
| 751 | new ActionMode.Callback() { |
| 752 | @Override |
| 753 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 754 | menu.clear(); |
| 755 | return true; |
| 756 | } |
| 757 | |
| 758 | @Override |
| 759 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 760 | menu.clear(); |
| 761 | return true; |
| 762 | } |
| 763 | |
| 764 | @Override |
| 765 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 766 | return false; |
| 767 | } |
| 768 | |
| 769 | @Override |
| 770 | public void onDestroyActionMode(ActionMode mode) {} |
| 771 | }); |
| 772 | final String text = "abcd efg hijk lmn"; |
| 773 | onView(withId(R.id.textview)).perform(replaceText(text)); |
| 774 | |
| 775 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(text.indexOf('f'))); |
| 776 | |
| 777 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 778 | .check(matches(isDisplayed())); |
| 779 | onHandleView(com.android.internal.R.id.selection_end_handle) |
| 780 | .check(matches(isDisplayed())); |
| 781 | } |
| 782 | |
| 783 | @Test |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 784 | public void testSetSelectionAndActionMode() throws Throwable { |
Abodunrinwa Toki | 6264cbd | 2017-11-06 15:12:52 +0000 | [diff] [blame] | 785 | final TextView textView = mActivity.findViewById(R.id.textview); |
| 786 | final ActionMode.Callback amCallback = mock(ActionMode.Callback.class); |
| 787 | when(amCallback.onCreateActionMode(any(ActionMode.class), any(Menu.class))) |
| 788 | .thenReturn(true); |
| 789 | when(amCallback.onPrepareActionMode(any(ActionMode.class), any(Menu.class))) |
| 790 | .thenReturn(true); |
| 791 | textView.setCustomSelectionActionModeCallback(amCallback); |
| 792 | |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 793 | final String text = "abc def"; |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 794 | onView(withId(R.id.textview)).perform(replaceText(text)); |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 795 | mActivityRule.runOnUiThread( |
| 796 | () -> Selection.setSelection((Spannable) textView.getText(), 0, 3)); |
| 797 | mInstrumentation.waitForIdleSync(); |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 798 | // Don't automatically start action mode. |
Abodunrinwa Toki | 6264cbd | 2017-11-06 15:12:52 +0000 | [diff] [blame] | 799 | verify(amCallback, never()).onCreateActionMode(any(ActionMode.class), any(Menu.class)); |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 800 | // Make sure that "Select All" is included in the selection action mode when the entire text |
| 801 | // is not selected. |
Keisuke Kuroyanagi | 99e862318 | 2016-11-16 20:05:14 +0900 | [diff] [blame] | 802 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(text.indexOf('e'))); |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 803 | sleepForFloatingToolbarPopup(); |
| 804 | assertFloatingToolbarIsDisplayed(); |
| 805 | // Changing the selection range by API should not interrupt the selection action mode. |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 806 | mActivityRule.runOnUiThread( |
| 807 | () -> Selection.setSelection((Spannable) textView.getText(), 0, 3)); |
| 808 | mInstrumentation.waitForIdleSync(); |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 809 | sleepForFloatingToolbarPopup(); |
| 810 | assertFloatingToolbarIsDisplayed(); |
| 811 | assertFloatingToolbarContainsItem( |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 812 | mActivity.getString(com.android.internal.R.string.selectAll)); |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 813 | // Make sure that "Select All" is no longer included when the entire text is selected by |
| 814 | // API. |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 815 | mActivityRule.runOnUiThread( |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 816 | () -> Selection.setSelection((Spannable) textView.getText(), 0, text.length())); |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 817 | mInstrumentation.waitForIdleSync(); |
| 818 | |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 819 | sleepForFloatingToolbarPopup(); |
| 820 | assertFloatingToolbarIsDisplayed(); |
| 821 | assertFloatingToolbarDoesNotContainItem( |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 822 | mActivity.getString(com.android.internal.R.string.selectAll)); |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 823 | // Make sure that shrinking the selection range to cursor (an empty range) by API |
| 824 | // terminates selection action mode and does not trigger the insertion action mode. |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 825 | mActivityRule.runOnUiThread( |
| 826 | () -> Selection.setSelection((Spannable) textView.getText(), 0)); |
| 827 | mInstrumentation.waitForIdleSync(); |
| 828 | |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 829 | // Make sure that user click can trigger the insertion action mode. |
| 830 | onView(withId(R.id.textview)).perform(clickOnTextAtIndex(text.length())); |
| 831 | onHandleView(com.android.internal.R.id.insertion_handle).perform(click()); |
| 832 | sleepForFloatingToolbarPopup(); |
| 833 | assertFloatingToolbarIsDisplayed(); |
| 834 | // Make sure that an existing insertion action mode keeps alive after the insertion point is |
| 835 | // moved by API. |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 836 | mActivityRule.runOnUiThread( |
| 837 | () -> Selection.setSelection((Spannable) textView.getText(), 0)); |
| 838 | mInstrumentation.waitForIdleSync(); |
| 839 | |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 840 | sleepForFloatingToolbarPopup(); |
| 841 | assertFloatingToolbarIsDisplayed(); |
| 842 | assertFloatingToolbarDoesNotContainItem( |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 843 | mActivity.getString(com.android.internal.R.string.copy)); |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 844 | // Make sure that selection action mode is started after selection is created by API when |
| 845 | // insertion action mode is active. |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 846 | mActivityRule.runOnUiThread( |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 847 | () -> Selection.setSelection((Spannable) textView.getText(), 1, text.length())); |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 848 | mInstrumentation.waitForIdleSync(); |
| 849 | |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 850 | sleepForFloatingToolbarPopup(); |
| 851 | assertFloatingToolbarIsDisplayed(); |
| 852 | assertFloatingToolbarContainsItem( |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 853 | mActivity.getString(com.android.internal.R.string.copy)); |
Keisuke Kuroyanagi | c477b58 | 2016-03-15 15:38:40 +0900 | [diff] [blame] | 854 | } |
Keisuke Kuroyanagi | 183fd50 | 2016-04-01 15:00:53 +0900 | [diff] [blame] | 855 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 856 | @Test |
| 857 | public void testTransientState() throws Throwable { |
Keisuke Kuroyanagi | 183fd50 | 2016-04-01 15:00:53 +0900 | [diff] [blame] | 858 | final String text = "abc def"; |
Keisuke Kuroyanagi | 183fd50 | 2016-04-01 15:00:53 +0900 | [diff] [blame] | 859 | onView(withId(R.id.textview)).perform(replaceText(text)); |
| 860 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 861 | final TextView textView = mActivity.findViewById(R.id.textview); |
Keisuke Kuroyanagi | 183fd50 | 2016-04-01 15:00:53 +0900 | [diff] [blame] | 862 | assertFalse(textView.hasTransientState()); |
| 863 | |
| 864 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(text.indexOf('b'))); |
| 865 | // hasTransientState should return true when user generated selection is active. |
| 866 | assertTrue(textView.hasTransientState()); |
| 867 | onView(withId(R.id.textview)).perform(clickOnTextAtIndex(text.indexOf('d'))); |
| 868 | // hasTransientState should return false as the selection has been cleared. |
| 869 | assertFalse(textView.hasTransientState()); |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 870 | mActivityRule.runOnUiThread( |
Keisuke Kuroyanagi | 183fd50 | 2016-04-01 15:00:53 +0900 | [diff] [blame] | 871 | () -> Selection.setSelection((Spannable) textView.getText(), 0, text.length())); |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 872 | mInstrumentation.waitForIdleSync(); |
| 873 | |
Keisuke Kuroyanagi | 183fd50 | 2016-04-01 15:00:53 +0900 | [diff] [blame] | 874 | // hasTransientState should return false when selection is created by API. |
| 875 | assertFalse(textView.hasTransientState()); |
| 876 | } |
Abodunrinwa Toki | 6eecdc9 | 2017-04-11 05:15:25 +0100 | [diff] [blame] | 877 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 878 | @Test |
| 879 | public void testResetMenuItemTitle() throws Throwable { |
| 880 | mActivity.getSystemService(TextClassificationManager.class).setTextClassifier(null); |
| 881 | final TextView textView = mActivity.findViewById(R.id.textview); |
Abodunrinwa Toki | 7b97180 | 2017-05-23 19:01:56 +0100 | [diff] [blame] | 882 | final int itemId = 1; |
| 883 | final String title1 = " AFIGBO"; |
| 884 | final int index = title1.indexOf('I'); |
| 885 | final String title2 = title1.substring(index); |
| 886 | final String[] title = new String[]{title1}; |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 887 | mActivityRule.runOnUiThread(() -> textView.setCustomSelectionActionModeCallback( |
Abodunrinwa Toki | 7b97180 | 2017-05-23 19:01:56 +0100 | [diff] [blame] | 888 | new ActionMode.Callback() { |
| 889 | @Override |
| 890 | public boolean onCreateActionMode(ActionMode actionMode, Menu menu) { |
| 891 | return true; |
| 892 | } |
| 893 | |
| 894 | @Override |
| 895 | public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) { |
| 896 | menu.removeItem(itemId); |
| 897 | menu.add(Menu.NONE /* group */, itemId, 0 /* order */, title[0]); |
| 898 | return true; |
| 899 | } |
| 900 | |
| 901 | @Override |
| 902 | public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) { |
| 903 | return false; |
| 904 | } |
| 905 | |
| 906 | @Override |
| 907 | public void onDestroyActionMode(ActionMode actionMode) { |
| 908 | } |
| 909 | })); |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 910 | mInstrumentation.waitForIdleSync(); |
| 911 | |
Abodunrinwa Toki | 7b97180 | 2017-05-23 19:01:56 +0100 | [diff] [blame] | 912 | onView(withId(R.id.textview)).perform(replaceText(title1)); |
| 913 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(index)); |
| 914 | sleepForFloatingToolbarPopup(); |
| 915 | assertFloatingToolbarContainsItem(title1); |
| 916 | |
| 917 | // Change the menu item title. |
| 918 | title[0] = title2; |
| 919 | // Change the selection to invalidate the action mode without restarting it. |
| 920 | onHandleView(com.android.internal.R.id.selection_start_handle) |
| 921 | .perform(dragHandle(textView, Handle.SELECTION_START, index)); |
| 922 | sleepForFloatingToolbarPopup(); |
| 923 | assertFloatingToolbarContainsItem(title2); |
| 924 | } |
| 925 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 926 | @Test |
| 927 | public void testAssistItemIsAtIndexZero() throws Throwable { |
Abodunrinwa Toki | 76b51dc | 2017-07-13 23:37:11 +0100 | [diff] [blame] | 928 | useSystemDefaultTextClassifier(); |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 929 | final TextView textView = mActivity.findViewById(R.id.textview); |
| 930 | mActivityRule.runOnUiThread(() -> textView.setCustomSelectionActionModeCallback( |
Abodunrinwa Toki | 6eecdc9 | 2017-04-11 05:15:25 +0100 | [diff] [blame] | 931 | new ActionMode.Callback() { |
| 932 | @Override |
| 933 | public boolean onCreateActionMode(ActionMode actionMode, Menu menu) { |
| 934 | // Create another item at order position 0 to confirm that it will never be |
| 935 | // placed before the textAssist item. |
| 936 | menu.add(Menu.NONE, 0 /* id */, 0 /* order */, "Test"); |
| 937 | return true; |
| 938 | } |
| 939 | |
| 940 | @Override |
| 941 | public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) { |
| 942 | return true; |
| 943 | } |
| 944 | |
| 945 | @Override |
| 946 | public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) { |
| 947 | return false; |
| 948 | } |
| 949 | |
| 950 | @Override |
Abodunrinwa Toki | ea6cb12 | 2017-04-28 22:14:13 +0100 | [diff] [blame] | 951 | public void onDestroyActionMode(ActionMode actionMode) { |
| 952 | } |
Abodunrinwa Toki | 6eecdc9 | 2017-04-11 05:15:25 +0100 | [diff] [blame] | 953 | })); |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 954 | mInstrumentation.waitForIdleSync(); |
Abodunrinwa Toki | 6eecdc9 | 2017-04-11 05:15:25 +0100 | [diff] [blame] | 955 | final String text = "droid@android.com"; |
| 956 | |
| 957 | onView(withId(R.id.textview)).perform(replaceText(text)); |
| 958 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(text.indexOf('@'))); |
| 959 | sleepForFloatingToolbarPopup(); |
| 960 | assertFloatingToolbarItemIndex(android.R.id.textAssist, 0); |
| 961 | } |
Abodunrinwa Toki | ea6cb12 | 2017-04-28 22:14:13 +0100 | [diff] [blame] | 962 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 963 | @Test |
Abodunrinwa Toki | 76b51dc | 2017-07-13 23:37:11 +0100 | [diff] [blame] | 964 | public void testNoAssistItemForPasswordField() throws Throwable { |
| 965 | useSystemDefaultTextClassifier(); |
| 966 | final TextView textView = mActivity.findViewById(R.id.textview); |
| 967 | mActivityRule.runOnUiThread(() -> { |
| 968 | textView.setInputType( |
| 969 | InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); |
| 970 | }); |
| 971 | mInstrumentation.waitForIdleSync(); |
| 972 | final String password = "afigbo@android.com"; |
| 973 | |
| 974 | onView(withId(R.id.textview)).perform(replaceText(password)); |
| 975 | onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(password.indexOf('@'))); |
| 976 | sleepForFloatingToolbarPopup(); |
| 977 | assertFloatingToolbarDoesNotContainItem(android.R.id.textAssist); |
| 978 | } |
| 979 | |
| 980 | @Test |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 981 | public void testPastePlainText_menuAction() { |
Abodunrinwa Toki | ea6cb12 | 2017-04-28 22:14:13 +0100 | [diff] [blame] | 982 | initializeClipboardWithText(TextStyle.STYLED); |
| 983 | |
| 984 | onView(withId(R.id.textview)).perform(replaceText("")); |
| 985 | onView(withId(R.id.textview)).perform(longClick()); |
| 986 | sleepForFloatingToolbarPopup(); |
| 987 | clickFloatingToolbarItem( |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 988 | mActivity.getString(com.android.internal.R.string.paste_as_plain_text)); |
| 989 | mInstrumentation.waitForIdleSync(); |
Abodunrinwa Toki | ea6cb12 | 2017-04-28 22:14:13 +0100 | [diff] [blame] | 990 | |
| 991 | onView(withId(R.id.textview)).check(matches(withText("styledtext"))); |
| 992 | onView(withId(R.id.textview)).check(doesNotHaveStyledText()); |
| 993 | } |
| 994 | |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 995 | @Test |
Abodunrinwa Toki | ea6cb12 | 2017-04-28 22:14:13 +0100 | [diff] [blame] | 996 | public void testPastePlainText_noMenuItemForPlainText() { |
| 997 | initializeClipboardWithText(TextStyle.PLAIN); |
| 998 | |
| 999 | onView(withId(R.id.textview)).perform(replaceText("")); |
| 1000 | onView(withId(R.id.textview)).perform(longClick()); |
| 1001 | sleepForFloatingToolbarPopup(); |
| 1002 | |
| 1003 | assertFloatingToolbarDoesNotContainItem( |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 1004 | mActivity.getString(com.android.internal.R.string.paste_as_plain_text)); |
Abodunrinwa Toki | ea6cb12 | 2017-04-28 22:14:13 +0100 | [diff] [blame] | 1005 | } |
| 1006 | |
Abodunrinwa Toki | 76b51dc | 2017-07-13 23:37:11 +0100 | [diff] [blame] | 1007 | private void useSystemDefaultTextClassifier() { |
| 1008 | mActivity.getSystemService(TextClassificationManager.class).setTextClassifier(null); |
| 1009 | } |
| 1010 | |
Abodunrinwa Toki | ea6cb12 | 2017-04-28 22:14:13 +0100 | [diff] [blame] | 1011 | private void initializeClipboardWithText(TextStyle textStyle) { |
| 1012 | final ClipData clip; |
| 1013 | switch (textStyle) { |
| 1014 | case STYLED: |
| 1015 | clip = ClipData.newHtmlText("html", "styledtext", "<b>styledtext</b>"); |
| 1016 | break; |
| 1017 | case PLAIN: |
| 1018 | clip = ClipData.newPlainText("plain", "plaintext"); |
| 1019 | break; |
| 1020 | default: |
| 1021 | throw new IllegalArgumentException("Invalid text style"); |
| 1022 | } |
Siyamed Sinir | 2191418 | 2017-06-16 15:24:00 -0700 | [diff] [blame] | 1023 | mActivity.getWindow().getDecorView().post(() -> |
| 1024 | mActivity.getSystemService(ClipboardManager.class).setPrimaryClip(clip)); |
| 1025 | mInstrumentation.waitForIdleSync(); |
Abodunrinwa Toki | ea6cb12 | 2017-04-28 22:14:13 +0100 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | private enum TextStyle { |
| 1029 | PLAIN, STYLED |
| 1030 | } |
Abodunrinwa Toki | 223c839 | 2015-07-24 20:55:59 -0700 | [diff] [blame] | 1031 | } |