about summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/config/helpers.c36
-rw-r--r--tests/config/helpers.h4
-rw-r--r--tests/test_cmd_statuses.c32
-rw-r--r--tests/test_preferences.c9
-rw-r--r--tests/test_server_events.c106
-rw-r--r--tests/test_server_events.h6
-rw-r--r--tests/testsuite.c74
-rw-r--r--tests/ui/mock_ui.c37
-rw-r--r--tests/ui/mock_ui.h4
9 files changed, 219 insertions, 89 deletions
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 <glib.h>
 #include <setjmp.h>
 #include <stdarg.h>
 #include <stddef.h>
-#include <ftw.h>
 #include <stdlib.h>
 #include <cmocka.h>
+#include <glib.h>
+#include <stdio.h>
+#include <unistd.h>
 
 #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 <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"
+#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
@@ -45,6 +45,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, ...)
 {
     va_list args;
@@ -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)
 {
@@ -86,6 +99,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)
 {
     cons_show_error = _mock_cons_show_error;
@@ -153,6 +178,18 @@ expect_cons_show_account_list(gchar **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)
 {
     will_return(_mock_ui_ask_password, strdup(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);