1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00
profanity/tests/test_server_events.c

213 lines
6.2 KiB
C
Raw Normal View History

2014-01-20 13:40:48 -05:00
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "server_events.h"
#include "roster_list.h"
2014-01-28 18:56:45 -05:00
#include "chat_session.h"
2014-01-20 13:40:48 -05:00
#include "config/preferences.h"
#include "ui/ui.h"
#include "ui/stub_ui.h"
#include "muc.h"
2014-01-20 13:40:48 -05:00
void console_doesnt_show_online_presence_when_set_none(void **state)
{
prefs_set_string(PREF_STATUSES_CONSOLE, "none");
roster_init();
roster_add("test1@server", "bob", NULL, "both", FALSE);
2014-09-20 19:09:17 -04:00
Resource *resource = resource_new("resource", RESOURCE_ONLINE, NULL, 10);
2014-01-20 13:40:48 -05:00
handle_contact_online("test1@server", resource, NULL);
roster_clear();
}
void console_shows_online_presence_when_set_online(void **state)
{
prefs_set_string(PREF_STATUSES_CONSOLE, "online");
roster_init();
roster_add("test1@server", "bob", NULL, "both", FALSE);
2014-09-20 19:09:17 -04:00
Resource *resource = resource_new("resource", RESOURCE_ONLINE, NULL, 10);
2014-01-20 13:40:48 -05:00
PContact contact = roster_get_contact("test1@server");
2014-12-24 19:46:15 -05:00
expect_memory(cons_show_contact_online, contact, contact, sizeof(contact));
expect_memory(cons_show_contact_online, resource, resource, sizeof(resource));
expect_value(cons_show_contact_online, last_activity, NULL);
2014-01-20 13:40:48 -05:00
handle_contact_online("test1@server", resource, NULL);
2014-01-20 13:40:48 -05:00
roster_clear();
}
void console_shows_online_presence_when_set_all(void **state)
{
prefs_set_string(PREF_STATUSES_CONSOLE, "all");
roster_init();
roster_add("test1@server", "bob", NULL, "both", FALSE);
2014-09-20 19:09:17 -04:00
Resource *resource = resource_new("resource", RESOURCE_ONLINE, NULL, 10);
2014-01-20 13:40:48 -05:00
PContact contact = roster_get_contact("test1@server");
2014-12-24 19:46:15 -05:00
expect_memory(cons_show_contact_online, contact, contact, sizeof(contact));
expect_memory(cons_show_contact_online, resource, resource, sizeof(resource));
expect_value(cons_show_contact_online, last_activity, NULL);
2014-01-20 13:40:48 -05:00
handle_contact_online("test1@server", resource, NULL);
2014-01-20 13:40:48 -05:00
roster_clear();
}
void console_doesnt_show_dnd_presence_when_set_none(void **state)
{
prefs_set_string(PREF_STATUSES_CONSOLE, "none");
roster_init();
roster_add("test1@server", "bob", NULL, "both", FALSE);
2014-09-20 19:09:17 -04:00
Resource *resource = resource_new("resource", RESOURCE_DND, NULL, 10);
2014-01-20 13:40:48 -05:00
handle_contact_online("test1@server", resource, NULL);
roster_clear();
}
void console_doesnt_show_dnd_presence_when_set_online(void **state)
{
prefs_set_string(PREF_STATUSES_CONSOLE, "online");
roster_init();
roster_add("test1@server", "bob", NULL, "both", FALSE);
2014-09-20 19:09:17 -04:00
Resource *resource = resource_new("resource", RESOURCE_DND, NULL, 10);
2014-01-20 13:40:48 -05:00
handle_contact_online("test1@server", resource, NULL);
roster_clear();
}
void console_shows_dnd_presence_when_set_all(void **state)
{
prefs_set_string(PREF_STATUSES_CONSOLE, "all");
roster_init();
roster_add("test1@server", "bob", NULL, "both", FALSE);
2014-09-20 19:09:17 -04:00
Resource *resource = resource_new("resource", RESOURCE_ONLINE, NULL, 10);
2014-01-20 13:40:48 -05:00
PContact contact = roster_get_contact("test1@server");
2014-12-24 19:46:15 -05:00
expect_memory(cons_show_contact_online, contact, contact, sizeof(contact));
expect_memory(cons_show_contact_online, resource, resource, sizeof(resource));
expect_value(cons_show_contact_online, last_activity, NULL);
2014-01-20 13:40:48 -05:00
handle_contact_online("test1@server", resource, NULL);
2014-01-20 13:40:48 -05:00
roster_clear();
}
void handle_message_error_when_no_recipient(void **state)
{
char *err_msg = "Some error.";
char *from = NULL;
char *type = "cancel";
2014-01-28 18:56:45 -05:00
2014-12-24 19:46:15 -05:00
expect_string(ui_handle_error, err_msg, err_msg);
handle_message_error(from, type, err_msg);
}
2014-01-28 18:56:45 -05:00
void handle_message_error_when_recipient_cancel(void **state)
2014-01-28 18:56:45 -05:00
{
char *err_msg = "Some error.";
char *from = "bob@server.com";
char *type = "cancel";
2014-01-28 18:56:45 -05:00
prefs_set_boolean(PREF_STATES, FALSE);
chat_sessions_init();
handle_message_error(from, type, err_msg);
2014-01-28 18:56:45 -05:00
}
void handle_message_error_when_recipient_cancel_disables_chat_session(void **state)
2014-01-28 18:56:45 -05:00
{
char *err_msg = "Some error.";
2015-01-09 19:25:27 -05:00
char *barejid = "bob@server.com";
2015-01-04 15:19:42 -05:00
char *resource = "resource";
char *type = "cancel";
2014-01-28 18:56:45 -05:00
prefs_set_boolean(PREF_STATES, TRUE);
chat_sessions_init();
2015-01-09 19:25:27 -05:00
chat_session_on_recipient_activity(barejid, resource);
2014-01-28 18:56:45 -05:00
2015-01-09 19:25:27 -05:00
handle_message_error(barejid, type, err_msg);
ChatSession *session = chat_session_get(barejid);
2014-01-28 18:56:45 -05:00
2015-01-09 19:25:27 -05:00
assert_null(session);
2014-01-28 18:56:45 -05:00
chat_sessions_clear();
}
void handle_message_error_when_recipient_and_no_type(void **state)
{
char *err_msg = "Some error.";
char *from = "bob@server.com";
char *type = NULL;
2014-12-24 19:46:15 -05:00
expect_string(ui_handle_recipient_error, recipient, from);
expect_string(ui_handle_recipient_error, err_msg, err_msg);
handle_message_error(from, type, err_msg);
}
void handle_presence_error_when_no_recipient(void **state)
{
char *err_msg = "Some error.";
char *from = NULL;
char *type = NULL;
2014-12-24 19:46:15 -05:00
expect_string(ui_handle_error, err_msg, err_msg);
handle_presence_error(from, type, err_msg);
}
void handle_presence_error_when_from_recipient(void **state)
2014-01-28 18:56:45 -05:00
{
char *err_msg = "Some error.";
char *from = "bob@server.com";
char *type = NULL;
2014-01-28 18:56:45 -05:00
2014-12-24 19:46:15 -05:00
expect_string(ui_handle_recipient_error, recipient, from);
expect_string(ui_handle_recipient_error, err_msg, err_msg);
2014-01-28 18:56:45 -05:00
handle_presence_error(from, type, err_msg);
2014-01-28 18:56:45 -05:00
}
void handle_offline_removes_chat_session(void **state)
{
chat_sessions_init();
char *barejid = "friend@server.chat.com";
char *resource = "home";
roster_init();
roster_add(barejid, "bob", NULL, "both", FALSE);
Resource *resourcep = resource_new(resource, RESOURCE_ONLINE, NULL, 10);
roster_update_presence(barejid, resourcep, NULL);
2015-01-09 19:25:27 -05:00
chat_session_on_recipient_activity(barejid, resource);
handle_contact_offline(barejid, resource, NULL);
2015-01-09 19:25:27 -05:00
ChatSession *session = chat_session_get(barejid);
2015-01-09 19:25:27 -05:00
assert_null(session);
roster_clear();
chat_sessions_clear();
}
void lost_connection_clears_chat_sessions(void **state)
{
chat_sessions_init();
chat_session_on_recipient_activity("bob@server.org", "laptop");
chat_session_on_recipient_activity("steve@server.org", "mobile");
expect_any_cons_show_error();
handle_lost_connection();
ChatSession *session1 = chat_session_get("bob@server.org");
ChatSession *session2 = chat_session_get("steve@server.org");
assert_null(session1);
assert_null(session2);
}