about summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-01-06 21:22:09 +0000
committerJames Booth <boothj5@gmail.com>2015-01-06 21:22:09 +0000
commitf7843def2f307286448fda166b425f48aaa6ddf0 (patch)
tree2e91bac7c056a91e88f40c97ca403360afbc3f45 /tests
parent8d759741e8b71f4bdb0dabcc4c6c453074f85960 (diff)
downloadprofani-tty-f7843def2f307286448fda166b425f48aaa6ddf0.tar.gz
Added chat session tests
Diffstat (limited to 'tests')
-rw-r--r--tests/helpers.c11
-rw-r--r--tests/helpers.h3
-rw-r--r--tests/test_chat_session.c125
-rw-r--r--tests/test_chat_session.h11
-rw-r--r--tests/testsuite.c36
5 files changed, 186 insertions, 0 deletions
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 <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)
+{
+    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 <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"
@@ -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),