From f7843def2f307286448fda166b425f48aaa6ddf0 Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 6 Jan 2015 21:22:09 +0000 Subject: [PATCH] Added chat session tests --- .gitignore | 1 + Makefile.am | 1 + tests/helpers.c | 11 ++++ tests/helpers.h | 3 + tests/test_chat_session.c | 125 ++++++++++++++++++++++++++++++++++++++ tests/test_chat_session.h | 11 ++++ tests/testsuite.c | 36 +++++++++++ 7 files changed, 188 insertions(+) create mode 100644 tests/test_chat_session.c create mode 100644 tests/test_chat_session.h diff --git a/.gitignore b/.gitignore index 099c3584..2d7a5c25 100644 --- a/.gitignore +++ b/.gitignore @@ -62,3 +62,4 @@ profanity.mk profanity.project profanity.workspace m4/ +test.sh diff --git a/Makefile.am b/Makefile.am index 520e4c19..4639f8c8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -88,6 +88,7 @@ tests_sources = \ tests/test_roster_list.c tests/test_roster_list.h \ tests/test_server_events.c tests/test_server_events.h \ tests/test_autocomplete.c tests/test_autocomplete.h \ + tests/test_chat_session.c tests/test_chat_session.h \ tests/testsuite.c main_source = src/main.c diff --git a/tests/helpers.c b/tests/helpers.c index a6a473e4..bc0b8009 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,16 @@ void close_preferences(void **state) rmdir("./tests/files"); } +void init_chat_sessions(void **state) +{ + chat_sessions_init(); +} + +void close_chat_sessions(void **state) +{ + chat_sessions_clear(); +} + 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..a93c64b6 --- /dev/null +++ b/tests/test_chat_session.c @@ -0,0 +1,125 @@ +#include +#include +#include +#include +#include +#include + +#include "chat_session.h" + +void returns_false_when_chat_session_does_not_exist(void **state) +{ + gboolean result = chat_session_exists("somejid@server.org"); + assert_false(result); +} + +void creates_chat_session_on_message_send(void **state) +{ + char *barejid = "myjid@server.org"; + + chat_session_on_message_send(barejid); + gboolean exists = chat_session_exists(barejid); + + assert_true(exists); +} + +void creates_chat_session_on_activity(void **state) +{ + char *barejid = "myjid@server.org"; + + chat_session_on_activity(barejid); + gboolean exists = chat_session_exists(barejid); + + assert_true(exists); +} + +void returns_null_resource_for_new_session(void **state) +{ + char *barejid = "myjid@server.org"; + + chat_session_on_message_send(barejid); + char *resource = chat_session_get_resource(barejid); + + assert_null(resource); +} + +void returns_true_send_states_for_new_session(void **state) +{ + char *barejid = "myjid@server.org"; + + chat_session_on_message_send(barejid); + gboolean send_states = chat_session_send_states(barejid); + + assert_true(send_states); +} + +void sets_resource_on_incoming_message(void **state) +{ + char *barejid = "myjid@server.org"; + char *expected_resource = "laptop"; + + chat_session_on_message_send(barejid); + chat_session_on_incoming_message(barejid, expected_resource, FALSE); + char *actual_resource = chat_session_get_resource(barejid); + + assert_string_equal(expected_resource, actual_resource); +} + +void sets_send_states_on_incoming_message(void **state) +{ + char *barejid = "myjid@server.org"; + + chat_session_on_message_send(barejid); + chat_session_on_incoming_message(barejid, "resource", TRUE); + gboolean send_states = chat_session_send_states(barejid); + + assert_true(send_states); +} + +void replaces_chat_session_when_new_resource(void **state) +{ + char *barejid = "myjid@server.org"; + char *first_resource = "laptop"; + char *second_resource = "mobile"; + + chat_session_on_message_send(barejid); + chat_session_on_incoming_message(barejid, first_resource, TRUE); + chat_session_on_incoming_message(barejid, second_resource, TRUE); + char *actual_resource = chat_session_get_resource(barejid); + + assert_string_equal(second_resource, actual_resource); +} + +void removes_chat_session_on_window_close(void **state) +{ + char *barejid = "myjid@server.org"; + + chat_session_on_message_send(barejid); + chat_session_on_window_close(barejid); + gboolean exists = chat_session_exists(barejid); + + assert_false(exists); +} + +void removes_chat_session_on_cancel_for_barejid(void **state) +{ + char *barejid = "myjid@server.org"; + + chat_session_on_message_send(barejid); + chat_session_on_cancel(barejid); + gboolean exists = chat_session_exists(barejid); + + assert_false(exists); +} + +void removes_chat_session_on_cancel_for_fulljid(void **state) +{ + char *barejid = "myjid@server.org"; + char *fulljid = "myjid@server.org/desktop"; + + chat_session_on_message_send(barejid); + chat_session_on_cancel(fulljid); + gboolean exists = chat_session_exists(barejid); + + assert_false(exists); +} \ 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..8916f6b4 --- /dev/null +++ b/tests/test_chat_session.h @@ -0,0 +1,11 @@ +void returns_false_when_chat_session_does_not_exist(void **state); +void creates_chat_session_on_message_send(void **state); +void creates_chat_session_on_activity(void **state); +void returns_null_resource_for_new_session(void **state); +void returns_true_send_states_for_new_session(void **state); +void sets_resource_on_incoming_message(void **state); +void sets_send_states_on_incoming_message(void **state); +void replaces_chat_session_when_new_resource(void **state); +void removes_chat_session_on_window_close(void **state); +void removes_chat_session_on_cancel_for_barejid(void **state); +void removes_chat_session_on_cancel_for_fulljid(void **state); \ No newline at end of file diff --git a/tests/testsuite.c b/tests/testsuite.c index fb439331..db05639d 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -8,9 +8,11 @@ #include #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" @@ -204,6 +206,40 @@ 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_setup_teardown(returns_false_when_chat_session_does_not_exist, + init_chat_sessions, + close_chat_sessions), + unit_test_setup_teardown(creates_chat_session_on_message_send, + init_chat_sessions, + close_chat_sessions), + unit_test_setup_teardown(creates_chat_session_on_activity, + init_chat_sessions, + close_chat_sessions), + unit_test_setup_teardown(returns_null_resource_for_new_session, + init_chat_sessions, + close_chat_sessions), + unit_test_setup_teardown(returns_true_send_states_for_new_session, + init_chat_sessions, + close_chat_sessions), + unit_test_setup_teardown(sets_resource_on_incoming_message, + init_chat_sessions, + close_chat_sessions), + unit_test_setup_teardown(sets_send_states_on_incoming_message, + init_chat_sessions, + close_chat_sessions), + unit_test_setup_teardown(replaces_chat_session_when_new_resource, + init_chat_sessions, + close_chat_sessions), + unit_test_setup_teardown(removes_chat_session_on_window_close, + init_chat_sessions, + close_chat_sessions), + unit_test_setup_teardown(removes_chat_session_on_cancel_for_barejid, + init_chat_sessions, + close_chat_sessions), + unit_test_setup_teardown(removes_chat_session_on_cancel_for_fulljid, + init_chat_sessions, + close_chat_sessions), + unit_test_setup_teardown(cmd_connect_shows_message_when_disconnecting, load_preferences, close_preferences),