Declare libdconf_service as a dependency
[dconf.git] / tests / client.c
blob4727e0cda65531dff157ac9c43921a1343d27274
1 #define _DEFAULT_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));
71 check_and_free (dconf_client_read_full (client, "/test/value", DCONF_READ_DEFAULT_VALUE, NULL), NULL);
74 g_assert_cmpint (g_queue_get_length (&dconf_mock_dbus_outstanding_call_handles), ==, 2);
77 static void
78 fail_one_call (void)
80 DConfEngineCallHandle *handle;
81 GError *error;
83 error = g_error_new_literal (G_FILE_ERROR, G_FILE_ERROR_NOENT, "--expected error from testcase--");
84 handle = g_queue_pop_head (&dconf_mock_dbus_outstanding_call_handles);
85 dconf_engine_call_handle_reply (handle, NULL, error);
86 g_error_free (error);
89 static GLogWriterOutput
90 log_writer_cb (GLogLevelFlags log_level,
91 const GLogField *fields,
92 gsize n_fields,
93 gpointer user_data)
95 gsize i;
97 for (i = 0; i < n_fields; i++)
99 if (g_strcmp0 (fields[i].key, "MESSAGE") == 0 &&
100 strstr (fields[i].value, "--expected error from testcase--"))
101 return G_LOG_WRITER_HANDLED;
104 return G_LOG_WRITER_UNHANDLED;
107 static void
108 test_fast (void)
110 DConfClient *client;
111 gint i;
113 g_log_set_writer_func (log_writer_cb, NULL, NULL);
115 client = dconf_client_new ();
116 g_signal_connect (client, "changed", G_CALLBACK (changed), NULL);
118 queue_up_100_writes (client);
120 /* Start indicating that the writes failed.
122 * For the first failures, we should continue to see the most recently
123 * written value (99).
125 * After we fail that last one, we should see NULL returned.
127 * Each time, we should see a change notify.
130 for (i = 0; g_queue_get_length (&dconf_mock_dbus_outstanding_call_handles) > 1; i++)
132 changed_was_called = FALSE;
133 fail_one_call ();
134 g_assert (changed_was_called);
136 check_and_free (dconf_client_read (client, "/test/value"), g_variant_new_int32 (99));
137 check_and_free (dconf_client_read_full (client, "/test/value", DCONF_READ_DEFAULT_VALUE, NULL), NULL);
140 /* Because of the pending-merging logic, we should only have had to
141 * fail two calls.
143 g_assert (i == 2);
145 /* Fail the last call. */
146 changed_was_called = FALSE;
147 fail_one_call ();
148 g_assert (changed_was_called);
150 /* Should read back now as NULL */
151 check_and_free (dconf_client_read (client, "/test/value"), NULL);
152 check_and_free (dconf_client_read_full (client, "/test/value", DCONF_READ_DEFAULT_VALUE, NULL), NULL);
154 /* Cleanup */
155 g_signal_handlers_disconnect_by_func (client, changed, NULL);
156 g_object_unref (client);
160 main (int argc, char **argv)
162 setenv ("DCONF_PROFILE", SRCDIR "/profile/will-never-exist", TRUE);
164 main_thread = g_thread_self ();
166 g_test_init (&argc, &argv, NULL);
168 g_test_add_func ("/client/lifecycle", test_lifecycle);
169 g_test_add_func ("/client/basic-fast", test_fast);
171 return g_test_run ();