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