1 /* Tests of TpAutomaticProxyFactory and TpBasicProxyFactory
2  *
3  * Copyright © 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 <string.h>
13 
14 #include <telepathy-glib/telepathy-glib.h>
15 
16 #include "tests/lib/util.h"
17 #include "tests/lib/simple-conn.h"
18 #include "tests/lib/stream-tube-chan.h"
19 
20 typedef struct {
21     GMainLoop *mainloop;
22     TpDBusDaemon *dbus;
23 
24     /* Service side objects */
25     TpBaseConnection *base_connection;
26     TpTestsStreamTubeChannel *tube_chan_service;
27     TpHandleRepoIface *contact_repo;
28 
29     /* Client side objects */
30     TpConnection *connection;
31 
32     TpClientChannelFactory *factory;
33 
34     GError *error /* initialized where needed */;
35     gint wait;
36 } Test;
37 
38 static void
create_tube_service(Test * test)39 create_tube_service (Test *test)
40 {
41   gchar *chan_path;
42   TpHandle handle, alf_handle;
43   GType type;
44 
45   tp_clear_object (&test->tube_chan_service);
46 
47   /* Create service-side tube channel object */
48   chan_path = g_strdup_printf ("%s/Channel",
49       tp_proxy_get_object_path (test->connection));
50 
51   test->contact_repo = tp_base_connection_get_handles (test->base_connection,
52       TP_HANDLE_TYPE_CONTACT);
53   g_assert (test->contact_repo != NULL);
54 
55   handle = tp_handle_ensure (test->contact_repo, "bob", NULL, &test->error);
56   type = TP_TESTS_TYPE_CONTACT_STREAM_TUBE_CHANNEL;
57 
58   g_assert_no_error (test->error);
59 
60   alf_handle = tp_handle_ensure (test->contact_repo, "alf", NULL, &test->error);
61   g_assert_no_error (test->error);
62 
63   test->tube_chan_service = g_object_new (
64       type,
65       "connection", test->base_connection,
66       "handle", handle,
67       "requested", TRUE,
68       "object-path", chan_path,
69       "initiator-handle", alf_handle,
70       NULL);
71 
72   g_free (chan_path);
73 
74   tp_handle_unref (test->contact_repo, handle);
75 }
76 
77 static void
setup(Test * test,gconstpointer data)78 setup (Test *test,
79        gconstpointer data)
80 {
81   test->mainloop = g_main_loop_new (NULL, FALSE);
82   test->dbus = tp_tests_dbus_daemon_dup_or_die ();
83 
84   test->error = NULL;
85 
86   /* Create (service and client sides) connection objects */
87   tp_tests_create_and_connect_conn (TP_TESTS_TYPE_SIMPLE_CONNECTION,
88       "me@test.com", &test->base_connection, &test->connection);
89 
90   create_tube_service (test);
91 }
92 
93 static void
teardown(Test * test,gconstpointer data)94 teardown (Test *test,
95           gconstpointer data)
96 {
97   g_clear_error (&test->error);
98 
99   tp_clear_object (&test->dbus);
100   g_main_loop_unref (test->mainloop);
101   test->mainloop = NULL;
102 
103   tp_clear_object (&test->tube_chan_service);
104 
105   tp_tests_connection_assert_disconnect_succeeds (test->connection);
106   g_object_unref (test->connection);
107   g_object_unref (test->base_connection);
108 
109   tp_clear_object (&test->factory);
110 }
111 
112 static void
test_basic_creation(Test * test,gconstpointer data G_GNUC_UNUSED)113 test_basic_creation (Test *test,
114     gconstpointer data G_GNUC_UNUSED)
115 {
116   test->factory = TP_CLIENT_CHANNEL_FACTORY (tp_basic_proxy_factory_new ());
117 
118   g_assert (TP_IS_BASIC_PROXY_FACTORY (test->factory));
119   g_assert (TP_IS_CLIENT_CHANNEL_FACTORY (test->factory));
120 }
121 
122 static void
test_auto_creation(Test * test,gconstpointer data G_GNUC_UNUSED)123 test_auto_creation (Test *test,
124     gconstpointer data G_GNUC_UNUSED)
125 {
126   test->factory = TP_CLIENT_CHANNEL_FACTORY (
127       tp_automatic_proxy_factory_new ());
128 
129   g_assert (TP_IS_AUTOMATIC_PROXY_FACTORY (test->factory));
130   g_assert (TP_IS_CLIENT_CHANNEL_FACTORY (test->factory));
131 }
132 
133 static gboolean
array_contains_feature(GArray * features,const GQuark feature)134 array_contains_feature (GArray *features,
135     const GQuark feature)
136 {
137   guint i;
138 
139   for (i = 0; i < features->len; i++)
140     {
141       if (g_array_index (features, GQuark, i) == feature)
142         return TRUE;
143     }
144 
145   return FALSE;
146 }
147 
148 /* Create a proxy for a stream tube */
149 static void
test_basic_stream_tube(Test * test,gconstpointer data G_GNUC_UNUSED)150 test_basic_stream_tube (Test *test,
151     gconstpointer data G_GNUC_UNUSED)
152 {
153   TpChannel *chan;
154   gchar *chan_path;
155   GHashTable *props;
156   GArray *features;
157 
158   test->factory = TP_CLIENT_CHANNEL_FACTORY (tp_basic_proxy_factory_new ());
159 
160   g_object_get (test->tube_chan_service,
161       "object-path", &chan_path,
162       "channel-properties", &props,
163       NULL);
164 
165   chan = tp_client_channel_factory_create_channel (test->factory,
166       test->connection, chan_path, props, &test->error);
167   g_assert_no_error (test->error);
168 
169   g_assert (TP_IS_CHANNEL (chan));
170   g_assert (!TP_IS_STREAM_TUBE_CHANNEL (chan));
171 
172   features = tp_client_channel_factory_dup_channel_features (test->factory,
173       chan);
174   g_assert_cmpuint (features->len, ==, 1);
175   g_assert (array_contains_feature (features, TP_CHANNEL_FEATURE_CORE));
176 
177   g_free (chan_path);
178   g_hash_table_unref (props);
179   g_array_unref (features);
180 }
181 
182 static void
test_auto_stream_tube(Test * test,gconstpointer data G_GNUC_UNUSED)183 test_auto_stream_tube (Test *test,
184     gconstpointer data G_GNUC_UNUSED)
185 {
186   TpChannel *chan;
187   gchar *chan_path;
188   GHashTable *props;
189   GArray *features;
190 
191   test->factory = TP_CLIENT_CHANNEL_FACTORY (
192       tp_automatic_proxy_factory_new ());
193 
194   g_object_get (test->tube_chan_service,
195       "object-path", &chan_path,
196       "channel-properties", &props,
197       NULL);
198 
199   chan = tp_client_channel_factory_create_channel (test->factory,
200       test->connection, chan_path, props, &test->error);
201   g_assert_no_error (test->error);
202 
203   g_assert (TP_IS_CHANNEL (chan));
204   g_assert (TP_IS_STREAM_TUBE_CHANNEL (chan));
205 
206   features = tp_client_channel_factory_dup_channel_features (test->factory,
207       chan);
208   g_assert_cmpuint (features->len, ==, 3);
209   g_assert (array_contains_feature (features, TP_CHANNEL_FEATURE_CORE));
210   g_assert (array_contains_feature (features, TP_CHANNEL_FEATURE_GROUP));
211   g_assert (array_contains_feature (features, TP_CHANNEL_FEATURE_PASSWORD));
212 
213   g_free (chan_path);
214   g_hash_table_unref (props);
215   g_array_unref (features);
216 }
217 
218 static void
test_basic_dup(Test * test,gconstpointer data G_GNUC_UNUSED)219 test_basic_dup (Test *test,
220     gconstpointer data G_GNUC_UNUSED)
221 {
222   TpBasicProxyFactory *fac;
223 
224   test->factory = TP_CLIENT_CHANNEL_FACTORY (tp_basic_proxy_factory_dup ());
225   g_assert (TP_IS_BASIC_PROXY_FACTORY (test->factory));
226   g_assert (TP_IS_CLIENT_CHANNEL_FACTORY (test->factory));
227 
228   fac = tp_basic_proxy_factory_dup ();
229   g_assert ((gpointer) fac == (gpointer) test->factory);
230 
231   g_object_unref (fac);
232 }
233 
234 static void
test_auto_dup(Test * test,gconstpointer data G_GNUC_UNUSED)235 test_auto_dup (Test *test,
236     gconstpointer data G_GNUC_UNUSED)
237 {
238   TpAutomaticProxyFactory *fac;
239 
240   test->factory = TP_CLIENT_CHANNEL_FACTORY (tp_automatic_proxy_factory_dup ());
241   g_assert (TP_IS_AUTOMATIC_PROXY_FACTORY (test->factory));
242   g_assert (TP_IS_CLIENT_CHANNEL_FACTORY (test->factory));
243 
244   fac = tp_automatic_proxy_factory_dup ();
245   g_assert ((gpointer) fac == (gpointer) test->factory);
246 
247   g_object_unref (fac);
248 }
249 
250 int
main(int argc,char ** argv)251 main (int argc,
252       char **argv)
253 {
254   tp_tests_init (&argc, &argv);
255   g_test_bug_base ("http://bugs.freedesktop.org/show_bug.cgi?id=");
256 
257   g_test_add ("/client-channel-factory/basic/creation", Test, NULL, setup,
258       test_basic_creation, teardown);
259   g_test_add ("/client-channel-factory/auto/creation", Test, NULL, setup,
260       test_auto_creation, teardown);
261   g_test_add ("/client-channel-factory/basic/stream-tube", Test, NULL, setup,
262       test_basic_stream_tube, teardown);
263   g_test_add ("/client-channel-factory/auto/stream-tube", Test, NULL, setup,
264       test_auto_stream_tube, teardown);
265   g_test_add ("/client-channel-factory/basic/dup", Test, NULL, setup,
266       test_basic_dup, teardown);
267   g_test_add ("/client-channel-factory/auto/dup", Test, NULL, setup,
268       test_auto_dup, teardown);
269 
270   return tp_tests_run_with_bus ();
271 }
272