00001 // -*- C++ -*- 00012 /* 00013 * $Log$ 00014 * 00015 */ 00016 00017 #ifndef Listener_cpp 00018 #define Listener_cpp 00019 00020 #include <iostream> 00021 #include <iomanip> 00022 #include <string> 00023 #include <stdio.h> 00024 //#include <unistd.h> 00025 #include <cppunit/ui/text/TestRunner.h> 00026 #include <cppunit/TextOutputter.h> 00027 #include <cppunit/extensions/TestFactoryRegistry.h> 00028 #include <cppunit/extensions/HelperMacros.h> 00029 #include <cppunit/TestAssert.h> 00030 00031 #include <coil/Listener.h> 00032 00037 namespace Listener 00038 { 00039 short g_iCallBackFlag; 00040 /* 00041 --------------------------------------------------------------------------- 00042 --------------------------------------------------------------------------- 00043 */ 00044 void CallBackFunc() 00045 { 00046 g_iCallBackFlag = 1; 00047 } 00048 class CallBackTestClass 00049 { 00050 private: 00051 short m_callbackflag; 00052 public: 00053 CallBackTestClass() 00054 { 00055 m_callbackflag = 0; 00056 } 00057 void callbackfunc() 00058 { 00059 m_callbackflag = 1; 00060 } 00061 short getCallBackFuncStat() 00062 { 00063 return m_callbackflag; 00064 } 00065 }; 00066 class ListenerTests 00067 : public CppUnit::TestFixture 00068 { 00069 CPPUNIT_TEST_SUITE(ListenerTests); 00070 CPPUNIT_TEST(test_ListenerObject); 00071 CPPUNIT_TEST(test_ListenerObject2); 00072 CPPUNIT_TEST(test_ListenerObject3); 00073 CPPUNIT_TEST(test_ListenerFunc); 00074 CPPUNIT_TEST_SUITE_END(); 00075 00076 private: 00077 short m_callbackflag; 00078 public: 00079 class TestClassMember 00080 { 00081 private: 00082 short m_callbackflag; 00083 public: 00084 TestClassMember() 00085 { 00086 m_callbackflag = 0; 00087 } 00088 void callbackfunc() 00089 { 00090 m_callbackflag = 1; 00091 } 00092 short getCallBackFuncStat() 00093 { 00094 return m_callbackflag; 00095 } 00096 00097 }; 00098 00102 ListenerTests() 00103 { 00104 } 00105 00109 ~ListenerTests() 00110 { 00111 } 00112 00116 virtual void setUp() 00117 { 00118 } 00119 00123 virtual void tearDown() 00124 { 00125 } 00126 00127 /* test case */ 00128 /* 00129 --------------------------------------------------------------------------- 00130 --------------------------------------------------------------------------- 00131 */ 00132 void callbackThis() 00133 { 00134 m_callbackflag = 1; 00135 } 00136 00137 /* 00138 --------------------------------------------------------------------------- 00139 This function tests coil::ListenerObject. 00140 Check that the registered callback function is called. 00141 --------------------------------------------------------------------------- 00142 */ 00143 void test_ListenerObject() 00144 { 00145 short istat; 00146 TestClassMember * tc; 00147 tc = new TestClassMember; 00148 ListenerObject<TestClassMember> lo(tc,&TestClassMember::callbackfunc) ; 00149 lo.invoke(); 00150 istat = tc->getCallBackFuncStat(); 00151 CPPUNIT_ASSERT_MESSAGE("ListenerObject", (istat == 1) ); 00152 delete tc; 00153 } 00154 /* 00155 --------------------------------------------------------------------------- 00156 This function tests coil::ListenerObject. 00157 Check that the registered callback function is called. 00158 --------------------------------------------------------------------------- 00159 */ 00160 void test_ListenerObject2() 00161 { 00162 short istat; 00163 CallBackTestClass * tc; 00164 tc = new CallBackTestClass; 00165 ListenerObject<CallBackTestClass> lo(tc,&CallBackTestClass::callbackfunc) ; 00166 lo.invoke(); 00167 istat = tc->getCallBackFuncStat(); 00168 CPPUNIT_ASSERT_MESSAGE("ListenerObject", (istat == 1) ); 00169 delete tc; 00170 } 00171 /* 00172 --------------------------------------------------------------------------- 00173 This function tests coil::ListenerObject. 00174 Check that the registered callback function is called. 00175 --------------------------------------------------------------------------- 00176 */ 00177 void test_ListenerObject3() 00178 { 00179 m_callbackflag = 0; 00180 ListenerObject<ListenerTests> lo(this,&ListenerTests::callbackThis) ; 00181 lo.invoke(); 00182 CPPUNIT_ASSERT_MESSAGE("ListenerObject", (m_callbackflag == 1) ); 00183 } 00184 /* 00185 --------------------------------------------------------------------------- 00186 This function tests coil::ListenerFunc. 00187 Check that the registered callback function is called. 00188 --------------------------------------------------------------------------- 00189 */ 00190 void test_ListenerFunc() 00191 { 00192 g_iCallBackFlag = 0; 00193 ListenerFunc lf(&CallBackFunc) ; 00194 lf.invoke(); 00195 CPPUNIT_ASSERT_MESSAGE("ListenerFunc", (g_iCallBackFlag == 1) ); 00196 } 00197 }; 00198 }; // namespace Listener 00199 00200 /* 00201 * Register test suite 00202 */ 00203 CPPUNIT_TEST_SUITE_REGISTRATION(Listener::ListenerTests); 00204 00205 #ifdef LOCAL_MAIN 00206 int main(int argc, char* argv[]) 00207 { 00208 CppUnit::TextUi::TestRunner runner; 00209 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); 00210 CppUnit::Outputter* outputter = 00211 new CppUnit::TextOutputter(&runner.result(), std::cout); 00212 runner.setOutputter(outputter); 00213 bool retcode = runner.run(); 00214 return !retcode; 00215 } 00216 #endif // MAIN 00217 #endif // Listener_cpp