use of com.fanap.podchat.chat.ChatListener in project pod-chat-android-sdk by FanapSoft.
the class MessagesCacheTest method testIfParticipantsAreSame.
@Test
public void testIfParticipantsAreSame() {
populateMessagesFromServer();
assert threadMessagesList.size() > 0;
Collections.shuffle(threadMessagesList);
MessageVO message = threadMessagesList.get(0);
assert message.getConversation() != null;
Thread thread = message.getConversation();
chat.setListener(new ChatListener() {
@Override
public void onNewMessage(String content, ChatResponse<ResultNewMessage> response) {
resumeProcess();
}
});
RequestMessage repRequest = new RequestMessage.Builder("Message at " + new Date(), thread.getId()).messageType(TextMessageType.Constants.TEXT).build();
chat.sendTextMessage(repRequest, null);
pauseProcess();
// Load Server messages
ArrayList<MessageVO> msgFromServer = new ArrayList<>();
chatListeners = new ChatListener() {
@Override
public void onGetHistory(String content, ChatResponse<ResultHistory> history) {
if (!history.isCache()) {
System.out.println("Received Message List Server: " + content);
msgFromServer.addAll(history.getResult().getHistory());
chat.removeListener(chatListeners);
resumeProcess();
}
}
};
chat.addListener(chatListeners);
RequestGetHistory requestGetHistory = new RequestGetHistory.Builder(thread.getId()).withNoCache().build();
chat.getHistory(requestGetHistory, null);
pauseProcess();
// Load cache
ArrayList<MessageVO> msgFromCache = new ArrayList<>();
chatListeners = new ChatListener() {
@Override
public void onGetHistory(String content, ChatResponse<ResultHistory> history) {
if (history.isCache()) {
System.out.println("Received Message List Cache: " + content);
msgFromCache.addAll(history.getResult().getHistory());
chat.removeListener(chatListeners);
resumeProcess();
}
}
};
chat.addListener(chatListeners);
RequestGetHistory requestGetHistoryCache = new RequestGetHistory.Builder(thread.getId()).build();
chat.getHistory(requestGetHistoryCache, null);
pauseProcess();
assert msgFromServer.size() > 0;
assert msgFromCache.size() > 0;
assert msgFromCache.size() == msgFromServer.size();
for (MessageVO msg : msgFromServer) {
if (msg.getMessage() == null)
continue;
System.out.println(">>>>>>>>>>> Server Message " + msg.getMessage() + " Conversation: " + msg.getConversation());
MessageVO msgInCache = msgFromCache.stream().filter(messageVO -> messageVO.getId() == msg.getId()).findFirst().get();
System.out.println(">>>>>>>>>>> Cache Message " + msgInCache.getMessage() + " RepInfo: " + msgInCache.getConversation());
Assert.assertEquals(msg.getParticipant() != null, msgInCache.getParticipant() != null);
if (msg.getParticipant() != null) {
Assert.assertEquals(msg.getParticipant().getId(), msgInCache.getParticipant().getId());
Assert.assertEquals(msg.getParticipant().getName(), msgInCache.getParticipant().getName());
Assert.assertEquals(msg.getParticipant().getChatProfileVO() != null, msgInCache.getParticipant().getChatProfileVO() != null);
}
}
}
use of com.fanap.podchat.chat.ChatListener in project pod-chat-android-sdk by FanapSoft.
the class MessagesCacheTest method sendReplyMessageAndCheckCache.
@Test
public void sendReplyMessageAndCheckCache() {
populateThreadsListFromServerOnly();
populateMessagesFromServer();
assert threads.size() > 0;
assert threadMessagesList.size() > 0;
Thread thread = threads.get(0);
MessageVO message = threadMessagesList.get(0);
ChatListener mTestListener = Mockito.mock(ChatListener.class);
chat.setListener(mTestListener);
String replyTXT = "Reply to " + message.getMessage();
RequestReplyMessage repRequest = new RequestReplyMessage.Builder(replyTXT, thread.getId(), message.getId(), TextMessageType.Constants.TEXT).build();
chat.replyMessage(repRequest, null);
Mockito.verify(mTestListener, Mockito.after(2000).atLeastOnce()).onNewMessage(Mockito.any(), Mockito.any());
RequestGetHistory requestGetHistory = new RequestGetHistory.Builder(thread.getId()).build();
chat.getHistory(requestGetHistory, null);
Mockito.verify(mTestListener, Mockito.after(2000).atLeastOnce()).onGetHistory(Mockito.any(), Mockito.argThat((ChatResponse<ResultHistory> response) -> response.isCache() && response.getResult().getHistory().stream().anyMatch(messageVO -> messageVO.getMessage().equals(replyTXT))));
}
use of com.fanap.podchat.chat.ChatListener in project pod-chat-android-sdk by FanapSoft.
the class MessagesCacheTest method createChat.
@Before
public void createChat() {
view = Mockito.mock(ChatContract.view.class);
chatActivity = chatActivityRule.getActivity();
presenter = new ChatPresenter(appContext, view, chatActivity);
RequestConnect rc = new RequestConnect.Builder(socketAddress, APP_ID, serverName, TOKEN, ssoHost, platformHost, fileServer, "podSpaceServer").build();
chatListeners = new ChatListener() {
@Override
public void onChatState(String state) {
if (state.equals(CHAT_READY)) {
resumeProcess();
}
}
};
chat.addListener(chatListeners);
chat.connect(rc);
chat.isCacheables(CACHE);
pauseProcess();
}
use of com.fanap.podchat.chat.ChatListener in project pod-chat-android-sdk by FanapSoft.
the class MessagesCacheTest method populateThreadsListFromServerOnly.
// requests for list of threads from server
public void populateThreadsListFromServerOnly() {
chatListeners = new ChatListener() {
@Override
public void onGetThread(String content, ChatResponse<ResultThreads> response) {
if (!response.isCache()) {
System.out.println("Received List: " + content);
threads.addAll(response.getResult().getThreads().stream().filter(thread -> thread.getTitle() != null && thread.getId() > 0 && !thread.isClosed() && thread.getLastMessageVO() != null).collect(Collectors.toList()));
chat.removeListener(chatListeners);
resumeProcess();
}
}
};
chat.setListener(chatListeners);
RequestThread requestThread = new RequestThread.Builder().count(25).withNoCache().build();
presenter.getThreads(requestThread, null);
pauseProcess();
System.out.println("Received List: " + threads.size());
}
use of com.fanap.podchat.chat.ChatListener in project pod-chat-android-sdk by FanapSoft.
the class MessagesCacheTest method populateMessagesFromServerOrCache.
public void populateMessagesFromServerOrCache() {
populateThreadsListFromServerOnly();
assert threads.size() > 0;
Thread thread = threads.get(0);
chatListeners = new ChatListener() {
@Override
public void onGetHistory(String content, ChatResponse<ResultHistory> history) {
System.out.println("Received Message List, Cache: " + history.isCache() + " Content: " + content);
threadMessagesList.addAll(history.getResult().getHistory());
chat.removeListener(chatListeners);
resumeProcess();
}
};
chat.addListener(chatListeners);
RequestGetHistory requestGetHistory = new RequestGetHistory.Builder(thread.getId()).build();
chat.getHistory(requestGetHistory, null);
pauseProcess();
}
Aggregations