The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
visitor.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
17 #include <boost/test/unit_test.hpp>
18 
19 #include "config_cache.hpp"
21 #include "gui/widgets/label.hpp"
22 #include "gui/widgets/grid.hpp"
23 
24 #include <iostream>
25 #include <typeinfo>
26 
28  , gui2::twidget* widget
29  , const std::string& id
30  , const unsigned row
31  , const unsigned column)
32 {
33  BOOST_REQUIRE_NE(widget, static_cast<gui2::twidget*>(nullptr));
34 
35  widget->set_id(id);
36  grid.set_child(widget
37  , row
38  , column
41  , 0);
42 }
43 
44 template<class T>
45 static void test_control()
46 {
47  //std::cerr << __func__ << ": " << typeid(T).name() << ".\n";
48 
49  T control;
50  boost::scoped_ptr<gui2::iterator::twalker_> visitor(control.create_walker());
51 
52  BOOST_REQUIRE_NE(visitor.get(), static_cast<void*>(nullptr));
53 
54  /***** INITIAL STATE *****/
55 
56  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iterator::twalker_::widget), false);
57  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iterator::twalker_::grid), true);
58  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iterator::twalker_::child), true);
59 
60  BOOST_CHECK_EQUAL(visitor->get(gui2::iterator::twalker_::widget), &control);
61  BOOST_CHECK_EQUAL(visitor->get(gui2::iterator::twalker_::grid), static_cast<void*>(nullptr));
62  BOOST_CHECK_EQUAL(visitor->get(gui2::iterator::twalker_::child), static_cast<void*>(nullptr));
63 
64  /***** VISITING WIDGET *****/
65 
67  BOOST_CHECK_EQUAL(visitor->next(gui2::iterator::twalker_::grid), gui2::iterator::twalker_::fail);
68  BOOST_CHECK_EQUAL(visitor->next(gui2::iterator::twalker_::child), gui2::iterator::twalker_::fail);
69 
70  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iterator::twalker_::widget), true);
71  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iterator::twalker_::grid), true);
72  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iterator::twalker_::child), true);
73 
74  BOOST_CHECK_EQUAL(visitor->get(gui2::iterator::twalker_::widget), static_cast<void*>(nullptr));
75  BOOST_CHECK_EQUAL(visitor->get(gui2::iterator::twalker_::grid), static_cast<void*>(nullptr));
76  BOOST_CHECK_EQUAL(visitor->get(gui2::iterator::twalker_::child), static_cast<void*>(nullptr));
77 
78  /***** POST END *****/
79 
80  BOOST_CHECK_EQUAL(visitor->next(gui2::iterator::twalker_::widget), gui2::iterator::twalker_::fail);
81 }
82 
83 static void test_control()
84 {
85  /* Could add more widgets to the list. */
86  test_control<gui2::tlabel>();
87 
88 }
89 
90 static void test_grid()
91 {
92  /* An empty grid behaves the same as a control so test here. */
93  test_control<gui2::tgrid>();
94 
95  //std::cerr << __func__ << ": Detailed test.\n";
96 
97  /* Test the child part here. */
98  gui2::tgrid grid(2 ,2);
99  add_widget(grid, new gui2::tlabel(), "(1,1)", 0, 0);
100  add_widget(grid, new gui2::tlabel(), "(1,2)", 0, 1);
101  add_widget(grid, new gui2::tlabel(), "(2,1)", 1, 0);
102  add_widget(grid, new gui2::tlabel(), "(2,2)", 1, 1);
103 
104  boost::scoped_ptr<gui2::iterator::twalker_> visitor(grid.create_walker());
105 
106  /***** LABEL 1,1 *****/
107 
108  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iterator::twalker_::child), false);
109 
110  BOOST_REQUIRE_NE(visitor->get(gui2::iterator::twalker_::child), static_cast<void*>(nullptr));
111  BOOST_CHECK_EQUAL(visitor->get(gui2::iterator::twalker_::child)->id(), "(1,1)");
112 
113  /***** LABEL 2,1 *****/
114 
115  BOOST_CHECK_EQUAL(visitor->next(gui2::iterator::twalker_::child), gui2::iterator::twalker_::valid);
116 
117  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iterator::twalker_::child), false);
118 
119  BOOST_REQUIRE_NE(visitor->get(gui2::iterator::twalker_::child), static_cast<void*>(nullptr));
120  BOOST_CHECK_EQUAL(visitor->get(gui2::iterator::twalker_::child)->id(), "(2,1)");
121 
122  /***** LABEL 1,2 *****/
123 
124  BOOST_CHECK_EQUAL(visitor->next(gui2::iterator::twalker_::child), gui2::iterator::twalker_::valid);
125 
126  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iterator::twalker_::child), false);
127 
128  BOOST_REQUIRE_NE(visitor->get(gui2::iterator::twalker_::child), static_cast<void*>(nullptr));
129  BOOST_CHECK_EQUAL(visitor->get(gui2::iterator::twalker_::child)->id(), "(1,2)");
130 
131  /***** LABEL 2,2 *****/
132 
133  BOOST_CHECK_EQUAL(visitor->next(gui2::iterator::twalker_::child), gui2::iterator::twalker_::valid);
134 
135  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iterator::twalker_::child), false);
136 
137  BOOST_REQUIRE_NE(visitor->get(gui2::iterator::twalker_::child), static_cast<void*>(nullptr));
138  BOOST_CHECK_EQUAL(visitor->get(gui2::iterator::twalker_::child)->id(), "(2,2)");
139 
140  /***** END *****/
141 
142  BOOST_CHECK_EQUAL(visitor->next(gui2::iterator::twalker_::child), gui2::iterator::twalker_::invalid);
143 
144  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iterator::twalker_::child), true);
145 
146  BOOST_CHECK_EQUAL(visitor->get(gui2::iterator::twalker_::child), static_cast<void*>(nullptr));
147 
148  /***** POST END *****/
149 
150  BOOST_CHECK_EQUAL(visitor->next(gui2::iterator::twalker_::child), gui2::iterator::twalker_::fail);
151 }
152 
153 BOOST_AUTO_TEST_CASE(test_gui2_visitor)
154 {
155 
156  /**** Initialize the environment. *****/
158 
159  cache.clear_defines();
160  cache.add_define("EDITOR");
161  cache.add_define("MULTIPLAYER");
162 
163  test_control();
164  test_grid();
165 }
166 
167 
Visit the widget itself.
Definition: walker.hpp:37
static config_cache & instance()
Get reference to the singleton object.
Base container class.
Definition: grid.hpp:29
static void add_widget(gui2::tgrid &grid, gui2::twidget *widget, const std::string &id, const unsigned row, const unsigned column)
Definition: visitor.cpp:27
static const unsigned VERTICAL_GROW_SEND_TO_CLIENT
Definition: grid.hpp:41
static const unsigned HORIZONTAL_GROW_SEND_TO_CLIENT
Definition: grid.hpp:48
Label showing a text.
Definition: label.hpp:29
void set_id(const std::string &id)
Definition: widget.cpp:97
When calling next the following it has the following results.
Definition: walker.hpp:60
GLenum GLenum GLvoid GLvoid * column
Definition: glew.h:3805
void clear_defines()
Clear stored defines map to default values.
When calling next the following it has the following results.
Definition: walker.hpp:71
static void test_grid()
Definition: visitor.cpp:90
static tcache cache
Definition: minimap.cpp:139
static void test_control()
Definition: visitor.cpp:45
GLenum GLenum GLvoid * row
Definition: glew.h:3805
Base class for all widgets.
Definition: widget.hpp:49
void set_child(twidget *widget, const unsigned row, const unsigned col, const unsigned flags, const unsigned border_size)
Sets a child in the grid.
Definition: grid.cpp:69
BOOST_AUTO_TEST_CASE(test_gui2_visitor)
Definition: visitor.cpp:153
void add_define(const std::string &define)
Add a entry to preproc defines map.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Visit the children of its nested grid.
Definition: walker.hpp:40
Singleton class to manage game config file caching.