-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56bd9b0
commit 5c728d2
Showing
2 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
app/src/androidTest/java/com/sdsmdg/demoexample/MainActivityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package com.sdsmdg.demoexample; | ||
|
||
import android.support.test.espresso.ViewInteraction; | ||
import android.support.test.rule.ActivityTestRule; | ||
import android.support.test.runner.AndroidJUnit4; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.view.ViewParent; | ||
|
||
import org.hamcrest.Description; | ||
import org.hamcrest.Matcher; | ||
import org.hamcrest.TypeSafeMatcher; | ||
import org.hamcrest.core.IsInstanceOf; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static android.support.test.espresso.Espresso.onView; | ||
import static android.support.test.espresso.assertion.ViewAssertions.matches; | ||
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; | ||
import static android.support.test.espresso.matcher.ViewMatchers.withId; | ||
import static org.hamcrest.Matchers.allOf; | ||
|
||
@RunWith(AndroidJUnit4.class) | ||
public class MainActivityTest { | ||
|
||
@Rule | ||
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class); | ||
|
||
@Test | ||
public void mainActivityTest() { | ||
ViewInteraction button = onView( | ||
allOf(withId(R.id.button), | ||
childAtPosition( | ||
childAtPosition( | ||
IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class), | ||
0), | ||
0), | ||
isDisplayed())); | ||
button.check(matches(isDisplayed())); | ||
|
||
ViewInteraction button2 = onView( | ||
allOf(withId(R.id.button2), | ||
childAtPosition( | ||
childAtPosition( | ||
IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class), | ||
0), | ||
1), | ||
isDisplayed())); | ||
button2.check(matches(isDisplayed())); | ||
|
||
ViewInteraction button3 = onView( | ||
allOf(withId(R.id.button3), | ||
childAtPosition( | ||
childAtPosition( | ||
IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class), | ||
0), | ||
2), | ||
isDisplayed())); | ||
button3.check(matches(isDisplayed())); | ||
|
||
ViewInteraction button4 = onView( | ||
allOf(withId(R.id.button4), | ||
childAtPosition( | ||
childAtPosition( | ||
IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class), | ||
1), | ||
0), | ||
isDisplayed())); | ||
button4.check(matches(isDisplayed())); | ||
|
||
ViewInteraction button5 = onView( | ||
allOf(withId(R.id.button5), | ||
childAtPosition( | ||
childAtPosition( | ||
IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class), | ||
1), | ||
1), | ||
isDisplayed())); | ||
button5.check(matches(isDisplayed())); | ||
|
||
ViewInteraction button6 = onView( | ||
allOf(withId(R.id.button6), | ||
childAtPosition( | ||
childAtPosition( | ||
IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class), | ||
1), | ||
2), | ||
isDisplayed())); | ||
button6.check(matches(isDisplayed())); | ||
|
||
ViewInteraction button7 = onView( | ||
allOf(withId(R.id.button6), | ||
childAtPosition( | ||
childAtPosition( | ||
IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class), | ||
1), | ||
2), | ||
isDisplayed())); | ||
button7.check(matches(isDisplayed())); | ||
|
||
} | ||
|
||
private static Matcher<View> childAtPosition( | ||
final Matcher<View> parentMatcher, final int position) { | ||
|
||
return new TypeSafeMatcher<View>() { | ||
@Override | ||
public void describeTo(Description description) { | ||
description.appendText("Child at position " + position + " in parent "); | ||
parentMatcher.describeTo(description); | ||
} | ||
|
||
@Override | ||
public boolean matchesSafely(View view) { | ||
ViewParent parent = view.getParent(); | ||
return parent instanceof ViewGroup && parentMatcher.matches(parent) | ||
&& view.equals(((ViewGroup) parent).getChildAt(position)); | ||
} | ||
}; | ||
} | ||
} |