From 226590a1f44b0ded244fcc4bfb45cdee62f70ae7 Mon Sep 17 00:00:00 2001 From: Kenny Root Date: Sat, 5 Sep 2015 22:03:04 -0700 Subject: Add a test for setting the host color to red --- .../java/org/connectbot/ConnectbotMatchers.java | 97 ++++++++++++++++++ .../java/org/connectbot/StartupTest.java | 108 ++++++++++----------- 2 files changed, 148 insertions(+), 57 deletions(-) create mode 100644 app/src/androidTest/java/org/connectbot/ConnectbotMatchers.java (limited to 'app/src/androidTest') diff --git a/app/src/androidTest/java/org/connectbot/ConnectbotMatchers.java b/app/src/androidTest/java/org/connectbot/ConnectbotMatchers.java new file mode 100644 index 0000000..85a071d --- /dev/null +++ b/app/src/androidTest/java/org/connectbot/ConnectbotMatchers.java @@ -0,0 +1,97 @@ +package org.connectbot; + +import android.support.annotation.ColorInt; +import android.support.annotation.NonNull; +import android.support.test.espresso.matcher.BoundedMatcher; +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; + +import org.connectbot.bean.HostBean; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; + +import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant; +import static android.support.test.espresso.matcher.ViewMatchers.withId; +import static org.hamcrest.CoreMatchers.allOf; + +public class ConnectbotMatchers { + /** + * Matches the nickname of a {@link HostBean}. + */ + @NonNull + public static Matcher withHostNickname(final String content) { + return new BoundedMatcher(HostBean.class) { + @Override + public boolean matchesSafely(HostBean host) { + return host.getNickname().matches(content); + } + + @Override + public void describeTo(Description description) { + description.appendText("with host nickname '" + content + "'"); + } + }; + } + + /** + * Matches the drawable state on an ImageView that is set with setImageState. + */ + @NonNull + public static Matcher withDrawableState(final int expectedState) { + return new TypeSafeMatcher() { + @Override + public boolean matchesSafely(View view) { + if (!(view instanceof ImageView)) { + return false; + } + + int[] states = view.getDrawableState(); + for (int state : states) { + if (state == expectedState) { + return true; + } + } + return false; + } + + @Override + public void describeTo(Description description) { + description.appendText("with drawable state '" + expectedState + "'"); + } + }; + } + + @NonNull + public static Matcher withTextColor(@ColorInt final int expectedColor) { + return new TypeSafeMatcher() { + @Override + public boolean matchesSafely(View view) { + if (!(view instanceof TextView)) { + return false; + } + + TextView tv = (TextView) view; + return tv.getCurrentTextColor() == expectedColor; + } + + @Override + public void describeTo(Description description) { + description.appendText("with color '" + Integer.toHexString(expectedColor) + "'"); + } + }; + } + + @NonNull + public static Matcher hostDisconnected() { + return hasDescendant(allOf(withId(android.R.id.icon), + withDrawableState(android.R.attr.state_expanded))); + } + + @NonNull + public static Matcher hostConnected() { + return hasDescendant(allOf(withId(android.R.id.icon), + withDrawableState(android.R.attr.state_checked))); + } +} diff --git a/app/src/androidTest/java/org/connectbot/StartupTest.java b/app/src/androidTest/java/org/connectbot/StartupTest.java index 0eb9c11..8025517 100644 --- a/app/src/androidTest/java/org/connectbot/StartupTest.java +++ b/app/src/androidTest/java/org/connectbot/StartupTest.java @@ -11,7 +11,11 @@ import org.junit.Test; import org.junit.runner.RunWith; import android.content.Intent; +import android.content.res.Resources; +import android.support.annotation.ColorInt; +import android.support.annotation.ColorRes; import android.support.annotation.NonNull; +import android.support.annotation.StringRes; import android.support.test.InstrumentationRegistry; import android.support.test.espresso.intent.Intents; import android.support.test.espresso.matcher.BoundedMatcher; @@ -19,6 +23,7 @@ import android.support.test.rule.ActivityTestRule; import android.support.test.runner.AndroidJUnit4; import android.view.View; import android.widget.ImageView; +import android.widget.TextView; import static android.support.test.espresso.Espresso.onData; import static android.support.test.espresso.Espresso.onView; @@ -36,6 +41,10 @@ import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant; import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withText; +import static org.connectbot.ConnectbotMatchers.hostConnected; +import static org.connectbot.ConnectbotMatchers.hostDisconnected; +import static org.connectbot.ConnectbotMatchers.withHostNickname; +import static org.connectbot.ConnectbotMatchers.withTextColor; import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; @@ -87,10 +96,51 @@ public class StartupTest { .check(matches(hostDisconnected())); } + @Test + public void localConnectionCanChangeToRed() throws Exception { + startNewLocalConnectionAndGoBack("Local1"); + changeColor("Local1", R.color.red, R.string.color_red); + } + + /** + * Changes the color of {@code hostName} from the {@link HostListActivity} to the {@code color} + * from {@code R.color.[color]} with identifying {@code stringForColor} from + * {@code R.string.[colorname]}. + */ + private void changeColor(String hostName, @ColorRes int color, @StringRes int stringForColor) { + // Bring up the context menu. + onData(withHostNickname(hostName)).inAdapterView(withId(android.R.id.list)) + .perform(longClick()); + onView(withText(R.string.list_host_edit)).perform(click()); + + // Click on the color category and select the desired one. + onView(withText(R.string.hostpref_color_title)).perform(click()); + onView(withText(stringForColor)).perform(click()); + + // Go back to the host list. + onView(withText(R.string.hostpref_color_title)).perform(pressBack()); + + Resources res = InstrumentationRegistry.getTargetContext().getResources(); + onData(withHostNickname(hostName)).inAdapterView(withId(android.R.id.list)) + .check(matches(hasDescendant(allOf(withId(android.R.id.text1), + withTextColor(res.getColor(color)))))); + } + + private void startNewLocalConnectionAndGoBack(String name) { + startNewLocalConnection(name); + onView(withId(R.id.console_flip)).perform(closeSoftKeyboard(), pressBack()); + onData(withHostNickname(name)).inAdapterView(withId(android.R.id.list)) + .check(matches(isDisplayed())); + } + private void startNewLocalConnection() { + startNewLocalConnection("Local"); + } + + private void startNewLocalConnection(String name) { onView(withId(R.id.transport_selection)).perform(click()); onData(allOf(is(instanceOf(String.class)), is("local"))).perform(click()); - onView(withId(R.id.front_quickconnect)).perform(typeText("Local")); + onView(withId(R.id.front_quickconnect)).perform(typeText(name)); Intents.init(); try { @@ -103,60 +153,4 @@ public class StartupTest { onView(withId(R.id.console_flip)).check(matches( hasDescendant(allOf(isDisplayed(), withId(R.id.terminal_view))))); } - - /** - * Matches the nickname of a {@link HostBean}. - */ - public static Matcher withHostNickname(final String content) { - return new BoundedMatcher(HostBean.class) { - @Override - public boolean matchesSafely(HostBean host) { - return host.getNickname().matches(content); - } - - @Override - public void describeTo(Description description) { - description.appendText("with host nickname '" + content + "'"); - } - }; - } - - /** - * Matches the drawable state on an ImageView that is set with setImageState. - */ - public static Matcher withDrawableState(final int expectedState) { - return new TypeSafeMatcher() { - @Override - public boolean matchesSafely(View view) { - if (!(view instanceof ImageView)) { - return false; - } - - int[] states = view.getDrawableState(); - for (int state : states) { - if (state == expectedState) { - return true; - } - } - return false; - } - - @Override - public void describeTo(Description description) { - description.appendText("with drawable state '" + expectedState + "'"); - } - }; - } - - @NonNull - private Matcher hostDisconnected() { - return hasDescendant(allOf(withId(android.R.id.icon), - withDrawableState(android.R.attr.state_expanded))); - } - - @NonNull - private Matcher hostConnected() { - return hasDescendant(allOf(withId(android.R.id.icon), - withDrawableState(android.R.attr.state_checked))); - } } -- cgit v1.2.3