Update python binding
[empathy.git] / tests / check-empathy-irc-network.c
blobb8124a30519e18187ad3a1b5a5e1a864cc792b65
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
5 #include <check.h>
6 #include "check-helpers.h"
7 #include "check-libempathy.h"
8 #include "check-irc-helper.h"
10 #include <libempathy/empathy-irc-network.h>
12 START_TEST (test_empathy_irc_network_new)
14 EmpathyIrcNetwork *network;
16 network = empathy_irc_network_new ("Network1");
17 check_network (network, "Network1", "UTF-8", NULL, 0);
19 g_object_unref (network);
21 END_TEST
23 START_TEST (test_property_change)
25 EmpathyIrcNetwork *network;
27 network = empathy_irc_network_new ("Network1");
28 check_network (network, "Network1", "UTF-8", NULL, 0);
30 g_object_set (network,
31 "name", "Network2",
32 "charset", "ISO-8859-1",
33 NULL);
35 check_network (network, "Network2", "ISO-8859-1", NULL, 0);
37 g_object_unref (network);
40 END_TEST
42 static gboolean modified;
44 static void
45 modified_cb (EmpathyIrcNetwork *network,
46 gpointer unused)
48 modified = TRUE;
51 START_TEST (test_modified_signal)
53 EmpathyIrcNetwork *network;
55 network = empathy_irc_network_new ("Network1");
56 check_network (network, "Network1", "UTF-8", NULL, 0);
58 modified = FALSE;
59 g_signal_connect (network, "modified", G_CALLBACK (modified_cb), NULL);
61 g_object_set (network, "name", "Network2", NULL);
62 fail_if (!modified);
63 modified = FALSE;
64 g_object_set (network, "name", "Network2", NULL);
65 fail_if (modified);
67 g_object_unref (network);
69 END_TEST
71 static void
72 add_servers (EmpathyIrcNetwork *network,
73 struct server_t *servers,
74 guint nb_servers)
76 guint i;
78 for (i = 0; i < nb_servers; i ++)
80 EmpathyIrcServer *server;
82 server = empathy_irc_server_new (servers[i].address,
83 servers[i].port, servers[i].ssl);
84 modified = FALSE;
85 empathy_irc_network_append_server (network, server);
86 fail_if (!modified);
87 g_object_unref (server);
91 START_TEST (test_add_server)
93 EmpathyIrcNetwork *network;
94 EmpathyIrcServer *server;
95 GSList *servers, *l;
96 struct server_t test_servers[] = {
97 { "server1", 6667, FALSE },
98 { "server2", 6668, TRUE },
99 { "server3", 6667, FALSE },
100 { "server4", 6669, TRUE }};
101 struct server_t servers_without_3[] = {
102 { "server1", 6667, FALSE },
103 { "server2", 6668, TRUE },
104 { "server4", 6669, TRUE }};
106 network = empathy_irc_network_new ("Network1");
107 check_network (network, "Network1", "UTF-8", NULL, 0);
109 modified = FALSE;
110 g_signal_connect (network, "modified", G_CALLBACK (modified_cb), NULL);
112 check_network (network, "Network1", "UTF-8", NULL, 0);
114 /* add the servers */
115 add_servers (network, test_servers, 4);
117 check_network (network, "Network1", "UTF-8", test_servers, 4);
119 /* Now let's remove the 3rd server */
120 servers = empathy_irc_network_get_servers (network);
121 l = g_slist_nth (servers, 2);
122 fail_if (l == NULL);
123 server = l->data;
124 modified = FALSE;
125 empathy_irc_network_remove_server (network, server);
126 fail_if (!modified);
128 /* free the list */
129 g_slist_foreach (servers, (GFunc) g_object_unref, NULL);
130 g_slist_free (servers);
132 /* The 3rd server should have disappear */
133 check_network (network, "Network1", "UTF-8", servers_without_3, 3);
135 g_object_unref (network);
137 END_TEST
139 START_TEST (test_modified_signal_because_of_server)
141 EmpathyIrcNetwork *network;
142 EmpathyIrcServer *server;
144 network = empathy_irc_network_new ("Network1");
145 check_network (network, "Network1", "UTF-8", NULL, 0);
147 g_signal_connect (network, "modified", G_CALLBACK (modified_cb), NULL);
149 server = empathy_irc_server_new ("server1", 6667, FALSE);
150 empathy_irc_network_append_server (network, server);
152 /* Change server properties */
153 modified = FALSE;
154 g_object_set (server, "address", "server2", NULL);
155 fail_if (!modified);
156 modified = FALSE;
157 g_object_set (server, "port", 6668, NULL);
158 fail_if (!modified);
159 modified = FALSE;
160 g_object_set (server, "ssl", TRUE, NULL);
161 fail_if (!modified);
163 empathy_irc_network_remove_server (network, server);
164 modified = FALSE;
165 g_object_set (server, "address", "server3", NULL);
166 /* We removed the server so the network is not modified anymore */
167 fail_if (modified);
169 g_object_unref (network);
171 END_TEST
173 START_TEST (test_empathy_irc_network_set_server_position)
175 EmpathyIrcNetwork *network;
176 GSList *servers, *l;
177 struct server_t test_servers[] = {
178 { "server1", 6667, FALSE },
179 { "server2", 6668, TRUE },
180 { "server3", 6667, FALSE },
181 { "server4", 6669, TRUE }};
182 struct server_t test_servers_sorted[] = {
183 { "server2", 6668, TRUE },
184 { "server4", 6669, TRUE },
185 { "server3", 6667, FALSE },
186 { "server1", 6667, FALSE }};
188 network = empathy_irc_network_new ("Network1");
189 check_network (network, "Network1", "UTF-8", NULL, 0);
191 modified = FALSE;
192 g_signal_connect (network, "modified", G_CALLBACK (modified_cb), NULL);
194 /* add the servers */
195 add_servers (network, test_servers, 4);
196 check_network (network, "Network1", "UTF-8", test_servers, 4);
198 /* get servers list */
199 servers = empathy_irc_network_get_servers (network);
200 fail_if (g_slist_length (servers) != 4);
201 modified = FALSE;
203 /* server1 go to the last position */
204 empathy_irc_network_set_server_position (network, servers->data, -1);
206 /* server2 go to the first position */
207 l = servers->next;
208 empathy_irc_network_set_server_position (network, l->data, 0);
210 /* server3 go to the third position */
211 l = l->next;
212 empathy_irc_network_set_server_position (network, l->data, 2);
214 /* server4 go to the second position*/
215 l = l->next;
216 empathy_irc_network_set_server_position (network, l->data, 1);
218 fail_if (!modified);
220 /* free the list */
221 g_slist_foreach (servers, (GFunc) g_object_unref, NULL);
222 g_slist_free (servers);
224 /* Check if servers are sorted */
225 check_network (network, "Network1", "UTF-8", test_servers_sorted, 4);
227 END_TEST
229 TCase *
230 make_empathy_irc_network_tcase (void)
232 TCase *tc = tcase_create ("empathy-irc-network");
233 tcase_add_test (tc, test_empathy_irc_network_new);
234 tcase_add_test (tc, test_property_change);
235 tcase_add_test (tc, test_modified_signal);
236 tcase_add_test (tc, test_add_server);
237 tcase_add_test (tc, test_modified_signal_because_of_server);
238 tcase_add_test (tc, test_empathy_irc_network_set_server_position);
239 return tc;