From 6d6bc67d5c70f4e43dd1eb543f58767de119ea8a Mon Sep 17 00:00:00 2001 From: James Booth Date: Fri, 27 Dec 2013 14:17:24 +0000 Subject: [PATCH] Test for presence update when changing priority --- src/command/commands.c | 3 ++- tests/config/mock_accounts.c | 21 +++++++++++++++++++++ tests/config/mock_accounts.h | 3 +++ tests/test_cmd_account.c | 25 ++++++++++++++++++++++++- tests/test_cmd_account.h | 1 + tests/testsuite.c | 1 + tests/xmpp/mock_xmpp.c | 35 +++++++++++++++++++++++++++++++++++ tests/xmpp/mock_xmpp.h | 4 ++++ 8 files changed, 91 insertions(+), 2 deletions(-) diff --git a/src/command/commands.c b/src/command/commands.c index 1797c7ed..c5b253e7 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -266,7 +266,8 @@ cmd_account(gchar **args, struct cmd_help_t help) resource_presence_t last_presence = accounts_get_last_presence(connected_account); if (presence_type == last_presence) { - presence_update(last_presence, jabber_get_presence_message(), 0); + char *message = jabber_get_presence_message(); + presence_update(last_presence, message, 0); } } cons_show("Updated %s priority for account %s: %s", property, account_name, value); diff --git a/tests/config/mock_accounts.c b/tests/config/mock_accounts.c index 893a0449..db2e0fd5 100644 --- a/tests/config/mock_accounts.c +++ b/tests/config/mock_accounts.c @@ -256,6 +256,14 @@ _mock_accounts_set_login_presence(const char * const account_name, const char * static void _stub_accounts_set_login_presence(const char * const account_name, const char * const value) { + // do nothing +} + +static resource_presence_t +_mock_accounts_get_last_presence(const char * const account_name) +{ + check_expected(account_name); + return (resource_presence_t)mock(); } // set up functions @@ -430,6 +438,12 @@ stub_accounts_set_login_presence(void) accounts_set_login_presence = _stub_accounts_set_login_presence; } +void +mock_accounts_get_last_presence(void) +{ + accounts_get_last_presence = _mock_accounts_get_last_presence; +} + // mock behaviours void @@ -617,3 +631,10 @@ accounts_set_login_presence_expect(char *account_name, char *presence) expect_string(_mock_accounts_set_login_presence, account_name, account_name); expect_string(_mock_accounts_set_login_presence, value, presence); } + +void +accounts_get_last_presence_return(resource_presence_t presence) +{ + expect_any(_mock_accounts_get_last_presence, account_name); + will_return(_mock_accounts_get_last_presence, presence); +} diff --git a/tests/config/mock_accounts.h b/tests/config/mock_accounts.h index 38c6ca94..b914968a 100644 --- a/tests/config/mock_accounts.h +++ b/tests/config/mock_accounts.h @@ -89,3 +89,6 @@ void accounts_set_priority_dnd_expect(char *account_name, gint priority); void mock_accounts_set_login_presence(void); void stub_accounts_set_login_presence(void); void accounts_set_login_presence_expect(char *account_name, char *presence); + +void mock_accounts_get_last_presence(void); +void accounts_get_last_presence_return(resource_presence_t presence); diff --git a/tests/test_cmd_account.c b/tests/test_cmd_account.c index da1bc0f5..c924c6ee 100644 --- a/tests/test_cmd_account.c +++ b/tests/test_cmd_account.c @@ -1032,5 +1032,28 @@ void cmd_account_set_priority_when_empty_shows_message(void **state) free(help); } -// test presence updated when connected as account and current presence equals setting +void cmd_account_set_priority_updates_presence_when_account_connected_with_presence(void **state) +{ + stub_cons_show(); + stub_accounts_set_priorities(); + mock_accounts_get_last_presence(); + mock_presence_update(); + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "set", "a_account", "online", "10", NULL }; + accounts_account_exists_return(TRUE); + + mock_connection_status(JABBER_CONNECTED); + mock_connection_account_name("a_account"); + + accounts_get_last_presence_return(RESOURCE_ONLINE); + + mock_connection_presence_message("Free to chat"); + + presence_update_expect(RESOURCE_ONLINE, "Free to chat", 0); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} diff --git a/tests/test_cmd_account.h b/tests/test_cmd_account.h index 7ff9970a..d4730652 100644 --- a/tests/test_cmd_account.h +++ b/tests/test_cmd_account.h @@ -55,3 +55,4 @@ void cmd_account_set_priority_too_low_shows_message(void **state); void cmd_account_set_priority_too_high_shows_message(void **state); void cmd_account_set_priority_when_not_number_shows_message(void **state); void cmd_account_set_priority_when_empty_shows_message(void **state); +void cmd_account_set_priority_updates_presence_when_account_connected_with_presence(void **state); diff --git a/tests/testsuite.c b/tests/testsuite.c index 843a5981..62bbfc63 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -246,6 +246,7 @@ int main(int argc, char* argv[]) { unit_test(cmd_account_set_priority_too_high_shows_message), unit_test(cmd_account_set_priority_when_not_number_shows_message), unit_test(cmd_account_set_priority_when_empty_shows_message), + unit_test(cmd_account_set_priority_updates_presence_when_account_connected_with_presence), }; return run_tests(tests); } diff --git a/tests/xmpp/mock_xmpp.c b/tests/xmpp/mock_xmpp.c index c405ae48..70bcbc3b 100644 --- a/tests/xmpp/mock_xmpp.c +++ b/tests/xmpp/mock_xmpp.c @@ -43,6 +43,20 @@ _mock_jabber_connect_with_account(const ProfAccount * const account) return (jabber_conn_status_t)mock(); } +static char * +_mock_jabber_get_presence_message(void) +{ + return (char *)mock(); +} + +static void +_mock_presence_update(resource_presence_t status, const char * const msg, int idle) +{ + check_expected(status); + check_expected(msg); + check_expected(idle); +} + void mock_jabber_connect_with_details(void) { @@ -55,6 +69,12 @@ mock_jabber_connect_with_account(void) jabber_connect_with_account = _mock_jabber_connect_with_account; } +void +mock_presence_update(void) +{ + presence_update = _mock_presence_update; +} + void mock_connection_status(jabber_conn_status_t status) { @@ -69,6 +89,13 @@ mock_connection_account_name(char *name) will_return(_mock_jabber_get_account_name, name); } +void +mock_connection_presence_message(char *message) +{ + jabber_get_presence_message = _mock_jabber_get_presence_message; + will_return(_mock_jabber_get_presence_message, message); +} + void expect_room_list_request(char *conf_server) { @@ -120,3 +147,11 @@ jabber_connect_with_account_return(ProfAccount *account, expect_any(_mock_jabber_connect_with_account, account); will_return(_mock_jabber_connect_with_account, result); } + +void +presence_update_expect(resource_presence_t presence, char *msg, int idle) +{ + expect_value(_mock_presence_update, status, presence); + expect_string(_mock_presence_update, msg, msg); + expect_value(_mock_presence_update, idle, idle); +} diff --git a/tests/xmpp/mock_xmpp.h b/tests/xmpp/mock_xmpp.h index 678fa25d..c52f5e5f 100644 --- a/tests/xmpp/mock_xmpp.h +++ b/tests/xmpp/mock_xmpp.h @@ -5,6 +5,7 @@ void mock_connection_status(jabber_conn_status_t status); void mock_connection_account_name(char *name); +void mock_connection_presence_message(char *message); void expect_room_list_request(char *conf_server); void mock_jabber_connect_with_details(void); @@ -19,4 +20,7 @@ void jabber_connect_with_account_expect_and_return(ProfAccount *account, jabber_conn_status_t result); void jabber_connect_with_account_return(jabber_conn_status_t result); +void mock_presence_update(void); +void presence_update_expect(resource_presence_t presence, char *msg, int idle); + #endif