The Battle for Wesnoth  1.19.5+dev
visitor.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2024
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
18 #include <boost/test/unit_test.hpp>
19 
20 #include "config_cache.hpp"
22 #include "gui/widgets/label.hpp"
23 #include "gui/widgets/grid.hpp"
24 
25 #include <typeinfo>
26 
27 static void add_widget(gui2::grid& grid
28  , std::unique_ptr<gui2::widget> widget
29  , const std::string& id
30  , const unsigned row
31  , const unsigned column)
32 {
33  BOOST_REQUIRE_NE(widget.get(), static_cast<gui2::widget*>(nullptr));
34 
35  widget->set_id(id);
36  grid.set_child(std::move(widget)
37  , row
38  , column
41  , 0);
42 }
43 
44 template<class T>
45 static void test_control(T&& control)
46 {
47  //PLAIN_LOG << __func__ << ": " << typeid(T).name() << ".";
48 
49  const auto visitor = control.create_walker();
50 
51  BOOST_REQUIRE_NE(visitor.get(), static_cast<void*>(nullptr));
52 
53  /***** INITIAL STATE *****/
54 
55  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::self), false);
56  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::internal), true);
57  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), true);
58 
59  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::self), &control);
60  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::internal), static_cast<void*>(nullptr));
61  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
62 
63  /***** VISITING WIDGET *****/
64 
68 
69  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::self), true);
70  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::internal), true);
71  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), true);
72 
73  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::self), static_cast<void*>(nullptr));
74  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::internal), static_cast<void*>(nullptr));
75  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
76 
77  /***** POST END *****/
78 
80 }
81 
82 static void test_control()
83 {
84  /* Could add more widgets to the list. */
86 }
87 
88 static void test_grid()
89 {
90  /* An empty grid behaves the same as a control so test here. */
92 
93  //PLAIN_LOG << __func__ << ": Detailed test.";
94 
95  /* Test the child part here. */
96  gui2::grid grid(2 ,2);
97  add_widget(grid, std::make_unique<gui2::label>(gui2::implementation::builder_label(config())), "(1,1)", 0, 0);
98  add_widget(grid, std::make_unique<gui2::label>(gui2::implementation::builder_label(config())), "(1,2)", 0, 1);
99  add_widget(grid, std::make_unique<gui2::label>(gui2::implementation::builder_label(config())), "(2,1)", 1, 0);
100  add_widget(grid, std::make_unique<gui2::label>(gui2::implementation::builder_label(config())), "(2,2)", 1, 1);
101 
102  const auto visitor = grid.create_walker();
103 
104  /***** LABEL 1,1 *****/
105 
106  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), false);
107 
108  BOOST_REQUIRE_NE(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
109  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child)->id(), "(1,1)");
110 
111  /***** LABEL 2,1 *****/
112 
114 
115  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), false);
116 
117  BOOST_REQUIRE_NE(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
118  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child)->id(), "(2,1)");
119 
120  /***** LABEL 1,2 *****/
121 
123 
124  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), false);
125 
126  BOOST_REQUIRE_NE(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
127  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child)->id(), "(1,2)");
128 
129  /***** LABEL 2,2 *****/
130 
132 
133  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), false);
134 
135  BOOST_REQUIRE_NE(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
136  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child)->id(), "(2,2)");
137 
138  /***** END *****/
139 
141 
142  BOOST_CHECK_EQUAL(visitor->at_end(gui2::iteration::walker_base::child), true);
143 
144  BOOST_CHECK_EQUAL(visitor->get(gui2::iteration::walker_base::child), static_cast<void*>(nullptr));
145 
146  /***** POST END *****/
147 
149 }
150 
151 BOOST_AUTO_TEST_CASE(test_gui2_visitor)
152 {
153 
154  /**** Initialize the environment. *****/
156 
157  cache.clear_defines();
158  cache.add_define("EDITOR");
159  cache.add_define("MULTIPLAYER");
160 
161  test_control();
162  test_grid();
163 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
Singleton class to manage game config file caching.
void add_define(const std::string &define)
Add a entry to preproc defines map.
static config_cache & instance()
Get reference to the singleton object.
void clear_defines()
Clear stored defines map to default values.
Base container class.
Definition: grid.hpp:32
static const unsigned HORIZONTAL_GROW_SEND_TO_CLIENT
Definition: grid.hpp:56
virtual iteration::walker_ptr create_walker() override
See widget::create_walker.
Definition: grid.cpp:689
static const unsigned VERTICAL_GROW_SEND_TO_CLIENT
Definition: grid.hpp:49
void set_child(std::unique_ptr< widget > widget, const unsigned row, const unsigned col, const unsigned flags, const unsigned border_size)
Sets a child in the grid.
Definition: grid.cpp:71
@ internal
Visit its nested grid.
Definition: walker.hpp:39
@ self
Visit the widget itself.
Definition: walker.hpp:37
@ child
Visit the children of its nested grid.
Definition: walker.hpp:41
@ invalid
When calling next the following it has the following results.
Definition: walker.hpp:69
@ valid
When calling next the following it has the following results.
Definition: walker.hpp:58
Base class for all widgets.
Definition: widget.hpp:55
BOOST_AUTO_TEST_CASE(test_gui2_visitor)
Definition: visitor.cpp:151
static void test_control(T &&control)
Definition: visitor.cpp:45
static void add_widget(gui2::grid &grid, std::unique_ptr< gui2::widget > widget, const std::string &id, const unsigned row, const unsigned column)
Definition: visitor.cpp:27
static void test_grid()
Definition: visitor.cpp:88