Skip to content

Commit

Permalink
Add MainActivityTest
Browse files Browse the repository at this point in the history
  • Loading branch information
yadav-rahul committed Apr 2, 2017
1 parent 56bd9b0 commit 5c728d2
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ android {
targetSdkVersion 25
versionCode 3
versionName "1.1.2"
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
Expand All @@ -28,4 +29,7 @@ dependencies {
testCompile 'junit:junit:4.12'
compile project(":tastytoast")
compile 'com.android.support:appcompat-v7:25.2.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
}
}
122 changes: 122 additions & 0 deletions app/src/androidTest/java/com/sdsmdg/demoexample/MainActivityTest.java
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));
}
};
}
}

0 comments on commit 5c728d2

Please sign in to comment.