diff --git a/Makefile.am b/Makefile.am index 99a9db8f..5b0bc5d8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -62,6 +62,7 @@ test_sources = \ src/config/theme.c src/config/theme.h \ src/ui/windows.c src/ui/windows.h \ src/ui/window.c src/ui/window.h \ + src/server_events.c src/server_events.h \ tests/xmpp/mock_xmpp.h tests/xmpp/mock_xmpp.c \ tests/ui/mock_ui.h tests/ui/mock_ui.c \ tests/config/mock_accounts.h tests/config/mock_accounts.c \ @@ -80,6 +81,7 @@ test_sources = \ tests/test_parser.c \ tests/test_roster_list.c \ tests/test_preferences.c \ + tests/test_server_events.c \ tests/testsuite.c main_source = src/main.c diff --git a/src/config/preferences.c b/src/config/preferences.c index 3a7e489a..3d5c9325 100644 --- a/src/config/preferences.c +++ b/src/config/preferences.c @@ -23,6 +23,7 @@ #include "config.h" #include +#include #include #include @@ -86,6 +87,7 @@ prefs_close(void) { autocomplete_free(boolean_choice_ac); g_key_file_free(prefs); + prefs = NULL; } char * diff --git a/src/server_events.c b/src/server_events.c index d8943885..23e4fce3 100644 --- a/src/server_events.c +++ b/src/server_events.c @@ -315,11 +315,17 @@ handle_contact_online(char *barejid, Resource *resource, { gboolean updated = roster_update_presence(barejid, resource, last_activity); - if (updated && prefs_get_boolean(PREF_STATUSES)) { + if (updated) { + char *show_console = prefs_get_string(PREF_STATUSES_CONSOLE); PContact contact = roster_get_contact(barejid); if (p_contact_subscription(contact) != NULL) { if (strcmp(p_contact_subscription(contact), "none") != 0) { - cons_show_contact_online(contact, resource, last_activity); + if (g_strcmp0(show_console, "all") == 0) { + cons_show_contact_online(contact, resource, last_activity); + } else if (g_strcmp0(show_console, "online") == 0 && + resource->presence == RESOURCE_ONLINE) { + cons_show_contact_online(contact, resource, last_activity); + } ui_chat_win_contact_online(contact, resource, last_activity); } } diff --git a/src/server_events.h b/src/server_events.h index ce3c3b49..64bd04a7 100644 --- a/src/server_events.h +++ b/src/server_events.h @@ -23,6 +23,8 @@ #ifndef SERVER_EVENTS_H #define SERVER_EVENTS_H +#include "xmpp/xmpp.h" + void handle_error_message(const char *from, const char *err_msg); void handle_login_account_success(char *account_name); void handle_lost_connection(void); diff --git a/tests/config/helpers.c b/tests/config/helpers.c index f7e7fdb9..9068546a 100644 --- a/tests/config/helpers.c +++ b/tests/config/helpers.c @@ -1,30 +1,16 @@ -#define _XOPEN_SOURCE 600 -#include #include #include #include -#include #include #include +#include +#include +#include #include "common.h" +#include "config/preferences.h" -static int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) -{ - int rv = remove(fpath); - - if (rv) - perror(fpath); - - return rv; -} - -static int rmrf(char *path) -{ - return nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS); -} - -void create_config_dir(void **state) +void create_config_file(void **state) { setenv("XDG_CONFIG_HOME", "./tests/files/xdg_config_home", 1); gchar *xdg_config = xdg_get_config_home(); @@ -37,10 +23,18 @@ void create_config_dir(void **state) } g_string_free(profanity_dir, TRUE); + fopen("./tests/files/xdg_config_home/profanity/profrc", "ab+"); + g_free(xdg_config); + + prefs_load(); } -void delete_config_dir(void **state) +void delete_config_file(void **state) { - rmrf("./tests/files"); + prefs_close(); + remove("./tests/files/xdg_config_home/profanity/profrc"); + rmdir("./tests/files/xdg_config_home/profanity"); + rmdir("./tests/files/xdg_config_home"); + rmdir("./tests/files"); } diff --git a/tests/config/helpers.h b/tests/config/helpers.h index cd4d5f9d..4f46ad3f 100644 --- a/tests/config/helpers.h +++ b/tests/config/helpers.h @@ -1,2 +1,2 @@ -void create_config_dir(void **state); -void delete_config_dir(void **state); +void create_config_file(void **state); +void delete_config_file(void **state); diff --git a/tests/test_cmd_statuses.c b/tests/test_cmd_statuses.c index 44f5ea1e..04380724 100644 --- a/tests/test_cmd_statuses.c +++ b/tests/test_cmd_statuses.c @@ -81,17 +81,13 @@ void cmd_statuses_console_sets_all(void **state) expect_cons_show("All presence updates will appear in the console."); - prefs_load(); gboolean result = cmd_statuses(args, *help); - prefs_close(); - prefs_load(); char *setting = prefs_get_string(PREF_STATUSES_CONSOLE); assert_non_null(setting); assert_string_equal("all", setting); assert_true(result); - prefs_close(); free(help); } @@ -103,17 +99,13 @@ void cmd_statuses_console_sets_online(void **state) expect_cons_show("Only online/offline presence updates will appear in the console."); - prefs_load(); gboolean result = cmd_statuses(args, *help); - prefs_close(); - prefs_load(); char *setting = prefs_get_string(PREF_STATUSES_CONSOLE); assert_non_null(setting); assert_string_equal("online", setting); assert_true(result); - prefs_close(); free(help); } @@ -125,17 +117,13 @@ void cmd_statuses_console_sets_none(void **state) expect_cons_show("Presence updates will not appear in the console."); - prefs_load(); gboolean result = cmd_statuses(args, *help); - prefs_close(); - prefs_load(); char *setting = prefs_get_string(PREF_STATUSES_CONSOLE); assert_non_null(setting); assert_string_equal("none", setting); assert_true(result); - prefs_close(); free(help); } @@ -147,17 +135,13 @@ void cmd_statuses_chat_sets_all(void **state) expect_cons_show("All presence updates will appear in chat windows."); - prefs_load(); gboolean result = cmd_statuses(args, *help); - prefs_close(); - prefs_load(); char *setting = prefs_get_string(PREF_STATUSES_CHAT); assert_non_null(setting); assert_string_equal("all", setting); assert_true(result); - prefs_close(); free(help); } @@ -169,17 +153,13 @@ void cmd_statuses_chat_sets_online(void **state) expect_cons_show("Only online/offline presence updates will appear in chat windows."); - prefs_load(); gboolean result = cmd_statuses(args, *help); - prefs_close(); - prefs_load(); char *setting = prefs_get_string(PREF_STATUSES_CHAT); assert_non_null(setting); assert_string_equal("online", setting); assert_true(result); - prefs_close(); free(help); } @@ -191,17 +171,13 @@ void cmd_statuses_chat_sets_none(void **state) expect_cons_show("Presence updates will not appear in chat windows."); - prefs_load(); gboolean result = cmd_statuses(args, *help); - prefs_close(); - prefs_load(); char *setting = prefs_get_string(PREF_STATUSES_CHAT); assert_non_null(setting); assert_string_equal("none", setting); assert_true(result); - prefs_close(); free(help); } @@ -213,17 +189,13 @@ void cmd_statuses_muc_sets_on(void **state) expect_cons_show("Chat room presence updates enabled."); - prefs_load(); gboolean result = cmd_statuses(args, *help); - prefs_close(); - prefs_load(); gboolean setting = prefs_get_boolean(PREF_STATUSES_MUC); assert_non_null(setting); assert_true(setting); assert_true(result); - prefs_close(); free(help); } @@ -235,15 +207,11 @@ void cmd_statuses_muc_sets_off(void **state) expect_cons_show("Chat room presence updates disabled."); - prefs_load(); gboolean result = cmd_statuses(args, *help); - prefs_close(); - prefs_load(); gboolean setting = prefs_get_boolean(PREF_STATUSES_MUC); assert_false(setting); assert_true(result); - prefs_close(); free(help); } diff --git a/tests/test_preferences.c b/tests/test_preferences.c index 77aabc6f..41e254cc 100644 --- a/tests/test_preferences.c +++ b/tests/test_preferences.c @@ -10,32 +10,23 @@ void statuses_console_defaults_to_all(void **state) { - prefs_load(); char *setting = prefs_get_string(PREF_STATUSES_CONSOLE); assert_non_null(setting); assert_string_equal("all", setting); - - prefs_close(); } void statuses_chat_defaults_to_all(void **state) { - prefs_load(); char *setting = prefs_get_string(PREF_STATUSES_CHAT); assert_non_null(setting); assert_string_equal("all", setting); - - prefs_close(); } void statuses_muc_defaults_to_on(void **state) { - prefs_load(); gboolean setting = prefs_get_boolean(PREF_STATUSES_MUC); assert_true(setting); - - prefs_close(); } diff --git a/tests/test_server_events.c b/tests/test_server_events.c new file mode 100644 index 00000000..a3f6dc02 --- /dev/null +++ b/tests/test_server_events.c @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "server_events.h" +#include "roster_list.h" +#include "config/preferences.h" +#include "ui/ui.h" +#include "ui/mock_ui.h" + +void console_doesnt_show_online_presence_when_set_none(void **state) +{ + mock_cons_show_contact_online(); + stub_ui_chat_win_contact_online(); + prefs_set_string(PREF_STATUSES_CONSOLE, "none"); + roster_init(); + roster_add("test1@server", "bob", NULL, "both", FALSE); + Resource *resource = resource_new("resource", RESOURCE_ONLINE, NULL, 10, "caps"); + + handle_contact_online("test1@server", resource, NULL); + + roster_clear(); +} + +void console_shows_online_presence_when_set_online(void **state) +{ + mock_cons_show_contact_online(); + stub_ui_chat_win_contact_online(); + prefs_set_string(PREF_STATUSES_CONSOLE, "online"); + roster_init(); + roster_add("test1@server", "bob", NULL, "both", FALSE); + Resource *resource = resource_new("resource", RESOURCE_ONLINE, NULL, 10, "caps"); + PContact contact = roster_get_contact("test1@server"); + + expect_cons_show_contact_online(contact, resource, NULL); + + handle_contact_online("test1@server", resource, NULL); + + roster_clear(); +} + +void console_shows_online_presence_when_set_all(void **state) +{ + mock_cons_show_contact_online(); + stub_ui_chat_win_contact_online(); + prefs_set_string(PREF_STATUSES_CONSOLE, "all"); + roster_init(); + roster_add("test1@server", "bob", NULL, "both", FALSE); + Resource *resource = resource_new("resource", RESOURCE_ONLINE, NULL, 10, "caps"); + PContact contact = roster_get_contact("test1@server"); + + expect_cons_show_contact_online(contact, resource, NULL); + + handle_contact_online("test1@server", resource, NULL); + + roster_clear(); +} + +void console_doesnt_show_dnd_presence_when_set_none(void **state) +{ + mock_cons_show_contact_online(); + stub_ui_chat_win_contact_online(); + prefs_set_string(PREF_STATUSES_CONSOLE, "none"); + roster_init(); + roster_add("test1@server", "bob", NULL, "both", FALSE); + Resource *resource = resource_new("resource", RESOURCE_DND, NULL, 10, "caps"); + + handle_contact_online("test1@server", resource, NULL); + + roster_clear(); +} + +void console_doesnt_show_dnd_presence_when_set_online(void **state) +{ + mock_cons_show_contact_online(); + stub_ui_chat_win_contact_online(); + prefs_set_string(PREF_STATUSES_CONSOLE, "online"); + roster_init(); + roster_add("test1@server", "bob", NULL, "both", FALSE); + Resource *resource = resource_new("resource", RESOURCE_DND, NULL, 10, "caps"); + + handle_contact_online("test1@server", resource, NULL); + + roster_clear(); +} + +void console_shows_dnd_presence_when_set_all(void **state) +{ + mock_cons_show_contact_online(); + stub_ui_chat_win_contact_online(); + prefs_set_string(PREF_STATUSES_CONSOLE, "all"); + roster_init(); + roster_add("test1@server", "bob", NULL, "both", FALSE); + Resource *resource = resource_new("resource", RESOURCE_ONLINE, NULL, 10, "caps"); + PContact contact = roster_get_contact("test1@server"); + + expect_cons_show_contact_online(contact, resource, NULL); + + handle_contact_online("test1@server", resource, NULL); + + roster_clear(); +} diff --git a/tests/test_server_events.h b/tests/test_server_events.h new file mode 100644 index 00000000..b6774456 --- /dev/null +++ b/tests/test_server_events.h @@ -0,0 +1,6 @@ +void console_doesnt_show_online_presence_when_set_none(void **state); +void console_shows_online_presence_when_set_online(void **state); +void console_shows_online_presence_when_set_all(void **state); +void console_doesnt_show_dnd_presence_when_set_none(void **state); +void console_doesnt_show_dnd_presence_when_set_online(void **state); +void console_shows_dnd_presence_when_set_all(void **state); diff --git a/tests/testsuite.c b/tests/testsuite.c index 6f229bcf..8688703d 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -20,6 +20,7 @@ #include "test_parser.h" #include "test_roster_list.h" #include "test_preferences.h" +#include "test_server_events.h" #define PROF_RUN_TESTS(name) fprintf(stderr, "\n-> Running %s\n", #name); \ fflush(stderr); \ @@ -332,50 +333,72 @@ int main(int argc, char* argv[]) { unit_test(cmd_statuses_shows_usage_when_bad_chat_setting), unit_test(cmd_statuses_shows_usage_when_bad_muc_setting), unit_test_setup_teardown(cmd_statuses_console_sets_all, - create_config_dir, - delete_config_dir), + create_config_file, + delete_config_file), unit_test_setup_teardown(cmd_statuses_console_sets_online, - create_config_dir, - delete_config_dir), + create_config_file, + delete_config_file), unit_test_setup_teardown(cmd_statuses_console_sets_none, - create_config_dir, - delete_config_dir), + create_config_file, + delete_config_file), unit_test_setup_teardown(cmd_statuses_chat_sets_all, - create_config_dir, - delete_config_dir), + create_config_file, + delete_config_file), unit_test_setup_teardown(cmd_statuses_chat_sets_online, - create_config_dir, - delete_config_dir), + create_config_file, + delete_config_file), unit_test_setup_teardown(cmd_statuses_chat_sets_none, - create_config_dir, - delete_config_dir), + create_config_file, + delete_config_file), unit_test_setup_teardown(cmd_statuses_muc_sets_on, - create_config_dir, - delete_config_dir), + create_config_file, + delete_config_file), unit_test_setup_teardown(cmd_statuses_muc_sets_off, - create_config_dir, - delete_config_dir), + create_config_file, + delete_config_file), }; const UnitTest preferences_tests[] = { unit_test_setup_teardown(statuses_console_defaults_to_all, - create_config_dir, - delete_config_dir), + create_config_file, + delete_config_file), unit_test_setup_teardown(statuses_chat_defaults_to_all, - create_config_dir, - delete_config_dir), + create_config_file, + delete_config_file), unit_test_setup_teardown(statuses_muc_defaults_to_on, - create_config_dir, - delete_config_dir), + create_config_file, + delete_config_file), }; + const UnitTest server_events_tests[] = { + unit_test_setup_teardown(console_doesnt_show_online_presence_when_set_none, + create_config_file, + delete_config_file), + unit_test_setup_teardown(console_shows_online_presence_when_set_online, + create_config_file, + delete_config_file), + unit_test_setup_teardown(console_shows_online_presence_when_set_all, + create_config_file, + delete_config_file), + unit_test_setup_teardown(console_doesnt_show_dnd_presence_when_set_none, + create_config_file, + delete_config_file), + unit_test_setup_teardown(console_doesnt_show_dnd_presence_when_set_online, + create_config_file, + delete_config_file), + unit_test_setup_teardown(console_shows_dnd_presence_when_set_all, + create_config_file, + delete_config_file), + }; + +/* int bak, new; fflush(stdout); bak = dup(1); new = open("/dev/null", O_WRONLY); dup2(new, 1); close(new); - +*/ int result = 0; PROF_RUN_TESTS(common_tests); @@ -391,11 +414,12 @@ int main(int argc, char* argv[]) { PROF_RUN_TESTS(contact_tests); PROF_RUN_TESTS(cmd_statuses_tests); PROF_RUN_TESTS(preferences_tests); - + PROF_RUN_TESTS(server_events_tests); +/* fflush(stdout); dup2(bak, 1); close(bak); - +*/ if (result > 0) { return 1; } else { diff --git a/tests/ui/mock_ui.c b/tests/ui/mock_ui.c index 83119910..cd6c62ff 100644 --- a/tests/ui/mock_ui.c +++ b/tests/ui/mock_ui.c @@ -44,6 +44,14 @@ void _stub_cons_show(const char * const msg, ...) { } +static +void _mock_cons_show_contact_online(PContact contact, Resource *resource, GDateTime *last_activity) +{ + check_expected(contact); + check_expected(resource); + check_expected(last_activity); +} + static void _mock_cons_show_error(const char * const msg, ...) { @@ -78,6 +86,11 @@ char * _stub_ui_ask_password(void) return NULL; } +static +void _stub_ui_chat_win_contact_online(PContact contact, Resource *resource, GDateTime *last_activity) +{ +} + void mock_cons_show(void) { @@ -85,6 +98,18 @@ mock_cons_show(void) } +void +mock_cons_show_contact_online(void) +{ + cons_show_contact_online = _mock_cons_show_contact_online; +} + +void +stub_ui_chat_win_contact_online(void) +{ + ui_chat_win_contact_online = _stub_ui_chat_win_contact_online; +} + void mock_cons_show_error(void) { @@ -152,6 +177,18 @@ expect_cons_show_account_list(gchar **accounts) expect_memory(_mock_cons_show_account_list, accounts, accounts, sizeof(accounts)); } +void +expect_cons_show_contact_online(PContact contact, Resource *resource, GDateTime *last_activity) +{ + expect_memory(_mock_cons_show_contact_online, contact, contact, sizeof(contact)); + expect_memory(_mock_cons_show_contact_online, resource, resource, sizeof(Resource)); + if (last_activity == NULL) { + expect_any(_mock_cons_show_contact_online, last_activity); + } else { + expect_memory(_mock_cons_show_contact_online, last_activity, last_activity, sizeof(last_activity)); + } +} + void mock_ui_ask_password_returns(char *password) { diff --git a/tests/ui/mock_ui.h b/tests/ui/mock_ui.h index 2833be29..6f6d34d7 100644 --- a/tests/ui/mock_ui.h +++ b/tests/ui/mock_ui.h @@ -11,6 +11,10 @@ void mock_cons_show(void); void expect_cons_show(char *output); void expect_cons_show_calls(int n); +void mock_cons_show_contact_online(void); +void expect_cons_show_contact_online(PContact contact, Resource *resource, GDateTime *last_activity); +void stub_ui_chat_win_contact_online(void); + void mock_cons_show_error(void); void expect_cons_show_error(char *output);