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