Back to index

unity  6.0.0
test_glib_signals.cpp
Go to the documentation of this file.
00001 #include <UnityCore/GLibSignal.h>
00002 #include <gtest/gtest.h>
00003 
00004 #include "test_glib_signals_utils.h"
00005 
00006 using namespace std;
00007 using namespace unity;
00008 using namespace unity::glib;
00009 
00010 namespace
00011 {
00012 
00013 class TestGLibSignals : public testing::Test
00014 {
00015 public:
00016   TestGLibSignals()
00017     : signal0_received_(false)
00018     , signal1_received_(false)
00019     , signal2_received_(false)
00020     , signal3_received_(false)
00021     , signal4_received_(false)
00022     , signal5_received_(false)
00023     , signal6_received_(false)
00024     , arg1_("")
00025     , arg2_(-1)
00026     , arg3_(0.0f)
00027     , arg4_(0.00)
00028     , arg5_(false)
00029     , arg6_(0)
00030   {
00031     test_signals_ = static_cast<TestSignals*>(g_object_new(TEST_TYPE_SIGNALS, NULL));
00032   }
00033 
00034   virtual ~TestGLibSignals()
00035   {
00036     if (G_IS_OBJECT(test_signals_))
00037       g_object_unref(test_signals_);
00038   }
00039 
00040   void Signal0Callback(TestSignals* signals)
00041   {
00042     signal0_received_ = true;
00043   }
00044 
00045   void Signal1Callback(TestSignals* signals, const char* str)
00046   {
00047     arg1_ = str;
00048     signal1_received_ = true;
00049   }
00050 
00051   void Signal2Callback(TestSignals* signals, const char* str, int i)
00052   {
00053     arg1_ = str;
00054     arg2_ = i;
00055     signal2_received_ = true;
00056   }
00057 
00058   void Signal3Callback(TestSignals* signals, const char* str, int i, float f)
00059   {
00060     arg1_ = str;
00061     arg2_ = i;
00062     arg3_ = f;
00063     signal3_received_ = true;
00064   }
00065 
00066   void Signal4Callback(TestSignals* signals, const char* str, int i, float f, double d) // heh
00067   {
00068     arg1_ = str;
00069     arg2_ = i;
00070     arg3_ = f;
00071     arg4_ = d;
00072     signal4_received_ = true;
00073   }
00074 
00075   void Signal5Callback(TestSignals* signals, const char* str, int i, float f,
00076                        double d, gboolean b)
00077   {
00078     arg1_ = str;
00079     arg2_ = i;
00080     arg3_ = f;
00081     arg4_ = d;
00082     arg5_ = b ? true : false;
00083     signal5_received_ = true;
00084   }
00085 
00086   gboolean Signal6Callback(TestSignals* signals, const char* str, int i, float f,
00087                            double d, gboolean b, char c)
00088   {
00089     arg1_ = str;
00090     arg2_ = i;
00091     arg3_ = f;
00092     arg4_ = d;
00093     arg5_ = b ? true : false;
00094     arg6_ = c;
00095     signal6_received_ = true;
00096     return TRUE;
00097   }
00098 
00099 protected:
00100   TestSignals* test_signals_;
00101 
00102   bool signal0_received_;
00103   bool signal1_received_;
00104   bool signal2_received_;
00105   bool signal3_received_;
00106   bool signal4_received_;
00107   bool signal5_received_;
00108   bool signal6_received_;
00109 
00110   string arg1_;
00111   int arg2_;
00112   float arg3_;
00113   double arg4_;
00114   bool arg5_;
00115   char arg6_;
00116 };
00117 
00118 class MockSignalManager : public SignalManager
00119 {
00120 public:
00121   std::vector<SignalBase::Ptr> GetConnections() const { return connections_; }
00122 };
00123 
00124 
00125 TEST_F(TestGLibSignals, TestConstructions)
00126 {
00127   Signal<void, TestSignals*> signal0;
00128   Signal<void, TestSignals*, string> signal1;
00129   Signal<void, TestSignals*, string, int> signal2;
00130   Signal<void, TestSignals*, string, int, float> signal3;
00131   Signal<void, TestSignals*, string, int, float, double> signal4;
00132   Signal<void, TestSignals*, string, int, float, double, gboolean> signal5;
00133   Signal<gboolean, TestSignals*, string, int, float, double, gboolean, char> signal6;
00134 }
00135 
00136 TEST_F(TestGLibSignals, TestSignal0)
00137 {
00138   Signal<void, TestSignals*> signal;
00139   signal.Connect(test_signals_, "signal0",
00140                  sigc::mem_fun(this, &TestGLibSignals::Signal0Callback));
00141 
00142   g_signal_emit_by_name(test_signals_, "signal0");
00143 
00144   EXPECT_TRUE(signal0_received_);
00145   EXPECT_EQ(arg1_, "");
00146 }
00147 
00148 TEST_F(TestGLibSignals, TestSignal1)
00149 {
00150   Signal<void, TestSignals*, const char*> signal;
00151   signal.Connect(test_signals_, "signal1",
00152                  sigc::mem_fun(this, &TestGLibSignals::Signal1Callback));
00153 
00154   g_signal_emit_by_name(test_signals_, "signal1", "test");
00155 
00156   EXPECT_TRUE(signal1_received_);
00157   EXPECT_EQ(arg1_, "test");
00158   EXPECT_EQ(arg2_, -1);
00159 }
00160 
00161 TEST_F(TestGLibSignals, TestSignal2)
00162 {
00163   Signal<void, TestSignals*, const char*, int> signal;
00164   signal.Connect(test_signals_, "signal2",
00165                  sigc::mem_fun(this, &TestGLibSignals::Signal2Callback));
00166 
00167   g_signal_emit_by_name(test_signals_, "signal2", "test", 100);
00168 
00169   EXPECT_TRUE(signal2_received_);
00170   EXPECT_EQ(arg1_, "test");
00171   EXPECT_EQ(arg2_, 100);
00172   EXPECT_EQ(arg3_, 0.0f);
00173 }
00174 
00175 TEST_F(TestGLibSignals, TestSignal3)
00176 {
00177   Signal<void, TestSignals*, const char*, int, float> signal;
00178   signal.Connect(test_signals_, "signal3",
00179                  sigc::mem_fun(this, &TestGLibSignals::Signal3Callback));
00180 
00181   g_signal_emit_by_name(test_signals_, "signal3", "test", 100, 200.0f);
00182 
00183   EXPECT_TRUE(signal3_received_);
00184   EXPECT_EQ(arg1_, "test");
00185   EXPECT_EQ(arg2_, 100);
00186   EXPECT_EQ(arg3_, 200.0f);
00187   EXPECT_EQ(arg4_, 0.00);
00188 }
00189 
00190 
00191 TEST_F(TestGLibSignals, TestSignal4)
00192 {
00193   Signal<void, TestSignals*, const char*, int, float, double> signal;
00194   signal.Connect(test_signals_, "signal4",
00195                  sigc::mem_fun(this, &TestGLibSignals::Signal4Callback));
00196 
00197   g_signal_emit_by_name(test_signals_, "signal4", "test", 100, 200.0f, 300.00);
00198 
00199   EXPECT_TRUE(signal4_received_);
00200   EXPECT_EQ(arg1_, "test");
00201   EXPECT_EQ(arg2_, 100);
00202   EXPECT_EQ(arg3_, 200.0f);
00203   EXPECT_EQ(arg4_, 300.00);
00204   EXPECT_EQ(arg5_, false);
00205 }
00206 
00207 TEST_F(TestGLibSignals, TestSignal5)
00208 {
00209   Signal<void, TestSignals*, const char*, int, float, double, gboolean> signal;
00210   signal.Connect(test_signals_, "signal5",
00211                  sigc::mem_fun(this, &TestGLibSignals::Signal5Callback));
00212 
00213   g_signal_emit_by_name(test_signals_, "signal5", "test", 100, 200.0f, 300.00,
00214                         TRUE);
00215 
00216   EXPECT_TRUE(signal5_received_);
00217   EXPECT_EQ(arg1_, "test");
00218   EXPECT_EQ(arg2_, 100);
00219   EXPECT_EQ(arg3_, 200.0f);
00220   EXPECT_EQ(arg4_, 300.00);
00221   EXPECT_EQ(arg5_, true);
00222   EXPECT_EQ(arg6_, 0);
00223 }
00224 
00225 // This tests accumulation as well
00226 TEST_F(TestGLibSignals, TestSignal6)
00227 {
00228   Signal<gboolean, TestSignals*, const char*, int, float, double, gboolean, char> signal;
00229   signal.Connect(test_signals_, "signal6",
00230                  sigc::mem_fun(this, &TestGLibSignals::Signal6Callback));
00231 
00232   gboolean ret = FALSE;
00233   g_signal_emit_by_name(test_signals_, "signal6", "test", 100, 200.0f, 300.00,
00234                         TRUE, 'x', &ret);
00235 
00236   EXPECT_TRUE(signal6_received_);
00237   EXPECT_EQ(arg1_, "test");
00238   EXPECT_EQ(arg2_, 100);
00239   EXPECT_EQ(arg3_, 200.0f);
00240   EXPECT_EQ(arg4_, 300.00);
00241   EXPECT_EQ(arg5_, true);
00242   EXPECT_EQ(arg6_, 'x');
00243   EXPECT_EQ(ret, TRUE);
00244 }
00245 
00246 TEST_F(TestGLibSignals, TestDisconnection)
00247 {
00248   Signal<void, TestSignals*> signal;
00249   signal.Connect(test_signals_, "signal0",
00250                  sigc::mem_fun(this, &TestGLibSignals::Signal0Callback));
00251   signal.Disconnect();
00252 
00253   g_signal_emit_by_name(test_signals_, "signal0");
00254 
00255   EXPECT_FALSE(signal0_received_);
00256 }
00257 
00258 TEST_F(TestGLibSignals, TestAutoDisconnection)
00259 {
00260   {
00261     Signal<void, TestSignals*> signal;
00262     signal.Connect(test_signals_, "signal0",
00263                    sigc::mem_fun(this, &TestGLibSignals::Signal0Callback));
00264   }
00265 
00266   g_signal_emit_by_name(test_signals_, "signal0");
00267 
00268   EXPECT_FALSE(signal0_received_);
00269 }
00270 
00271 TEST_F(TestGLibSignals, TestCleanDestruction)
00272 {
00273   Signal<void, TestSignals*> signal;
00274   signal.Connect(test_signals_, "signal0",
00275                  sigc::mem_fun(this, &TestGLibSignals::Signal0Callback));
00276   g_object_unref(test_signals_);
00277 }
00278 
00279 TEST_F(TestGLibSignals, TestManagerConstruction)
00280 {
00281   MockSignalManager manager;
00282   EXPECT_TRUE(manager.GetConnections().empty());
00283 }
00284 
00285 TEST_F(TestGLibSignals, TestManagerAddition)
00286 {
00287   MockSignalManager manager;
00288 
00289   manager.Add(new Signal<void, TestSignals*>(test_signals_,
00290                                              "signal0",
00291                                              sigc::mem_fun(this, &TestGLibSignals::Signal0Callback)));
00292   manager.Add(new Signal<void, TestSignals*, const char*>(test_signals_,
00293                                                           "signal1",
00294                                                           sigc::mem_fun(this, &TestGLibSignals::Signal1Callback)));
00295   manager.Add(new Signal<void, TestSignals*, const char*, int>(test_signals_,
00296                                                                "signal2",
00297                                                                sigc::mem_fun(this, &TestGLibSignals::Signal2Callback)));
00298   manager.Add(new Signal<void, TestSignals*, const char*, int, float>(test_signals_,
00299                                                                       "signal3",
00300                                                                       sigc::mem_fun(this, &TestGLibSignals::Signal3Callback)));
00301   manager.Add(new Signal<void, TestSignals*, const char*, int, float, double>(test_signals_,
00302                                                                               "signal4",
00303                                                                               sigc::mem_fun(this, &TestGLibSignals::Signal4Callback)));
00304   manager.Add(new Signal<void, TestSignals*, const char*, int, float, double, gboolean>(test_signals_,
00305               "signal5",
00306               sigc::mem_fun(this, &TestGLibSignals::Signal5Callback)));
00307   manager.Add(new Signal<gboolean, TestSignals*, const char*, int, float, double, gboolean, char>(test_signals_,
00308               "signal6",
00309               sigc::mem_fun(this, &TestGLibSignals::Signal6Callback)));
00310 
00311   EXPECT_EQ(manager.GetConnections().size(), 7);
00312 }
00313 
00314 TEST_F(TestGLibSignals, TestManagerAdditionTemplate)
00315 {
00316   MockSignalManager manager;
00317 
00318   manager.Add<void, TestSignals*>(test_signals_, "signal0",
00319                                   sigc::mem_fun(this, &TestGLibSignals::Signal0Callback));
00320   manager.Add<void, TestSignals*, const char*>(test_signals_, "signal1",
00321                                                sigc::mem_fun(this, &TestGLibSignals::Signal1Callback));
00322   manager.Add<void, TestSignals*, const char*, int>(test_signals_, "signal2",
00323                                                     sigc::mem_fun(this, &TestGLibSignals::Signal2Callback));
00324   manager.Add<void, TestSignals*, const char*, int, float>(test_signals_, "signal3",
00325                                                            sigc::mem_fun(this, &TestGLibSignals::Signal3Callback));
00326   manager.Add<void, TestSignals*, const char*, int, float, double>(test_signals_, "signal4",
00327                                                                    sigc::mem_fun(this, &TestGLibSignals::Signal4Callback));
00328   manager.Add<void, TestSignals*, const char*, int, float, double, gboolean>(test_signals_, "signal5",
00329                                                                              sigc::mem_fun(this, &TestGLibSignals::Signal5Callback));
00330   manager.Add<gboolean, TestSignals*, const char*, int, float, double, gboolean, char>(test_signals_, "signal6", sigc::mem_fun(this, &TestGLibSignals::Signal6Callback));
00331 
00332   EXPECT_EQ(manager.GetConnections().size(), 7);
00333 }
00334 
00335 TEST_F(TestGLibSignals, TestManagerConnection)
00336 {
00337   MockSignalManager manager;
00338 
00339   manager.Add(new Signal<void, TestSignals*>(test_signals_,
00340                                              "signal0",
00341                                              sigc::mem_fun(this, &TestGLibSignals::Signal0Callback)));
00342 
00343   g_signal_emit_by_name(test_signals_, "signal0");
00344   EXPECT_TRUE(signal0_received_);
00345 
00346   manager.Add(new Signal<void, TestSignals*, const char*>(test_signals_,
00347                                                           "signal1",
00348                                                           sigc::mem_fun(this, &TestGLibSignals::Signal1Callback)));
00349   g_signal_emit_by_name(test_signals_, "signal1", "test");
00350   EXPECT_TRUE(signal1_received_);
00351 
00352   gboolean ret = FALSE;
00353   manager.Add<gboolean, TestSignals*, const char*, int, float, double, gboolean, char>(test_signals_, "signal6",
00354               sigc::mem_fun(this, &TestGLibSignals::Signal6Callback));
00355   g_signal_emit_by_name(test_signals_, "signal6", "test", 100, 1.0f, 100.00, FALSE, 'x', &ret);
00356   EXPECT_TRUE(signal6_received_);
00357   EXPECT_TRUE(ret);
00358 }
00359 
00360 TEST_F(TestGLibSignals, TestManagerAutoDisconnect)
00361 {
00362   {
00363     SignalManager manager;
00364     manager.Add(new Signal<void, TestSignals*>(test_signals_,
00365                                                "signal0",
00366                                                sigc::mem_fun(this, &TestGLibSignals::Signal0Callback)));
00367   }
00368 
00369   g_signal_emit_by_name(test_signals_, "signal0");
00370   EXPECT_FALSE(signal0_received_);
00371 }
00372 
00373 TEST_F(TestGLibSignals, TestManagerDisconnection)
00374 {
00375   SignalManager manager;
00376 
00377   manager.Add(new Signal<void, TestSignals*>(test_signals_,
00378                                              "signal0",
00379                                              sigc::mem_fun(this, &TestGLibSignals::Signal0Callback)));
00380   manager.Disconnect(test_signals_, "signal0");
00381 
00382   g_signal_emit_by_name(test_signals_, "signal0");
00383   EXPECT_FALSE(signal0_received_);
00384 }
00385 
00386 TEST_F(TestGLibSignals, TestManagerObjectDisconnection)
00387 {
00388   SignalManager manager;
00389 
00390   manager.Add(new Signal<void, TestSignals*>(test_signals_,
00391                                              "signal0",
00392                                              sigc::mem_fun(this, &TestGLibSignals::Signal0Callback)));
00393 
00394   manager.Add(new Signal<void, TestSignals*, const char*>(test_signals_,
00395                                                           "signal1",
00396                                                           sigc::mem_fun(this, &TestGLibSignals::Signal1Callback)));
00397 
00398   manager.Disconnect(test_signals_);
00399 
00400   g_signal_emit_by_name(test_signals_, "signal0");
00401   EXPECT_FALSE(signal0_received_);
00402   g_signal_emit_by_name(test_signals_, "signal1", "test");
00403   EXPECT_FALSE(signal1_received_);
00404 }
00405 
00406 }