summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreehugger Robot <treehugger-gerrit@google.com>2017-03-21 23:18:17 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2017-03-21 23:18:18 +0000
commitcc41857964baf0d8e0ed39063892a7e77113ff41 (patch)
tree7173eb740ddbc01bc36cf66a63e91da32011d720
parent8c42ebbf384e1aa40c47ff52df2853f0117e9a8c (diff)
parentb93aa958dfacac1369da31d1b24cb4cd968d4a11 (diff)
downloadtesting-cc41857964baf0d8e0ed39063892a7e77113ff41.tar.gz
Merge "hasTextColor() view matcher to match a view based on it's text color" into android-support-test
-rw-r--r--espresso/core-tests/src/androidTest/java/android/support/test/espresso/matcher/ViewMatchersTest.java99
-rw-r--r--espresso/core-tests/src/androidTest/res/values/colors.xml21
-rw-r--r--espresso/core/src/main/java/android/support/test/espresso/matcher/ViewMatchers.java34
3 files changed, 113 insertions, 41 deletions
diff --git a/espresso/core-tests/src/androidTest/java/android/support/test/espresso/matcher/ViewMatchersTest.java b/espresso/core-tests/src/androidTest/java/android/support/test/espresso/matcher/ViewMatchersTest.java
index 6014cad..4781a9b 100644
--- a/espresso/core-tests/src/androidTest/java/android/support/test/espresso/matcher/ViewMatchersTest.java
+++ b/espresso/core-tests/src/androidTest/java/android/support/test/espresso/matcher/ViewMatchersTest.java
@@ -16,46 +16,6 @@
package android.support.test.espresso.matcher;
-import android.content.Context;
-import android.graphics.Color;
-import android.support.test.annotation.UiThreadTest;
-import android.support.test.espresso.matcher.ViewMatchers.Visibility;
-import android.support.test.rule.UiThreadTestRule;
-import android.support.test.runner.AndroidJUnit4;
-import android.support.test.testapp.test.R;
-import android.test.suitebuilder.annotation.MediumTest;
-import android.text.InputType;
-import android.text.Spannable;
-import android.text.style.ForegroundColorSpan;
-import android.text.util.Linkify;
-import android.view.View;
-import android.view.ViewGroup;
-import android.view.inputmethod.EditorInfo;
-import android.widget.ArrayAdapter;
-import android.widget.Button;
-import android.widget.CheckBox;
-import android.widget.Checkable;
-import android.widget.CheckedTextView;
-import android.widget.EditText;
-import android.widget.LinearLayout;
-import android.widget.RadioButton;
-import android.widget.RelativeLayout;
-import android.widget.ScrollView;
-import android.widget.Spinner;
-import android.widget.TextView;
-
-import com.google.common.collect.Lists;
-
-import org.hamcrest.Matcher;
-import org.hamcrest.Matchers;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.junit.runner.RunWith;
-
-import java.util.List;
-
import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.espresso.matcher.ViewMatchers.assertThat;
import static android.support.test.espresso.matcher.ViewMatchers.hasBackground;
@@ -65,6 +25,7 @@ import static android.support.test.espresso.matcher.ViewMatchers.hasErrorText;
import static android.support.test.espresso.matcher.ViewMatchers.hasImeAction;
import static android.support.test.espresso.matcher.ViewMatchers.hasLinks;
import static android.support.test.espresso.matcher.ViewMatchers.hasSibling;
+import static android.support.test.espresso.matcher.ViewMatchers.hasTextColor;
import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
import static android.support.test.espresso.matcher.ViewMatchers.isChecked;
import static android.support.test.espresso.matcher.ViewMatchers.isClickable;
@@ -94,6 +55,44 @@ import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.rules.ExpectedException.none;
+import android.content.Context;
+import android.graphics.Color;
+import android.os.Build;
+import android.support.test.annotation.UiThreadTest;
+import android.support.test.espresso.matcher.ViewMatchers.Visibility;
+import android.support.test.rule.UiThreadTestRule;
+import android.support.test.runner.AndroidJUnit4;
+import android.support.test.testapp.test.R;
+import android.test.suitebuilder.annotation.MediumTest;
+import android.text.InputType;
+import android.text.Spannable;
+import android.text.style.ForegroundColorSpan;
+import android.text.util.Linkify;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.inputmethod.EditorInfo;
+import android.widget.ArrayAdapter;
+import android.widget.Button;
+import android.widget.CheckBox;
+import android.widget.Checkable;
+import android.widget.CheckedTextView;
+import android.widget.EditText;
+import android.widget.LinearLayout;
+import android.widget.RadioButton;
+import android.widget.RelativeLayout;
+import android.widget.ScrollView;
+import android.widget.Spinner;
+import android.widget.TextView;
+import com.google.common.collect.Lists;
+import java.util.List;
+import org.hamcrest.Matcher;
+import org.hamcrest.Matchers;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+
/**
* Unit tests for {@link ViewMatchers}.
*/
@@ -356,6 +355,24 @@ public class ViewMatchersTest {
}
@Test
+ public void hasTextColorTest() {
+ TextView textView = new TextView(context);
+ textView.setText("text");
+
+ int color;
+ if (Build.VERSION.SDK_INT <= 22) {
+ color = context.getResources().getColor(R.color.green);
+ } else {
+ color = context.getColor(R.color.green);
+ }
+
+ textView.setTextColor(color);
+
+ assertTrue(hasTextColor(R.color.green).matches(textView));
+ assertFalse(hasTextColor(R.color.red).matches(textView));
+ }
+
+ @Test
public void hasDescendantTest() {
View v = new TextView(context);
ViewGroup parent = new RelativeLayout(context);
@@ -729,4 +746,4 @@ public class ViewMatchersTest {
assertFalse(withInputType(UNRECOGNIZED_INPUT_TYPE).matches(editText));
}
-}
+} \ No newline at end of file
diff --git a/espresso/core-tests/src/androidTest/res/values/colors.xml b/espresso/core-tests/src/androidTest/res/values/colors.xml
new file mode 100644
index 0000000..5bf5ed7
--- /dev/null
+++ b/espresso/core-tests/src/androidTest/res/values/colors.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ ~ Copyright (C) 2016 The Android Open Source Project
+ ~
+ ~ Licensed under the Apache License, Version 2.0 (the "License");
+ ~ you may not use this file except in compliance with the License.
+ ~ You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+
+<resources>
+ <color name="red">#FFB71800</color>
+ <color name="green">#377e00</color>
+</resources>
diff --git a/espresso/core/src/main/java/android/support/test/espresso/matcher/ViewMatchers.java b/espresso/core/src/main/java/android/support/test/espresso/matcher/ViewMatchers.java
index e2559ba..efb9c0f 100644
--- a/espresso/core/src/main/java/android/support/test/espresso/matcher/ViewMatchers.java
+++ b/espresso/core/src/main/java/android/support/test/espresso/matcher/ViewMatchers.java
@@ -21,6 +21,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static org.hamcrest.Matchers.is;
+import android.os.Build;
import android.support.test.annotation.Beta;
import android.support.test.espresso.util.HumanReadables;
import com.google.common.base.Predicate;
@@ -1171,4 +1172,37 @@ public final class ViewMatchers {
public static Matcher<View> hasBackground(final int drawableId) {
return new HasBackgroundMatcher(drawableId);
}
+
+ /**
+ * Returns a matcher that matches {@link android.widget.TextView} based on it's color.
+ */
+ public static Matcher<View> hasTextColor(final int colorResId) {
+ return new BoundedMatcher<View, TextView>(TextView.class) {
+ private Context context;
+
+ @Override
+ protected boolean matchesSafely(TextView textView) {
+ context = textView.getContext();
+ int textViewColor = textView.getCurrentTextColor();
+ int expectedColor;
+
+ if (Build.VERSION.SDK_INT <= 22) {
+ expectedColor = context.getResources().getColor(colorResId);
+ } else {
+ expectedColor = context.getColor(colorResId);
+ }
+
+ return textViewColor == expectedColor;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ String colorId = String.valueOf(colorResId);
+ if (context != null) {
+ colorId = context.getResources().getResourceName(colorResId);
+ }
+ description.appendText("has color with ID " + colorId);
+ }
+ };
+ }
}