TextViewActivityTest.java revision c24c2bbb69634435d1d9fc97671229df245bd8d0
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 17package android.widget; 18 19import static android.widget.espresso.TextViewActions.clickOnTextAtIndex; 20import static android.widget.espresso.TextViewActions.doubleTapAndDragOnText; 21import static android.widget.espresso.TextViewActions.doubleClickOnTextAtIndex; 22import static android.widget.espresso.TextViewActions.dragHandle; 23import static android.widget.espresso.TextViewActions.Handle; 24import static android.widget.espresso.TextViewActions.longPressAndDragOnText; 25import static android.widget.espresso.TextViewActions.longPressOnTextAtIndex; 26import static android.widget.espresso.TextViewAssertions.hasInsertionPointerAtIndex; 27import static android.widget.espresso.TextViewAssertions.hasSelection; 28import static android.widget.espresso.FloatingToolbarEspressoUtils.assertFloatingToolbarIsDisplayed; 29import static android.support.test.espresso.Espresso.onView; 30import static android.support.test.espresso.action.ViewActions.click; 31import static android.support.test.espresso.action.ViewActions.pressKey; 32import static android.support.test.espresso.action.ViewActions.typeTextIntoFocusedView; 33import static android.support.test.espresso.assertion.ViewAssertions.matches; 34import static android.support.test.espresso.matcher.RootMatchers.withDecorView; 35import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant; 36import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; 37import static android.support.test.espresso.matcher.ViewMatchers.withId; 38import static android.support.test.espresso.matcher.ViewMatchers.withText; 39import static org.hamcrest.Matchers.allOf; 40 41import com.android.frameworks.coretests.R; 42 43import android.support.test.espresso.ViewInteraction; 44import android.test.ActivityInstrumentationTestCase2; 45import android.test.suitebuilder.annotation.SmallTest; 46import android.view.KeyEvent; 47 48/** 49 * Tests the TextView widget from an Activity 50 */ 51public class TextViewActivityTest extends ActivityInstrumentationTestCase2<TextViewActivity>{ 52 53 public TextViewActivityTest() { 54 super(TextViewActivity.class); 55 } 56 57 @Override 58 public void setUp() { 59 getActivity(); 60 } 61 62 @SmallTest 63 public void testTypedTextIsOnScreen() throws Exception { 64 final String helloWorld = "Hello world!"; 65 onView(withId(R.id.textview)).perform(click()); 66 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); 67 68 onView(withId(R.id.textview)).check(matches(withText(helloWorld))); 69 } 70 71 @SmallTest 72 public void testPositionCursorAtTextAtIndex() throws Exception { 73 final String helloWorld = "Hello world!"; 74 onView(withId(R.id.textview)).perform(click()); 75 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); 76 onView(withId(R.id.textview)).perform(clickOnTextAtIndex(helloWorld.indexOf("world"))); 77 78 // Delete text at specified index and see if we got the right one. 79 onView(withId(R.id.textview)).perform(pressKey(KeyEvent.KEYCODE_FORWARD_DEL)); 80 onView(withId(R.id.textview)).check(matches(withText("Hello orld!"))); 81 } 82 83 @SmallTest 84 public void testLongPressToSelect() throws Exception { 85 final String helloWorld = "Hello Kirk!"; 86 onView(withId(R.id.textview)).perform(click()); 87 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); 88 onView(withId(R.id.textview)).perform( 89 longPressOnTextAtIndex(helloWorld.indexOf("Kirk"))); 90 91 onView(withId(R.id.textview)).check(hasSelection("Kirk")); 92 } 93 94 @SmallTest 95 public void testLongPressEmptySpace() throws Exception { 96 final String helloWorld = "Hello big round sun!"; 97 onView(withId(R.id.textview)).perform(click()); 98 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); 99 // Move cursor somewhere else 100 onView(withId(R.id.textview)).perform(clickOnTextAtIndex(helloWorld.indexOf("big"))); 101 // Long-press at end of line. 102 onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(helloWorld.length())); 103 104 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(helloWorld.length())); 105 } 106 107 @SmallTest 108 public void testLongPressAndDragToSelect() throws Exception { 109 final String helloWorld = "Hello little handsome boy!"; 110 onView(withId(R.id.textview)).perform(click()); 111 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); 112 onView(withId(R.id.textview)).perform( 113 longPressAndDragOnText(helloWorld.indexOf("little"), helloWorld.indexOf(" boy!"))); 114 115 onView(withId(R.id.textview)).check(hasSelection("little handsome")); 116 } 117 118 @SmallTest 119 public void testDoubleTapToSelect() throws Exception { 120 final String helloWorld = "Hello SuetYi!"; 121 onView(withId(R.id.textview)).perform(click()); 122 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); 123 onView(withId(R.id.textview)).perform( 124 doubleClickOnTextAtIndex(helloWorld.indexOf("SuetYi"))); 125 126 onView(withId(R.id.textview)).check(hasSelection("SuetYi")); 127 } 128 129 @SmallTest 130 public void testDoubleTapAndDragToSelect() throws Exception { 131 final String helloWorld = "Hello young beautiful girl!"; 132 onView(withId(R.id.textview)).perform(click()); 133 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); 134 onView(withId(R.id.textview)).perform( 135 doubleTapAndDragOnText(helloWorld.indexOf("young"), helloWorld.indexOf(" girl!"))); 136 137 onView(withId(R.id.textview)).check(hasSelection("young beautiful")); 138 } 139 140 @SmallTest 141 public void testSelectBackwordsByTouch() throws Exception { 142 final String helloWorld = "Hello king of the Jungle!"; 143 onView(withId(R.id.textview)).perform(click()); 144 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); 145 onView(withId(R.id.textview)).perform( 146 doubleTapAndDragOnText(helloWorld.indexOf(" Jungle!"), helloWorld.indexOf("king"))); 147 148 onView(withId(R.id.textview)).check(hasSelection("king of the")); 149 } 150 151 @SmallTest 152 public void testToolbarAppearsAfterSelection() throws Exception { 153 // It'll be nice to check that the toolbar is not visible (or does not exist) here 154 // I can't currently find a way to do this. I'll get to it later. 155 156 final String text = "Toolbar appears after selection."; 157 onView(withId(R.id.textview)).perform(click()); 158 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(text)); 159 onView(withId(R.id.textview)).perform( 160 longPressOnTextAtIndex(text.indexOf("appears"))); 161 162 // It takes the toolbar less than 100ms to start to animate into screen. 163 // Ideally, we'll wait using the UiController, but I guess this works for now. 164 Thread.sleep(100); 165 assertFloatingToolbarIsDisplayed(getActivity()); 166 } 167 168 private static ViewInteraction onHandleView(int id) { 169 return onView(allOf(withId(id), isAssignableFrom(Editor.HandleView.class))) 170 .inRoot(withDecorView(hasDescendant(withId(id)))); 171 } 172 173 @SmallTest 174 public void testSelectionHandles() throws Exception { 175 final String text = "abcd efg hijk lmn"; 176 onView(withId(R.id.textview)).perform(click()); 177 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(text)); 178 onView(withId(R.id.textview)).perform(doubleClickOnTextAtIndex(text.indexOf('f'))); 179 180 final TextView textView = (TextView)getActivity().findViewById(R.id.textview); 181 onHandleView(com.android.internal.R.id.selection_start_handle) 182 .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('a'))); 183 onView(withId(R.id.textview)).check(hasSelection("abcd efg")); 184 185 onHandleView(com.android.internal.R.id.selection_end_handle) 186 .perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('k') + 1)); 187 onView(withId(R.id.textview)).check(hasSelection("abcd efg hijk")); 188 } 189 190 @SmallTest 191 public void testSelectionHandles_multiLine() throws Exception { 192 final String text = "abcd\n" + "efg\n" + "hijk\n" + "lmn\n" + "opqr"; 193 onView(withId(R.id.textview)).perform(click()); 194 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(text)); 195 onView(withId(R.id.textview)).perform(doubleClickOnTextAtIndex(text.indexOf('i'))); 196 197 final TextView textView = (TextView)getActivity().findViewById(R.id.textview); 198 onHandleView(com.android.internal.R.id.selection_start_handle) 199 .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('e'))); 200 onView(withId(R.id.textview)).check(hasSelection("efg\nhijk")); 201 202 onHandleView(com.android.internal.R.id.selection_start_handle) 203 .perform(dragHandle(textView, Handle.SELECTION_START, text.indexOf('a'))); 204 onView(withId(R.id.textview)).check(hasSelection("abcd\nefg\nhijk")); 205 206 onHandleView(com.android.internal.R.id.selection_end_handle) 207 .perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('n') + 1)); 208 onView(withId(R.id.textview)).check(hasSelection("abcd\nefg\nhijk\nlmn")); 209 210 onHandleView(com.android.internal.R.id.selection_end_handle) 211 .perform(dragHandle(textView, Handle.SELECTION_END, text.indexOf('r') + 1)); 212 onView(withId(R.id.textview)).check(hasSelection("abcd\nefg\nhijk\nlmn\nopqr")); 213 } 214} 215