OSDN Git Service

37c7425ce478d6d38e81313393aa40ce7afbb3b1
[android-x86/frameworks-base.git] / core / tests / coretests / src / android / widget / espresso / TextViewAssertions.java
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.espresso;
18
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;
22
23 import android.support.test.espresso.NoMatchingViewException;
24 import android.support.test.espresso.ViewAssertion;
25 import android.view.View;
26 import android.widget.TextView;
27
28 import junit.framework.AssertionFailedError;
29 import org.hamcrest.Matcher;
30
31 /**
32  * A collection of assertions on a {@link android.widget.TextView}.
33  */
34 public final class TextViewAssertions {
35
36     private TextViewAssertions() {}
37
38     /**
39      * Returns a {@link ViewAssertion} that asserts that the text view has a specified
40      * selection.<br>
41      * <br>
42      * View constraints:
43      * <ul>
44      * <li>must be a text view displayed on screen
45      * <ul>
46      *
47      * @param selection  The expected selection.
48      */
49     public static ViewAssertion hasSelection(String selection) {
50         return hasSelection(is(selection));
51     }
52
53     /**
54      * Returns a {@link ViewAssertion} that asserts that the text view has a specified
55      * selection.<br>
56      * <br>
57      * View constraints:
58      * <ul>
59      * <li>must be a text view displayed on screen
60      * <ul>
61      *
62      * @param selection  A matcher representing the expected selection.
63      */
64     public static ViewAssertion hasSelection(Matcher<String> selection) {
65         return new TextSelectionAssertion(selection);
66     }
67
68     /**
69      * Returns a {@link ViewAssertion} that asserts that the text view insertion pointer is at
70      * a specified index.<br>
71      * <br>
72      * View constraints:
73      * <ul>
74      * <li>must be a text view displayed on screen
75      * <ul>
76      *
77      * @param index  The expected index.
78      */
79     public static ViewAssertion hasInsertionPointerAtIndex(int index) {
80         return hasInsertionPointerAtIndex(is(index));
81     }
82
83     /**
84      * Returns a {@link ViewAssertion} that asserts that the text view insertion pointer is at
85      * a specified index.<br>
86      * <br>
87      * View constraints:
88      * <ul>
89      * <li>must be a text view displayed on screen
90      * <ul>
91      *
92      * @param index  A matcher representing the expected index.
93      */
94     public static ViewAssertion hasInsertionPointerAtIndex(final Matcher<Integer> index) {
95         return new ViewAssertion() {
96             @Override
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();
102                     try {
103                         assertThat(selectionStart, index);
104                         assertThat(selectionEnd, index);
105                     } catch (IndexOutOfBoundsException e) {
106                         throw new AssertionFailedError(e.getMessage());
107                     }
108                 } else {
109                     throw new AssertionFailedError("TextView not found");
110                 }
111             }
112         };
113     }
114
115     /**
116      * A {@link ViewAssertion} to check the selected text in a {@link TextView}.
117      */
118     private static final class TextSelectionAssertion implements ViewAssertion {
119
120         private final Matcher<String> mSelection;
121
122         public TextSelectionAssertion(Matcher<String> selection) {
123             mSelection = checkNotNull(selection);
124         }
125
126         @Override
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();
132                 try {
133                     String selectedText = textView.getText()
134                             .subSequence(selectionStart, selectionEnd)
135                             .toString();
136                     assertThat(selectedText, mSelection);
137                 } catch (IndexOutOfBoundsException e) {
138                     throw new AssertionFailedError(e.getMessage());
139                 }
140             } else {
141                 throw new AssertionFailedError("TextView not found");
142             }
143         }
144     }
145 }