From 2f921cda143ec3be5bd5d4ffd05cbed2d79c228a Mon Sep 17 00:00:00 2001 From: mjaillot Date: Mon, 6 Sep 2021 15:44:27 +0200 Subject: [PATCH] Add title bar tests --- .../java/org/pixeldroid/app/DrawerMenuTest.kt | 9 +- .../java/org/pixeldroid/app/HomeFeedTest.kt | 5 +- .../app/testUtility/ToolbarMatchers.java | 132 ++++++++++++++++++ .../java/org/pixeldroid/app/PostUnitTest.kt | 2 +- 4 files changed, 143 insertions(+), 5 deletions(-) create mode 100644 app/src/androidTest/java/org/pixeldroid/app/testUtility/ToolbarMatchers.java diff --git a/app/src/androidTest/java/org/pixeldroid/app/DrawerMenuTest.kt b/app/src/androidTest/java/org/pixeldroid/app/DrawerMenuTest.kt index 81175963..b72f1b66 100644 --- a/app/src/androidTest/java/org/pixeldroid/app/DrawerMenuTest.kt +++ b/app/src/androidTest/java/org/pixeldroid/app/DrawerMenuTest.kt @@ -1,6 +1,7 @@ package org.pixeldroid.app import android.content.Context +import androidx.appcompat.widget.Toolbar import androidx.test.core.app.ActivityScenario import androidx.test.core.app.ApplicationProvider import androidx.test.espresso.Espresso.onView @@ -21,6 +22,8 @@ import org.junit.Rule import org.junit.Test import org.junit.rules.Timeout import org.junit.runner.RunWith +import org.pixeldroid.app.testUtility.ToolbarMatchers.withToolbarSubtitle +import org.pixeldroid.app.testUtility.ToolbarMatchers.withToolbarTitle @RunWith(AndroidJUnit4::class) class DrawerMenuTest { @@ -158,7 +161,8 @@ class DrawerMenuTest { waitForView(R.id.profilePictureImageView) - onView(withId(R.id.accountNameTextView)).check(matches(withText("PixelDroid Developer"))) + onView(isAssignableFrom(Toolbar::class.java)).check(matches(withToolbarTitle("PixelDroid Developer"))) + onView(isAssignableFrom(Toolbar::class.java)).check(matches(withToolbarSubtitle("@pixeldroid"))) } @Test @@ -176,7 +180,8 @@ class DrawerMenuTest { onView(withText("@user2")).perform(click()) waitForView(R.id.profilePictureImageView) - onView(withId(R.id.accountNameTextView)).check(matches(withText("User 2"))) + onView(isAssignableFrom(Toolbar::class.java)).check(matches(withToolbarTitle("User 2"))) + onView(isAssignableFrom(Toolbar::class.java)).check(matches(withToolbarSubtitle("@user2"))) } @Test diff --git a/app/src/androidTest/java/org/pixeldroid/app/HomeFeedTest.kt b/app/src/androidTest/java/org/pixeldroid/app/HomeFeedTest.kt index 84c9e735..3151cc5a 100644 --- a/app/src/androidTest/java/org/pixeldroid/app/HomeFeedTest.kt +++ b/app/src/androidTest/java/org/pixeldroid/app/HomeFeedTest.kt @@ -2,6 +2,7 @@ package org.pixeldroid.app import android.content.Context +import androidx.appcompat.widget.Toolbar import androidx.test.core.app.ActivityScenario import androidx.test.core.app.ApplicationProvider import androidx.test.espresso.Espresso.onView @@ -180,7 +181,7 @@ class HomeFeedTest { onView(withId(R.id.list)).perform( actionOnItemAtPosition(0, clickChildViewWithId(R.id.username)) ) - onView(withId(R.id.accountNameTextView)).check(matches(isDisplayed())) + onView(withId(R.id.profilePictureImageView)).check(matches(isDisplayed())) } @Test @@ -191,7 +192,7 @@ class HomeFeedTest { onView(withId(R.id.list)).perform( actionOnItemAtPosition(0, clickChildViewWithId(R.id.profilePic)) ) - onView(withId(R.id.accountNameTextView)).check(matches(isDisplayed())) + onView(withId(R.id.profilePictureImageView)).check(matches(isDisplayed())) } @Test diff --git a/app/src/androidTest/java/org/pixeldroid/app/testUtility/ToolbarMatchers.java b/app/src/androidTest/java/org/pixeldroid/app/testUtility/ToolbarMatchers.java new file mode 100644 index 00000000..c38df4d8 --- /dev/null +++ b/app/src/androidTest/java/org/pixeldroid/app/testUtility/ToolbarMatchers.java @@ -0,0 +1,132 @@ +package org.pixeldroid.app.testUtility; + +import android.content.res.Resources; +import android.view.View; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; + +import static org.hamcrest.CoreMatchers.is; + +import androidx.annotation.Nullable; +import androidx.appcompat.widget.Toolbar; +import androidx.test.espresso.matcher.BoundedMatcher; + +public class ToolbarMatchers { + + public static Matcher withToolbarTitle(CharSequence text) { + return new WithCharSequenceMatcher(is(text), ToolbarMethod.GET_TITLE); + } + + public static Matcher withToolbarSubtitle(CharSequence text) { + return new WithCharSequenceMatcher(is(text), ToolbarMethod.GET_SUBTITLE); + } + + + public static Matcher withToolbarTitle(final int resourceId) { + return new WithStringResourceMatcher(resourceId, ToolbarMethod.GET_TITLE); + } + + public static Matcher withToolbarSubTitle(final int resourceId) { + return new WithStringResourceMatcher(resourceId, ToolbarMethod.GET_SUBTITLE); + } + + + private static final class WithCharSequenceMatcher extends BoundedMatcher { + + private final Matcher charSequenceMatcher; + private final ToolbarMethod method; + + private WithCharSequenceMatcher(Matcher charSequenceMatcher, ToolbarMethod method) { + super(Toolbar.class); + this.charSequenceMatcher = charSequenceMatcher; + this.method = method; + } + + @Override + public void describeTo(Description description) { + description.appendText("with text: "); + charSequenceMatcher.describeTo(description); + } + + @Override + protected boolean matchesSafely(Toolbar toolbar) { + + CharSequence actualText; + switch (method) { + case GET_TITLE: + actualText = toolbar.getTitle(); + break; + case GET_SUBTITLE: + actualText = toolbar.getSubtitle(); + break; + default: + throw new IllegalStateException("Unexpected Toolbar method: " + method.toString()); + } + + return charSequenceMatcher.matches(actualText); + } + } + + static final class WithStringResourceMatcher extends BoundedMatcher { + + private final int resourceId; + + @Nullable + private String resourceName; + @Nullable + private String expectedText; + + private final ToolbarMethod method; + + + private WithStringResourceMatcher(int resourceId, ToolbarMethod method) { + super(Toolbar.class); + this.resourceId = resourceId; + this.method = method; + } + + @Override + public void describeTo(Description description) { + description.appendText("with string from resource id: ").appendValue(resourceId); + if (null != resourceName) { + description.appendText("[").appendText(resourceName).appendText("]"); + } + if (null != expectedText) { + description.appendText(" value: ").appendText(expectedText); + } + } + + @Override + public boolean matchesSafely(Toolbar toolbar) { + if (null == expectedText) { + try { + expectedText = toolbar.getResources().getString(resourceId); + resourceName = toolbar.getResources().getResourceEntryName(resourceId); + } catch (Resources.NotFoundException ignored) { + /* view could be from a context unaware of the resource id. */ + } + } + CharSequence actualText; + switch (method) { + case GET_TITLE: + actualText = toolbar.getTitle(); + break; + case GET_SUBTITLE: + actualText = toolbar.getSubtitle(); + break; + default: + throw new IllegalStateException("Unexpected Toolbar method: " + method.toString()); + } + // FYI: actualText may not be string ... its just a char sequence convert to string. + return null != expectedText + && null != actualText + && expectedText.equals(actualText.toString()); + } + } + + enum ToolbarMethod { + GET_TITLE, + GET_SUBTITLE + } +} \ No newline at end of file diff --git a/app/src/test/java/org/pixeldroid/app/PostUnitTest.kt b/app/src/test/java/org/pixeldroid/app/PostUnitTest.kt index e0d34fd3..7b5265c0 100644 --- a/app/src/test/java/org/pixeldroid/app/PostUnitTest.kt +++ b/app/src/test/java/org/pixeldroid/app/PostUnitTest.kt @@ -34,7 +34,7 @@ class PostUnitTest { fun getProfilePicUrlReturnsAValidURL() = Assert.assertNotNull(status.getProfilePicUrl()) @Test - fun getUsernameReturnsACorrectName() = Assert.assertEquals(status.account!!.display_name, status.account!!.getusername()) + fun getUsernameReturnsACorrectName() = Assert.assertEquals(status.account!!.username, status.account!!.getusername()) /*@Test fun getUsernameReturnsOtherNameIfUsernameIsNull() {