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 20 import static android.widget.espresso.ContextMenuUtils.assertContextMenuContainsItemDisabled; 21 import static android.widget.espresso.ContextMenuUtils.assertContextMenuContainsItemEnabled; 22 import static android.widget.espresso.ContextMenuUtils.assertContextMenuIsNotDisplayed; 23 import static android.widget.espresso.DragHandleUtils.assertNoSelectionHandles; 24 import static android.widget.espresso.DragHandleUtils.onHandleView; 25 import static android.widget.espresso.TextViewActions.mouseClickOnTextAtIndex; 26 import static android.widget.espresso.TextViewActions.mouseDoubleClickOnTextAtIndex; 27 import static android.widget.espresso.TextViewActions.mouseLongClickOnTextAtIndex; 28 import static android.widget.espresso.TextViewActions.mouseDoubleClickAndDragOnText; 29 import static android.widget.espresso.TextViewActions.mouseDragOnText; 30 import static android.widget.espresso.TextViewActions.mouseLongClickAndDragOnText; 31 import static android.widget.espresso.TextViewActions.mouseTripleClickAndDragOnText; 32 import static android.widget.espresso.TextViewActions.mouseTripleClickOnTextAtIndex; 33 import static android.widget.espresso.TextViewAssertions.hasInsertionPointerAtIndex; 34 import static android.widget.espresso.TextViewAssertions.hasSelection; 35 36 import static android.support.test.espresso.Espresso.onView; 37 import static android.support.test.espresso.Espresso.pressBack; 38 import static android.support.test.espresso.action.ViewActions.click; 39 import static android.support.test.espresso.action.ViewActions.replaceText; 40 import static android.support.test.espresso.action.ViewActions.typeTextIntoFocusedView; 41 import static android.support.test.espresso.assertion.ViewAssertions.matches; 42 import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; 43 import static android.support.test.espresso.matcher.ViewMatchers.withId; 44 import static android.support.test.espresso.matcher.ViewMatchers.withText; 45 46 import com.android.frameworks.coretests.R; 47 48 import android.test.ActivityInstrumentationTestCase2; 49 import android.test.suitebuilder.annotation.SmallTest; 50 import android.view.MotionEvent; 51 52 /** 53 * Tests mouse interaction of the TextView widget from an Activity 54 */ 55 public class TextViewActivityMouseTest extends ActivityInstrumentationTestCase2<TextViewActivity>{ 56 57 public TextViewActivityMouseTest() { 58 super(TextViewActivity.class); 59 } 60 61 @Override 62 public void setUp() throws Exception { 63 super.setUp(); 64 getActivity(); 65 } 66 67 @SmallTest 68 public void testSelectTextByDrag() throws Exception { 69 final String helloWorld = "Hello world!"; 70 onView(withId(R.id.textview)).perform(click()); 71 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); 72 73 assertNoSelectionHandles(); 74 75 onView(withId(R.id.textview)).perform( 76 mouseDragOnText(helloWorld.indexOf("llo"), helloWorld.indexOf("ld!"))); 77 78 onView(withId(R.id.textview)).check(hasSelection("llo wor")); 79 80 onHandleView(com.android.internal.R.id.selection_start_handle) 81 .check(matches(isDisplayed())); 82 onHandleView(com.android.internal.R.id.selection_end_handle) 83 .check(matches(isDisplayed())); 84 85 onView(withId(R.id.textview)).perform(mouseClickOnTextAtIndex(helloWorld.indexOf("w"))); 86 onView(withId(R.id.textview)).check(hasSelection("")); 87 88 assertNoSelectionHandles(); 89 } 90 91 @SmallTest 92 public void testSelectTextByDrag_reverse() throws Exception { 93 final String helloWorld = "Hello world!"; 94 onView(withId(R.id.textview)).perform(click()); 95 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); 96 onView(withId(R.id.textview)).perform( 97 mouseDragOnText( helloWorld.indexOf("ld!"), helloWorld.indexOf("llo"))); 98 99 onView(withId(R.id.textview)).check(hasSelection("llo wor")); 100 } 101 102 @SmallTest 103 public void testContextMenu() throws Exception { 104 final String text = "abc def ghi."; 105 onView(withId(R.id.textview)).perform(click()); 106 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(text)); 107 108 assertContextMenuIsNotDisplayed(); 109 110 onView(withId(R.id.textview)).perform( 111 mouseClickOnTextAtIndex(text.indexOf("d"), MotionEvent.BUTTON_SECONDARY)); 112 113 assertContextMenuContainsItemDisabled( 114 getActivity().getString(com.android.internal.R.string.copy)); 115 assertContextMenuContainsItemEnabled( 116 getActivity().getString(com.android.internal.R.string.undo)); 117 118 // Hide context menu. 119 pressBack(); 120 assertContextMenuIsNotDisplayed(); 121 122 onView(withId(R.id.textview)).perform( 123 mouseDragOnText(text.indexOf("c"), text.indexOf("h"))); 124 onView(withId(R.id.textview)).perform( 125 mouseClickOnTextAtIndex(text.indexOf("d"), MotionEvent.BUTTON_SECONDARY)); 126 127 assertContextMenuContainsItemEnabled( 128 getActivity().getString(com.android.internal.R.string.copy)); 129 assertContextMenuContainsItemEnabled( 130 getActivity().getString(com.android.internal.R.string.undo)); 131 132 // Hide context menu. 133 pressBack(); 134 135 onView(withId(R.id.textview)).check(hasSelection("c def g")); 136 137 onView(withId(R.id.textview)).perform( 138 mouseClickOnTextAtIndex(text.indexOf("i"), MotionEvent.BUTTON_SECONDARY)); 139 assertContextMenuContainsItemDisabled( 140 getActivity().getString(com.android.internal.R.string.copy)); 141 assertContextMenuContainsItemEnabled( 142 getActivity().getString(com.android.internal.R.string.undo)); 143 144 // Hide context menu. 145 pressBack(); 146 147 onView(withId(R.id.textview)).check(hasSelection("")); 148 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(text.indexOf("i"))); 149 150 // TODO: Add tests for suggestions 151 } 152 153 @SmallTest 154 public void testDragAndDrop() throws Exception { 155 final String text = "abc def ghi."; 156 onView(withId(R.id.textview)).perform(click()); 157 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(text)); 158 onView(withId(R.id.textview)).perform( 159 mouseDragOnText(text.indexOf("d"), text.indexOf("f") + 1)); 160 161 onView(withId(R.id.textview)).perform( 162 mouseDragOnText(text.indexOf("e"), text.length())); 163 164 onView(withId(R.id.textview)).check(matches(withText("abc ghi.def"))); 165 onView(withId(R.id.textview)).check(hasSelection("")); 166 assertNoSelectionHandles(); 167 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex("abc ghi.def".length())); 168 } 169 170 @SmallTest 171 public void testDragAndDrop_longClick() throws Exception { 172 final String text = "abc def ghi."; 173 onView(withId(R.id.textview)).perform(click()); 174 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(text)); 175 onView(withId(R.id.textview)).perform( 176 mouseDragOnText(text.indexOf("d"), text.indexOf("f") + 1)); 177 178 onView(withId(R.id.textview)).perform( 179 mouseLongClickAndDragOnText(text.indexOf("e"), text.length())); 180 181 onView(withId(R.id.textview)).check(matches(withText("abc ghi.def"))); 182 onView(withId(R.id.textview)).check(hasSelection("")); 183 assertNoSelectionHandles(); 184 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex("abc ghi.def".length())); 185 } 186 187 @SmallTest 188 public void testSelectTextByLongClick() throws Exception { 189 final String helloWorld = "Hello world!"; 190 onView(withId(R.id.textview)).perform(click()); 191 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); 192 193 onView(withId(R.id.textview)).perform(mouseLongClickOnTextAtIndex(0)); 194 onView(withId(R.id.textview)).check(hasSelection("Hello")); 195 196 onView(withId(R.id.textview)).perform(mouseLongClickOnTextAtIndex( 197 helloWorld.indexOf("world"))); 198 onView(withId(R.id.textview)).check(hasSelection("world")); 199 200 onView(withId(R.id.textview)).perform(mouseLongClickOnTextAtIndex( 201 helloWorld.indexOf("llo"))); 202 onView(withId(R.id.textview)).check(hasSelection("Hello")); 203 204 onView(withId(R.id.textview)).perform(mouseLongClickOnTextAtIndex( 205 helloWorld.indexOf("rld"))); 206 onView(withId(R.id.textview)).check(hasSelection("world")); 207 208 onView(withId(R.id.textview)).perform(mouseLongClickOnTextAtIndex(helloWorld.length())); 209 onView(withId(R.id.textview)).check(hasSelection("!")); 210 } 211 212 @SmallTest 213 public void testSelectTextByDoubleClick() throws Exception { 214 final String helloWorld = "Hello world!"; 215 onView(withId(R.id.textview)).perform(click()); 216 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); 217 218 onView(withId(R.id.textview)).perform(mouseDoubleClickOnTextAtIndex(0)); 219 onView(withId(R.id.textview)).check(hasSelection("Hello")); 220 221 onView(withId(R.id.textview)).perform(mouseDoubleClickOnTextAtIndex( 222 helloWorld.indexOf("world"))); 223 onView(withId(R.id.textview)).check(hasSelection("world")); 224 225 onView(withId(R.id.textview)).perform(mouseDoubleClickOnTextAtIndex( 226 helloWorld.indexOf("llo"))); 227 onView(withId(R.id.textview)).check(hasSelection("Hello")); 228 229 onView(withId(R.id.textview)).perform(mouseDoubleClickOnTextAtIndex( 230 helloWorld.indexOf("rld"))); 231 onView(withId(R.id.textview)).check(hasSelection("world")); 232 233 onView(withId(R.id.textview)).perform(mouseDoubleClickOnTextAtIndex(helloWorld.length())); 234 onView(withId(R.id.textview)).check(hasSelection("!")); 235 } 236 237 @SmallTest 238 public void testSelectTextByDoubleClickAndDrag() throws Exception { 239 final String text = "abcd efg hijk lmn"; 240 onView(withId(R.id.textview)).perform(click()); 241 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(text)); 242 243 onView(withId(R.id.textview)).perform( 244 mouseDoubleClickAndDragOnText(text.indexOf("f"), text.indexOf("j"))); 245 onView(withId(R.id.textview)).check(hasSelection("efg hijk")); 246 } 247 248 @SmallTest 249 public void testSelectTextByDoubleClickAndDrag_reverse() throws Exception { 250 final String text = "abcd efg hijk lmn"; 251 onView(withId(R.id.textview)).perform(click()); 252 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(text)); 253 254 onView(withId(R.id.textview)).perform( 255 mouseDoubleClickAndDragOnText(text.indexOf("j"), text.indexOf("f"))); 256 onView(withId(R.id.textview)).check(hasSelection("efg hijk")); 257 } 258 259 @SmallTest 260 public void testSelectTextByLongPressAndDrag() throws Exception { 261 final String text = "abcd efg hijk lmn"; 262 onView(withId(R.id.textview)).perform(click()); 263 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(text)); 264 265 onView(withId(R.id.textview)).perform( 266 mouseLongClickAndDragOnText(text.indexOf("f"), text.indexOf("j"))); 267 onView(withId(R.id.textview)).check(hasSelection("efg hijk")); 268 } 269 270 @SmallTest 271 public void testSelectTextByLongPressAndDrag_reverse() throws Exception { 272 final String text = "abcd efg hijk lmn"; 273 onView(withId(R.id.textview)).perform(click()); 274 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(text)); 275 276 onView(withId(R.id.textview)).perform( 277 mouseLongClickAndDragOnText(text.indexOf("j"), text.indexOf("f"))); 278 onView(withId(R.id.textview)).check(hasSelection("efg hijk")); 279 } 280 281 @SmallTest 282 public void testSelectTextByTripleClick() throws Exception { 283 final StringBuilder builder = new StringBuilder(); 284 builder.append("First paragraph.\n"); 285 builder.append("Second paragraph."); 286 for (int i = 0; i < 10; i++) { 287 builder.append(" This paragraph is very long."); 288 } 289 builder.append('\n'); 290 builder.append("Third paragraph."); 291 final String text = builder.toString(); 292 293 onView(withId(R.id.textview)).perform(click()); 294 onView(withId(R.id.textview)).perform(replaceText(text)); 295 296 onView(withId(R.id.textview)).perform( 297 mouseTripleClickOnTextAtIndex(text.indexOf("rst"))); 298 onView(withId(R.id.textview)).check(hasSelection("First paragraph.\n")); 299 300 onView(withId(R.id.textview)).perform( 301 mouseTripleClickOnTextAtIndex(text.indexOf("cond"))); 302 onView(withId(R.id.textview)).check(hasSelection( 303 text.substring(text.indexOf("Second"), text.indexOf("Third")))); 304 305 onView(withId(R.id.textview)).perform( 306 mouseTripleClickOnTextAtIndex(text.indexOf("ird"))); 307 onView(withId(R.id.textview)).check(hasSelection("Third paragraph.")); 308 309 onView(withId(R.id.textview)).perform( 310 mouseTripleClickOnTextAtIndex(text.indexOf("very long"))); 311 onView(withId(R.id.textview)).check(hasSelection( 312 text.substring(text.indexOf("Second"), text.indexOf("Third")))); 313 } 314 315 @SmallTest 316 public void testSelectTextByTripleClickAndDrag() throws Exception { 317 final StringBuilder builder = new StringBuilder(); 318 builder.append("First paragraph.\n"); 319 builder.append("Second paragraph."); 320 for (int i = 0; i < 10; i++) { 321 builder.append(" This paragraph is very long."); 322 } 323 builder.append('\n'); 324 builder.append("Third paragraph."); 325 final String text = builder.toString(); 326 327 onView(withId(R.id.textview)).perform(click()); 328 onView(withId(R.id.textview)).perform(replaceText(text)); 329 330 onView(withId(R.id.textview)).perform( 331 mouseTripleClickAndDragOnText(text.indexOf("irst"), text.indexOf("st"))); 332 onView(withId(R.id.textview)).check(hasSelection("First paragraph.\n")); 333 334 onView(withId(R.id.textview)).perform( 335 mouseTripleClickAndDragOnText(text.indexOf("cond"), text.indexOf("Third") - 2)); 336 onView(withId(R.id.textview)).check(hasSelection( 337 text.substring(text.indexOf("Second"), text.indexOf("Third")))); 338 339 onView(withId(R.id.textview)).perform( 340 mouseTripleClickAndDragOnText(text.indexOf("First"), text.indexOf("ird"))); 341 onView(withId(R.id.textview)).check(hasSelection(text)); 342 } 343 344 @SmallTest 345 public void testSelectTextByTripleClickAndDrag_reverse() throws Exception { 346 final StringBuilder builder = new StringBuilder(); 347 builder.append("First paragraph.\n"); 348 builder.append("Second paragraph."); 349 for (int i = 0; i < 10; i++) { 350 builder.append(" This paragraph is very long."); 351 } 352 builder.append('\n'); 353 builder.append("Third paragraph."); 354 final String text = builder.toString(); 355 356 onView(withId(R.id.textview)).perform(click()); 357 onView(withId(R.id.textview)).perform(replaceText(text)); 358 359 onView(withId(R.id.textview)).perform( 360 mouseTripleClickAndDragOnText(text.indexOf("st"), text.indexOf("irst"))); 361 onView(withId(R.id.textview)).check(hasSelection("First paragraph.\n")); 362 363 onView(withId(R.id.textview)).perform( 364 mouseTripleClickAndDragOnText(text.indexOf("Third") - 2, text.indexOf("cond"))); 365 onView(withId(R.id.textview)).check(hasSelection( 366 text.substring(text.indexOf("Second"), text.indexOf("Third")))); 367 368 onView(withId(R.id.textview)).perform( 369 mouseTripleClickAndDragOnText(text.indexOf("ird"), text.indexOf("First"))); 370 onView(withId(R.id.textview)).check(hasSelection(text)); 371 } 372 } 373