Back to index

unity  6.0.0
test_hud.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/Hud.h>
00005 #include <sigc++/connection.h>
00006 
00007 using namespace std;
00008 
00009 namespace
00010 {
00011 
00012 GMainLoop* loop_ = NULL;
00013 unity::hud::Hud *hud;
00014 
00015 class TestHud : public ::testing::Test
00016 {
00017 public:
00018   TestHud()
00019     : query_return_result(false)
00020     , connected_result(false)
00021   {
00022   }
00023   unity::hud::Hud::Queries queries;
00024   bool query_return_result;
00025   bool connected_result;
00026   int number_signals_found;
00027 };
00028 
00029 TEST_F(TestHud, TestConstruction)
00030 {
00031   loop_ = g_main_loop_new(NULL, FALSE);
00032   hud = new unity::hud::Hud("com.canonical.Unity.Test", "/com/canonical/hud");
00033   
00034   // performs a check on the hud, if the hud is connected, report a sucess
00035   auto timeout_check = [] (gpointer data) -> gboolean
00036   {
00037     TestHud* self = static_cast<TestHud*>(data);
00038     if (hud->connected)
00039     {
00040       self->connected_result = true;
00041       g_main_loop_quit(loop_);
00042       return FALSE;
00043     }
00044     else
00045     {
00046       self->connected_result = false;
00047       return TRUE;
00048     }
00049   };
00050   
00051 
00052   // if the hud is not connected when this lambda runs, fail.
00053   auto timeout_bailout = [] (gpointer data) -> gboolean 
00054   {
00055     TestHud* self = static_cast<TestHud*>(data);
00056     // reached timeout, failed testing
00057     self->connected_result = false;
00058     g_main_loop_quit(loop_);
00059     return FALSE;
00060   };
00061   
00062   g_timeout_add(1000, timeout_check, this);
00063   g_timeout_add(10000, timeout_bailout, this);
00064 
00065   g_main_loop_run(loop_);
00066   
00067   EXPECT_EQ(connected_result, true);
00068 }
00069 
00070 TEST_F(TestHud, TestQueryReturn)
00071 {
00072   query_return_result = false;
00073   
00074   // make sure we receive the queries
00075   auto query_connection = [this](unity::hud::Hud::Queries queries_) 
00076   { 
00077     query_return_result = true;
00078     g_main_loop_quit(loop_);
00079     queries = queries_;
00080   };
00081 
00082   auto timeout_bailout = [] (gpointer data) -> gboolean
00083   {
00084     TestHud* self = static_cast<TestHud*>(data);
00085     self->query_return_result = false;
00086     g_main_loop_quit(loop_);
00087     return FALSE;
00088   };
00089    
00090   sigc::connection connection = hud->queries_updated.connect(query_connection);
00091  
00092   guint source_id = g_timeout_add(10000, timeout_bailout, this);
00093  
00094   // next check we get 30 entries from this specific known callback
00095   hud->RequestQuery("Request30Queries");
00096   g_main_loop_run(loop_);
00097   EXPECT_EQ(query_return_result, true);
00098   EXPECT_NE(queries.size(), 0);
00099   g_source_remove(source_id);
00100 
00101   // finally close the connection - Nothing to check for here
00102   hud->CloseQuery();
00103   connection.disconnect();
00104 }
00105 
00106 TEST_F(TestHud, TestSigEmission)
00107 {
00108   // checks that the signal emission from Hud is working correctly
00109   // the service is setup to emit the same signal every 1000ms
00110   // using the same query key as its StarQuery method
00111   // so calling StartQuery and listening we expect > 1 
00112   // signal emission
00113   number_signals_found = 0;
00114 
00115   // make sure we receive the queries
00116   auto query_connection = [this](unity::hud::Hud::Queries queries_) 
00117   { 
00118     number_signals_found += 1;
00119   };
00120 
00121   auto timeout_bailout = [] (gpointer data) -> gboolean
00122   {
00123     g_main_loop_quit(loop_);
00124     return FALSE;
00125   };
00126    
00127   sigc::connection connection = hud->queries_updated.connect(query_connection);
00128  
00129   hud->RequestQuery("Request30Queries");
00130   guint source_id = g_timeout_add(10000, timeout_bailout, this);
00131  
00132   g_main_loop_run(loop_);
00133   EXPECT_GT(number_signals_found, 1);
00134   g_source_remove(source_id);
00135 
00136   // finally close the connection - Nothing to check for here
00137   hud->CloseQuery();
00138   connection.disconnect();
00139   
00140 }
00141 
00142 }