about summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/helpers.c13
-rw-r--r--tests/helpers.h3
-rw-r--r--tests/test_chat_session.c51
-rw-r--r--tests/test_chat_session.h4
-rw-r--r--tests/test_cmd_disconnect.c37
-rw-r--r--tests/test_cmd_disconnect.h1
-rw-r--r--tests/test_cmd_otr.c2
-rw-r--r--tests/test_server_events.c52
-rw-r--r--tests/test_server_events.h4
-rw-r--r--tests/testsuite.c123
-rw-r--r--tests/ui/stub_ui.c26
-rw-r--r--tests/ui/stub_ui.h2
-rw-r--r--tests/xmpp/stub_xmpp.c9
13 files changed, 277 insertions, 50 deletions
diff --git a/tests/helpers.c b/tests/helpers.c
index a6a473e4..10310886 100644
--- a/tests/helpers.c
+++ b/tests/helpers.c
@@ -10,6 +10,7 @@
 #include "common.h"
 #include "helpers.h"
 #include "config/preferences.h"
+#include "chat_session.h"
 
 void create_config_dir(void **state)
 {
@@ -72,6 +73,18 @@ void close_preferences(void **state)
     rmdir("./tests/files");
 }
 
+void init_chat_sessions(void **state)
+{
+    load_preferences(NULL);
+    chat_sessions_init();
+}
+
+void close_chat_sessions(void **state)
+{
+    chat_sessions_clear();
+    close_preferences(NULL);
+}
+
 static GCompareFunc cmp_func;
 
 void
diff --git a/tests/helpers.h b/tests/helpers.h
index 17d8329c..2d7af6e7 100644
--- a/tests/helpers.h
+++ b/tests/helpers.h
@@ -3,5 +3,8 @@
 void load_preferences(void **state);
 void close_preferences(void **state);
 
+void init_chat_sessions(void **state);
+void close_chat_sessions(void **state);
+
 void glist_set_cmp(GCompareFunc func);
 int glist_contents_equal(const void *actual, const void *expected);
\ No newline at end of file
diff --git a/tests/test_chat_session.c b/tests/test_chat_session.c
new file mode 100644
index 00000000..b5e1f7b6
--- /dev/null
+++ b/tests/test_chat_session.c
@@ -0,0 +1,51 @@
+#include <stdarg.h>
+#include <string.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+#include <stdlib.h>
+
+#include "chat_session.h"
+
+void returns_false_when_chat_session_does_not_exist(void **state)
+{
+    ChatSession *session = chat_session_get("somejid@server.org");
+    assert_null(session);
+}
+
+void creates_chat_session_on_recipient_activity(void **state)
+{
+    char *barejid = "myjid@server.org";
+    char *resource = "tablet";
+
+    chat_session_recipient_active(barejid, resource, FALSE);
+    ChatSession *session = chat_session_get(barejid);
+
+    assert_non_null(session);
+    assert_string_equal(session->resource, resource);
+}
+
+void replaces_chat_session_on_recipient_activity_with_different_resource(void **state)
+{
+    char *barejid = "myjid@server.org";
+    char *resource1 = "tablet";
+    char *resource2 = "mobile";
+
+    chat_session_recipient_active(barejid, resource1, FALSE);
+    chat_session_recipient_active(barejid, resource2, FALSE);
+    ChatSession *session = chat_session_get(barejid);
+
+    assert_string_equal(session->resource, resource2);
+}
+
+void removes_chat_session(void **state)
+{
+    char *barejid = "myjid@server.org";
+    char *resource1 = "laptop";
+
+    chat_session_recipient_active(barejid, resource1, FALSE);
+    chat_session_remove(barejid);
+    ChatSession *session = chat_session_get(barejid);
+
+    assert_null(session);
+}
\ No newline at end of file
diff --git a/tests/test_chat_session.h b/tests/test_chat_session.h
new file mode 100644
index 00000000..4ce03fd5
--- /dev/null
+++ b/tests/test_chat_session.h
@@ -0,0 +1,4 @@
+void returns_false_when_chat_session_does_not_exist(void **state);
+void creates_chat_session_on_recipient_activity(void **state);
+void replaces_chat_session_on_recipient_activity_with_different_resource(void **state);
+void removes_chat_session(void **state);
\ No newline at end of file
diff --git a/tests/test_cmd_disconnect.c b/tests/test_cmd_disconnect.c
new file mode 100644
index 00000000..68253820
--- /dev/null
+++ b/tests/test_cmd_disconnect.c
@@ -0,0 +1,37 @@
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "chat_session.h"
+#include "command/commands.h"
+#include "xmpp/xmpp.h"
+#include "roster_list.h"
+
+#include "ui/stub_ui.h"
+
+void clears_chat_sessions(void **state)
+{
+    CommandHelp *help = malloc(sizeof(CommandHelp));
+
+    chat_sessions_init();
+    roster_init();
+    chat_session_recipient_active("bob@server.org", "laptop", FALSE);
+    chat_session_recipient_active("mike@server.org", "work", FALSE);
+
+    will_return(jabber_get_connection_status, JABBER_CONNECTED);
+    will_return(jabber_get_fulljid, "myjid@myserver.com");
+    expect_any_cons_show();
+
+    gboolean result = cmd_disconnect(NULL, *help);
+
+    assert_true(result);
+
+    ChatSession *session1 = chat_session_get("bob@server.org");
+    ChatSession *session2 = chat_session_get("mike@server.org");
+    assert_null(session1);
+    assert_null(session2);
+    free(help);
+}
\ No newline at end of file
diff --git a/tests/test_cmd_disconnect.h b/tests/test_cmd_disconnect.h
new file mode 100644
index 00000000..856b501e
--- /dev/null
+++ b/tests/test_cmd_disconnect.h
@@ -0,0 +1 @@
+void clears_chat_sessions(void **state);
diff --git a/tests/test_cmd_otr.c b/tests/test_cmd_otr.c
index 3cd9dd59..7d0adc1a 100644
--- a/tests/test_cmd_otr.c
+++ b/tests/test_cmd_otr.c
@@ -552,9 +552,7 @@ cmd_otr_start_sends_otr_query_message_to_current_recipeint(void **state)
     will_return(otr_start_query, query_message);
 
     expect_string(message_send_chat, barejid, chatwin->barejid);
-    expect_value(message_send_chat, resource, NULL);
     expect_string(message_send_chat, msg, query_message);
-    expect_any(message_send_chat, send_state);
 
     gboolean result = cmd_otr(args, *help);
     assert_true(result);
diff --git a/tests/test_server_events.c b/tests/test_server_events.c
index 435493a2..ce54b4f2 100644
--- a/tests/test_server_events.c
+++ b/tests/test_server_events.c
@@ -11,6 +11,7 @@
 #include "chat_session.h"
 #include "config/preferences.h"
 #include "ui/ui.h"
+#include "ui/stub_ui.h"
 #include "muc.h"
 
 void console_doesnt_show_online_presence_when_set_none(void **state)
@@ -120,29 +121,24 @@ void handle_message_error_when_recipient_cancel(void **state)
     prefs_set_boolean(PREF_STATES, FALSE);
     chat_sessions_init();
 
-    expect_string(ui_handle_recipient_not_found, recipient, from);
-    expect_string(ui_handle_recipient_not_found, err_msg, err_msg);
-
     handle_message_error(from, type, err_msg);
 }
 
 void handle_message_error_when_recipient_cancel_disables_chat_session(void **state)
 {
     char *err_msg = "Some error.";
-    char *from = "bob@server.com";
+    char *barejid = "bob@server.com";
+    char *resource = "resource";
     char *type = "cancel";
 
     prefs_set_boolean(PREF_STATES, TRUE);
     chat_sessions_init();
-    chat_session_on_incoming_message(from, TRUE);
+    chat_session_recipient_active(barejid, resource, FALSE);
 
-    expect_any(ui_handle_recipient_not_found, recipient);
-    expect_any(ui_handle_recipient_not_found, err_msg);
+    handle_message_error(barejid, type, err_msg);
+    ChatSession *session = chat_session_get(barejid);
 
-    handle_message_error(from, type, err_msg);
-    gboolean chat_session_supported = chat_session_on_message_send(from);
-
-    assert_false(chat_session_supported);
+    assert_null(session);
     chat_sessions_clear();
 }
 
@@ -180,3 +176,37 @@ void handle_presence_error_when_from_recipient(void **state)
 
     handle_presence_error(from, type, err_msg);
 }
+
+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);
+    chat_session_recipient_active(barejid, resource, FALSE);
+    handle_contact_offline(barejid, resource, NULL);
+    ChatSession *session = chat_session_get(barejid);
+
+    assert_null(session);
+
+    roster_clear();
+    chat_sessions_clear();
+}
+
+void lost_connection_clears_chat_sessions(void **state)
+{
+    chat_sessions_init();
+    chat_session_recipient_active("bob@server.org", "laptop", FALSE);
+    chat_session_recipient_active("steve@server.org", "mobile", FALSE);
+    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);
+}
diff --git a/tests/test_server_events.h b/tests/test_server_events.h
index 68e78557..81a436f4 100644
--- a/tests/test_server_events.h
+++ b/tests/test_server_events.h
@@ -9,4 +9,6 @@ void handle_message_error_when_recipient_cancel(void **state);
 void handle_message_error_when_recipient_cancel_disables_chat_session(void **state);
 void handle_message_error_when_recipient_and_no_type(void **state);
 void handle_presence_error_when_no_recipient(void **state);
-void handle_presence_error_when_from_recipient(void **state);
\ No newline at end of file
+void handle_presence_error_when_from_recipient(void **state);
+void handle_offline_removes_chat_session(void **state);
+void lost_connection_clears_chat_sessions(void **state);
diff --git a/tests/testsuite.c b/tests/testsuite.c
index 67279311..eb039cbe 100644
--- a/tests/testsuite.c
+++ b/tests/testsuite.c
@@ -8,9 +8,11 @@
 #include <sys/stat.h>
 
 #include "config.h"
+#include "chat_session.h"
 
 #include "helpers.h"
 #include "test_autocomplete.h"
+#include "test_chat_session.h"
 #include "test_common.h"
 #include "test_contact.h"
 #include "test_cmd_connect.h"
@@ -31,6 +33,7 @@
 #include "test_muc.h"
 #include "test_cmd_roster.h"
 #include "test_cmd_win.h"
+#include "test_cmd_disconnect.h"
 #include "test_form.h"
 
 int main(int argc, char* argv[]) {
@@ -204,31 +207,93 @@ int main(int argc, char* argv[]) {
         unit_test(find_five_times_finds_fifth),
         unit_test(find_twice_returns_first_when_two_match_and_reset),
 
-        unit_test(cmd_connect_shows_message_when_disconnecting),
-        unit_test(cmd_connect_shows_message_when_connecting),
-        unit_test(cmd_connect_shows_message_when_connected),
-        unit_test(cmd_connect_shows_message_when_undefined),
-        unit_test(cmd_connect_when_no_account),
-        unit_test(cmd_connect_fail_message),
-        unit_test(cmd_connect_lowercases_argument),
-        unit_test(cmd_connect_asks_password_when_not_in_account),
-        unit_test(cmd_connect_shows_message_when_connecting_with_account),
-        unit_test(cmd_connect_connects_with_account),
-        unit_test(cmd_connect_shows_usage_when_no_server_value),
-        unit_test(cmd_connect_shows_usage_when_server_no_port_value),
-        unit_test(cmd_connect_shows_usage_when_no_port_value),
-        unit_test(cmd_connect_shows_usage_when_port_no_server_value),
-        unit_test(cmd_connect_shows_message_when_port_0),
-        unit_test(cmd_connect_shows_message_when_port_minus1),
-        unit_test(cmd_connect_shows_message_when_port_65536),
-        unit_test(cmd_connect_shows_message_when_port_contains_chars),
-        unit_test(cmd_connect_with_server_when_provided),
-        unit_test(cmd_connect_with_port_when_provided),
-        unit_test(cmd_connect_with_server_and_port_when_provided),
-        unit_test(cmd_connect_shows_usage_when_server_provided_twice),
-        unit_test(cmd_connect_shows_usage_when_port_provided_twice),
-        unit_test(cmd_connect_shows_usage_when_invalid_first_property),
-        unit_test(cmd_connect_shows_usage_when_invalid_second_property),
+        unit_test_setup_teardown(returns_false_when_chat_session_does_not_exist,
+            init_chat_sessions,
+            close_chat_sessions),
+        unit_test_setup_teardown(creates_chat_session_on_recipient_activity,
+            init_chat_sessions,
+            close_chat_sessions),
+        unit_test_setup_teardown(replaces_chat_session_on_recipient_activity_with_different_resource,
+            init_chat_sessions,
+            close_chat_sessions),
+        unit_test_setup_teardown(removes_chat_session,
+            init_chat_sessions,
+            close_chat_sessions),
+        unit_test_setup_teardown(cmd_connect_shows_message_when_disconnecting,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_shows_message_when_connecting,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_shows_message_when_connected,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_shows_message_when_undefined,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_when_no_account,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_fail_message,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_lowercases_argument,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_asks_password_when_not_in_account,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_shows_message_when_connecting_with_account,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_connects_with_account,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_shows_usage_when_no_server_value,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_shows_usage_when_server_no_port_value,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_shows_usage_when_no_port_value,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_shows_usage_when_port_no_server_value,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_shows_message_when_port_0,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_shows_message_when_port_minus1,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_shows_message_when_port_65536,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_shows_message_when_port_contains_chars,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_with_server_when_provided,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_with_port_when_provided,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_with_server_and_port_when_provided,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_shows_usage_when_server_provided_twice,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_shows_usage_when_port_provided_twice,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_shows_usage_when_invalid_first_property,
+            load_preferences,
+            close_preferences),
+        unit_test_setup_teardown(cmd_connect_shows_usage_when_invalid_second_property,
+            load_preferences,
+            close_preferences),
 
         unit_test(cmd_rooms_shows_message_when_disconnected),
         unit_test(cmd_rooms_shows_message_when_disconnecting),
@@ -389,6 +454,8 @@ int main(int argc, char* argv[]) {
         unit_test(handle_message_error_when_recipient_and_no_type),
         unit_test(handle_presence_error_when_no_recipient),
         unit_test(handle_presence_error_when_from_recipient),
+        unit_test(handle_offline_removes_chat_session),
+        unit_test(lost_connection_clears_chat_sessions),
 
         unit_test(cmd_alias_add_shows_usage_when_no_args),
         unit_test(cmd_alias_add_shows_usage_when_no_value),
@@ -485,7 +552,9 @@ int main(int argc, char* argv[]) {
         unit_test(cmd_otr_start_shows_message_when_in_private),
         unit_test(cmd_otr_start_shows_message_when_already_started),
         unit_test(cmd_otr_start_shows_message_when_no_key),
-        unit_test(cmd_otr_start_sends_otr_query_message_to_current_recipeint),
+        unit_test_setup_teardown(cmd_otr_start_sends_otr_query_message_to_current_recipeint,
+            load_preferences,
+            close_preferences),
 #else
         unit_test(cmd_otr_shows_message_when_otr_unsupported),
 #endif
@@ -542,6 +611,8 @@ int main(int argc, char* argv[]) {
         unit_test(remove_text_multi_value_does_nothing_when_doesnt_exist),
         unit_test(remove_text_multi_value_removes_when_one),
         unit_test(remove_text_multi_value_removes_when_many),
+
+        unit_test(clears_chat_sessions),
     };
 
     return run_tests(all_tests);
diff --git a/tests/ui/stub_ui.c b/tests/ui/stub_ui.c
index 726ff81d..ef0778d0 100644
--- a/tests/ui/stub_ui.c
+++ b/tests/ui/stub_ui.c
@@ -20,12 +20,24 @@ expect_cons_show(char *expected)
 }
 
 void
+expect_any_cons_show(void)
+{
+    expect_any(cons_show, output);
+}
+
+void
 expect_cons_show_error(char *expected)
 {
     expect_string(cons_show_error, output, expected);
 }
 
 void
+expect_any_cons_show_error(void)
+{
+    expect_any(cons_show_error, output);
+}
+
+void
 expect_ui_current_print_line(char *message)
 {
     expect_string(ui_current_print_line, output, message);
@@ -176,12 +188,12 @@ char * ui_ask_password(void)
 void ui_handle_stanza(const char * const msg) {}
 
 // ui events
-void ui_contact_typing(const char * const from) {}
-void ui_incoming_msg(const char * const from, const char * const message, GTimeVal *tv_stamp) {}
+void ui_contact_typing(const char * const barejid, const char * const resource) {}
+void ui_incoming_msg(const char * const from, const char * const resource, const char * const message, GTimeVal *tv_stamp) {}
 void ui_incoming_private_msg(const char * const fulljid, const char * const message, GTimeVal *tv_stamp) {}
 
 void ui_disconnected(void) {}
-void ui_recipient_gone(const char * const barejid) {}
+void ui_recipient_gone(const char * const barejid, const char * const resource) {}
 
 void ui_outgoing_chat_msg(const char * const from, const char * const barejid,
     const char * const message) {}
@@ -247,6 +259,12 @@ void ui_group_added(const char * const contact, const char * const group) {}
 void ui_group_removed(const char * const contact, const char * const group) {}
 void ui_chat_win_contact_online(PContact contact, Resource *resource, GDateTime *last_activity) {}
 void ui_chat_win_contact_offline(PContact contact, char *resource, char *status) {}
+gboolean ui_chat_win_exists(const char * const barejid)
+{
+    return TRUE;
+}
+
+void ui_contact_offline(char *barejid, char *resource, char *status) {}
 
 void ui_handle_recipient_not_found(const char * const recipient, const char * const err_msg)
 {
@@ -425,6 +443,7 @@ void cons_beep_setting(void) {}
 void cons_flash_setting(void) {}
 void cons_splash_setting(void) {}
 void cons_vercheck_setting(void) {}
+void cons_resource_setting(void) {}
 void cons_occupants_setting(void) {}
 void cons_roster_setting(void) {}
 void cons_presence_setting(void) {}
@@ -433,7 +452,6 @@ void cons_time_setting(void) {}
 void cons_mouse_setting(void) {}
 void cons_statuses_setting(void) {}
 void cons_titlebar_setting(void) {}
-void cons_exittitle_setting(void) {}
 void cons_notify_setting(void) {}
 void cons_states_setting(void) {}
 void cons_outtype_setting(void) {}
diff --git a/tests/ui/stub_ui.h b/tests/ui/stub_ui.h
index f64eba02..81357a86 100644
--- a/tests/ui/stub_ui.h
+++ b/tests/ui/stub_ui.h
@@ -1,4 +1,6 @@
 void expect_cons_show(char *expected);
+void expect_any_cons_show(void);
 void expect_cons_show_error(char *expected);
+void expect_any_cons_show_error(void);
 void expect_ui_current_print_line(char *message);
 void expect_ui_current_print_formatted_line(char show_char, int attrs, char *message);
\ No newline at end of file
diff --git a/tests/xmpp/stub_xmpp.c b/tests/xmpp/stub_xmpp.c
index 580a6c61..cc5ad5fc 100644
--- a/tests/xmpp/stub_xmpp.c
+++ b/tests/xmpp/stub_xmpp.c
@@ -29,7 +29,7 @@ void jabber_shutdown(void) {}
 void jabber_process_events(void) {}
 const char * jabber_get_fulljid(void)
 {
-    return NULL;
+    return (char *)mock();
 }
 
 const char * jabber_get_domain(void)
@@ -58,13 +58,10 @@ GList * jabber_get_available_resources(void)
 }
 
 // message functions
-void message_send_chat(const char * const barejid, const char * const resource, const char * const msg,
-    gboolean send_state)
+void message_send_chat(const char * const barejid, const char * const msg)
 {
     check_expected(barejid);
-    check_expected(resource);
     check_expected(msg);
-    check_expected(send_state);
 }
 
 void message_send_private(const char * const fulljid, const char * const msg) {}
@@ -218,4 +215,4 @@ void roster_send_add_new(const char * const barejid, const char * const name)
 void roster_send_remove(const char * const barejid)
 {
     check_expected(barejid);
-}
\ No newline at end of file
+}