Skip to content
This repository was archived by the owner on Jul 25, 2024. It is now read-only.

Commit c80ce15

Browse files
kunall17niftynei
authored andcommitted
Implemented new test for the new recyclerView UI
1 parent 29b2938 commit c80ce15

File tree

7 files changed

+200
-119
lines changed

7 files changed

+200
-119
lines changed

app/src/androidTest/java/com/zulip/android/activities/ChatBoxTest.java

Lines changed: 182 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -2,196 +2,267 @@
22

33

44
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;
67
import android.support.test.rule.ActivityTestRule;
78
import android.support.test.runner.AndroidJUnit4;
8-
import android.test.suitebuilder.annotation.LargeTest;
9-
import android.text.TextUtils;
109

11-
import com.j256.ormlite.stmt.QueryBuilder;
1210
import com.zulip.android.R;
1311
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;
1514
import com.zulip.android.models.MessageType;
16-
import com.zulip.android.models.Person;
15+
import com.zulip.android.util.ZLog;
1716

1817
import org.apache.commons.lang.RandomStringUtils;
19-
import org.hamcrest.Matcher;
18+
import org.hamcrest.core.AllOf;
2019
import org.junit.Before;
2120
import org.junit.FixMethodOrder;
2221
import org.junit.Rule;
2322
import org.junit.Test;
2423
import org.junit.runner.RunWith;
2524
import org.junit.runners.MethodSorters;
2625

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;
3326
import static android.support.test.espresso.Espresso.onView;
3427
import static android.support.test.espresso.action.ViewActions.click;
3528
import static android.support.test.espresso.action.ViewActions.replaceText;
3629
import static android.support.test.espresso.assertion.ViewAssertions.matches;
3730
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
3831
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;
3938
import static org.hamcrest.Matchers.allOf;
40-
import static org.hamcrest.Matchers.instanceOf;
4139
import static org.hamcrest.Matchers.not;
4240

4341

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+
*/
4446
@LargeTest
4547
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
4648
@RunWith(AndroidJUnit4.class)
47-
public class ChatBoxTest extends BaseTest {
49+
public class ChatBoxTest {
4850
@Rule
4951
public ActivityTestRule<ZulipActivity> mActivityTestRule = new ActivityTestRule<>(ZulipActivity.class);
5052

53+
public static String LOG_TAG = ChatBoxTest.class.getName();
5154
private static String testMessageStream;
5255
private static String testMessagePrivate;
5356

54-
private static String testStreamName;
55-
private static String testSubjectStream;
56-
private List<Person> testPersons;
5757
private ZulipApp app;
5858

5959
@Before
6060
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-
}
6661
app = ZulipApp.get();
67-
setUpTestStream();
68-
setUpPrivate();
69-
}
7062

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);
8067
}
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+
8174
}
8275

83-
private void setUpTestStream() {
76+
private void sleep(int millis) {
8477
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);
9481
}
9582
}
9683

9784
@Test
9885
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);
10288

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);
10691

10792
//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));
11095

11196
//Click Send Button
11297
ViewInteraction imageView = onView(allOf(withId(R.id.send_btn), isDisplayed()));
11398
imageView.perform(click());
11499

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();
120109
}
121110

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);
128121
}
129-
return TextUtils.join(", ", names);
130122
}
131123

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+
132209
@Test
133210
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);
137213

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);
141216

142217
//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));
145220

146221
//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();
155234
}
156235

157236
@Test
158237
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);
159257
//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());
162260

261+
sleep(500);
163262
//Check if Arrow TextView is not displayed for switchingChatBox
164263
onView(withId(R.id.textView)).check(matches(not(isDisplayed())));
165264
}
166265

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-
195266
public static String getTestMessageStream() {
196267
return testMessageStream;
197268
}

app/src/main/java/com/zulip/android/activities/MessageListFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public interface Listener {
7171
}
7272

7373
private static final String PARAM_FILTER = "filter";
74-
NarrowFilter filter;
74+
public NarrowFilter filter;
7575

7676
private Listener mListener;
7777
private RecyclerView recyclerView;

0 commit comments

Comments
 (0)