Remove calls to g_type_init()
[dconf.git] / tests / client.c
blob7d0f95d601c5211fb3ea919f1a71934283d36e0c
1 #define _BSD_SOURCE
2 #include "../client/dconf-client.h"
3 #include "../engine/dconf-engine.h"
4 #include "dconf-mock.h"
5 #include <string.h>
6 #include <stdlib.h>
8 static GThread *main_thread;
10 static void
11 test_lifecycle (void)
13 DConfClient *client;
14 GWeakRef weak;
16 client = dconf_client_new ();
17 g_weak_ref_init (&weak, client);
18 g_object_unref (client);
20 g_assert (g_weak_ref_get (&weak) == NULL);
21 g_weak_ref_clear (&weak);
24 static gboolean changed_was_called;
26 static void
27 changed (DConfClient *client,
28 const gchar *prefix,
29 const gchar * const *changes,
30 const gchar *tag,
31 gpointer user_data)
33 g_assert (g_thread_self () == main_thread);
35 changed_was_called = TRUE;
38 static void
39 check_and_free (GVariant *to_check,
40 GVariant *expected)
42 if (expected)
44 g_variant_ref_sink (expected);
45 g_assert (to_check);
47 g_assert (g_variant_equal (to_check, expected));
48 g_variant_unref (to_check);
49 g_variant_unref (expected);
51 else
52 g_assert (to_check == NULL);
55 static void
56 queue_up_100_writes (DConfClient *client)
58 gint i;
60 /* We send 100 writes, letting them pile up.
61 * At no time should there be more than 2 writes on the wire.
63 for (i = 0; i < 100; i++)
65 changed_was_called = FALSE;
66 dconf_client_write_fast (client, "/test/value", g_variant_new_int32 (i), NULL);
67 g_assert (changed_was_called);
69 /* We should always see the most recently written value. */
70 check_and_free (dconf_client_read (client, "/test/value"), g_variant_new_int32 (i));
73 g_assert_cmpint (g_queue_get_length (&dconf_mock_dbus_outstanding_call_handles), ==, 2);
76 static void
77 fail_one_call (void)
79 DConfEngineCallHandle *handle;
80 GError *error;
82 error = g_error_new_literal (G_FILE_ERROR, G_FILE_ERROR_NOENT, "--expected error from testcase--");
83 handle = g_queue_pop_head (&dconf_mock_dbus_outstanding_call_handles);
84 dconf_engine_call_handle_reply (handle, NULL, error);
85 g_error_free (error);
88 static void
89 log_handler (const gchar *log_domain,
90 GLogLevelFlags log_level,
91 const gchar *message,
92 gpointer user_data)
94 if (strstr (message, "--expected error from testcase--"))
95 return;
97 g_log_default_handler (log_domain, log_level, message, user_data);
100 static gboolean
101 fatal_log_handler (const gchar *log_domain,
102 GLogLevelFlags log_level,
103 const gchar *message,
104 gpointer user_data)
106 if (strstr (message, "--expected error from testcase--"))
107 return FALSE;
109 return TRUE;
112 static void
113 test_fast (void)
115 DConfClient *client;
116 gint i;
118 g_log_set_default_handler (log_handler, NULL);
119 g_test_log_set_fatal_handler (fatal_log_handler, NULL);
121 client = dconf_client_new ();
122 g_signal_connect (client, "changed", G_CALLBACK (changed), NULL);
124 queue_up_100_writes (client);
126 /* Start indicating that the writes failed.
128 * For the first failures, we should continue to see the most recently
129 * written value (99).
131 * After we fail that last one, we should see NULL returned.
133 * Each time, we should see a change notify.
136 for (i = 0; g_queue_get_length (&dconf_mock_dbus_outstanding_call_handles) > 1; i++)
138 changed_was_called = FALSE;
139 fail_one_call ();
140 g_assert (changed_was_called);
142 check_and_free (dconf_client_read (client, "/test/value"), g_variant_new_int32 (99));
145 /* Because of the pending-merging logic, we should only have had to
146 * fail two calls.
148 g_assert (i == 2);
150 /* Fail the last call. */
151 changed_was_called = FALSE;
152 fail_one_call ();
153 g_assert (changed_was_called);
155 /* Should read back now as NULL */
156 check_and_free (dconf_client_read (client, "/test/value"), NULL);
158 /* Cleanup */
159 g_signal_handlers_disconnect_by_func (client, changed, NULL);
160 g_object_unref (client);
164 main (int argc, char **argv)
166 setenv ("DCONF_PROFILE", SRCDIR "/profile/will-never-exist", TRUE);
168 main_thread = g_thread_self ();
170 g_test_init (&argc, &argv, NULL);
172 g_test_add_func ("/client/lifecycle", test_lifecycle);
173 g_test_add_func ("/client/basic-fast", test_fast);
175 return g_test_run ();