1
#include <gtest/gtest.h>
2
#include <glib-object.h>
3
#include <UnityCore/GLibWrapper.h>
4
#include <UnityCore/GLibDBusProxy.h>
12
GMainLoop* loop_ = g_main_loop_new(NULL, FALSE);
13
glib::DBusProxy proxy("com.canonical.Unity.Test",
14
"/com/canonical/gdbus_wrapper",
15
"com.canonical.gdbus_wrapper");
17
class TestGDBusProxy: public ::testing::Test
21
: connected_result(false)
24
bool connected_result;
27
TEST_F(TestGDBusProxy, TestConstruction)
30
// performs a check on the proxy, if the proxy is connected, report a sucess
31
auto timeout_check = [] (gpointer data) -> gboolean
33
TestGDBusProxy* self = static_cast<TestGDBusProxy*>(data);
34
if (proxy.IsConnected())
36
self->connected_result = true;
37
g_main_loop_quit(loop_);
42
self->connected_result = false;
48
// if the proxy is not connected when this lambda runs, fail.
49
auto timeout_bailout = [] (gpointer data) -> gboolean
51
TestGDBusProxy* self = static_cast<TestGDBusProxy*>(data);
52
// reached timeout, failed testing
53
self->connected_result = false;
54
g_main_loop_quit(loop_);
58
g_timeout_add_seconds(1, timeout_check, this); // check once a second
59
g_timeout_add_seconds(10, timeout_bailout, this); // bail out after ten
61
g_main_loop_run(loop_);
63
EXPECT_EQ(connected_result, true);
66
TEST_F(TestGDBusProxy, TestMethodReturn)
68
glib::String expected_return((gchar *)"TestStringTestString☻☻☻"); // cast to get gcc to shut up
69
gchar* returned_result = (gchar*)"Not equal"; // cast to get gcc to shut up
70
gchar* returned_signal = (gchar*)"Not equal"; // cast to get gcc to shut up
72
GVariant* param_value = g_variant_new_string(expected_return);
73
GVariant* parameters = g_variant_new_tuple(¶m_value, 1);
75
auto signal_connection = [&](GVariant *variant)
77
if (variant != nullptr)
79
returned_signal = (gchar*)g_variant_get_string(g_variant_get_child_value(variant, 0), NULL);
84
auto method_connection = [&](GVariant *variant)
86
if (variant != nullptr)
88
returned_result = (gchar*)g_variant_get_string(g_variant_get_child_value(variant, 0), NULL);
92
auto timeout_bailout = [] (gpointer data) -> gboolean // bail out after 10 seconds
94
g_main_loop_quit(loop_);
98
g_timeout_add_seconds(10, timeout_bailout, this);
100
EXPECT_EQ(proxy.IsConnected(), true); // fail if we are not connected
101
proxy.Connect("TestSignal", signal_connection);
102
proxy.Call("TestMethod", parameters, method_connection);
105
// next check we get 30 entries from this specific known callback
106
g_main_loop_run(loop_);
108
EXPECT_EQ(g_strcmp0(expected_return, returned_result), 0);
109
EXPECT_EQ(g_strcmp0(expected_return, returned_signal), 0);