TextViewActivityTest.java revision e36c7112e7c48f3c8964f95a2a489d25504356b0
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.longPressAndDragOnText; 23import static android.widget.espresso.TextViewActions.longPressOnTextAtIndex; 24import static android.widget.espresso.TextViewAssertions.hasInsertionPointerAtIndex; 25import static android.widget.espresso.TextViewAssertions.hasSelection; 26import static android.widget.espresso.FloatingToolbarEspressoUtils.assertFloatingToolbarIsDisplayed; 27import static android.support.test.espresso.Espresso.onView; 28import static android.support.test.espresso.action.ViewActions.click; 29import static android.support.test.espresso.action.ViewActions.pressKey; 30import static android.support.test.espresso.action.ViewActions.typeTextIntoFocusedView; 31import static android.support.test.espresso.assertion.ViewAssertions.matches; 32import static android.support.test.espresso.matcher.ViewMatchers.withId; 33import static android.support.test.espresso.matcher.ViewMatchers.withText; 34 35import com.android.frameworks.coretests.R; 36 37import android.test.ActivityInstrumentationTestCase2; 38import android.test.suitebuilder.annotation.SmallTest; 39import android.view.KeyEvent; 40 41/** 42 * Tests the TextView widget from an Activity 43 */ 44public class TextViewActivityTest extends ActivityInstrumentationTestCase2<TextViewActivity>{ 45 46 public TextViewActivityTest() { 47 super(TextViewActivity.class); 48 } 49 50 @Override 51 public void setUp() { 52 getActivity(); 53 } 54 55 @SmallTest 56 public void testTypedTextIsOnScreen() throws Exception { 57 final String helloWorld = "Hello world!"; 58 onView(withId(R.id.textview)).perform(click()); 59 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); 60 61 onView(withId(R.id.textview)).check(matches(withText(helloWorld))); 62 } 63 64 @SmallTest 65 public void testPositionCursorAtTextAtIndex() throws Exception { 66 final String helloWorld = "Hello world!"; 67 onView(withId(R.id.textview)).perform(click()); 68 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); 69 onView(withId(R.id.textview)).perform(clickOnTextAtIndex(helloWorld.indexOf("world"))); 70 71 // Delete text at specified index and see if we got the right one. 72 onView(withId(R.id.textview)).perform(pressKey(KeyEvent.KEYCODE_FORWARD_DEL)); 73 onView(withId(R.id.textview)).check(matches(withText("Hello orld!"))); 74 } 75 76 @SmallTest 77 public void testLongPressToSelect() throws Exception { 78 final String helloWorld = "Hello Kirk!"; 79 onView(withId(R.id.textview)).perform(click()); 80 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); 81 onView(withId(R.id.textview)).perform( 82 longPressOnTextAtIndex(helloWorld.indexOf("Kirk"))); 83 84 onView(withId(R.id.textview)).check(hasSelection("Kirk")); 85 } 86 87 @SmallTest 88 public void testLongPressEmptySpace() throws Exception { 89 final String helloWorld = "Hello big round sun!"; 90 onView(withId(R.id.textview)).perform(click()); 91 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); 92 // Move cursor somewhere else 93 onView(withId(R.id.textview)).perform(clickOnTextAtIndex(helloWorld.indexOf("big"))); 94 // Long-press at end of line. 95 onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(helloWorld.length())); 96 97 onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(helloWorld.length())); 98 } 99 100 @SmallTest 101 public void testLongPressAndDragToSelect() throws Exception { 102 final String helloWorld = "Hello little handsome boy!"; 103 onView(withId(R.id.textview)).perform(click()); 104 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); 105 onView(withId(R.id.textview)).perform( 106 longPressAndDragOnText(helloWorld.indexOf("little"), helloWorld.indexOf(" boy!"))); 107 108 onView(withId(R.id.textview)).check(hasSelection("little handsome")); 109 } 110 111 @SmallTest 112 public void testDoubleTapToSelect() throws Exception { 113 final String helloWorld = "Hello SuetYi!"; 114 onView(withId(R.id.textview)).perform(click()); 115 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); 116 onView(withId(R.id.textview)).perform( 117 doubleClickOnTextAtIndex(helloWorld.indexOf("SuetYi"))); 118 119 onView(withId(R.id.textview)).check(hasSelection("SuetYi")); 120 } 121 122 @SmallTest 123 public void testDoubleTapAndDragToSelect() throws Exception { 124 final String helloWorld = "Hello young beautiful girl!"; 125 onView(withId(R.id.textview)).perform(click()); 126 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); 127 onView(withId(R.id.textview)).perform( 128 doubleTapAndDragOnText(helloWorld.indexOf("young"), helloWorld.indexOf(" girl!"))); 129 130 onView(withId(R.id.textview)).check(hasSelection("young beautiful")); 131 } 132 133 @SmallTest 134 public void testSelectBackwordsByTouch() throws Exception { 135 final String helloWorld = "Hello king of the Jungle!"; 136 onView(withId(R.id.textview)).perform(click()); 137 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); 138 onView(withId(R.id.textview)).perform( 139 doubleTapAndDragOnText(helloWorld.indexOf(" Jungle!"), helloWorld.indexOf("king"))); 140 141 onView(withId(R.id.textview)).check(hasSelection("king of the")); 142 } 143 144 @SmallTest 145 public void testToolbarAppearsAfterSelection() throws Exception { 146 // It'll be nice to check that the toolbar is not visible (or does not exist) here 147 // I can't currently find a way to do this. I'll get to it later. 148 149 final String text = "Toolbar appears after selection."; 150 onView(withId(R.id.textview)).perform(click()); 151 onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(text)); 152 onView(withId(R.id.textview)).perform( 153 longPressOnTextAtIndex(text.indexOf("appears"))); 154 155 // It takes the toolbar less than 100ms to start to animate into screen. 156 // Ideally, we'll wait using the UiController, but I guess this works for now. 157 Thread.sleep(100); 158 assertFloatingToolbarIsDisplayed(getActivity()); 159 } 160} 161