tests/gui/iterator.cpp

Go to the documentation of this file.
00001 /* $Id: iterator.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 #define GETTEXT_DOMAIN "wesnoth-lib"
00017 
00018 #include <boost/test/unit_test.hpp>
00019 
00020 #include "config_cache.hpp"
00021 #include "gui/auxiliary/iterator/iterator.hpp"
00022 #include "gui/widgets/label.hpp"
00023 #include "gui/widgets/grid.hpp"
00024 
00025 #include <iostream>
00026 #include <sstream>
00027 #include <typeinfo>
00028 
00029 /*
00030  * In the unit tests we use a widget tree that looks like:
00031  *
00032  * [0]
00033  *  \
00034  *   [1|2|3|4]
00035  *    \
00036  *    [5|6|7|8]
00037  *
00038  * Where widgets 0 and 1 are a grid and the rest of the widgets a label.
00039  * The unit tests traverse the tree.
00040  */
00041 
00042 static std::string top_down_t_t_t_result()
00043 {
00044     static const std::string result =
00045         "At '0'. Iterate widget: reached the end. Iterate grid: failed. "
00046             "Iterate child: proceed. Down and visit '1'.\n"
00047         "At '1'. Iterate widget: reached the end. Iterate grid: failed. "
00048             "Iterate child: proceed. Down and visit '5'.\n"
00049         "At '5'. Iterate widget: reached the end. Iterate grid: failed. "
00050             "Iterate child: reached the end. Up widget '5'. "
00051             "Iterate: reached '6'. Down and visit '6'.\n"
00052         "At '6'. Iterate widget: reached the end. Iterate grid: failed. "
00053             "Iterate child: reached the end. Up widget '6'. "
00054             "Iterate: reached '7'. Down and visit '7'.\n"
00055         "At '7'. Iterate widget: reached the end. Iterate grid: failed. "
00056             "Iterate child: reached the end. Up widget '7'. "
00057             "Iterate: reached '8'. Down and visit '8'.\n"
00058         "At '8'. Iterate widget: reached the end. Iterate grid: failed. "
00059             "Iterate child: reached the end. Up widget '8'. "
00060             "Iterate: failed. Up widget '1'. "
00061             "Iterate: reached '2'. Down and visit '2'.\n"
00062         "At '2'. Iterate widget: reached the end. Iterate grid: failed. "
00063             "Iterate child: reached the end. Up widget '2'. "
00064             "Iterate: reached '3'. Down and visit '3'.\n"
00065         "At '3'. Iterate widget: reached the end. Iterate grid: failed. "
00066             "Iterate child: reached the end. Up widget '3'. "
00067             "Iterate: reached '4'. Down and visit '4'.\n"
00068         "At '4'. Iterate widget: reached the end. Iterate grid: failed. "
00069             "Iterate child: reached the end. Up widget '4'. "
00070             "Iterate: failed. Finished iteration.\n";
00071 
00072     return result;
00073 }
00074 
00075 static std::string bottom_up_t_t_t_result()
00076 {
00077     static const std::string result =
00078         "Constructor:  Down widget '1'. Down widget '5'. Finished at '5'.\n"
00079         "At '5'. Iterate widget: reached the end. Iterate grid: failed. "
00080             "Iterate child: Up '1'. Iterate child: visit '1'. "
00081             "Down widget '6'. Visit '6'.\n"
00082         "At '6'. Iterate widget: reached the end. Iterate grid: failed. "
00083             "Iterate child: Up '1'. Iterate child: visit '1'. "
00084             "Down widget '7'. Visit '7'.\n"
00085         "At '7'. Iterate widget: reached the end. Iterate grid: failed. "
00086             "Iterate child: Up '1'. Iterate child: visit '1'. "
00087             "Down widget '8'. Visit '8'.\n"
00088         "At '8'. Iterate widget: reached the end. Iterate grid: failed. "
00089             "Iterate child: Up '1'. Iterate child: reached the end. Visit '1'.\n"
00090         "At '1'. Iterate widget: reached the end. Iterate grid: failed. "
00091             "Iterate child: Up '0'. Iterate child: visit '0'. "
00092             "Down widget '2'. Visit '2'.\n"
00093         "At '2'. Iterate widget: reached the end. Iterate grid: failed. "
00094             "Iterate child: Up '0'. Iterate child: visit '0'. "
00095             "Down widget '3'. Visit '3'.\n"
00096         "At '3'. Iterate widget: reached the end. Iterate grid: failed. "
00097             "Iterate child: Up '0'. Iterate child: visit '0'. "
00098             "Down widget '4'. Visit '4'.\n"
00099         "At '4'. Iterate widget: reached the end. Iterate grid: failed. "
00100             "Iterate child: Up '0'. Iterate child: reached the end. Visit '0'.\n"
00101         "At '0'. Iterate widget: reached the end. Iterate grid: failed. "
00102             "Iterate child: Finished iteration.\n";
00103 
00104     return result;
00105 }
00106 
00107 static void add_widget(gui2::tgrid& grid
00108         , gui2::twidget* widget
00109         , const std::string& id
00110         , const unsigned row
00111         , const unsigned column)
00112 {
00113     BOOST_REQUIRE_NE(widget, static_cast<gui2::twidget*>(NULL));
00114 
00115     widget->set_id(id);
00116     grid.set_child(widget
00117             , row
00118             , column
00119             , gui2::tgrid::VERTICAL_GROW_SEND_TO_CLIENT
00120                 | gui2::tgrid::HORIZONTAL_GROW_SEND_TO_CLIENT
00121             , 0);
00122 }
00123 
00124 template<class T>
00125 static void test_control()
00126 {
00127     T control;
00128 
00129     {
00130         gui2::iterator::titerator< gui2::iterator::policy::order::ttop_down<
00131                 true
00132                 , true
00133                 , true> >
00134             iterator(control);
00135 
00136         /***** INITIAL STATE *****/
00137 
00138         BOOST_CHECK_EQUAL(iterator.at_end(), false);
00139 
00140         BOOST_CHECK_EQUAL(&*iterator, &control);
00141 
00142         /***** POST END *****/
00143 
00144         BOOST_CHECK_EQUAL(iterator.next(), false);
00145 
00146         BOOST_CHECK_EQUAL(iterator.at_end(), true);
00147 
00148     }
00149     {
00150         gui2::iterator::titerator< gui2::iterator::policy::order::ttop_down<
00151                 false
00152                 , true
00153                 , true> >
00154             iterator(control);
00155 
00156         /***** INITIAL STATE *****/
00157 
00158         BOOST_CHECK_EQUAL(iterator.at_end(), true);
00159     }
00160 
00161     {
00162         gui2::iterator::titerator<gui2::iterator::policy::order::tbottom_up<true, true, true> > iterator(control);
00163         BOOST_CHECK_EQUAL(iterator.at_end(), false);
00164     }
00165     {
00166         gui2::iterator::titerator<gui2::iterator::policy::order::tbottom_up<false, false, false> > iterator(control);
00167         BOOST_CHECK_EQUAL(iterator.at_end(), true);
00168     }
00169 }
00170 
00171 static void test_control()
00172 {
00173     /* Could add more widgets to the list. */
00174     test_control<gui2::tlabel>();
00175 
00176 }
00177 
00178 static void test_grid()
00179 {
00180     /* An empty grid behaves the same as a control so test here. */
00181     test_control<gui2::tgrid>();
00182 
00183     /* Test the child part here. */
00184     gui2::tgrid grid(2 ,2);
00185     grid.set_id("0");
00186 
00187     gui2::tgrid* g = new gui2::tgrid(2, 2);
00188     add_widget(grid, g, "1", 0, 0);
00189     add_widget(grid, new gui2::tlabel(), "2", 1, 0);
00190     add_widget(grid, new gui2::tlabel(), "3", 0, 1);
00191     add_widget(grid, new gui2::tlabel(), "4", 1, 1);
00192 
00193     add_widget(*g, new gui2::tlabel(), "5", 0, 0);
00194     add_widget(*g, new gui2::tlabel(), "6", 1, 0);
00195     add_widget(*g, new gui2::tlabel(), "7", 0, 1);
00196     add_widget(*g, new gui2::tlabel(), "8", 1, 1);
00197 
00198     {
00199         std::stringstream sstr;
00200         lg::tredirect_output_setter redirect_output(sstr);
00201 
00202         gui2::iterator::titerator<gui2::iterator::policy::order::ttop_down<
00203                 true
00204                 , true
00205                 , true> >
00206             iterator(grid);
00207 
00208         while(iterator.next()) {
00209             /* DO NOTHING */
00210         }
00211 
00212         BOOST_CHECK_EQUAL(top_down_t_t_t_result(), sstr.str());
00213     }
00214     {
00215         std::stringstream sstr;
00216         lg::tredirect_output_setter redirect_output(sstr);
00217 
00218         gui2::iterator::titerator<gui2::iterator::policy::order::ttop_down<
00219                 true
00220                 , true
00221                 , true> >
00222             iterator(grid);
00223 
00224         for( ; !iterator.at_end(); ++iterator) {
00225             /* DO NOTHING */
00226         }
00227 
00228         BOOST_CHECK_EQUAL(top_down_t_t_t_result(), sstr.str());
00229     }
00230     {
00231         std::stringstream sstr;
00232         lg::tredirect_output_setter redirect_output(sstr);
00233 
00234         gui2::iterator::titerator<gui2::iterator::policy::order::tbottom_up<
00235                 true
00236                 , true
00237                 , true> >
00238             iterator(grid);
00239 
00240         while(iterator.next()) {
00241             /* DO NOTHING */
00242         }
00243 
00244         BOOST_CHECK_EQUAL(bottom_up_t_t_t_result(), sstr.str());
00245     }
00246     {
00247         std::stringstream sstr;
00248         lg::tredirect_output_setter redirect_output(sstr);
00249 
00250         gui2::iterator::titerator<gui2::iterator::policy::order::tbottom_up<
00251                 true
00252                 , true
00253                 , true> >
00254             iterator(grid);
00255 
00256         for( ; !iterator.at_end(); ++iterator) {
00257             /* DO NOTHING */
00258         }
00259 
00260         BOOST_CHECK_EQUAL(bottom_up_t_t_t_result(), sstr.str());
00261     }
00262 }
00263 
00264 BOOST_AUTO_TEST_CASE(test_gui2_iterator)
00265 {
00266     /**** Initialize the environment. *****/
00267     game_config::config_cache& cache = game_config::config_cache::instance();
00268 
00269     cache.clear_defines();
00270     cache.add_define("EDITOR");
00271     cache.add_define("MULTIPLAYER");
00272 
00273     lg::set_log_domain_severity("gui/iterator", 3); // FIXME get_severity or something like it
00274     lg::timestamps(false);
00275 
00276     std::stringstream sstr;
00277     lg::tredirect_output_setter redirect_output(sstr);
00278 
00279     test_control();
00280     test_grid();
00281 }
00282 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:54 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs