Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
forrestguice committed Jan 24, 2025
1 parent 6980ff9 commit 3b742fc
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/support-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
implementation 'com.android.support:support-annotations:28.0.0'
implementation 'com.android.support.test:rules:0.5'
implementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
Expand Down
3 changes: 3 additions & 0 deletions app/support-test/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.forrestguice.suntimes.support_test">

<uses-sdk tools:overrideLibrary="android.support.test.uiautomator.v18" />

</manifest>
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package com.forrestguice.support.test.espresso;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Looper;
import android.support.test.espresso.DataInteraction;
import android.support.test.espresso.IdlingResource;
import android.support.test.espresso.ViewInteraction;
import android.support.test.uiautomator.UiDevice;
import android.view.View;

import org.hamcrest.Matcher;

import java.util.List;

import static android.support.test.InstrumentationRegistry.getInstrumentation;

public class Espresso
{
public static DataInteraction onData(Matcher<? extends Object> dataMatcher) {
Expand Down Expand Up @@ -56,4 +61,12 @@ public static void pressBack() {
public static void openActionBarOverflowOrOptionsMenu(Context context) {
android.support.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu(context);
}

@TargetApi(18)
public static void click(int x, int y)
{
if (Build.VERSION.SDK_INT >= 18) {
UiDevice.getInstance(getInstrumentation()).click(x, y);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,42 @@
import android.support.v7.widget.AppCompatImageButton;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.is;

public class ViewMatchers
{
public static Matcher<View> withEffectiveVisibility(final android.support.test.espresso.matcher.ViewMatchers.Visibility visibility) {
return android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility(visibility);
}

public static Matcher<View> withAlpha(final float alpha) {
return android.support.test.espresso.matcher.ViewMatchers.withAlpha(alpha);
}

public static Matcher<View> withParent(final Matcher<View> parentMatcher) {
return android.support.test.espresso.matcher.ViewMatchers.withParent(parentMatcher);
}

public static Matcher<View> withChild(final Matcher<View> childMatcher) {
return android.support.test.espresso.matcher.ViewMatchers.withChild(childMatcher);
}

public static Matcher<View> hasChildCount(final int childCount) {
return android.support.test.espresso.matcher.ViewMatchers.hasChildCount(childCount);
}

public static Matcher<View> hasMinimumChildCount(final int minChildCount) {
return android.support.test.espresso.matcher.ViewMatchers.hasMinimumChildCount(minChildCount);
}

public static Matcher<View> hasLinks() {
return android.support.test.espresso.matcher.ViewMatchers.hasLinks();
}
Expand All @@ -27,6 +56,10 @@ public static Matcher<View> hasSibling(final Matcher<View> siblingMatcher) {
return android.support.test.espresso.matcher.ViewMatchers.hasSibling(siblingMatcher);
}

public static Matcher<View> hasDescendant(final Matcher<View> descendantMatcher) {
return android.support.test.espresso.matcher.ViewMatchers.hasDescendant(descendantMatcher);
}

public static Matcher<View> isEnabled() {
return android.support.test.espresso.matcher.ViewMatchers.isEnabled();
}
Expand All @@ -35,6 +68,16 @@ public static Matcher<View> isClickable() {
return android.support.test.espresso.matcher.ViewMatchers.isClickable();
}

public static Matcher<View> withHint(String hintText) {
return android.support.test.espresso.matcher.ViewMatchers.withHint(hintText);
}
public static Matcher<View> withHint(final Matcher<String> stringMatcher) {
return android.support.test.espresso.matcher.ViewMatchers.withHint(stringMatcher);
}
public static Matcher<View> withHint(final int resourceId) {
return android.support.test.espresso.matcher.ViewMatchers.withHint(resourceId);
}

public static Matcher<View> isChecked() {
return android.support.test.espresso.matcher.ViewMatchers.isChecked();
}
Expand Down Expand Up @@ -66,14 +109,32 @@ public static Matcher<View> withClassName(final Matcher<String> classNameMatcher
return android.support.test.espresso.matcher.ViewMatchers.withClassName(classNameMatcher);
}

public static Matcher<View> withParent(final Matcher<View> parentMatcher) {
return android.support.test.espresso.matcher.ViewMatchers.withParent(parentMatcher);
}

public static Matcher<View> withId(final int id) {
return android.support.test.espresso.matcher.ViewMatchers.withId(id);
}

public static Matcher<View> withId(final Matcher<Integer> integerMatcher) {
return android.support.test.espresso.matcher.ViewMatchers.withId(integerMatcher);
}

public static Matcher<View> withResourceName(String name) {
return withResourceName(is(name));
}

public static Matcher<View> withResourceName(final Matcher<String> stringMatcher) {
return android.support.test.espresso.matcher.ViewMatchers.withResourceName(stringMatcher);
}

public static Matcher<View> withTagKey(final int key) {
return android.support.test.espresso.matcher.ViewMatchers.withTagKey(key);
}
public static Matcher<View> withTagKey(final int key, final Matcher<Object> objectMatcher) {
return android.support.test.espresso.matcher.ViewMatchers.withTagKey(key, objectMatcher);
}
public static Matcher<View> withTagValue(final Matcher<Object> tagValueMatcher) {
return android.support.test.espresso.matcher.ViewMatchers.withTagValue(tagValueMatcher);
}

public static Matcher<View> withText(String text) {
return android.support.test.espresso.matcher.ViewMatchers.withText(text);
}
Expand All @@ -84,6 +145,10 @@ public static Matcher<View> withText(final int resourceId) {
return android.support.test.espresso.matcher.ViewMatchers.withText(resourceId);
}

public static Matcher<View> withSubstring(String substring) {
return android.support.test.espresso.matcher.ViewMatchers.withSubstring(substring);
}

public static Matcher<View> withSpinnerText(final int resourceId) {
return android.support.test.espresso.matcher.ViewMatchers.withSpinnerText(resourceId);
}
Expand All @@ -94,6 +159,21 @@ public static Matcher<View> withSpinnerText(final Matcher<String> stringMatcher)
return android.support.test.espresso.matcher.ViewMatchers.withSpinnerText(stringMatcher);
}

public static Matcher<View> hasErrorText(final Matcher<String> stringMatcher) {
return android.support.test.espresso.matcher.ViewMatchers.hasErrorText(stringMatcher);
}
public static Matcher<View> hasErrorText(final String expectedError) {
return android.support.test.espresso.matcher.ViewMatchers.hasErrorText(expectedError);
}

public static Matcher<View> withInputType(final int inputType) {
return android.support.test.espresso.matcher.ViewMatchers.withInputType(inputType);
}

public static Matcher<View> withParentIndex(final int index) {
return android.support.test.espresso.matcher.ViewMatchers.withParentIndex(index);
}

public static Matcher<View> withContentDescription(final int resourceId) {
return android.support.test.espresso.matcher.ViewMatchers.withContentDescription(resourceId);
}
Expand All @@ -106,7 +186,7 @@ public static Matcher<View> withContentDescription(
}

public static Matcher<View> isAssignableFrom(final Class<? extends View> clazz) {
return android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom(clazz);
return android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom(clazz);
}

public static Matcher<View> tabLayout() {
Expand Down Expand Up @@ -138,4 +218,89 @@ public boolean matchesSafely(TabItem item) {
};
}*/

/**
* based on https://stackoverflow.com/a/73584446
*/
public static Matcher<View> isMenuDropDownListView() {
return withClassName(endsWith("MenuDropDownListView"));
}
public static Matcher<View> withPositionInMenuDropDownListView(int position) {
return allOf(withParent(isMenuDropDownListView()), withParentIndex(position));
}
public static Matcher<View> isMenuItemWithTextInMenuDropDownListView(String text) {
return allOf(withParent(isMenuDropDownListView()), withChild(withChild(withChild(withText(text)))));
}
public static Matcher<View> isMenuItemWithTextInMenuDropDownListView(int textResID) {
return allOf(withParent(isMenuDropDownListView()), withChild(withChild(withChild(withText(textResID)))));
}

public static Matcher<View> isCheckBox() {
return withClassName(endsWith("CheckBox"));
}
public static Matcher<View> isRadioButton() {
return withClassName(endsWith("RadioButton"));
}

public static Matcher<View> isCheckBoxWithTextInDropDownMenu(int textResID) {
return allOf(
isCheckBox(),
withParent(
withParent(
isMenuItemWithTextInMenuDropDownListView(textResID)
)
)
);
}
public static Matcher<View> isRadioButtonWithTextInDropDownMenu(int textResID) {
return allOf(
isRadioButton(),
withParent(
withParent(
isMenuItemWithTextInMenuDropDownListView(textResID)
)
)
);
}

/**
* based on https://stackoverflow.com/a/34607110
*/
public static Matcher<View> withTextAsDoubleApproximateTo(final double value, final double tolerance, final String[] ignoreSymbols)
{
return new TypeSafeMatcher<View>()
{
@Override
protected boolean matchesSafely(View view)
{
if (view instanceof TextView)
{
TextView textView = (TextView) view;
String text = textView.getText().toString();
for (String symbol : ignoreSymbols) {
text = text.replaceAll(symbol, "");
}
double v = Double.parseDouble(text);
return Math.abs(v - value) <= tolerance;

} else {
return false;
}
}

@Override
public void describeTo(Description description) {
description.appendText("value is not approximate to " + value + " (differs by more than " + tolerance + ")");
}
};
}
public static Matcher<View> withTextAsDoubleApproximateTo(final double value, final double tolerance)
{
return withTextAsDoubleApproximateTo(value, tolerance, new String[] {
"°", "∠", "δ", "$", "%", "#", "!", "\\(", "\\)", "\\*",
"N", "NNE", "NE", "ENE",
"E", "ESE", "SE", "SSE",
"S", "SSW", "SW", "WSW",
"W", "WNW", "NW", "NNW" });
}

}

0 comments on commit 3b742fc

Please sign in to comment.