Skip to content

Commit

Permalink
Fix Espresso test
Browse files Browse the repository at this point in the history
  • Loading branch information
andreynovikov committed Feb 15, 2024
1 parent b2b60f9 commit 3003c69
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 46 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ dependencies {
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.5.1'
androidTestImplementation 'androidx.test:rules:1.5.0'
implementation "androidx.tracing:tracing:1.2.0"
implementation "androidx.lifecycle:lifecycle-viewmodel:2.7.0"
implementation "androidx.lifecycle:lifecycle-livedata:2.7.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:2.7.0"
Expand Down
100 changes: 54 additions & 46 deletions app/src/androidTest/java/mobi/maptrek/SimpleRunTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.longClick;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition;
import static androidx.test.espresso.matcher.RootMatchers.isDialog;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withClassName;
Expand Down Expand Up @@ -72,8 +73,9 @@ public void beforeTest() {

@Test
public void mainActivityTest() {
// Close Introduction panel if test is running separately

try {
// Close Introduction panel if test is running separately
ViewInteraction appCompatButton = onView(
allOf(
withId(R.id.skip),
Expand All @@ -87,6 +89,7 @@ public void mainActivityTest() {
}

// World map download dialog is displayed - close it

ViewInteraction appCompatButton2 = onView(
allOf(
withId(android.R.id.button2),
Expand All @@ -102,53 +105,76 @@ public void mainActivityTest() {
allOf(
withId(R.id.moreButton),
childAtPosition(
allOf(
withId(R.id.actionPanel),
childAtPosition(
withClassName(is("android.widget.RelativeLayout")),
1
)
),
withId(R.id.actionPanel),
4
),
isDisplayed()
)
);
appCompatImageButton.perform(click());

// Press 'About' menu item
// Press 'Settings' menu item

DataInteraction linearLayout = onData(anything())
.inAdapterView(
allOf(
withId(android.R.id.list),
childAtPosition(
withClassName(is("android.widget.FrameLayout")),
0
withClassName(is("android.widget.FrameLayout")),
0
)
)
)
.atPosition(0);
.atPosition(1);
linearLayout.perform(click());

// Check for 'Trekarta' title
// Check for 'About' preference item

ViewInteraction textView = onView(
allOf(
withId(android.R.id.title),
withText("About"),
isDisplayed()
)
);
textView.check(matches(withText("About")));

// Press 'About' preference item

ViewInteraction recyclerView = onView(
allOf(
withId(androidx.preference.R.id.recycler_view),
childAtPosition(
withId(android.R.id.list_container),
0
)
)
);
recyclerView.perform(actionOnItemAtPosition(3, click()));

// Check for 'Trekarta' title

ViewInteraction imageView = onView(
allOf(
withId(R.id.title),
withContentDescription("Trekarta"),
childAtPosition(
childAtPosition(
IsInstanceOf.instanceOf(android.widget.LinearLayout.class),
0
),
1
),
isDisplayed()
)
);
textView.check(matches(withContentDescription("Trekarta")));
imageView.check(matches(isDisplayed()));

// Check for 'trekarta.info' link

ViewInteraction textView2 = onView(
allOf(
withId(R.id.links),
withText("https://trekarta.info/"),
isDisplayed()
)
);
textView2.check(matches(withText("https://trekarta.info/")));

pressBack();
pressBack();

// Press 'Places' button
Expand All @@ -157,13 +183,7 @@ public void mainActivityTest() {
allOf(
withId(R.id.placesButton),
childAtPosition(
allOf(
withId(R.id.actionPanel),
childAtPosition(
withClassName(is("android.widget.RelativeLayout")),
1
)
),
withId(R.id.actionPanel),
2
),
isDisplayed()
Expand All @@ -179,13 +199,7 @@ public void mainActivityTest() {
allOf(
withId(R.id.mapsButton),
childAtPosition(
allOf(
withId(R.id.actionPanel),
childAtPosition(
withClassName(is("android.widget.RelativeLayout")),
1
)
),
withId(R.id.actionPanel),
3
),
isDisplayed()
Expand All @@ -200,8 +214,8 @@ public void mainActivityTest() {
allOf(
withId(android.R.id.list),
childAtPosition(
withClassName(is("android.widget.FrameLayout")),
0
withClassName(is("android.widget.FrameLayout")),
0
)
)
)
Expand All @@ -210,7 +224,7 @@ public void mainActivityTest() {

// Find first legend title

ViewInteraction textView2 = onView(
ViewInteraction textView3 = onView(
allOf(
withId(R.id.name),
withText("Administrative"),
Expand All @@ -224,7 +238,7 @@ public void mainActivityTest() {
isDisplayed()
)
);
textView2.check(matches(withText("Administrative")));
textView3.check(matches(withText("Administrative")));

pressBack();

Expand All @@ -234,13 +248,7 @@ public void mainActivityTest() {
allOf(
withId(R.id.locationButton),
childAtPosition(
allOf(
withId(R.id.actionPanel),
childAtPosition(
withClassName(is("android.widget.RelativeLayout")),
1
)
),
withId(R.id.actionPanel),
0
),
isDisplayed()
Expand Down

0 comments on commit 3003c69

Please sign in to comment.