1 /* Basic test for the request properties given to channel managers.
2 *
3 * Copyright (C) 2010 Collabora Ltd. <http://www.collabora.co.uk/>
4 *
5 * Copying and distribution of this file, with or without modification,
6 * are permitted in any medium without royalty provided the copyright
7 * notice and this notice are preserved.
8 */
9
10 #include "config.h"
11
12 #include <telepathy-glib/channel.h>
13 #include <telepathy-glib/connection.h>
14 #include <telepathy-glib/dbus.h>
15 #include <telepathy-glib/debug.h>
16 #include <telepathy-glib/gtypes.h>
17 #include <telepathy-glib/interfaces.h>
18
19 #include "tests/lib/echo-channel-manager-conn.h"
20 #include "tests/lib/simple-channel-manager.h"
21 #include "tests/lib/myassert.h"
22 #include "tests/lib/util.h"
23
24 typedef struct
25 {
26 GMainLoop *mainloop;
27 TpDBusDaemon *dbus;
28 TpTestsEchoChannelManagerConnection *service_conn;
29 TpTestsSimpleChannelManager *channel_manager;
30
31 TpConnection *conn;
32 GError *error /* initialized where needed */;
33
34 guint waiting;
35 } Test;
36
37 static void
setup(Test * test,gconstpointer data)38 setup (Test *test,
39 gconstpointer data)
40 {
41 TpBaseConnection *service_conn_as_base;
42 gboolean ok;
43 gchar *name, *conn_path;
44
45 tp_debug_set_flags ("all");
46
47 test->mainloop = g_main_loop_new (NULL, FALSE);
48 test->dbus = tp_tests_dbus_daemon_dup_or_die ();
49
50 test->channel_manager = tp_tests_object_new_static_class (
51 TP_TESTS_TYPE_SIMPLE_CHANNEL_MANAGER, NULL);
52 g_assert (test->channel_manager != NULL);
53
54 test->service_conn = TP_TESTS_ECHO_CHANNEL_MANAGER_CONNECTION (
55 tp_tests_object_new_static_class (
56 TP_TESTS_TYPE_ECHO_CHANNEL_MANAGER_CONNECTION,
57 "account", "me@example",
58 "protocol", "example",
59 "channel-manager", test->channel_manager,
60 NULL));
61 g_assert (test->service_conn != NULL);
62 service_conn_as_base = TP_BASE_CONNECTION (test->service_conn);
63 g_assert (service_conn_as_base != NULL);
64
65 test->channel_manager->conn = TP_BASE_CONNECTION (test->service_conn);
66
67 ok = tp_base_connection_register (service_conn_as_base, "example",
68 &name, &conn_path, &test->error);
69 g_assert (ok);
70 g_assert_no_error (test->error);
71
72 test->conn = tp_connection_new (test->dbus, name, conn_path, &test->error);
73 g_assert (test->conn != NULL);
74 g_assert_no_error (test->error);
75
76 g_assert (tp_connection_run_until_ready (test->conn, TRUE, &test->error,
77 NULL));
78 g_assert_no_error (test->error);
79
80 g_free (name);
81 g_free (conn_path);
82
83 test->waiting = 0;
84 }
85
86 static void
teardown(Test * test,gconstpointer data)87 teardown (Test *test,
88 gconstpointer data)
89 {
90 tp_tests_connection_assert_disconnect_succeeds (test->conn);
91
92 g_object_unref (test->service_conn);
93 test->service_conn = NULL;
94
95 g_object_unref (test->dbus);
96 test->dbus = NULL;
97
98 g_main_loop_unref (test->mainloop);
99 test->mainloop = NULL;
100 }
101
102 static void
test_wait(Test * test)103 test_wait (Test *test)
104 {
105 while (test->waiting > 0)
106 g_main_loop_run (test->mainloop);
107 }
108
109 static void
connection_inspect_handles_cb(TpConnection * conn,const gchar ** ids,const GError * error,gpointer user_data,GObject * weak_object)110 connection_inspect_handles_cb (TpConnection *conn,
111 const gchar **ids,
112 const GError *error,
113 gpointer user_data,
114 GObject *weak_object)
115 {
116 Test *test = user_data;
117 const gchar *id;
118
119 g_assert_no_error (error);
120
121 g_assert_cmpuint (g_strv_length ((gchar **) ids), ==, 1);
122
123 id = ids[0];
124
125 g_assert_cmpstr (id, ==, "lolbags");
126
127 test->waiting--;
128 g_main_loop_quit (test->mainloop);
129 }
130
131 static void
channel_manager_request_cb(TpTestsSimpleChannelManager * channel_manager,GHashTable * request_properties,Test * test)132 channel_manager_request_cb (TpTestsSimpleChannelManager *channel_manager,
133 GHashTable *request_properties,
134 Test *test)
135 {
136 const gchar *target_id = tp_asv_get_string (request_properties,
137 TP_PROP_CHANNEL_TARGET_ID);
138 TpHandle handle = tp_asv_get_uint32 (request_properties,
139 TP_PROP_CHANNEL_TARGET_HANDLE, NULL);
140 GArray *handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
141
142 tp_asv_dump (request_properties);
143
144 g_assert (handle != 0);
145 g_assert (target_id != NULL);
146
147 g_assert_cmpstr (target_id, ==, "lolbags#dingdong");
148
149 g_array_append_val (handles, handle);
150
151 tp_cli_connection_call_inspect_handles (test->conn, -1,
152 TP_HANDLE_TYPE_CONTACT, handles,
153 connection_inspect_handles_cb, test, NULL, NULL);
154 test->waiting++;
155
156 g_array_unref (handles);
157
158 test->waiting--;
159 g_main_loop_quit (test->mainloop);
160 }
161
162 static void
create_channel_cb(TpConnection * proxy,const gchar * object_path,GHashTable * properties,const GError * error,gpointer user_data,GObject * weak_object)163 create_channel_cb (TpConnection *proxy,
164 const gchar *object_path,
165 GHashTable *properties,
166 const GError *error,
167 gpointer user_data,
168 GObject *weak_object)
169 {
170 Test *test = user_data;
171 const gchar *target_id = tp_asv_get_string (properties,
172 TP_PROP_CHANNEL_TARGET_ID);
173
174 g_assert_no_error (error);
175
176 tp_asv_dump (properties);
177
178 g_assert (target_id != NULL);
179 g_assert_cmpstr (target_id, ==, "lolbags");
180
181 test->waiting--;
182 g_main_loop_quit (test->mainloop);
183 }
184
185 static void
test_target_id(Test * test,gconstpointer data G_GNUC_UNUSED)186 test_target_id (Test *test,
187 gconstpointer data G_GNUC_UNUSED)
188 {
189 GHashTable *request;
190
191 g_test_bug ("27855");
192
193 request = tp_asv_new (
194 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
195 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
196 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, "lolbags#dingdong",
197 NULL);
198
199 g_signal_connect (test->channel_manager, "request",
200 G_CALLBACK (channel_manager_request_cb), test);
201 test->waiting++;
202
203 tp_cli_connection_interface_requests_call_create_channel (test->conn, -1,
204 request, create_channel_cb, test, NULL, NULL);
205 test->waiting++;
206
207 test_wait (test);
208
209 g_hash_table_unref (request);
210 }
211
212 int
main(int argc,char ** argv)213 main (int argc,
214 char **argv)
215 {
216 tp_tests_abort_after (10);
217 g_test_init (&argc, &argv, NULL);
218 g_test_bug_base ("http://bugs.freedesktop.org/show_bug.cgi?id=");
219
220 g_test_add ("/channel-manager-request-properties/target-id", Test, NULL, setup,
221 test_target_id, teardown);
222
223 return tp_tests_run_with_bus ();
224 }
225