summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Desmottes <guillaume.desmottes@collabora.co.uk>2016-09-01 10:50:17 (GMT)
committerGuillaume Desmottes <guillaume.desmottes@collabora.co.uk>2016-09-06 13:44:25 (GMT)
commit361376bf6db8b253fc66ce7dfd2fcceade90d65b (patch)
tree8daa2b57e987abc3ae25afa3e786084698c72c16
parenta3469eeb2dc29079f38b65cff1e7095431b45f5a (diff)
downloadtraprain-361376bf6db8b253fc66ce7dfd2fcceade90d65b.tar.gz
traprain-361376bf6db8b253fc66ce7dfd2fcceade90d65b.tar.xz
client: API to add routes to the mock service client lib
Signed-off-by: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk> Reviewed-by: Mathieu Duponchelle <mathieu.duponchelle@collabora.co.uk> Differential Revision: https://phabricator.apertis.org/D4180
-rw-r--r--Makefile.am3
-rw-r--r--configure.ac2
-rw-r--r--tests/test-helper.c45
-rw-r--r--tests/test-helper.h4
-rw-r--r--tests/test-mock-service.c72
-rw-r--r--traprain-client/mock.c126
-rw-r--r--traprain-client/mock.h15
-rw-r--r--traprain-common/route-internal.h3
-rw-r--r--traprain-common/route.c13
9 files changed, 262 insertions, 21 deletions
diff --git a/Makefile.am b/Makefile.am
index caf68a9..0f0d24b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -277,6 +277,7 @@ traprain_client_libtraprain_client_internal_la_CFLAGS = \
traprain_client_libtraprain_client_internal_la_LIBADD = \
$(top_builddir)/dbus/libtraprain-codegen.la \
+ $(top_builddir)/traprain-common/libtraprain-common-internal.la \
$(TRAPRAIN_LIBS) \
$(CODE_COVERAGE_LDFLAGS) \
$(NULL)
@@ -366,6 +367,7 @@ traprain_client_TraprainClient_@TRP_API_VERSION@_gir_SCANNERFLAGS = \
--identifier-prefix=TrpClient \
--symbol-prefix=trp_client \
--c-include="traprain-client/navigation.h" \
+ --include-uninstalled=$(top_builddir)/traprain-common/TraprainCommon-@TRP_API_VERSION@.gir \
$(WARN_SCANNERFLAGS) \
$(NULL)
@@ -553,6 +555,7 @@ tests_test_mock_service_LDFLAGS = \
tests_test_mock_service_LDADD = \
$(TRAPRAIN_LIBS) \
$(top_builddir)/traprain-client/libtraprain-client-internal.la \
+ $(top_builddir)/tests/libtraprain-test-helper.la \
$(NULL)
EXTRA_DIST += tests/services/org.apertis.Navigation1.service.in
diff --git a/configure.ac b/configure.ac
index a9cd07e..c47870e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -59,7 +59,7 @@ AC_SUBST([TRP_COMMON_PACKAGE_REQUIRES_PRIVATE],[$TRP_PACKAGE_REQUIRES_PRIVATE])
AC_SUBST([TRP_SERVICE_PACKAGE_REQUIRES],["$TRP_PACKAGE_REQUIRES traprain-common-$TRP_API_VERSION"])
AC_SUBST([TRP_SERVICE_PACKAGE_REQUIRES_PRIVATE],[$TRP_PACKAGE_REQUIRES_PRIVATE])
-AC_SUBST([TRP_CLIENT_PACKAGE_REQUIRES],[$TRP_PACKAGE_REQUIRES])
+AC_SUBST([TRP_CLIENT_PACKAGE_REQUIRES],["$TRP_PACKAGE_REQUIRES traprain-common-$TRP_API_VERSION"])
AC_SUBST([TRP_CLIENT_PACKAGE_REQUIRES_PRIVATE],[$TRP_PACKAGE_REQUIRES_PRIVATE])
AX_PKG_CHECK_MODULES([TRAPRAIN_MOCK_SERVICE],
diff --git a/tests/test-helper.c b/tests/test-helper.c
index 8e0c176..6b37aef 100644
--- a/tests/test-helper.c
+++ b/tests/test-helper.c
@@ -10,13 +10,11 @@
#include "test-helper.h"
-TrpRoute *
-create_first_service_route (TrpServiceNavigation *nav)
+void
+build_first_route (TrpRoute *route)
{
- TrpRoute *route;
guint segment;
- route = trp_service_navigation_create_route (nav, FST_ROUTE_DISTANCE, FST_ROUTE_TIME);
trp_route_add_title (route, "fr_FR", FST_ROUTE_TITLE_FR);
trp_route_add_title (route, "en_US", FST_ROUTE_TITLE_EN);
segment = trp_route_add_segment (route, FST_ROUTE_FST_SEG_LAT, FST_ROUTE_FST_SEG_LON);
@@ -24,37 +22,60 @@ create_first_service_route (TrpServiceNavigation *nav)
trp_route_add_segment_description (route, segment, "fr_FR", FST_ROUTE_FST_SEG_DESC_FR);
segment = trp_route_add_segment (route, FST_ROUTE_SND_SEG_LAT, FST_ROUTE_SND_SEG_LON);
trp_route_add_segment_description (route, segment, "en_US", FST_ROUTE_SND_SEG_DESC_EN);
-
- return route;
}
TrpRoute *
-create_second_service_route (TrpServiceNavigation *nav)
+create_first_service_route (TrpServiceNavigation *nav)
{
TrpRoute *route;
+
+ route = trp_service_navigation_create_route (nav, FST_ROUTE_DISTANCE, FST_ROUTE_TIME);
+ build_first_route (route);
+
+ return route;
+}
+
+void
+build_second_route (TrpRoute *route)
+{
guint segment;
- route = trp_service_navigation_create_route (nav, SND_ROUTE_DISTANCE, SND_ROUTE_TIME);
trp_route_add_title (route, "fr_FR", SND_ROUTE_TITLE_FR);
trp_route_add_title (route, "en_US", SND_ROUTE_TITLE_EN);
segment = trp_route_add_segment (route, SND_ROUTE_FST_SEG_LAT, SND_ROUTE_FST_SEG_LON);
trp_route_add_segment_description (route, segment, "fr_FR", SND_ROUTE_FST_SEG_DESC_FR);
trp_route_add_segment (route, SND_ROUTE_SND_SEG_LAT, SND_ROUTE_SND_SEG_LON);
-
- return route;
}
TrpRoute *
-create_third_service_route (TrpServiceNavigation *nav)
+create_second_service_route (TrpServiceNavigation *nav)
{
TrpRoute *route;
+
+ route = trp_service_navigation_create_route (nav, SND_ROUTE_DISTANCE, SND_ROUTE_TIME);
+ build_second_route (route);
+
+ return route;
+}
+
+void
+build_third_route (TrpRoute *route)
+{
guint segment;
- route = trp_service_navigation_create_route (nav, THD_ROUTE_DISTANCE, THD_ROUTE_TIME);
trp_route_add_title (route, "en_US", THD_ROUTE_TITLE_EN);
segment = trp_route_add_segment (route, THD_ROUTE_FST_SEG_LAT, THD_ROUTE_FST_SEG_LON);
trp_route_add_segment_description (route, segment, "fr_FR", THD_ROUTE_FST_SEG_DESC_FR);
trp_route_add_segment (route, THD_ROUTE_SND_SEG_LAT, THD_ROUTE_SND_SEG_LON);
+}
+
+TrpRoute *
+create_third_service_route (TrpServiceNavigation *nav)
+{
+ TrpRoute *route;
+
+ route = trp_service_navigation_create_route (nav, THD_ROUTE_DISTANCE, THD_ROUTE_TIME);
+ build_third_route (route);
return route;
}
diff --git a/tests/test-helper.h b/tests/test-helper.h
index 9262ce7..a66ec90 100644
--- a/tests/test-helper.h
+++ b/tests/test-helper.h
@@ -69,6 +69,10 @@ TrpRoute *create_first_service_route (TrpServiceNavigation *nav);
TrpRoute *create_second_service_route (TrpServiceNavigation *nav);
TrpRoute *create_third_service_route (TrpServiceNavigation *nav);
+void build_first_route (TrpRoute *route);
+void build_second_route (TrpRoute *route);
+void build_third_route (TrpRoute *route);
+
G_END_DECLS
#endif /* __TRAPRAIN_TEST_HELPER_H__ */
diff --git a/tests/test-mock-service.c b/tests/test-mock-service.c
index 90a8910..129221b 100644
--- a/tests/test-mock-service.c
+++ b/tests/test-mock-service.c
@@ -13,6 +13,8 @@
#include <gio/gio.h>
#include <glib.h>
+#include "test-helper.h"
+
#include "traprain-client/mock.h"
#include "traprain-client/navigation.h"
@@ -21,6 +23,8 @@ typedef struct
GTestDBus *dbus;
GMainLoop *loop;
GError *error;
+ /* Index returned by trp_client_mock_add_route_finish() */
+ gint index;
TrpClientNavigation *client;
TrpClientMock *mock;
@@ -64,18 +68,24 @@ client_init_cb (GObject *source,
g_main_loop_quit (test->loop);
}
-/* Test activation of the service using TrpClientNavigation */
static void
-test_activation_client_service (Test *test,
- gconstpointer unused)
+init_client (Test *test)
{
- GPtrArray *routes;
-
g_async_initable_init_async (G_ASYNC_INITABLE (test->client),
G_PRIORITY_DEFAULT, NULL,
client_init_cb, test);
g_main_loop_run (test->loop);
g_assert_no_error (test->error);
+}
+
+/* Test activation of the service using TrpClientNavigation */
+static void
+test_activation_client_service (Test *test,
+ gconstpointer unused)
+{
+ GPtrArray *routes;
+
+ init_client (test);
routes = trp_client_navigation_get_routes (test->client);
g_assert_cmpuint (routes->len, ==, 0);
@@ -93,10 +103,8 @@ mock_new_cb (GObject *source,
g_main_loop_quit (test->loop);
}
-/* Test activation of the service using TrpClientMock */
static void
-test_activation_client_mock (Test *test,
- gconstpointer unused)
+init_mock (Test *test)
{
trp_client_mock_new_async (NULL, mock_new_cb, test);
g_main_loop_run (test->loop);
@@ -104,6 +112,52 @@ test_activation_client_mock (Test *test,
g_assert (TRP_CLIENT_IS_MOCK (test->mock));
}
+/* Test activation of the service using TrpClientMock */
+static void
+test_activation_client_mock (Test *test,
+ gconstpointer unused)
+{
+ init_mock (test);
+}
+
+static void
+add_route_cb (GObject *source,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ Test *test = user_data;
+
+ test->index = trp_client_mock_add_route_finish (TRP_CLIENT_MOCK (source),
+ result, &test->error);
+ g_main_loop_quit (test->loop);
+}
+
+static TrpRoute *
+create_first_mock_route (Test *test)
+{
+ TrpRoute *route;
+
+ route = trp_client_mock_create_route (test->mock, FST_ROUTE_DISTANCE, FST_ROUTE_TIME);
+ build_first_route (route);
+
+ return route;
+}
+
+/* Test adding a route */
+static void
+test_add_route (Test *test,
+ gconstpointer unused)
+{
+ init_client (test);
+ init_mock (test);
+
+ trp_client_mock_add_route_async (test->mock, create_first_mock_route (test), add_route_cb, test);
+ g_main_loop_run (test->loop);
+ /* Not yet implemented */
+ g_assert_error (test->error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
+ g_assert_cmpint (test->index, ==, -1);
+}
+
int
main (int argc, char **argv)
{
@@ -113,6 +167,8 @@ main (int argc, char **argv)
setup, test_activation_client_service, teardown);
g_test_add ("/mock-service/activation/TrpClientMock", Test, NULL,
setup, test_activation_client_mock, teardown);
+ g_test_add ("/mock-service/add-route", Test, NULL,
+ setup, test_add_route, teardown);
return g_test_run ();
}
diff --git a/traprain-client/mock.c b/traprain-client/mock.c
index 7fa5494..69f9dfd 100644
--- a/traprain-client/mock.c
+++ b/traprain-client/mock.c
@@ -13,6 +13,7 @@
#include "mock.h"
#include "dbus/org.apertis.Traprain1.Mock.h"
+#include "traprain-common/route-internal.h"
#define MOCK_BUS_NAME "org.apertis.Traprain1.Mock"
#define NAVIGATION_ROUTES_PATH "/org/apertis/Navigation1/Routes"
@@ -279,6 +280,29 @@ trp_client_mock_init_finish (GAsyncInitable *initable,
return g_task_propagate_boolean (G_TASK (result), error);
}
+/**
+ * trp_client_mock_create_route:
+ * @total_distance: the total distance covered by this route, in metres
+ * @total_time: an estimation of the time needed to travel this route, in seconds
+ *
+ * Create a new TrpRoute representing a potential navigation route.
+ *
+ * One should generally create a route, set its title using trp_route_add_title()
+ * and add the geometry of the route using trp_route_add_segment().
+ * Once the route has been fully defined it should be passed to the #TrpClientMock
+ * for publishing using trp_client_mock_add_route_async().
+ *
+ * Returns: (transfer full): a new #TrpRoute
+ * Since: UNRELEASED
+ */
+TrpRoute *
+trp_client_mock_create_route (TrpClientMock *self,
+ guint total_distance,
+ guint total_time)
+{
+ return _trp_route_new (total_distance, total_time);
+}
+
static void
async_initable_iface_init (gpointer g_iface,
gpointer data)
@@ -288,3 +312,105 @@ async_initable_iface_init (gpointer g_iface,
iface->init_async = trp_client_mock_init_async;
iface->init_finish = trp_client_mock_init_finish;
}
+
+static gboolean
+check_invalidated (TrpClientMock *self,
+ GError **error)
+{
+ if (self->error == NULL)
+ return TRUE;
+
+ if (error != NULL)
+ *error = g_error_copy (self->error);
+
+ return FALSE;
+}
+
+static void
+add_route_cb (GObject *source,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ g_autoptr (GTask) task = user_data;
+ guint index;
+ GError *error = NULL;
+
+ if (!trp_1_mock_call_add_route_finish ((Trp1Mock *) source, &index, result, &error))
+ {
+ g_task_return_error (task, error);
+ return;
+ }
+
+ g_task_return_int (task, index);
+}
+
+/**
+ * trp_client_mock_add_route_async:
+ * @self: a #TrpClientMock
+ * @route: a #TrpRoute
+ * @callback: callback to call when the request is satisfied.
+ * @user_data: the data to pass to @callback function.
+ *
+ * Add @route to the list of routes exposed by the mock service.
+ * @route should contain at least two segments: the start and destination of
+ * the route.
+ *
+ * When the operation is finished, @callback will be called.
+ * You can then call trp_client_mock_add_route_finish() to get the result of
+ * the operation.
+ *
+ * Since: UNRELEASED
+ */
+void
+trp_client_mock_add_route_async (TrpClientMock *self,
+ TrpRoute *route,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ g_autoptr (GTask) task = NULL;
+ g_autoptr (TrpRoute) _route = route; /* transfer */
+ GError *error = NULL;
+ GVariant *title, *geometry, *geometry_desc;
+
+ g_return_if_fail (TRP_CLIENT_IS_MOCK (self));
+ g_return_if_fail (TRP_IS_ROUTE (route));
+ g_return_if_fail (route->geometry->len >= 2);
+
+ task = g_task_new (self, self->cancellable, callback, user_data);
+
+ if (!check_invalidated (self, &error))
+ {
+ g_task_return_error (task, error);
+ return;
+ }
+
+ g_assert (self->proxy != NULL);
+
+ _trp_route_to_variants (_route, &title, &geometry, &geometry_desc);
+
+ trp_1_mock_call_add_route (self->proxy, title, route->total_distance,
+ route->total_time, geometry, geometry_desc,
+ self->cancellable, add_route_cb, g_steal_pointer (&task));
+}
+
+/**
+ * trp_client_mock_add_route_finish:
+ * @self: a #TrpClientMock
+ * @result: a #GAsyncResult
+ * @error: a #GError, or %NULL
+ *
+ * Finishes an operation started with trp_client_mock_add_route_async().
+ *
+ * Returns: the index of the newly added route, or -1 on error
+ * Since: UNRELEASED
+ */
+gint
+trp_client_mock_add_route_finish (TrpClientMock *self,
+ GAsyncResult *result,
+ GError **error)
+{
+ g_return_val_if_fail (g_task_is_valid (result, self), -1);
+ g_return_val_if_fail (error == NULL || *error == NULL, -1);
+
+ return g_task_propagate_int (G_TASK (result), error);
+}
diff --git a/traprain-client/mock.h b/traprain-client/mock.h
index bdd6fb5..247f37a 100644
--- a/traprain-client/mock.h
+++ b/traprain-client/mock.h
@@ -14,6 +14,8 @@
#include <gio/gio.h>
#include <glib-object.h>
+#include "traprain-common/route.h"
+
G_BEGIN_DECLS
#define TRP_CLIENT_TYPE_MOCK (trp_client_mock_get_type ())
@@ -26,6 +28,19 @@ void trp_client_mock_new_async (GCancellable *cancellable,
TrpClientMock *trp_client_mock_new_finish (GAsyncResult *result,
GError **error);
+TrpRoute *trp_client_mock_create_route (TrpClientMock *self,
+ guint total_distance,
+ guint total_time);
+
+void trp_client_mock_add_route_async (TrpClientMock *self,
+ TrpRoute *route,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+
+gint trp_client_mock_add_route_finish (TrpClientMock *self,
+ GAsyncResult *result,
+ GError **error);
+
G_END_DECLS
#endif /* __TRAPRAIN_CLIENT_MOCK_H__ */
diff --git a/traprain-common/route-internal.h b/traprain-common/route-internal.h
index 07c07fb..243dccf 100644
--- a/traprain-common/route-internal.h
+++ b/traprain-common/route-internal.h
@@ -38,6 +38,9 @@ struct _TrpRoute
gboolean writable;
};
+TrpRoute *_trp_route_new (guint total_distance,
+ guint total_time);
+
void _trp_route_to_variants (TrpRoute *route,
GVariant **title,
GVariant **geometry,
diff --git a/traprain-common/route.c b/traprain-common/route.c
index 6e95dc8..1f6a97e 100644
--- a/traprain-common/route.c
+++ b/traprain-common/route.c
@@ -81,6 +81,19 @@ trp_route_class_init (TrpRouteClass *klass)
object_class->dispose = trp_route_dispose;
}
+TrpRoute *
+_trp_route_new (guint total_distance,
+ guint total_time)
+{
+ TrpRoute *route;
+
+ route = g_object_new (TRP_TYPE_ROUTE, NULL);
+ route->total_distance = total_distance;
+ route->total_time = total_time;
+
+ return route;
+}
+
/**
* trp_route_add_title:
* @route: a #TrpRoute