3.12.12
[nijm-empathy.git] / tests / empathy-irc-network-test.c
blob7bee7f467bb31ed1f60c00c5199cbfe9f309a8fb
1 #include "config.h"
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <tp-account-widgets/tpaw-irc-network.h>
8 #include "test-helper.h"
9 #include "test-irc-helper.h"
11 static void
12 test_tpaw_irc_network_new (void)
14 TpawIrcNetwork *network;
16 network = tpaw_irc_network_new ("Network1");
17 check_network (network, "Network1", "UTF-8", NULL, 0);
19 g_object_unref (network);
22 static void
23 test_property_change (void)
25 TpawIrcNetwork *network;
27 network = tpaw_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);
41 static gboolean modified;
43 static void
44 modified_cb (TpawIrcNetwork *network,
45 gpointer unused)
47 modified = TRUE;
50 static void
51 test_modified_signal (void)
53 TpawIrcNetwork *network;
55 network = tpaw_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 g_assert (modified);
63 modified = FALSE;
64 g_object_set (network, "name", "Network2", NULL);
65 g_assert (!modified);
67 g_object_unref (network);
70 static void
71 add_servers (TpawIrcNetwork *network,
72 struct server_t *servers,
73 guint nb_servers)
75 guint i;
77 for (i = 0; i < nb_servers; i ++)
79 TpawIrcServer *server;
81 server = tpaw_irc_server_new (servers[i].address,
82 servers[i].port, servers[i].ssl);
83 modified = FALSE;
84 tpaw_irc_network_append_server (network, server);
85 g_assert (modified);
86 g_object_unref (server);
90 static void
91 test_add_server (void)
93 TpawIrcNetwork *network;
94 TpawIrcServer *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 = tpaw_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 = tpaw_irc_network_get_servers (network);
121 l = g_slist_nth (servers, 2);
122 g_assert (l != NULL);
123 server = l->data;
124 modified = FALSE;
125 tpaw_irc_network_remove_server (network, server);
126 g_assert (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);
138 static void
139 test_modified_signal_because_of_server (void)
141 TpawIrcNetwork *network;
142 TpawIrcServer *server;
144 network = tpaw_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 = tpaw_irc_server_new ("server1", 6667, FALSE);
150 tpaw_irc_network_append_server (network, server);
152 /* Change server properties */
153 modified = FALSE;
154 g_object_set (server, "address", "server2", NULL);
155 g_assert (modified);
156 modified = FALSE;
157 g_object_set (server, "port", 6668, NULL);
158 g_assert (modified);
159 modified = FALSE;
160 g_object_set (server, "ssl", TRUE, NULL);
161 g_assert (modified);
163 tpaw_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 g_assert (!modified);
169 g_object_unref (network);
172 static void
173 test_tpaw_irc_network_set_server_position (void)
175 TpawIrcNetwork *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 = tpaw_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 = tpaw_irc_network_get_servers (network);
200 g_assert (g_slist_length (servers) == 4);
201 modified = FALSE;
203 /* server1 go to the last position */
204 tpaw_irc_network_set_server_position (network, servers->data, -1);
206 /* server2 go to the first position */
207 l = servers->next;
208 tpaw_irc_network_set_server_position (network, l->data, 0);
210 /* server3 go to the third position */
211 l = l->next;
212 tpaw_irc_network_set_server_position (network, l->data, 2);
214 /* server4 go to the second position*/
215 l = l->next;
216 tpaw_irc_network_set_server_position (network, l->data, 1);
218 g_assert (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);
229 main (int argc,
230 char **argv)
232 int result;
234 test_init (argc, argv);
236 g_test_add_func ("/irc-network/new", test_tpaw_irc_network_new);
237 g_test_add_func ("/irc-network/property-change", test_property_change);
238 g_test_add_func ("/irc-network/modified-signal", test_modified_signal);
239 g_test_add_func ("/irc-network/add-server", test_add_server);
240 g_test_add_func ("/irc-network/modified-signal-because-of-server",
241 test_modified_signal_because_of_server);
242 g_test_add_func ("/irc-network/set-server-position",
243 test_tpaw_irc_network_set_server_position);
245 result = g_test_run ();
246 test_deinit ();
247 return result;