Commit 11a82654 authored by Philip Withnall's avatar Philip Withnall

lib: Add GtDBusQueue for creating mock D-Bus services

See the documentation in GtDBusQueue for details. This includes all the
code and documentation, but no real unit tests.
Signed-off-by: Philip Withnall's avatarPhilip Withnall <withnall@endlessm.com>
parent d47f30b1
This diff is collapsed.
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright © 2018 Endless Mobile, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors:
* - Philip Withnall <withnall@endlessm.com>
*/
#pragma once
#include <gio/gio.h>
#include <glib.h>
#include <glib-object.h>
G_BEGIN_DECLS
typedef struct _GtDBusQueue GtDBusQueue;
GtDBusQueue *gt_dbus_queue_new (void);
void gt_dbus_queue_free (GtDBusQueue *self);
G_DEFINE_AUTOPTR_CLEANUP_FUNC (GtDBusQueue, gt_dbus_queue_free)
GDBusConnection *gt_dbus_queue_get_client_connection (GtDBusQueue *self);
gboolean gt_dbus_queue_connect (GtDBusQueue *self,
GError **error);
void gt_dbus_queue_disconnect (GtDBusQueue *self,
gboolean assert_queue_empty);
guint gt_dbus_queue_own_name (GtDBusQueue *self,
const gchar *name);
void gt_dbus_queue_unown_name (GtDBusQueue *self,
guint id);
guint gt_dbus_queue_export_object (GtDBusQueue *self,
const gchar *object_path,
GDBusInterfaceInfo *interface_info,
GError **error);
void gt_dbus_queue_unexport_object (GtDBusQueue *self,
guint id);
typedef void (*GtDBusQueueServerFunc) (GtDBusQueue *queue,
gpointer user_data);
void gt_dbus_queue_set_server_func (GtDBusQueue *self,
GtDBusQueueServerFunc func,
gpointer user_data);
gsize gt_dbus_queue_get_n_messages (GtDBusQueue *self);
gboolean gt_dbus_queue_try_pop_message (GtDBusQueue *self,
GDBusMethodInvocation **out_invocation);
gboolean gt_dbus_queue_pop_message (GtDBusQueue *self,
GDBusMethodInvocation **out_invocation);
gboolean gt_dbus_queue_match_client_message (GtDBusQueue *self,
GDBusMethodInvocation *invocation,
const gchar *expected_object_path,
const gchar *expected_interface_name,
const gchar *expected_method_name,
const gchar *expected_parameters_string);
gchar *gt_dbus_queue_format_message (GDBusMethodInvocation *invocation);
gchar *gt_dbus_queue_format_messages (GtDBusQueue *self);
/**
* gt_dbus_queue_assert_no_messages:
* @self: a #GtDBusQueue
*
* Assert that there are no messages currently in the mock service’s message
* queue.
*
* If there are, an assertion fails and some debug output is printed.
*
* Since: 0.1.0
*/
#define gt_dbus_queue_assert_no_messages(self) \
G_STMT_START { \
if (gt_dbus_queue_get_n_messages (self) > 0) \
{ \
g_autofree gchar *anm_list = gt_dbus_queue_format_messages (self); \
g_autofree gchar *anm_message = \
g_strdup_printf ("Expected no messages, but saw %" G_GSIZE_FORMAT ":\n%s", \
gt_dbus_queue_get_n_messages (self), \
anm_list); \
g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
anm_message); \
} \
} G_STMT_END
/**
* gt_dbus_queue_assert_pop_message:
* @self: a #GtDBusQueue
* @expected_object_path: object path the invocation is expected to be calling
* @expected_interface_name: interface name the invocation is expected to be calling
* @expected_method_name: method name the invocation is expected to be calling
* @parameters_format: g_variant_get() format string to extract the parameters
* from the popped #GDBusMethodInvocation into the return locations provided
* in @...
* @...: return locations for the parameter placeholders given in @parameters_format
*
* Assert that a message can be popped off the mock service’s message queue
* (using gt_dbus_queue_pop_message(), which will block) and that it is a method
* call from the #GtDBusQueue’s client connection to the mock service, calling
* @expected_method_name on @expected_interface_name at @expected_object_path
* (as determined using gt_dbus_queue_match_client_message() with a %NULL
* parameters argument). The parameters in the method call will be returned in
* the return locations given in the varargs, according to the
* @parameters_format, using g_variant_get_va().
*
* If a timeout occurs when popping a message, or if the popped message doesn’t
* match the expected object path, interface name or method name, an assertion
* fails and some debug output is printed.
*
* Returns: (transfer full): the popped #GDBusMethodInvocation
* Since: 0.1.0
*/
#define gt_dbus_queue_assert_pop_message(self, expected_object_path, expected_interface_name, expected_method_name, parameters_format, ...) \
gt_dbus_queue_assert_pop_message_impl (self, G_LOG_DOMAIN, __FILE__, __LINE__, \
G_STRFUNC, expected_object_path, \
expected_interface_name, \
expected_method_name, \
parameters_format, __VA_ARGS__)
/* Private implementations of the assertion functions above. */
/*< private >*/
GDBusMethodInvocation *gt_dbus_queue_assert_pop_message_impl (GtDBusQueue *self,
const gchar *macro_log_domain,
const gchar *macro_file,
gint macro_line,
const gchar *macro_function,
const gchar *object_path,
const gchar *interface_name,
const gchar *method_name,
const gchar *parameters_format,
...);
G_END_DECLS
......@@ -14,6 +14,7 @@
<reference id="reference">
<title>API Reference</title>
<xi:include href="xml/dbus-queue.xml" />
<xi:include href="xml/signal-logger.xml" />
</reference>
......
<SECTION>
<TITLE>GtDBusQueue</TITLE>
<FILE>dbus-queue</FILE>
<SUBSECTION>
GtDBusQueue
GtDBusQueueServerFunc
gt_dbus_queue_new
gt_dbus_queue_free
gt_dbus_queue_get_client_connection
gt_dbus_queue_connect
gt_dbus_queue_disconnect
gt_dbus_queue_own_name
gt_dbus_queue_unown_name
gt_dbus_queue_export_object
gt_dbus_queue_unexport_object
gt_dbus_queue_set_server_func
gt_dbus_queue_get_n_messages
gt_dbus_queue_try_pop_message
gt_dbus_queue_pop_message
gt_dbus_queue_match_client_message
gt_dbus_queue_format_message
gt_dbus_queue_format_messages
gt_dbus_queue_assert_no_messages
gt_dbus_queue_assert_pop_message
<SUBSECTION Private>
gt_dbus_queue_assert_pop_message_impl
</SECTION>
<SECTION>
<TITLE>GtSignalLogger</TITLE>
<FILE>signal-logger</FILE>
......
libglib_testing_api_version = '0'
libglib_testing_api_name = 'glib-testing-' + libglib_testing_api_version
libglib_testing_sources = [
'dbus-queue.c',
'signal-logger.c',
]
libglib_testing_headers = [
'dbus-queue.h',
'signal-logger.h',
]
......
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright © 2018 Endless Mobile, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors:
* - Philip Withnall <withnall@endlessm.com>
*/
#include <glib.h>
#include <libglib-testing/dbus-queue.h>
#include <locale.h>
/* Test that creating and destroying a D-Bus queue works. A basic smoketest. */
static void
test_dbus_queue_construction (void)
{
g_autoptr(GtDBusQueue) queue = NULL;
queue = gt_dbus_queue_new ();
/* Call a method to avoid warnings about unused variables. */
g_assert_cmpuint (gt_dbus_queue_get_n_messages (queue), ==, 0);
}
int
main (int argc,
char *argv[])
{
setlocale (LC_ALL, "");
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/dbus-queue/construction",
test_dbus_queue_construction);
return g_test_run ();
}
......@@ -11,6 +11,7 @@ envs = test_env + [
]
test_programs = [
['dbus-queue', [], deps],
['signal-logger', [], deps],
]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment