Back to index

unity  6.0.0
test_gdbus_proxy.cpp
Go to the documentation of this file.
00001 #include <gtest/gtest.h>
00002 #include <glib-object.h>
00003 #include <UnityCore/GLibWrapper.h>
00004 #include <UnityCore/GLibDBusProxy.h>
00005 
00006 using namespace std;
00007 using namespace unity;
00008 
00009 namespace
00010 {
00011 
00012 GMainLoop* loop_ = NULL;
00013 glib::DBusProxy* proxy = NULL;
00014 
00015 class TestGDBusProxy: public ::testing::Test
00016 {
00017 public:
00018   TestGDBusProxy()
00019     : connected_result(false)
00020     , got_signal_return(false)
00021     , got_result_return(false)
00022   {
00023   }
00024   bool connected_result;
00025   bool got_signal_return;
00026   bool got_result_return;
00027 };
00028 
00029 TEST_F(TestGDBusProxy, TestConstruction)
00030 {
00031   loop_ = g_main_loop_new(NULL, FALSE);
00032   proxy = new glib::DBusProxy("com.canonical.Unity.Test", 
00033                               "/com/canonical/gdbus_wrapper", 
00034                               "com.canonical.gdbus_wrapper");
00035   // performs a check on the proxy, if the proxy is connected, report a sucess
00036   auto timeout_check = [] (gpointer data) -> gboolean
00037   {
00038     TestGDBusProxy* self = static_cast<TestGDBusProxy*>(data);
00039     if (proxy->IsConnected())
00040     {
00041       self->connected_result = true;
00042       g_main_loop_quit(loop_);
00043       return FALSE;
00044     }
00045     else
00046     {
00047       self->connected_result = false;
00048       return TRUE;
00049     }
00050   };
00051   
00052 
00053   // if the proxy is not connected when this lambda runs, fail.
00054   auto timeout_bailout = [] (gpointer data) -> gboolean 
00055   {
00056     TestGDBusProxy* self = static_cast<TestGDBusProxy*>(data);
00057     // reached timeout, failed testing
00058     self->connected_result = false;
00059     g_main_loop_quit(loop_);
00060     return FALSE;
00061   };
00062   
00063   guint timeout_source = g_timeout_add(1000, timeout_check, this); // check once a second
00064   guint bailout_source = g_timeout_add(10000, timeout_bailout, this); // bail out after ten
00065 
00066   g_main_loop_run(loop_);
00067   g_source_remove(timeout_source);
00068   g_source_remove(bailout_source);
00069   
00070   EXPECT_EQ(connected_result, true);
00071 }
00072 
00073 TEST_F(TestGDBusProxy, TestMethodReturn)
00074 {
00075   // Our service is setup so that if you call the TestMethod method, it will emit the TestSignal method
00076   // with whatever string you pass in
00077   gchar* expected_return = (gchar *)"TestStringTestString☻☻☻"; // cast to get gcc to shut up
00078   gchar* returned_result = g_strdup("Not equal"); 
00079   gchar* returned_signal = g_strdup("Not equal"); 
00080 
00081   GVariant* param_value = g_variant_new_string(expected_return);
00082   GVariant* parameters = g_variant_new_tuple(&param_value, 1);
00083   // signal callback
00084   auto signal_connection = [&](GVariant *variant)
00085   {
00086     if (variant != nullptr)
00087     {
00088       g_free(returned_signal);
00089       returned_signal = g_strdup(g_variant_get_string(g_variant_get_child_value(variant, 0), NULL));
00090     }
00091 
00092     got_signal_return = true;
00093     if (got_signal_return && got_result_return)
00094       g_main_loop_quit(loop_);
00095   };
00096 
00097   // method callback
00098   auto method_connection = [&](GVariant *variant)
00099   {
00100     if (variant != nullptr)
00101     {
00102       g_free(returned_result);
00103       returned_result = g_strdup(g_variant_get_string(g_variant_get_child_value(variant, 0), NULL));
00104     }
00105 
00106     got_result_return = true;
00107     if (got_signal_return && got_result_return)
00108       g_main_loop_quit(loop_);
00109   };
00110   
00111   auto timeout_bailout = [] (gpointer data) -> gboolean // bail out after 10 seconds
00112   {
00113     g_main_loop_quit(loop_);
00114     return FALSE;
00115   };
00116    
00117   guint bailout_source = g_timeout_add(10000, timeout_bailout, this);
00118 
00119   EXPECT_EQ(proxy->IsConnected(), true); // fail if we are not connected
00120   proxy->Connect("TestSignal", signal_connection);
00121   proxy->Call("TestMethod", parameters, method_connection); 
00122 
00123  
00124   // next check we get 30 entries from this specific known callback
00125   g_main_loop_run(loop_);
00126 
00127   EXPECT_EQ(g_strcmp0(expected_return, returned_result), 0);
00128   EXPECT_EQ(g_strcmp0(expected_return, returned_signal), 0);
00129 
00130   g_free(returned_result);
00131   g_free(returned_signal);
00132   g_source_remove(bailout_source);
00133 }
00134 
00135 
00136 }