2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 package android.widget.espresso;
19 import static android.support.test.espresso.matcher.ViewMatchers.assertThat;
20 import static com.android.internal.util.Preconditions.checkNotNull;
21 import static org.hamcrest.Matchers.is;
23 import android.support.test.espresso.NoMatchingViewException;
24 import android.support.test.espresso.ViewAssertion;
25 import android.view.View;
26 import android.widget.TextView;
28 import junit.framework.AssertionFailedError;
29 import org.hamcrest.Matcher;
32 * A collection of assertions on a {@link android.widget.TextView}.
34 public final class TextViewAssertions {
36 private TextViewAssertions() {}
39 * Returns a {@link ViewAssertion} that asserts that the text view has a specified
44 * <li>must be a text view displayed on screen
47 * @param selection The expected selection.
49 public static ViewAssertion hasSelection(String selection) {
50 return hasSelection(is(selection));
54 * Returns a {@link ViewAssertion} that asserts that the text view has a specified
59 * <li>must be a text view displayed on screen
62 * @param selection A matcher representing the expected selection.
64 public static ViewAssertion hasSelection(Matcher<String> selection) {
65 return new TextSelectionAssertion(selection);
69 * Returns a {@link ViewAssertion} that asserts that the text view insertion pointer is at
70 * a specified index.<br>
74 * <li>must be a text view displayed on screen
77 * @param index The expected index.
79 public static ViewAssertion hasInsertionPointerAtIndex(int index) {
80 return hasInsertionPointerAtIndex(is(index));
84 * Returns a {@link ViewAssertion} that asserts that the text view insertion pointer is at
85 * a specified index.<br>
89 * <li>must be a text view displayed on screen
92 * @param index A matcher representing the expected index.
94 public static ViewAssertion hasInsertionPointerAtIndex(final Matcher<Integer> index) {
95 return new ViewAssertion() {
97 public void check(View view, NoMatchingViewException exception) {
98 if (view instanceof TextView) {
99 TextView textView = (TextView) view;
100 int selectionStart = textView.getSelectionStart();
101 int selectionEnd = textView.getSelectionEnd();
103 assertThat(selectionStart, index);
104 assertThat(selectionEnd, index);
105 } catch (IndexOutOfBoundsException e) {
106 throw new AssertionFailedError(e.getMessage());
109 throw new AssertionFailedError("TextView not found");
116 * A {@link ViewAssertion} to check the selected text in a {@link TextView}.
118 private static final class TextSelectionAssertion implements ViewAssertion {
120 private final Matcher<String> mSelection;
122 public TextSelectionAssertion(Matcher<String> selection) {
123 mSelection = checkNotNull(selection);
127 public void check(View view, NoMatchingViewException exception) {
128 if (view instanceof TextView) {
129 TextView textView = (TextView) view;
130 int selectionStart = textView.getSelectionStart();
131 int selectionEnd = textView.getSelectionEnd();
133 String selectedText = textView.getText()
134 .subSequence(selectionStart, selectionEnd)
136 assertThat(selectedText, mSelection);
137 } catch (IndexOutOfBoundsException e) {
138 throw new AssertionFailedError(e.getMessage());
141 throw new AssertionFailedError("TextView not found");