diff options
author | Guillaume Desmottes <guillaume.desmottes@collabora.co.uk> | 2016-09-05 08:46:41 (GMT) |
---|---|---|
committer | Guillaume Desmottes <guillaume.desmottes@collabora.co.uk> | 2016-09-06 13:44:28 (GMT) |
commit | 797551569ff9470254f0126180c7f86342c067ec (patch) | |
tree | e6d9ef228cc50746e832c0a187648d70d0fe49d5 | |
parent | 3224349b3ebde7df19fc33784bef730e02213306 (diff) | |
download | traprain-797551569ff9470254f0126180c7f86342c067ec.tar.gz traprain-797551569ff9470254f0126180c7f86342c067ec.tar.xz |
mock-service: implement RemoveRoute()
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/D4183
-rw-r--r-- | mock-service/mock-service.c | 31 | ||||
-rw-r--r-- | tests/test-mock-service.c | 67 | ||||
-rw-r--r-- | traprain-client/mock.c | 81 | ||||
-rw-r--r-- | traprain-client/mock.h | 9 |
4 files changed, 188 insertions, 0 deletions
diff --git a/mock-service/mock-service.c b/mock-service/mock-service.c index 6e21c66..51f4aa7 100644 --- a/mock-service/mock-service.c +++ b/mock-service/mock-service.c @@ -172,6 +172,35 @@ error: } static void +on_handle_remove_route (Trp1Mock *mock, + GDBusMethodInvocation *invocation, + guint index, + gpointer user_data) +{ + TrpMockService *self = user_data; + GError *error = NULL; + guint n_routes; + + n_routes = trp_service_navigation_get_n_routes (self->service); + if (index >= n_routes) + { + g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, + G_DBUS_ERROR_INVALID_ARGS, + "Invalid index, service contains only %d routes", + n_routes); + return; + } + + if (!trp_service_navigation_remove_route (self->service, index, &error)) + { + g_dbus_method_invocation_take_error (invocation, error); + return; + } + + trp_1_mock_complete_remove_route (mock, invocation); +} + +static void trp_mock_service_init (TrpMockService *self) { self->service = trp_service_navigation_new (); @@ -181,6 +210,8 @@ trp_mock_service_init (TrpMockService *self) g_signal_connect (self->mock, "handle-add-route", G_CALLBACK (on_handle_add_route), self); + g_signal_connect (self->mock, "handle-remove-route", + G_CALLBACK (on_handle_remove_route), self); } TrpMockService * diff --git a/tests/test-mock-service.c b/tests/test-mock-service.c index abba6ad..6d25f31 100644 --- a/tests/test-mock-service.c +++ b/tests/test-mock-service.c @@ -26,6 +26,7 @@ typedef struct GError *error; /* Index returned by trp_client_mock_add_route_finish() */ gint index; + gboolean remove_result; TrpClientNavigation *client; TrpClientMock *mock; @@ -203,6 +204,70 @@ test_add_route (Test *test, check_second_client_route (g_ptr_array_index (routes, 1)); } +static void +remove_route_cb (GObject *source, + GAsyncResult *result, + gpointer user_data) +{ + Test *test = user_data; + + test->remove_result = trp_client_mock_remove_route_finish (TRP_CLIENT_MOCK (source), + result, &test->error); + + test->wait_count--; + if (test->wait_count == 0) + g_main_loop_quit (test->loop); +} + +/* Try removing routes from the mock service */ +static void +test_remove_route (Test *test, + gconstpointer unused) +{ + GPtrArray *routes; + + init_client (test); + init_mock (test); + + /* Add 2 routes */ + test->wait_count = 2; + g_signal_connect (test->client, "notify::routes", G_CALLBACK (client_routes_cb), test); + trp_client_mock_add_route_async (test->mock, create_first_mock_route (test), add_route_cb, test); + g_main_loop_run (test->loop); + g_assert_no_error (test->error); + g_assert_cmpint (test->index, ==, 0); + + /* Add second route */ + test->wait_count = 2; + trp_client_mock_add_route_async (test->mock, create_second_mock_route (test), add_route_cb, test); + g_main_loop_run (test->loop); + g_assert_no_error (test->error); + g_assert_cmpint (test->index, ==, 1); + + routes = trp_client_navigation_get_routes (test->client); + g_assert_cmpuint (routes->len, ==, 2); + check_first_client_route (g_ptr_array_index (routes, 0)); + check_second_client_route (g_ptr_array_index (routes, 1)); + + /* Remove the second route */ + test->wait_count = 2; + trp_client_mock_remove_route_async (test->mock, 1, remove_route_cb, test); + g_main_loop_run (test->loop); + g_assert_no_error (test->error); + g_assert (test->remove_result); + + routes = trp_client_navigation_get_routes (test->client); + g_assert_cmpuint (routes->len, ==, 1); + check_first_client_route (g_ptr_array_index (routes, 0)); + + /* Try removing an invalid route */ + test->wait_count = 1; + trp_client_mock_remove_route_async (test->mock, 10, remove_route_cb, test); + g_main_loop_run (test->loop); + g_assert_error (test->error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS); + g_assert (!test->remove_result); +} + int main (int argc, char **argv) { @@ -214,6 +279,8 @@ main (int argc, char **argv) setup, test_activation_client_mock, teardown); g_test_add ("/mock-service/add-route", Test, NULL, setup, test_add_route, teardown); + g_test_add ("/mock-service/remove-route", Test, NULL, + setup, test_remove_route, teardown); return g_test_run (); } diff --git a/traprain-client/mock.c b/traprain-client/mock.c index 69f9dfd..fb396e4 100644 --- a/traprain-client/mock.c +++ b/traprain-client/mock.c @@ -414,3 +414,84 @@ trp_client_mock_add_route_finish (TrpClientMock *self, return g_task_propagate_int (G_TASK (result), error); } + +static void +remove_route_cb (GObject *source, + GAsyncResult *result, + gpointer user_data) +{ + g_autoptr (GTask) task = user_data; + GError *error = NULL; + + if (!trp_1_mock_call_remove_route_finish ((Trp1Mock *) source, result, &error)) + { + g_task_return_error (task, error); + return; + } + + g_task_return_boolean (task, TRUE); +} + +/** + * trp_client_mock_remove_route_async: + * @self: a #TrpClientMock + * @index: the index of the route to remove + * @callback: callback to call when the request is satisfied. + * @user_data: the data to pass to @callback function. + * + * Remove the route at index @index from the list of routes exposed by the + * mock service. + * + * When the operation is finished, @callback will be called. + * You can then call trp_client_mock_remove_route_finish() to get the result of + * the operation. + * + * Since: UNRELEASED + */ +void +trp_client_mock_remove_route_async (TrpClientMock *self, + guint index, + GAsyncReadyCallback callback, + gpointer user_data) +{ + g_autoptr (GTask) task = NULL; + GError *error = NULL; + + g_return_if_fail (TRP_CLIENT_IS_MOCK (self)); + + 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_1_mock_call_remove_route (self->proxy, index, self->cancellable, + remove_route_cb, g_steal_pointer (&task)); +} + +/** + * trp_client_mock_remove_route_finish: + * @self: a #TrpClientMock + * @result: a #GAsyncResult + * @error: a #GError, or %NULL + * + * Finishes an operation started with trp_client_mock_remove_route_async(). + * + * Returns: %TRUE if the operation succeeded, %FALSE on error + * Since: UNRELEASED + */ +gboolean +trp_client_mock_remove_route_finish (TrpClientMock *self, + GAsyncResult *result, + GError **error) +{ + g_return_val_if_fail (g_task_is_valid (result, self), FALSE); + g_assert_no_error (*error); + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + + return g_task_propagate_boolean (G_TASK (result), error); +} diff --git a/traprain-client/mock.h b/traprain-client/mock.h index 247f37a..f7849db 100644 --- a/traprain-client/mock.h +++ b/traprain-client/mock.h @@ -41,6 +41,15 @@ gint trp_client_mock_add_route_finish (TrpClientMock *self, GAsyncResult *result, GError **error); +void trp_client_mock_remove_route_async (TrpClientMock *self, + guint index, + GAsyncReadyCallback callback, + gpointer user_data); + +gboolean trp_client_mock_remove_route_finish (TrpClientMock *self, + GAsyncResult *result, + GError **error); + G_END_DECLS #endif /* __TRAPRAIN_CLIENT_MOCK_H__ */ |