1 /* Regression test for https://bugs.freedesktop.org/show_bug.cgi?id=15644
2  *
3  * Copyright (C) 2007-2008 Collabora Ltd. <http://www.collabora.co.uk/>
4  * Copyright (C) 2007-2008 Nokia Corporation
5  *
6  * Copying and distribution of this file, with or without modification,
7  * are permitted in any medium without royalty provided the copyright
8  * notice and this notice are preserved.
9  */
10 
11 #include "config.h"
12 
13 #include <telepathy-glib/channel.h>
14 #include <telepathy-glib/connection.h>
15 #include <telepathy-glib/dbus.h>
16 #include <telepathy-glib/debug.h>
17 #include <telepathy-glib/interfaces.h>
18 
19 #include "tests/lib/myassert.h"
20 #include "tests/lib/simple-conn.h"
21 #include "tests/lib/textchan-null.h"
22 #include "tests/lib/util.h"
23 
24 static GMainLoop *mainloop;
25 gboolean shutdown_finished = FALSE;
26 gboolean invalidated = FALSE;
27 
28 static void
on_invalidated(TpChannel * chan,guint domain,gint code,gchar * message,gpointer user_data)29 on_invalidated (TpChannel *chan,
30                 guint domain,
31                 gint code,
32                 gchar *message,
33                 gpointer user_data)
34 {
35   TpChannel **client = user_data;
36 
37   MYASSERT (domain == TP_ERROR, ": domain \"%s\"",
38       g_quark_to_string (domain));
39   MYASSERT (code == TP_ERROR_CANCELLED, ": code %u", code);
40 
41   MYASSERT (*client == chan, "%p vs %p", *client, chan);
42   g_object_unref (*client);
43   *client = NULL;
44 
45   invalidated = TRUE;
46 
47   if (shutdown_finished)
48     g_main_loop_quit (mainloop);
49 }
50 
51 static gboolean
disconnect(gpointer data)52 disconnect (gpointer data)
53 {
54   tp_tests_simple_connection_inject_disconnect (data);
55 
56   return FALSE;
57 }
58 
59 static void
on_shutdown_finished(TpBaseConnection * base_conn,gpointer user_data)60 on_shutdown_finished (TpBaseConnection *base_conn,
61                       gpointer user_data)
62 {
63   shutdown_finished = TRUE;
64 
65   if (invalidated)
66     g_main_loop_quit (mainloop);
67 }
68 
69 int
main(int argc,char ** argv)70 main (int argc,
71       char **argv)
72 {
73   TpTestsSimpleConnection *service_conn;
74   TpBaseConnection *service_conn_as_base;
75   TpHandleRepoIface *contact_repo;
76   TpTestsTextChannelNull *service_chan;
77   TpDBusDaemon *dbus;
78   TpConnection *conn;
79   TpChannel *chan;
80   GError *error = NULL;
81   gchar *name;
82   gchar *conn_path;
83   gchar *chan_path;
84   TpHandle handle;
85 
86   tp_tests_abort_after (10);
87   tp_debug_set_flags ("all");
88   mainloop = g_main_loop_new (NULL, FALSE);
89   dbus = tp_tests_dbus_daemon_dup_or_die ();
90 
91   service_conn = TP_TESTS_SIMPLE_CONNECTION (tp_tests_object_new_static_class (
92         TP_TESTS_TYPE_SIMPLE_CONNECTION,
93         "account", "me@example.com",
94         "protocol", "simple",
95         NULL));
96   service_conn_as_base = TP_BASE_CONNECTION (service_conn);
97   MYASSERT (service_conn != NULL, "");
98   MYASSERT (service_conn_as_base != NULL, "");
99 
100   g_signal_connect (service_conn, "shutdown-finished",
101       G_CALLBACK (on_shutdown_finished), NULL);
102 
103   MYASSERT (tp_base_connection_register (service_conn_as_base, "simple",
104         &name, &conn_path, &error), "");
105   g_assert_no_error (error);
106 
107   conn = tp_connection_new (dbus, name, conn_path, &error);
108   MYASSERT (conn != NULL, "");
109   g_assert_no_error (error);
110 
111   MYASSERT (tp_connection_run_until_ready (conn, TRUE, &error, NULL), "");
112   g_assert_no_error (error);
113 
114   /* Paste on a channel */
115 
116   contact_repo = tp_base_connection_get_handles (service_conn_as_base,
117       TP_HANDLE_TYPE_CONTACT);
118   MYASSERT (contact_repo != NULL, "");
119 
120   handle = tp_handle_ensure (contact_repo, "them@example.org", NULL, &error);
121   g_assert_no_error (error);
122   chan_path = g_strdup_printf ("%s/Channel", conn_path);
123 
124   service_chan = TP_TESTS_TEXT_CHANNEL_NULL (tp_tests_object_new_static_class (
125         TP_TESTS_TYPE_TEXT_CHANNEL_NULL,
126         "connection", service_conn,
127         "object-path", chan_path,
128         "handle", handle,
129         NULL));
130 
131   chan = tp_channel_new (conn, chan_path, TP_IFACE_CHANNEL_TYPE_TEXT,
132       TP_HANDLE_TYPE_CONTACT, handle, &error);
133   g_assert_no_error (error);
134 
135   tp_channel_run_until_ready (chan, &error, NULL);
136   g_assert_no_error (error);
137 
138   g_signal_connect (chan, "invalidated", G_CALLBACK (on_invalidated),
139       &chan);
140 
141   g_idle_add (disconnect, service_conn);
142 
143   g_main_loop_run (mainloop);
144 
145   g_message ("Cleaning up");
146 
147   tp_handle_unref (contact_repo, handle);
148   g_object_unref (conn);
149   g_assert (chan == NULL);
150 
151   g_object_unref (service_chan);
152   service_conn_as_base = NULL;
153   g_object_unref (service_conn);
154   g_object_unref (dbus);
155   g_main_loop_unref (mainloop);
156   g_free (name);
157   g_free (conn_path);
158   g_free (chan_path);
159 
160   return 0;
161 }
162