Add title bar tests

This commit is contained in:
mjaillot 2021-09-06 15:44:27 +02:00
parent 1b05eeedea
commit 2f921cda14
4 changed files with 143 additions and 5 deletions

View File

@ -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

View File

@ -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<StatusViewHolder>(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<StatusViewHolder>(0, clickChildViewWithId(R.id.profilePic))
)
onView(withId(R.id.accountNameTextView)).check(matches(isDisplayed()))
onView(withId(R.id.profilePictureImageView)).check(matches(isDisplayed()))
}
@Test

View File

@ -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<View> withToolbarTitle(CharSequence text) {
return new WithCharSequenceMatcher(is(text), ToolbarMethod.GET_TITLE);
}
public static Matcher<View> withToolbarSubtitle(CharSequence text) {
return new WithCharSequenceMatcher(is(text), ToolbarMethod.GET_SUBTITLE);
}
public static Matcher<View> withToolbarTitle(final int resourceId) {
return new WithStringResourceMatcher(resourceId, ToolbarMethod.GET_TITLE);
}
public static Matcher<View> withToolbarSubTitle(final int resourceId) {
return new WithStringResourceMatcher(resourceId, ToolbarMethod.GET_SUBTITLE);
}
private static final class WithCharSequenceMatcher extends BoundedMatcher<View, Toolbar> {
private final Matcher<CharSequence> charSequenceMatcher;
private final ToolbarMethod method;
private WithCharSequenceMatcher(Matcher<CharSequence> 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<View, Toolbar> {
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
}
}

View File

@ -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() {