summaryrefslogtreecommitdiff
path: root/libpurple/tests/test_protocol_xfer.c
blob: 71627c3f38fd2af5a5b12987514f129f57f91d9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * Purple
 *
 * Purple is the legal property of its developers, whose names are too
 * numerous to list here. Please refer to the COPYRIGHT file distributed
 * with this source distribution
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
 */

#include <glib.h>
#include <string.h>

#include <purple.h>

#include "test_ui.h"

/******************************************************************************
 * PurpleProtcolXfer Implementations
 *****************************************************************************/
static GType test_purple_protocol_xfer_get_type(void);

typedef struct {
	PurpleProtocol parent;

	gboolean can_send;
	gboolean new_xfer_called;
	gboolean send_called;
} TestPurpleProtocolXfer;

typedef struct {
	PurpleProtocolClass parent;
} TestPurpleProtocolXferClass;


static gboolean
test_purple_protocol_xfer_can_receive(PurpleProtocolXfer *prplxfer,
                                      G_GNUC_UNUSED PurpleConnection *c,
                                      G_GNUC_UNUSED const gchar *who)
{
	TestPurpleProtocolXfer *test_xfer = (TestPurpleProtocolXfer *)prplxfer;

	return test_xfer->can_send;
}

static void
test_purple_protocol_xfer_send_file(PurpleProtocolXfer *prplxfer,
                                    G_GNUC_UNUSED PurpleConnection *c,
                                    G_GNUC_UNUSED const gchar *who,
                                    G_GNUC_UNUSED const gchar *filename)
{
	TestPurpleProtocolXfer *test_xfer = (TestPurpleProtocolXfer *)prplxfer;

	test_xfer->send_called = TRUE;
}

static PurpleXfer *
test_purple_protocol_xfer_new_xfer(PurpleProtocolXfer *prplxfer,
                                   PurpleConnection *c,
                                   const gchar *who)
{
	TestPurpleProtocolXfer *test_xfer = (TestPurpleProtocolXfer *)prplxfer;
	PurpleAccount *a = purple_connection_get_account(c);

	test_xfer->new_xfer_called = TRUE;

	return purple_xfer_new(a, PURPLE_XFER_TYPE_SEND, who);
}

static void
test_purple_protocol_xfer_iface_init(PurpleProtocolXferInterface *iface) {
	iface->can_receive = test_purple_protocol_xfer_can_receive;
	iface->send_file = test_purple_protocol_xfer_send_file;
	iface->new_xfer = test_purple_protocol_xfer_new_xfer;
}

G_DEFINE_TYPE_WITH_CODE(
	TestPurpleProtocolXfer,
	test_purple_protocol_xfer,
	PURPLE_TYPE_PROTOCOL,
	G_IMPLEMENT_INTERFACE(
		PURPLE_TYPE_PROTOCOL_XFER,
		test_purple_protocol_xfer_iface_init
	)
);

static void
test_purple_protocol_xfer_init(G_GNUC_UNUSED TestPurpleProtocolXfer *prplxfer) {
}

static void
test_purple_protocol_xfer_class_init(G_GNUC_UNUSED TestPurpleProtocolXferClass *klass) {
}

static TestPurpleProtocolXfer *
test_purple_protocol_xfer_new(void) {
	return (TestPurpleProtocolXfer *)g_object_new(
		test_purple_protocol_xfer_get_type(),
		"id", "prpl-xfer",
		NULL);
}

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_purple_protocol_xfer_can_receive_func(void) {
	TestPurpleProtocolXfer *xfer = NULL;
	PurpleAccount *account = NULL;
	PurpleConnection *connection = NULL;
	gboolean actual = FALSE;

	xfer = test_purple_protocol_xfer_new();
	account = purple_account_new("prpl-xfer-can-receive", "prpl-xfer");
	connection = g_object_new(PURPLE_TYPE_CONNECTION, "account", account, NULL);

	g_assert_true(PURPLE_IS_PROTOCOL_XFER(xfer));

	xfer->can_send = FALSE;
	actual = purple_protocol_xfer_can_receive(PURPLE_PROTOCOL_XFER(xfer),
	                                          connection, "foo");
	g_assert_false(actual);

	xfer->can_send = TRUE;
	actual = purple_protocol_xfer_can_receive(
		PURPLE_PROTOCOL_XFER(xfer),
		connection,
		"foo"
	);
	g_assert_true(actual);

	g_clear_object(&account);
	g_clear_object(&xfer);
}

static void
test_purple_protocol_xfer_send_file_func(void) {
	TestPurpleProtocolXfer *prplxfer = NULL;
	PurpleAccount *account = NULL;
	PurpleConnection *connection = NULL;

	prplxfer = g_object_new(test_purple_protocol_xfer_get_type(), NULL);
	account = purple_account_new("prpl-xfer-send", "prpl-xfer");
	connection = g_object_new(PURPLE_TYPE_CONNECTION, "account", account, NULL);

	purple_protocol_xfer_send_file(PURPLE_PROTOCOL_XFER(prplxfer), connection,
	                               "foo", "somefile");
	g_assert_true(prplxfer->send_called);

	g_clear_object(&account);
	g_clear_object(&prplxfer);
}

static void
test_purple_protocol_xfer_new_func(void) {
	TestPurpleProtocolXfer *prplxfer = NULL;
	PurpleAccount *account = NULL;
	PurpleConnection *connection = NULL;
	PurpleXfer *xfer = NULL;

	prplxfer = g_object_new(test_purple_protocol_xfer_get_type(), NULL);
	account = purple_account_new("prpl-xfer-new-xfer", "prpl-xfer");
	connection = g_object_new(PURPLE_TYPE_CONNECTION, "account", account, NULL);

	xfer = purple_protocol_xfer_new_xfer(PURPLE_PROTOCOL_XFER(prplxfer),
	                                     connection, "foo");
	g_assert_true(PURPLE_IS_XFER(xfer));
	g_assert_cmpstr("foo", ==, purple_xfer_get_remote_user(xfer));
	g_assert_true(prplxfer->new_xfer_called);

	g_clear_object(&account);
	g_clear_object(&prplxfer);
}

/******************************************************************************
 * Main
 *****************************************************************************/
gint
main(gint argc, gchar **argv) {
	gint res = 0;

	g_test_init(&argc, &argv, NULL);

	g_test_set_nonfatal_assertions();

	test_ui_purple_init();

	g_test_add_func(
		"/protocol-xfer/can-receive",
		test_purple_protocol_xfer_can_receive_func
	);

	g_test_add_func(
		"/protocol-xfer/send-file",
		test_purple_protocol_xfer_send_file_func
	);

	g_test_add_func(
		"/protocol-xfer/new",
		test_purple_protocol_xfer_new_func
	);

	res = g_test_run();

	/* FIXME: We cannot call test_ui_purple_uninit() here because connections
	 * are not easily destroyed if they haven't been fully implemented. */

	return res;
}