1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00
profanity/tests/ui/mock_ui.c

473 lines
9.0 KiB
C
Raw Normal View History

2013-12-14 11:17:53 -05:00
#include <glib.h>
#include <setjmp.h>
#include <cmocka.h>
#include <string.h>
2013-12-14 11:17:53 -05:00
#include "ui/ui.h"
2014-02-01 18:48:24 -05:00
#include "ui/window.h"
2014-02-01 16:18:15 -05:00
#include "tests/helpers.h"
#include "xmpp/bookmark.h"
2013-12-14 11:17:53 -05:00
2013-12-15 14:38:23 -05:00
char output[256];
2014-01-28 18:56:45 -05:00
// Mocks and stubs
2013-12-19 16:05:39 -05:00
static
void _mock_cons_show(const char * const msg, ...)
{
va_list args;
va_start(args, msg);
vsnprintf(output, sizeof(output), msg, args);
check_expected(output);
va_end(args);
}
static
void _stub_cons_show(const char * const msg, ...)
{
}
2014-01-20 13:40:48 -05:00
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, ...)
2013-12-19 16:05:39 -05:00
{
va_list args;
va_start(args, msg);
vsnprintf(output, sizeof(output), msg, args);
check_expected(output);
va_end(args);
2013-12-19 16:05:39 -05:00
}
static
void _mock_cons_show_account(ProfAccount *account)
2013-12-14 11:17:53 -05:00
{
check_expected(account);
2013-12-14 11:17:53 -05:00
}
2014-02-01 16:18:15 -05:00
static
void _mock_cons_show_bookmarks(const GList *list)
{
check_expected(list);
}
static
void _mock_cons_show_aliases(GList *aliases)
{
check_expected(aliases);
}
static
void _mock_cons_show_account_list(gchar **accounts)
2013-12-14 11:17:53 -05:00
{
check_expected(accounts);
2013-12-14 11:17:53 -05:00
}
static
char * _mock_ui_ask_password(void)
2013-12-14 11:17:53 -05:00
{
return (char *)mock();
2013-12-14 11:17:53 -05:00
}
static
char * _stub_ui_ask_password(void)
2013-12-14 11:17:53 -05:00
{
return NULL;
2013-12-14 11:17:53 -05:00
}
2014-02-01 18:48:24 -05:00
static
win_type_t _mock_ui_current_win_type(void)
{
return (win_type_t)mock();
}
static
char * _mock_ui_current_recipeint(void)
{
return (char *)mock();
}
static
void _mock_ui_handle_error(const char * const err_msg)
{
check_expected(err_msg);
}
2014-01-28 18:56:45 -05:00
static
void _mock_ui_handle_recipient_error(const char * const recipient,
const char * const err_msg)
{
check_expected(recipient);
check_expected(err_msg);
}
static
void _stub_ui_handle_recipient_error(const char * const recipient,
const char * const err_msg)
{
}
2014-01-28 18:56:45 -05:00
static
void _mock_ui_handle_recipient_not_found(const char * const recipient,
const char * const err_msg)
{
check_expected(recipient);
check_expected(err_msg);
}
2014-01-20 13:40:48 -05:00
static
void _stub_ui_chat_win_contact_online(PContact contact, Resource *resource, GDateTime *last_activity)
{
}
2014-01-28 18:56:45 -05:00
static
void _stub_ui_handle_recipient_not_found(const char * const recipient, const char * const err_msg)
{
}
2014-02-15 21:22:29 -05:00
static
void _stub_ui_current_update_virtual(void)
2014-02-15 21:22:29 -05:00
{
}
2014-02-17 16:52:42 -05:00
static
void _mock_ui_current_print_formatted_line(const char show_char, int attrs, const char * const msg, ...)
{
check_expected(show_char);
check_expected(attrs);
va_list args;
va_start(args, msg);
vsnprintf(output, sizeof(output), msg, args);
check_expected(output);
va_end(args);
}
static
void _mock_ui_current_print_line(const char * const msg, ...)
{
va_list args;
va_start(args, msg);
vsnprintf(output, sizeof(output), msg, args);
check_expected(output);
va_end(args);
}
static
gboolean _mock_ui_current_win_is_otr(void)
{
return (gboolean)mock();
}
2014-03-08 16:10:23 -05:00
static
void _mock_ui_room_join(char *room)
{
check_expected(room);
}
2014-03-15 21:23:12 -04:00
static
void _mock_cons_show_roster(GSList *list)
{
check_expected(list);
}
2014-04-01 18:14:53 -04:00
static
gboolean _mock_ui_win_exists(int index)
{
check_expected(index);
return (gboolean)mock();
}
2014-04-01 18:33:12 -04:00
static
void _mock_ui_switch_win(const int i)
{
check_expected(i);
}
2014-01-28 18:56:45 -05:00
// bind mocks and stubs
void
mock_cons_show(void)
2013-12-14 11:17:53 -05:00
{
cons_show = _mock_cons_show;
2013-12-14 11:17:53 -05:00
}
2014-01-20 13:40:48 -05:00
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)
2013-12-14 11:17:53 -05:00
{
cons_show_error = _mock_cons_show_error;
2013-12-14 11:17:53 -05:00
}
void
mock_cons_show_account(void)
2013-12-14 11:17:53 -05:00
{
cons_show_account = _mock_cons_show_account;
2013-12-14 11:17:53 -05:00
}
2014-02-01 16:18:15 -05:00
void
mock_cons_show_bookmarks(void)
{
cons_show_bookmarks = _mock_cons_show_bookmarks;
}
void
mock_cons_show_aliases(void)
{
cons_show_aliases = _mock_cons_show_aliases;
}
void
mock_cons_show_account_list(void)
2013-12-14 11:17:53 -05:00
{
cons_show_account_list = _mock_cons_show_account_list;
2013-12-14 11:17:53 -05:00
}
void
mock_ui_ask_password(void)
2013-12-14 11:17:53 -05:00
{
ui_ask_password = _mock_ui_ask_password;
2013-12-14 11:17:53 -05:00
}
2014-02-01 18:48:24 -05:00
void
mock_ui_current_recipient(void)
{
ui_current_recipient = _mock_ui_current_recipeint;
}
void
stub_ui_ask_password(void)
2013-12-14 11:17:53 -05:00
{
ui_ask_password = _stub_ui_ask_password;
2013-12-14 11:17:53 -05:00
}
void
stub_cons_show(void)
2013-12-14 11:17:53 -05:00
{
cons_show = _stub_cons_show;
2013-12-14 11:17:53 -05:00
}
void
2014-01-28 18:56:45 -05:00
stub_ui_handle_recipient_not_found(void)
{
2014-01-28 18:56:45 -05:00
ui_handle_recipient_not_found = _stub_ui_handle_recipient_not_found;
}
void
stub_ui_handle_recipient_error(void)
{
ui_handle_recipient_error = _stub_ui_handle_recipient_error;
}
2014-02-15 21:22:29 -05:00
void
stub_ui_current_update_virtual(void)
2014-02-15 21:22:29 -05:00
{
ui_current_update_virtual = _stub_ui_current_update_virtual;
2014-02-15 21:22:29 -05:00
}
2014-02-17 16:52:42 -05:00
void
mock_ui_current_print_formatted_line(void)
{
ui_current_print_formatted_line = _mock_ui_current_print_formatted_line;
}
void
mock_ui_current_print_line(void)
{
ui_current_print_line = _mock_ui_current_print_line;
}
2014-03-15 21:23:12 -04:00
void
mock_cons_show_roster(void)
{
cons_show_roster = _mock_cons_show_roster;
}
2014-04-01 18:14:53 -04:00
void
mock_ui_win_exists(void)
{
ui_win_exists = _mock_ui_win_exists;
}
2014-01-28 18:56:45 -05:00
// expectations
void
expect_cons_show(char *expected)
2013-12-14 11:17:53 -05:00
{
expect_string(_mock_cons_show, output, expected);
2013-12-14 11:17:53 -05:00
}
void
expect_cons_show_calls(int n)
2013-12-15 14:38:23 -05:00
{
expect_any_count(_mock_cons_show, output, n);
2013-12-15 14:38:23 -05:00
}
void
expect_cons_show_error(char *expected)
2013-12-15 17:28:22 -05:00
{
expect_string(_mock_cons_show_error, output, expected);
2013-12-15 17:28:22 -05:00
}
void
expect_cons_show_account(ProfAccount *account)
2013-12-15 14:56:48 -05:00
{
expect_memory(_mock_cons_show_account, account, account, sizeof(ProfAccount));
2013-12-15 14:56:48 -05:00
}
2014-02-01 16:18:15 -05:00
static gboolean
_cmp_bookmark(Bookmark *bm1, Bookmark *bm2)
{
if (strcmp(bm1->jid, bm2->jid) != 0) {
return FALSE;
}
if (strcmp(bm1->nick, bm2->nick) != 0) {
return FALSE;
}
if (bm1->autojoin != bm2->autojoin) {
return FALSE;
}
return TRUE;
}
void
expect_cons_show_bookmarks(GList *bookmarks)
{
glist_set_cmp((GCompareFunc)_cmp_bookmark);
expect_any(_mock_cons_show_bookmarks, list);
// expect_check(_mock_cons_show_bookmarks, list, (CheckParameterValue)glist_contents_equal, bookmarks);
}
void
expect_cons_show_account_list(gchar **accounts)
2013-12-15 18:07:53 -05:00
{
expect_memory(_mock_cons_show_account_list, accounts, accounts, sizeof(accounts));
2013-12-15 18:07:53 -05:00
}
2014-01-20 13:40:48 -05:00
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_value(_mock_cons_show_contact_online, last_activity, NULL);
2014-01-20 13:40:48 -05:00
} else {
expect_memory(_mock_cons_show_contact_online, last_activity, last_activity, sizeof(last_activity));
}
}
void
expect_cons_show_aliases()
{
// write a custom checker for the list
expect_any(_mock_cons_show_aliases, aliases);
}
void
mock_ui_ask_password_returns(char *password)
2013-12-14 11:17:53 -05:00
{
will_return(_mock_ui_ask_password, strdup(password));
2013-12-14 11:17:53 -05:00
}
void
expect_ui_handle_error(char *err_msg)
{
2014-01-28 18:56:45 -05:00
ui_handle_error = _mock_ui_handle_error;
expect_string(_mock_ui_handle_error, err_msg, err_msg);
}
2014-01-28 18:56:45 -05:00
void
expect_ui_handle_recipient_error(char *recipient, char *err_msg)
{
ui_handle_recipient_error = _mock_ui_handle_recipient_error;
expect_string(_mock_ui_handle_recipient_error, recipient, recipient);
expect_string(_mock_ui_handle_recipient_error, err_msg, err_msg);
}
void
expect_ui_handle_recipient_not_found(char *recipient, char *err_msg)
{
ui_handle_recipient_not_found = _mock_ui_handle_recipient_not_found;
expect_string(_mock_ui_handle_recipient_not_found, recipient, recipient);
expect_string(_mock_ui_handle_recipient_not_found, err_msg, err_msg);
}
2014-02-01 18:48:24 -05:00
void
mock_current_win_type(win_type_t type)
{
ui_current_win_type = _mock_ui_current_win_type;
will_return(_mock_ui_current_win_type, type);
}
void
ui_current_recipient_returns(char *jid)
{
will_return(_mock_ui_current_recipeint, jid);
}
2014-02-17 16:52:42 -05:00
void
ui_current_print_formatted_line_expect(char show_char, int attrs, char *message)
{
expect_value(_mock_ui_current_print_formatted_line, show_char, show_char);
expect_value(_mock_ui_current_print_formatted_line, attrs, attrs);
expect_string(_mock_ui_current_print_formatted_line, output, message);
}
void
ui_current_print_line_expect(char *message)
{
expect_string(_mock_ui_current_print_line, output, message);
}
void
ui_current_win_is_otr_returns(gboolean result)
{
ui_current_win_is_otr = _mock_ui_current_win_is_otr;
will_return(_mock_ui_current_win_is_otr, result);
}
2014-03-08 16:10:23 -05:00
void
ui_room_join_expect(char *room)
{
ui_room_join = _mock_ui_room_join;
expect_string(_mock_ui_room_join, room, room);
}
2014-03-15 21:23:12 -04:00
void
cons_show_roster_expect(GSList *list)
{
expect_any(_mock_cons_show_roster, list);
}
2014-04-01 18:14:53 -04:00
void
ui_win_exists_expect_and_return(int given_index, gboolean result)
{
expect_value(_mock_ui_win_exists, index, given_index);
will_return(_mock_ui_win_exists, result);
}
2014-04-01 18:33:12 -04:00
void
ui_switch_win_expect(int given_i)
{
ui_switch_win = _mock_ui_switch_win;
expect_value(_mock_ui_switch_win, i, given_i);
}