|
| 1 | +package com.zulip.android.activities; |
| 2 | + |
| 3 | + |
| 4 | +import android.support.test.espresso.ViewInteraction; |
| 5 | +import android.support.test.espresso.matcher.BoundedMatcher; |
| 6 | +import android.support.test.rule.ActivityTestRule; |
| 7 | +import android.support.test.runner.AndroidJUnit4; |
| 8 | +import android.test.suitebuilder.annotation.LargeTest; |
| 9 | + |
| 10 | +import com.zulip.android.R; |
| 11 | +import com.zulip.android.ZulipApp; |
| 12 | +import com.zulip.android.models.Message; |
| 13 | +import com.zulip.android.models.MessageType; |
| 14 | + |
| 15 | +import org.apache.commons.lang.RandomStringUtils; |
| 16 | +import org.hamcrest.Matcher; |
| 17 | +import org.junit.Before; |
| 18 | +import org.junit.Rule; |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.runner.RunWith; |
| 21 | + |
| 22 | +import static android.support.test.espresso.Espresso.onData; |
| 23 | +import static android.support.test.espresso.Espresso.onView; |
| 24 | +import static android.support.test.espresso.action.ViewActions.click; |
| 25 | +import static android.support.test.espresso.action.ViewActions.replaceText; |
| 26 | +import static android.support.test.espresso.assertion.ViewAssertions.matches; |
| 27 | +import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; |
| 28 | +import static android.support.test.espresso.matcher.ViewMatchers.withId; |
| 29 | +import static org.hamcrest.Matchers.allOf; |
| 30 | +import static org.hamcrest.Matchers.instanceOf; |
| 31 | + |
| 32 | + |
| 33 | +@LargeTest |
| 34 | +@RunWith(AndroidJUnit4.class) |
| 35 | +public class ChatBoxTest extends BaseTest { |
| 36 | + @Rule |
| 37 | + public ActivityTestRule<ZulipActivity> mActivityTestRule = new ActivityTestRule<>(ZulipActivity.class); |
| 38 | + |
| 39 | + private static String testMessageStream; |
| 40 | + private static String testMessagePrivate; |
| 41 | + |
| 42 | + @Before |
| 43 | + public void setUp() { |
| 44 | + testMessageStream = (testMessageStream == null) ? RandomStringUtils.randomAlphanumeric(10) : testMessageStream; |
| 45 | + testMessagePrivate = (testMessagePrivate == null) ? RandomStringUtils.randomAlphanumeric(15) : testMessagePrivate; |
| 46 | + if (ZulipApp.get().getApiKey() == null) { |
| 47 | + login(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + @Test |
| 53 | + public void sendStreamMessage() { |
| 54 | + //Fill ChatBox by clicking content TextView in the ListView |
| 55 | + onData(allOf(instanceOf(Message.class), |
| 56 | + getMessageFromType(MessageType.STREAM_MESSAGE))) |
| 57 | + .inAdapterView(withId(R.id.listview)) |
| 58 | + .atPosition(0) |
| 59 | + .onChildView(withId(R.id.contentView)) |
| 60 | + .perform(click()); |
| 61 | + |
| 62 | + //Fill message EditText |
| 63 | + ViewInteraction editText2 = onView(allOf(withId(R.id.message_et), isDisplayed())); |
| 64 | + editText2.perform(replaceText(testMessageStream)); |
| 65 | + |
| 66 | + //Click Send Button |
| 67 | + ViewInteraction imageView = onView(allOf(withId(R.id.send_btn), isDisplayed())); |
| 68 | + imageView.perform(click()); |
| 69 | + |
| 70 | + //Verify sent Message |
| 71 | + onData(allOf(instanceOf(Message.class), |
| 72 | + getMessageFromTypeAndContentString(MessageType.STREAM_MESSAGE, testMessageStream))) |
| 73 | + .inAdapterView(withId(R.id.listview)) |
| 74 | + .check(matches(isDisplayed())); |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + @Test |
| 79 | + public void sendPrivateMessage() { |
| 80 | + //Fill ChatBox by clicking content TextView in the ListView |
| 81 | + onData(allOf(instanceOf(Message.class), |
| 82 | + getMessageFromType(MessageType.PRIVATE_MESSAGE))) |
| 83 | + .inAdapterView(withId(R.id.listview)).atPosition(0) |
| 84 | + .onChildView(withId(R.id.contentView)) |
| 85 | + .perform(click()); |
| 86 | + |
| 87 | + //Fill message EditText |
| 88 | + ViewInteraction editText2 = onView(allOf(withId(R.id.message_et), isDisplayed())); |
| 89 | + editText2.perform(replaceText(testMessagePrivate)); |
| 90 | + |
| 91 | + //Click Send Button |
| 92 | + ViewInteraction imageView = onView(allOf(withId(R.id.send_btn), isDisplayed())); |
| 93 | + imageView.perform(click()); |
| 94 | + |
| 95 | + //Verify sent Message |
| 96 | + onData(allOf(instanceOf(Message.class), |
| 97 | + getMessageFromTypeAndContentString(MessageType.PRIVATE_MESSAGE, testMessagePrivate))) |
| 98 | + .inAdapterView(withId(R.id.listview)) |
| 99 | + .check(matches(isDisplayed())); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void switchChatBox() { |
| 104 | + //Click Switch Button |
| 105 | + ViewInteraction imageView = onView(allOf(withId(R.id.togglePrivateStream_btn), isDisplayed())); |
| 106 | + imageView.perform(click()); |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + public static Matcher<Object> getMessageFromType(final MessageType messageType) { |
| 111 | + return new BoundedMatcher<Object, Message>(Message.class) { |
| 112 | + @Override |
| 113 | + public void describeTo(org.hamcrest.Description description) { |
| 114 | + description.appendText("Error"); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + protected boolean matchesSafely(Message item) { |
| 119 | + return item.getType() == messageType; |
| 120 | + } |
| 121 | + }; |
| 122 | + } |
| 123 | + |
| 124 | + public static Matcher<Object> getMessageFromTypeAndContentString(final MessageType messageType, final String text) { |
| 125 | + return new BoundedMatcher<Object, Message>(Message.class) { |
| 126 | + @Override |
| 127 | + public void describeTo(org.hamcrest.Description description) { |
| 128 | + description.appendText("Error"); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + protected boolean matchesSafely(Message item) { |
| 133 | + return item.getType() == messageType && item.getContent().contains(text); |
| 134 | + } |
| 135 | + }; |
| 136 | + } |
| 137 | +} |
0 commit comments