diff --git a/src/command/commands.c b/src/command/commands.c index f98d49ac..eb980da9 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -1766,21 +1766,34 @@ cmd_bookmark(gchar **args, struct cmd_help_t help) } if (strcmp(cmd, "add") == 0) { - bookmark_add(jid, nick, autojoin); - GString *msg = g_string_new("Bookmark added for "); - g_string_append(msg, jid); - if (nick != NULL) { - g_string_append(msg, ", nickname: "); - g_string_append(msg, nick); + gboolean added = bookmark_add(jid, nick, autojoin); + if (added) { + GString *msg = g_string_new("Bookmark added for "); + g_string_append(msg, jid); + if (nick != NULL) { + g_string_append(msg, ", nickname: "); + g_string_append(msg, nick); + } + if (autojoin) { + g_string_append(msg, ", autojoin enabled"); + } + g_string_append(msg, "."); + cons_show(msg->str); + g_string_free(msg, TRUE); + } else { + cons_show("Bookmark updated for %s.", jid); } - if (autojoin) { - g_string_append(msg, ", autojoin enabled"); - } - g_string_append(msg, "."); - cons_show(msg->str); - g_string_free(msg, TRUE); } else if (strcmp(cmd, "remove") == 0) { - bookmark_remove(jid, autojoin); + gboolean removed = bookmark_remove(jid, autojoin); + if (removed) { + if (autojoin) { + cons_show("Autojoin disabled for %s.", jid); + } else { + cons_show("Bookmark removed for %s.", jid); + } + } else { + cons_show("No bookmark exists for %s.", jid); + } } else { cons_show("Usage: %s", help.usage); } diff --git a/src/tools/autocomplete.c b/src/tools/autocomplete.c index 4981b690..974ed570 100644 --- a/src/tools/autocomplete.c +++ b/src/tools/autocomplete.c @@ -133,7 +133,7 @@ autocomplete_get_list(Autocomplete ac) } gboolean -autocomplete_contains(Autocomplete ac, char *value) +autocomplete_contains(Autocomplete ac, const char *value) { GSList *curr = ac->items; diff --git a/src/tools/autocomplete.h b/src/tools/autocomplete.h index fadd6e88..57c6015a 100644 --- a/src/tools/autocomplete.h +++ b/src/tools/autocomplete.h @@ -57,5 +57,5 @@ char * autocomplete_param_no_with_func(char *input, int *size, char *command, void autocomplete_reset(Autocomplete ac); -gboolean autocomplete_contains(Autocomplete ac, char *value); +gboolean autocomplete_contains(Autocomplete ac, const char *value); #endif diff --git a/src/xmpp/bookmark.c b/src/xmpp/bookmark.c index 726772a2..e31a5795 100644 --- a/src/xmpp/bookmark.c +++ b/src/xmpp/bookmark.c @@ -58,20 +58,30 @@ bookmark_request(void) xmpp_stanza_release(iq); } -static void +static gboolean _bookmark_add(const char *jid, const char *nick, gboolean autojoin) { + gboolean added = TRUE; + if (autocomplete_contains(bookmark_ac, jid)) { + added = FALSE; + } /* TODO: send request */ /* TODO: manage bookmark_list */ /* this may be command for modifying */ autocomplete_remove(bookmark_ac, jid); autocomplete_add(bookmark_ac, jid); + + return added; } -static void +static gboolean _bookmark_remove(const char *jid, gboolean autojoin) { + gboolean removed = FALSE; + if (autocomplete_contains(bookmark_ac, jid)) { + removed = TRUE; + } /* TODO: manage bookmark_list */ if (autojoin) { /* TODO: just set autojoin=0 */ @@ -79,6 +89,8 @@ _bookmark_remove(const char *jid, gboolean autojoin) /* TODO: send request */ autocomplete_remove(bookmark_ac, jid); } + + return removed; } static const GList * diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h index dd91e398..fdbcb500 100644 --- a/src/xmpp/xmpp.h +++ b/src/xmpp/xmpp.h @@ -132,8 +132,8 @@ void (*iq_set_autoping)(int seconds); Capabilities* (*caps_get)(const char * const caps_str); void (*caps_close)(void); -void (*bookmark_add)(const char *jid, const char *nick, gboolean autojoin); -void (*bookmark_remove)(const char *jid, gboolean autojoin); +gboolean (*bookmark_add)(const char *jid, const char *nick, gboolean autojoin); +gboolean (*bookmark_remove)(const char *jid, gboolean autojoin); const GList * (*bookmark_get_list)(void); char * (*bookmark_find)(char *search_str); void (*bookmark_autocomplete_reset)(void); diff --git a/tests/config/mock_accounts.c b/tests/config/mock_accounts.c index 38e6f89a..3a7139e3 100644 --- a/tests/config/mock_accounts.c +++ b/tests/config/mock_accounts.c @@ -46,7 +46,7 @@ void _mock_accounts_add(const char *account_name, const char *altdomain, const int port) { check_expected(account_name); - check_expected(altdomain); + check_expected(altdomain); } void @@ -252,7 +252,7 @@ _mock_accounts_get_last_presence(const char * const account_name) void mock_accounts_get_account(void) { - accounts_get_account = _mock_accounts_get_account; + accounts_get_account = _mock_accounts_get_account; } void diff --git a/tests/test_cmd_bookmark.c b/tests/test_cmd_bookmark.c index 60d48550..4dc12217 100644 --- a/tests/test_cmd_bookmark.c +++ b/tests/test_cmd_bookmark.c @@ -169,7 +169,7 @@ void cmd_bookmark_add_adds_bookmark_with_jid(void **state) mock_connection_status(JABBER_CONNECTED); - expect_bookmark_add(jid, NULL, FALSE); + expect_and_return_bookmark_add(jid, NULL, FALSE, TRUE); expect_cons_show("Bookmark added for room@conf.server."); gboolean result = cmd_bookmark(args, *help); @@ -189,7 +189,7 @@ void cmd_bookmark_add_adds_bookmark_with_jid_nick(void **state) mock_connection_status(JABBER_CONNECTED); - expect_bookmark_add(jid, nick, FALSE); + expect_and_return_bookmark_add(jid, nick, FALSE, TRUE); expect_cons_show("Bookmark added for room@conf.server, nickname: bob."); gboolean result = cmd_bookmark(args, *help); @@ -208,7 +208,7 @@ void cmd_bookmark_add_adds_bookmark_with_jid_autojoin(void **state) mock_connection_status(JABBER_CONNECTED); - expect_bookmark_add(jid, NULL, TRUE); + expect_and_return_bookmark_add(jid, NULL, TRUE, TRUE); expect_cons_show("Bookmark added for room@conf.server, autojoin enabled."); gboolean result = cmd_bookmark(args, *help); @@ -228,7 +228,7 @@ void cmd_bookmark_add_adds_bookmark_with_jid_nick_autojoin(void **state) mock_connection_status(JABBER_CONNECTED); - expect_bookmark_add(jid, nick, TRUE); + expect_and_return_bookmark_add(jid, nick, TRUE, TRUE); expect_cons_show("Bookmark added for room@conf.server, nickname: bob, autojoin enabled."); gboolean result = cmd_bookmark(args, *help); @@ -254,7 +254,7 @@ void cmd_bookmark_add_adds_bookmark_with_room_details(void **state) mock_current_win_type(WIN_MUC); ui_current_recipient_returns(jid); - expect_bookmark_add(jid, nick, FALSE); + expect_and_return_bookmark_add(jid, nick, FALSE, TRUE); expect_cons_show("Bookmark added for room@conf.server, nickname: bob."); gboolean result = cmd_bookmark(args, *help); @@ -281,7 +281,7 @@ void cmd_bookmark_add_adds_bookmark_with_room_details_autojoin(void **state) mock_current_win_type(WIN_MUC); ui_current_recipient_returns(jid); - expect_bookmark_add(jid, nick, TRUE); + expect_and_return_bookmark_add(jid, nick, TRUE, TRUE); expect_cons_show("Bookmark added for room@conf.server, nickname: bob, autojoin enabled."); gboolean result = cmd_bookmark(args, *help); @@ -290,3 +290,98 @@ void cmd_bookmark_add_adds_bookmark_with_room_details_autojoin(void **state) free(help); muc_close(); } + +void cmd_bookmark_remove_removes_bookmark(void **state) +{ + mock_bookmark_remove(); + mock_cons_show(); + char *jid = "room@conf.server"; + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "remove", jid, NULL }; + + mock_connection_status(JABBER_CONNECTED); + + expect_and_return_bookmark_remove(jid, FALSE, TRUE); + expect_cons_show("Bookmark removed for room@conf.server."); + + gboolean result = cmd_bookmark(args, *help); + assert_true(result); + + free(help); +} + +void cmd_bookmark_remove_removes_autojoin(void **state) +{ + mock_bookmark_remove(); + mock_cons_show(); + char *jid = "room@conf.server"; + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "remove", jid, "autojoin", NULL }; + + mock_connection_status(JABBER_CONNECTED); + + expect_and_return_bookmark_remove(jid, TRUE, TRUE); + expect_cons_show("Autojoin disabled for room@conf.server."); + + gboolean result = cmd_bookmark(args, *help); + assert_true(result); + + free(help); +} + +void cmd_bookmark_add_shows_message_when_upated(void **state) +{ + mock_bookmark_add(); + mock_cons_show(); + char *jid = "room@conf.server"; + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "add", jid, NULL }; + + mock_connection_status(JABBER_CONNECTED); + + expect_and_return_bookmark_add(jid, NULL, FALSE, FALSE); + expect_cons_show("Bookmark updated for room@conf.server."); + + gboolean result = cmd_bookmark(args, *help); + assert_true(result); + + free(help); +} + +void cmd_bookmark_remove_shows_message_when_no_bookmark(void **state) +{ + mock_bookmark_remove(); + mock_cons_show(); + char *jid = "room@conf.server"; + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "remove", jid, NULL }; + + mock_connection_status(JABBER_CONNECTED); + + expect_and_return_bookmark_remove(jid, FALSE, FALSE); + expect_cons_show("No bookmark exists for room@conf.server."); + + gboolean result = cmd_bookmark(args, *help); + assert_true(result); + + free(help); +} + +void cmd_bookmark_remove_autojoin_shows_message_when_no_bookmark(void **state) +{ + mock_bookmark_remove(); + mock_cons_show(); + char *jid = "room@conf.server"; + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "remove", jid, "autojoin", NULL }; + + mock_connection_status(JABBER_CONNECTED); + + expect_and_return_bookmark_remove(jid, TRUE, FALSE); + expect_cons_show("No bookmark exists for room@conf.server."); + + gboolean result = cmd_bookmark(args, *help); + assert_true(result); + + free(help); +} diff --git a/tests/test_cmd_bookmark.h b/tests/test_cmd_bookmark.h index de381c1a..ef8729ca 100644 --- a/tests/test_cmd_bookmark.h +++ b/tests/test_cmd_bookmark.h @@ -13,3 +13,8 @@ void cmd_bookmark_add_adds_bookmark_with_jid_autojoin(void **state); void cmd_bookmark_add_adds_bookmark_with_jid_nick_autojoin(void **state); void cmd_bookmark_add_adds_bookmark_with_room_details(void **state); void cmd_bookmark_add_adds_bookmark_with_room_details_autojoin(void **state); +void cmd_bookmark_remove_removes_bookmark(void **state); +void cmd_bookmark_remove_removes_autojoin(void **state); +void cmd_bookmark_add_shows_message_when_upated(void **state); +void cmd_bookmark_remove_shows_message_when_no_bookmark(void **state); +void cmd_bookmark_remove_autojoin_shows_message_when_no_bookmark(void **state); diff --git a/tests/testsuite.c b/tests/testsuite.c index 8446461f..a3be51cc 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -454,6 +454,11 @@ int main(int argc, char* argv[]) { unit_test(cmd_bookmark_add_adds_bookmark_with_jid_nick_autojoin), unit_test(cmd_bookmark_add_adds_bookmark_with_room_details), unit_test(cmd_bookmark_add_adds_bookmark_with_room_details_autojoin), + unit_test(cmd_bookmark_remove_removes_bookmark), + unit_test(cmd_bookmark_remove_removes_autojoin), + unit_test(cmd_bookmark_add_shows_message_when_upated), + unit_test(cmd_bookmark_remove_shows_message_when_no_bookmark), + unit_test(cmd_bookmark_remove_autojoin_shows_message_when_no_bookmark), }; diff --git a/tests/ui/mock_ui.c b/tests/ui/mock_ui.c index d5b6aeae..4fdd0a99 100644 --- a/tests/ui/mock_ui.c +++ b/tests/ui/mock_ui.c @@ -95,7 +95,7 @@ void _mock_cons_show_account_list(gchar **accounts) static char * _mock_ui_ask_password(void) { - return (char *)mock(); + return (char *)mock(); } static @@ -160,7 +160,7 @@ void mock_cons_show(void) { cons_show = _mock_cons_show; - + } void diff --git a/tests/xmpp/mock_xmpp.c b/tests/xmpp/mock_xmpp.c index d6518322..054e3fdd 100644 --- a/tests/xmpp/mock_xmpp.c +++ b/tests/xmpp/mock_xmpp.c @@ -64,12 +64,21 @@ _mock_bookmark_get_list(void) return (GList *)mock(); } -static void +static gboolean _mock_bookmark_add(const char *jid, const char *nick, gboolean autojoin) { check_expected(jid); check_expected(nick); check_expected(autojoin); + return (gboolean)mock(); +} + +static gboolean +_mock_bookmark_remove(const char *jid, gboolean autojoin) +{ + check_expected(jid); + check_expected(autojoin); + return (gboolean)mock(); } void @@ -103,6 +112,12 @@ mock_bookmark_add(void) bookmark_add = _mock_bookmark_add; } +void +mock_bookmark_remove(void) +{ + bookmark_remove = _mock_bookmark_remove; +} + void bookmark_get_list_returns(GList *bookmarks) { @@ -181,7 +196,8 @@ presence_update_expect(resource_presence_t presence, char *msg, int idle) } void -expect_bookmark_add(char *expected_jid, char *expected_nick, gboolean expected_autojoin) +expect_and_return_bookmark_add(char *expected_jid, char *expected_nick, + gboolean expected_autojoin, gboolean added) { expect_string(_mock_bookmark_add, jid, expected_jid); if (expected_nick != NULL) { @@ -190,4 +206,16 @@ expect_bookmark_add(char *expected_jid, char *expected_nick, gboolean expected_a expect_any(_mock_bookmark_add, nick); } expect_value(_mock_bookmark_add, autojoin, expected_autojoin); + + will_return(_mock_bookmark_add, added); +} + +void +expect_and_return_bookmark_remove(char *expected_jid, gboolean expected_autojoin, + gboolean removed) +{ + expect_string(_mock_bookmark_remove, jid, expected_jid); + expect_value(_mock_bookmark_remove, autojoin, expected_autojoin); + + will_return(_mock_bookmark_remove, removed); } diff --git a/tests/xmpp/mock_xmpp.h b/tests/xmpp/mock_xmpp.h index b3da50d4..82b4f8d0 100644 --- a/tests/xmpp/mock_xmpp.h +++ b/tests/xmpp/mock_xmpp.h @@ -24,6 +24,11 @@ void presence_update_expect(resource_presence_t presence, char *msg, int idle); void bookmark_get_list_returns(GList *bookmarks); void mock_bookmark_add(void); -void expect_bookmark_add(char *expected_jid, char *expected_nick, gboolean expected_autojoin); +void expect_and_return_bookmark_add(char *expected_jid, char *expected_nick, + gboolean expected_autojoin, gboolean added); + +void mock_bookmark_remove(void); +void expect_and_return_bookmark_remove(char *expected_jid, gboolean expected_autojoin, + gboolean removed); #endif