listeners_unittest.cpp

Go to the documentation of this file.
00001 /*
00002  * File Name: listeners_unittest.cpp
00003  */
00004 
00005 /*
00006  * This file is part of uds-plugin-images.
00007  *
00008  * uds-plugin-images is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * uds-plugin-images is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 #include <cassert>
00028 #include <algorithm>
00029 #include <tut.h>
00030 #include <tut_reporter.h>
00031 #include "listeners.h"
00032 #include "plugin_inc.h"
00033 #include "log.h"
00034 
00035 namespace
00036 {
00037 
00038     typedef std::vector<unsigned long> id_vector;
00039     typedef std::vector<unsigned long>::iterator id_vector_iter;
00040 
00041 class A
00042 {
00043 public:
00044     A(){}
00045     ~A(){}
00046 
00047 public:
00048     static void callback( IPluginUnknown          *thiz_sender,
00049                           unsigned long           handler_id,
00050                           const PluginEvent       plugin_event,
00051                           void                    *user_data,
00052                           const PluginEventAttrs  *plugin_data )
00053     {
00054         LOGPRINTF("Callback of A instance %p type %d", 
00055                   user_data, plugin_event); 
00056     }
00057     
00058     void add_id(const unsigned long id)
00059     {
00060         ids.push_back(id);
00061     }
00062 
00063     void remove_id(const unsigned long id)
00064     {
00065         id_vector_iter it = std::find(ids.begin(), ids.end(), id);
00066         if (it != ids.end())
00067         {
00068             ids.erase(it);
00069         }
00070     }
00071 
00072     unsigned long front()
00073     {
00074         return ids.front();
00075     }
00076 
00077 private:
00078     id_vector ids;  
00079 };
00080 
00081 class B
00082 {
00083 public:
00084     B(){}
00085     ~B(){}
00086 
00087 public:
00088     static void callback( IPluginUnknown          *thiz_sender,
00089                           unsigned long           handler_id,
00090                           const PluginEvent       plugin_event,
00091                           void                    *user_data,
00092                           const PluginEventAttrs  *plugin_data )
00093     {
00094         LOGPRINTF("Callback of B instance %p  type %d",
00095                    user_data, plugin_event);
00096     }
00097 };
00098 
00099 
00100 };  // anonymous namespace
00101 
00102 namespace tut
00103 {
00104     struct empty
00105     { 
00106     };
00107 
00108     typedef test_group<empty> tf;
00109     typedef tf::object object;
00110     tf listeners_group("listeners test");
00111 
00112     // Test 1: Basic test.
00113     template<>
00114     template<>
00115     void object::test<1>()
00116     {
00117         using namespace utils;
00118         Listeners container;
00119         A a;            
00120         B b;
00121         unsigned long id;
00122 
00123         id = container.add_listener(EVENT_PAGINATE_START, A::callback, &a);
00124         a.add_id(id);
00125         id = container.add_listener(EVENT_PAGINATE_END, A::callback, &a);
00126         a.add_id(id);        
00127 
00128         id =  container.add_listener(EVENT_PAGINATE_START, B::callback, &b);
00129 
00130         for(int e = EVENT_PAGINATE_START;
00131             e != EVENT_MARKER_OUT_OF_DATE;
00132             ++e)
00133         {
00134             container.broadcast(0, static_cast<PluginEvent>(e), 0);
00135         }
00136     
00137         container.remove_listener(id);
00138         container.remove_listener(a.front());
00139 
00140         container.broadcast(0, EVENT_PAGINATE_START, 0);
00141     }
00142 
00143 };
00144 
00145 
00146 using std::exception;
00147 using std::cerr;
00148 using std::endl;
00149 
00150 namespace tut
00151 {
00152     test_runner_singleton runner;
00153 }
00154 
00155 int main()
00156 {
00157     tut::reporter reporter;
00158     tut::runner.get().set_callback(&reporter);
00159 
00160     try
00161     {
00162         tut::runner.get().run_tests();
00163     }
00164     catch (const std::exception& ex)
00165     {
00166         cerr << "tut raised ex: " << ex.what() << endl;
00167         return 1;
00168     }
00169 
00170     while(1);
00171     return 0;
00172 }
00173 
00174 
00175 
Generated by  doxygen 1.6.2-20100208