tests/gui/fire_event.cpp

Go to the documentation of this file.
00001 /* $Id: fire_event.cpp 52869 2012-02-03 20:18:33Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2011 - 2012 by Mark de Wever <koraq@xs4all.nl>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 // In this domain since it compares a shared string from this domain.
00017 #define GETTEXT_DOMAIN "wesnoth-lib"
00018 
00019 #include "config_cache.hpp"
00020 #include "gui/widgets/grid.hpp"
00021 #include "gui/widgets/label.hpp"
00022 
00023 #include <boost/test/unit_test.hpp>
00024 
00025 #include <boost/bind.hpp>
00026 
00027 #include <sstream>
00028 
00029 
00030 static void print(std::stringstream& sstr
00031         , const std::string& queue
00032         , const std::string& id)
00033 {
00034     sstr << queue << ':' << id << '\n';
00035 }
00036 
00037 template<gui2::event::tevent E>
00038 void connect_queue(
00039           std::stringstream& sstr
00040         , gui2::twidget& widget)
00041 {
00042     widget.connect_signal<E>(
00043               boost::bind(print, boost::ref(sstr), "pre", widget.id())
00044             , gui2::event::tdispatcher::back_pre_child);
00045 
00046     widget.connect_signal<E>(
00047               boost::bind(print, boost::ref(sstr), "child", widget.id())
00048             , gui2::event::tdispatcher::back_child);
00049 
00050     widget.connect_signal<E>(
00051               boost::bind(print, boost::ref(sstr), "post", widget.id())
00052             , gui2::event::tdispatcher::back_post_child);
00053 }
00054 
00055 static void connect_signals(
00056           std::stringstream& sstr
00057         , gui2::twidget& widget)
00058 {
00059     /** @todo Add the rest of the events. */
00060     connect_queue<gui2::event::DRAW>(sstr, widget);
00061     connect_queue<gui2::event::CLOSE_WINDOW>(sstr, widget);
00062     connect_queue<gui2::event::MOUSE_ENTER>(sstr, widget);
00063     connect_queue<gui2::event::MOUSE_LEAVE>(sstr, widget);
00064     connect_queue<gui2::event::LEFT_BUTTON_DOWN>(sstr, widget);
00065     connect_queue<gui2::event::LEFT_BUTTON_UP>(sstr, widget);
00066     connect_queue<gui2::event::LEFT_BUTTON_CLICK>(sstr, widget);
00067     connect_queue<gui2::event::LEFT_BUTTON_DOUBLE_CLICK>(sstr, widget);
00068     connect_queue<gui2::event::MIDDLE_BUTTON_DOWN>(sstr, widget);
00069     connect_queue<gui2::event::MIDDLE_BUTTON_UP>(sstr, widget);
00070     connect_queue<gui2::event::MIDDLE_BUTTON_CLICK>(sstr, widget);
00071     connect_queue<gui2::event::MIDDLE_BUTTON_DOUBLE_CLICK>(sstr, widget);
00072     connect_queue<gui2::event::RIGHT_BUTTON_DOWN>(sstr, widget);
00073     connect_queue<gui2::event::RIGHT_BUTTON_UP>(sstr, widget);
00074     connect_queue<gui2::event::RIGHT_BUTTON_CLICK>(sstr, widget);
00075     connect_queue<gui2::event::RIGHT_BUTTON_DOUBLE_CLICK>(sstr, widget);
00076 }
00077 
00078 static void add_widget(gui2::tgrid& grid
00079         , gui2::twidget* widget
00080         , const std::string& id
00081         , const unsigned row
00082         , const unsigned column)
00083 {
00084     BOOST_REQUIRE_NE(widget, static_cast<gui2::twidget*>(NULL));
00085 
00086     widget->set_id(id);
00087     grid.set_child(widget
00088             , row
00089             , column
00090             , gui2::tgrid::VERTICAL_GROW_SEND_TO_CLIENT
00091                 | gui2::tgrid::HORIZONTAL_GROW_SEND_TO_CLIENT
00092             , 0);
00093 }
00094 
00095 static std::string set_event_order()
00096 {
00097     return
00098             "pre:root\n"
00099             "pre:level 1\n"
00100             "child:level 2\n"
00101             "post:level 1\n"
00102             "post:root\n";
00103 
00104 }
00105 
00106 /** @todo Add the rest of the events. */
00107 static void validate_draw(std::stringstream& sstr)
00108 {
00109     BOOST_CHECK_EQUAL(sstr.str(), set_event_order());
00110 }
00111 
00112 static void validate_right_button_down(std::stringstream& sstr)
00113 {
00114     BOOST_CHECK_EQUAL(sstr.str(), set_event_order());
00115 }
00116 
00117 BOOST_AUTO_TEST_CASE(test_fire_event)
00118 {
00119     /**** Initialize the environment. *****/
00120     game_config::config_cache& cache = game_config::config_cache::instance();
00121 
00122     cache.clear_defines();
00123     cache.add_define("EDITOR");
00124     cache.add_define("MULTIPLAYER");
00125 
00126     std::stringstream sstr;
00127 
00128     /**** Initialize the grid. *****/
00129     gui2::tgrid grid(1, 1);
00130     grid.set_id("root");
00131     connect_signals(sstr, grid);
00132 
00133     gui2::tgrid *child_grid = new gui2::tgrid(1, 1);
00134     add_widget(grid, child_grid, "level 1", 0, 0);
00135     connect_signals(sstr, *child_grid);
00136 
00137     gui2::twidget *child = new gui2::tgrid(1, 1);
00138     add_widget(*child_grid, child, "level 2", 0, 0);
00139     connect_signals(sstr, *child);
00140 
00141     /** @todo Add the rest of the events. */
00142     sstr.str("");
00143     grid.fire(gui2::event::DRAW, *child);
00144     validate_draw(sstr);
00145 
00146     sstr.str("");
00147     grid.fire(gui2::event::RIGHT_BUTTON_DOWN, *child);
00148     validate_right_button_down(sstr);
00149 }
00150 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Thu May 24 2012 01:02:53 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs