1 /* A very basic feature test for TpAccountRequest
2  *
3  * Copyright (C) 2012 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/account-request.h>
15 #include <telepathy-glib/interfaces.h>
16 
17 #include "tests/lib/simple-account.h"
18 #include "tests/lib/simple-account-manager.h"
19 #include "tests/lib/util.h"
20 
21 typedef struct {
22   GMainLoop *mainloop;
23   TpDBusDaemon *dbus;
24 
25   TpTestsSimpleAccountManager *am;
26   TpTestsSimpleAccount *account_service;
27 
28   TpAccountManager *account_manager;
29   TpAccountRequest *account;
30 
31   GAsyncResult *result;
32   GError *error /* initialized where needed */;
33 } Test;
34 
35 static void
setup(Test * test,gconstpointer data G_GNUC_UNUSED)36 setup (Test *test,
37     gconstpointer data G_GNUC_UNUSED)
38 {
39   test->mainloop = g_main_loop_new (NULL, FALSE);
40   test->dbus = tp_tests_dbus_daemon_dup_or_die ();
41   g_assert (test->dbus != NULL);
42 
43   /* create the account manager service */
44   tp_dbus_daemon_request_name (test->dbus,
45       TP_ACCOUNT_MANAGER_BUS_NAME, FALSE, &test->error);
46   g_assert_no_error (test->error);
47   test->am = tp_tests_object_new_static_class (
48       TP_TESTS_TYPE_SIMPLE_ACCOUNT_MANAGER, NULL);
49   tp_dbus_daemon_register_object (test->dbus, TP_ACCOUNT_MANAGER_OBJECT_PATH,
50       test->am);
51 
52   /* and now the account manager proxy */
53   test->account_manager = tp_account_manager_dup ();
54   g_assert (test->account_manager != NULL);
55 
56   /* finally create the account service */
57   test->account_service = tp_tests_object_new_static_class (
58       TP_TESTS_TYPE_SIMPLE_ACCOUNT, NULL);
59   tp_dbus_daemon_register_object (test->dbus,
60       TP_ACCOUNT_OBJECT_PATH_BASE "gabble/jabber/lospolloshermanos",
61       test->account_service);
62 
63   test->account = NULL;
64 }
65 
66 static void
teardown(Test * test,gconstpointer data G_GNUC_UNUSED)67 teardown (Test *test,
68     gconstpointer data G_GNUC_UNUSED)
69 {
70   g_clear_object (&test->account);
71 
72   tp_dbus_daemon_release_name (test->dbus, TP_ACCOUNT_MANAGER_BUS_NAME,
73       &test->error);
74   g_assert_no_error (test->error);
75   tp_dbus_daemon_unregister_object (test->dbus, test->am);
76   g_clear_object (&test->am);
77 
78   tp_dbus_daemon_unregister_object (test->dbus, test->account_service);
79   g_clear_object (&test->account_service);
80 
81   g_clear_object (&test->dbus);
82   tp_clear_pointer (&test->mainloop, g_main_loop_unref);
83 
84   g_clear_error (&test->error);
85   g_clear_object (&test->result);
86 }
87 
88 static void
test_new(Test * test,gconstpointer data G_GNUC_UNUSED)89 test_new (Test *test,
90     gconstpointer data G_GNUC_UNUSED)
91 {
92   test->account = tp_account_request_new (test->account_manager,
93       "gabble", "jabber", "Gustavo Fring");
94   g_assert (TP_IS_ACCOUNT_REQUEST (test->account));
95 }
96 
97 static void
test_gobject_properties(Test * test,gconstpointer data G_GNUC_UNUSED)98 test_gobject_properties (Test *test,
99     gconstpointer data G_GNUC_UNUSED)
100 {
101   TpAccountManager *am;
102   gchar *manager, *protocol, *display_name;
103 
104   test->account = tp_account_request_new (test->account_manager,
105       "gabble", "jabber", "Charles Dickens");
106 
107   g_object_get (test->account,
108       "account-manager", &am,
109       "connection-manager", &manager,
110       "protocol", &protocol,
111       "display-name", &display_name,
112       NULL);
113 
114   g_assert (am == test->account_manager);
115   g_assert_cmpstr (manager, ==, "gabble");
116   g_assert_cmpstr (protocol, ==, "jabber");
117   g_assert_cmpstr (display_name, ==, "Charles Dickens");
118 
119   g_object_unref (am);
120   g_free (manager);
121   g_free (protocol);
122   g_free (display_name);
123 }
124 
125 static void
test_parameters(Test * test,gconstpointer data G_GNUC_UNUSED)126 test_parameters (Test *test,
127     gconstpointer data G_GNUC_UNUSED)
128 {
129   GVariant *v_str, *v_int;
130   GVariant *params;
131 
132   gboolean found;
133   const gchar *s;
134   guint u;
135 
136   test->account = tp_account_request_new (test->account_manager,
137       "gabble", "jabber", "Mike Ehrmantraut");
138 
139   v_str = g_variant_new_string ("banana");
140   tp_account_request_set_parameter (test->account, "cheese", v_str);
141   g_variant_unref (v_str);
142 
143   v_int = g_variant_new_uint32 (42);
144   tp_account_request_set_parameter (test->account, "life", v_int);
145   g_variant_unref (v_int);
146 
147   tp_account_request_set_parameter_string (test->account,
148       "great", "expectations");
149 
150   g_object_get (test->account,
151       "parameters", &params,
152       NULL);
153 
154   g_assert_cmpuint (g_variant_n_children (params), ==, 3);
155 
156   found = g_variant_lookup (params, "cheese", "&s", &s);
157   g_assert (found);
158   g_assert_cmpstr (s, ==, "banana");
159   found = g_variant_lookup (params, "life", "u", &u);
160   g_assert (found);
161   g_assert_cmpuint (u, ==, 42);
162   found = g_variant_lookup (params, "great", "&s", &s);
163   g_assert (found);
164   g_assert_cmpstr (s, ==, "expectations");
165 
166   g_variant_unref (params);
167 
168   /* now let's unset one and see if it's okay */
169   tp_account_request_unset_parameter (test->account, "cheese");
170 
171   g_object_get (test->account,
172       "parameters", &params,
173       NULL);
174 
175   g_assert_cmpuint (g_variant_n_children (params), ==, 2);
176 
177   found = g_variant_lookup (params, "life", "u", &u);
178   g_assert (found);
179   g_assert_cmpuint (u, ==, 42);
180   found = g_variant_lookup (params, "great", "&s", &s);
181   g_assert (found);
182   g_assert_cmpstr (s, ==, "expectations");
183 
184   g_variant_unref (params);
185 }
186 
187 static void
test_properties(Test * test,gconstpointer data G_GNUC_UNUSED)188 test_properties (Test *test,
189     gconstpointer data G_GNUC_UNUSED)
190 {
191   GVariant *props;
192   gchar *icon_name, *nickname;
193   TpConnectionPresenceType presence_type;
194   gchar *presence_status, *presence_message;
195   gboolean enabled, connect_automatically;
196   gchar **supersedes;
197   GArray *avatar;
198   gchar *mime_type;
199   gboolean found;
200   const gchar *s;
201   gboolean b;
202   GVariant *v;
203   gchar *service, *storage_provider;
204 
205   test->account = tp_account_request_new (test->account_manager,
206       "gabble", "jabber", "Walter Jr.");
207 
208   g_object_get (test->account,
209       "properties", &props,
210       NULL);
211 
212   g_assert_cmpuint (g_variant_n_children (props), ==, 0);
213 
214   g_variant_unref (props);
215 
216   /* now set an icon and try again */
217   tp_account_request_set_icon_name (test->account, "user32.dll");
218 
219   g_object_get (test->account,
220       "properties", &props,
221       "icon-name", &icon_name,
222       NULL);
223 
224   g_assert_cmpuint (g_variant_n_children (props), ==, 1);
225   found = g_variant_lookup (props, TP_PROP_ACCOUNT_ICON, "&s", &s);
226   g_assert (found);
227   g_assert_cmpstr (s, ==, "user32.dll");
228   g_assert_cmpstr (icon_name, ==, "user32.dll");
229 
230   g_variant_unref (props);
231   g_free (icon_name);
232 
233   /* now set the nickname and try again */
234   tp_account_request_set_nickname (test->account, "Walter Jr.");
235 
236   g_object_get (test->account,
237       "properties", &props,
238       "nickname", &nickname,
239       NULL);
240 
241   g_assert_cmpuint (g_variant_n_children (props), ==, 2);
242   found = g_variant_lookup (props, TP_PROP_ACCOUNT_ICON, "&s", &s);
243   g_assert (found);
244   g_assert_cmpstr (s, ==, "user32.dll");
245   found = g_variant_lookup (props, TP_PROP_ACCOUNT_NICKNAME, "&s", &s);
246   g_assert (found);
247   g_assert_cmpstr (s, ==, "Walter Jr.");
248   g_assert_cmpstr (nickname, ==, "Walter Jr.");
249 
250   g_variant_unref (props);
251   g_free (nickname);
252 
253   /* next is requested presence */
254   tp_account_request_set_requested_presence (test->account,
255       TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, "available",
256       "come at me, bro!");
257 
258   g_object_get (test->account,
259       "requested-presence-type", &presence_type,
260       "requested-status", &presence_status,
261       "requested-status-message", &presence_message,
262       NULL);
263 
264   g_assert_cmpuint (presence_type, ==, TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
265   g_assert_cmpstr (presence_status, ==, "available");
266   g_assert_cmpstr (presence_message, ==, "come at me, bro!");
267 
268   g_free (presence_status);
269   g_free (presence_message);
270 
271   /* and automatic presence */
272   tp_account_request_set_automatic_presence (test->account,
273       TP_CONNECTION_PRESENCE_TYPE_BUSY, "busy",
274       "come at me later, actually!");
275 
276   g_object_get (test->account,
277       "automatic-presence-type", &presence_type,
278       "automatic-status", &presence_status,
279       "automatic-status-message", &presence_message,
280       NULL);
281 
282   g_assert_cmpuint (presence_type, ==, TP_CONNECTION_PRESENCE_TYPE_BUSY);
283   g_assert_cmpstr (presence_status, ==, "busy");
284   g_assert_cmpstr (presence_message, ==, "come at me later, actually!");
285 
286   g_free (presence_status);
287   g_free (presence_message);
288 
289   /* now enabled and connect automatically */
290   tp_account_request_set_enabled (test->account, FALSE);
291   tp_account_request_set_connect_automatically (test->account, TRUE);
292 
293   g_object_get (test->account,
294       "properties", &props,
295       "enabled", &enabled,
296       "connect-automatically", &connect_automatically,
297       NULL);
298 
299   g_assert_cmpint (enabled, ==, FALSE);
300   g_assert_cmpint (connect_automatically, ==, TRUE);
301 
302   found = g_variant_lookup (props, TP_PROP_ACCOUNT_ENABLED, "b", &b);
303   g_assert (found);
304   g_assert_cmpint (b, ==, FALSE);
305   found = g_variant_lookup (props, TP_PROP_ACCOUNT_CONNECT_AUTOMATICALLY,
306       "b", &b);
307   g_assert (found);
308   g_assert_cmpint (b, ==, TRUE);
309 
310   g_variant_unref (props);
311 
312   /* supersedes */
313   tp_account_request_add_supersedes (test->account,
314       "/science/yeah/woo");
315 
316   g_object_get (test->account,
317       "properties", &props,
318       "supersedes", &supersedes,
319       NULL);
320 
321   g_assert_cmpuint (g_strv_length (supersedes), ==, 1);
322   g_assert_cmpstr (supersedes[0], ==,
323       "/science/yeah/woo");
324   g_assert (supersedes[1] == NULL);
325 
326   found = g_variant_lookup (props, TP_PROP_ACCOUNT_SUPERSEDES, "^a&o", NULL);
327   g_assert (found);
328 
329   g_strfreev (supersedes);
330   g_variant_unref (props);
331 
332   /* avatar */
333   avatar = g_array_new (FALSE, FALSE, sizeof (guchar));
334   g_array_append_vals (avatar, "hello world", strlen ("hello world") + 1);
335   tp_account_request_set_avatar (test->account,
336       (const guchar *) avatar->data, avatar->len, "image/lolz");
337   g_array_unref (avatar);
338   avatar = NULL;
339 
340   g_object_get (test->account,
341       "properties", &props,
342       "avatar", &avatar,
343       "avatar-mime-type", &mime_type,
344       NULL);
345 
346   g_assert_cmpstr (avatar->data, ==, "hello world");
347   g_assert_cmpuint (avatar->len, ==, strlen ("hello world") + 1);
348   g_assert_cmpstr (mime_type, ==, "image/lolz");
349 
350   v = g_variant_lookup_value (props, TP_PROP_ACCOUNT_INTERFACE_AVATAR_AVATAR,
351       NULL);
352   g_assert (v != NULL);
353   g_variant_unref (v);
354 
355   g_variant_unref (props);
356   g_array_unref (avatar);
357   g_free (mime_type);
358 
359   /* service */
360   tp_account_request_set_service (test->account, "Mushroom");
361 
362   g_object_get (test->account,
363       "properties", &props,
364       "service", &service,
365       NULL);
366 
367   v = g_variant_lookup_value (props, TP_PROP_ACCOUNT_SERVICE, NULL);
368   g_assert (v != NULL);
369   g_assert_cmpstr (g_variant_get_string (v, NULL), ==, "Mushroom");
370   g_variant_unref (v);
371 
372   g_assert_cmpstr (service, ==, "Mushroom");
373 
374   g_variant_unref (props);
375   g_free (service);
376 
377   /* storage provider */
378   tp_account_request_set_storage_provider (test->account, "my.provider");
379 
380   g_object_get (test->account,
381       "properties", &props,
382       "storage-provider", &storage_provider,
383       NULL);
384 
385   v = g_variant_lookup_value (props,
386       TP_PROP_ACCOUNT_INTERFACE_STORAGE_STORAGE_PROVIDER, NULL);
387   g_assert (v != NULL);
388   g_assert_cmpstr (g_variant_get_string (v, NULL), ==, "my.provider");
389   g_variant_unref (v);
390 
391   g_assert_cmpstr (storage_provider, ==, "my.provider");
392 
393   g_variant_unref (props);
394   g_free (storage_provider);
395 }
396 
397 static void
test_create_succeed(Test * test,gconstpointer data G_GNUC_UNUSED)398 test_create_succeed (Test *test,
399     gconstpointer data G_GNUC_UNUSED)
400 {
401   TpAccount *account;
402   GValueArray *array;
403   GPtrArray *supersedes;
404   GArray *avatar;
405 
406   test->account = tp_account_request_new (test->account_manager,
407       "gabble", "jabber", "Hank Schrader");
408 
409   tp_account_request_set_display_name (test->account, "Walter White");
410   tp_account_request_set_icon_name (test->account, "gasmask");
411   tp_account_request_set_nickname (test->account, "Heisenberg");
412   tp_account_request_set_requested_presence (test->account,
413       TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, "available",
414       "Better call Saul!");
415   tp_account_request_set_automatic_presence (test->account,
416       TP_CONNECTION_PRESENCE_TYPE_BUSY, "busy",
417       "Cooking");
418   tp_account_request_set_enabled (test->account, TRUE);
419   tp_account_request_set_connect_automatically (test->account, TRUE);
420 
421   tp_account_request_set_parameter_string (test->account,
422       "account", "walter@white.us");
423   tp_account_request_set_parameter_string (test->account,
424       "password", "holly");
425 
426   tp_account_request_add_supersedes (test->account,
427       "/some/silly/account");
428 
429   avatar = g_array_new (FALSE, FALSE, sizeof (guchar));
430   g_array_append_vals (avatar, "blue meth", strlen ("blue meth") + 1);
431   tp_account_request_set_avatar (test->account,
432       (const guchar *) avatar->data, avatar->len, "image/png");
433   g_array_unref (avatar);
434   avatar = NULL;
435 
436   tp_account_request_create_account_async (test->account,
437       tp_tests_result_ready_cb, &test->result);
438   tp_tests_run_until_result (&test->result);
439 
440   account = tp_account_request_create_account_finish (test->account,
441       test->result, &test->error);
442   g_assert_no_error (test->error);
443   g_assert (account != NULL);
444 
445   g_assert_cmpstr (test->am->create_cm, ==, "gabble");
446   g_assert_cmpstr (test->am->create_protocol, ==, "jabber");
447   g_assert_cmpstr (test->am->create_display_name, ==, "Walter White");
448   g_assert_cmpuint (g_hash_table_size (test->am->create_parameters), ==, 2);
449   g_assert_cmpstr (tp_asv_get_string (test->am->create_parameters, "account"),
450       ==, "walter@white.us");
451   g_assert_cmpstr (tp_asv_get_string (test->am->create_parameters, "password"),
452       ==, "holly");
453   g_assert_cmpuint (g_hash_table_size (test->am->create_properties), ==, 8);
454   g_assert_cmpstr (tp_asv_get_string (test->am->create_properties,
455           TP_PROP_ACCOUNT_ICON),
456       ==, "gasmask");
457   g_assert_cmpstr (tp_asv_get_string (test->am->create_properties,
458           TP_PROP_ACCOUNT_NICKNAME),
459       ==, "Heisenberg");
460   g_assert_cmpint (tp_asv_get_boolean (test->am->create_properties,
461           TP_PROP_ACCOUNT_ENABLED, NULL),
462       ==, TRUE);
463   g_assert_cmpint (tp_asv_get_boolean (test->am->create_properties,
464           TP_PROP_ACCOUNT_CONNECT_AUTOMATICALLY, NULL),
465       ==, TRUE);
466 
467   array = tp_asv_get_boxed (test->am->create_properties,
468       TP_PROP_ACCOUNT_REQUESTED_PRESENCE,
469       TP_STRUCT_TYPE_SIMPLE_PRESENCE);
470   g_assert_cmpuint (g_value_get_uint (array->values), ==,
471       TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
472   g_assert_cmpstr (g_value_get_string (array->values + 1), ==,
473       "available");
474   g_assert_cmpstr (g_value_get_string (array->values + 2), ==,
475       "Better call Saul!");
476 
477   array = tp_asv_get_boxed (test->am->create_properties,
478       TP_PROP_ACCOUNT_AUTOMATIC_PRESENCE,
479       TP_STRUCT_TYPE_SIMPLE_PRESENCE);
480   g_assert_cmpuint (g_value_get_uint (array->values), ==,
481       TP_CONNECTION_PRESENCE_TYPE_BUSY);
482   g_assert_cmpstr (g_value_get_string (array->values + 1), ==,
483       "busy");
484   g_assert_cmpstr (g_value_get_string (array->values + 2), ==,
485       "Cooking");
486 
487   supersedes = tp_asv_get_boxed (test->am->create_properties,
488       TP_PROP_ACCOUNT_SUPERSEDES,
489       TP_ARRAY_TYPE_OBJECT_PATH_LIST);
490   g_assert_cmpuint (supersedes->len, ==, 1);
491   g_assert_cmpstr (g_ptr_array_index (supersedes, 0), ==,
492       "/some/silly/account");
493 
494   array = tp_asv_get_boxed (test->am->create_properties,
495       TP_PROP_ACCOUNT_INTERFACE_AVATAR_AVATAR,
496       TP_STRUCT_TYPE_AVATAR);
497   avatar = g_value_get_boxed (array->values);
498   g_assert_cmpstr (avatar->data, ==, "blue meth");
499   g_assert_cmpstr (g_value_get_string (array->values + 1), ==,
500       "image/png");
501 
502   g_object_unref (account);
503 }
504 
505 static void
test_create_fail(Test * test,gconstpointer data G_GNUC_UNUSED)506 test_create_fail (Test *test,
507     gconstpointer data G_GNUC_UNUSED)
508 {
509   TpAccount *account;
510 
511   test->account = tp_account_request_new (test->account_manager,
512       "gabble", "jabber", "Walter White");
513 
514   /* this will make CreateAccount fail */
515   tp_account_request_set_parameter_string (test->account,
516       "fail", "yes");
517 
518   tp_account_request_create_account_async (test->account,
519       tp_tests_result_ready_cb, &test->result);
520   tp_tests_run_until_result (&test->result);
521 
522   account = tp_account_request_create_account_finish (test->account,
523       test->result, &test->error);
524   g_assert (test->error != NULL);
525   g_assert (account == NULL);
526 
527   g_clear_error (&test->error);
528   test->result = NULL;
529 
530   /* now let's unset the fail=yes and make sure it works */
531 
532   tp_account_request_unset_parameter (test->account, "fail");
533 
534   tp_account_request_create_account_async (test->account,
535       tp_tests_result_ready_cb, &test->result);
536   tp_tests_run_until_result (&test->result);
537 
538   account = tp_account_request_create_account_finish (test->account,
539       test->result, &test->error);
540   g_assert_no_error (test->error);
541   g_assert (account != NULL);
542 
543   g_object_unref (account);
544 }
545 
546 int
main(int argc,char ** argv)547 main (int argc,
548     char **argv)
549 {
550   tp_tests_abort_after (10);
551   tp_debug_set_flags ("all");
552 
553   g_test_init (&argc, &argv, NULL);
554   g_test_bug_base ("http://bugs.freedesktop.org/show_bug.cgi?id=");
555 
556   g_test_add ("/account-request/new", Test, NULL, setup, test_new, teardown);
557   g_test_add ("/account-request/gobject-properties", Test, NULL, setup,
558       test_gobject_properties, teardown);
559   g_test_add ("/account-request/parameters", Test, NULL, setup,
560       test_parameters, teardown);
561   g_test_add ("/account-request/properties", Test, NULL, setup,
562       test_properties, teardown);
563   g_test_add ("/account-request/create-succeed", Test, NULL, setup,
564       test_create_succeed, teardown);
565   g_test_add ("/account-request/create-fail", Test, NULL, setup,
566       test_create_fail, teardown);
567 
568   return tp_tests_run_with_bus ();
569 }
570