1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

WIP - /statuses command options

This commit is contained in:
James Booth 2014-01-20 18:40:48 +00:00
parent 3f0addbc99
commit a7a2850637
13 changed files with 233 additions and 91 deletions

View File

@ -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

View File

@ -23,6 +23,7 @@
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
@ -86,6 +87,7 @@ prefs_close(void)
{
autocomplete_free(boolean_choice_ac);
g_key_file_free(prefs);
prefs = NULL;
}
char *

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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");
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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();
}

106
tests/test_server_events.c Normal file
View File

@ -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();
}

View File

@ -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);

View File

@ -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 {

View File

@ -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)
{

View File

@ -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);