|
2 | 2 |
|
3 | 3 |
|
4 | 4 | import android.support.test.espresso.ViewInteraction;
|
5 |
| -import android.support.test.espresso.matcher.BoundedMatcher; |
| 5 | +import android.support.test.espresso.contrib.RecyclerViewActions; |
| 6 | +import android.support.test.filters.LargeTest; |
6 | 7 | import android.support.test.rule.ActivityTestRule;
|
7 | 8 | import android.support.test.runner.AndroidJUnit4;
|
8 |
| -import android.test.suitebuilder.annotation.LargeTest; |
9 |
| -import android.text.TextUtils; |
10 | 9 |
|
11 |
| -import com.j256.ormlite.stmt.QueryBuilder; |
12 | 10 | import com.zulip.android.R;
|
13 | 11 | import com.zulip.android.ZulipApp;
|
14 |
| -import com.zulip.android.models.Message; |
| 12 | +import com.zulip.android.filters.NarrowFilterToday; |
| 13 | +import com.zulip.android.helper.ViewAssertions; |
15 | 14 | import com.zulip.android.models.MessageType;
|
16 |
| -import com.zulip.android.models.Person; |
| 15 | +import com.zulip.android.util.ZLog; |
17 | 16 |
|
18 | 17 | import org.apache.commons.lang.RandomStringUtils;
|
19 |
| -import org.hamcrest.Matcher; |
| 18 | +import org.hamcrest.core.AllOf; |
20 | 19 | import org.junit.Before;
|
21 | 20 | import org.junit.FixMethodOrder;
|
22 | 21 | import org.junit.Rule;
|
23 | 22 | import org.junit.Test;
|
24 | 23 | import org.junit.runner.RunWith;
|
25 | 24 | import org.junit.runners.MethodSorters;
|
26 | 25 |
|
27 |
| -import java.sql.SQLException; |
28 |
| -import java.util.ArrayList; |
29 |
| -import java.util.Arrays; |
30 |
| -import java.util.List; |
31 |
| - |
32 |
| -import static android.support.test.espresso.Espresso.onData; |
33 | 26 | import static android.support.test.espresso.Espresso.onView;
|
34 | 27 | import static android.support.test.espresso.action.ViewActions.click;
|
35 | 28 | import static android.support.test.espresso.action.ViewActions.replaceText;
|
36 | 29 | import static android.support.test.espresso.assertion.ViewAssertions.matches;
|
37 | 30 | import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
|
38 | 31 | import static android.support.test.espresso.matcher.ViewMatchers.withId;
|
| 32 | +import static android.support.test.espresso.matcher.ViewMatchers.withText; |
| 33 | +import static com.zulip.android.helper.Matchers.withFirstId; |
| 34 | +import static com.zulip.android.helper.Matchers.withMessageHeaderHolder; |
| 35 | +import static com.zulip.android.helper.Matchers.withMessageHolder; |
| 36 | +import static com.zulip.android.helper.Matchers.withMessageHolderAndClick; |
| 37 | +import static com.zulip.android.helper.ViewAssertions.checkMessagesOnlyFromToday; |
39 | 38 | import static org.hamcrest.Matchers.allOf;
|
40 |
| -import static org.hamcrest.Matchers.instanceOf; |
41 | 39 | import static org.hamcrest.Matchers.not;
|
42 | 40 |
|
43 | 41 |
|
| 42 | +/** |
| 43 | + * Make sure you have entered the login details here{@link BaseTest#EMAIL_TEST} |
| 44 | + * And your password here{@link BaseTest#PASSWORD_TEST} to login if you have not logged in! |
| 45 | + */ |
44 | 46 | @LargeTest
|
45 | 47 | @FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
46 | 48 | @RunWith(AndroidJUnit4.class)
|
47 |
| -public class ChatBoxTest extends BaseTest { |
| 49 | +public class ChatBoxTest { |
48 | 50 | @Rule
|
49 | 51 | public ActivityTestRule<ZulipActivity> mActivityTestRule = new ActivityTestRule<>(ZulipActivity.class);
|
50 | 52 |
|
| 53 | + public static String LOG_TAG = ChatBoxTest.class.getName(); |
51 | 54 | private static String testMessageStream;
|
52 | 55 | private static String testMessagePrivate;
|
53 | 56 |
|
54 |
| - private static String testStreamName; |
55 |
| - private static String testSubjectStream; |
56 |
| - private List<Person> testPersons; |
57 | 57 | private ZulipApp app;
|
58 | 58 |
|
59 | 59 | @Before
|
60 | 60 | public void setUp() {
|
61 |
| - setTestMessageStream((testMessageStream == null) ? RandomStringUtils.randomAlphanumeric(10) : testMessageStream); |
62 |
| - setTestMessagePrivate((testMessagePrivate == null) ? RandomStringUtils.randomAlphanumeric(15) : testMessagePrivate); |
63 |
| - if (ZulipApp.get().getApiKey() == null) { |
64 |
| - login(); |
65 |
| - } |
66 | 61 | app = ZulipApp.get();
|
67 |
| - setUpTestStream(); |
68 |
| - setUpPrivate(); |
69 |
| - } |
70 | 62 |
|
71 |
| - private void setUpPrivate() { |
72 |
| - try { |
73 |
| - QueryBuilder<Message, Object> orderQb = app.getDao(Message.class).queryBuilder(); |
74 |
| - orderQb.where().eq(Message.TYPE_FIELD, MessageType.PRIVATE_MESSAGE).and() |
75 |
| - .isNotNull(Message.RECIPIENTS_FIELD); |
76 |
| - Message message = orderQb.queryForFirst(); |
77 |
| - testPersons = Arrays.asList(message.getPersonalReplyTo(app)); |
78 |
| - } catch (SQLException e) { |
79 |
| - e.printStackTrace(); |
| 63 | + if (ZulipApp.get().getApiKey() == null) { |
| 64 | + BaseTest baseTest = new BaseTest(); |
| 65 | + baseTest.login(); |
| 66 | + sleep(4000); |
80 | 67 | }
|
| 68 | + |
| 69 | + //This is to make sure the latest recieved messages will be added to the list! |
| 70 | + app.setPointer(app.getMaxMessageId()); |
| 71 | + setTestMessageStream((testMessageStream == null) ? RandomStringUtils.randomAlphanumeric(10) : testMessageStream); |
| 72 | + setTestMessagePrivate((testMessagePrivate == null) ? RandomStringUtils.randomAlphanumeric(15) : testMessagePrivate); |
| 73 | + |
81 | 74 | }
|
82 | 75 |
|
83 |
| - private void setUpTestStream() { |
| 76 | + private void sleep(int millis) { |
84 | 77 | try {
|
85 |
| - QueryBuilder<Message, Object> messageQueryBuilder = app.getDao(Message.class).queryBuilder(); |
86 |
| - messageQueryBuilder.where().eq(Message.TYPE_FIELD, MessageType.STREAM_MESSAGE).and() |
87 |
| - .isNotNull(Message.SUBJECT_FIELD).and() |
88 |
| - .isNotNull(Message.STREAM_FIELD); |
89 |
| - Message message = messageQueryBuilder.queryForFirst(); |
90 |
| - testStreamName = message.getStream().getName(); |
91 |
| - testSubjectStream = message.getSubject(); |
92 |
| - } catch (SQLException e) { |
93 |
| - e.printStackTrace(); |
| 78 | + Thread.sleep(millis); |
| 79 | + } catch (InterruptedException e) { |
| 80 | + ZLog.logException(e); |
94 | 81 | }
|
95 | 82 | }
|
96 | 83 |
|
97 | 84 | @Test
|
98 | 85 | public void sendStreamMessage() {
|
99 |
| - //Fill message EditText |
100 |
| - ViewInteraction streamET = onView(allOf(withId(R.id.stream_actv), isDisplayed())); |
101 |
| - streamET.perform(replaceText(testStreamName)); |
| 86 | + //Wait to make sure the messages are loaded |
| 87 | + sleep(2000); |
102 | 88 |
|
103 |
| - //Fill message EditText |
104 |
| - ViewInteraction subjectET = onView(allOf(withId(R.id.topic_actv), isDisplayed())); |
105 |
| - subjectET.perform(replaceText(testSubjectStream)); |
| 89 | + onView(withId(R.id.recyclerView)).perform(RecyclerViewActions.scrollToHolder(withMessageHolderAndClick(MessageType.STREAM_MESSAGE, R.id.contentView))); |
| 90 | + sleep(1000); |
106 | 91 |
|
107 | 92 | //Fill message EditText
|
108 |
| - ViewInteraction editText2 = onView(allOf(withId(R.id.message_et), isDisplayed())); |
109 |
| - editText2.perform(replaceText(testMessageStream)); |
| 93 | + ViewInteraction messageInteraction = onView(allOf(withId(R.id.message_et), isDisplayed())); |
| 94 | + messageInteraction.perform(replaceText(testMessageStream)); |
110 | 95 |
|
111 | 96 | //Click Send Button
|
112 | 97 | ViewInteraction imageView = onView(allOf(withId(R.id.send_btn), isDisplayed()));
|
113 | 98 | imageView.perform(click());
|
114 | 99 |
|
115 |
| - //Verify sent Message |
116 |
| - onData(allOf(instanceOf(Message.class), |
117 |
| - getMessageFromTypeAndContentString(MessageType.STREAM_MESSAGE, testMessageStream))) |
118 |
| - .inAdapterView(withId(R.id.listview)) |
119 |
| - .check(matches(isDisplayed())); |
| 100 | + sleep(2000); |
| 101 | + |
| 102 | + //Scroll And check the new sent message |
| 103 | + onView(withId(R.id.recyclerView)).perform(RecyclerViewActions.scrollToHolder(withMessageHolder(testMessageStream, R.id.contentView))); |
| 104 | + |
| 105 | + onView(AllOf.allOf(withId(R.id.contentView), withText(testMessageStream))).check(matches(isDisplayed())); |
| 106 | + |
| 107 | + checkIfMessagesMatchTheHeaderParent(); |
| 108 | + checkOrderOfMessagesCurrentList(); |
120 | 109 | }
|
121 | 110 |
|
122 |
| - private String getDisplayRecipents() { |
123 |
| - ArrayList<String> names = new ArrayList<String>(); |
124 |
| - for (Person person : testPersons) { |
125 |
| - if (person.getId() != app.getYou().getId()) { |
126 |
| - names.add(person.getEmail()); |
127 |
| - } |
| 111 | + private void hideToolBar() { |
| 112 | + try { |
| 113 | + mActivityTestRule.runOnUiThread(new Runnable() { |
| 114 | + @Override |
| 115 | + public void run() { |
| 116 | + mActivityTestRule.getActivity().hideView(mActivityTestRule.getActivity().findViewById(R.id.appBarLayout)); |
| 117 | + } |
| 118 | + }); |
| 119 | + } catch (Throwable throwable) { |
| 120 | + ZLog.logException(throwable); |
128 | 121 | }
|
129 |
| - return TextUtils.join(", ", names); |
130 | 122 | }
|
131 | 123 |
|
| 124 | + @Test |
| 125 | + public void checkAfterNarrowToStream() { |
| 126 | + sleep(2000); |
| 127 | + |
| 128 | + hideToolBar(); |
| 129 | + |
| 130 | + sleep(2000); |
| 131 | + |
| 132 | + //Scroll to Stream MessageHeaderParent |
| 133 | + onView(withId(R.id.recyclerView)).perform(RecyclerViewActions.scrollToHolder(withMessageHeaderHolder(MessageType.STREAM_MESSAGE))); |
| 134 | + |
| 135 | + //Perform a click on the subject textView to narrow to the topic |
| 136 | + onView(withFirstId(R.id.instance)).perform(click()); |
| 137 | + |
| 138 | + sleep(4000); |
| 139 | + //Check if all messages belong to this subject |
| 140 | + onView(withId(R.id.recyclerView)).check(ViewAssertions.checkIfBelongToSameNarrow(mActivityTestRule.getActivity())); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void checkAfterNarrowToPrivate() { |
| 145 | + sleep(2000); |
| 146 | + |
| 147 | + try { |
| 148 | + mActivityTestRule.runOnUiThread(new Runnable() { |
| 149 | + @Override |
| 150 | + public void run() { |
| 151 | + mActivityTestRule.getActivity().hideView(mActivityTestRule.getActivity().findViewById(R.id.appBarLayout)); |
| 152 | + } |
| 153 | + }); |
| 154 | + } catch (Throwable throwable) { |
| 155 | + ZLog.logException(throwable); |
| 156 | + } |
| 157 | + sleep(2000); |
| 158 | + |
| 159 | + //Scroll to private MessageHeaderParent |
| 160 | + onView(withId(R.id.recyclerView)).perform(RecyclerViewActions.scrollToHolder(withMessageHeaderHolder(MessageType.PRIVATE_MESSAGE))); |
| 161 | + |
| 162 | + //Perform a click on the recipients to narrow to that group or single person |
| 163 | + onView(withFirstId(R.id.instance)).perform(click()); |
| 164 | + |
| 165 | + sleep(4000); |
| 166 | + |
| 167 | + //Check if all messages belong to this private message group |
| 168 | + onView(withId(R.id.recyclerView)).check(ViewAssertions.checkIfBelongToSameNarrow(mActivityTestRule.getActivity())); |
| 169 | + } |
| 170 | + |
| 171 | + |
| 172 | + @Test |
| 173 | + public void checkOrderOfMessagesCurrentList() { |
| 174 | + sleep(2000); |
| 175 | + onView(withId(R.id.recyclerView)).check(ViewAssertions.checkOrderOfMessages(mActivityTestRule.getActivity())); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void checkIfMessagesMatchTheHeaderParent() { |
| 180 | + sleep(2000); |
| 181 | + onView(withId(R.id.recyclerView)).check(ViewAssertions.checkIfMessagesMatchTheHeaderParent(mActivityTestRule.getActivity())); |
| 182 | + } |
| 183 | + |
| 184 | + |
| 185 | + @Test |
| 186 | + public void checkTodaysFilter() { |
| 187 | + sleep(2000); |
| 188 | + /** |
| 189 | + * Narrow to today messages |
| 190 | + * runOnUiThread for changing fragment here{@link ZulipActivity#pushListFragment(MessageListFragment, String)} |
| 191 | + */ |
| 192 | + try { |
| 193 | + mActivityTestRule.runOnUiThread(new Runnable() { |
| 194 | + @Override |
| 195 | + public void run() { |
| 196 | + mActivityTestRule.getActivity().doNarrow(new NarrowFilterToday()); |
| 197 | + } |
| 198 | + }); |
| 199 | + } catch (Throwable throwable) { |
| 200 | + ZLog.logException(throwable); |
| 201 | + } |
| 202 | + |
| 203 | + sleep(2000); |
| 204 | + //Check messages if only they are from today |
| 205 | + onView(withId(R.id.recyclerView)).check(checkMessagesOnlyFromToday()); |
| 206 | + } |
| 207 | + |
| 208 | + |
132 | 209 | @Test
|
133 | 210 | public void sendPrivateMessage() {
|
134 |
| - //Click Switch Button |
135 |
| - ViewInteraction imageView = onView(allOf(withId(R.id.togglePrivateStream_btn), isDisplayed())); |
136 |
| - imageView.perform(click()); |
| 211 | + //Wait to make sure the messages are loaded |
| 212 | + sleep(2000); |
137 | 213 |
|
138 |
| - //Fill message EditText |
139 |
| - ViewInteraction recipentET = onView(allOf(withId(R.id.topic_actv), isDisplayed())); |
140 |
| - recipentET.perform(replaceText(getDisplayRecipents())); |
| 214 | + onView(withId(R.id.recyclerView)).perform(RecyclerViewActions.scrollToHolder(withMessageHolderAndClick(MessageType.PRIVATE_MESSAGE, R.id.contentView))); |
| 215 | + sleep(1000); |
141 | 216 |
|
142 | 217 | //Fill message EditText
|
143 |
| - ViewInteraction editText2 = onView(allOf(withId(R.id.message_et), isDisplayed())); |
144 |
| - editText2.perform(replaceText(testMessagePrivate)); |
| 218 | + ViewInteraction messageInteraction = onView(allOf(withId(R.id.message_et), isDisplayed())); |
| 219 | + messageInteraction.perform(replaceText(testMessagePrivate)); |
145 | 220 |
|
146 | 221 | //Click Send Button
|
147 |
| - ViewInteraction sendBtn = onView(allOf(withId(R.id.send_btn), isDisplayed())); |
148 |
| - sendBtn.perform(click()); |
149 |
| - |
150 |
| - //Verify sent Message |
151 |
| - onData(allOf(instanceOf(Message.class), |
152 |
| - getMessageFromTypeAndContentString(MessageType.PRIVATE_MESSAGE, testMessagePrivate))) |
153 |
| - .inAdapterView(withId(R.id.listview)) |
154 |
| - .check(matches(isDisplayed())); |
| 222 | + ViewInteraction imageView = onView(allOf(withId(R.id.send_btn), isDisplayed())); |
| 223 | + imageView.perform(click()); |
| 224 | + |
| 225 | + sleep(2000); |
| 226 | + |
| 227 | + //Scroll And check the new sent message |
| 228 | + onView(withId(R.id.recyclerView)).perform(RecyclerViewActions.scrollToHolder(withMessageHolder(testMessagePrivate, R.id.contentView))); |
| 229 | + |
| 230 | + onView(AllOf.allOf(withId(R.id.contentView), withText(testMessagePrivate))).check(matches(isDisplayed())); |
| 231 | + |
| 232 | + checkIfMessagesMatchTheHeaderParent(); |
| 233 | + checkOrderOfMessagesCurrentList(); |
155 | 234 | }
|
156 | 235 |
|
157 | 236 | @Test
|
158 | 237 | public void switchChatBox() {
|
| 238 | + sleep(2000); |
| 239 | + //Show the Fab button |
| 240 | + try { |
| 241 | + mActivityTestRule.runOnUiThread(new Runnable() { |
| 242 | + @Override |
| 243 | + public void run() { |
| 244 | + mActivityTestRule.getActivity().showView(mActivityTestRule.getActivity().fab); |
| 245 | + } |
| 246 | + }); |
| 247 | + } catch (Throwable throwable) { |
| 248 | + ZLog.logException(throwable); |
| 249 | + } |
| 250 | + |
| 251 | + sleep(500); |
| 252 | + //Click Fab Button |
| 253 | + ViewInteraction fabInteraction = onView(allOf(withId(R.id.fab), isDisplayed())); |
| 254 | + fabInteraction.perform(click()); |
| 255 | + |
| 256 | + sleep(500); |
159 | 257 | //Click Switch Button
|
160 |
| - ViewInteraction imageView = onView(allOf(withId(R.id.togglePrivateStream_btn), isDisplayed())); |
161 |
| - imageView.perform(click()); |
| 258 | + ViewInteraction switchBtnInteraction = onView(allOf(withId(R.id.togglePrivateStream_btn), isDisplayed())); |
| 259 | + switchBtnInteraction.perform(click()); |
162 | 260 |
|
| 261 | + sleep(500); |
163 | 262 | //Check if Arrow TextView is not displayed for switchingChatBox
|
164 | 263 | onView(withId(R.id.textView)).check(matches(not(isDisplayed())));
|
165 | 264 | }
|
166 | 265 |
|
167 |
| - public static Matcher<Object> getMessageFromType(final MessageType messageType) { |
168 |
| - return new BoundedMatcher<Object, Message>(Message.class) { |
169 |
| - @Override |
170 |
| - public void describeTo(org.hamcrest.Description description) { |
171 |
| - description.appendText("Error"); |
172 |
| - } |
173 |
| - |
174 |
| - @Override |
175 |
| - protected boolean matchesSafely(Message item) { |
176 |
| - return item.getType() == messageType; |
177 |
| - } |
178 |
| - }; |
179 |
| - } |
180 |
| - |
181 |
| - public static Matcher<Object> getMessageFromTypeAndContentString(final MessageType messageType, final String text) { |
182 |
| - return new BoundedMatcher<Object, Message>(Message.class) { |
183 |
| - @Override |
184 |
| - public void describeTo(org.hamcrest.Description description) { |
185 |
| - description.appendText("Error"); |
186 |
| - } |
187 |
| - |
188 |
| - @Override |
189 |
| - protected boolean matchesSafely(Message item) { |
190 |
| - return item.getType() == messageType && item.getContent().contains(text); |
191 |
| - } |
192 |
| - }; |
193 |
| - } |
194 |
| - |
195 | 266 | public static String getTestMessageStream() {
|
196 | 267 | return testMessageStream;
|
197 | 268 | }
|
|
0 commit comments